Example usage for javax.xml.xpath XPathExpression evaluate

List of usage examples for javax.xml.xpath XPathExpression evaluate

Introduction

In this page you can find the example usage for javax.xml.xpath XPathExpression evaluate.

Prototype

public Object evaluate(InputSource source, QName returnType) throws XPathExpressionException;

Source Link

Document

Evaluate the compiled XPath expression in the context of the specified InputSource and return the result as the specified type.

Usage

From source file:nl.surfnet.sab.SabResponseParser.java

public SabRoleHolder parse(InputStream inputStream) throws IOException {

    String organisation = null;//w w  w.  ja va  2 s  .  c  om
    List<String> roles = new ArrayList<String>();
    XPath xpath = getXPath();
    try {
        Document document = createDocument(inputStream);
        validateStatus(document, xpath);

        // Extract organisation
        XPathExpression organisationExpr = xpath.compile(XPATH_ORGANISATION);
        NodeList nodeList = (NodeList) organisationExpr.evaluate(document, XPathConstants.NODESET);
        for (int i = 0; nodeList != null && i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node != null) {
                organisation = StringUtils.trimWhitespace(node.getTextContent());
                node.getParentNode().getTextContent();
            }
        }

        // Extract roles
        XPathExpression rolesExpr = xpath.compile(XPATH_ROLES);
        NodeList rolesNodeList = (NodeList) rolesExpr.evaluate(document, XPathConstants.NODESET);
        for (int i = 0; rolesNodeList != null && i < rolesNodeList.getLength(); i++) {
            Node node = rolesNodeList.item(i);
            if (node != null) {
                roles.add(StringUtils.trimWhitespace(node.getTextContent()));
            }
        }

    } catch (XPathExpressionException e) {
        throw new RuntimeException(e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    } catch (SAXException e) {
        throw new IOException(e);
    }
    return new SabRoleHolder(organisation, roles);
}

From source file:com.photon.phresco.framework.impl.ConfigurationWriter.java

private Node getNode(String xpath) throws XPathExpressionException {
    XPathExpression xPathExpression = getXPath().compile(xpath);
    return (Node) xPathExpression.evaluate(reader.getDocument(), XPathConstants.NODE);
}

From source file:de.slub.fedora.oai.OaiHarvester.java

private void handleXmlResult(InputStream content)
        throws ParserConfigurationException, IOException, SAXException, XPathExpressionException {

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    Document document = documentBuilderFactory.newDocumentBuilder().parse(content);

    XPath xPath = XPathFactory.newInstance().newXPath();

    XPathExpression xSelectIdentifier = xPath.compile("//header/identifier");
    NodeList nodes = (NodeList) xSelectIdentifier.evaluate(document, XPathConstants.NODESET);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node n = nodes.item(i);/*from   www  .  j a va 2  s .  co  m*/
        jobQueue.add(new ObjectIndexJob(IndexJob.Type.CREATE, getLocalIdentifier(n.getTextContent())));
    }

    XPathExpression xSelectResumptionToken = xPath.compile("//resumptionToken");
    resumptionToken = (String) xSelectResumptionToken.evaluate(document, XPathConstants.STRING);

    XPathExpression xSelectExpirationDate = xPath.compile("//resumptionToken/@expirationDate");
    String s = (String) xSelectExpirationDate.evaluate(document, XPathConstants.STRING);
    if (s == null || s.isEmpty()) {
        expirationDate = null;
    } else {
        expirationDate = DatatypeConverter.parseDateTime(s).getTime();
    }
}

From source file:org.ala.harvester.IdentifyLifeHarvester.java

/**
 * Harvest a document and update the repository.
 * //from  w  w w.  j  a  v  a  2 s  .  c o  m
 * @param infosourceId
 * @param url
 * @throws Exception
 */
