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.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);
            }//from www.  j av  a  2s . co m
        }
    }
    return innerClassDeclarationByNameMap;
}

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

public Map<String, List<MethodDeclaration>> getMethodDeclarationListByNameMap() {
    if (methodDeclarationListByNameMap == null) {
        JXPathContext context = JXPathContext.newContext(classDeclElement);
        methodDeclarationListByNameMap = new HashMap<String, List<MethodDeclaration>>();
        getMethodDeclarationListByNameMap(context, FUNCTION_METHOD_DECL);
        getMethodDeclarationListByNameMap(context, VOID_METHOD_DECL);
    }/*from  ww  w .  j  av  a  2 s  .c  o  m*/
    return methodDeclarationListByNameMap;
}

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

private ImportDeclaration[] parseImportDeclaration() {
    try {/*w  w w. ja v  a  2s .  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;/*from ww w .j  a  v a 2 s .  c  o m*/
    for (Object annotationElement : annotationList) {
        annotations[i++] = new AnnotationJdom((Element) annotationElement);
    }
    return annotations;
}

From source file:org.lilyproject.runtime.conf.ConfImpl.java

public String evalInheritanceConstraint(Conf conf, String inheritConstraint) {
    try {/* w  w  w  .  j  a v a2  s  .co  m*/
        JXPathContext jxpc = JXPathContext.newContext(conf);
        return (String) jxpc.getValue(inheritConstraint);
    } catch (Exception e) {
        throw new ConfException(
                "Error evaluating configuration inheritance uniqueness constraint expression: \""
                        + inheritConstraint + "\" on conf defined at " + conf.getLocation(),
                e);
    }
}

From source file:org.lilyproject.runtime.conf.test.InheritanceTest.java

public void testInheritance1() throws Exception {
    ConfImpl parent = loadConf("inherit1_parent.xml");

    // Child 1//ww  w  .ja v a 2s  . c o  m
    {
        ConfImpl child = loadConf("inherit1_child1.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(3, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value1", context.getValue("properties/property[@key='key1']"));
        assertEquals("value2 - altered", context.getValue("properties/property[@key='key2']"));
        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 2
    {
        ConfImpl child = loadConf("inherit1_child2.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(4, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value1", context.getValue("properties/property[@key='key1']"));
        assertEquals("value2 - altered", context.getValue("properties/property[@key='key2'][1]"));
        assertEquals("value2", context.getValue("properties/property[@key='key2'][2]"));
        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 3
    {
        ConfImpl child = loadConf("inherit1_child3.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(1, context.getValue("count(properties)", Integer.class));
        assertEquals(1, context.getValue("count(properties/property)", Integer.class));

        assertEquals("value3", context.getValue("properties/property[@key='key3']"));
    }

    // Child 4
    {
        ConfImpl child = loadConf("inherit1_child4.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(0, context.getValue("count(*)", Integer.class));
    }
}

From source file:org.lilyproject.runtime.conf.test.InheritanceTest.java

public void testInheritance2() throws Exception {
    ConfImpl parent = loadConf("inherit2_parent.xml");

    // Child 1//from   w  w  w . j a va 2  s  . c om
    {
        ConfImpl child = loadConf("inherit2_child1.xml");
        child.inherit(parent);

        JXPathContext context = JXPathContext.newContext(child);
        assertEquals(3, context.getValue("count(*)", Integer.class));

        assertEquals("val1", context.getValue("@attr1"));
        assertEquals("val2 - altered", context.getValue("@attr2"));

        assertEquals("ee", context.getValue("c/e"));

        context.setLenient(true);
        assertEquals(null, context.getValue("c/d"));
    }
}

From source file:org.lilyproject.runtime.conf.test.JXPathTest.java

public void testIt() throws Exception {
    String path = "jxpathconf.xml";
    Conf conf = XmlConfBuilder.build(getClass().getResourceAsStream(path), path);

    JXPathContext context = JXPathContext.newContext(conf);

    assertEquals("Venus", context.getValue("planet"));
    assertEquals("Mars", context.getValue("@planet"));

    assertEquals("5", context.getValue("/things/thing[@name='Book']/@quantity"));
    assertEquals("50", context.getValue("/things/thing[@name='Bicycle']/@quantity"));

    assertEquals("Book", context.getValue("/things/thing[1]/@name"));

    assertEquals("Bicycle", context.getValue("/things/thing[2]/@name"));
    assertEquals("Bicycle", context.getValue("/things/thing[last()]/@name"));

    List<Conf> things = new ArrayList<Conf>();
    Iterator thingsIt = context.iteratePointers("things/thing[position() < 3]");
    while (thingsIt.hasNext()) {
        Pointer pointer = (Pointer) thingsIt.next();
        assertTrue(pointer.getNode() instanceof Conf);
        things.add((Conf) pointer.getNode());
    }/*from   w w w.j a  v a  2 s.  com*/
    assertEquals(2, things.size());
}

From source file:org.lilyproject.runtime.rapi.ConfPlaceholderConfigurer.java

@Override
protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
    int colonPos = placeholder.indexOf(':');
    if (colonPos == -1) {
        return null;
    }/*from www .  j  av  a2  s .  c o  m*/

    try {
        String confPath = placeholder.substring(0, colonPos);
        String confExpr = placeholder.substring(colonPos + 1);

        Conf conf = confRegistry.getConfiguration(confPath);
        JXPathContext context = JXPathContext.newContext(conf);
        Object value = context.getValue(confExpr);

        return value == null ? "" : value.toString();
    } catch (Exception e) {
        throw new RuntimeException(
                "Error fetching configuration value for placeholder \"" + placeholder + "\".", e);
    }
}

From source file:org.mitre.ptmatchadapter.format.SimplePatientCsvFormat.java

/**
 * Returns supported Patient properties as a string of comma-separated values.
 * Values of String fields are enclosed by double-quotes.
 * // www  . ja v a 2s .c o  m
 * @param patient
 *          the patient resource to serialize to a CSV string
 * @return
 *          comma-delimited values of fields associated with the Patient
 */
public String toCsv(Patient patient) {
    final StringBuilder sb = new StringBuilder(INITIAL_ROW_LENGTH);
    JXPathContext patientCtx = JXPathContext.newContext(patient);

    try {
        // resource id (logical id only)
        sb.append(patient.getIdElement().getIdPart());
    } catch (NullPointerException e) {
        // check for null by exception since it is unexpected. 
        // Privacy concern keeps me from logging anything about this patient.
        LOG.error("Patient has null identifier element. This is unexpected!");
    }
    sb.append(COMMA);

    // identifiers of interest
    for (String sysName : identifierSystems) {
        Pointer ptr = patientCtx.getPointer("identifier[system='" + sysName + "']");
        Identifier id = (Identifier) ptr.getValue();
        if (id != null) {
            sb.append(id.getValue());
        }
        sb.append(COMMA);
    }

    // Extract Name Parts of interest
    for (String use : nameUses) {
        Pointer ptr;
        if (use.isEmpty()) {
            ptr = patientCtx.getPointer("name[not(use)]");
        } else {
            ptr = patientCtx.getPointer("name[use='" + use + "']");
        }
        HumanName name = (HumanName) ptr.getValue();
        if (name != null) {
            JXPathContext nameCtx = JXPathContext.newContext(ptr.getValue());
            for (String part : nameParts) {
                sb.append(DOUBLE_QUOTE);
                if (TEXT_NAME_PART.equals(part)) {
                    Object val = nameCtx.getValue(part);
                    if (val != null) {
                        sb.append(val.toString());
                    }
                } else {
                    // other supported parts return lists of string types
                    Object namePart = nameCtx.getValue(part);
                    if (namePart instanceof List<?>) {
                        List<StringType> partList = (List<StringType>) namePart;
                        if (partList.size() > 0) {
                            sb.append(partList.get(0).getValue());
                        }
                    }
                }
                sb.append(DOUBLE_QUOTE);
                sb.append(COMMA);
            }
        } else {
            // add blank sections for the name parts
            for (int i = 0; i < nameParts.length; i++) {
                sb.append(COMMA);
            }
        }
    }

    // Gender
    sb.append(patient.getGender().toString());
    sb.append(COMMA);

    // Date of Birth
    Date dob = patient.getBirthDate();
    if (dob != null) {
        sb.append(dateFormat.format(dob));
    }

    sb.append(COMMA);
    sb.append(buidContactInfo(patient));

    return sb.toString();
}