Search in sources :

Example 6 with Attribute

use of net.sourceforge.pmd.lang.ast.xpath.Attribute in project pmd by pmd.

the class TypeOfFunction method call.

public Object call(Context context, List args) throws FunctionCallException {
    String nodeTypeName = null;
    String fullTypeName = null;
    String shortTypeName = null;
    Attribute attr = null;
    for (int i = 0; i < args.size(); i++) {
        if (args.get(i) instanceof List) {
            if (attr == null) {
                attr = ((List<Attribute>) args.get(i)).get(0);
                nodeTypeName = attr.getStringValue();
            } else {
                throw new IllegalArgumentException("typeof function can take only a single argument which is an Attribute.");
            }
        } else {
            if (fullTypeName == null) {
                fullTypeName = (String) args.get(i);
            } else if (shortTypeName == null) {
                shortTypeName = (String) args.get(i);
            } else {
                break;
            }
        }
    }
    if (fullTypeName == null) {
        throw new IllegalArgumentException("typeof function must be given at least one String argument for the fully qualified type name.");
    }
    Node n = (Node) context.getNodeSet().get(0);
    return typeof(n, nodeTypeName, fullTypeName, shortTypeName);
}
Also used : Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) List(java.util.List)

Example 7 with Attribute

use of net.sourceforge.pmd.lang.ast.xpath.Attribute in project pmd by pmd.

the class MatchesFunctionTest method tryRegexp.

private Object tryRegexp(MyNode myNode, String exp) throws FunctionCallException, NoSuchMethodException {
    MatchesFunction function = new MatchesFunction();
    List<Object> list = new ArrayList<>();
    List<Attribute> attrs = new ArrayList<>();
    attrs.add(new Attribute(myNode, "matches", myNode.getClass().getMethod("getClassName", new Class[0])));
    list.add(attrs);
    list.add(exp);
    Context c = new Context(null);
    c.setNodeSet(new ArrayList<>());
    return function.call(c, list);
}
Also used : Context(org.jaxen.Context) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) ArrayList(java.util.ArrayList) MatchesFunction(net.sourceforge.pmd.lang.xpath.MatchesFunction)

Example 8 with Attribute

use of net.sourceforge.pmd.lang.ast.xpath.Attribute in project pmd by pmd.

the class AttributesSubMenu method init.

private void init() {
    AttributeAxisIterator i = new AttributeAxisIterator(node);
    while (i.hasNext()) {
        Attribute attribute = i.next();
        add(new XPathFragmentAddingItem(attribute.getName() + " = " + attribute.getValue(), model, AttributeToolkit.constructPredicate(attribute)));
    }
}
Also used : Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) AttributeAxisIterator(net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator)

Example 9 with Attribute

use of net.sourceforge.pmd.lang.ast.xpath.Attribute in project pmd by pmd.

the class DumpFacade method dump.

private void dump(XmlNode node, String prefix) {
    // 
    // Dump format is generally composed of the following items...
    // 
    // 1) Dump prefix
    writer.print(prefix);
    // 2) JJT Name of the Node
    writer.print(node.getXPathNodeName());
    // 
    // If there are any additional details, then:
    // 1) A colon
    // 2) The Node.getImage() if it is non-empty
    // 3) Extras in parentheses
    // 
    // Standard image handling
    String image = node.getImage();
    // Special image handling (e.g. Nodes with normally null images)
    image = StringUtil.escapeWhitespace(image);
    // Extras
    List<String> extras = new ArrayList<>();
    Iterator<Attribute> iterator = node.getAttributeIterator();
    while (iterator.hasNext()) {
        Attribute attribute = iterator.next();
        extras.add(attribute.getName() + "=" + StringUtil.escapeWhitespace(attribute.getValue()));
    }
    // Output image and extras
    if (image != null || !extras.isEmpty()) {
        writer.print(':');
        if (image != null) {
            writer.print(image);
        }
        for (String extra : extras) {
            writer.print('(');
            writer.print(extra);
            writer.print(')');
        }
    }
    writer.println();
}
Also used : Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) ArrayList(java.util.ArrayList)

Example 10 with Attribute

use of net.sourceforge.pmd.lang.ast.xpath.Attribute 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

Attribute (net.sourceforge.pmd.lang.ast.xpath.Attribute)12 ArrayList (java.util.ArrayList)3 List (java.util.List)2 Node (net.sourceforge.pmd.lang.ast.Node)2 AttributeAxisIterator (net.sourceforge.pmd.lang.ast.xpath.AttributeAxisIterator)2 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)2 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)2 XmlNode (net.sourceforge.pmd.lang.xml.ast.XmlNode)2 Method (java.lang.reflect.Method)1 Iterator (java.util.Iterator)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 DummyNode (net.sourceforge.pmd.lang.ast.DummyNode)1 DocumentNavigator (net.sourceforge.pmd.lang.ast.xpath.DocumentNavigator)1 MatchesFunction (net.sourceforge.pmd.lang.xpath.MatchesFunction)1 CompoundIterator (net.sourceforge.pmd.util.CompoundIterator)1 Context (org.jaxen.Context)1 Test (org.junit.Test)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1