001: package com.mockrunner.gen.proc;
002:
003: import java.io.IOException;
004: import java.io.LineNumberReader;
005: import java.io.PrintWriter;
006: import java.io.StringReader;
007: import java.io.StringWriter;
008: import java.util.List;
009:
010: import com.mockrunner.gen.proc.JavaLineParser.Block;
011: import com.mockrunner.gen.proc.JavaLineParser.Line;
012:
013: public class JavaLineProcessor {
014: private JavaLineParser parser;
015:
016: public JavaLineProcessor() {
017: parser = new JavaLineParser();
018: }
019:
020: public void addLine(String line) {
021: parser.addLine(line);
022: }
023:
024: public void addBlock(String block) {
025: parser.addBlock(block);
026: }
027:
028: public void addLines(List lines) {
029: parser.addLines(lines);
030: }
031:
032: public void addBlocks(List blocks) {
033: parser.addBlocks(blocks);
034: }
035:
036: public String process(String source) {
037: List result = parser.parse(source);
038: LineNumberReader input = new LineNumberReader(new StringReader(
039: source));
040: StringWriter resultStringWriter = new StringWriter(source
041: .length() + 100);
042: PrintWriter output = new PrintWriter(resultStringWriter);
043: for (int ii = 0; ii < result.size(); ii++) {
044: Line nextLine = (Line) result.get(ii);
045: int processUntil = nextLine.getLineNumber();
046: dumpToOutputUntil(input, output, processUntil);
047: handleLine(nextLine, input, output);
048: }
049: dumpToOutputUntilEnd(input, output);
050: output.flush();
051: return resultStringWriter.toString();
052: }
053:
054: private void handleLine(Line line, LineNumberReader input,
055: PrintWriter output) {
056: try {
057: StringBuffer nextLine = new StringBuffer(input.readLine());
058: int firstNonWhitespace = 0;
059: while (firstNonWhitespace < nextLine.length()
060: && Character.isWhitespace(nextLine
061: .charAt(firstNonWhitespace))) {
062: firstNonWhitespace++;
063: }
064: if (firstNonWhitespace < nextLine.length()) {
065: if (line instanceof Block) {
066: nextLine.insert(firstNonWhitespace, "/*");
067: output.println(nextLine);
068: int processUntil = ((Block) line)
069: .getEndLineNumber();
070: dumpToOutputUntil(input, output, processUntil);
071: nextLine = new StringBuffer(input.readLine());
072: int lastNonWhitespace = nextLine.length() - 1;
073: while (lastNonWhitespace > 0
074: && Character.isWhitespace(nextLine
075: .charAt(lastNonWhitespace))) {
076: lastNonWhitespace--;
077: }
078: nextLine.insert(lastNonWhitespace + 1, "*/");
079: output.println(nextLine);
080: } else {
081: nextLine.insert(firstNonWhitespace, "//");
082: output.println(nextLine);
083: }
084: }
085: } catch (IOException exc) {
086: throw new RuntimeException(exc.getMessage());
087: }
088: }
089:
090: private void dumpToOutputUntil(LineNumberReader input,
091: PrintWriter output, int processUntil) {
092: while (input.getLineNumber() < processUntil - 1) {
093: try {
094: output.println(input.readLine());
095: } catch (IOException exc) {
096: throw new RuntimeException(exc.getMessage());
097: }
098: }
099: }
100:
101: private void dumpToOutputUntilEnd(LineNumberReader input,
102: PrintWriter output) {
103: String line = null;
104: try {
105: while (null != (line = input.readLine())) {
106: output.println(line);
107: }
108: } catch (IOException exc) {
109: throw new RuntimeException(exc.getMessage());
110: }
111: }
112: }
|