Search in sources :

Example 11 with InputSource

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

the class JibxDataFormatMarshallTest 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:start", 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 12 with InputSource

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

the class JibxDataFormatMarshallWithBindingNameTest 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:start", 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 13 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 14 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 15 with InputSource

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

the class XmlParser method parse.

/**
     * Parses the content of the given file as XML turning it into a tree
     * of Nodes.
     *
     * @param file the File containing the XML to be parsed
     * @return the root node of the parsed tree of Nodes
     * @throws SAXException Any SAX exception, possibly
     *                      wrapping another exception.
     * @throws IOException  An IO exception from the parser,
     *                      possibly from a byte stream or character stream
     *                      supplied by the application.
     */
public Node parse(File file) throws IOException, SAXException {
    InputSource input = new InputSource(new FileInputStream(file));
    input.setSystemId("file://" + file.getAbsolutePath());
    getXMLReader().parse(input);
    return parent;
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream)

Aggregations

InputSource (org.xml.sax.InputSource)966 StringReader (java.io.StringReader)365 IOException (java.io.IOException)249 Document (org.w3c.dom.Document)243 DocumentBuilder (javax.xml.parsers.DocumentBuilder)237 SAXException (org.xml.sax.SAXException)230 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)190 XMLReader (org.xml.sax.XMLReader)166 Test (org.junit.Test)149 NodeList (org.w3c.dom.NodeList)131 InputStream (java.io.InputStream)126 Element (org.w3c.dom.Element)124 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)117 SAXParser (javax.xml.parsers.SAXParser)94 ByteArrayInputStream (java.io.ByteArrayInputStream)80 SAXSource (javax.xml.transform.sax.SAXSource)78 Node (org.w3c.dom.Node)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)74 File (java.io.File)64 FileInputStream (java.io.FileInputStream)55