Author: lincolnthree
Date: 2010-06-29 15:29:44 -0400 (Tue, 29 Jun 2010)
New Revision: 13321
Added:
sandbox/encore/src/main/java/org/jboss/encore/grammar/java/JavaParser.java
sandbox/encore/src/test/java/org/jboss/encore/
sandbox/encore/src/test/java/org/jboss/encore/grammar/
sandbox/encore/src/test/java/org/jboss/encore/grammar/java/
sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/java/
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/java/MockClassFile.java
Removed:
sandbox/encore/src/main/antlr3/
sandbox/encore/src/main/java/org/jboss/encore/grammar/java/Parser.java
sandbox/encore/src/test/java/org/jboss/seam/encore/grammar/xml/
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/xml/
Modified:
sandbox/encore/pom.xml
Log:
Switched to Eclipse JDT & prototyped simple parser
Modified: sandbox/encore/pom.xml
===================================================================
--- sandbox/encore/pom.xml 2010-06-29 13:38:42 UTC (rev 13320)
+++ sandbox/encore/pom.xml 2010-06-29 19:29:44 UTC (rev 13321)
@@ -10,35 +10,24 @@
enhancement framework </description>
<dependencies>
+ <!-- Resource manipulation dependencies -->
<dependency>
- <groupId>org.apache.maven</groupId>
- <artifactId>maven-plugin-api</artifactId>
- <version>2.0</version>
- </dependency>
- <dependency>
<groupId>org.jboss.shrinkwrap</groupId>
<artifactId>shrinkwrap-api</artifactId>
<version>1.0.0-alpha-9</version>
</dependency>
-
+
+ <!-- Maven plugin dependencies -->
<dependency>
- <groupId>org.antlr</groupId>
- <artifactId>antlr</artifactId>
- <version>3.2</version>
+ <groupId>org.apache.maven</groupId>
+ <artifactId>maven-plugin-api</artifactId>
+ <version>2.0</version>
</dependency>
-
-
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</artifactId>
<version>3.0-beta-1</version>
</dependency>
- <dependency>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-clean-plugin</artifactId>
- <version>2.1.1</version>
- <scope>provided</scope>
- </dependency>
<dependency>
<groupId>junit</groupId>
@@ -82,18 +71,6 @@
<build>
<plugins>
<plugin>
- <groupId>org.antlr</groupId>
- <artifactId>antlr3-maven-plugin</artifactId>
- <version>3.2</version>
- <executions>
- <execution>
- <goals>
- <goal>antlr</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
Added: sandbox/encore/src/main/java/org/jboss/encore/grammar/java/JavaParser.java
===================================================================
--- sandbox/encore/src/main/java/org/jboss/encore/grammar/java/JavaParser.java
(rev 0)
+++ sandbox/encore/src/main/java/org/jboss/encore/grammar/java/JavaParser.java 2010-06-29
19:29:44 UTC (rev 13321)
@@ -0,0 +1,44 @@
+package org.jboss.encore.grammar.java;
+
+import java.io.InputStream;
+
+import org.eclipse.jdt.core.dom.AST;
+import org.eclipse.jdt.core.dom.ASTParser;
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.internal.compiler.util.Util;
+
+public class JavaParser
+{
+
+ /**
+ * Parses and process the java source code as a compilation unit and the
+ * result it abstract syntax tree (AST) representation and this action uses
+ * the third edition of java Language Specification, that gets the
+ * possibility to support J2SE 5 during the parsing.
+ *
+ * @param source - the java source to be parsed (i.e. the char[] contains
+ * Java source).
+ * @return CompilationUnit Abstract syntax tree representation of a java
+ * source file.
+ */
+ public static CompilationUnit parse(InputStream inputStream)
+ {
+ try
+ {
+ char[] source = Util.getInputStreamAsCharArray(inputStream,
inputStream.available(), "ISO8859_1");
+
+ ASTParser parser = ASTParser.newParser(AST.JLS3);
+ parser.setSource(source);
+ parser.setKind(ASTParser.K_COMPILATION_UNIT);
+ // to get more informations from the unit.
+ parser.setResolveBindings(true);
+
+ return (CompilationUnit) parser.createAST(null);
+ }
+ catch (Exception e)
+ {
+ throw new IllegalArgumentException("InputStream must be a parsable java
file: ", e);
+ }
+ }
+
+}
Deleted: sandbox/encore/src/main/java/org/jboss/encore/grammar/java/Parser.java
===================================================================
--- sandbox/encore/src/main/java/org/jboss/encore/grammar/java/Parser.java 2010-06-29
13:38:42 UTC (rev 13320)
+++ sandbox/encore/src/main/java/org/jboss/encore/grammar/java/Parser.java 2010-06-29
19:29:44 UTC (rev 13321)
@@ -1,164 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2010, Red Hat, Inc., and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-
-package org.jboss.encore.grammar.java;
-
-import java.io.File;
-
-import org.antlr.runtime.ANTLRFileStream;
-import org.antlr.runtime.CommonTokenStream;
-import org.antlr.runtime.Token;
-import org.antlr.runtime.TokenRewriteStream;
-import org.antlr.runtime.debug.BlankDebugEventListener;
-import org.antlr.runtime.tree.CommonTree;
-import org.antlr.runtime.tree.CommonTreeAdaptor;
-import org.antlr.runtime.tree.TreeAdaptor;
-import org.jboss.seam.encore.grammar.java.JavaLexer;
-import org.jboss.seam.encore.grammar.java.JavaParser;
-import org.jboss.seam.encore.grammar.java.JavaParser.compilationUnit_return;
-
-/**
- * @author <a href="mailto:lincolnbaxter@gmail.com>Lincoln Baxter,
III</a>
- *
- */
-public class Parser
-{
- public static long lexerTime = 0;
- public static boolean profile = false;
-
- static JavaLexer lexer;
-
- static final TreeAdaptor adaptor = new CommonTreeAdaptor()
- {
- @Override
- public Object create(final Token payload)
- {
- return new CommonTree(payload);
- }
- };
-
- public static void main(final String[] args)
- {
- try
- {
- long start = System.currentTimeMillis();
-
- String arg =
"/home/lb3/JBoss/encore/src/main/java/org/jboss/encore/grammar/java/Parser.java";
- System.out.println("Doing file: " + arg);
- File file = new File(arg);
- doFile(file); // parse it
-
- long stop = System.currentTimeMillis();
-
- System.out.println("finished parsing OK");
- if (profile)
- {
- System.out.println("num decisions " + profiler.numDecisions);
- }
- }
- catch (Exception e)
- {
- System.err.println("exception: " + e);
- e.printStackTrace(System.err); // so we can get stack trace
- }
- }
-
- // This method decides what action to take based on the type of
- // file we are looking at
- public static void doFile(final File f) throws Exception
- {
- // If this is a directory, walk each file/dir in that directory
- if (f.isDirectory())
- {
- String files[] = f.list();
- for (String file : files)
- {
- doFile(new File(f, file));
- }
- }
-
- // otherwise, if this is a java file, parse it!
- else if (((f.getName().length() > 5) &&
f.getName().substring(f.getName().length() - 5).equals(".java")) ||
f.getName().equals("input"))
- {
- System.err.println("parsing " + f.getAbsolutePath());
- parseFile(f.getAbsolutePath());
- }
- }
-
- static class CountDecisions extends BlankDebugEventListener
- {
- public int numDecisions = 0;
-
- @Override
- public void enterDecision(final int decisionNumber)
- {
- numDecisions++;
- }
- }
-
- static CountDecisions profiler = new CountDecisions();
-
- // Here's where we do the real work...
- public static void parseFile(final String f) throws Exception
- {
- try
- {
- // Create a scanner that reads from the input stream passed to us
- if (lexer == null)
- {
- lexer = new JavaLexer();
- }
- lexer.setCharStream(new ANTLRFileStream(f));
-
- CommonTokenStream tokens = new TokenRewriteStream(lexer);
- long start = System.currentTimeMillis();
- tokens.LT(1); // force load
- long stop = System.currentTimeMillis();
- lexerTime += stop - start;
-
- /*
- * long t1 = System.currentTimeMillis(); tokens.LT(1); long t2 =
- * System.currentTimeMillis();
- * System.out.println("lexing time: "+(t2-t1)+"ms");
- */
- // System.out.println(tokens);
-
- // Create a parser that reads from the scanner
- JavaParser parser = null;
- parser = new JavaParser(tokens);
-
- parser.setTreeAdaptor(adaptor);
-
- // start parsing at the compilationUnit rule
- compilationUnit_return compilationUnit = parser.compilationUnit();
-
- System.out.println("out");
- // System.err.println("finished "+f);
- }
- catch (Exception e)
- {
- System.err.println("parser exception: " + e);
- e.printStackTrace(); // so we can get stack trace
- }
- }
-
-}
Added: sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
===================================================================
--- sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java
(rev 0)
+++
sandbox/encore/src/test/java/org/jboss/encore/grammar/java/JavaParserTest.java 2010-06-29
19:29:44 UTC (rev 13321)
@@ -0,0 +1,25 @@
+package org.jboss.encore.grammar.java;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.InputStream;
+import java.util.List;
+
+import org.eclipse.jdt.core.dom.CompilationUnit;
+import org.eclipse.jdt.core.dom.ImportDeclaration;
+import org.junit.Test;
+
+public class JavaParserTest
+{
+ @Test
+ public void testParse() throws Exception
+ {
+ InputStream stream =
JavaParserTest.class.getResourceAsStream("/org/jboss/seam/encore/grammar/java/MockClassFile.java");
+ CompilationUnit unit = JavaParser.parse(stream);
+
+ @SuppressWarnings("unchecked")
+ List<ImportDeclaration> imports = unit.imports();
+
+ assertEquals("java.net.URL",
imports.get(0).getName().getFullyQualifiedName());
+ }
+}
Added:
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/java/MockClassFile.java
===================================================================
---
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/java/MockClassFile.java
(rev 0)
+++
sandbox/encore/src/test/resources/org/jboss/seam/encore/grammar/java/MockClassFile.java 2010-06-29
19:29:44 UTC (rev 13321)
@@ -0,0 +1,22 @@
+package org.jboss.seam.encore.grammar.java;
+
+import java.net.URL;
+
+public class MockClassFile
+{
+ private String field;
+ private URL urlField;
+
+ public MockClassFile()
+ {
+ }
+
+ public void method()
+ {
+ }
+
+ public String valueOf(URL url)
+ {
+ return url.getPath();
+ }
+}