Example usage for javax.xml.xpath XPathConstants NODESET

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

Introduction

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

Prototype

QName NODESET

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

Click Source Link

Document

The XPath 1.0 NodeSet data type.

Maps to Java org.w3c.dom.NodeList .

Usage

From source file:ch.entwine.weblounge.bridge.oaipmh.harvester.OaiPmhResponse.java

public static NodeList xpathNodeList(XPath xpath, Node context, String expr) {
    try {//from ww  w.  j  ava 2  s. c o  m
        return (NodeList) xpath.evaluate(expr, context, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        throw new RuntimeException("malformed xpath expression " + expr, e);
    }
}

From source file:de.ingrid.iplug.opensearch.query.OSDescriptorBuilder.java

/**
 * Request the descriptorAddress and return an OSDescriptor object.
 * /* w  ww . jav a 2  s . c  o  m*/
 * @param descriptorAddress
 * @return
 * @throws Exception
 */
public OSDescriptor receiveDescriptor(String descriptorAddress) throws Exception {
    OSCommunication comm = new OSCommunication();
    OSDescriptor osDesciptor = new OSDescriptor();

    try {
        InputStream response = comm.sendRequest(descriptorAddress);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document descriptorDoc = builder.parse(response);

        XPath xpath = XPathFactory.newInstance().newXPath();
        NodeList typeList = (NodeList) xpath.evaluate("/OpenSearchDescription/Url/@type", descriptorDoc,
                XPathConstants.NODESET);
        NodeList templateList = (NodeList) xpath.evaluate("/OpenSearchDescription/Url/@template", descriptorDoc,
                XPathConstants.NODESET);

        for (int i = 0; i < typeList.getLength(); i++) {
            osDesciptor.setTypeAndUrl(typeList.item(i).getTextContent(), templateList.item(i).getTextContent());
        }
    } catch (ParserConfigurationException e) {
        log.error("Error while parsing DescriptorFile from: " + descriptorAddress);
        e.printStackTrace();
    } catch (SAXException e) {
        log.error("Error while parsing DescriptorFile from: " + descriptorAddress);
        e.printStackTrace();
    } catch (XPathExpressionException e) {
        log.error("Error while parsing DescriptorFile from: " + descriptorAddress);
        e.printStackTrace();
    } finally {
        comm.releaseConnection();
    }
    return osDesciptor;
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.opinion.OpinionCorpusFactory.java

@Override
protected OpinionCorpusFactory addXmlPacket(OpinionCorpus corpus, InputStream input)
        throws ParserConfigurationException, SAXException, IOException, XPathException {

    Validate.notNull(corpus, CannedMessages.NULL_ARGUMENT, "corpus");
    Validate.notNull(input, CannedMessages.NULL_ARGUMENT, "input");

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    domFactory.setNamespaceAware(true);/*from  w ww.  j av  a2 s  .  com*/
    Document doc = domFactory.newDocumentBuilder().parse(input);

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

    OpinionDocumentFactory opinionFactory;

    if ("document".equals(doc.getDocumentElement().getLocalName())) {
        opinionFactory = new OpinionDocumentFactory().setCorpus(corpus).setXmlNode(doc.getDocumentElement());
        corpus.addDocument(opinionFactory.create());
        return this;
    }

    Node corpusNode = (Node) xpath.compile("/corpus").evaluate(doc, XPathConstants.NODE);
    if (corpusNode == null) {
        corpusNode = Validate.notNull(doc.getDocumentElement(), CannedMessages.NULL_ARGUMENT, "/corpus");
    }

    String title = (String) xpath.compile("./@title").evaluate(corpusNode, XPathConstants.STRING);
    String description = (String) xpath.compile("./@description").evaluate(corpusNode, XPathConstants.STRING);
    String language = (String) xpath.compile("./@language").evaluate(corpusNode, XPathConstants.STRING);

    if (StringUtils.isNotEmpty(title)) {
        corpus.setTitle(title);
    }

    if (StringUtils.isNotEmpty(description)) {
        corpus.setDescription(description);
    }

    if (StringUtils.isNotEmpty(language)) {
        corpus.setLanguage(language);
    }

    NodeList documentNodes = (NodeList) xpath.compile("./document").evaluate(corpusNode,
            XPathConstants.NODESET);
    if (documentNodes == null || documentNodes.getLength() == 0) {
        documentNodes = corpusNode.getChildNodes();
        Validate.isTrue(documentNodes != null && documentNodes.getLength() > 0, CannedMessages.NULL_ARGUMENT,
                "/corpus/document");
    }

    for (int index = 0; index < documentNodes.getLength(); index++) {
        opinionFactory = new OpinionDocumentFactory().setCorpus(corpus).setXmlNode(documentNodes.item(index));
        corpus.addDocument(opinionFactory.create());
    }

    return this;
}

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

/**
 * Apply validation macro rules.//from   ww  w  .  j  a  va 2  s.co 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.microsoft.samples.federation.FederationMetadataDocument.java

private String getSingleNodeText(String xpathStr) {
    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();

    try {//from w w  w.  ja  v  a  2s .  co  m
        XPathExpression xpe = xpath.compile(xpathStr);
        NodeList nodeList = (NodeList) xpe.evaluate(_fmdDoc, XPathConstants.NODESET);
        if (nodeList.getLength() > 0) {
            return nodeList.item(0).getNodeValue();
        }

    } catch (XPathExpressionException e) {
        //do something here
    }
    return null;
}

From source file:dk.dma.msinm.web.rest.LocationRestService.java

/**
 * Parses the KML and returns a JSON list of locations.
 *
 * TDOD: Handle MultiGeometry.//w w w  . j a  va  2 s .  c  om
 * Example: http://www.microformats.dk/2008/11/02/kommunegrnserne-til-de-98-danske-kommuner/
 *
 * @param kml the KML to parse
 * @return the corresponding list of locations
 */
@POST
@Path("/parse-kml")
@Produces("application/json")
public List<LocationVo> parseKml(String kml) throws UnsupportedEncodingException {

    // Strip BOM from UTF-8 with BOM
    if (kml.startsWith("\uFEFF")) {
        kml = kml.replace("\uFEFF", "");
    }

    // Extract the default namespace
    String namespace = extractDefaultNamespace(kml);

    List<LocationVo> result = new ArrayList<>();
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(new KmlNamespaceContext(namespace));
    InputSource inputSource = new InputSource(new StringReader(kml));

    try {
        // Fetch all "Placemark" elements
        NodeList placemarks = (NodeList) xpath.evaluate("//kml:Placemark", inputSource, XPathConstants.NODESET);

        for (int i = 0; i < placemarks.getLength(); i++) {

            // Fetch all "Point" coordinates
            NodeList coordinates = (NodeList) xpath.evaluate("//kml:Point/kml:coordinates", placemarks.item(i),
                    XPathConstants.NODESET);
            extractLocations(result, coordinates, Location.LocationType.POINT);

            // Fetch all "Polyline" coordinates
            coordinates = (NodeList) xpath.evaluate("//kml:LineString/kml:coordinates", placemarks.item(i),
                    XPathConstants.NODESET);
            extractLocations(result, coordinates, Location.LocationType.POLYLINE);

            // Fetch all "Polygon" coordinates
            coordinates = (NodeList) xpath.evaluate(
                    "//kml:Polygon/kml:outerBoundaryIs/kml:LinearRing/kml:coordinates", placemarks.item(i),
                    XPathConstants.NODESET);
            extractLocations(result, coordinates, Location.LocationType.POLYGON);
        }

    } catch (Exception e) {
        log.error("Error parsing kml", e);
    }

    return result;
}

From source file:org.fedoracommons.funapi.pmh.AbstractPmhResolver.java

/**
 * {@inheritDoc}//from www .j  a  v a2 s  . com
 */
public UnapiFormats getFormats() throws UnapiException {
    String mdFormats = listMetadataFormats();
    UnapiFormats formats = new UnapiFormats(null);
    XPath xpath = getXPath();
    NodeList nodelist = null;
    try {
        nodelist = (NodeList) xpath.evaluate("//oai:metadataFormat",
                new InputSource(new StringReader(mdFormats)), XPathConstants.NODESET);
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node node = nodelist.item(i);
            String format = xpath.evaluate("oai:metadataPrefix", node);
            String docs = xpath.evaluate("oai:schema", node);
            UnapiFormat uFormat = new UnapiFormat(format, "application/xml", docs);
            formats.addFormat(uFormat);
        }
    } catch (XPathExpressionException e) {
        throw new UnapiException(e.getMessage(), e);
    }
    return formats;
}

From source file:com.nebhale.cyclinglibrary.util.XmlPointParser.java

@Override
public Task parse(String raw, PointParserCallback callback) {
    Document document = createDocument(raw);

    try {/* w w  w.  j a  v  a  2 s  . c  om*/
        NodeList result;

        result = (NodeList) GPX_ROUTE.evaluate(document, XPathConstants.NODESET);
        if (result.getLength() != 0) {
            return parseGpxRoute(result, callback);
        }

        result = (NodeList) GPX_TRACK.evaluate(document, XPathConstants.NODESET);
        if (result.getLength() != 0) {
            return parseGpxTrack(result, callback);
        }

        result = (NodeList) TCX_TRACK.evaluate(document, XPathConstants.NODESET);
        if (result.getLength() != 0) {
            return parseTcxTrack(result, callback);
        }
    } catch (XPathExpressionException e) {
        throw new IllegalStateException(e);
    }

    throw new IllegalArgumentException("Unable to parse input");
}

From source file:com.ephesoft.dcma.util.OCREngineUtil.java

/**
 * To format HOCR for Tesseract.//from w  w w . ja va2 s.c o m
 * @param outputFilePath {@link String}
 * @param actualFolderLocation  {@link String}
 * @param pageId {@link String}
 * @throws XPathExpressionException if error occurs
 * @throws TransformerException if error occurs
 * @throws IOException if error occurs
 */
public static void formatHOCRForTesseract(final String outputFilePath, final String actualFolderLocation,
        final String pageId) throws XPathExpressionException, TransformerException, IOException {
    LOGGER.info("Entering format HOCR for tessearct . outputfilepath : " + outputFilePath);
    InputStream inputStream = new FileInputStream(outputFilePath);
    XPathFactory xFactory = new org.apache.xpath.jaxp.XPathFactoryImpl();
    XPath xpath = xFactory.newXPath();
    XPathExpression pageExpr = xpath.compile("//div[@class=\"ocr_page\"]");
    XPathExpression wordExpr = xpath.compile("//span[@class=\"ocr_word\"]");

    // Output format supported by Tesseract 3.00
    XPathExpression xOcrWordExpr = xpath.compile("//span[@class=\"xocr_word\"]");

    // Output format supported by Tesseract 3.01
    XPathExpression ocrXWordExpr = xpath.compile("//span[@class=\"ocrx_word\"]");

    org.w3c.dom.Document doc2 = null;
    try {
        doc2 = XMLUtil.createDocumentFrom(inputStream);
    } catch (Exception e) {
        LOGGER.info("Premature end of file for " + outputFilePath + e);
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
    if (doc2 != null) {
        LOGGER.info("document is not null.");
        NodeList wordList = (NodeList) wordExpr.evaluate(doc2, XPathConstants.NODESET);
        for (int wordNodeIndex = 0; wordNodeIndex < wordList.getLength(); wordNodeIndex++) {
            setWordNodeTextContent(xOcrWordExpr, ocrXWordExpr, wordList, wordNodeIndex);
        }

        NodeList pageList = (NodeList) pageExpr.evaluate(doc2, XPathConstants.NODESET);
        for (int pageNodeIndex = 0; pageNodeIndex < pageList.getLength(); pageNodeIndex++) {
            Node pageNode = pageList.item(pageNodeIndex);
            if (pageNode != null
                    && ((Node) pageNode.getAttributes().getNamedItem(UtilConstants.ID_ATTR)) != null) {
                String pageID = ((Node) pageNode.getAttributes().getNamedItem(UtilConstants.ID_ATTR))
                        .getTextContent();
                wordExpr = xpath.compile("//div[@id='" + pageID + "']//span[@class='ocr_word']");
                NodeList wordInPageList = (NodeList) wordExpr.evaluate(pageNode, XPathConstants.NODESET);

                Node pageNodeClone = pageNode.cloneNode(false);
                for (int i = 0; i < wordInPageList.getLength(); i++) {
                    pageNodeClone.appendChild(wordInPageList.item(i));
                }
                pageNode.getParentNode().appendChild(pageNodeClone);
                pageNode.getParentNode().removeChild(pageNode);
            }
        }

        XMLUtil.flushDocumentToFile(doc2.getDocumentElement().getOwnerDocument(), outputFilePath);
        File tempFile = new File(actualFolderLocation + File.separator + pageId + "_tempFile_hocr.html");
        FileUtils.copyFile(new File(outputFilePath), tempFile);

        XMLUtil.htmlOutputStream(tempFile.getAbsolutePath(), outputFilePath);
        boolean isTempFileDeleted = tempFile.delete();
        if (!isTempFileDeleted) {
            tempFile.delete();
        }
    }
    LOGGER.info("Exiting format HOCR for tessearct . outputfilepath : " + outputFilePath);
}

From source file:nz.co.jsrsolutions.tideservice.geocoding.GoogleTideDataGeoCoder.java

@Override
public GeoLocation getGeoLocation(Port port) throws TideDataGeoCoderException {

    URI uri;// w  w  w . ja  va2  s. c o  m
    try {

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(port.getName());
        stringBuffer.append(", ");
        stringBuffer.append(port.getSubArea().getName());
        uri = mUriBuilder.buildGetGeoLocUri(stringBuffer.toString());
        HttpUriRequest request = new GoogleGeoCoderHttpGet(uri);
        HttpResponse response = mHttpClient.execute(request);

        if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            throw new TideDataGeoCoderException(
                    "HTTP GET returned: " + response.getStatusLine().getStatusCode());
        }

        // read result and parse into XML Document
        HttpEntity entity = response.getEntity();

        Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(entity.getContent());

        // prepare XPath
        XPath xpath = XPathFactory.newInstance().newXPath();

        // extract the result
        NodeList resultNodeList = null;

        // a) Examine the status
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/status", geocoderResultDocument,
                XPathConstants.NODESET);
        if (resultNodeList.getLength() != 1) {
            throw new TideDataGeoCoderException("Couldn't parse result document.");
        }
        GoogleGeoCoderResultStatus resultStatus = GoogleGeoCoderResultStatus
                .valueOf(resultNodeList.item(0).getTextContent());

        switch (resultStatus) {
        case OK:
            break;
        case ZERO_RESULTS:
            throw new TideDataGeoCoderException("No results found for: " + stringBuffer.toString());
        case OVER_QUERY_LIMIT:
            throw new TideDataGeoCoderException("Over query limit (was 2500 requests per day)");
        case REQUEST_DENIED:
            throw new TideDataGeoCoderException("Request denied (using sensor=true|false ?)");
        case INVALID_REQUEST:
            throw new TideDataGeoCoderException("Invalid request - badly formed query");
        }

        // c) extract the coordinates of the first result
        resultNodeList = (NodeList) xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*",
                geocoderResultDocument, XPathConstants.NODESET);
        float lat = Float.NaN;
        float lng = Float.NaN;
        for (int i = 0; i < resultNodeList.getLength(); ++i) {
            Node node = resultNodeList.item(i);
            if ("lat".equals(node.getNodeName())) {
                lat = Float.parseFloat(node.getTextContent());
            }
            if ("lng".equals(node.getNodeName())) {
                lng = Float.parseFloat(node.getTextContent());
            }
        }

        GeoLocation geoLocation = new GeoLocation();
        geoLocation.setLatitude((long) (lat * 1e6));
        geoLocation.setLongitude((long) (lng * 1e6));

        return geoLocation;

    } catch (URISyntaxException e) {
        throw new TideDataGeoCoderException(e);
    } catch (ClientProtocolException e) {
        throw new TideDataGeoCoderException(e);
    } catch (IOException e) {
        throw new TideDataGeoCoderException(e);
    } catch (IllegalStateException e) {
        throw new TideDataGeoCoderException(e);
    } catch (SAXException e) {
        throw new TideDataGeoCoderException(e);
    } catch (ParserConfigurationException e) {
        throw new TideDataGeoCoderException(e);
    } catch (XPathExpressionException e) {
        throw new TideDataGeoCoderException(e);
    }

}