diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriter.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriter.java
new file mode 100644
index 0000000000..5c2fa372a9
--- /dev/null
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriter.java
@@ -0,0 +1,43 @@
+/*
+ *
+ * Copyright 1997-2003 PMD for Eclipse Development team
+ * under sponsorship of the Defense Advanced Research Projects
+ * Agency (DARPA).
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the Cougaar Open Source License as published by
+ * DARPA on the Cougaar Open Source Website (www.cougaar.org).
+ *
+ * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
+ * PROVIDED "AS IS" WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
+ * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
+ * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
+ * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
+ * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
+ * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THE COUGAAR SOFTWARE.
+ *
+ *
+ */
+package net.sourceforge.pmd.eclipse;
+
+import java.io.Writer;
+
+import net.sourceforge.pmd.ast.ASTCompilationUnit;
+
+/**
+ * Interface of an AST Writer
+ *
+ * @author Philippe Herlin
+ * @version $Revision$
+ *
+ * $Log$
+ * Revision 1.1 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
+ */
+public interface ASTWriter {
+ void write(Writer writer, ASTCompilationUnit compilationUnit) throws PMDEclipseException;
+
+}
diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriterImpl.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriterImpl.java
new file mode 100644
index 0000000000..2a8a7bc0b6
--- /dev/null
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/ASTWriterImpl.java
@@ -0,0 +1,125 @@
+/*
+ *
+ * Copyright 1997-2003 PMD for Eclipse Development team
+ * under sponsorship of the Defense Advanced Research Projects
+ * Agency (DARPA).
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the Cougaar Open Source License as published by
+ * DARPA on the Cougaar Open Source Website (www.cougaar.org).
+ *
+ * THE COUGAAR SOFTWARE AND ANY DERIVATIVE SUPPLIED BY LICENSOR IS
+ * PROVIDED "AS IS" WITHOUT WARRANTIES OF ANY KIND, WHETHER EXPRESS OR
+ * IMPLIED, INCLUDING (BUT NOT LIMITED TO) ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND WITHOUT
+ * ANY WARRANTIES AS TO NON-INFRINGEMENT. IN NO EVENT SHALL COPYRIGHT
+ * HOLDER BE LIABLE FOR ANY DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL
+ * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE OF DATA OR PROFITS,
+ * TORTIOUS CONDUCT, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THE COUGAAR SOFTWARE.
+ *
+ *
+ */
+package net.sourceforge.pmd.eclipse;
+
+import java.io.IOException;
+import java.io.Writer;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.FactoryConfigurationError;
+import javax.xml.parsers.ParserConfigurationException;
+
+import net.sourceforge.pmd.ast.ASTCompilationUnit;
+import net.sourceforge.pmd.ast.ASTType;
+import net.sourceforge.pmd.ast.SimpleNode;
+
+import org.apache.xml.serialize.DOMSerializer;
+import org.apache.xml.serialize.OutputFormat;
+import org.apache.xml.serialize.XMLSerializer;
+import org.w3c.dom.DOMException;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+/**
+ * Implements a default AST Writer
+ *
+ * @author Philippe Herlin
+ * @version $Revision$
+ *
+ * $Log$
+ * Revision 1.1 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
+ */
+public class ASTWriterImpl implements ASTWriter {
+
+ /**
+ * @see net.sourceforge.pmd.eclipse.ASTWriter#write(java.io.Writer, net.sourceforge.pmd.ast.ASTCompilationUnit)
+ */
+ public void write(Writer writer, ASTCompilationUnit compilationUnit) throws PMDEclipseException {
+ try {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder documentBuilder = factory.newDocumentBuilder();
+ Document doc = documentBuilder.newDocument();
+
+ Element compilationUnitElement = getElement(doc, compilationUnit);
+ doc.appendChild(compilationUnitElement);
+
+ OutputFormat outputFormat = new OutputFormat(doc, "UTF-8", true);
+ DOMSerializer serializer = new XMLSerializer(writer, outputFormat);
+ serializer.serialize(doc);
+
+ } catch (DOMException e) {
+ throw new PMDEclipseException(e);
+ } catch (FactoryConfigurationError e) {
+ throw new PMDEclipseException(e);
+ } catch (ParserConfigurationException e) {
+ throw new PMDEclipseException(e);
+ } catch (IOException e) {
+ throw new PMDEclipseException(e);
+ }
+ }
+
+ /**
+ * Return a default node element
+ * @param doc the generated document
+ * @param simpleNode a simple node
+ * @return a default node element
+ */
+ private Element getElement(Document doc, SimpleNode simpleNode) {
+ Element simpleNodeElement = doc.createElement(simpleNode.toString());
+ simpleNodeElement.setAttribute("beginColumn", String.valueOf(simpleNode.getBeginColumn()));
+ simpleNodeElement.setAttribute("beginLine", String.valueOf(simpleNode.getBeginLine()));
+ simpleNodeElement.setAttribute("endColumn", String.valueOf(simpleNode.getEndColumn()));
+ simpleNodeElement.setAttribute("endLine", String.valueOf(simpleNode.getEndLine()));
+
+ if (simpleNode.getImage() != null) {
+ simpleNodeElement.setAttribute("image", simpleNode.getImage());
+ }
+
+ for (int i = 0; i < simpleNode.jjtGetNumChildren(); i++) {
+ Element element = getElement(doc, (SimpleNode) simpleNode.jjtGetChild(i));
+ simpleNodeElement.appendChild(element);
+ }
+
+ return simpleNodeElement;
+ }
+
+ /**
+ * Return a type element
+ * @param doc the generated document
+ * @param type a type node
+ * @return a type element
+ */
+ private Element getElement(Document doc, ASTType type) {
+ Element typeElement = getElement(doc, (SimpleNode) type);
+ if (type.isArray()) {
+ typeElement.setAttribute("isArray", "true");
+ typeElement.setAttribute("dimensions", String.valueOf(type.getDimensions()));
+ }
+
+ return typeElement;
+ }
+
+}
diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/DefaultWriterFactory.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/DefaultWriterFactory.java
index befb65556f..ceaa80aec3 100644
--- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/DefaultWriterFactory.java
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/DefaultWriterFactory.java
@@ -31,6 +31,9 @@ package net.sourceforge.pmd.eclipse;
* @version $Revision$
*
* $Log$
+ * Revision 1.2 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
* Revision 1.1 2003/10/16 22:26:37 phherlin
* Fix bug #810858.
* Complete refactoring of rule set generation. Using a DOM tree and the Xerces 2 serializer.
@@ -45,4 +48,11 @@ public class DefaultWriterFactory extends WriterAbstractFactory {
return new RuleSetWriterImpl();
}
+ /**
+ * @see net.sourceforge.pmd.eclipse.WriterFactory#getASTWriter()
+ */
+ public ASTWriter getASTWriter() {
+ return new ASTWriterImpl();
+ }
+
}
diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterAbstractFactory.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterAbstractFactory.java
index 297986558b..efd91d4476 100644
--- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterAbstractFactory.java
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterAbstractFactory.java
@@ -30,6 +30,9 @@ package net.sourceforge.pmd.eclipse;
* @version $Revision$
*
* $Log$
+ * Revision 1.2 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
* Revision 1.1 2003/10/16 22:26:37 phherlin
* Fix bug #810858.
* Complete refactoring of rule set generation. Using a DOM tree and the Xerces 2 serializer.
@@ -53,4 +56,10 @@ public abstract class WriterAbstractFactory implements WriterFactory {
*/
public abstract RuleSetWriter getRuleSetWriter();
+ /**
+ * Return a ast writer
+ * @return a ast writer
+ */
+ public abstract ASTWriter getASTWriter();
+
}
diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterFactory.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterFactory.java
index aac0539da8..63a671d374 100644
--- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterFactory.java
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/WriterFactory.java
@@ -29,6 +29,9 @@ package net.sourceforge.pmd.eclipse;
* @version $Revision$
*
* $Log$
+ * Revision 1.2 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
* Revision 1.1 2003/10/16 22:26:37 phherlin
* Fix bug #810858.
* Complete refactoring of rule set generation. Using a DOM tree and the Xerces 2 serializer.
@@ -40,4 +43,10 @@ public interface WriterFactory {
* @return a ruleset writer
*/
RuleSetWriter getRuleSetWriter();
+
+ /**
+ * Return a ast writer
+ * @return a ruleset writer
+ */
+ ASTWriter getASTWriter();
}
\ No newline at end of file
diff --git a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java
index 6f57dab32f..deab8f15a9 100644
--- a/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java
+++ b/pmd-eclipse/src/net/sourceforge/pmd/eclipse/actions/PMDGenerateASTAction.java
@@ -1,15 +1,18 @@
package net.sourceforge.pmd.eclipse.actions;
import java.io.ByteArrayInputStream;
-import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Iterator;
+import net.sourceforge.pmd.ast.ASTCompilationUnit;
import net.sourceforge.pmd.ast.JavaParser;
import net.sourceforge.pmd.ast.ParseException;
-import net.sourceforge.pmd.ast.SimpleNode;
+import net.sourceforge.pmd.eclipse.ASTWriter;
import net.sourceforge.pmd.eclipse.PMDConstants;
+import net.sourceforge.pmd.eclipse.PMDEclipseException;
import net.sourceforge.pmd.eclipse.PMDPlugin;
+import net.sourceforge.pmd.eclipse.WriterAbstractFactory;
+
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.core.resources.IContainer;
@@ -33,6 +36,9 @@ import org.eclipse.ui.IWorkbenchPart;
* @version $Revision$
*
* $Log$
+ * Revision 1.6 2003/10/27 20:14:13 phherlin
+ * Refactoring AST generation. Using a ASTWriter.
+ *
* Revision 1.5 2003/06/19 20:59:45 phherlin
* In the generated XML AST, put the image information on an image attribute instead of the tag body
*
@@ -45,14 +51,6 @@ import org.eclipse.ui.IWorkbenchPart;
*
*/
public class PMDGenerateASTAction implements IObjectActionDelegate {
- private static final String QUOTE = "\"";
- private static final String INDENT = "\t";
- private static final String TAG_BEGIN = "<";
- private static final String TAG_END = ">";
- private static final String TAG_ENDEND = "/>";
- private static final String ENDTAG_BEGIN = TAG_BEGIN + "/";
- private static final String ENDTAG_END = TAG_END;
-
private static Log log = LogFactory.getLog("net.sourceforge.pmd.eclipse.actions.PMDGenerateASTAction");
private IWorkbenchPart targetPart;
@@ -97,12 +95,11 @@ public class PMDGenerateASTAction implements IObjectActionDelegate {
log.info("Genrating AST for file " + file.getName());
try {
JavaParser parser = new JavaParser(file.getContents());
- SimpleNode root = parser.CompilationUnit();
- StringWriter sr = new StringWriter();
- PrintWriter pr = new PrintWriter(sr);
- pr.println("");
- dump(root, pr, "");
- pr.flush();
+ ASTCompilationUnit compilationUnit = parser.CompilationUnit();
+ StringWriter stringWriter = new StringWriter();
+ ASTWriter astWriter = WriterAbstractFactory.getFactory().getASTWriter();
+ astWriter.write(stringWriter, compilationUnit);
+ stringWriter.flush();
String name = file.getName();
int dotPosition = name.indexOf('.');
@@ -120,7 +117,7 @@ public class PMDGenerateASTAction implements IObjectActionDelegate {
if (astFile.exists()) {
astFile.delete(false, null);
}
- ByteArrayInputStream astInputStream = new ByteArrayInputStream(sr.toString().getBytes());
+ ByteArrayInputStream astInputStream = new ByteArrayInputStream(stringWriter.toString().getBytes());
astFile.create(astInputStream, false, null);
}
@@ -128,49 +125,8 @@ public class PMDGenerateASTAction implements IObjectActionDelegate {
PMDPlugin.getDefault().showError(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_CORE_EXCEPTION), e);
} catch (ParseException e) {
PMDPlugin.getDefault().showError(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_PMD_EXCEPTION), e);
- }
- }
-
- /**
- * Dump a node and its subnodes -> recursive
- * @param node a node
- * @param out a print writer to write into
- * @param prefix texte to print before the name of the node
- */
- private void dump(SimpleNode node, PrintWriter out, String prefix) {
- StringBuffer sb = new StringBuffer(prefix);
- sb
- .append(TAG_BEGIN)
- .append(node.toString())
- .append(" beginLine=" + QUOTE)
- .append(node.getBeginLine())
- .append(QUOTE)
- .append(" beginColumn=" + QUOTE)
- .append(node.getBeginColumn())
- .append(QUOTE)
- .append(" endLine=" + QUOTE)
- .append(node.getEndLine())
- .append(QUOTE)
- .append(" endColumn=" + QUOTE)
- .append(node.getEndColumn())
- .append(QUOTE);
-
- if (node.getImage() != null) {
- sb.append(" image=").append(QUOTE).append(node.getImage()).append(QUOTE);
- }
-
- if (node.jjtGetNumChildren() == 0) {
- sb.append(TAG_ENDEND);
- out.println(sb.toString());
- } else {
- sb.append(">");
- out.println(sb.toString());
-
- for (int i = 0; i < node.jjtGetNumChildren(); i++) {
- dump((SimpleNode) node.jjtGetChild(i), out, prefix + INDENT);
- }
-
- out.println(prefix + ENDTAG_BEGIN + node.toString() + ENDTAG_END);
+ } catch (PMDEclipseException e) {
+ PMDPlugin.getDefault().showError(PMDPlugin.getDefault().getMessage(PMDConstants.MSGKEY_ERROR_PMD_EXCEPTION), e);
}
}