*/
public class SpecExtractor {
private ArrayList<SpecConstruct> _constructs;
- private StringBuilder _potentialConstruct;
- private int _beginLine, _endLine;
+ private int _beginLineNum, _endLineNum;
+ private String _beginLine;
SpecExtractor() {
_constructs = new ArrayList<SpecConstruct>();
- _potentialConstruct = new StringBuilder();
}
/**
* to the _constructs list.
* </p>
* @param files
+ * @throws SpecNotMatchException
*/
- public void extract(File[] files) {
+ public void extract(File[] files) throws SpecNotMatchException {
for (int i = 0; i < files.length; i++)
extract(files[i]);
}
- public void extract(File file) {
+ public void extract(File file) throws SpecNotMatchException {
try {
LineNumberReader reader = new LineNumberReader(new FileReader(file));
String prevLine = "", curLine, trimedLine;
StringBuilder specText = new StringBuilder();
- boolean _foundHead = false;
+ boolean foundHead = false;
int specIndex = 0;
while ((curLine = reader.readLine()) != null) {
if (prevLine.endsWith("\\"))
continue;
trimedLine = trimSpace(curLine);
- if (!_foundHead) {
+ if (!foundHead) {
if (trimedLine.startsWith("/**")) {
- _beginLine = reader.getLineNumber();
- _foundHead = true;
+ _beginLineNum = reader.getLineNumber();
+ _beginLine = curLine;
+ foundHead = true;
specText.append("\n");
specText.append(curLine);
if (trimedLine.endsWith("*/")) {
- _endLine = reader.getLineNumber();
- _foundHead = false;
+ _endLineNum = reader.getLineNumber();
+ foundHead = false;
System.out.println("Spec<" + specIndex + "> Begin: "
- + _beginLine + " End: " + _endLine);
+ + _beginLine + " End: " + _endLineNum);
System.out.println(specText);
specIndex++;
}
specText.append("\n");
specText.append(curLine);
if (trimedLine.endsWith("*/")) {
- _endLine = reader.getLineNumber();
- _foundHead = false;
+ _endLineNum = reader.getLineNumber();
+ foundHead = false;
System.out.println("Spec<" + specIndex + "> Begin: "
- + _beginLine + " End: " + _endLine);
+ + _beginLine + " End: " + _endLineNum);
System.out.println(specText);
specIndex++;
specText = new StringBuilder();
- } else {
-
}
}
}
+ // At the end we can only find the head "/**" but no tail found
+ if (foundHead) {
+ String msg = "In file \"" + file.getAbsolutePath() + "\", line: "
+ + _beginLineNum + "\n" + _beginLine + "\n" + "Can't find matching spec.";
+ throw new SpecNotMatchException(msg);
+ }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
public static void main(String[] argvs) {
SpecExtractor extractor = new SpecExtractor();
File file = new File("./grammer/spec1.txt");
- extractor.extract(file);
+ try {
+ extractor.extract(file);
+ } catch (SpecNotMatchException e) {
+ e.printStackTrace();
+ }
}
}