From 5cf7891d710b5b166d4d47174eea39dcaa491c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Fournier?= Date: Sun, 10 Jun 2018 03:26:28 +0200 Subject: [PATCH] Add getXPathAttributes() to Node interface Deprecate AttributeNode --- .../net/sourceforge/pmd/lang/ast/AbstractNode.java | 7 +++++++ .../main/java/net/sourceforge/pmd/lang/ast/Node.java | 10 ++++++++++ .../pmd/lang/ast/xpath/AttributeNode.java | 5 +++++ .../pmd/lang/ast/xpath/DocumentNavigator.java | 6 +----- .../sourceforge/pmd/lang/ast/xpath/NodeIterator.java | 5 ++++- .../lang/ast/xpath/saxon/AttributeAxisIterator.java | 7 +++---- .../pmd/lang/ast/xpath/saxon/AttributeNode.java | 8 ++++++++ .../pmd/util/fxdesigner/NodeInfoPanelController.java | 4 +--- .../sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java | 12 +++++++++++- 9 files changed, 50 insertions(+), 14 deletions(-) diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java index 2d2b60012e..f43c03ab64 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/AbstractNode.java @@ -21,6 +21,7 @@ import org.w3c.dom.Element; import net.sourceforge.pmd.PMDVersion; import net.sourceforge.pmd.lang.ast.xpath.Attribute; +import net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator; import net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator; import net.sourceforge.pmd.lang.dfa.DataFlowNode; @@ -515,4 +516,10 @@ public abstract class AbstractNode implements Node { public String toString() { return getXPathNodeName(); } + + + @Override + public Iterator getXPathAttributes() { + return new AttributeAxisIterator(this); + } } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java index 204cc3c859..a905195701 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/Node.java @@ -5,11 +5,13 @@ package net.sourceforge.pmd.lang.ast; +import java.util.Iterator; import java.util.List; import org.jaxen.JaxenException; import org.w3c.dom.Document; +import net.sourceforge.pmd.lang.ast.xpath.Attribute; import net.sourceforge.pmd.lang.dfa.DataFlowNode; /** @@ -322,4 +324,12 @@ public interface Node { * @return The XPath node name */ String getXPathNodeName(); + + + /** + * Returns an iterator containing all the attributes that are available + * from XPath for this node. + */ + Iterator getXPathAttributes(); + } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeNode.java index f34343f133..b08b8e579f 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/AttributeNode.java @@ -6,11 +6,16 @@ package net.sourceforge.pmd.lang.ast.xpath; import java.util.Iterator; + /** * This interface can be used by an AST node to indicate it can directly provide * access to it's attributes, versus having them be determined via * introspection. + * + * @deprecated See {@link net.sourceforge.pmd.lang.ast.Node#getXPathAttributes()}. + * Will be removed in 7.0.0 */ +@Deprecated public interface AttributeNode { Iterator getAttributeIterator(); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DocumentNavigator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DocumentNavigator.java index f56743ddb4..4369e33606 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DocumentNavigator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/DocumentNavigator.java @@ -135,11 +135,7 @@ public class DocumentNavigator extends DefaultNavigator { @Override public Iterator getAttributeAxisIterator(Object arg0) { - if (arg0 instanceof AttributeNode) { - return ((AttributeNode) arg0).getAttributeIterator(); - } else { - return new AttributeAxisIterator((Node) arg0); - } + return ((Node) arg0).getXPathAttributes(); } /** diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NodeIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NodeIterator.java index 7f1cb1867a..2d189cfcef 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NodeIterator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/NodeIterator.java @@ -10,13 +10,16 @@ import java.util.NoSuchElementException; import net.sourceforge.pmd.lang.ast.Node; /** + * Base class for node iterators used to implement XPath axis + * iterators for Jaxen. + * * @author daniels */ public abstract class NodeIterator implements Iterator { private Node node; - public NodeIterator(Node contextNode) { + protected NodeIterator(Node contextNode) { this.node = getFirstNode(contextNode); } diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeAxisIterator.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeAxisIterator.java index e8c8e57b43..db767a8bd3 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeAxisIterator.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeAxisIterator.java @@ -12,7 +12,8 @@ import net.sf.saxon.om.Navigator; import net.sf.saxon.om.SequenceIterator; /** - * This is an Attribute axis iterator. + * An adapter over our {@link net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator} + * for the Saxon model. */ public class AttributeAxisIterator extends Navigator.BaseEnumeration { @@ -26,9 +27,7 @@ public class AttributeAxisIterator extends Navigator.BaseEnumeration { */ public AttributeAxisIterator(ElementNode startNodeInfo) { this.startNodeInfo = startNodeInfo; - this.iterator = startNodeInfo.node instanceof net.sourceforge.pmd.lang.ast.xpath.AttributeNode - ? ((net.sourceforge.pmd.lang.ast.xpath.AttributeNode) startNodeInfo.node).getAttributeIterator() - : new net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator(startNodeInfo.node); + this.iterator = startNodeInfo.node.getXPathAttributes(); } @Override diff --git a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeNode.java b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeNode.java index 913f00caec..69e93cddb4 100644 --- a/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeNode.java +++ b/pmd-core/src/main/java/net/sourceforge/pmd/lang/ast/xpath/saxon/AttributeNode.java @@ -15,12 +15,20 @@ import net.sf.saxon.value.Value; /** * A Saxon OM Attribute node for an AST Node Attribute. + * Belongs to an {@link ElementNode}, and wraps an + * {@link Attribute}. */ public class AttributeNode extends AbstractNodeInfo { protected final Attribute attribute; protected final int id; protected Value value; + + /** + * Creates a new AttributeNode from a PMD Attribute. + * + * @param id The index within the attribute order + */ public AttributeNode(Attribute attribute, int id) { this.attribute = attribute; this.id = id; diff --git a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/NodeInfoPanelController.java b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/NodeInfoPanelController.java index ef594572fc..f939675032 100644 --- a/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/NodeInfoPanelController.java +++ b/pmd-ui/src/main/java/net/sourceforge/pmd/util/fxdesigner/NodeInfoPanelController.java @@ -14,8 +14,6 @@ import org.reactfx.EventStreams; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.ast.xpath.Attribute; -import net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator; -import net.sourceforge.pmd.lang.ast.xpath.AttributeNode; import net.sourceforge.pmd.lang.java.ast.TypeNode; import net.sourceforge.pmd.lang.symboltable.NameDeclaration; import net.sourceforge.pmd.util.fxdesigner.model.MetricEvaluator; @@ -140,7 +138,7 @@ public class NodeInfoPanelController implements Initializable { */ private static ObservableList getAttributes(Node node) { ObservableList result = FXCollections.observableArrayList(); - Iterator attributeAxisIterator = node instanceof AttributeNode ? ((AttributeNode) node).getAttributeIterator() : new AttributeAxisIterator(node); + Iterator attributeAxisIterator = node.getXPathAttributes(); while (attributeAxisIterator.hasNext()) { Attribute attribute = attributeAxisIterator.next(); // TODO the display should be handled in a ListCell diff --git a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java index 7ae16a7dfb..d3436300af 100644 --- a/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java +++ b/pmd-xml/src/main/java/net/sourceforge/pmd/lang/xml/ast/XmlNodeWrapper.java @@ -204,7 +204,7 @@ public class XmlNodeWrapper extends AbstractDomNodeProxy implements XmlNode { @Override - public Iterator getAttributeIterator() { + public Iterator getXPathAttributes() { List> iterators = new ArrayList<>(); // Expose DOM Attributes @@ -249,6 +249,16 @@ public class XmlNodeWrapper extends AbstractDomNodeProxy implements XmlNode { } + /** + * @deprecated use {@link #getXPathAttributes()} + */ + @Override + @Deprecated + public Iterator getAttributeIterator() { + return getXPathAttributes(); + } + + @Override public org.w3c.dom.Node getNode() { return node;