private void harvestDoc(int infosourceId, String url) throws Exception {
    byte[] content = null;

    System.out.println("******** request: " + url);
    Object[] resp = restfulClient.restGet(url);
    if ((Integer) resp[0] == HttpStatus.SC_OK) {
        content = resp[1].toString().getBytes("UTF-8");
    }

    if (content != null) {
        List<String> ids = new ArrayList<String>();
        String keyUrl = connectionParams.get(KEY_END_POINT_ATTR_NAME);

        // Instantiates a DOM builder to create a DOM of the response.
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        // return a parsed Document
        Document doc = builder.parse(new ByteArrayInputStream(content));

        XPathFactory xfactory = XPathFactory.newInstance();
        XPath xpath = xfactory.newXPath();

        XPathExpression xe = xpath.compile("//keys/key[@id]");
        NodeList nodeSet = (NodeList) xe.evaluate(doc, XPathConstants.NODESET);
        for (int i = 0; i < nodeSet.getLength(); i++) {
            NamedNodeMap map = nodeSet.item(i).getAttributes();
            ids.add(map.getNamedItem("id").getNodeValue());
        }

        for (int i = 0; i < ids.size(); i++) {
            Object[] res = restfulClient.restGet(keyUrl + "/" + ids.get(i));
            if ((Integer) res[0] == HttpStatus.SC_OK) {
                //map the document 
                List<ParsedDocument> pds = documentMapper.map(keyUrl + ids.get(i),
                        res[1].toString().getBytes());

                //store the results
                for (ParsedDocument pd : pds) {
                    //debugParsedDoc(pd);
                    repository.storeDocument(infosourceId, pd);
                    logger.debug("Parent guid for stored doc: " + pd.getParentGuid());
                }
            }
        }
    } else {
        logger.warn("Unable to process url: " + url);
    }
}

From source file:com.twentyn.patentExtractor.PatentDocument.java

public static PatentDocument patentDocumentFromXMLStream(InputStream iStream)
        throws IOException, ParserConfigurationException, SAXException, TransformerConfigurationException,
        TransformerException, XPathExpressionException {

    // Create XPath objects for validating that this document is actually a patent.
    XPath xpath = Util.getXPathFactory().newXPath();
    XPathExpression versionXPath = xpath.compile(PATH_DTD_VERSION);
    XPathExpression versionXPathApp = xpath.compile(PATH_DTD_VERSION_APP);

    DocumentBuilderFactory docFactory = Util.mkDocBuilderFactory();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(iStream);

    Util.DocumentType docType = Util.identifyDocType(doc);
    if (docType != Util.DocumentType.PATENT && docType != Util.DocumentType.APPLICATION) {
        LOGGER.warn("Found unexpected document type: " + docType);
        return null;
    }/*from   ww  w. j  ava  2s.c  o  m*/

    boolean isApplication = docType == Util.DocumentType.APPLICATION;
    // Yes this is in fact the way suggested by the XPath API.
    String version;
    if (!isApplication) {
        version = (String) versionXPath.evaluate(doc, XPathConstants.STRING);
    } else {
        version = (String) versionXPathApp.evaluate(doc, XPathConstants.STRING);
    }

    if (version == null || !VERSION_MAP.containsKey(version)) {
        LOGGER.warn(String.format("Unrecognized patent DTD version: %s", version));
        return null;
    }

    HashMap<String, String> paths = VERSION_MAP.get(version);

    /* Create XPath objects for extracting the fields of interest based on the version information.
     * TODO: extract these into some sharable, thread-safe place, maybe via dependency injection.
     */
    XPathExpression idXPath = xpath.compile(paths.get(PATH_KEY_FILE_ID));
    XPathExpression dateXPath = xpath.compile(paths.get(PATH_KEY_DATE));
    XPathExpression titleXPath = xpath.compile(paths.get(PATH_KEY_TITLE));
    XPathExpression classificationXPath = xpath.compile(paths.get(PATH_KEY_MAIN_CLASSIFICATION));
    XPathExpression furtherClassificationsXPath = xpath.compile(paths.get(PATH_KEY_FURTHER_CLASSIFICATIONS));
    XPathExpression searchedClassificationsXPath = xpath.compile(paths.get(PATH_KEY_SEARCHED_CLASSIFICATIONS));

    String fileId = (String) idXPath.evaluate(doc, XPathConstants.STRING);
    String date = (String) dateXPath.evaluate(doc, XPathConstants.STRING);
    NodeList titleNodes = (NodeList) titleXPath.evaluate(doc, XPathConstants.NODESET);
    String title = StringUtils.join(" ", extractTextFromHTML(docBuilder, titleNodes));
    String classification = (String) classificationXPath.evaluate(doc, XPathConstants.STRING);
    NodeList furtherClassificationNodes = (NodeList) furtherClassificationsXPath.evaluate(doc,
            XPathConstants.NODESET);
    ArrayList<String> furtherClassifications = null;
    if (furtherClassificationNodes != null) {
        furtherClassifications = new ArrayList<>(furtherClassificationNodes.getLength());
        for (int i = 0; i < furtherClassificationNodes.getLength(); i++) {
            Node n = furtherClassificationNodes.item(i);
            String txt = n.getTextContent();
            if (txt != null) {
                furtherClassifications.add(i, txt);
            }
        }
    } else {
        furtherClassifications = new ArrayList<>(0);
    }

    NodeList otherClassificationNodes = (NodeList) searchedClassificationsXPath.evaluate(doc,
            XPathConstants.NODESET);
    ArrayList<String> otherClassifications = null;
    if (otherClassificationNodes != null) {
        otherClassifications = new ArrayList<>(otherClassificationNodes.getLength());
        for (int i = 0; i < otherClassificationNodes.getLength(); i++) {
            Node n = otherClassificationNodes.item(i);
            String txt = n.getTextContent();
            if (txt != null) {
                otherClassifications.add(i, txt);
            }
        }
    } else {
        otherClassifications = new ArrayList<>(0);
    }

    // Extract text content for salient document paths.
    List<String> allTextList = getRelevantDocumentText(docBuilder, PATHS_TEXT, xpath, doc);
    List<String> claimsTextList = getRelevantDocumentText(docBuilder, new String[] { PATH_CLAIMS }, xpath, doc);

    return new PatentDocument(fileId, date, title, classification, furtherClassifications, otherClassifications,
            allTextList, claimsTextList, isApplication);
}

