Search in sources :

Example 36 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class XSLTExFuncTest method testTemplatesEnableExtFunc.

/**
     * use Templates template = factory.newTemplates(new StreamSource( new
     * FileInputStream(xslFilename))); // Use the template to create a
     * transformer Transformer xformer = template.newTransformer();
     *
     * @param factory
     * @return
     */
/**
     * Security is enabled, use new feature: enableExtensionFunctions Use the
     * template to create a transformer
     */
public void testTemplatesEnableExtFunc() {
    Policy p = new SimplePolicy(new AllPermission());
    Policy.setPolicy(p);
    System.setSecurityManager(new SecurityManager());
    TransformerFactory factory = TransformerFactory.newInstance();
    /**
         * Use of the extension function 'http://exslt.org/strings:tokenize' is
         * not allowed when the secure processing feature is set to true.
         * Attempt to use the new property to enable extension function
         */
    boolean isExtensionSupported = enableExtensionFunction(factory);
    try {
        SAXSource xslSource = new SAXSource(new InputSource(xslFile));
        xslSource.setSystemId(xslFileId);
        Templates template = factory.newTemplates(xslSource);
        Transformer transformer = template.newTransformer();
        StringWriter stringResult = new StringWriter();
        Result result = new StreamResult(stringResult);
        transformer.transform(new SAXSource(new InputSource(xmlFile)), result);
        System.out.println("testTemplatesEnableExtFunc: OK");
    } catch (TransformerConfigurationException e) {
        fail(e.getMessage());
    } catch (TransformerException e) {
        fail(e.getMessage());
    } finally {
        System.setSecurityManager(null);
    }
}
Also used : Policy(java.security.Policy) InputSource(org.xml.sax.InputSource) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) SAXSource(javax.xml.transform.sax.SAXSource) StringWriter(java.io.StringWriter) AllPermission(java.security.AllPermission)

Example 37 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class XSLT method main.

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream resStream = new ByteArrayOutputStream();
    TransformerFactory trf = TransformerFactory.newInstance();
    Transformer tr = trf.newTransformer(new StreamSource(System.getProperty("test.src", ".") + "/" + args[1]));
    String res, expectedRes;
    tr.transform(new StreamSource(System.getProperty("test.src", ".") + "/" + args[0]), new StreamResult(resStream));
    res = resStream.toString();
    System.out.println("Transformation completed. Result:" + res);
    if (!res.replaceAll("\\s", "").equals(args[2]))
        throw new RuntimeException("Incorrect transformation result. Expected:" + args[2] + " Observed:" + res);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 38 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class TransformerTest method testBug8169112.

/**
     * @bug 8169112
     * @summary Test compilation of large xsl file with outlining.
     *
     * This test merely compiles a large xsl file and tests if its bytecode
     * passes verification by invoking the transform() method for
     * dummy content. The test succeeds if no Exception is thrown
     */
@Test
public final void testBug8169112() throws FileNotFoundException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    String xslFile = getClass().getResource("Bug8169112.xsl").toString();
    Transformer t = tf.newTransformer(new StreamSource(xslFile));
    String xmlIn = "<?xml version=\"1.0\"?><DOCROOT/>";
    ByteArrayInputStream bis = new ByteArrayInputStream(xmlIn.getBytes());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    t.transform(new StreamSource(bis), new StreamResult(bos));
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 39 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class TransformerTest method transformResourceToStringWriter.

private StringWriter transformResourceToStringWriter(Transformer transformer, String xmlResource) throws TransformerException {
    StringWriter sw = new StringWriter();
    transformer.transform(new StreamSource(getClass().getResource(xmlResource).toString()), new StreamResult(sw));
    return sw;
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource)

Example 40 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jdk8u_jdk by JetBrains.

the class JAXPSAXParserTest method testTransform.

public final void testTransform() {
    String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<r>\n" + "    <e/>\n" + "</r>\n";
    // #5064280 workaround
    String IDENTITY_XSLT_WITH_INDENT = "<xsl:stylesheet version='1.0' " + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + "xmlns:xalan='http://xml.apache.org/xslt' " + "exclude-result-prefixes='xalan'>" + "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" + "<xsl:template match='@*|node()'>" + "<xsl:copy>" + "<xsl:apply-templates select='@*|node()'/>" + "</xsl:copy>" + "</xsl:template>" + "</xsl:stylesheet>";
    try {
        //Skip the default XMLReader
        System.setProperty("org.xml.sax.driver", "com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser");
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
        Result result = new StreamResult(sw);
        t.transform(new StreamSource(new StringReader(data)), result);
        success("JAXPSAXParserTest passed");
    } catch (Exception e) {
        /**
             * JAXPSAXParser throws NullPointerException since the jaxp 1.5 security
             * manager is not initialized when JAXPSAXParser is instantiated using
             * the default constructor.
            */
        fail(e.toString());
    } finally {
        System.clearProperty("org.xml.sax.driver");
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)1328 DOMSource (javax.xml.transform.dom.DOMSource)881 Transformer (javax.xml.transform.Transformer)775 StringWriter (java.io.StringWriter)447 TransformerFactory (javax.xml.transform.TransformerFactory)445 Document (org.w3c.dom.Document)415 TransformerException (javax.xml.transform.TransformerException)366 ByteArrayOutputStream (java.io.ByteArrayOutputStream)337 DocumentBuilder (javax.xml.parsers.DocumentBuilder)332 ByteArrayInputStream (java.io.ByteArrayInputStream)288 IOException (java.io.IOException)266 InputStream (java.io.InputStream)249 Test (org.junit.Test)246 StreamSource (javax.xml.transform.stream.StreamSource)213 XMLStreamReader (javax.xml.stream.XMLStreamReader)185 Source (javax.xml.transform.Source)183 File (java.io.File)175 Element (org.w3c.dom.Element)157 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)149 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)149