A much-improved implementation of asXML(); thx to Wim Deblauwe for coordinating and coding this up!
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@3531 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -394,6 +394,4 @@ Moved Ant task to the net.sourceforge.pmd.ant package
|
|||||||
Added new HTML report format
|
Added new HTML report format
|
||||||
|
|
||||||
June 25 2002 - 0.1:
|
June 25 2002 - 0.1:
|
||||||
Initial release
|
Initial release
|
||||||
|
|
||||||
|
|
@ -1,7 +1,5 @@
|
|||||||
package test.net.sourceforge.pmd.ast;
|
package test.net.sourceforge.pmd.ast;
|
||||||
|
|
||||||
import net.sourceforge.pmd.ast.ASTAnnotation;
|
|
||||||
import net.sourceforge.pmd.ast.ASTAnnotationTypeDeclaration;
|
|
||||||
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
import net.sourceforge.pmd.ast.ASTClassOrInterfaceDeclaration;
|
||||||
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
import net.sourceforge.pmd.ast.ASTCompilationUnit;
|
||||||
import net.sourceforge.pmd.ast.ASTEqualityExpression;
|
import net.sourceforge.pmd.ast.ASTEqualityExpression;
|
||||||
@ -10,8 +8,14 @@ import net.sourceforge.pmd.ast.ASTModifiers;
|
|||||||
import net.sourceforge.pmd.ast.ASTRelationalExpression;
|
import net.sourceforge.pmd.ast.ASTRelationalExpression;
|
||||||
import net.sourceforge.pmd.ast.ASTTypeDeclaration;
|
import net.sourceforge.pmd.ast.ASTTypeDeclaration;
|
||||||
import net.sourceforge.pmd.ast.DiscardableNodeCleaner;
|
import net.sourceforge.pmd.ast.DiscardableNodeCleaner;
|
||||||
|
import net.sourceforge.pmd.ast.ASTImplementsList;
|
||||||
|
import net.sourceforge.pmd.ast.ASTAnnotation;
|
||||||
|
import net.sourceforge.pmd.ast.ASTAnnotationTypeDeclaration;
|
||||||
|
import net.sourceforge.pmd.PMD;
|
||||||
import test.net.sourceforge.pmd.testframework.ParserTst;
|
import test.net.sourceforge.pmd.testframework.ParserTst;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class DiscardableNodeCleanerTest extends ParserTst {
|
public class DiscardableNodeCleanerTest extends ParserTst {
|
||||||
|
|
||||||
public void testRemoveDiscardNodes() throws Throwable {
|
public void testRemoveDiscardNodes() throws Throwable {
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -3,15 +3,20 @@ package net.sourceforge.pmd.ast;
|
|||||||
|
|
||||||
import net.sourceforge.pmd.IPositionProvider;
|
import net.sourceforge.pmd.IPositionProvider;
|
||||||
import net.sourceforge.pmd.dfa.IDataFlowNode;
|
import net.sourceforge.pmd.dfa.IDataFlowNode;
|
||||||
|
import net.sourceforge.pmd.jaxen.Attribute;
|
||||||
import net.sourceforge.pmd.jaxen.DocumentNavigator;
|
import net.sourceforge.pmd.jaxen.DocumentNavigator;
|
||||||
import net.sourceforge.pmd.symboltable.Scope;
|
import net.sourceforge.pmd.symboltable.Scope;
|
||||||
|
import org.apache.xerces.dom.DocumentImpl;
|
||||||
import org.jaxen.BaseXPath;
|
import org.jaxen.BaseXPath;
|
||||||
import org.jaxen.JaxenException;
|
import org.jaxen.JaxenException;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class SimpleNode implements Node , IPositionProvider {
|
public class SimpleNode implements Node, IPositionProvider {
|
||||||
protected Node parent;
|
protected Node parent;
|
||||||
protected Node[] children;
|
protected Node[] children;
|
||||||
protected int id;
|
protected int id;
|
||||||
@ -38,7 +43,7 @@ public class SimpleNode implements Node , IPositionProvider {
|
|||||||
|
|
||||||
public void discardIfNecessary() {
|
public void discardIfNecessary() {
|
||||||
if (discardable) {
|
if (discardable) {
|
||||||
SimpleNode parent = (SimpleNode)this.jjtGetParent();
|
SimpleNode parent = (SimpleNode) this.jjtGetParent();
|
||||||
SimpleNode kid = (SimpleNode) this.jjtGetChild(0);
|
SimpleNode kid = (SimpleNode) this.jjtGetChild(0);
|
||||||
kid.jjtSetParent(parent);
|
kid.jjtSetParent(parent);
|
||||||
parent.jjtReplaceChild(this, kid);
|
parent.jjtReplaceChild(this, kid);
|
||||||
@ -187,11 +192,11 @@ public class SimpleNode implements Node , IPositionProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!descendIntoNestedClasses) {
|
if (!descendIntoNestedClasses) {
|
||||||
if (node instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration)node).isNested()) {
|
if (node instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) node).isNested()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (node instanceof ASTClassOrInterfaceBodyDeclaration && ((ASTClassOrInterfaceBodyDeclaration)node).isAnonymousInnerClass()) {
|
if (node instanceof ASTClassOrInterfaceBodyDeclaration && ((ASTClassOrInterfaceBodyDeclaration) node).isAnonymousInnerClass()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -278,34 +283,33 @@ public class SimpleNode implements Node , IPositionProvider {
|
|||||||
return prefix + toString();
|
return prefix + toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Document asXml() {
|
||||||
*
|
Document document = new DocumentImpl();
|
||||||
* @return a String with an XML representation of this node and its children
|
appendElement(document);
|
||||||
*/
|
return document;
|
||||||
public String asXml() {
|
|
||||||
String cn = getClass().getName();
|
|
||||||
if (cn.indexOf('.') != -1) {
|
|
||||||
cn = cn.substring(cn.lastIndexOf('.')+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
final StringBuffer sb = new StringBuffer();
|
|
||||||
|
|
||||||
sb.append('<');
|
|
||||||
sb.append(cn);
|
|
||||||
sb.append(" id=\"");
|
|
||||||
sb.append(id);
|
|
||||||
sb.append("\">");
|
|
||||||
if (children!=null) {
|
|
||||||
for (int i=0;i<children.length;i++) {
|
|
||||||
sb.append(((SimpleNode) children[i]).asXml());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sb.append("</");
|
|
||||||
sb.append(cn);
|
|
||||||
sb.append('>');
|
|
||||||
return sb.toString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void appendElement(org.w3c.dom.Node parentNode) {
|
||||||
|
DocumentNavigator docNav = new DocumentNavigator();
|
||||||
|
Document ownerDocument = parentNode.getOwnerDocument();
|
||||||
|
if( ownerDocument == null )
|
||||||
|
{
|
||||||
|
//If the parentNode is a Document itself, it's ownerDocument is null
|
||||||
|
ownerDocument = (Document) parentNode;
|
||||||
|
}
|
||||||
|
String elementName = docNav.getElementName(this);
|
||||||
|
Element element = ownerDocument.createElement(elementName);
|
||||||
|
parentNode.appendChild( element );
|
||||||
|
for (Iterator iter = docNav.getAttributeAxisIterator(this); iter.hasNext();) {
|
||||||
|
Attribute attr = (Attribute) iter.next();
|
||||||
|
element.setAttribute(attr.getName(), attr.getValue());
|
||||||
|
}
|
||||||
|
for (Iterator iter = docNav.getChildAxisIterator(this); iter.hasNext();) {
|
||||||
|
SimpleNode child = (SimpleNode) iter.next();
|
||||||
|
child.appendElement(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Override this method if you want to customize how the node dumps
|
/* Override this method if you want to customize how the node dumps
|
||||||
out its children. */
|
out its children. */
|
||||||
public void dump(String prefix) {
|
public void dump(String prefix) {
|
||||||
@ -326,7 +330,7 @@ public class SimpleNode implements Node , IPositionProvider {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Traverses down the tree to find the first child instance of type childType
|
* Traverses down the tree to find the first child instance of type childType
|
||||||
*
|
*
|
||||||
* @param childType class which you want to find.
|
* @param childType class which you want to find.
|
||||||
* @return Node of type childType. Returns <code>null</code> if none found.
|
* @return Node of type childType. Returns <code>null</code> if none found.
|
||||||
@ -336,23 +340,23 @@ public class SimpleNode implements Node , IPositionProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Node getFirstChildOfType(Class childType, Node node) {
|
private Node getFirstChildOfType(Class childType, Node node) {
|
||||||
for (int i=0;i<node.jjtGetNumChildren();i++) {
|
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
||||||
Node n = node.jjtGetChild(i);
|
Node n = node.jjtGetChild(i);
|
||||||
if (n!=null) {
|
if (n != null) {
|
||||||
if (n.getClass().equals(childType))
|
if (n.getClass().equals(childType))
|
||||||
return n;
|
return n;
|
||||||
Node n2 = getFirstChildOfType(childType, n);
|
Node n2 = getFirstChildOfType(childType, n);
|
||||||
if (n2!=null)
|
if (n2 != null)
|
||||||
return n2;
|
return n2;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds if this node contains a child of the given type.
|
* Finds if this node contains a child of the given type.
|
||||||
* This is an utility method that uses {@link #findChildrenOfType(Class)}
|
* This is an utility method that uses {@link #findChildrenOfType(Class)}
|
||||||
*
|
*
|
||||||
* @param type the node type to search
|
* @param type the node type to search
|
||||||
* @return <code>true</code> if there is at lease on child of the given type and <code>false</code> in any other case
|
* @return <code>true</code> if there is at lease on child of the given type and <code>false</code> in any other case
|
||||||
*/
|
*/
|
||||||
|
@ -45,6 +45,7 @@ public class CodeEditorTextPane extends JTextPane implements HasLines, ActionLis
|
|||||||
if (fw != null)
|
if (fw != null)
|
||||||
fw.close();
|
fw.close();
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
|
ioe.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,9 @@ import net.sourceforge.pmd.jaxen.DocumentNavigator;
|
|||||||
import org.jaxen.BaseXPath;
|
import org.jaxen.BaseXPath;
|
||||||
import org.jaxen.JaxenException;
|
import org.jaxen.JaxenException;
|
||||||
import org.jaxen.XPath;
|
import org.jaxen.XPath;
|
||||||
|
import org.w3c.dom.Document;
|
||||||
|
import org.apache.xml.serialize.OutputFormat;
|
||||||
|
import org.apache.xml.serialize.XMLSerializer;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
@ -32,6 +35,8 @@ import java.awt.event.KeyEvent;
|
|||||||
import java.awt.event.MouseEvent;
|
import java.awt.event.MouseEvent;
|
||||||
import java.awt.event.MouseListener;
|
import java.awt.event.MouseListener;
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -293,10 +298,38 @@ public class Designer implements ClipboardOwner {
|
|||||||
|
|
||||||
private String getXml() {
|
private String getXml() {
|
||||||
ASTCompilationUnit cu = createParser().CompilationUnit();
|
ASTCompilationUnit cu = createParser().CompilationUnit();
|
||||||
|
String result;
|
||||||
if (cu!=null) {
|
if (cu!=null) {
|
||||||
return cu.asXml();
|
try {
|
||||||
|
result = getXmlString( cu );
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
result = "Error trying to construct XML representation";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return "";
|
else
|
||||||
|
{
|
||||||
|
result = "";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an unformatted xml string (without the declaration)
|
||||||
|
*
|
||||||
|
* @param node
|
||||||
|
* @return
|
||||||
|
* @throws java.io.IOException
|
||||||
|
*/
|
||||||
|
private String getXmlString(SimpleNode node) throws IOException {
|
||||||
|
Document document = node.asXml();
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
OutputFormat outputFormat = new OutputFormat("XML", "UTF-8", true);
|
||||||
|
XMLSerializer xmlSerializer = new XMLSerializer(writer, outputFormat);
|
||||||
|
xmlSerializer.asDOMSerializer();
|
||||||
|
xmlSerializer.serialize(document);
|
||||||
|
String xmlString = writer.toString();
|
||||||
|
return xmlString;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void lostOwnership(Clipboard clipboard, Transferable contents) {
|
public void lostOwnership(Clipboard clipboard, Transferable contents) {
|
||||||
|
@ -43,8 +43,8 @@
|
|||||||
</subsection>
|
</subsection>
|
||||||
<subsection name="Contributors">
|
<subsection name="Contributors">
|
||||||
<ul>
|
<ul>
|
||||||
|
<li>Wim Deblauwe - Coordinated and coded a much nicer asXML() implementation, suggested cleanup of UnusedFormalParameter, Javadoc patch, SystemPrintln bug report, helped get Ant task and CLI squared away with JDK 1.5 params, JDK 1.5-specific bug reports, suggested improvements for ExceptionSignatureDeclaration</li>
|
||||||
<li>Tom Parker - Found missed case in NullAssignment, suggested addition to UnnecessaryBooleanAssertion, suggested splitting up AvoidThrowingCertainExceptionTypes, AvoidInstantiatingObjectsInLoops bug report, AtLeastOneConstructor bug report</li>
|
<li>Tom Parker - Found missed case in NullAssignment, suggested addition to UnnecessaryBooleanAssertion, suggested splitting up AvoidThrowingCertainExceptionTypes, AvoidInstantiatingObjectsInLoops bug report, AtLeastOneConstructor bug report</li>
|
||||||
<li>Wim Deblauwe - suggested cleanup of UnusedFormalParameter, Javadoc patch, SystemPrintln bug report, helped get Ant task and CLI squared away with JDK 1.5 params, JDK 1.5-specific bug reports, suggested improvements for ExceptionSignatureDeclaration</li>
|
|
||||||
<li>Glen Cordrey - Reported bug involved JavaCC string handling</li>
|
<li>Glen Cordrey - Reported bug involved JavaCC string handling</li>
|
||||||
<li>Dave Brosius - a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
|
<li>Dave Brosius - a couple of nice patches to clean up some string handling inefficiencies, non-static class usages, and unclosed streams/readers - found with Findbugs, I daresay :-)</li>
|
||||||
<li>Oto 'tapik' Buchta - Patched XMLRenderer for UTF8 support</li>
|
<li>Oto 'tapik' Buchta - Patched XMLRenderer for UTF8 support</li>
|
||||||
|
Reference in New Issue
Block a user