001: package com.mockrunner.gen.proc;
002:
003: import java.io.IOException;
004: import java.io.LineNumberReader;
005: import java.io.StringReader;
006: import java.util.ArrayList;
007: import java.util.List;
008:
009: public class JavaLineParser {
010: private List linesToParse;
011: private List blocksToParse;
012:
013: public JavaLineParser() {
014: linesToParse = new ArrayList();
015: blocksToParse = new ArrayList();
016: }
017:
018: public void addLine(String line) {
019: linesToParse.add(line);
020: }
021:
022: public void addLines(List lines) {
023: linesToParse.addAll(lines);
024: }
025:
026: public void addBlock(String blockLine) {
027: blocksToParse.add(blockLine);
028: }
029:
030: public void addBlocks(List blocks) {
031: blocksToParse.addAll(blocks);
032: }
033:
034: public List parse(String source) {
035: List resultList = new ArrayList();
036: List tempLinesToParse = new ArrayList(linesToParse);
037: List tempBlocksToParse = new ArrayList(blocksToParse);
038: LineNumberReader input = new LineNumberReader(new StringReader(
039: source));
040: String currentLine = null;
041: try {
042: while (null != (currentLine = input.readLine())) {
043: String line = checkLine(currentLine, linesToParse);
044: if (line != null) {
045: resultList.add(new Line(currentLine, input
046: .getLineNumber()));
047: tempLinesToParse.remove(line);
048: } else {
049: line = checkBlock(currentLine, blocksToParse);
050: if (line != null) {
051: int startLineNumber = input.getLineNumber();
052: int endLineNumber = determineEndLineNumber(input);
053: if (endLineNumber > startLineNumber) {
054: resultList.add(new Block(currentLine,
055: startLineNumber, endLineNumber));
056: tempBlocksToParse.remove(line);
057: }
058: }
059: }
060: }
061: } catch (IOException exc) {
062: throw new RuntimeException(exc.getMessage());
063: }
064: checkLinesOrBlocksLeft(tempLinesToParse, tempBlocksToParse);
065: return resultList;
066: }
067:
068: private void checkLinesOrBlocksLeft(List tempLinesToParse,
069: List tempBlocksToParse) {
070: StringBuffer message = new StringBuffer("");
071: if (tempLinesToParse.size() > 0) {
072: message.append("Lines not found:\n");
073: for (int ii = 0; ii < tempLinesToParse.size(); ii++) {
074: message.append(tempLinesToParse.get(ii).toString()
075: + "\n");
076: }
077: }
078: if (tempBlocksToParse.size() > 0) {
079: message.append("Blocks not found:\n");
080: for (int ii = 0; ii < tempBlocksToParse.size(); ii++) {
081: message.append(tempBlocksToParse.get(ii).toString()
082: + "\n");
083: }
084: }
085: if (message.length() > 0) {
086: throw new RuntimeException(message.toString());
087: }
088: }
089:
090: private String checkLine(String currentLine, List linesToParse) {
091: for (int ii = 0; ii < linesToParse.size(); ii++) {
092: String nextLine = (String) linesToParse.get(ii);
093: if (currentLine.trim().indexOf(nextLine) != -1) {
094: return nextLine;
095: }
096: }
097: return null;
098: }
099:
100: private String checkBlock(String currentLine, List blocksToParse) {
101: for (int ii = 0; ii < blocksToParse.size(); ii++) {
102: String nextLine = (String) blocksToParse.get(ii);
103: if (currentLine.trim().indexOf(nextLine) != -1) {
104: return nextLine;
105: }
106: }
107: return null;
108: }
109:
110: private int determineEndLineNumber(LineNumberReader input) {
111: String currentLine = null;
112: try {
113: while ((null != (currentLine = input.readLine()))
114: && (currentLine.trim().indexOf("{") == -1))
115: ;
116: int level = 1;
117: while ((level > 0)
118: && (null != (currentLine = input.readLine()))) {
119: if (currentLine.trim().indexOf("{") != -1) {
120: level++;
121: } else if (currentLine.trim().indexOf("}") != -1) {
122: level--;
123: }
124: }
125: if (level == 0) {
126: return input.getLineNumber();
127: } else {
128: return -1;
129: }
130: } catch (IOException exc) {
131: throw new RuntimeException(exc.getMessage());
132: }
133: }
134:
135: public static class Line {
136: private String line;
137: private int lineNumber;
138:
139: public Line(String line, int lineNumber) {
140: this .line = line;
141: this .lineNumber = lineNumber;
142: }
143:
144: public String getLine() {
145: return line;
146: }
147:
148: public int getLineNumber() {
149: return lineNumber;
150: }
151: }
152:
153: public static class Block extends Line {
154: private int endLineNumber;
155:
156: public Block(String line, int lineNumber, int endLineNumber) {
157: super (line, lineNumber);
158: this .endLineNumber = endLineNumber;
159: }
160:
161: public int getEndLineNumber() {
162: return endLineNumber;
163: }
164: }
165: }
|