From source file:eu.impact_project.wsclient.HtmlServiceProvider.java

private NodeList applyXPath(Document doc, String xpathExpression, String namespace) {

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    if (!namespace.equals(NO_NAMESPACE))
        xpath.setNamespaceContext(new WSDLNamespaceContext());

    XPathExpression expr;

    Object result = null;/* w w w. java  2s .  c om*/
    try {
        expr = xpath.compile(xpathExpression);
        result = expr.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }

    return (NodeList) result;

}

From source file:it.tidalwave.northernwind.frontend.ui.component.calendar.DefaultCalendarViewController.java

/*******************************************************************************************************************
 *
 *
 *
 ******************************************************************************************************************/
@PostConstruct/*from  www .ja v  a 2 s  .  c o  m*/
/* package */ void initialize() throws NotFoundException, IOException, ParserConfigurationException,
        SAXException, XPathExpressionException, HttpStatusException {
    final String pathParams = requestHolder.get().getPathParams(siteNode);
    final int currentYear = getCurrentYear(pathParams);

    final ResourceProperties siteNodeProperties = siteNode.getProperties();
    final ResourceProperties viewProperties = siteNode.getPropertyGroup(view.getId());

    //        try
    //          {
    //            siteNodeProperties.getProperty(PROPERTY_ENTRIES);
    //          }
    //        catch (NotFoundException e)
    //          {
    //            throw new HttpStatusException(404);
    //          }

    final String entries = siteNodeProperties.getProperty(PROPERTY_ENTRIES);
    final StringBuilder builder = new StringBuilder();
    final int selectedYear = viewProperties.getIntProperty(PROPERTY_SELECTED_YEAR, currentYear);
    final int firstYear = viewProperties.getIntProperty(PROPERTY_FIRST_YEAR,
            Math.min(selectedYear, currentYear));
    final int lastYear = viewProperties.getIntProperty(PROPERTY_LAST_YEAR, Math.max(selectedYear, currentYear));
    final int columns = 4;

    builder.append("<div class='nw-calendar'>\n");
    appendTitle(builder, siteNodeProperties);

    builder.append("<table class='nw-calendar-table'>\n").append("<tbody>\n");

    builder.append(String.format("<tr>%n<th colspan='%d' class='nw-calendar-title'>%d</th>%n</tr>%n", columns,
            selectedYear));

    final String[] monthNames = DateFormatSymbols.getInstance(requestLocaleManager.getLocales().get(0))
            .getMonths();
    final String[] shortMonthNames = DateFormatSymbols.getInstance(Locale.ENGLISH).getShortMonths();

    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    final DocumentBuilder db = dbf.newDocumentBuilder();
    final Document document = db.parse(new InputSource(new StringReader(entries)));
    final XPathFactory xPathFactory = XPathFactory.newInstance();
    final XPath xPath = xPathFactory.newXPath();

    for (int month = 1; month <= 12; month++) {
        if ((month - 1) % columns == 0) {
            builder.append("<tr>\n");

            for (int column = 0; column < columns; column++) {
                builder.append(String.format("<th width='%d%%'>%s</th>", 100 / columns,
                        monthNames[month + column - 1]));
            }

            builder.append("</tr>\n<tr>\n");
        }

        builder.append("<td>\n<ul>\n");
        final String pathTemplate = "/calendar/year[@id='%d']/month[@id='%s']/item";
        final String jq1 = String.format(pathTemplate, selectedYear, shortMonthNames[month - 1].toLowerCase());
        final XPathExpression jx1 = xPath.compile(jq1);
        final NodeList nodes = (NodeList) jx1.evaluate(document, XPathConstants.NODESET);

        for (int i = 0; i < nodes.getLength(); i++) {
            // FIXME: verbose XML code below
            final Node node = nodes.item(i);
            final String link = site
                    .createLink(new ResourcePath(node.getAttributes().getNamedItem("link").getNodeValue()));

            String linkClass = "";
            Node typeNode = node.getAttributes().getNamedItem("type");

            if (typeNode != null) {
                linkClass = String.format(" class='nw-calendar-table-link-%s'", typeNode.getNodeValue());
            }

            final String name = node.getAttributes().getNamedItem("name").getNodeValue();
            builder.append(String.format("<li><a href='%s'%s>%s</a></li>%n", link, linkClass, name));
        }

        builder.append("</ul>\n</td>\n");

        if ((month - 1) % columns == (columns - 1)) {
            builder.append("</tr>\n");
        }
    }

    builder.append("</tbody>\n</table>\n");

    appendYearSelector(builder, firstYear, lastYear, selectedYear);
    builder.append("</div>\n");
    view.setContent(builder.toString());
}

