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);
}
}
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);
}
}
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);
}
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);
}
}
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);
}
}
Aggregations