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

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

Introduction

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

Prototype

public static JXPathContext newContext(Object contextBean) 

Source Link

Document

Creates a new JXPathContext with the specified object as the root node.

Usage

From source file:org.freud.analysed.javasource.jdom.JavaSourceJdom.java

private ClassDeclaration parseClassDeclaration() {

    JXPathContext context = JXPathContext.newContext(root);
    for (JavaSourceTokenType tokenType : POSSIBLE_CLASS_DECLARATION_TYPES) {
        try {/*from  w w  w. j ava 2  s  .co m*/
            final String tokenName = tokenType.name();
            final Element element = (Element) context
                    .selectSingleNode("/" + JAVA_SOURCE_ROOT_ELEMENT_NAME + "/" + tokenName);
            if (element != null) {
                classDeclaration = new ClassDeclarationJdom(element, getDeclarationType(tokenType), null);
            }
        } catch (JXPathException e) {
            // ignore and try another path
        }
    }
    if (classDeclaration == null) {
        throw new IllegalStateException("Internal: could not find class declaration in: " + this);
    }

    return classDeclaration;
}

From source file:org.freud.analysed.javasource.jdom.JavaSourceJdom.java

private PackageDeclaration parsePackageDeclaration() {
    try {//from   ww  w  .java  2  s  .com
        JXPathContext context = JXPathContext.newContext(root);
        packageDeclaration = new PackageDeclarationJdom((Element) context.selectSingleNode(
                "/" + JAVA_SOURCE_ROOT_ELEMENT_NAME + "/" + JavaSourceTokenType.PACKAGE.name()));
    } catch (JXPathException e) {
        packageDeclaration = new PackageDeclarationJdom();
    }

    return packageDeclaration;
}

From source file:org.freud.analysed.javasource.jdom.JavaSourceJdom.java

private List<ImportDeclaration> parseImportDeclaration() {
    try {/* ww w.  j  a  v a2 s  .  com*/
        final JXPathContext context = JXPathContext.newContext(root);
        final List importNodes = context
                .selectNodes("/" + JAVA_SOURCE_ROOT_ELEMENT_NAME + "/" + JavaSourceTokenType.IMPORT.name());
        importDeclarations = new ArrayList<ImportDeclaration>(importNodes.size());
        for (Object importNode : importNodes) {
            importDeclarations.add(new ImportDeclarationJdom((Element) importNode));
        }
    } catch (JXPathException e) {
        importDeclarations = emptyList();
    }
    return importDeclarations;
}

From source file:org.freud.analysed.javasource.jdom.JavaSourceJdom.java

public static List<Annotation> parseAnnotations(final Element element) {
    final List<Annotation> annotations;
    JXPathContext context = JXPathContext.newContext(element);
    List annotationList = context.selectNodes(
            "/" + JavaSourceTokenType.MODIFIER_LIST.getName() + "/" + JavaSourceTokenType.AT.getName());
    annotations = new ArrayList<Annotation>(annotationList.size());
    for (Object annotationElement : annotationList) {
        annotations.add(new AnnotationJdom((Element) annotationElement));
    }/*from  w ww .j av a  2  s.  c  o m*/
    return annotations;
}

From source file:org.freud.analysed.javasource.jdom.MethodCallJdom.java

@SuppressWarnings("unchecked")
public String getMethodName() {
    if (methodName == null) {
        final Attribute idAttr = methodCallElement.getAttribute(JdomTreeAdaptor.ID_ATTR);
        if (idAttr != null) {
            methodName = idAttr.getValue();
            instanceReferences = EMPTY_ARRAY;
        } else {/*from w ww . ja va 2s.c  om*/
            JXPathContext context = JXPathContext.newContext(methodCallElement);
            final List<Element> identElementList = context
                    .selectNodes("//" + JavaSourceTokenType.IDENT.getName());

            Collections.sort(identElementList, JdomTreePositionComparator.getInstance());

            final int methodNameIndex = identElementList.size() - 1;
            methodName = identElementList.get(methodNameIndex).getText();
            instanceReferences = new String[methodNameIndex];
            for (int i = 0, size = instanceReferences.length; i < size; i++) {
                instanceReferences[i] = identElementList.get(i).getText();
            }
        }
    }
    return methodName;
}

From source file:org.freud.analysed.javasource.jdom.MethodDeclarationJdom.java

