Search in sources :

Example 11 with Attribute

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

the class XmlParserTest method assertNode.

/**
 * Asserts a single node inclusive attributes.
 *
 * @param node
 *            the node
 * @param toString
 *            the to String representation to expect
 * @param childs
 *            number of childs
 * @param atts
 *            attributes - each object pair forms one attribute: first name,
 *            then value.
 */
private void assertNode(Node node, String toString, int childs, Object... atts) {
    Assert.assertEquals(toString, String.valueOf(node));
    Assert.assertEquals(childs, node.jjtGetNumChildren());
    Iterator<Attribute> attributeIterator = ((XmlNode) node).getAttributeIterator();
    if (atts != null) {
        for (int i = 0; i < atts.length; i += 2) {
            Assert.assertTrue(attributeIterator.hasNext());
            String name = String.valueOf(atts[i]);
            Object value = atts[i + 1];
            Attribute attribute = attributeIterator.next();
            Assert.assertEquals(name, attribute.getName());
            Assert.assertEquals(value, attribute.getValue());
        }
    }
    Assert.assertFalse(attributeIterator.hasNext());
}
Also used : XmlNode(net.sourceforge.pmd.lang.xml.ast.XmlNode) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute)

Example 12 with Attribute

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

the class MatchesFunction method call.

@Override
public Object call(Context context, List args) throws FunctionCallException {
    if (args.isEmpty()) {
        return Boolean.FALSE;
    }
    List attributes = (List) args.get(0);
    Attribute attr = (Attribute) attributes.get(0);
    for (int i = 1; i < args.size(); i++) {
        Pattern check = Pattern.compile((String) args.get(i));
        Matcher matcher = check.matcher(attr.getStringValue());
        if (matcher.find()) {
            return context.getNodeSet();
        }
    }
    return Boolean.FALSE;
}
Also used : Pattern(java.util.regex.Pattern) Attribute(net.sourceforge.pmd.lang.ast.xpath.Attribute) Matcher(java.util.regex.Matcher) List(java.util.List)

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