Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed May 25, 2010
1 parent 2f8c498 commit 41c072d
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 64 deletions.
24 changes: 24 additions & 0 deletions source/src/fr/zenexity/pdt/editors/IO.java
@@ -1,7 +1,11 @@
package fr.zenexity.pdt.editors;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
Expand Down Expand Up @@ -35,4 +39,24 @@ public static void readLines(IFile file, LineReader reader) throws IOException,
readLines(file.getContents(), reader);
}

/**
* Read file content to a String (always use utf-8)
* @param file The file to read
* @return The String content
* @throws java.io.IOException
* @throws CoreException
*/
public static String readContentAsString(IFile file) throws IOException, CoreException {
InputStream is = file.getContents();
StringWriter result = new StringWriter();
PrintWriter out = new PrintWriter(result);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
out.println(line);
}
is.close();
return result.toString();
}

}
39 changes: 20 additions & 19 deletions source/src/org/playframework/playclipse/ModelInspector.java
@@ -1,7 +1,6 @@
package org.playframework.playclipse;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
Expand All @@ -28,30 +27,32 @@ public IMethod resolveAction(String input) {
}

private IMethod getAction(String fullClassName, String query) {
IType parent = null;
try {
IType parent = javaProject.findType(fullClassName);
// Look for package fragments
if (parent == null) {
return null;
}
IJavaElement[] children = parent.getChildren();
for (int i = 0; i < children.length; i++) {
IJavaElement child = children[i];
if (child instanceof IMethod) {
IMethod method = (IMethod)child;
int flags = method.getFlags();
if ((query.isEmpty() || method.getElementName().startsWith(query))
&& Flags.isPublic(flags)
&& Flags.isStatic(flags)
&& method.getReturnType().equals("V")) {
parent = javaProject.findType(fullClassName);
} catch (JavaModelException e) {}
if (parent == null) {
return null;
}
return findMethod(parent, query);
}

private IMethod findMethod(IType type, String query) {
// We can't use IType.getMethod(name, parameterTypeSignature) because we usually don't know the parameters,
// we only have the name.
try {
for (IMethod method: type.getMethods()) {
int flags = method.getFlags();
if (Flags.isPublic(flags)
&& Flags.isStatic(flags)
&& method.getReturnType().equals("V")) {
if (method.getElementName().equals(query))
return method;
}
}
}
} catch (JavaModelException e) {
e.printStackTrace();
return null;
}

return null;
}

Expand Down
30 changes: 30 additions & 0 deletions source/src/org/playframework/playclipse/builder/ErrorChecker.java
@@ -1,8 +1,13 @@
package org.playframework.playclipse.builder;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.playframework.playclipse.ModelInspector;

public abstract class ErrorChecker {

Expand Down Expand Up @@ -34,4 +39,29 @@ protected IMarker addMarker(String message, int lineNumber, int severity, int be

public abstract void check();

private IJavaProject javaProject = null;
private ModelInspector inspector = null;

protected IJavaProject getJavaProject() {
if (javaProject == null) javaProject = JavaCore.create(getProject());
return javaProject;
}

protected ModelInspector getInspector() {
if (inspector == null) inspector = new ModelInspector(getJavaProject());
return inspector;
}

protected IProject getProject() {
IContainer container = file.getParent();
while (container != null) {
if (container instanceof IProject) {
return (IProject)container;
}
container = container.getParent();
}
// Should not happen
return null;
}

}
19 changes: 12 additions & 7 deletions source/src/org/playframework/playclipse/builder/PlayBuilder.java
Expand Up @@ -15,12 +15,19 @@ public class PlayBuilder extends IncrementalProjectBuilder {

class ResourceVisitor implements IResourceVisitor {
public boolean visit(IResource resource) {
System.out.println("Visit: " + resource.getName());
if (resource instanceof IFile && resource.getName().equals("routes")) {
checkRoute((IFile)resource);
System.out.println("end reading routes");
if (!(resource instanceof IFile)) return true;
IFile file = (IFile)resource;
if (resource.getName().equals("routes")) {
deleteMarkers(file);
(new RouteChecker(file)).check();
return false;
} else return true;
}
if (TemplateChecker.isTemplate(resource.getFullPath())) {
deleteMarkers(file);
(new TemplateChecker(file)).check();
return false;
}
return true;
}
}

Expand All @@ -41,8 +48,6 @@ protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws
}

void checkRoute(IFile file) {
deleteMarkers(file);
(new RouteChecker(file)).check();
}

private void deleteMarkers(IFile file) {
Expand Down
Expand Up @@ -2,14 +2,9 @@

import java.util.regex.Pattern;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.playframework.playclipse.ModelInspector;

import fr.zenexity.pdt.editors.IO;
import fr.zenexity.pdt.editors.IO.LineReader;
Expand All @@ -28,7 +23,6 @@ public void check() {
try {
IO.readLines(file, new LineReader() {
public void readLine(String line, int lineNumber, int offset) {
System.out.println("" + lineNumber + " - " + line);
if (comment.matcher(line).find()) {
// commented line
return;
Expand All @@ -45,30 +39,24 @@ public void readLine(String line, int lineNumber, int offset) {
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static Pattern METHOD = Pattern.compile("(\\*|GET|POST|PUT|DELETE|UPDATE)");
private static Pattern METHOD = Pattern.compile("(\\*|GET|POST|PUT|DELETE|UPDATE|HEAD)");

private void checkLine(String line, int lineNumber, int offset) throws CoreException {
String[] rule = line.split("\\s+");
if (rule.length != 3) {
System.out.println("ERROR length with {" + line + "}");
addMarker("Invalid route syntax", lineNumber, IMarker.SEVERITY_ERROR, offset, offset + line.length());
return;
}
String method = rule[0];
//String path = rule[1];
String action = rule[2];
System.out.println("Method = " + method + " - " + METHOD.matcher(method).matches());
if (METHOD.matcher(method).matches() == false) {
System.out.println("===> add marker line " + lineNumber);
addMarker("Invalid method", lineNumber, IMarker.SEVERITY_ERROR, offset, offset + method.length());
}


if (action.indexOf(":") == -1 && // TODO: Check module routes
action.indexOf("{") == -1 && // TODO: Check if it's valid?
getInspector().resolveAction(action) == null) {
Expand All @@ -77,29 +65,4 @@ private void checkLine(String line, int lineNumber, int offset) throws CoreExcep
}
}

private IJavaProject javaProject = null;
private ModelInspector inspector = null;

private IJavaProject getJavaProject() {
if (javaProject == null) javaProject = JavaCore.create(getProject());
return javaProject;
}

private ModelInspector getInspector() {
if (inspector == null) inspector = new ModelInspector(getJavaProject());
return inspector;
}

public IProject getProject() {
IContainer container = file.getParent();
while (container != null) {
if (container instanceof IProject) {
return (IProject)container;
}
container = container.getParent();
}
// Should not happen
return null;
}

}

0 comments on commit 41c072d

Please sign in to comment.