Permalink
Browse files

Fixed package/directory traversal. Clean up formatting.

  • Loading branch information...
1 parent f9f7823 commit 386ef12896ba49edb7d7add80b6f23773ad43c5d @jannehietamaki committed Jan 6, 2013
@@ -37,44 +37,46 @@
public class JavaSourceContext {
private static final String FILE_SEP = System.getProperty("file.separator");
private final static String REMOVE_COMMENTS_REGEXP = "(?://.*)|(/\\*(?:.|[\\n\\r])*?\\*/)";
-
+
private final String className;
private final File sourceRoot;
-
+
public JavaSourceContext(File file) {
String packageName = parsePackage(file);
File rootDirectory = file.getParentFile();
if(packageName != null) {
- for(String token : packageName.split("\\.")) {
- if(!token.equals(rootDirectory.getName())) {
- throw new RuntimeException("Package structure does not match directory structure: " + token + " != " + rootDirectory.getName());
- }
- rootDirectory = rootDirectory.getParentFile();
- }
+ String[] pathTokens = packageName.split("\\.");
+ for(int i = pathTokens.length - 1; i >= 0; i--) {
+ String token = pathTokens[i];
+ if(!token.equals(rootDirectory.getName())) {
+ throw new RuntimeException("Package structure does not match directory structure: " + token + " != " + rootDirectory.getName());
+ }
+ rootDirectory = rootDirectory.getParentFile();
+ }
}
sourceRoot = rootDirectory;
-
+
String fileName = file.getName();
String className = fileName.substring(0, fileName.length() - Kind.SOURCE.extension.length());
if(packageName != null) {
className = packageName + "." + className;
}
this.className = className;
}
-
+
public File getSourceRoot() {
return sourceRoot;
}
-
+
public String getClassName() {
return className;
}
-
- /*
+
+ /*
* Parse package definition from a Java source file:
* First remove all comments, split file into lines, find first non-empty line
* Then, if the line starts with keyword "package", parse the package definition from it.
- *
+ *
*/
private String parsePackage(File file) {
try {
@@ -95,4 +97,4 @@ private String parsePackage(File file) {
throw new RuntimeException(e);
}
}
-}
+}

0 comments on commit 386ef12

Please sign in to comment.