--- /dev/null
+package eu.svjatoslav.inspector.java.methods;
+
+public class Import {
+
+ String path;
+ boolean isStatic = false;
+
+}
package eu.svjatoslav.inspector.java.methods;
+import java.io.BufferedReader;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import eu.svjatoslav.inspector.tokenizer.Tokenizer;
+import eu.svjatoslav.inspector.tokenizer.TokenizerMatch;
public class JavaFile {
+ private final List<Import> imports = new ArrayList<Import>();
+
private final File file;
- public JavaFile(final File file) {
+ StringBuffer contents = new StringBuffer();
+
+ public JavaFile(final File file) throws IOException {
this.file = file;
+ parse();
+ }
+
+ public void parse() throws IOException {
+ System.out.println("java file: " + file);
+
+ readFile();
+
+ final Tokenizer tokenizer = new Tokenizer(contents.toString());
+ tokenizer.addTerminator(" ", true);
+ tokenizer.addTerminator("\t", true);
+ tokenizer.addTerminator("\n", true);
+
+ tokenizer.addTerminator(";", false);
+
+ while (true) {
+ final TokenizerMatch match = tokenizer.getToken();
+ if (match == null)
+ break;
+
+ if (match.token.equals("import"))
+ parseImport(tokenizer);
+ }
+
+ }
+
+ private void parseImport(final Tokenizer tokenizer) {
+ final Import imp = new Import();
+
+ final TokenizerMatch match = tokenizer.getToken();
+
+ if (match.token.equals("static")) {
+ imp.isStatic = true;
+ imp.path = tokenizer.getToken().token;
+ } else
+ imp.path = match.token;
+
+ imports.add(imp);
}
- public void parse() {
+ private void readFile() throws FileNotFoundException, IOException {
+ final FileReader fileReader = new FileReader(file);
+
+ final BufferedReader bufferedReader = new BufferedReader(fileReader);
+
+ while (true) {
+ final String line = bufferedReader.readLine();
+
+ if (line == null)
+ break;
+
+ contents.append(line);
+ contents.append("\n");
+ }
+ bufferedReader.close();
+ fileReader.close();
}
}
package eu.svjatoslav.inspector.java.methods;
import java.io.File;
+import java.io.IOException;
import eu.svjatoslav.commons.file.FilePathParser;
public void parseFile(final File file) {
final String fileExtension = FilePathParser.getFileExtension(file);
- if ("java".equalsIgnoreCase(fileExtension)){
- JavaFile javaFile = new JavaFile(file);
- // oeu
- }
+ if ("java".equalsIgnoreCase(fileExtension))
+ try {
+ final JavaFile javaFile = new JavaFile(file);
+ } catch (final IOException e) {
+ System.out.println("Error parsing file: " + file.toString()
+ + ", " + e.toString());
+ }
}
}
--- /dev/null
+package eu.svjatoslav.inspector.tokenizer;
+
+public class Terminator {
+
+ String value;
+ boolean empty;
+
+ public Terminator(final String value, final boolean empty) {
+ this.value = value;
+ this.empty = empty;
+ }
+}
--- /dev/null
+package eu.svjatoslav.inspector.tokenizer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Tokenizer {
+
+ private final List<Terminator> terminators = new ArrayList<Terminator>();
+ private final String source;
+
+ private int currentIndex = 0;
+
+ public Tokenizer(final String source) {
+ this.source = source;
+ }
+
+ public void addTerminator(final String terminator, final boolean empty) {
+
+ terminators.add(new Terminator(terminator, empty));
+ }
+
+ public TokenizerMatch getToken() {
+ final StringBuffer result = new StringBuffer();
+
+ while (true) {
+ if (currentIndex >= source.length())
+ return null;
+
+ boolean accumulateCurrentChar = true;
+
+ findTerminator: for (final Terminator terminator : terminators)
+ if (terminatorMatches(terminator))
+ // empty space detected
+ if (terminator.empty) {
+ currentIndex += terminator.value.length();
+ if (result.length() > 0)
+ return new TokenizerMatch(result.toString(),
+ terminator);
+ else {
+ accumulateCurrentChar = false;
+ break findTerminator;
+ }
+ } else if (result.length() > 0)
+ return new TokenizerMatch(result.toString(), terminator);
+ else {
+ currentIndex += terminator.value.length();
+ return new TokenizerMatch(terminator.value, terminator);
+ }
+
+ if (accumulateCurrentChar) {
+ result.append(source.charAt(currentIndex));
+ currentIndex++;
+ }
+ }
+
+ }
+
+ public boolean terminatorMatches(final Terminator terminator) {
+ if ((currentIndex + terminator.value.length()) > source.length())
+ return false;
+
+ for (int i = 0; i < terminator.value.length(); i++)
+ if (terminator.value.charAt(i) != source.charAt(i + currentIndex))
+ return false;
+
+ return true;
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.inspector.tokenizer;
+
+public class TokenizerMatch {
+
+ public String token;
+ public Terminator terminator;
+
+ public TokenizerMatch(final String token, final Terminator terminator) {
+ this.token = token;
+ this.terminator = terminator;
+ }
+}