001: package com.avaje.util.codegen;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.IOException;
007:
008: import com.avaje.lib.util.StringHelper;
009:
010: public class SourceCodeParser {
011:
012: int commentMode = 1;
013: int codeMode = 0;
014:
015: int currentMode = codeMode;
016:
017: SourceCode source;
018:
019: String sourceDirectory = "src";
020:
021: public static void main(String[] args) throws Exception {
022:
023: SourceCodeParser p = new SourceCodeParser();
024: SourceCode source = p.parse("app.data.Bug");
025:
026: System.out
027: .println("-----------------------------------------------");
028: System.out.println("packComment>");
029: System.out.println(source.getPackageComment());
030: System.out.println("classComment>");
031: System.out.println(source.getClassComment());
032: System.out.println("classDec>");
033: System.out.println(source.getClassDeclaration());
034:
035: System.err.println(source.getMethod("getId"));
036: System.err.println(".");
037:
038: System.err.println(source.getPropertyCode("id"));
039: System.err.println(".");
040: System.err.println(source.getMethod("getBody"));
041: System.err.println(".");
042: System.err.println(source.getPropertyCode("body"));
043: System.err.println(".");
044: // System.out.println("method>");
045: // System.out.println(source.getMethod("setCustomerId"));
046: // System.out.println("method>");
047: // System.out.println(source.getMethod("getId"));
048: // System.out.println("method>");
049:
050: // String cc = source.getClassComment();
051: // System.err.println("ClassComment> "+cc);
052:
053: }
054:
055: public SourceCode parse(String beanClassName) throws IOException {
056:
057: String fileName = StringHelper.replaceString(beanClassName,
058: ".", "/");
059: File f = new File(sourceDirectory + File.separator + fileName
060: + ".java");
061: return parse(f);
062: }
063:
064: public SourceCode parse(File f) throws IOException {
065:
066: source = new SourceCode();
067:
068: FileReader read = new FileReader(f);
069:
070: BufferedReader b = new BufferedReader(read);
071: String line = null;
072: while ((line = b.readLine()) != null) {
073: parseLine(line, 0);
074: }
075: read.close();
076: return source;
077: }
078:
079: private void parseLine(String line, int from) {
080: if (isWhitespace(line, from)) {
081: return;
082: }
083: if (currentMode == codeMode) {
084: parseCodeMode(line, from);
085: } else {
086: parseCommentMode(line, from);
087: }
088:
089: }
090:
091: private void parseCodeMode(String line, int from) {
092: int cmPos = line.indexOf("/*", from);
093: if (cmPos > -1) {
094: int ePos = line.indexOf("*/", cmPos);
095: if (ePos == -1) {
096: String c = line.substring(from);
097: source.addComment(c);
098: currentMode = commentMode;
099: } else {
100: // add comment... continue with codeMode
101: String c = line.substring(cmPos, ePos + 2);
102: source.addComment(c);
103: parseCodeMode(line, ePos + 2);
104: }
105: return;
106: }
107: String c = line.substring(from);
108: source.addCode(c);
109: }
110:
111: private void parseCommentMode(String line, int from) {
112: int ePos = line.indexOf("*/", from);
113: if (ePos > -1) {
114: String c = line.substring(from, ePos + 2);
115: source.addComment(c);
116: currentMode = codeMode;
117: parseLine(line, ePos + 2);
118:
119: } else {
120: String c = line.substring(from);
121: source.addComment(c);
122: }
123: }
124:
125: private boolean isWhitespace(String line, int from) {
126: String s = line.substring(from);
127: return (s.trim().length() == 0);
128: }
129:
130: }
|