Search in sources :

Example 11 with SAXSource

use of javax.xml.transform.sax.SAXSource in project camel by apache.

the class XmlConverterTest method testToStreamSourceBySaxSource.

public void testToStreamSourceBySaxSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
    StreamSource out = conv.toStreamSource(source, null);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource)

Example 12 with SAXSource

use of javax.xml.transform.sax.SAXSource in project camel by apache.

the class XmlConverterTest method testToSaxSourceByDomSource.

public void testToSaxSourceByDomSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    DOMSource source = conv.toDOMSource("<foo>bar</foo>");
    SAXSource out = conv.toSAXSource(source, null);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SAXSource(javax.xml.transform.sax.SAXSource)

Example 13 with SAXSource

use of javax.xml.transform.sax.SAXSource in project camel by apache.

the class JingValidator method process.

public void process(Exchange exchange) throws Exception {
    Jaxp11XMLReaderCreator xmlCreator = new Jaxp11XMLReaderCreator();
    DefaultValidationErrorHandler errorHandler = new DefaultValidationErrorHandler();
    PropertyMapBuilder mapBuilder = new PropertyMapBuilder();
    mapBuilder.put(ValidateProperty.XML_READER_CREATOR, xmlCreator);
    mapBuilder.put(ValidateProperty.ERROR_HANDLER, errorHandler);
    PropertyMap propertyMap = mapBuilder.toPropertyMap();
    Validator validator = getSchema().createValidator(propertyMap);
    Message in = exchange.getIn();
    SAXSource saxSource = in.getBody(SAXSource.class);
    if (saxSource == null) {
        Source source = exchange.getIn().getMandatoryBody(Source.class);
        saxSource = ExchangeHelper.convertToMandatoryType(exchange, SAXSource.class, source);
    }
    InputSource bodyInput = saxSource.getInputSource();
    // now lets parse the body using the validator
    XMLReader reader = xmlCreator.createXMLReader();
    reader.setContentHandler(validator.getContentHandler());
    reader.setDTDHandler(validator.getDTDHandler());
    reader.setErrorHandler(errorHandler);
    reader.parse(bodyInput);
    errorHandler.handleErrors(exchange, schema);
}
Also used : InputSource(org.xml.sax.InputSource) Jaxp11XMLReaderCreator(com.thaiopensource.xml.sax.Jaxp11XMLReaderCreator) PropertyMap(com.thaiopensource.util.PropertyMap) SAXSource(javax.xml.transform.sax.SAXSource) Message(org.apache.camel.Message) PropertyMapBuilder(com.thaiopensource.util.PropertyMapBuilder) DefaultValidationErrorHandler(org.apache.camel.processor.validation.DefaultValidationErrorHandler) Validator(com.thaiopensource.validate.Validator) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader)

Example 14 with SAXSource

use of javax.xml.transform.sax.SAXSource 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 15 with SAXSource

use of javax.xml.transform.sax.SAXSource 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)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10