Search in sources :

Example 96 with XPathExpression

use of javax.xml.xpath.XPathExpression in project ddf by codice.

the class XPathCache method getCompiledExpression.

public static XPathExpression getCompiledExpression(String xpathExpressionkey) throws XPathExpressionException, NullPointerException {
    // go to cache, check if we have the compiled expression
    XPathExpression compiledExpression = expressionMap.get(xpathExpressionkey);
    if (compiledExpression == null) {
        // must compile new expression and place in the map
        compiledExpression = XPATH.compile(xpathExpressionkey);
        expressionMap.put(xpathExpressionkey, compiledExpression);
    }
    return compiledExpression;
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression)

Example 97 with XPathExpression

use of javax.xml.xpath.XPathExpression in project ddf by codice.

the class XPathHelper method evaluate.

/**
     * @param xpathExpressionKey
     * @param returnType
     * @param nsContext
     * @return
     * @throws XPathExpressionException
     */
public synchronized Object evaluate(String xpathExpressionKey, QName returnType, NamespaceContext nsContext) throws XPathExpressionException {
    XPathCache.getXPath().setNamespaceContext(nsContext);
    XPathExpression compiledExpression = XPathCache.getCompiledExpression(xpathExpressionKey);
    Thread thread = Thread.currentThread();
    ClassLoader loader = thread.getContextClassLoader();
    thread.setContextClassLoader(this.getClass().getClassLoader());
    DocumentBuilder documentBuilder;
    byte[] array;
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        documentBuilder = dbf.newDocumentBuilder();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(bos);
        transformer.transform(source, result);
        array = bos.toByteArray();
    } catch (IOException | ParserConfigurationException | TransformerException e) {
        throw new XPathExpressionException(e);
    }
    try (ByteArrayInputStream bis = new ByteArrayInputStream(array)) {
        return compiledExpression.evaluate(documentBuilder.parse(bis), returnType);
    } catch (IOException | SAXException e) {
        throw new XPathExpressionException(e);
    } finally {
        thread.setContextClassLoader(loader);
    }
}
Also used : XPathExpression(javax.xml.xpath.XPathExpression) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) XPathExpressionException(javax.xml.xpath.XPathExpressionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 98 with XPathExpression

use of javax.xml.xpath.XPathExpression in project ddf by codice.

the class GmdTransformer method setMetacardLocationFromBoundingPolygonPoints.

private void setMetacardLocationFromBoundingPolygonPoints(Metacard metacard) {
    try (InputStream inputStream = getSourceInputStream()) {
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(false);
        try {
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            domFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        } catch (ParserConfigurationException e) {
            LOGGER.debug("Unable to configure features on document builder.", e);
        }
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document document = builder.parse(inputStream);
        XPath xPath = XPathFactory.newInstance().newXPath();
        XPathExpression dataExpression = xPath.compile(GmdConstants.BOUNDING_POLYGON_POINT_PATH);
        NodeList dataList = (NodeList) dataExpression.evaluate(document, XPathConstants.NODESET);
        if (dataList.getLength() > 0) {
            List<Coordinate> coordinates = new ArrayList<>();
            for (int i = 0; i < dataList.getLength(); i++) {
                Node node = dataList.item(i);
                String[] coordinateStrings = node.getTextContent().split(" ");
                if (coordinateStrings.length == 2) {
                    Double lat = Double.parseDouble(coordinateStrings[0]);
                    Double lon = Double.parseDouble(coordinateStrings[1]);
                    Coordinate coordinate = new Coordinate(lat, lon);
                    coordinates.add(coordinate);
                }
            }
            if (CollectionUtils.isNotEmpty(coordinates)) {
                // Close the polygon
                coordinates.add(coordinates.get(0));
                LinearRing linearRing = factory.createLinearRing(coordinates.toArray(new Coordinate[coordinates.size()]));
                String wkt = WKT_WRITER_THREAD_LOCAL.get().write(factory.createPolygon(linearRing, null));
                if (wkt != null) {
                    metacard.setAttribute(new AttributeImpl(Core.LOCATION, wkt));
                }
            }
        }
    } catch (NumberFormatException | ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
        LOGGER.debug("Unable to parse location in XML document.  Metacard location will not be set.", e);
    }
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) InputStream(java.io.InputStream) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) AttributeImpl(ddf.catalog.data.impl.AttributeImpl) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Coordinate(com.vividsolutions.jts.geom.Coordinate) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LinearRing(com.vividsolutions.jts.geom.LinearRing)

Aggregations

XPathExpression (javax.xml.xpath.XPathExpression)98 XPath (javax.xml.xpath.XPath)69 NodeList (org.w3c.dom.NodeList)56 Document (org.w3c.dom.Document)48 XPathExpressionException (javax.xml.xpath.XPathExpressionException)40 XPathFactory (javax.xml.xpath.XPathFactory)40 Node (org.w3c.dom.Node)38 DocumentBuilder (javax.xml.parsers.DocumentBuilder)24 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)19 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)13 Element (org.w3c.dom.Element)12 PBXNativeTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXNativeTarget)11 PBXTarget (com.facebook.buck.apple.xcode.xcodeproj.PBXTarget)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 IOException (java.io.IOException)11 Path (java.nio.file.Path)11 PBXFileReference (com.facebook.buck.apple.xcode.xcodeproj.PBXFileReference)10 InputSource (org.xml.sax.InputSource)9