Search in sources :

Example 26 with Source

use of javax.xml.transform.Source in project sonarqube by SonarSource.

the class DebtModelXMLExporter method prettyFormatXml.

private static String prettyFormatXml(String xml) {
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
    } catch (TransformerException ignored) {
    // Ignore, raw XML will be returned
    }
    return xml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException)

Example 27 with Source

use of javax.xml.transform.Source in project OpenAttestation by OpenAttestation.

the class ConverterUtil method formateXMLString.

public static String formateXMLString(String inputXML) {
    StreamResult xmlOutput = null;
    try {
        Source xmlInput = new StreamSource(new StringReader(inputXML));
        StringWriter stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(xmlInput, xmlOutput);
    } catch (Exception e) {
        // simple exception handling, please review it
        throw new RuntimeException(e);
    }
    return xmlOutput.getWriter().toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 28 with Source

use of javax.xml.transform.Source in project feign by OpenFeign.

the class JAXBDecoder method decode.

@Override
public Object decode(Response response, Type type) throws IOException {
    if (response.status() == 404)
        return Util.emptyValueOf(type);
    if (response.body() == null)
        return null;
    if (!(type instanceof Class)) {
        throw new UnsupportedOperationException("JAXB only supports decoding raw types. Found " + type);
    }
    try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        /* Explicitly control sax configuration to prevent XXE attacks */
        saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
        saxParserFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false);
        saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
        saxParserFactory.setNamespaceAware(namespaceAware);
        Source source = new SAXSource(saxParserFactory.newSAXParser().getXMLReader(), new InputSource(response.body().asInputStream()));
        Unmarshaller unmarshaller = jaxbContextFactory.createUnmarshaller((Class) type);
        return unmarshaller.unmarshal(source);
    } catch (JAXBException e) {
        throw new DecodeException(e.toString(), e);
    } catch (ParserConfigurationException e) {
        throw new DecodeException(e.toString(), e);
    } catch (SAXException e) {
        throw new DecodeException(e.toString(), e);
    } finally {
        if (response.body() != null) {
            response.body().close();
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) JAXBException(javax.xml.bind.JAXBException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Unmarshaller(javax.xml.bind.Unmarshaller) DecodeException(feign.codec.DecodeException) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 29 with Source

use of javax.xml.transform.Source in project jersey by jersey.

the class SourceEntityProviderTest method getSourceTest.

@Test
public void getSourceTest() throws Exception {
    Response response = target().path("test").path("source").request().get();
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    String content = extractContent(response.readEntity(Source.class));
    assertTrue(content.startsWith(prefix) || content.startsWith(xdkPrefix));
}
Also used : Response(javax.ws.rs.core.Response) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 30 with Source

use of javax.xml.transform.Source in project chuck by jgilfelt.

the class FormatUtils method formatXml.

public static String formatXml(String xml) {
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
    } catch (Exception e) {
        return xml;
    }
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource)

Aggregations

Source (javax.xml.transform.Source)238 StreamSource (javax.xml.transform.stream.StreamSource)161 DOMSource (javax.xml.transform.dom.DOMSource)108 Transformer (javax.xml.transform.Transformer)76 StreamResult (javax.xml.transform.stream.StreamResult)74 InputSource (org.xml.sax.InputSource)67 SAXSource (javax.xml.transform.sax.SAXSource)56 StringReader (java.io.StringReader)52 IOException (java.io.IOException)46 TransformerException (javax.xml.transform.TransformerException)45 Result (javax.xml.transform.Result)42 TransformerFactory (javax.xml.transform.TransformerFactory)41 Test (org.junit.Test)39 StringWriter (java.io.StringWriter)35 InputStream (java.io.InputStream)32 SAXException (org.xml.sax.SAXException)32 Schema (javax.xml.validation.Schema)29 Validator (javax.xml.validation.Validator)29 SchemaFactory (javax.xml.validation.SchemaFactory)28 ByteArrayInputStream (java.io.ByteArrayInputStream)26