Example usage for org.apache.commons.jxpath Pointer getNode

List of usage examples for org.apache.commons.jxpath Pointer getNode

Introduction

In this page you can find the example usage for org.apache.commons.jxpath Pointer getNode.

Prototype

Object getNode();

Source Link

Document

Returns the raw value of the object, property or collection element this pointer represents.

Usage

From source file:org.firesoa.common.jxpath.XMLModelTestCase.java

public void testNodes() {
    Pointer pointer = context.getPointer("/vendor[1]/contact[1]");
    assertFalse(pointer.getNode().equals(pointer.getValue()));
}

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.  jav a  2  s .  c  o  m*/
    assertEquals(2, things.size());
}

From source file:org.lockss.daemon.TitleSetXpath.java

private List selectNodes(CompiledExpression expr, JXPathContext context) {
    ArrayList list = new ArrayList();
    for (Iterator iter = expr.iteratePointers(context); iter.hasNext();) {
        Pointer pointer = (Pointer) iter.next();
        list.add(pointer.getNode());
    }/* w ww  .  j a  v  a 2 s .  c  o  m*/
    return list;
}

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

@Test
public void testToCsv() {
    final SimplePatientCsvFormat fmt = new SimplePatientCsvFormat();

    Patient patient = newPatient();/* www  . j  av a  2s. c o  m*/

    JXPathContext patientCtx = JXPathContext.newContext(patient);
    Pointer ptr = patientCtx.getPointer("identifier[system='SSN']");
    assertNotNull(ptr);
    Identifier id = (Identifier) ptr.getValue();
    if (id != null) {
        assertNotNull("ssn", id.getValue());
    }
    Object obj1 = ptr.getNode();

    ptr = patientCtx.getPointer("name[use='official']");
    assertNotNull(ptr);
    Object obj2 = ptr.getValue();
    ptr = patientCtx.getPointer("name[not(use)]");
    assertNotNull(ptr);
    obj2 = ptr.getValue();

    String str = fmt.toCsv(patient);
    LOG.info("CSV: {}", str);
    assertTrue(str.length() > 0);
    // Ensure the literal, null doesn't appear
    assertTrue(!str.contains("null"));
    // Ensure there is no sign of array delimiters
    assertTrue(!str.contains("["));
    // Ensure line doesn't end with a comma
    assertTrue(!str.endsWith(","));
    assertTrue(str.contains("Smith"));

    List<ContactPoint> matches = ContactPointUtil.find(patient.getTelecom(), ContactPointSystem.PHONE,
            ContactPointUse.WORK);
    assertNotNull(matches);
    assertNotNull(matches.get(0));
}

From source file:org.nasdanika.cdo.xpath.EMFFunctions.java

/**
 * Expression context method.//from   w  w w .jav a2s  . c  o m
 * @param expressionContext
 * @return
 */
public static String eClassName(ExpressionContext expressionContext) {
    Pointer cnp = expressionContext.getContextNodePointer();
    if (cnp != null) {
        Object node = cnp.getNode();
        if (node instanceof EObject) {
            return ((EObject) node).eClass().getName();
        }
    }

    return null;
}

From source file:org.nasdanika.cdo.xpath.EMFFunctions.java

/**
 * Expression context method.//from   ww w.j a v  a  2  s .c o m
 * @param expressionContext
 * @return
 */
public static String ePackageNsURI(ExpressionContext expressionContext) {
    Pointer cnp = expressionContext.getContextNodePointer();
    if (cnp != null) {
        Object node = cnp.getNode();
        if (node instanceof EObject) {
            return ((EObject) node).eClass().getEPackage().getNsURI();
        }
    }

    return null;
}

From source file:org.onecmdb.core.utils.xpath.generator.DebugContentGenerator.java

public void transfer(OutputStream out) {
    log.debug("Debug Query path <" + cmd.getPath() + ">");
    PrintWriter text = new PrintWriter(new OutputStreamWriter(out), true);

    Double count = (Double) cmd.getXPathContext().getValue("count(" + cmd.getPath() + ")");
    Iterator<Pointer> iter = cmd.getPathPointers();
    out(text, cmd.getPath() + "[" + count + "]");
    boolean first = true;
    while (iter.hasNext()) {
        Pointer p = (Pointer) iter.next();

        log.debug(p.asPath());/*w  w  w .  j a  v a  2 s .  c  o  m*/

        out(text, "\t" + p.asPath() + "=" + p.getNode());
        String[] outputAttributes = cmd.getOutputAttributeAsArray();
        for (String outputAttribute : outputAttributes) {
            Iterator<Pointer> outputAttrPointersIter = cmd.getRelativePointers(p, outputAttribute);
            while (outputAttrPointersIter.hasNext()) {
                Pointer outputPointer = outputAttrPointersIter.next();
                // Try to create some path.
                out(text, "\t\t" + outputPointer.asPath() + "=" + outputPointer.getNode());
            }
        }
    }
}

From source file:org.xchain.framework.jxpath.ScopedJXPathContextImpl.java

