Search in sources :

Example 81 with StreamSource

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

the class Test method main.

public static void main(String[] args) throws IOException, TransformerException {
    try {
        String address = deployWebservice();
        Service service = Service.create(new URL(address), ServiceImpl.SERVICE_NAME);
        Dispatch<Source> d = service.createDispatch(ServiceImpl.PORT_NAME, Source.class, Service.Mode.MESSAGE);
        Source response = d.invoke(new StreamSource(new StringReader(XML_REQUEST)));
        String resultXml = toString(response);
        log("= request ======== \n");
        log(XML_REQUEST);
        log("= result ========= \n");
        log(resultXml);
        log("\n==================");
        boolean xsAnyMixedPartSame = resultXml.contains(XS_ANY_MIXED_PART);
        log("resultXml.contains(XS_ANY_PART) = " + xsAnyMixedPartSame);
        if (!xsAnyMixedPartSame) {
            fail("The xs:any content=mixed part is supposed to be same in request and response.");
            throw new RuntimeException();
        }
        log("TEST PASSED");
    } finally {
        stopWebservice();
        // if you need to debug or explore wsdl generation result
        // comment this line out:
        deleteGeneratedFiles();
    }
}
Also used : StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Service(javax.xml.ws.Service) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 82 with StreamSource

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

the class BooleanAttributes method test.

public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
    BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
    ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
    iw.setOutput(ios);
    ImageWriteParam param = null;
    IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
    IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
    IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
    Source src = new StreamSource(new StringReader(metaXml));
    DOMResult dst = new DOMResult();
    transform(src, dst);
    Document doc = (Document) dst.getNode();
    Element node = doc.getDocumentElement();
    String metaFormat = node.getNodeName();
    // Verify that the default metadata gets formatted correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, false);
    meta.mergeTree(metaFormat, node);
    // Verify that the merged metadata gets formatte correctly.
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
    iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
    iw.dispose();
    ios.close();
    ImageReader ir = ImageIO.getImageReader(iw);
    byte[] bytes = os.toByteArray();
    if (bytes.length == 0)
        throw new AssertionError("Zero length image file");
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    ImageInputStream iis = new MemoryCacheImageInputStream(is);
    ir.setInput(iis);
    if (useStreamMeta)
        meta = ir.getStreamMetadata();
    else
        meta = ir.getImageMetadata(0);
    // Verify again after writing and re-reading the image
    verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamSource(javax.xml.transform.stream.StreamSource) Element(org.w3c.dom.Element) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) ImageWriter(javax.imageio.ImageWriter) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ImageWriteParam(javax.imageio.ImageWriteParam) Document(org.w3c.dom.Document) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) IIOImage(javax.imageio.IIOImage) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream) IIOMetadata(javax.imageio.metadata.IIOMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) ImageReader(javax.imageio.ImageReader) ImageOutputStream(javax.imageio.stream.ImageOutputStream) MemoryCacheImageOutputStream(javax.imageio.stream.MemoryCacheImageOutputStream)

Example 83 with StreamSource

use of javax.xml.transform.stream.StreamSource 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 84 with StreamSource

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

the class NamespacePrefixTest method testConcurrentTransformations.

@Test
public void testConcurrentTransformations() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Templates tmpl = tf.newTemplates(xslsrc);
    concurrentTestPassed.set(true);
    // Execute multiple TestWorker tasks
    for (int id = 0; id < THREADS_COUNT; id++) {
        EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));
    }
    // Initiate shutdown of previously submitted task
    EXECUTOR.shutdown();
    // Wait for termination of submitted tasks
    if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
        // If not all tasks terminates during the time out force them to shutdown
        EXECUTOR.shutdownNow();
    }
    // Check if all transformation threads generated the correct namespace prefix
    assertTrue(concurrentTestPassed.get());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Templates(javax.xml.transform.Templates) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.testng.annotations.Test)

Example 85 with StreamSource

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

the class NamespacePrefixTest method testReuseTransformer.

@Test
public void testReuseTransformer() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Transformer t = tf.newTransformer(xslsrc);
    for (int i = 0; i < TRANSF_COUNT; i++) {
        checkResult(doTransformation(t));
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.testng.annotations.Test)

Aggregations

StreamSource (javax.xml.transform.stream.StreamSource)338 Source (javax.xml.transform.Source)115 StringReader (java.io.StringReader)101 StreamResult (javax.xml.transform.stream.StreamResult)85 Transformer (javax.xml.transform.Transformer)74 Test (org.junit.Test)73 InputStream (java.io.InputStream)68 TransformerFactory (javax.xml.transform.TransformerFactory)58 ByteArrayInputStream (java.io.ByteArrayInputStream)56 IOException (java.io.IOException)52 DOMSource (javax.xml.transform.dom.DOMSource)49 TransformerException (javax.xml.transform.TransformerException)48 StringWriter (java.io.StringWriter)45 SchemaFactory (javax.xml.validation.SchemaFactory)44 InputSource (org.xml.sax.InputSource)44 Schema (javax.xml.validation.Schema)43 SAXException (org.xml.sax.SAXException)42 SAXSource (javax.xml.transform.sax.SAXSource)33 Validator (javax.xml.validation.Validator)31 File (java.io.File)27