本文整理汇总了Java中net.sf.saxon.s9api.Processor类的典型用法代码示例。如果您正苦于以下问题:Java Processor类的具体用法?Java Processor怎么用?Java Processor使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Processor类属于net.sf.saxon.s9api包,在下文中一共展示了Processor类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAddAttribute
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
@Test
public void testAddAttribute() throws Exception {
GauloisPipe piper = new GauloisPipe(configFactory);
ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "./src/test/resources/AddAttributeJavaStep.xml");
Config config = cu.buildConfig(emptyInputParams);
config.verify();
piper.setConfig(config);
piper.setInstanceName("ADD_ATTRIBUTE");
piper.launch();
Processor proc = new Processor(configFactory.getConfiguration());
File expect = new File("target/generated-test-files/source-identity-addAttribute.xml");
XdmNode document = proc.newDocumentBuilder().build(expect);
XPathExecutable exec = proc.newXPathCompiler().compile("//*[@test]");
XPathSelector selector = exec.load();
selector.setContextItem((XdmItem)document);
XdmValue result = selector.evaluate();
assertTrue(result.size()>0);
expect.delete();
}
示例2: testXdmValueToXsl
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
@Test
public void testXdmValueToXsl() throws InvalidSyntaxException, SaxonApiException, URISyntaxException, IOException, ValidationException {
GauloisPipe piper = new GauloisPipe(configFactory);
ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "./src/test/resources/paramDate.xml");
HashMap<QName,ParameterValue> params = new HashMap<>();
QName qnDate = new QName("date");
// to get a date XdmValue from saxon, we must be brilliants !
Processor proc = new Processor(configFactory.getConfiguration());
XQueryEvaluator ev = proc.newXQueryCompiler().compile("current-dateTime()").load();
XdmItem item = ev.evaluateSingle();
params.put(qnDate, new ParameterValue(qnDate, item, factory.getDatatype(new QName("xs","http://www.w3.org/2001/XMLSchema","dateTime"))));
Config config = cu.buildConfig(params);
config.verify();
piper.setConfig(config);
piper.setInstanceName("XDM_VALUE_TO_XSL");
piper.launch();
File expect = new File("target/generated-test-files/date-output.xml");
XdmNode document = proc.newDocumentBuilder().build(expect);
XPathExecutable exec = proc.newXPathCompiler().compile("/date");
XPathSelector selector = exec.load();
selector.setContextItem((XdmItem)document);
XdmValue result = selector.evaluate();
assertTrue(result.size()>0);
}
示例3: testStaticBaseUri
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
public void testStaticBaseUri() throws InvalidSyntaxException, SaxonApiException, URISyntaxException, IOException, ValidationException {
File expect = new File("target/generated-test-file/static-base-uri-ret.xml");
if(expect.exists()) expect.delete();
GauloisPipe piper = new GauloisPipe(configFactory);
ConfigUtil cu = new ConfigUtil(configFactory.getConfiguration(), piper.getUriResolver(), "./src/test/resources/static-base-uri.xml");
HashMap<QName,ParameterValue> params = new HashMap<>();
Config config = cu.buildConfig(params);
config.verify();
piper.setConfig(config);
piper.setInstanceName("STATIC-BASE-URI");
piper.launch();
assertTrue(expect.exists());
Processor proc = new Processor(configFactory.getConfiguration());
XdmNode document = proc.newDocumentBuilder().build(expect);
XPathExecutable exec = proc.newXPathCompiler().compile("/ret/*/text()");
XPathSelector selector = exec.load();
selector.setContextItem((XdmItem)document);
XdmValue result = selector.evaluate();
String staticBaseUri = result.itemAt(0).getStringValue();
String gpStaticBaseUri = result.itemAt(1).getStringValue();
assertTrue("gp:staticBaseUri does not ends with target/classes/xsl/static-base-uri-ret.xml", gpStaticBaseUri.endsWith("target/classes/xsl/static-base-uri-ret.xml"));
expect.delete();
}
示例4: applyXpath
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
public static List<String> applyXpath(String xml, String xpathString) {
List<String> result = new ArrayList<>();
try {
Processor proc = new Processor(false);
XPathCompiler xpath = proc.newXPathCompiler();
DocumentBuilder builder = proc.newDocumentBuilder();
// Load the XML document.
StringReader reader = new StringReader(xml);
XdmNode doc = builder.build(new StreamSource(reader));
// Compile the xpath
XPathSelector selector = xpath.compile(xpathString).load();
selector.setContextItem(doc);
// Evaluate the expression.
XdmValue nodes = selector.evaluate();
for (XdmItem item : nodes) {
result.add(item.toString());
}
} catch (Exception e) {
LOGGER.error("Error applying XPath", e);
}
return result;
}
示例5: compileXsl
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
/**
* Compiles an xsl file.
* @param inputStream the xsl file as {@link InputStream}
* @param processor the {@link Processor} that provides the compiler
* @return an {@link XsltExecutable} that will be used further in the xsl transformation process
* @throws SaxonApiException it is thrown if an exception occurs during the compilation
*/
public XsltExecutable compileXsl(final InputStream inputStream, final Processor processor) throws SaxonApiException {
XsltCompiler xsltCompiler = processor.newXsltCompiler();
Source xslSource = streamSourceFactory.createStreamSource(inputStream);
xsltCompiler.setErrorListener(errorListener);
XsltExecutable xsltExecutable = xsltCompiler.compile(xslSource);
return xsltExecutable;
}
示例6: transform
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
/**
* Transforms an inputstream based on a template and an xsl.
* @param xslInputStream the xsl as an {@link InputStream}
* @param requestInputStream the request as an {@link InputStream}
* @param templateInputStream the template as an {@link InputStream}
* @param nameToXml the names identifying a request or response and their bodies
* @return the result of the transformation
* @throws SaxonApiException it is thrown if an exception occurs during the xsl transformation
* @throws SAXException it is thrown if an exception occurs during the xsl transformation
*/
public byte[] transform(final InputStream xslInputStream, final InputStream requestInputStream, final InputStream templateInputStream,
final Map<String, String> nameToXml) throws SaxonApiException, SAXException {
Processor processor = processorFactory.createProcessor();
XMLReader xmlReader = createXMLReader();
//xsl compilation
XsltExecutable xsltExecutable = xslCompiler.compileXsl(xslInputStream, processor);
XsltTransformer xsltTransformer = xsltExecutable.load();
//set the output
ByteArrayOutputStream output = xslOutputProvider.getOutput(xsltTransformer);
//set the request
setRequest(requestInputStream, processor, xsltTransformer);
setSessionEntities(nameToXml, processor, xsltTransformer);
//set the template
setTemplate(templateInputStream, processor, xmlReader, xsltTransformer);
xsltTransformer.transform();
return output.toByteArray();
}
示例7: evaluateXQuery
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
/**
* Executes a given XQuery expression on the given XML.
* @param xml is the XQuery data source
* @param query is the query expression
* @return with the query result as String
* @throws SaxonApiException was thrown when the XQuery execution is failed
*/
public String evaluateXQuery(final String xml, final String query) throws SaxonApiException {
Processor processor = processorFactory.createProcessor();
XQueryCompiler xqueryCompiler = processor.newXQueryCompiler();
xqueryCompiler.setErrorListener(errorListener);
XQueryExecutable xqueryExec = xqueryCompiler.compile(query);
XQueryEvaluator xqueryEval = xqueryExec.load();
xqueryEval.setErrorListener(errorListener);
SAXSource requestXml = saxSourceFactory.createSAXSource(inputSourceFactory.createInputSource(xml));
xqueryEval.setSource(requestXml);
ByteArrayOutputStream baos = byteArrayOutputStreamFactory.createByteArrayOutputStream();
Serializer ser = serializerFactory.createSerializer(baos);
xqueryEval.setDestination(ser);
xqueryEval.run();
return baos.toString();
}
示例8: call
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
public Sequence call(XPathContext context, Sequence[] arguments) throws XPathException {
final Processor processor = new Processor(context.getConfiguration());
final DocumentBuilder builder = processor.newDocumentBuilder();
try {
BuildingStreamWriterImpl writer = builder.newBuildingStreamWriter();
writer.writeStartElement("test1");
writer.writeCharacters("text1");
writer.writeEndElement();
writer.writeStartElement("test2");
writer.writeCharacters("text2");
writer.writeEndElement();
return writer.getDocumentNode().getUnderlyingValue();
} catch (SaxonApiException | XMLStreamException e) {
logger.error(e, e);
throw new XPathException("Failed to create return value: " + e.getMessage());
}
}
示例9: evaluateXPath2
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
/**
* Evaluates an XPath 2.0 expression using the Saxon s9api interfaces.
*
* @param xmlSource
* The XML Source.
* @param expr
* The XPath expression to be evaluated.
* @param nsBindings
* A collection of namespace bindings required to evaluate the
* XPath expression, where each entry maps a namespace URI (key)
* to a prefix (value).
* @return An XdmValue object representing a value in the XDM data model;
* this is a sequence of zero or more items, where each item is
* either an atomic value or a node.
* @throws SaxonApiException
* If an error occurs while evaluating the expression; this
* always wraps some other underlying exception.
*/
public static XdmValue evaluateXPath2(Source xmlSource, String expr,
Map<String, String> nsBindings) throws SaxonApiException {
Processor proc = new Processor(false);
XPathCompiler compiler = proc.newXPathCompiler();
for (String nsURI : nsBindings.keySet()) {
compiler.declareNamespace(nsBindings.get(nsURI), nsURI);
}
XPathSelector xpath = compiler.compile(expr).load();
DocumentBuilder builder = proc.newDocumentBuilder();
XdmNode node = null;
if (DOMSource.class.isInstance(xmlSource)) {
DOMSource domSource = (DOMSource) xmlSource;
node = builder.wrap(domSource.getNode());
} else {
node = builder.build(xmlSource);
}
xpath.setContextItem(node);
return xpath.evaluate();
}
示例10: XsltExecutor
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
public XsltExecutor(XsltConfig_V2 config,
BlockingQueue<Task> taskQueue, FaultTolerance faultTolerance, UserExecContext ctx,
Integer totalFileCounter)
throws DPUException {
LOG.info("New executor created!");
this.proc = new Processor(false);
// register UUID extension function
proc.registerExtensionFunction(UUIDGenerator.getInstance());
this.compiler = proc.newXsltCompiler();
try {
this.executable = compiler.compile(
new StreamSource(new StringReader(config.getXsltTemplate())));
} catch (SaxonApiException ex) {
throw ContextUtils.dpuException(ctx, ex, "xslt.dpu.executor.errors.xsltCompile");
}
this.config = config;
this.taskQueue = taskQueue;
this.ctx = ctx;
this.totalFileCounter = totalFileCounter;
}
示例11: newValidator
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
/**
* Compiles the (Schematron-derived) XSLT stylesheet that implements additional validation checks.
*/
private XsltExecutable newValidator(String validatorPath) {
// This method should only be called once in the constructor, and the
// stylesheet is only compiled once, so we
// don't cache any of these objects like we normally would.
final URL resource = getClass().getResource(validatorPath);
if (resource == null) {
throw new RuntimeException("Deployment error: can't find additional TAXII validator (" + validatorPath + ")");
}
try {
final boolean useLicensedEdition = false;
return new Processor(useLicensedEdition).newXsltCompiler()
.compile(new StreamSource(resource.toString()));
}
catch (SaxonApiException e) {
throw new RuntimeException("Deployment error: The validator stylesheet contains static errors or it cannot be read. See the standard error output for details.",
e);
}
}
示例12: WikiI5Processor
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
public WikiI5Processor(I5Corpus corpus, String inflectives) throws I5Exception {
if (corpus == null){
throw new IllegalArgumentException("I5Corpus cannot be null.");
}
this.corpus = corpus;
this.xPathFactory = XPathFactory.newInstance();
this.xPath = xPathFactory.newXPath();
this.processor = new Processor(true);
this.serializer = new Serializer();
String errorFilename = corpus.getDumpFilename().substring(0,15) + "-"+
corpus.getType();
errorHandler = new I5ErrorHandler(errorFilename);
// Set temporary i5 file
tempI5 = new File(corpus.getLang()+"wiki-"+corpus.getType()+"-temp.i5");
setSerializer(corpus.getEncoding());
setTransformer(inflectives);
setXmlBuilder(); // Setting a document builder for reading XML
setXmlReader(); // Setting an XML reader for DTD validation
}
示例13: constructElementParserDatatype
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
private Datatype constructElementParserDatatype(final QName qn, final boolean allowsEmpty, final boolean allowsMultiple) throws ValidationException {
return new Datatype() {
@Override
public boolean isAtomic() { return false; }
@Override
public boolean allowsMultiple() { return allowsMultiple; }
@Override
public boolean allowsEmpty() { return allowsEmpty; }
@Override
public XdmValue convert(String input, Configuration configuration) throws ValidationException {
Processor proc = new Processor(configuration);
DocumentBuilder builder = proc.newDocumentBuilder();
String wrappedInput="<fake:document xmlns:fake=\"top:marchand:xml:gaulois:wrapper\">".concat(input).concat("</fake:document>");
InputStream is = new ByteArrayInputStream(wrappedInput.getBytes(Charset.forName("UTF-8")));
try {
XdmNode documentNode = builder.build(new StreamSource(is));
XPathCompiler compiler = proc.newXPathCompiler();
compiler.declareNamespace("fake", "top:marchand:xml:gaulois:wrapper");
XPathSelector selector = compiler.compile("/fake:document/node()").load();
selector.setContextItem(documentNode);
XdmValue ret = selector.evaluate();
if(ret.size()==0 && !allowsEmpty()) throw new ValidationException(qn.toString()+" does not allow empty sequence");
if(ret.size()>1 && !allowsMultiple()) throw new ValidationException(qn.toString()+" does not allow sequence with more than one element");
return ret;
} catch(SaxonApiException ex) {
throw new ValidationException(input+" can not be casted to "+qn.toString());
}
}
};
}
示例14: testExtFunctions
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
@Test
public void testExtFunctions() throws Exception {
Configuration config = new DefaultSaxonConfigurationFactory().getConfiguration();
Processor proc = new Processor(config);
XPathCompiler xpc = proc.newXPathCompiler();
xpc.declareNamespace("ex", "top:marchand:xml:extfunctions");
QName var = new QName("connect");
xpc.declareVariable(var);
XPathExecutable xpe = xpc.compile("ex:basex-query('for $i in 1 to 10 return <test>{$i}</test>',$connect)");
assertNotNull("unable to compile extension function", xpe);
}
示例15: doTest
import net.sf.saxon.s9api.Processor; //导入依赖的package包/类
@Test
public void doTest() throws SaxonApiException, FileNotFoundException, IOException {
Configuration config = Configuration.newConfiguration();
Processor processor = new Processor(config);
XsltTransformer transformer = processor.newXsltCompiler().compile(new StreamSource("src/test/resources/identity.xsl")).load();
Serializer serializer = processor.newSerializer(new File("target/generated-test-files/output.xml"));
FileAppenderStep fas = new FileAppenderStep();
fas.setParameter(FileAppenderStep.FILE_NAME, new XdmAtomicValue("target/generated-test-files/appendee.txt"));
fas.setParameter(FileAppenderStep.VALUE, new XdmAtomicValue("blablabla"));
fas.setParameter(FileAppenderStep.LINE_SEPARATOR, new XdmAtomicValue("LF"));
fas.setDestination(serializer);
transformer.setDestination(fas);
transformer.setSource(new StreamSource("src/test/resources/source.xml"));
File expect = new File("target/generated-test-files/appendee.txt");
if(expect.exists()) expect.delete();
transformer.transform();
assertTrue(expect.isFile());
BufferedReader br = new BufferedReader(new FileReader(expect));
char[] buff = new char[30];
int ret = br.read(buff);
br.close();
assertEquals(10, ret);
char[] ex = new char[] { 'b', 'l', 'a', 'b', 'l', 'a', 'b', 'l', 'a', '\n'};
assertArrayEquals(ex, Arrays.copyOf(buff, ret));
fas.setDestination(processor.newSerializer(new File("target/generated-test-files/output2.xml")));
transformer.transform();
br = new BufferedReader(new FileReader(expect));
ret = br.read(buff);
br.close();
assertEquals(20, ret);
}