public void genericTransform(XdmNode input, Map<String, Object> parameters, Destination dest) throws SaxonApiException { if (parameters != null) { for (Map.Entry<String, Object> param : parameters.entrySet()) { this.transformer.setParameter(new QName(null, param.getKey()), new XdmAtomicValue(param.getValue().toString())); } } this.transformer.setSource(input.asSource()); this.transformer.setDestination(dest); this.transformer.transform(); if (parameters != null) { //cancel the parameters for (Map.Entry<String, Object> param : parameters.entrySet()) { this.transformer.setParameter(new QName(null, param.getKey()), null); } } }
private void generateAggregatedHtmlReport(File xmlFile, File outputDir) throws SaxonApiException { Processor proc = new Processor(false); XsltCompiler comp = proc.newXsltCompiler(); InputStream xsl = getClass().getClassLoader().getResourceAsStream("spotbugs/html-aggregated.xsl"); XsltExecutable exp = comp.compile(new StreamSource(xsl)); XdmNode source = proc.newDocumentBuilder().build(new StreamSource(xmlFile)); Serializer out = proc.newSerializer(new File(outputDir, "SpotBugsAggregated.html")); out.setOutputProperty(Serializer.Property.METHOD, "html"); out.setOutputProperty(Serializer.Property.INDENT, "yes"); XsltTransformer trans = exp.load(); trans.setInitialContextNode(source); trans.setDestination(out); trans.transform(); }
private void writeSummaryReport(Set<String> testNames, File reportDir) { try { XsltTransformer formatter = xspecHtmlSummaryFormatterLoader.load(); formatter.setInitialTemplate(new QName("main")); formatter.setParameter(new QName("test-names"), new XdmValue( Collections2.<String,XdmItem>transform( testNames, new Function<String,XdmItem>() { public XdmItem apply(String s) { return new XdmAtomicValue(s); }}))); formatter.setParameter(new QName("report-dir"), new XdmAtomicValue(reportDir.toURI())); formatter.setDestination( serializeToFile(new File(reportDir, "index.html"))); formatter.setMessageListener(SaxonSinkReporter.INSTANCE); formatter.transform(); } catch (SaxonApiException e) { throw new RuntimeException(e); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }
/** * Check conformance to TAXII specification beyond what XML Schema provides. */ private void checkConformance(Object m, ValidationErrorHandler errorHandler) { final XsltTransformer transformer = schematronValidator.load(); transformer.setMessageListener(errorHandler); try { transformer.setSource(new JAXBSource(jaxbContext, m)); transformer.setDestination(new SAXDestination(new DefaultHandler())); transformer.transform(); } catch (SaxonApiException | JAXBException e) { errorHandler.getResults().addError("Conformance error: " + e.getMessage()); } }
xsltTransformer.setInitialContextNode(xdmNode); if (params != null) { for (Map.Entry<String, Object> entry : params.entrySet()) { QName paramName = new QName(entry.getKey()); XdmAtomicValue paramValue = new XdmAtomicValue(String.valueOf(entry.getValue())); xsltTransformer.setParameter(paramName, paramValue); xsltTransformer.setDestination(out); xsltTransformer.transform();
XsltExecutable exec = compiler.compile(xslt); XsltTransformer transformer = exec.load(); transformer.setSource(new DOMSource(sourceDoc)); transformer.setDestination(new DOMDestination(resultDoc)); transformer.transform(); } catch (SaxonApiException e) { throw new RuntimeException(e);
RuntimeValue v = params.get(name); if (runtime.getAllowGeneralExpressions()) { transformer.setParameter(name, v.getValue()); } else { transformer.setParameter(name, v.getUntypedAtomic(runtime)); transformer.setInitialContextNode(document); transformer.setMessageListener(new CatchMessages()); result = new XdmDestination(); transformer.setDestination(result); transformer.setInitialMode(initialMode); transformer.setInitialTemplate(templateName); transformer.setBaseOutputURI(outputBaseURI); transformer.setSchemaValidationMode(ValidationMode.DEFAULT); transformer.getUnderlyingController().setUnparsedTextURIResolver(unparsedTextURIResolver); transformer.transform(); } finally { config.setOutputURIResolver(uriResolver);
xspecCompiler.setSource(testAsSource); xspecCompiler.setDestination(xspecTestCompiled); xspecCompiler.setErrorListener(saxonReporter); xspecCompiler.setMessageListener(saxonReporter); xspecCompiler.setURIResolver(new XSpecResolver(xspecCompiler .getURIResolver())); xspecCompiler.transform(); XsltTransformer xspecTestRunner = xspecTestCompiler.compile( compiledTestAsSource).load(); xspecTestRunner.setInitialTemplate(XSPEC_MAIN_TEMPLATE); xspecTestRunner.setDestination(xspecTestResult); xspecTestRunner.setErrorListener(saxonReporter); xspecTestRunner.setMessageListener(saxonReporter); xspecTestRunner.setURIResolver(new XSpecResolver(testResolver)); xspecTestRunner.transform(); XsltTransformer htmlFormatter = xspecHtmlFormatterLoader.load(); htmlFormatter .setSource(xspecTestResult.getXdmNode().asSource()); htmlFormatter.setParameter(XSPEC_CSS_URI_PARAM, XSPEC_CSS_URI); htmlFormatter.setDestination(serializeToFile(htmlReport)); htmlFormatter.setMessageListener(SaxonSinkReporter.INSTANCE); htmlFormatter.transform(); XsltTransformer junitFormatter = xspecJUnitFormatterLoader .load(); junitFormatter.setSource(xspecTestResult.getXdmNode() .asSource());
public void transform(Source xmlSource, final Result outputTarget) throws XPathException { try { xsltTransformer.setSource(xmlSource); if (outputTarget.getSystemId() != null) { //bug 2214 xsltTransformer.setBaseOutputURI(outputTarget.getSystemId()); return; xsltTransformer.setDestination(destination); xsltTransformer.transform(); if (destination instanceof Serializer && ((Serializer)destination).isMustCloseAfterUse()) { destination.close();
transformer.setSource(node); transformer.setErrorListener(eval.getErrorListener()); if (arguments.length > 2) { bindParameters(arguments[2]); transformer.setDestination(dest); transformer.transform(); ArrayList<TransformerException> runtimeErrors = ((TransformErrorListener)transformer.getErrorListener()).getErrors(); if (!runtimeErrors.isEmpty()) { throw new XPathException(runtimeErrors.get(0).getMessage(), runtimeErrors.get(0).getLocator(), runtimeErrors.get(0));
trans.setParameter(new QName("",entry.getKey()), xdmValue); trans.setSource(content); trans.transform();
@Override protected void setConvertedParameter(QName name, XdmValue value) { xsltTransformer.setParameter(name, value); }
/** * 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} * @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) 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); //set the template setTemplate(templateInputStream, processor, xmlReader, xsltTransformer); xsltTransformer.transform(); return output.toByteArray(); }
/** Retrieves the output for a given xslt transformer. * @param xsltTransformer the {@link XsltTransformer} that will receive the output * @return the output as a {@link ByteArrayOutputStream} */ public ByteArrayOutputStream getOutput(final XsltTransformer xsltTransformer) { Serializer serializer = serializerFactory.createSerializer(); serializer.setOutputProperty(Serializer.Property.METHOD, "xml"); serializer.setOutputProperty(Serializer.Property.INDENT, "no"); ByteArrayOutputStream output = byteArrayOutputStreamFactory.createByteArrayOutputStream(); serializer.setOutputStream(output); xsltTransformer.setDestination(serializer); return output; }
/** * Set the source document for the transformation. This method is equivalent to building * a document from the supplied source object, and then supplying the document node of * the resulting document as the initial context node. * @param source the principal source document for the transformation */ public void setSource(Source source) throws SaxonApiException { setInitialContextNode(processor.newDocumentBuilder().build(source)); }
for (Map.Entry<String, Object> entry : new Configs().getEntrySet(variables)) { XdmValue xdmValue = XdmNode.wrap(new UntypedAtomicValue(entry.getValue().toString())); evaluator.setParameter(new QName(entry.getKey()), xdmValue); File file = new File(entry.getValue().toString()); XdmValue doc = parseXmlDocument(file); evaluator.setParameter(new QName(entry.getKey()), doc); evaluator.setTraceListener(new XQueryTraceListener()); // TODO redirect from stderr to SLF4J
/** * Load the stylesheet to prepare it for execution. This version of the load() method * creates an <code>XsltTransformer</code> which offers interfaces for stylesheet * invocation corresponding to those described in the XSLT 2.0 specification. It can be used * with XSLT 2.0 or XSLT 3.0 stylesheets, but does not offer new XSLT 3.0 functionality such * as the ability to supply parameters to the initial template, or the ability to invoke * stylesheet-defined functions, or the ability to return an arbitrary sequence as a result * without wrapping it in a document node. If such facilities are required, use the * method {@link #load30()} in preference. * * @return An XsltTransformer. The returned XsltTransformer can be used to set up the * dynamic context for stylesheet evaluation, and to run the stylesheet. */ public XsltTransformer load() { XsltTransformer xt = new XsltTransformer( processor, preparedStylesheet.newController(), preparedStylesheet.getCompileTimeParams()); StructuredQName initialTemplate = preparedStylesheet.getDefaultInitialTemplateName(); if (initialTemplate != null) { xt.setInitialTemplate(new QName(initialTemplate)); } return xt; }
/** * Supply an initial template for a transformation. * <p>This is a Saxon extension to the JAXP interface, needed for XSLT 2.0</p> * <p>Changed in 9.9 so it no longer validates the supplied name; an incorrect name will * lead to an error later.</p> * * @param name the name of the initial template, in Clark notation (either a local name, * or "{uri}local") */ public void setInitialTemplate(String name) { xsltTransformer.setInitialTemplate(QName.fromClarkName(name)); }
xsltTransformer.setInitialContextNode(xdmNode); if (params != null) { for (Map.Entry<String, Object> entry : params.entrySet()) { QName paramName = new QName(entry.getKey()); XdmAtomicValue paramValue = new XdmAtomicValue(String.valueOf(entry.getValue())); xsltTransformer.setParameter(paramName, paramValue); xsltTransformer.setDestination(out); xsltTransformer.transform();
XsltExecutable exec = compiler.compile(xslt); XsltTransformer transformer = exec.load(); transformer.setSource(new DOMSource(sourceDoc)); transformer.setDestination(new DOMDestination(resultDoc)); transformer.transform(); } catch (SaxonApiException e) { throw new RuntimeException(e);