public CodeBlock getImplementation() {
    if (methodCodeBlock == null) {
        try {/*from  w  w  w.  j av a 2  s  .  c  om*/
            JXPathContext context = JXPathContext.newContext(methodDeclElement);
            Element codeBlockElement = (Element) context
                    .selectSingleNode("/" + JavaSourceTokenType.BLOCK_SCOPE.getName());
            methodCodeBlock = CodeBlockJdom.createMethodImplementation(codeBlockElement, this,
                    classDeclaration);
        } catch (JXPathException e) {
            return null;
        }
    }
    return methodCodeBlock;
}

From source file:org.geoserver.sfs.CapabilitiesTest.java

@Test
public void testBasicContents() throws Exception {
    MockHttpServletResponse response = getAsServletResponse(root() + "capabilities");
    assertEquals(200, response.getErrorCode());

    JSONArray json = (JSONArray) json(response);
    // print(json);

    // check we have the right number of layers
    assertEquals(getCatalog().getFeatureTypes().size(), json.size());

    // extract and check one
    JXPathContext context = JXPathContext.newContext(json);
    JSONObject primitive = (JSONObject) context.getValue(".[name = 'sf:PrimitiveGeoFeature']");
    assertNotNull(primitive);/*from  w w  w.  j  a  v a 2 s  .  c o m*/
    assertEquals("urn:ogc:def:crs:EPSG:4326", primitive.get("crs"));
    assertEquals("xy", primitive.get("axisorder"));
    JSONArray bbox = (JSONArray) primitive.get("bbox");
    assertEquals(-180.0, bbox.getDouble(0), 1E-7);
    assertEquals(-90.0, bbox.getDouble(1), 1E-7);
    assertEquals(180.0, bbox.getDouble(2), 1E-7);
    assertEquals(90.0, bbox.getDouble(3), 1E-7);
}

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

private static JXPathContext initialiseContext(NamespaceSupport ns, Document doc) {
    JXPathContext context = JXPathContext.newContext(doc);
    addNamespaces(ns, context);//from  ww w  . j  a  v a  2 s .c  o  m
    context.setLenient(true);
    return context;
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Object parseAndSetFieldValue(Object obj, String fieldName, String fieldValue) throws Exception {
    if (obj instanceof Map) {
        ((Map) obj).put(fieldName, parseValue(fieldValue, String.class)); //Map of Strings
    } else if (obj instanceof List) {
        int index = Integer.parseInt(fieldName);
        while (((List) obj).size() <= index)
            ((List) obj).add(null);
        ((List) obj).set(index, parseValue(fieldValue, String.class)); //List of Strings
    } else if (fieldName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        JXPathContext ctx = JXPathContext.newContext(obj);
        ctx.setValue(fieldName.replace('.', '/'), parseValue(fieldValue, String.class));
    } else {//w w w  .  ja v a 2 s. c o  m
        // Find a Field
        Field field = getField(obj, fieldName);
        if (field != null) {
            Class fieldClass = field.getType();
            Object value = parseValue(fieldValue, fieldClass);
            field.set(obj, value);
            return obj;
        }
        // Find a getter and setter
        Method getter = getGetter(obj, fieldName);
        Method setter = getSetter(obj, getter, fieldName);
        if (setter != null && getter != null) {
            Object value = parseValue(fieldValue, getter.getReturnType());
            setter.invoke(obj, value);
        }
    }
    return obj;
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Object setFieldValue(Object obj, String fieldName, Object fieldValue) throws Exception {
    if (obj instanceof Map) {
        ((Map) obj).put(fieldName, fieldValue); //Map of Strings
    } else if (obj instanceof List) {
        int index = Integer.parseInt(fieldName);
        while (((List) obj).size() <= index)
            ((List) obj).add(null);
        ((List) obj).set(index, fieldValue); //List of Strings
    } else if (fieldName.indexOf('.') != -1) { //Set it by JXPath, valid only for strings
        JXPathContext ctx = JXPathContext.newContext(obj);
        ctx.setValue(fieldName.replace('.', '/'), fieldValue);
    } else {/*from w w w .j  ava  2 s  . c om*/
        // Find a Field
        Field field = getField(obj, fieldName);
        if (field != null) {
            field.set(obj, fieldValue);
            return obj;
        }
        // Find a getter and setter.
        Method getter = getGetter(obj, fieldName);
        Method setter = getSetter(obj, getter, fieldName);
        if (setter != null && getter != null) {
            setter.invoke(obj, fieldValue);
        }
    }
    return obj;
}