From source file:com.amalto.core.storage.hibernate.DefaultStorageClassLoader.java

@Override
public InputStream generateEhCacheConfig() {
    try {/*from   w  ww. j ava 2  s . com*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setExpandEntityReferences(false);
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        Document document = documentBuilder.parse(this.getClass().getResourceAsStream(EHCACHE_XML_CONFIG));
        // <diskStore path="java.io.tmpdir"/>
        XPathExpression compile = pathFactory.compile("ehcache/diskStore"); //$NON-NLS-1$
        Node node = (Node) compile.evaluate(document, XPathConstants.NODE);
        node.getAttributes().getNamedItem("path") //$NON-NLS-1$
                .setNodeValue(dataSource.getCacheDirectory() + '/' + dataSource.getName());
        return toInputStream(document);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.seer.datacruncher.validation.MacroRulesValidation.java

/**
 * Apply validation macro rules.// w ww  . ja  va 2  s .c o m
 * @param datastreamDTO
 * @return ResultStepValidation
 */
protected ResultStepValidation doValidation(DatastreamDTO datastreamDTO) {
    long schemaId = datastreamDTO.getIdSchema();
    List<MacroEntity> list = new ArrayList<MacroEntity>();
    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder
                .parse(new ByteArrayInputStream(schemasXSDDao.find(schemaId).getSchemaXSD().getBytes()));
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        XPathExpression expr = xpath.compile("//annotation/appinfo/text()");
        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;
        for (int i = 0; i < nodes.getLength(); i++) {
            String val = nodes.item(i).getNodeValue();
            if (val.startsWith("@RuleCheck")) {
                String[] arr = val.split(";\n");
                for (String s : arr) {
                    list.add(macrosDao.getMacroEntityByName(s.trim().substring(11)));
                }
                break;
            }
        }
    } catch (ParserConfigurationException e) {
        log.error("ParserConfigurationException", e);
    } catch (SAXException e) {
        log.error("SAXException", e);
    } catch (IOException e) {
        log.error("IOException", e);
    } catch (XPathExpressionException e) {
        log.error("XPathExpressionException", e);
    }
    ResultStepValidation resultStepValidation = new ResultStepValidation();
    resultStepValidation.setValid(true);
    resultStepValidation.setMessageResult(I18n.getMessage("success.validationOK"));
    List<Element> xmlTextNodes = null;
    try {
        xmlTextNodes = StreamsUtils.parseStreamXml(datastreamDTO.getOutput());
    } catch (DocumentException e) {
        log.error("Stream parse exception", e);
    }
    JexlEngine jexl = JexlEngineFactory.getInstance();
    boolean isSuccess = true;
    Pattern pattern = Pattern.compile(MACRO_SQL_VALIDATOR_PATTERN, Pattern.CASE_INSENSITIVE);
    for (MacroEntity ent : list) {
        if (!isSuccess)
            continue;
        String rule = ent.getRule();
        List<Map<String, String>> varsList = parseVars(ent.getVars());
        combineVariableLists(varsList, schemaId);
        Matcher matcher = pattern.matcher(rule);
        while (matcher.find()) {
            String varName = matcher.group(4);
            String sqlRes = "false";
            for (Map<String, String> m : varsList) {
                if (m.get("uniqueName").equals(varName)) {
                    for (Element el : xmlTextNodes) {
                        int t = datastreamDTO.getIdStreamType();
                        String fieldPath = (t == StreamType.XML || t == StreamType.XMLEXI ? "" : "ROOT/")
                                + m.get("nodePath");
                        if (fieldPath.equals(StreamsUtils.formatPathForXmlNode(el.getPath()))) {
                            String prepSql = matcher.group().replaceAll(matcher.group(4),
                                    "\"" + el.getText() + "\"");
                            String signum = matcher.group(3);
                            if (signum.equals("<")) {
                                prepSql = prepSql.replaceAll(signum, ">=");
                            } else if (signum.equals(">")) {
                                prepSql = prepSql.replaceAll(signum, "<=");
                            } else if (signum.equals("!=")) {
                                prepSql = prepSql.replaceAll(signum, "=");
                            }
                            Query q = entityManager.createNativeQuery(prepSql);
                            @SuppressWarnings("rawtypes")
                            List resList = q.getResultList();
                            if ((signum.equals("=") && resList.size() > 0)
                                    || (signum.equals("!=") && resList.size() == 0)
                                    || (signum.equals(">") && resList.size() == 0)
                                    || (signum.equals("<") && resList.size() == 0)) {
                                sqlRes = "true";
                            }
                            break;
                        }
                    }
                }
            }
            rule = rule.replaceAll(matcher.group(), sqlRes);
        }
        Expression e = jexl.createExpression(rule);
        JexlContext context = new MapContext();
        for (Map<String, String> m : varsList) {
            for (Element el : xmlTextNodes) {
                int t = datastreamDTO.getIdStreamType();
                String fieldPath = (t == StreamType.XML || t == StreamType.XMLEXI ? "" : "ROOT/")
                        + m.get("nodePath");
                if (fieldPath.equals(StreamsUtils.formatPathForXmlNode(el.getPath()))) {
                    context.set(m.get("uniqueName"),
                            JEXLFieldFactory.getField(m.get("fieldType"), el.getText()).getValue());
                    break;
                }
            }
        }
        Object res = e.evaluate(context);
        if (res != null) {
            isSuccess = false;
            resultStepValidation.setValid(false);
            if (ent.getErrorType() == StreamStatus.Warning.getCode()) {
                resultStepValidation.setWarning(true);
            }
            resultStepValidation
                    .setMessageResult(I18n.getMessage("error.validationMacro") + ": " + res.toString());
        }
    }
    return resultStepValidation;
}

From source file:com.alliander.osgp.adapter.ws.endpointinterceptors.WebServiceMonitorInterceptor.java

/**
 * Search an XML element using an XPath expression.
 *
 * @param document/*from   w  ww  .j a  va  2  s  .c o  m*/
 *            The XML document.
 * @param element
 *            The name of the desired XML element.
 *
 * @return The content of the XML element, or null if the element is not
 *         found.
 *
 * @throws XPathExpressionException
 *             In case the expression fails to compile or evaluate, an
 *             exception will be thrown.
 */
private String evaluateXPathExpression(final Document document, final String element)
        throws XPathExpressionException {
    final String expression = String.format("//*[contains(local-name(), '%s')]", element);

    final XPathFactory xFactory = XPathFactory.newInstance();
    final XPath xPath = xFactory.newXPath();

    final XPathExpression xPathExpression = xPath.compile(expression);
    final NodeList nodeList = (NodeList) xPathExpression.evaluate(document, XPathConstants.NODESET);

    if (nodeList != null && nodeList.getLength() > 0) {
        return nodeList.item(0).getTextContent();
    } else {
        return null;
    }
}