Search in sources :

Example 76 with StreamSource

use of javax.xml.transform.stream.StreamSource in project gradle by gradle.

the class XslTransformer method main.

public static void main(String[] args) throws TransformerException, IOException {
    if (args.length < 3 || args.length > 4) {
        throw new IllegalArgumentException("USAGE: <style-sheet> <source-file> <dest-file> [dest-dir]");
    }
    File stylesheet = new File(args[0]);
    File source = new File(args[1]);
    File dest = new File(args[2]);
    String destDir = "";
    if (args.length > 3) {
        destDir = args[3];
    }
    System.out.format("=> stylesheet %s%n", stylesheet);
    System.out.format("=> source %s%n", source);
    System.out.format("=> dest %s%n", dest);
    System.out.format("=> destDir %s%n", destDir);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    System.out.format("=> transformer %s (%s)%n", transformer, transformer.getClass().getName());
    if (destDir.length() > 0) {
        transformer.setParameter("base.dir", destDir + "/");
    }
    FileOutputStream outstr = new FileOutputStream(dest);
    try {
        transformer.transform(new StreamSource(source), new StreamResult(outstr));
    } finally {
        outstr.close();
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 77 with StreamSource

use of javax.xml.transform.stream.StreamSource in project graphhopper by graphhopper.

the class InstructionListTest method verifyGPX.

public void verifyGPX(String gpx) {
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = null;
    try {
        Source schemaFile = new StreamSource(getClass().getResourceAsStream("gpx-schema.xsd"));
        schema = schemaFactory.newSchema(schemaFile);
    // using more schemas: http://stackoverflow.com/q/1094893/194609
    } catch (SAXException e1) {
        throw new IllegalStateException("There was a problem with the schema supplied for validation. Message:" + e1.getMessage());
    }
    Validator validator = schema.newValidator();
    try {
        validator.validate(new StreamSource(new StringReader(gpx)));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) Schema(javax.xml.validation.Schema) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 78 with StreamSource

use of javax.xml.transform.stream.StreamSource in project hazelcast by hazelcast.

the class DiscoverySpiTest method testSchema.

@Test
public void testSchema() throws Exception {
    String xmlFileName = "test-hazelcast-discovery-spi.xml";
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    URL schemaResource = DiscoverySpiTest.class.getClassLoader().getResource("hazelcast-config-3.9.xsd");
    assertNotNull(schemaResource);
    InputStream xmlResource = DiscoverySpiTest.class.getClassLoader().getResourceAsStream(xmlFileName);
    Source source = new StreamSource(xmlResource);
    Schema schema = factory.newSchema(schemaResource);
    Validator validator = schema.newValidator();
    validator.validate(source);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Schema(javax.xml.validation.Schema) URL(java.net.URL) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Validator(javax.xml.validation.Validator) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 79 with StreamSource

use of javax.xml.transform.stream.StreamSource in project zaproxy by zaproxy.

the class ReportGenerator method XMLToHtml.

public static File XMLToHtml(Document xmlDocument, String infilexsl, File outFile) {
    File stylesheet = null;
    outFile = new File(outFile.getAbsolutePath());
    try {
        stylesheet = new File(infilexsl);
        DOMSource source = new DOMSource(xmlDocument);
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);
        // Make the transformation and write to the output file
        StreamResult result = new StreamResult(outFile);
        transformer.transform(source, result);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
    }
    return outFile;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 80 with StreamSource

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

the class SaajEmptyNamespaceTest method createSoapMessage.

// Create SOAP message with empty body
private static SOAPMessage createSoapMessage() throws SOAPException, UnsupportedEncodingException {
    String xml = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + "<SOAP-ENV:Body/></SOAP-ENV:Envelope>";
    MessageFactory mFactory = MessageFactory.newInstance();
    SOAPMessage msg = mFactory.createMessage();
    msg.getSOAPPart().setContent(new StreamSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
    return msg;
}
Also used : MessageFactory(javax.xml.soap.MessageFactory) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) SOAPMessage(javax.xml.soap.SOAPMessage)

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