Java Code Examples for javax.xml.transform.Templates#newTransformer()

The following examples show how to use javax.xml.transform.Templates#newTransformer() . 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: XslSubstringTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private String testTransform(String xsl) throws Exception {
    //Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    //Create factory, template and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Templates tmpl = tf.newTemplates(xslsrc);
    Transformer t = tmpl.newTransformer();
    //Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform
    t.transform(src, xmlResultStream);
    return xmlResultString.toString().trim();
}
 
Example 2
Source File: XPathNegativeZero.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
Example 3
Source File: XslSubstringTest.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private String testTransform(String xsl) throws Exception {
    //Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    //Create factory, template and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Templates tmpl = tf.newTemplates(xslsrc);
    Transformer t = tmpl.newTransformer();
    //Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform
    t.transform(src, xmlResultStream);
    return xmlResultString.toString().trim();
}
 
Example 4
Source File: TrAXFilter.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public TrAXFilter(Templates templates)  throws
    TransformerConfigurationException
{
    _templates = templates;
    _transformer = (TransformerImpl) templates.newTransformer();
    _transformerHandler = new TransformerHandlerImpl(_transformer);
    _overrideDefaultParser = _transformer.overrideDefaultParser();
}
 
Example 5
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 6
Source File: XPathNegativeZero.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
Example 7
Source File: XPathNegativeZero.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
Example 8
Source File: XPathNegativeZero.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();

    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);

    t.transform(src, res);
    return writer.toString();
}
 
Example 9
Source File: XslSubstringTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private String testTransform(String xsl) throws Exception {
    //Prepare sources for transormation
    Source src = new StreamSource(new StringReader(xml));
    Source xslsrc = new StreamSource(new StringReader(xslPre + xsl + xslPost));
    //Create factory, template and transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Templates tmpl = tf.newTemplates(xslsrc);
    Transformer t = tmpl.newTransformer();
    //Prepare output stream
    StringWriter xmlResultString = new StringWriter();
    StreamResult xmlResultStream = new StreamResult(xmlResultString);
    //Transform
    t.transform(src, xmlResultStream);
    return xmlResultString.toString().trim();
}
 
Example 10
Source File: TransformerFactoryImpl.java    From openjdk-jdk8u 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 11
Source File: TransformerFactoryImpl.java    From JDKSourceCode1.8 with MIT License 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 12
Source File: TransformerFactoryImpl.java    From jdk1.8-source-analysis with Apache License 2.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 13
Source File: SortTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "parameters")
public final void testTransform(String lang, String xml, String xsl, String gold)
        throws Exception {

    StringWriter sw = new StringWriter();
    // Create transformer factory
    TransformerFactory factory = TransformerFactory.newInstance();

    // Use the factory to create a template containing the xsl file
    Templates template = factory.newTemplates(new StreamSource(getClass().getResourceAsStream(xsl)));
    // Use the template to create a transformer
    Transformer xformer = template.newTransformer();
    xformer.setParameter("lang", lang);
    // Prepare the input and output files
    Source source = new StreamSource(getClass().getResourceAsStream(xml));

    /*
         * The following may be used to produce gold files.
         * Using however the original gold files, and compare without considering
         * the format
     */
    //String output = getClass().getResource(gold).getPath();
    //Result result = new StreamResult(new FileOutputStream(output));
    // use the following to verify the output against the pre-generated one
    Result result = new StreamResult(sw);

    // Apply the xsl file to the source file and write the result to the
    // output file
    xformer.transform(source, result);

    String out = sw.toString();

    List<String> lines = Files.readAllLines(Paths.get(filepath + gold));
    String[] resultLines = out.split("\n");
    int i = 0;

    // the purpose is to test sort, so ignore the format of the output

    for (String line : lines) {
        Assert.assertEquals(resultLines[i++].trim(), line.trim());
    }
}
 
Example 14
Source File: SchematronValidator.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private SchematronValidator(Templates templates) throws TransformerException {
   this.transformer = templates.newTransformer();
   this.transformer.setOutputProperty("encoding", "UTF-8");
   this.transformer.setOutputProperty("indent", "yes");
}
 
Example 15
Source File: SchematronValidator.java    From freehealth-connector with GNU Affero General Public License v3.0 4 votes vote down vote up
private SchematronValidator(Templates templates) throws TransformerException {
   this.transformer = templates.newTransformer();
   this.transformer.setOutputProperty("encoding", "UTF-8");
   this.transformer.setOutputProperty("indent", "yes");
}
 
Example 16
Source File: TemplatesTest.java    From TencentKona-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * bug 8079323 Test Templates serialization
 * <p>
 * Serialization compatibility test: verify that serializing the Templates
 * that contain auxiliary classes will result in a NotSerializableException
 * due to the use of Xalan's non-serializable Hashtable.
 *
 * @param templates an instance of Templates
 * @throws Exception as expected.
 */
@Test(dataProvider = "templates", expectedExceptions = NotSerializableException.class)
public void testSerialization(Templates templates) throws Exception {
    Transformer xformer = templates.newTransformer();
    try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);) {
        out.writeObject(templates);
        out.flush();
    }
}
 
Example 17
Source File: TemplatesTest.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * bug 8079323 Test Templates serialization
 * <p>
 * Serialization compatibility test: verify that serializing the Templates
 * that contain auxiliary classes will result in a NotSerializableException
 * due to the use of Xalan's non-serializable Hashtable.
 *
 * @param templates an instance of Templates
 * @throws Exception as expected.
 */
@Test(dataProvider = "templates", expectedExceptions = NotSerializableException.class)
public void testSerialization(Templates templates) throws Exception {
    Transformer xformer = templates.newTransformer();
    try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);) {
        out.writeObject(templates);
        out.flush();
    }
}
 
Example 18
Source File: TemplatesTest.java    From dragonwell8_jdk with GNU General Public License v2.0 3 votes vote down vote up
/**
 * bug 8079323 Test Templates serialization
 * <p>
 * Serialization compatibility test: verify that serializing the Templates
 * that contain auxiliary classes will result in a NotSerializableException
 * due to the use of Xalan's non-serializable Hashtable.
 *
 * @param templates an instance of Templates
 * @throws Exception as expected.
 */
@Test(dataProvider = "templates", expectedExceptions = NotSerializableException.class)
public void testSerialization(Templates templates) throws Exception {
    Transformer xformer = templates.newTransformer();
    try (ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);) {
        out.writeObject(templates);
        out.flush();
    }
}
 
Example 19
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;
}
 
Example 20
Source File: TransformerFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 3 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 templates Represents a pre-processed stylesheet
 * @return A TransformerHandler object that can handle SAX events
 * @throws TransformerConfigurationException
 */
@Override
public TransformerHandler newTransformerHandler(Templates templates)
    throws TransformerConfigurationException
{
    final Transformer transformer = templates.newTransformer();
    final TransformerImpl internal = (TransformerImpl)transformer;
    return new TransformerHandlerImpl(internal);
}