Example usage for javax.xml.xpath XPathConstants NODE

List of usage examples for javax.xml.xpath XPathConstants NODE

Introduction

In this page you can find the example usage for javax.xml.xpath XPathConstants NODE.

Prototype

QName NODE

To view the source code for javax.xml.xpath XPathConstants NODE.

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Usage

From source file:org.orcid.core.cli.LoadFundRefData.java

/**
 * Get an RDF organization from the given RDF file
 * *//*from w  w w .  j  a  v a 2 s . co  m*/
private RDFOrganization getOrganization(Document xmlDocument, NamedNodeMap attrs) {
    RDFOrganization organization = new RDFOrganization();
    try {
        Node node = attrs.getNamedItem("rdf:resource");
        String itemDoi = node.getNodeValue();
        LOGGER.info("Processing item {}", itemDoi);
        // Get organization name
        String orgName = (String) xPath.compile(orgNameExpression.replace("%s", itemDoi)).evaluate(xmlDocument,
                XPathConstants.STRING);
        // Get country code
        Node countryNode = (Node) xPath.compile(orgCountryExpression.replace("%s", itemDoi))
                .evaluate(xmlDocument, XPathConstants.NODE);
        NamedNodeMap countryAttrs = countryNode.getAttributes();
        String countryGeonameUrl = countryAttrs.getNamedItem("rdf:resource").getNodeValue();
        String countryCode = fetchFromGeoNames(countryGeonameUrl, "countryCode");

        // Get state name
        Node stateNode = (Node) xPath.compile(orgStateExpression.replace("%s", itemDoi)).evaluate(xmlDocument,
                XPathConstants.NODE);
        String stateName = null;
        String stateCode = null;
        if (stateNode != null) {
            NamedNodeMap stateAttrs = stateNode.getAttributes();
            String stateGeoNameCode = stateAttrs.getNamedItem("rdf:resource").getNodeValue();
            stateName = fetchFromGeoNames(stateGeoNameCode, "name");
            stateCode = fetchFromGeoNames(stateGeoNameCode, STATE_NAME);
        }

        // Get type
        String orgType = (String) xPath.compile(orgTypeExpression.replace("%s", itemDoi)).evaluate(xmlDocument,
                XPathConstants.STRING);
        // Get subType
        String orgSubType = (String) xPath.compile(orgSubTypeExpression.replace("%s", itemDoi))
                .evaluate(xmlDocument, XPathConstants.STRING);

        // Fill the organization object
        organization.doi = itemDoi;
        organization.name = orgName;
        organization.country = countryCode;
        organization.state = stateName;
        organization.stateCode = stateCode;
        // TODO: since we don't have city, we fill this with the state, this
        // should be modified soon
        organization.city = stateCode;
        organization.type = orgType;
        organization.subtype = orgSubType;
    } catch (XPathExpressionException xpe) {
        LOGGER.error("XPathExpressionException {}", xpe.getMessage());
    }

    return organization;
}

From source file:org.osaf.cosmo.calendar.hcalendar.HCalendarParser.java

private static Node findNode(XPathExpression expr, Object context) throws ParserException {
    try {//www.  j  a  v a  2  s. com
        return (Node) expr.evaluate(context, XPathConstants.NODE);
    } catch (XPathException e) {
        throw new ParserException("Unable to find node", -1, e);
    }
}

From source file:org.pentaho.marketplace.domain.model.entities.serialization.MarketplaceXmlSerializer.java

private String getElementChildValue(Element element, String child) throws XPathExpressionException {
    Element childElement = (Element) xpath.evaluate(child, element, XPathConstants.NODE);

    if (childElement != null) {
        return childElement.getTextContent();
    } else {// w ww . jav  a 2  s. com
        return null;
    }
}

From source file:org.pentaho.marketplace.domain.model.entities.serialization.MarketplaceXmlSerializer.java

private ICategory getCategory(Element pluginElement) throws XPathExpressionException {
    final String CATEGORY_ELEMENT_NAME = "category";

    Element categoryElement = (Element) xpath.evaluate(CATEGORY_ELEMENT_NAME, pluginElement,
            XPathConstants.NODE);
    if (categoryElement == null) {
        return null;
    }//w ww . ja v a  2  s .  com

    return this.getCategoryFromCategoryElement(categoryElement);
}

From source file:org.pentaho.marketplace.domain.model.entities.serialization.MarketplaceXmlSerializer.java

private ICategory getCategoryFromCategoryElement(Element categoryElement) throws XPathExpressionException {

    final String PARENT_ELEMENT_NAME = "parent";
    final String NAME_ELEMENT_NAME = "name";

    ICategory parent = null;//from   w  w  w.  ja  v a2  s.com
    Element parentElement = (Element) xpath.evaluate(PARENT_ELEMENT_NAME, categoryElement, XPathConstants.NODE);
    if (parentElement != null) {
        parent = getCategoryFromCategoryElement(parentElement);
    }

    String name = getElementChildValue(categoryElement, NAME_ELEMENT_NAME);
    ICategory category = this.categoryFactory.create(name, parent);
    return category;
}

From source file:org.pentaho.marketplace.domain.model.entities.serialization.MarketplaceXmlSerializer.java

/**
 * Parses the version element to get the development stage
 *
 * @param versionElement where the development stage element is contained
 * @return the parsed development stage//from  w  ww  . j av  a 2 s .co m
 */
