FileDocCategorySizeDatePackage
GetMark.javaAPI DocExample2616Sat Nov 25 12:54:14 GMT 2000None

GetMark

public class GetMark extends Object
GetMark -- get marked lines. In this version, the marks are hard-coded; ideally they would come from a FileProperties object.
author
Ian F. Darwin, ian@darwinsys.com
version
$Id: GetMark.java,v 1.11 2000/11/25 17:54:14 ian Exp $

Fields Summary
public final String
startMark
the default starting mark.
public final String
endMark
the default ending mark.
protected boolean
printing
True if we are currently inside marks.
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] av)
This simple main program looks after filenames and opening files and such like for you.

        GetMark o = new GetMark();
		PrintStream pw = new PrintStream(System.out);
        if (av.length == 0) {
            o.process("standard input", new LineNumberReader(
				new InputStreamReader(System.in)), pw);
		} else {
			for (int i=0; i<av.length; i++)
				try {
					o.process(av[i],
						new LineNumberReader(new FileReader(av[i])), pw);
				} catch (FileNotFoundException e) {
					System.err.println(e);
				}
        }
    
public voidprocess(java.lang.String fileName, java.io.LineNumberReader is, java.io.PrintStream out)
Get Marked parts of one file, given an open LineNumberReader.

	//-

              	 
       
		 
		  
		//+
		int nLines = 0;
		try {
			String inputLine;
			// Number of chars representing no indent
			final int NOINDENT = 0;
			// Number of chars to strip off to remove indentation
			int indent = NOINDENT;

			while ((inputLine = is.readLine()) != null) {
				if (inputLine.trim().equals(startMark)) {
					if (printing)
						System.err.println("ERROR: START INSIDE START, " +
							fileName + ':" + is.getLineNumber());
					printing = true;
					indent = NOINDENT;
				} else if (inputLine.trim().equals(endMark)) {
					if (!printing)
						System.err.println("ERROR: STOP WHILE STOPPED, " +
							fileName + ':" + is.getLineNumber());
					printing = false;
				} else if (printing) {
					if (indent < inputLine.length() && indent == NOINDENT) {
						while (Character.isWhitespace(inputLine.charAt(indent)))
							++indent;
					}
					if (indent == NOINDENT || inputLine.length() == 0)
						out.println(inputLine);
					else
						out.println(inputLine.substring(indent));
					++nLines;
				}
            }
            is.close();
			out.flush(); // Must not close - caller may still need it.
			if (nLines == 0)
				System.err.println("ERROR: No marks in " + fileName +
					"; no output generated!");
		//-
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }