本文整理汇总了Java中javax.xml.transform.Transformer.setURIResolver方法的典型用法代码示例。如果您正苦于以下问题:Java Transformer.setURIResolver方法的具体用法?Java Transformer.setURIResolver怎么用?Java Transformer.setURIResolver使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.Transformer
的用法示例。
在下文中一共展示了Transformer.setURIResolver方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doTransform
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Performs the XSLT transformation.
*/
private String doTransform(final Transformer transformer, final Source input, final URIResolver resolver)
{
final StringWriter writer = new StringWriter();
final StreamResult output = new StreamResult(writer);
if( resolver != null )
{
transformer.setURIResolver(resolver);
}
try
{
transformer.transform(input, output);
}
catch( final TransformerException ex )
{
throw new RuntimeException("Error transforming XSLT", ex);
}
return writer.toString();
}
示例2: createTransformer
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
Transformer createTransformer() throws Exception {
// Prepare sources for transormation
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf;
// newTransformer() method doc states that different transformer
// factories can be used concurrently by different Threads.
synchronized (TransformerFactory.class) {
tf = TransformerFactory.newInstance();
}
Transformer t = tf.newTransformer(xslsrc);
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver((String href, String base) -> new StreamSource(new StringReader(xml)));
return t;
}
示例3: docResolver01
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* This is to test the URIResolver.resolve() method when there is an error
* in the file.
*
* @throws Exception If any errors occur.
*/
@Test
public static void docResolver01() throws Exception {
try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) {
URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID);
StreamSource streamSource = new StreamSource(fis);
streamSource.setSystemId(SYSTEM_ID);
Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);
transformer.setURIResolver(resolver);
File f = new File(XML_DIR + "myFake.xml");
Document document = DocumentBuilderFactory.newInstance().
newDocumentBuilder().parse(f);
// Use a Transformer for output
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.err);
// No exception is expected because resolver resolve wrong URI.
transformer.transform(source, result);
}
}
示例4: newTransformer
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* javax.xml.transform.sax.TransformerFactory implementation.
* Process the Source into a Templates object, which is a a compiled
* representation of the source. Note that this method should not be
* used with XSLTC, as the time-consuming compilation is done for each
* and every transformation.
*
* @return A Templates object that can be used to create Transformers.
* @throws TransformerConfigurationException
*/
@Override
public Transformer newTransformer(Source source) throws
TransformerConfigurationException
{
final Templates templates = newTemplates(source);
final Transformer transformer = templates.newTransformer();
if (_uriResolver != null) {
transformer.setURIResolver(_uriResolver);
}
return(transformer);
}
示例5: newTransformerHandler
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* javax.xml.transform.sax.SAXTransformerFactory implementation.
* Get a TransformerHandler object that can process SAX ContentHandler
* events into a Result. This method will return a pure copy transformer.
*
* @return A TransformerHandler object that can handle SAX events
* @throws TransformerConfigurationException
*/
@Override
public TransformerHandler newTransformerHandler()
throws TransformerConfigurationException
{
final Transformer transformer = newTransformer();
if (_uriResolver != null) {
transformer.setURIResolver(_uriResolver);
}
return new TransformerHandlerImpl((TransformerImpl) transformer);
}
示例6: createTransformer
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
Transformer createTransformer() throws Exception {
// Prepare sources for transormation
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer(xslsrc);
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver((String href, String base) -> new StreamSource(new StringReader(xml)));
return t;
}
示例7: renderXML
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Renders an XML file into a PDF file by applying a stylesheet
* that converts the XML to XSL-FO. The PDF is written to a byte array
* that is returned as the method's result.
* @param xml the XML file
* @param xslt the XSLT file
* @param response HTTP response object
* @throws FOPException If an error occurs during the rendering of the
* XSL-FO
* @throws TransformerException If an error occurs during XSL
* transformation
* @throws IOException In case of an I/O problem
*/
public void renderXML(String xml, String xsl, HttpServletResponse response, ServletContext context)
throws FOPException, TransformerException, IOException {
URIResolver uriResolver = new ServletContextURIResolver(context);
//Setup sources
Source xmlSrc = new StreamSource(new StringReader(xml));
String realpath = context.getRealPath(xsl);
Source xsltSrc = new StreamSource(new File(realpath));
//Le damos el path real de la aplicacion para que encuentre los ficheros
realpath = context.getRealPath(".");
//this.transFactory.setURIResolver(uriResolver);
Transformer transformer = this.transFactory.newTransformer(xsltSrc);
transformer.setURIResolver(uriResolver);
//Start transformation and rendering process
render(xmlSrc, transformer, response, realpath);
}
示例8: testDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* bug 8062518
* Verifies that a reference to the DTM created by XSLT document function is
* actually read from the DTM by an extension function.
* @param xml Content of xml file to process
* @param xsl stylesheet content that loads external document {@code externalDoc}
* with XSLT 'document' function and then reads it with
* DocumentExtFunc.test() function
* @param externalDoc Content of the external xml document
* @param expectedResult Expected transformation result
**/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
final String externalDoc, final String expectedResult) throws Exception {
// Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer( xslsrc );
t.setErrorListener(tf.getErrorListener());
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (href.contains("externalDoc")) {
return new StreamSource(new StringReader(externalDoc));
} else {
return new StreamSource(new StringReader(xml));
}
}
});
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform the xml
t.transform(src, xmlResultStream);
// If the document can't be accessed and the bug is in place then
// reported exception will be thrown during transformation
System.out.println("Transformation result:"+xmlResultString.toString().trim());
// Check the result - it should contain two (node name, node values) entries -
// one for original document, another for a document created with
// call to 'document' function
assertEquals(xmlResultString.toString().trim(), expectedResult);
}
示例9: testDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* @bug 8062518 8153082
* Verifies that a reference to the DTM created by XSLT document function is
* actually read from the DTM by an extension function.
* @param xml Content of xml file to process
* @param xsl stylesheet content that loads external document {@code externalDoc}
* with XSLT 'document' function and then reads it with
* DocumentExtFunc.test() function
* @param externalDoc Content of the external xml document
* @param expectedResult Expected transformation result
**/
@Test(dataProvider = "document")
public void testDocument(final String xml, final String xsl,
final String externalDoc, final String expectedResult) throws Exception {
// Prepare sources for transormation
Source src = new StreamSource(new StringReader(xml));
Source xslsrc = new StreamSource(new StringReader(xsl));
// Create factory and transformer
TransformerFactory tf = TransformerFactory.newInstance();
tf.setFeature(ORACLE_ENABLE_EXTENSION_FUNCTION, true);
tf.setAttribute(EXTENSION_CLASS_LOADER,
runWithAllPerm(() -> Thread.currentThread().getContextClassLoader()));
Transformer t = tf.newTransformer( xslsrc );
t.setErrorListener(tf.getErrorListener());
// Set URI Resolver to return the newly constructed xml
// stream source object from xml test string
t.setURIResolver(new URIResolver() {
@Override
public Source resolve(String href, String base)
throws TransformerException {
if (href.contains("externalDoc")) {
return new StreamSource(new StringReader(externalDoc));
} else {
return new StreamSource(new StringReader(xml));
}
}
});
// Prepare output stream
StringWriter xmlResultString = new StringWriter();
StreamResult xmlResultStream = new StreamResult(xmlResultString);
//Transform the xml
t.transform(src, xmlResultStream);
// If the document can't be accessed and the bug is in place then
// reported exception will be thrown during transformation
System.out.println("Transformation result:"+xmlResultString.toString().trim());
// Check the result - it should contain two (node name, node values) entries -
// one for original document, another for a document created with
// call to 'document' function
assertEquals(xmlResultString.toString().trim(), expectedResult);
}