use of javax.xml.xpath.XPathExpression in project camel by apache.
the class XPathBuilder method logNamespaces.
private void logNamespaces(Exchange exchange) {
InputStream is = null;
NodeList answer = null;
XPathExpression xpathExpression = null;
try {
xpathExpression = poolLogNamespaces.poll();
if (xpathExpression == null) {
xpathExpression = createTraceNamespaceExpression();
}
// prepare the input
Object document;
if (isInputStreamNeeded(exchange)) {
is = exchange.getIn().getBody(InputStream.class);
document = getDocument(exchange, is);
} else {
Object body = exchange.getIn().getBody();
document = getDocument(exchange, body);
}
// fetch all namespaces
if (document instanceof InputSource) {
InputSource inputSource = (InputSource) document;
answer = (NodeList) xpathExpression.evaluate(inputSource, XPathConstants.NODESET);
} else if (document instanceof DOMSource) {
DOMSource source = (DOMSource) document;
answer = (NodeList) xpathExpression.evaluate(source.getNode(), XPathConstants.NODESET);
} else {
answer = (NodeList) xpathExpression.evaluate(document, XPathConstants.NODESET);
}
} catch (Exception e) {
LOG.warn("Unable to trace discovered namespaces in XPath expression", e);
} finally {
// IOHelper can handle if is is null
IOHelper.close(is);
poolLogNamespaces.add(xpathExpression);
}
if (answer != null) {
logDiscoveredNamespaces(answer);
}
}
use of javax.xml.xpath.XPathExpression in project sonarqube by SonarSource.
the class XpathParser method executeXPath.
public Object executeXPath(Node node, QName qname, String xPathExpression) {
XPathExpression expr = compiledExprs.get(xPathExpression);
try {
if (expr == null) {
expr = xpath.compile(xPathExpression);
compiledExprs.put(xPathExpression, expr);
}
return expr.evaluate(node, qname);
} catch (XPathExpressionException e) {
throw new XmlParserException("Unable to evaluate xpath expression :" + xPathExpression, e);
}
}
use of javax.xml.xpath.XPathExpression in project jangaroo-tools by CoreMedia.
the class ASDocScreenScraper method scrape.
public void scrape() throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException {
Document doc = loadAndParse(url);
this.document = doc;
XPath xpath = xPathFactory.newXPath();
XPathExpression packageNameExpression = xpath.compile("//*[@id='packageName']/text()");
Node packageNameNode = (Node) packageNameExpression.evaluate(doc, XPathConstants.NODE);
packageName = htmlTrim(packageNameNode.getNodeValue());
if ("Top Level".equals(packageName)) {
packageName = "";
}
String packageDirs = "../joo/" + packageName.replaceAll("\\.", "/");
File packageDirsFile = new File(packageDirs);
if (!packageDirsFile.mkdirs()) {
System.out.println("[INFO] Package directory " + packageDirsFile.getAbsolutePath() + " already exists.");
}
className = htmlTrim(packageNameNode.getParentNode().getNextSibling().getNextSibling().getNodeValue());
File classFile = new File(packageDirsFile, className + ".as");
System.out.println("Writing file " + classFile.getCanonicalPath());
PrintWriter writer = new PrintWriter(classFile, "UTF-8");
writer.println("package " + packageName + " {");
XPathExpression classDeclarationExpression = xpath.compile("/*[name()='html']/*[name()='body']/*[name()='div']/*[name()='div'][@id='content']/*[name()='div'][1]/*[name()='div'][1]/*[name()='table'][1]/*[name()='tr'][2]/*[name()='td'][2]/text()");
Node classDeclarationNode = (Node) classDeclarationExpression.evaluate(doc, XPathConstants.NODE);
String classDeclaration = classDeclarationNode.getNodeValue();
// TODO: more exact match (Pattern) needed?
isInterface = classDeclaration.indexOf("interface") != -1;
while (classDeclarationNode.getNextSibling() != null) {
classDeclarationNode = classDeclarationNode.getNextSibling();
classDeclaration += "a".equals(classDeclarationNode.getNodeName()) ? toType(classDeclarationNode) : classDeclarationNode.getNodeValue();
}
XPathExpression extendsExpression = xpath.compile("//*[@class='classHeaderTableLabel'][text()='Inheritance']/following-sibling::*/*[name()='a']");
Node extendsClassNode = (Node) extendsExpression.evaluate(doc, XPathConstants.NODE);
String extendsClause = "";
if (extendsClassNode != null) {
String extendsClass = toType(extendsClassNode);
if (!"Object".equals(extendsClass)) {
extendsClause = " extends " + extendsClass;
}
}
XPathExpression implementsExpression = xpath.compile("//*[@class='classHeaderTableLabel'][text()='Implements']/following-sibling::*/*[name()='a']");
NodeList implementsNodes = (NodeList) implementsExpression.evaluate(doc, XPathConstants.NODESET);
String implementsClause = getImplementsClause(implementsNodes);
XPathExpression propertyDeclarations = xpath.compile("//*[@class='content']//*[name()='span'][not(@product)][contains(@runtime,'Flash::')] | //*[@class='content']/*[@class='MainContent']/*[name()='span'][not(@product)][not(@runtime)]");
//XPathExpression propertyDeclarations = xpath.compile("//*[@class='MainContent'][2]/*[name()='div'][@class='detailBody']");
NodeList propertyDeclarationNodes = (NodeList) propertyDeclarations.evaluate(doc, XPathConstants.NODESET);
XPathExpression eventNameExpression = xPathFactory.newXPath().compile(".//*[name()='h2']/text()");
StringBuilder eventCode = new StringBuilder();
StringBuilder memberCode = new StringBuilder();
for (int i = 0; i < propertyDeclarationNodes.getLength(); i++) {
Node propertyDeclarationNode = propertyDeclarationNodes.item(i);
Node propertyDeclarationHeader = propertyDeclarationNode.getNextSibling();
propertyDeclarationNode = propertyDeclarationHeader.getNextSibling();
if (!(propertyDeclarationNode != null && propertyDeclarationNode instanceof Element && "detailBody".equals(((Element) propertyDeclarationNode).getAttribute("class")))) {
System.out.println("[WARN] Property declaration not followed by <div class='detailBody'>.");
} else {
NodeList eventNodes = (NodeList) xpath.evaluate("*[name()='a']/*[name()='code']/text()", propertyDeclarationNode, XPathConstants.NODESET);
Node implementationNode = (Node) xpath.evaluate("*[name()='span'][@class='label'][text()='Implementation']", propertyDeclarationNode, XPathConstants.NODE);
NodeList docNodes = propertyDeclarationNode.getChildNodes();
if (eventNodes.getLength() == 2) {
eventCode.append(getASDoc(docNodes, eventNodes.item(1)));
String eventName = ((String) eventNameExpression.evaluate(propertyDeclarationHeader, XPathConstants.STRING)).trim();
eventCode.append("[Event(name=\"").append(eventName).append("\", type=\"").append(eventNodes.item(0).getNodeValue()).append("\")]\n");
} else {
memberCode.append(getASDoc(docNodes, null));
memberCode.append(unparseCode(implementationNode == null ? propertyDeclarationNode.getFirstChild() : implementationNode.getNextSibling()));
}
}
}
writer.println(getImports());
writer.println(eventCode);
writer.print(getClassDoc());
writer.println(classDeclaration + extendsClause + implementsClause + " {");
writer.print(memberCode);
writer.println("}");
writer.println("}");
writer.flush();
writer.close();
}
use of javax.xml.xpath.XPathExpression in project jangaroo-tools by CoreMedia.
the class ASDocScreenScraper method main.
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException, URISyntaxException {
if (args.length > 0) {
new ASDocScreenScraper(ADOBE_FLASH_PLATFORM_REFERENCE_BASE_URL + args[0].replaceAll("\\.", "/") + ".html").scrape();
return;
}
Document classSummaryDocument = loadAndParse(new URI(ADOBE_FLASH_PLATFORM_REFERENCE_BASE_URL + "class-summary.html"));
XPath xpath = xPathFactory.newXPath();
XPathExpression classNodesExpression = xpath.compile("//*[@class='summaryTable']//*[name()='tr'][not(@product)][contains(@runtime,'Flash::')]//*[name()='a']/@href");
NodeList classNodes = (NodeList) classNodesExpression.evaluate(classSummaryDocument, XPathConstants.NODESET);
System.out.println("Hits: " + classNodes.getLength());
for (int i = 0; i < classNodes.getLength(); i++) {
String relativeClassUrl = classNodes.item(i).getNodeValue();
if (!relativeClassUrl.endsWith("package-detail.html")) {
System.out.println(relativeClassUrl);
new ASDocScreenScraper(ADOBE_FLASH_PLATFORM_REFERENCE_BASE_URL + relativeClassUrl).scrape();
}
}
}
use of javax.xml.xpath.XPathExpression in project spring-boot by spring-projects.
the class GradleIT method evaluateExpression.
private static String evaluateExpression(String expression) {
try {
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile(expression);
String version = expr.evaluate(new InputSource(new FileReader("pom.xml")));
return version;
} catch (Exception ex) {
throw new IllegalStateException("Failed to evaluate expression", ex);
}
}
Aggregations