ea05f016a5f2f702c6d9552280a254021e2e65ee
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / methods / JavaFile.java
1 package eu.svjatoslav.inspector.java.methods;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileNotFoundException;
6 import java.io.FileReader;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import eu.svjatoslav.inspector.tokenizer.InvalidSyntaxException;
12 import eu.svjatoslav.inspector.tokenizer.Tokenizer;
13 import eu.svjatoslav.inspector.tokenizer.TokenizerMatch;
14
15 public class JavaFile {
16
17         private final List<Import> imports = new ArrayList<Import>();
18
19         private String packageName;
20
21         private final File file;
22
23         StringBuffer contents = new StringBuffer();
24
25         public JavaFile(final File file) throws IOException, InvalidSyntaxException {
26                 this.file = file;
27                 parse();
28         }
29
30         public void parse() throws IOException, InvalidSyntaxException {
31                 System.out.println("java file: " + file);
32
33                 readFile();
34
35                 final Tokenizer tokenizer = new Tokenizer(contents.toString());
36
37                 // empty space
38                 tokenizer.addTerminator(" ", true);
39                 tokenizer.addTerminator("\t", true);
40                 tokenizer.addTerminator("\n", true);
41
42                 tokenizer.addTerminator(";", false);
43                 tokenizer.addTerminator("{", false);
44                 tokenizer.addTerminator("}", false);
45                 tokenizer.addTerminator("(", false);
46                 tokenizer.addTerminator(")", false);
47                 tokenizer.addTerminator("[", false);
48                 tokenizer.addTerminator("]", false);
49                 tokenizer.addTerminator("<", false);
50                 tokenizer.addTerminator(">", false);
51                 tokenizer.addTerminator(",", false);
52
53                 // comments
54                 tokenizer.addTerminator("//", "\n", true);
55                 tokenizer.addTerminator("/*", "*/", true);
56
57                 final Modifiers modifiers = new Modifiers();
58
59                 while (true) {
60                         final TokenizerMatch match = tokenizer.getNextToken();
61                         if (match == null)
62                                 break;
63
64                         if (match.token.equals("package")) {
65                                 parsePackage(tokenizer);
66                                 continue;
67                         }
68
69                         if (match.token.equals("import")) {
70                                 parseImport(tokenizer);
71                                 continue;
72                         }
73
74                         final boolean wasModifier = modifiers.parseModifier(match.token);
75                         if (wasModifier)
76                                 continue;
77
78                         if ("class".equals(match.token)) {
79                                 parseClass(tokenizer);
80                                 continue;
81                         }
82
83                         if ("interface".equals(match.token)) {
84                                 parseInterface(tokenizer);
85                                 continue;
86                         }
87
88                         System.out.println("    " + modifiers.toString() + " "
89                                         + match.token);
90                         modifiers.reset();
91                         skipUntilSemicolon(tokenizer);
92                 }
93
94         }
95
96         private void parseClass(final Tokenizer tokenizer)
97                         throws InvalidSyntaxException {
98
99                 final TokenizerMatch match = tokenizer.getNextToken();
100                 final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
101                                 false);
102                 System.out.println(clazz.toString());
103         }
104
105         private void parseImport(final Tokenizer tokenizer)
106                         throws InvalidSyntaxException {
107
108                 final Import imp = new Import();
109
110                 final TokenizerMatch match = tokenizer.getNextToken();
111
112                 if (match.token.equals("static")) {
113                         imp.isStatic = true;
114                         imp.path = tokenizer.getNextToken().token;
115                 } else
116                         imp.path = match.token;
117
118                 imports.add(imp);
119
120                 tokenizer.expectNextToken(";");
121         }
122
123         private void parseInterface(final Tokenizer tokenizer)
124                         throws InvalidSyntaxException {
125
126                 final TokenizerMatch match = tokenizer.getNextToken();
127                 final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
128                 System.out.println(clazz.toString());
129         }
130
131         private void parsePackage(final Tokenizer tokenizer)
132                         throws InvalidSyntaxException {
133
134                 final TokenizerMatch match = tokenizer.getNextToken();
135
136                 packageName = match.token;
137
138                 tokenizer.expectNextToken(";");
139         }
140
141         private void readFile() throws FileNotFoundException, IOException {
142                 final FileReader fileReader = new FileReader(file);
143
144                 final BufferedReader bufferedReader = new BufferedReader(fileReader);
145
146                 while (true) {
147                         final String line = bufferedReader.readLine();
148
149                         if (line == null)
150                                 break;
151
152                         contents.append(line);
153                         contents.append("\n");
154                 }
155
156                 bufferedReader.close();
157                 fileReader.close();
158         }
159
160         public void skipUntilSemicolon(final Tokenizer tokenizer) {
161                 while (true) {
162                         final TokenizerMatch token = tokenizer.getNextToken();
163
164                         if (token == null)
165                                 return;
166
167                         if (token.token.equals(";"))
168                                 return;
169                 }
170         }
171
172 }