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;
}
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);
}
}
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);
}
}
Aggregations