Example usage for org.apache.commons.jxpath JXPathContext selectNodes

List of usage examples for org.apache.commons.jxpath JXPathContext selectNodes

Introduction

In this page you can find the example usage for org.apache.commons.jxpath JXPathContext selectNodes.

Prototype

public List selectNodes(String xpath) 

Source Link

Document

Finds all nodes that match the specified XPath.

Usage

From source file:org.geotools.util.XmlXpathUtilites.java

private static List<String> getXPathValues(String xpathString, JXPathContext context) {

    List values = null;//from w  w w  .  j ava2  s  .co m
    try {
        values = context.selectNodes(xpathString);
    } catch (RuntimeException e) {
        throw new RuntimeException("Error reading xpath " + xpathString, e);
    }

    List<String> ls = null;
    if (values == null) {
        ls = new ArrayList<String>();
    } else {
        ls = new ArrayList<String>(values.size());
        for (int i = 0; i < values.size(); i++) {
            Object value = values.get(i);
            String unwrappedValue = "";
            if (value instanceof org.jdom.Attribute) {
                unwrappedValue = ((org.jdom.Attribute) value).getValue();
            } else if (value instanceof org.jdom.Element) {
                unwrappedValue = ((org.jdom.Element) value).getValue();
            }
            ls.add(unwrappedValue);
        }
    }

    return ls;
}

From source file:org.langera.freud.optional.css.CssJdom.java

@SuppressWarnings("unchecked")
public List<CssRule> getCssRuleList() {
    List<CssRule> cssRuleList = new ArrayList<CssRule>();
    JXPathContext context = JXPathContext.newContext(root.getRootElement());
    List<Element> cssRuleElementList = (List<Element>) context.selectNodes("/RULE");
    for (Element element : cssRuleElementList) {
        final CssRuleJdom cssRuleJdom = new CssRuleJdom(element, 0);
        cssRuleList.add(cssRuleJdom);//  ww  w  .j  a  va2  s  . c  o  m
        for (int i = 1; i < cssRuleJdom.getNumberOfCommaSeparatedSelectorLists(); i++) {
            cssRuleList.add(new CssRuleJdom(element, i));
        }
    }
    return cssRuleList;
}

From source file:org.langera.freud.optional.javasource.block.CodeBlockJdom.java

@SuppressWarnings("unchecked")
public List<MethodCall> getMethodCallListByMethodName(String methodName) {
    if (methodCallByMethodNameMap == null) {
        methodCallByMethodNameMap = new HashMap<String, List<MethodCall>>();
        JXPathContext context = JXPathContext.newContext(codeBlockElement);
        List<Element> methodCallElementList = context
                .selectNodes("//" + JavaSourceTokenType.METHOD_CALL.getName());
        for (Element methodCallElement : methodCallElementList) {
            final MethodCall methodCall = new MethodCallJdom(methodCallElement);
            List<MethodCall> methodCallList = methodCallByMethodNameMap.get(methodCall.getMethodName());
            if (methodCallList == null) {
                methodCallList = new LinkedList<MethodCall>();
                methodCallByMethodNameMap.put(methodCall.getMethodName(), methodCallList);
            }// w w w.ja  va2  s.  c  om
            methodCallList.add(methodCall);
        }
    }
    return methodCallByMethodNameMap.get(methodName);
}

From source file:org.langera.freud.optional.javasource.classdecl.ClassDeclarationJdom.java

@SuppressWarnings("unchecked")
public Map<String, ClassDeclaration> getInnerClassDeclarationByNameMap() {
    if (innerClassDeclarationByNameMap == null) {
        JXPathContext context = JXPathContext.newContext(classDeclElement);
        innerClassDeclarationByNameMap = new HashMap<String, ClassDeclaration>();
        for (JavaSourceTokenType tokenType : POSSIBLE_CLASS_DECLARATION_TYPES) {
            final String tokenName = tokenType.getName();
            List<Element> innerClassElementList = context
                    .selectNodes("/" + CLASS_TOP_LEVEL_SCOPE.getName() + "/" + tokenName);
            for (Element innerClassElement : innerClassElementList) {
                ClassDeclaration innerClass = new ClassDeclarationJdom(innerClassElement,
                        DeclarationType.valueOf(tokenName), this);
                innerClassDeclarationByNameMap.put(innerClass.getName(), innerClass);
            }/* ww  w.  j  av a2  s .c  om*/
        }
    }
    return innerClassDeclarationByNameMap;
}

From source file:org.langera.freud.optional.javasource.classdecl.ClassDeclarationJdom.java

private void getMethodDeclarationListByNameMap(final JXPathContext context,
        final JavaSourceTokenType methodElementName) {
    List<Element> methodDeclElementList = context.selectNodes("//" + methodElementName.getName());
    for (Element methodElement : methodDeclElementList) {
        MethodDeclaration methodDeclaration = new MethodDeclarationJdom(methodElement, this);
        final String name = methodDeclaration.getName();
        List<MethodDeclaration> methodDeclarationList = methodDeclarationListByNameMap.get(name);
        if (methodDeclarationList == null) {
            methodDeclarationList = new LinkedList<MethodDeclaration>();
            methodDeclarationListByNameMap.put(name, methodDeclarationList);
        }//from w  w w  .ja  v a2 s  .co m
        methodDeclarationList.add(methodDeclaration);

    }
}

From source file:org.langera.freud.optional.javasource.JavaSourceJdom.java

private ImportDeclaration[] parseImportDeclaration() {
    try {/*  w ww.java2  s. c o  m*/
        final JXPathContext context = JXPathContext.newContext(root);
        final List importNodes = context
                .selectNodes("/" + JAVA_SOURCE_ROOT_ELEMENT_NAME + "/" + JavaSourceTokenType.IMPORT.name());
        importDeclarations = new ImportDeclaration[importNodes.size()];
        int i = 0;
        for (Object importNode : importNodes) {
            importDeclarations[i++] = new ImportDeclarationJdom((Element) importNode);
        }
    } catch (JXPathException e) {
        importDeclarations = new ImportDeclaration[0];
    }
    return importDeclarations;
}

From source file:org.langera.freud.optional.javasource.JavaSourceJdom.java

public static Annotation[] parseAnnotations(final Element element) {
    final Annotation[] annotations;
    JXPathContext context = JXPathContext.newContext(element);
    List annotationList = context.selectNodes(
            "/" + JavaSourceTokenType.MODIFIER_LIST.getName() + "/" + JavaSourceTokenType.AT.getName());
    annotations = new Annotation[annotationList.size()];
    int i = 0;/*  w  w w .ja va2  s. c  o m*/
    for (Object annotationElement : annotationList) {
        annotations[i++] = new AnnotationJdom((Element) annotationElement);
    }
    return annotations;
}

From source file:pt.webdetails.cdf.dd.util.XPathUtils.java

public static String getArrayValue(JXPathContext node, String xPath) {

    String value = "";
    try {/* w  w  w  .  j  a v a 2 s .c om*/
        JSONArray values = new JSONArray(node.selectNodes(xPath));
        value = values.toString();
    } catch (JXPathException e) {
        logger.error(e.getMessage());
    }

    return value;
}

From source file:terrastore.store.conditions.JXPathCondition.java

@Override
public boolean isSatisfied(String key, Map<String, Object> value, String expression) {
    JXPathContext context = JXPathContext.newContext(value);
    context.setLenient(true);/*from   w w  w  .j  a va 2 s. co m*/
    List selection = context.selectNodes(expression);
    return selection != null && selection.size() > 0;
}