use of javax.xml.transform.sax.SAXResult in project jackrabbit by apache.
the class ResultHelper method getResult.
/**
* In case the underlying XML library doesn't properly handle xmlns attributes
* this method creates new content handler dealing with the misbehavior and
* returns an new instance of SAXResult. Otherwise the passed result
* is returned back.
*
* @param result
* @return A instance of <code>Result</code> that properly handles xmlns attributes.
* @throws SAXException
*/
public static Result getResult(Result result) throws SAXException {
try {
TransformerHandler handler = FACTORY.newTransformerHandler();
handler.setResult(result);
// Specify the output properties to avoid surprises especially in
// character encoding or the output method (might be html for some
// documents!)
Transformer transformer = handler.getTransformer();
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "no");
if (NEEDS_XMLNS_ATTRIBUTES) {
// so we need to do it explicitly with this wrapper
return new SAXResult(new SerializingContentHandler(handler));
} else {
return result;
}
} catch (TransformerConfigurationException e) {
throw new SAXException("Failed to initialize XML serializer", e);
}
}
use of javax.xml.transform.sax.SAXResult in project sling by apache.
the class SlingTransformer method setXMLConsumer.
@Override
protected void setXMLConsumer(XMLConsumer consumer) {
TransformerHandler transformerHandler;
try {
transformerHandler = this.createTransformerHandler();
} catch (Exception ex) {
throw new RuntimeException("Could not initialize transformer handler.", ex);
}
final Map<String, Object> map = this.getLogicSheetParameters();
if (map != null) {
final Transformer transformer = transformerHandler.getTransformer();
for (Entry<String, Object> entry : map.entrySet()) {
transformer.setParameter(entry.getKey(), entry.getValue());
}
}
final SAXResult result = new SAXResult();
result.setHandler(consumer);
// According to TrAX specs, all TransformerHandlers are LexicalHandlers
result.setLexicalHandler(consumer);
transformerHandler.setResult(result);
super.setXMLConsumer(new XMLConsumerAdapter(transformerHandler, transformerHandler));
}
use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.
the class TestGetSAXResult method runTest.
@Override
protected void runTest() throws Throwable {
TransformerFactory transformerFactory = xsltImplementation.newTransformerFactory();
StreamSource source = new StreamSource(file.getUrl().toString());
OMDocument document = metaFactory.getOMFactory().createOMDocument();
SAXResult result = document.getSAXResult();
transformerFactory.newTransformer().transform(source, result);
assertAbout(xml()).that(xml(OMDocument.class, document)).ignoringWhitespaceInPrologAndEpilog().ignoringRedundantNamespaceDeclarations().expandingEntityReferences().hasSameContentAs(file.getUrl());
}
use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.
the class XSLTImplementation method supportsLexicalHandlerWithStreamSource.
/**
* Determine if an identity transformation from a {@link StreamSource} to a {@link SAXResult}
* will generate events defined by {@link LexicalHandler}.
*
* @return <code>true</code> if the XSLT implementation will invoke the methods on the
* {@link LexicalHandler} set on the {@link SAXResult}, <code>false</code> otherwise
*/
public final synchronized boolean supportsLexicalHandlerWithStreamSource() {
if (supportsLexicalHandlerWithStreamSource == null) {
StreamSource source = new StreamSource(new StringReader("<!DOCTYPE root><root><![CDATA[test]]></root>"));
TestContentHandler handler = new TestContentHandler();
SAXResult result = new SAXResult(handler);
result.setLexicalHandler(handler);
try {
newTransformerFactory().newTransformer().transform(source, result);
} catch (TransformerException ex) {
throw new RuntimeException(ex);
}
supportsLexicalHandlerWithStreamSource = handler.getLexicalEventsReceived() == 4;
}
return supportsLexicalHandlerWithStreamSource;
}
Aggregations