private IDevelopmentStage getDevelopmentStage(Element versionElement) throws XPathExpressionException {
    final String DEVELOPMENT_STAGE_ELEMENT_NAME = "development_stage";
    final String DEVELOPMENT_STAGE_LANE_ELEMENT_NAME = "lane";
    final String DEVELOPMENT_STAGE_PHASE_ELEMENT_NAME = "phase";

    Element devStageElement = (Element) xpath.evaluate(DEVELOPMENT_STAGE_ELEMENT_NAME, versionElement,
            XPathConstants.NODE);
    if (devStageElement == null) {
        return null;
    }

    String lane = this.getElementChildValue(devStageElement, DEVELOPMENT_STAGE_LANE_ELEMENT_NAME);
    String phase = this.getElementChildValue(devStageElement, DEVELOPMENT_STAGE_PHASE_ELEMENT_NAME);

    // TODO: switch to factory to allow DI?
    return new DevelopmentStage(lane, phase);
}

From source file:org.pentaho.marketplace.domain.services.PluginService.java

/**
 * Parses the version element to get the development stage
 * @param versionElement where the development stage element is contained
 * @return// www  . j a v  a  2 s. c o m
 */
private IDevelopmentStage getDevelopmentStage(Element versionElement) throws XPathExpressionException {
    final String DEVELOPMENT_STAGE_ELEMENT_NAME = "development_stage";
    final String DEVELOPMENT_STAGE_LANE_ELEMENT_NAME = "lane";
    final String DEVELOPMENT_STAGE_PHASE_ELEMENT_NAME = "phase";

    Element devStageElement = (Element) xpath.evaluate(DEVELOPMENT_STAGE_ELEMENT_NAME, versionElement,
            XPathConstants.NODE);
    if (devStageElement == null) {
        return null;
    }

    String lane = this.getElementChildValue(devStageElement, DEVELOPMENT_STAGE_LANE_ELEMENT_NAME);
    String phase = this.getElementChildValue(devStageElement, DEVELOPMENT_STAGE_PHASE_ELEMENT_NAME);
    ;

    // TODO: switch to factory to allow DI?
    return new DevelopmentStage(lane, phase);
}

From source file:org.pentaho.reporting.engine.classic.extensions.datasources.xpath.LegacyXPathTableModel.java

private Node evaluateNode(final XPathExpression xPathExpression, final ResourceData xmlResourceData,
        final ResourceManager resourceManager, final DocumentBuilder builder)
        throws XPathExpressionException, ResourceLoadingException, IOException, SAXException {
    final InputStream stream = xmlResourceData.getResourceAsStream(resourceManager);
    try {/* w w  w . j  a v  a 2s.co m*/
        return (Node) xPathExpression.evaluate(builder.parse(new InputSource(stream)), XPathConstants.NODE);

    } finally {
        stream.close();
    }
}

From source file:org.pentaho.reporting.engine.classic.extensions.datasources.xpath.XPathTableModel.java

private Node evaluateNode(final XPath xpath, final String xpathQuery, final ResourceData xmlResourceData,
        final ResourceManager resourceManager)
        throws XPathExpressionException, ResourceLoadingException, IOException {
    final InputStream stream = xmlResourceData.getResourceAsStream(resourceManager);
    try {/* ww  w  .j av a  2s.co m*/
        return (Node) xpath.evaluate(xpathQuery, new InputSource(stream), XPathConstants.NODE);
    } finally {
        stream.close();
    }
}

From source file:org.pentaho.reporting.platform.plugin.ParameterXmlContentHandlerTest.java

/**
 * Test that 'skip' validation messages does not mess up with errors.
 * <p>/*from  w w  w . ja v a 2 s . c om*/
 * generated xml should look like:
 * <errors>
 * <error message="" parameter="parameter3"/>
 * <error message="not good at all" parameter="parameter2"/>
 * <error message="not good" parameter="parameter1"/>
 * <global-error message="kernel panic"/>
 * </errors>
 *
 * @throws ParserConfigurationException
 */
@Test
public void createErrorElementsTest() throws ParserConfigurationException, XPathExpressionException {
    final ValidationResult vr = new ValidationResult();
    vr.addError("parameter1", new ValidationMessage("not good"));
    vr.addError("parameter2", new ValidationMessage("not good at all"));
    vr.addError(new ValidationMessage("kernel panic"));

    // save parameter name - attribute value mapping
    final Map<String, String> attrMap = new HashMap();
    attrMap.put("parameter3", "");
    attrMap.put("parameter2", "not good at all");
    attrMap.put("parameter1", "not good");

    handler.document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    final Element el = handler.createErrorElements(vr);
    handler.document.appendChild(el);

    assertNotNull(el);
    assertEquals(3, el.getChildNodes().getLength());

    // use xpath for future validation just to get rid of numerous for() loops in DOM api
    final XPath xpath = xpathFactory.newXPath();

    final NodeList found = NodeList.class
            .cast(xpath.evaluate("/errors/error", handler.document, XPathConstants.NODESET));
    assertNotNull(found);

    for (int i = 0; i < found.getLength(); i++) {
        final Node node = found.item(i);
        assertEquals("error", node.getNodeName());
        final Element oneError = (Element) node;
        final String paramName = oneError.getAttribute("parameter");
        assertTrue(attrMap.containsKey(paramName));
        assertEquals(attrMap.get(paramName), oneError.getAttribute("message"));
    }

    final Node globalError = (Node) xpath.evaluate("/errors/global-error", handler.document,
            XPathConstants.NODE);
    assertNotNull(globalError);

    assertEquals("global-error", globalError.getNodeName());

    final Element globalErrEl = Element.class.cast(globalError);

    assertEquals("kernel panic", globalErrEl.getAttribute("message"));
}