public JXPathContext getRelativeContext(Pointer pointer) {
    Object contextBean = pointer.getNode();
    if (contextBean == null) {
        throw new JXPathException("Cannot create a relative context for a non-existent node: " + pointer);
    }/*w  w w .j ava2s .co m*/
    return new ScopedJXPathContextImpl(this, contextBean, pointer, scope);
}

From source file:pt.webdetails.cdf.dd.model.meta.reader.datasources.DataSourcesModelReader.java

/**
 * @param model      where component type builders are placed
 * @param pointer/*  w  ww  .  j  a v  a2  s. com*/
 * @param sourcePath sourcePath for all components
 */
private void readDataSourceComponent(MetaModel.Builder model, Pointer pointer, String sourcePath) {
    // TODO: What a generality...

    DataSourceComponentType.Builder builder = new DataSourceComponentType.Builder();

    Map<String, Object> def = (Map<String, Object>) pointer.getNode();
    JXPathContext jctx = JXPathContext.newContext(def);

    String label = (String) jctx.getValue("metadata/name");

    String dataSourceType = (String) jctx.getValue("metadata/datype");

    boolean isCPK = dataSourceType.equalsIgnoreCase("cpk");
    boolean isCDA = !isCPK;

    //TODO: oh so wrong
    PathOrigin origin = new OtherPluginStaticSystemOrigin(isCPK ? "cpk" : "cda", "");
    builder.setOrigin(origin);

    // This specific Data Source has special treatment below
    boolean isKettleOverX = isCDA && "kettle over kettleTransFromFile".equalsIgnoreCase(label);

    logger.debug(String.format("\t%s", label));

    String connType = (String) jctx.getValue("metadata/conntype");
    connType = connType != null ? connType : "";

    builder.setName(pointer.asPath().replaceAll(".*name='(.*?)'.*", "$1")).setLabel(label).setTooltip(label)
            .setCategory((String) jctx.getValue("metadata/group"))
            .setCategoryLabel((String) jctx.getValue("metadata/groupdesc")).setSourcePath(sourcePath)
            .addAttribute("", isCPK ? "CPK" : "CDA"); // meta: "CDA"

    if (isCDA) {
        builder.addAttribute("conntype", connType).addAttribute("datype", dataSourceType);
    } else if (isCPK) {
        builder.useProperty(null, "stepName").useProperty(null, "kettleOutput")
                .addAttribute("pluginId", (String) jctx.getValue("metadata/pluginId"))
                .addAttribute("endpoint", (String) jctx.getValue("metadata/endpoint"));
    }

    if (isCDA) {
        for (String cdaPropName : this.getCDAPropertyNames(def)) {
            if (cdaPropName.equals("id") || cdaPropName.equals("connection")) {
                continue;
            } else if (cdaPropName.equals("columns")) {
                builder.useProperty(null, "cdacolumns");
                builder.useProperty(null, "cdacalculatedcolumns");
            } else if (cdaPropName.equals("output")) {
                builder.useProperty(null, "output");
                builder.useProperty(null, "outputMode");
            } else if (cdaPropName.equals("left")) {
                builder.useProperty(null, "left");
                builder.useProperty(null, "leftkeys");
            } else if (cdaPropName.equals("right")) {
                builder.useProperty(null, "right");
                builder.useProperty(null, "rightkeys");
            } else if (isKettleOverX && cdaPropName.equalsIgnoreCase("query")) {
                builder.useProperty(cdaPropName, "kettleQuery");
            } else {
                builder.useProperty(null, cdaPropName);
            }
        }
    }
    model.addComponent(builder);
}

From source file:pt.webdetails.cdf.dd.render.CdaRenderer.java

public String render() throws Exception {
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document cdaFile = builder.newDocument();
    Element root = cdaFile.createElement("CDADescriptor");
    cdaFile.appendChild(root);/*w  w w .  j  av  a  2  s . c o m*/
    Element connections = cdaFile.createElement("DataSources");
    root.appendChild(connections);
    Iterator<Pointer> pointers = doc.iteratePointers(CDA_ELEMENTS_JXPATH);
    while (pointers.hasNext()) {
        Pointer pointer = pointers.next();
        JXPathContext context = JXPathContext.newContext(pointer.getNode());
        String connectionId = (String) context.getValue("properties/.[name='name']/value", String.class);
        Element conn;
        try {
            conn = exportConnection(cdaFile, context, connectionId);
            connections.appendChild(conn);
        } catch (Exception e) {
            // things without connections end up here. All is fine,
            // we just need to make sure exportDataAccess doesn't try to generate a connection link
            connectionId = null;
        }

        Element dataAccess = exportDataAccess(cdaFile, context, connectionId);
        if (dataAccess != null) {
            root.appendChild(dataAccess);
        }
    }

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();

    DOMSource source = new DOMSource(cdaFile);
    StreamResult res = new StreamResult(new OutputStreamWriter(result, CharsetHelper.getEncoding()));
    transformer.setOutputProperty(OutputKeys.ENCODING, CharsetHelper.getEncoding());
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "Query");
    transformer.transform(source, res);
    return result.toString();
}