Search in sources :

Example 86 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class JibxDataFormatSpringDslTest method testMarshall.

@Test
public void testMarshall() throws InterruptedException, ParserConfigurationException, IOException, SAXException {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    PurchaseOrder purchaseOrder = new PurchaseOrder();
    String name = "foo";
    purchaseOrder.setName(name);
    double price = 49;
    purchaseOrder.setPrice(price);
    double amount = 3;
    purchaseOrder.setAmount(amount);
    template.sendBody("direct:marshall", purchaseOrder);
    assertMockEndpointsSatisfied();
    String body = mock.getReceivedExchanges().get(0).getIn().getBody(String.class);
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Element root = builder.parse(new InputSource(new StringReader(body))).getDocumentElement();
    assertEquals(name, root.getAttribute("name"));
    assertEquals(price + "", root.getAttribute("price"));
    assertEquals(amount + "", root.getAttribute("amount"));
}
Also used : InputSource(org.xml.sax.InputSource) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) StringReader(java.io.StringReader) PurchaseOrder(org.apache.camel.dataformat.jibx.model.PurchaseOrder) Test(org.junit.Test)

Example 87 with InputSource

use of org.xml.sax.InputSource 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 88 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class XsltBuilderTest method testXsltTemplates.

public void testXsltTemplates() throws Exception {
    File file = new File("src/test/resources/org/apache/camel/builder/xml/example.xsl");
    Source source = new SAXSource(new InputSource(new FileInputStream(file)));
    XmlConverter converter = new XmlConverter();
    Templates styleSheet = converter.getTransformerFactory().newTemplates(source);
    XsltBuilder builder = XsltBuilder.xslt(styleSheet);
    Exchange exchange = new DefaultExchange(context);
    exchange.getIn().setBody("<hello>world!</hello>");
    builder.process(exchange);
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>", exchange.getOut().getBody());
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Templates(javax.xml.transform.Templates) File(java.io.File) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) FileInputStream(java.io.FileInputStream) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter)

Example 89 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class XPathTest method testXPathWithDocumentTypeInputSourceNoResultQName.

public void testXPathWithDocumentTypeInputSourceNoResultQName() throws Exception {
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
    InputSource doc = new InputSource(is);
    XPathBuilder builder = xpath("/foo");
    builder.setDocumentType(InputSource.class);
    builder.setResultQName(null);
    Object result = builder.evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals("bar", s);
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream)

Example 90 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class StreamCacheConverterTest method testConvertToStreamCache.

public void testConvertToStreamCache() throws Exception {
    context.start();
    ByteArrayInputStream inputStream = new ByteArrayInputStream(MESSAGE.getBytes());
    StreamCache streamCache = StreamCacheConverter.convertToStreamCache(new SAXSource(new InputSource(inputStream)), exchange);
    String message = exchange.getContext().getTypeConverter().convertTo(String.class, streamCache);
    assertNotNull(message);
    assertEquals("The converted message is wrong", MESSAGE, message);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamCache(org.apache.camel.StreamCache)

Aggregations

InputSource (org.xml.sax.InputSource)1124 StringReader (java.io.StringReader)401 IOException (java.io.IOException)304 Document (org.w3c.dom.Document)282 SAXException (org.xml.sax.SAXException)281 DocumentBuilder (javax.xml.parsers.DocumentBuilder)262 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)213 XMLReader (org.xml.sax.XMLReader)194 Test (org.junit.Test)160 InputStream (java.io.InputStream)158 NodeList (org.w3c.dom.NodeList)145 Element (org.w3c.dom.Element)144 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)143 ByteArrayInputStream (java.io.ByteArrayInputStream)103 SAXParser (javax.xml.parsers.SAXParser)103 SAXSource (javax.xml.transform.sax.SAXSource)95 SAXParserFactory (javax.xml.parsers.SAXParserFactory)90 File (java.io.File)82 Node (org.w3c.dom.Node)82 URL (java.net.URL)65