Search in sources :

Example 1 with CompoundIterator

use of net.sourceforge.pmd.util.CompoundIterator in project pmd by pmd.

the class XmlNodeWrapper method getAttributeIterator.

@Override
public Iterator<Attribute> getAttributeIterator() {
    List<Iterator<Attribute>> iterators = new ArrayList<>();
    // Expose DOM Attributes
    final NamedNodeMap attributes = node.getAttributes();
    iterators.add(new Iterator<Attribute>() {

        private int index;

        @Override
        public boolean hasNext() {
            return attributes != null && index < attributes.getLength();
        }

        @Override
        public Attribute next() {
            org.w3c.dom.Node attributeNode = attributes.item(index++);
            return new Attribute(parser.wrapDomNode(node), attributeNode.getNodeName(), attributeNode.getNodeValue());
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }
    });
    // Expose Text/CDATA nodes to have an 'Image' attribute like AST Nodes
    if (node instanceof Text) {
        iterators.add(Collections.singletonList(new Attribute(this, "Image", ((Text) node).getData())).iterator());
    }
    // Expose Java Attributes
    // iterators.add(new AttributeAxisIterator((net.sourceforge.pmd.lang.ast.Node) p));
    @SuppressWarnings("unchecked") Iterator<Attribute>[] it = (Iterator<Attribute>[]) new Iterator[iterators.size()];
    return new CompoundIterator<>(iterators.toArray(it));
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Node(net.sourceforge.pmd.lang.ast.Node) CompoundIterator(net.sourceforge.pmd.util.CompoundIterator) ArrayList(java.util.ArrayList) Text(org.w3c.dom.Text) Iterator(java.util.Iterator) CompoundIterator(net.sourceforge.pmd.util.CompoundIterator)

Aggregations

ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 Node (net.sourceforge.pmd.lang.ast.Node)1 Attribute (net.sourceforge.pmd.lang.ast.xpath.Attribute)1 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)1 CompoundIterator (net.sourceforge.pmd.util.CompoundIterator)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 Text (org.w3c.dom.Text)1