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