Add getXPathAttributes() to Node interface

Deprecate AttributeNode
This commit is contained in:
Clément Fournier
2018-06-10 03:26:28 +02:00
parent bf466b8b84
commit 5cf7891d71
9 changed files with 50 additions and 14 deletions

View File

@ -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<Attribute> getXPathAttributes() {
return new AttributeAxisIterator(this);
}
}

View File

@ -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<Attribute> getXPathAttributes();
}

View File

@ -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<Attribute> getAttributeIterator();
}

View File

@ -135,11 +135,7 @@ public class DocumentNavigator extends DefaultNavigator {
@Override
public Iterator<Attribute> getAttributeAxisIterator(Object arg0) {
if (arg0 instanceof AttributeNode) {
return ((AttributeNode) arg0).getAttributeIterator();
} else {
return new AttributeAxisIterator((Node) arg0);
}
return ((Node) arg0).getXPathAttributes();
}
/**

View File

@ -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<Node> {
private Node node;
public NodeIterator(Node contextNode) {
protected NodeIterator(Node contextNode) {
this.node = getFirstNode(contextNode);
}

View File

@ -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

View File

@ -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;

View File

@ -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<String> getAttributes(Node node) {
ObservableList<String> result = FXCollections.observableArrayList();
Iterator<Attribute> attributeAxisIterator = node instanceof AttributeNode ? ((AttributeNode) node).getAttributeIterator() : new AttributeAxisIterator(node);
Iterator<Attribute> attributeAxisIterator = node.getXPathAttributes();
while (attributeAxisIterator.hasNext()) {
Attribute attribute = attributeAxisIterator.next();
// TODO the display should be handled in a ListCell

View File

@ -204,7 +204,7 @@ public class XmlNodeWrapper extends AbstractDomNodeProxy implements XmlNode {
@Override
public Iterator<Attribute> getAttributeIterator() {
public Iterator<Attribute> getXPathAttributes() {
List<Iterator<Attribute>> 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<Attribute> getAttributeIterator() {
return getXPathAttributes();
}
@Override
public org.w3c.dom.Node getNode() {
return node;