use of javax.xml.xpath.XPath in project stanbol by apache.
the class HtmlExtractionRegistry method initialize.
public void initialize(InputStream configFileStream) throws InitializationException {
try {
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = parser.parse(new InputSource(configFileStream));
Node node;
NodeList nodes = (NodeList) xPath.evaluate("/htmlextractors/extractor", document, XPathConstants.NODESET);
if (nodes != null) {
TransformerFactory transFac = TransformerFactory.newInstance();
transFac.setURIResolver(new BundleURIResolver());
for (int j = 0, iCnt = nodes.getLength(); j < iCnt; j++) {
Node nd = nodes.item(j);
node = (Node) xPath.evaluate("@id", nd, XPathConstants.NODE);
String id = node.getNodeValue();
Node srcNode = (Node) xPath.evaluate("source", nd, XPathConstants.NODE);
if (srcNode != null) {
node = (Node) xPath.evaluate("@type", srcNode, XPathConstants.NODE);
String srcType = node.getNodeValue();
if (srcType.equals("xslt")) {
String rdfFormat = "rdfxml";
Syntax rdfSyntax = Syntax.RdfXml;
node = (Node) xPath.evaluate("@syntax", srcNode, XPathConstants.NODE);
if (node != null) {
rdfFormat = node.getNodeValue();
if (rdfFormat.equalsIgnoreCase("turtle")) {
rdfSyntax = Syntax.Turtle;
} else if (rdfFormat.equalsIgnoreCase("ntriple")) {
rdfSyntax = Syntax.Ntriples;
} else if (rdfFormat.equalsIgnoreCase("n3")) {
rdfSyntax = XsltExtractor.N3;
} else if (!rdfFormat.equalsIgnoreCase("rdfxml")) {
throw new InitializationException("Unknown RDF Syntax: " + rdfFormat + " for " + id + " extractor");
}
}
// TODO: do something about disjunctions of
// Extractors? Assume, only RDFa or Microformats are
// used?
String fileName = DOMUtils.getText(srcNode);
XsltExtractor xsltExtractor = new XsltExtractor(id, fileName, transFac);
xsltExtractor.setSyntax(rdfSyntax);
// name of URI/URL parameter of the script (default
// "uri")
node = (Node) xPath.evaluate("@uri", srcNode, XPathConstants.NODE);
if (node != null) {
xsltExtractor.setUriParameter(node.getNodeValue());
}
registry.put(id, xsltExtractor);
activeExtractors.add(id);
} else if (srcType.equals("java")) {
String clsName = srcNode.getNodeValue();
Object extractor = Class.forName(clsName).newInstance();
if (extractor instanceof HtmlExtractionComponent) {
registry.put(id, (HtmlExtractionComponent) extractor);
activeExtractors.add(id);
} else {
throw new InitializationException("clsName is not an HtmlExtractionComponent");
}
} else {
LOG.warn("No valid type for extractor found: " + id);
}
LOG.info("Extractor for: " + id);
}
}
}
} catch (FileNotFoundException e) {
throw new InitializationException(e.getMessage(), e);
} catch (XPathExpressionException e) {
throw new InitializationException(e.getMessage(), e);
} catch (DOMException e) {
throw new InitializationException(e.getMessage(), e);
} catch (ParserConfigurationException e) {
throw new InitializationException(e.getMessage(), e);
} catch (SAXException e) {
throw new InitializationException(e.getMessage(), e);
} catch (IOException e) {
throw new InitializationException(e.getMessage(), e);
} catch (ClassNotFoundException e) {
throw new InitializationException(e.getMessage(), e);
} catch (InstantiationException e) {
throw new InitializationException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new InitializationException(e.getMessage(), e);
}
}
use of javax.xml.xpath.XPath in project ddf by codice.
the class GmdTransformer method setMetacardDates.
private void setMetacardDates(Metacard metacard, final XstreamPathValueTracker pathValueTracker) {
try (InputStream inputStream = getSourceInputStream()) {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
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);
}
domFactory.setNamespaceAware(false);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document document = builder.parse(inputStream);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression dataExpression = xPath.compile(GmdConstants.RESOURCE_DATE_PATH);
NodeList dataList = (NodeList) dataExpression.evaluate(document, XPathConstants.NODESET);
if (dataList != null && dataList.getLength() > 0) {
List<String> dateList = new ArrayList<>();
List<String> dateTypes = pathValueTracker.getAllValues(toPath(GmdConstants.CITATION_DATE_TYPE_PATH));
for (int i = 0; i < dataList.getLength(); i++) {
Node node = dataList.item(i);
String datestring = node.getTextContent().trim().replaceAll(WHITE_SPACE_PATTER.pattern(), " ").replaceAll(NEW_LINE_PATTERN.pattern(), " ");
String[] stringArray = datestring.split(" ");
dateList.add(stringArray[0]);
}
if (CollectionUtils.isNotEmpty(dateList) && CollectionUtils.isNotEmpty(dateTypes) && dateList.size() == dateTypes.size()) {
setDates(dateList, dateTypes, metacard);
}
}
} catch (ParserConfigurationException | IOException | SAXException | XPathExpressionException e) {
LOGGER.debug("Unable to parse dates in XML document. Metacard Created / Effective / Modified dates will not be set.", e);
}
}
use of javax.xml.xpath.XPath in project bnd by bndtools.
the class PomParser method toBsn.
private Object toBsn(NodeList set) throws XPathExpressionException {
XPath xpath = xpathf.newXPath();
StringBuilder sb = new StringBuilder();
String del = "";
for (int i = 0; i < set.getLength(); i++) {
Node child = set.item(i);
String version = xpath.evaluate("version", child);
sb.append(del);
sb.append(xpath.evaluate("groupId", child));
sb.append(".");
sb.append(xpath.evaluate("artifactId", child));
if (version != null && version.trim().length() != 0) {
sb.append(";version=");
sb.append(Analyzer.cleanupVersion(version));
}
del = ", ";
}
return sb.toString();
}
use of javax.xml.xpath.XPath in project opennms by OpenNMS.
the class XmlTest method xpathGetNodesMatching.
protected static NodeList xpathGetNodesMatching(final String xml, final String expression) throws XPathExpressionException {
final XPath query = XPathFactory.newInstance().newXPath();
StringReader sr = null;
InputSource is = null;
NodeList nodes = null;
try {
sr = new StringReader(xml);
is = new InputSource(sr);
nodes = (NodeList) query.evaluate(expression, is, XPathConstants.NODESET);
} finally {
sr.close();
IOUtils.closeQuietly(sr);
}
return nodes;
}
use of javax.xml.xpath.XPath in project android by JetBrains.
the class ThemePreviewBuilderTest method testSearchFilter.
public void testSearchFilter() throws ParserConfigurationException, XPathExpressionException {
List<ThemePreviewBuilder.ComponentDefinition> componentDefinitionList = ImmutableList.of(new ThemePreviewBuilder.ComponentDefinition("Spinner", ThemePreviewBuilder.ComponentGroup.TEXT, "Spinner"), new ThemePreviewBuilder.ComponentDefinition("Custom_Component", ThemePreviewBuilder.ComponentGroup.CUSTOM, "CustomComponent").addAlias("Spinner").addAlias("ABC").addAlias("DEF"));
XPath xPath = XPathFactory.newInstance().newXPath();
ThemePreviewBuilder customComponentBuilder = new ThemePreviewBuilder().addAllComponents(componentDefinitionList);
// Check the search "spinner" returns both the actual spinner control and the custom component with the alias
Document document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("SPINNER")).build();
NodeList nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(2, nodeList.getLength());
// Test matching the name
document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("CustomCOMPONENT")).build();
nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(1, nodeList.getLength());
// Test matching the description
document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("Custom_COMPONENT")).build();
nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(1, nodeList.getLength());
// Test searching for an alias that only matches our custom component
document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC")).build();
nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(1, nodeList.getLength());
// Test partial match
document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("EF")).build();
nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(1, nodeList.getLength());
// Test case sensitive search
document = customComponentBuilder.addComponentFilter(new ThemePreviewBuilder.SearchFilter("AbC", true)).build();
nodeList = (NodeList) xPath.evaluate(COMPONENTS_XPATH, document.getDocumentElement(), XPathConstants.NODESET);
assertEquals(0, nodeList.getLength());
}
Aggregations