Java源码示例:net.sf.saxon.s9api.XPathCompiler
示例1
public String execute(Node node, String expression)
throws SaxonApiException {
Processor proc = new Processor(false);
XPathCompiler xpath = proc.newXPathCompiler();
DocumentBuilder builder = proc.newDocumentBuilder();
String fileName = getClass().getResource(
map.getLogicalSource().getIdentifier()).getFile();
XdmNode doc = builder.build(new File(fileName));
String expre = replace(node, expression);
expression = expression.replaceAll(
"\\{" + expression.split("\\{")[1].split("\\}")[0] + "\\}", "'"
+ expre + "'");
XPathSelector selector = xpath.compile(expression).load();
selector.setContextItem(doc);
// Evaluate the expression.
Object result = selector.evaluate();
return result.toString();
}
示例2
private XPathExecutable createXPath(final String expression, final Map<String, String> namespaces) {
try {
final XPathCompiler compiler = this.processor.newXPathCompiler();
if (namespaces != null) {
namespaces.forEach(compiler::declareNamespace);
}
return compiler.compile(expression);
} catch (final SaxonApiException e) {
throw new IllegalStateException(String.format("Can not compile xpath match expression '%s'",
StringUtils.isNotBlank(expression) ? expression : "EMPTY EXPRESSION"), e);
}
}
示例3
/**
* Erzeugt einen [@link XPathExecutable} auf Basis der angegebenen Informationen.
*
* @param expression der XPATH-Ausdruck
* @param namespaces optionale Namespace-Mappings
* @return ein kompiliertes Executable
*/
public XPathExecutable createXPath(final String expression, final Map<String, String> namespaces) {
try {
final XPathCompiler compiler = getProcessor().newXPathCompiler();
if (namespaces != null) {
namespaces.forEach(compiler::declareNamespace);
}
return compiler.compile(expression);
} catch (final SaxonApiException e) {
throw new IllegalStateException(String.format("Can not compile xpath match expression '%s'",
StringUtils.isNotBlank(expression) ? expression : "EMPTY EXPRESSION"), e);
}
}
示例4
private XPathSelector createSelector(AssertionType assertion) throws SaxonApiException {
try {
final XPathCompiler compiler = getProcessor().newXPathCompiler();
assertions.getNamespace().forEach(ns -> compiler.declareNamespace(ns.getPrefix(), ns.getValue()));
return compiler.compile(assertion.getTest()).load();
} catch (SaxonApiException e) {
throw new IllegalStateException(String.format("Can not compile xpath match expression '%s'",
StringUtils.isNotBlank(assertion.getTest()) ? assertion.getTest() : "EMPTY EXPRESSION"), e);
}
}
示例5
@Override
public void initialize(InputSplit inSplit, TaskAttemptContext context)
throws IOException, InterruptedException {
Path file = ((FileSplit)inSplit).getPath();
FileSystem fs = file.getFileSystem(context.getConfiguration());
FSDataInputStream fileIn = fs.open(file);
DocumentBuilder docBuilder = builderLocal.get();
try {
Document document = docBuilder.parse(fileIn);
net.sf.saxon.s9api.DocumentBuilder db = saxonBuilderLocal.get();
XdmNode xdmDoc = db.wrap(document);
XPathCompiler xpath = proc.newXPathCompiler();
xpath.declareNamespace("wp",
"http://www.mediawiki.org/xml/export-0.4/");
XPathSelector selector = xpath.compile(PATH_EXPRESSION).load();
selector.setContextItem(xdmDoc);
items = new ArrayList<XdmItem>();
for (XdmItem item : selector) {
items.add(item);
}
} catch (SAXException ex) {
ex.printStackTrace();
throw new IOException(ex);
} catch (SaxonApiException e) {
e.printStackTrace();
} finally {
if (fileIn != null) {
fileIn.close();
}
}
}
示例6
/**
* Set the namespaces in the XPathCompiler.
*
* @param xpathCompiler
* @param namespaceMappings Namespace prefix to Namespace uri mappings
*/
private void setPrefixNamespaceMappings(XPathCompiler xpathCompiler, HashMap<String,String> namespaceMappings) {
if (namespaceMappings != null) {
// Get the mappings
Set<Entry<String, String>> mappings = namespaceMappings.entrySet();
// If mappings exist, set the namespaces
if (mappings != null) {
Iterator<Entry<String, String>> it = mappings.iterator();
while (it.hasNext()) {
Entry<String, String> entry = it.next();
xpathCompiler.declareNamespace(entry.getKey(), entry.getValue());
}
}
}
// Add in the defaults
xpathCompiler.declareNamespace("xml",NamespaceConstant.XML);
xpathCompiler.declareNamespace("xs",NamespaceConstant.SCHEMA);
xpathCompiler.declareNamespace("fn",NamespaceConstant.FN);
}
示例7
/**
* Initialization to improve performance for repetitive invocations of filter and evaluate expressions
*
* @throws XPathException
*/
private void init() throws XPathException {
try {
// Get the processor
proc = new Processor(false);
// Set any specified configuration properties for the processor
if (featureMappings != null) {
for (Entry<String, Object> entry : featureMappings.entrySet()) {
proc.setConfigurationProperty(entry.getKey(), entry.getValue());
}
}
//proc.setConfigurationProperty(FeatureKeys.ENTITY_RESOLVER_CLASS, "com.elsevier.spark_xml_utils.common.IgnoreDoctype");
// Get the XPath compiler
XPathCompiler xpathCompiler = proc.newXPathCompiler();
// Set the namespace to prefix mappings
this.setPrefixNamespaceMappings(xpathCompiler, namespaceMappings);
// Compile the XPath expression and get a document builder
xsel = xpathCompiler.compile(xPathExpression).load();
builder = proc.newDocumentBuilder();
// Create and initialize the serializer
baos = new ByteArrayOutputStream();
serializer = proc.newSerializer(baos);
serializer.setOutputStream(baos);
serializer.setOutputProperty(Serializer.Property.METHOD, "xml");
serializer.setOutputProperty(Serializer.Property.OMIT_XML_DECLARATION,"yes");
serializer.setProcessor(proc);
} catch (SaxonApiException e) {
log.error("Problems creating an XPathProcessor. " + e.getMessage(),e);
throw new XPathException(e.getMessage());
}
}