Search in sources :

Example 46 with TransformerException

use of javax.xml.transform.TransformerException in project camel by apache.

the class SaxonXsltDTDTest method sendEntityMessage.

private void sendEntityMessage(Object message) throws Exception {
    MockEndpoint endpoint = getMockEndpoint("mock:result");
    endpoint.reset();
    endpoint.expectedMessageCount(1);
    template.sendBody("direct:start1", message);
    assertMockEndpointsSatisfied();
    List<Exchange> list = endpoint.getReceivedExchanges();
    Exchange exchange = list.get(0);
    String xml = exchange.getIn().getBody(String.class);
    assertTrue("Get a wrong transformed message", xml.indexOf("<transformed subject=\"\">") > 0);
    endpoint.reset();
    endpoint.expectedMessageCount(1);
    try {
        template.sendBody("direct:start2", message);
        list = endpoint.getReceivedExchanges();
        exchange = list.get(0);
        xml = exchange.getIn().getBody(String.class);
        assertTrue("Get a wrong transformed message", xml.indexOf("<transformed subject=\"\">") > 0);
    } catch (Exception ex) {
        // expect an exception here
        assertTrue("Get a wrong exception", ex instanceof CamelExecutionException);
        // the file could not be found
        assertTrue("Get a wrong exception cause", ex.getCause() instanceof TransformerException);
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TransformerException(javax.xml.transform.TransformerException) CamelExecutionException(org.apache.camel.CamelExecutionException) TransformerException(javax.xml.transform.TransformerException)

Example 47 with TransformerException

use of javax.xml.transform.TransformerException in project hadoop by apache.

the class Configuration method writeXml.

/**
   * Write out the non-default properties in this configuration to the
   * given {@link Writer}.
   *
   * <li>
   * When property name is not empty and the property exists in the
   * configuration, this method writes the property and its attributes
   * to the {@link Writer}.
   * </li>
   * <p>
   *
   * <li>
   * When property name is null or empty, this method writes all the
   * configuration properties and their attributes to the {@link Writer}.
   * </li>
   * <p>
   *
   * <li>
   * When property name is not empty but the property doesn't exist in
   * the configuration, this method throws an {@link IllegalArgumentException}.
   * </li>
   * <p>
   * @param out the writer to write to.
   */
public void writeXml(String propertyName, Writer out) throws IOException, IllegalArgumentException {
    Document doc = asXmlDocument(propertyName);
    try {
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        TransformerFactory transFactory = TransformerFactory.newInstance();
        Transformer transformer = transFactory.newTransformer();
        // Important to not hold Configuration log while writing result, since
        // 'out' may be an HDFS stream which needs to lock this configuration
        // from another thread.
        transformer.transform(source, result);
    } catch (TransformerException te) {
        throw new IOException(te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 48 with TransformerException

use of javax.xml.transform.TransformerException in project camel by apache.

the class XsltTestErrorListenerTest method testErrorListener.

public void testErrorListener() throws Exception {
    // Xalan transformer cannot work as expected, so we just skip the test
    if (xsltBuilder.getConverter().getTransformerFactory().getClass().getName().startsWith("org.apache.xalan")) {
        return;
    }
    errorListener.error(EasyMock.<TransformerException>anyObject());
    expectLastCall().atLeastOnce();
    errorListener.fatalError(EasyMock.<TransformerException>anyObject());
    expectLastCall().once();
    replay(errorListener);
    URL styleSheet = getClass().getResource("example-with-errors.xsl");
    try {
        xsltBuilder.setErrorListener(errorListener);
        xsltBuilder.setTransformerURL(styleSheet);
        fail("Should throw exception");
    } catch (Exception ex) {
    // expected
    }
    verify(errorListener);
}
Also used : URL(java.net.URL) TransformerException(javax.xml.transform.TransformerException)

Example 49 with TransformerException

use of javax.xml.transform.TransformerException in project camel by apache.

the class ModelHelper method dumpModelAsXml.

/**
     * Dumps the definition as XML
     *
     * @param context    the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
     * @param definition the definition, such as a {@link org.apache.camel.NamedNode}
     * @return the output in XML (is formatted)
     * @throws JAXBException is throw if error marshalling to XML
     */
public static String dumpModelAsXml(CamelContext context, NamedNode definition) throws JAXBException {
    JAXBContext jaxbContext = getJAXBContext(context);
    final Map<String, String> namespaces = new LinkedHashMap<>();
    // gather all namespaces from the routes or route which is stored on the expression nodes
    if (definition instanceof RoutesDefinition) {
        List<RouteDefinition> routes = ((RoutesDefinition) definition).getRoutes();
        for (RouteDefinition route : routes) {
            extractNamespaces(route, namespaces);
        }
    } else if (definition instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) definition;
        extractNamespaces(route, namespaces);
    }
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter buffer = new StringWriter();
    marshaller.marshal(definition, buffer);
    XmlConverter xmlConverter = newXmlConverter(context);
    String xml = buffer.toString();
    Document dom;
    try {
        dom = xmlConverter.toDOMDocument(xml, null);
    } catch (Exception e) {
        throw new TypeConversionException(xml, Document.class, e);
    }
    // Add additional namespaces to the document root element
    Element documentElement = dom.getDocumentElement();
    for (String nsPrefix : namespaces.keySet()) {
        String prefix = nsPrefix.equals("xmlns") ? nsPrefix : "xmlns:" + nsPrefix;
        documentElement.setAttribute(prefix, namespaces.get(nsPrefix));
    }
    // We invoke the type converter directly because we need to pass some custom XML output options
    Properties outputProperties = new Properties();
    outputProperties.put(OutputKeys.INDENT, "yes");
    outputProperties.put(OutputKeys.STANDALONE, "yes");
    try {
        return xmlConverter.toStringFromDocument(dom, outputProperties);
    } catch (TransformerException e) {
        throw new IllegalStateException("Failed converting document object to string", e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) TypeConversionException(org.apache.camel.TypeConversionException) Element(org.w3c.dom.Element) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) Properties(java.util.Properties) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) LinkedHashMap(java.util.LinkedHashMap) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringWriter(java.io.StringWriter) TransformerException(javax.xml.transform.TransformerException)

Example 50 with TransformerException

use of javax.xml.transform.TransformerException in project camel by apache.

the class XsltDTDTest method sendEntityMessage.

private void sendEntityMessage(Object message) throws Exception {
    MockEndpoint endpoint = getMockEndpoint("mock:result");
    endpoint.reset();
    endpoint.expectedMessageCount(1);
    template.sendBody("direct:start1", message);
    assertMockEndpointsSatisfied();
    List<Exchange> list = endpoint.getReceivedExchanges();
    Exchange exchange = list.get(0);
    String xml = exchange.getIn().getBody(String.class);
    assertTrue("Get a wrong transformed message", xml.indexOf("<transformed subject=\"\">") > 0);
    try {
        endpoint.reset();
        endpoint.expectedMessageCount(1);
        template.sendBody("direct:start2", message);
        assertMockEndpointsSatisfied();
        list = endpoint.getReceivedExchanges();
        exchange = list.get(0);
        xml = exchange.getIn().getBody(String.class);
        assertTrue("Get a wrong transformed message", xml.indexOf("<transformed subject=\"\">") > 0);
    } catch (Exception ex) {
        // expect an exception here
        assertTrue("Get a wrong exception", ex instanceof CamelExecutionException);
        // the file could not be found
        assertTrue("Get a wrong exception cause", ex.getCause() instanceof TransformerException);
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) CamelExecutionException(org.apache.camel.CamelExecutionException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) TransformerException(javax.xml.transform.TransformerException) CamelExecutionException(org.apache.camel.CamelExecutionException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59