Java Code Examples for javax.xml.transform.Transformer#setURIResolver()

The following examples show how to use javax.xml.transform.Transformer#setURIResolver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: URIResolverTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: ApacheFopWorker.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/** Transform an xsl-fo StreamSource to the specified output format.
 * @param src The xsl-fo StreamSource instance
 * @param stylesheet Optional stylesheet StreamSource instance
 * @param fop
 */
public static void transform(StreamSource src, StreamSource stylesheet, Fop fop) throws FOPException {
    Result res = new SAXResult(fop.getDefaultHandler());
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        if (stylesheet == null) {
            transformer = factory.newTransformer();
        } else {
            transformer = factory.newTransformer(stylesheet);
        }
        transformer.setURIResolver(new LocalResolver(transformer.getURIResolver()));
        transformer.transform(src, res);
    } catch (Exception e) {
        throw new FOPException(e);
    }
}
 
Example 3
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TransformerHandler object that can process SAX ContentHandler
 * events into a Result, based on the transformation instructions
 * specified by the argument.
 *
 * @param src The source of the transformation instructions.
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException
{
    final Transformer transformer = newTransformer(src);
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return new TransformerHandlerImpl((TransformerImpl) transformer);
}
 
Example 4
Source File: TransformerFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: TransformerFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 7
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * 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);
}
 
Example 8
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 9
Source File: TransformerFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TransformerHandler object that can process SAX ContentHandler
 * events into a Result, based on the transformation instructions
 * specified by the argument.
 *
 * @param src The source of the transformation instructions.
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException
{
    final Transformer transformer = newTransformer(src);
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return new TransformerHandlerImpl((TransformerImpl) transformer);
}
 
Example 10
Source File: TransformerFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TransformerHandler object that can process SAX ContentHandler
 * events into a Result, based on the transformation instructions
 * specified by the argument.
 *
 * @param src The source of the transformation instructions.
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException
{
    final Transformer transformer = newTransformer(src);
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return new TransformerHandlerImpl((TransformerImpl) transformer);
}
 
Example 11
Source File: TransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TransformerHandler object that can process SAX ContentHandler
 * events into a Result, based on the transformation instructions
 * specified by the argument.
 *
 * @param src The source of the transformation instructions.
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException
{
    final Transformer transformer = newTransformer(src);
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return new TransformerHandlerImpl((TransformerImpl) transformer);
}
 
Example 12
Source File: SCHTransformerCustomizer.java    From ph-schematron with Apache License 2.0 5 votes vote down vote up
public void customize (@Nonnull final EStep eStep, @Nonnull final Transformer aTransformer)
{
  // Ensure an error listener is present
  if (m_aCustomErrorListener != null)
    aTransformer.setErrorListener (m_aCustomErrorListener);
  else
    aTransformer.setErrorListener (new LoggingTransformErrorListener (Locale.US));

  // Set the optional URI Resolver
  if (m_aCustomURIResolver != null)
    aTransformer.setURIResolver (m_aCustomURIResolver);

  // Set all custom parameters
  if (m_aCustomParameters != null)
    for (final Map.Entry <String, ?> aEntry : m_aCustomParameters.entrySet ())
      aTransformer.setParameter (aEntry.getKey (), aEntry.getValue ());

  if (eStep == EStep.SCH2XSLT_3)
  {
    // On the last step, set the respective Schematron parameters as the
    // last action to avoid they are overwritten by a custom parameter.
    if (m_sPhase != null)
      aTransformer.setParameter ("phase", m_sPhase);

    if (m_sLanguageCode != null)
      aTransformer.setParameter ("langCode", m_sLanguageCode);
  }
}
 
Example 13
Source File: TransformerFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: TransformerFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 15
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * javax.xml.transform.sax.SAXTransformerFactory implementation.
 * Get a TransformerHandler object that can process SAX ContentHandler
 * events into a Result, based on the transformation instructions
 * specified by the argument.
 *
 * @param src The source of the transformation instructions.
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Source src)
    throws TransformerConfigurationException
{
    final Transformer transformer = newTransformer(src);
    if (_uriResolver != null) {
        transformer.setURIResolver(_uriResolver);
    }
    return new TransformerHandlerImpl((TransformerImpl) transformer);
}
 
Example 16
Source File: XsltUtilities.java    From org.hl7.fhir.core with Apache License 2.0 5 votes vote down vote up
public static byte[] saxonTransform(Map<String, byte[]> files, byte[] source, byte[] xslt) throws TransformerException {
  TransformerFactory f = new net.sf.saxon.TransformerFactoryImpl();
  f.setAttribute("http://saxon.sf.net/feature/version-warning", Boolean.FALSE);
  StreamSource xsrc = new StreamSource(new ByteArrayInputStream(xslt));
  f.setURIResolver(new ZipURIResolver(files));
  Transformer t = f.newTransformer(xsrc);

  t.setURIResolver(new ZipURIResolver(files));
  StreamSource src = new StreamSource(new ByteArrayInputStream(source));
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  StreamResult res = new StreamResult(out);
  t.transform(src, res);
  return out.toByteArray();
}
 
Example 17
Source File: XSLTFunctionsTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: XSLTFunctionsTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: XSLTFunctionsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @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);
}
 
Example 20
Source File: XsltView.java    From java-technology-stack with MIT License 3 votes vote down vote up
/**
 * Create the {@link Transformer} instance used to prefer the XSLT transformation.
 * <p>The default implementation simply calls {@link Templates#newTransformer()}, and
 * configures the {@link Transformer} with the custom {@link URIResolver} if specified.
 * @param templates the XSLT Templates instance to create a Transformer for
 * @return the Transformer object
 * @throws TransformerConfigurationException in case of creation failure
 */
protected Transformer createTransformer(Templates templates) throws TransformerConfigurationException {
	Transformer transformer = templates.newTransformer();
	if (this.uriResolver != null) {
		transformer.setURIResolver(this.uriResolver);
	}
	return transformer;
}