GetMarkpublic class GetMark extends Object GetMark -- get marked lines.
In this version, the marks are hard-coded; ideally they would come
from a FileProperties object. |
Fields Summary |
---|
public final String | startMarkthe default starting mark. | public final String | endMarkthe default ending mark. | protected boolean | printingTrue if we are currently inside marks. |
Methods Summary |
---|
public static void | main(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 void | process(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);
}
|
|