javax.xml.transform.Transformer#setURIResolver ( )源码实例Demo

下面列出了javax.xml.transform.Transformer#setURIResolver ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: URIResolverTest.java
/**
 * 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);
    }
}
 
源代码2 项目: scipio-erp   文件: ApacheFopWorker.java
/** 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);
    }
}
 
源代码3 项目: hottub   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码4 项目: org.hl7.fhir.core   文件: XsltUtilities.java
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();
}
 
源代码5 项目: JDKSourceCode1.8   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码6 项目: openjdk-8   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码7 项目: openjdk-8-source   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码8 项目: ph-schematron   文件: SCHTransformerCustomizer.java
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);
  }
}
 
/**
 * 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);
}
 
源代码10 项目: openjdk-8   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码11 项目: hottub   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码12 项目: openjdk-jdk8u   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码13 项目: JDKSourceCode1.8   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码14 项目: jdk8u60   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码15 项目: hottub   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码16 项目: jdk8u60   文件: TransformerFactoryImpl.java
/**
 * 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);
}
 
源代码17 项目: dragonwell8_jdk   文件: XSLTFunctionsTest.java
/**
 * 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);
}
 
源代码18 项目: hottub   文件: XSLTFunctionsTest.java
/**
 * 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);
}
 
源代码19 项目: openjdk-jdk9   文件: XSLTFunctionsTest.java
/**
 * @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);
}
 
源代码20 项目: java-technology-stack   文件: XsltView.java
/**
 * 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;
}