Changed license to LGPLv3.
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / methods / JavaFile.java
old mode 100644 (file)
new mode 100755 (executable)
index 5b636d6..f87a1d9
@@ -1,3 +1,12 @@
+/*
+ * JavaInspect - Utility to visualize java software
+ * Copyright (C) 2013-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
 package eu.svjatoslav.inspector.java.methods;
 
 import java.io.BufferedReader;
@@ -8,28 +17,35 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import eu.svjatoslav.inspector.tokenizer.Tokenizer;
-import eu.svjatoslav.inspector.tokenizer.TokenizerMatch;
+import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
+import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
+import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
 
 public class JavaFile {
 
        private final List<Import> imports = new ArrayList<Import>();
 
+       private String packageName;
+
        private final File file;
 
        StringBuffer contents = new StringBuffer();
 
-       public JavaFile(final File file) throws IOException {
+       public List<Clazz> classes = new ArrayList<Clazz>();
+
+       public JavaFile(final File file) throws IOException, InvalidSyntaxException {
                this.file = file;
                parse();
        }
 
-       public void parse() throws IOException {
+       public void parse() throws IOException, InvalidSyntaxException {
                System.out.println("java file: " + file);
 
                readFile();
 
                final Tokenizer tokenizer = new Tokenizer(contents.toString());
+
+               // empty space
                tokenizer.addTerminator(" ", true);
                tokenizer.addTerminator("\t", true);
                tokenizer.addTerminator("\n", true);
@@ -41,47 +57,105 @@ public class JavaFile {
                tokenizer.addTerminator(")", false);
                tokenizer.addTerminator("[", false);
                tokenizer.addTerminator("]", false);
+               tokenizer.addTerminator("<", false);
+               tokenizer.addTerminator(">", false);
+               tokenizer.addTerminator(",", false);
+               tokenizer.addTerminator("@", false);
+
+               // comments
+               tokenizer.addTerminator("//", "\n", true);
+               tokenizer.addTerminator("/*", "*/", true);
+
+               final Modifiers modifiers = new Modifiers();
 
                while (true) {
-                       final TokenizerMatch match = tokenizer.getToken();
+                       final TokenizerMatch match = tokenizer.getNextToken();
                        if (match == null)
                                break;
 
-                       if (match.token.equals("import"))
-                               parseImport(tokenizer);
-
-                       if (match.token.equals("package"))
+                       if (match.token.equals("package")) {
                                parsePackage(tokenizer);
+                               continue;
+                       }
 
+                       if (match.token.equals("import")) {
+                               parseImport(tokenizer);
+                               continue;
+                       }
+
+                       final boolean wasModifier = modifiers.parseModifier(match.token);
+                       if (wasModifier)
+                               continue;
+
+                       if ("class".equals(match.token)) {
+                               parseClass(tokenizer);
+                               continue;
+                       }
+
+                       if ("interface".equals(match.token)) {
+                               parseInterface(tokenizer);
+                               continue;
+                       }
+
+                       if ("@".equals(match.token)) {
+                               final Annotation annotation = new Annotation(tokenizer);
+                               continue;
+                       }
+
+                       System.out.println("    " + modifiers.toString() + " "
+                                       + match.token);
+                       modifiers.reset();
+                       skipUntilSemicolon(tokenizer);
                }
 
        }
 
-       private void parseImport(final Tokenizer tokenizer) {
+       private void parseClass(final Tokenizer tokenizer)
+                       throws InvalidSyntaxException {
+
+               final TokenizerMatch match = tokenizer.getNextToken();
+               final Clazz clazz = new Clazz(packageName, match.token, tokenizer,
+                               false);
+               // System.out.println(clazz.toString());
+               classes.add(clazz);
+
+       }
+
+       private void parseImport(final Tokenizer tokenizer)
+                       throws InvalidSyntaxException {
+
                final Import imp = new Import();
 
-               TokenizerMatch match = tokenizer.getToken();
+               final TokenizerMatch match = tokenizer.getNextToken();
 
                if (match.token.equals("static")) {
                        imp.isStatic = true;
-                       imp.path = tokenizer.getToken().token;
+                       imp.path = tokenizer.getNextToken().token;
                } else
                        imp.path = match.token;
 
                imports.add(imp);
 
-               // ;
-               match = tokenizer.getToken();
+               tokenizer.expectNextToken(";");
+       }
+
+       private void parseInterface(final Tokenizer tokenizer)
+                       throws InvalidSyntaxException {
+
+               final TokenizerMatch match = tokenizer.getNextToken();
+               final Clazz clazz = new Clazz(packageName, match.token, tokenizer, true);
+               // System.out.println(clazz.toString());
+               classes.add(clazz);
        }
 
-       private void parsePackage(final Tokenizer tokenizer) {
+       private void parsePackage(final Tokenizer tokenizer)
+                       throws InvalidSyntaxException {
 
-               TokenizerMatch match = tokenizer.getToken();
+               final TokenizerMatch match = tokenizer.getNextToken();
 
-               System.out.println(match.token);
+               packageName = match.token;
 
-               // ;
-               match = tokenizer.getToken();
+               tokenizer.expectNextToken(";");
        }
 
        private void readFile() throws FileNotFoundException, IOException {
@@ -103,4 +177,16 @@ public class JavaFile {
                fileReader.close();
        }
 
+       public void skipUntilSemicolon(final Tokenizer tokenizer) {
+               while (true) {
+                       final TokenizerMatch token = tokenizer.getNextToken();
+
+                       if (token == null)
+                               return;
+
+                       if (token.token.equals(";"))
+                               return;
+               }
+       }
+
 }