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