use of javax.xml.transform.sax.TransformerHandler in project sling by apache.
the class AbstractTraxSerializerFactory method createSerializer.
/**
* @see org.apache.sling.rewriter.SerializerFactory#createSerializer()
*/
public Serializer createSerializer() {
TransformerHandler tHandler = null;
try {
tHandler = this.tfactory.newTransformerHandler();
} catch (TransformerConfigurationException e) {
logger.error("Unable to create new transformer handler.", e);
}
final ContentHandler ch;
if (this.needsNamespacesAsAttributes) {
final NamespaceAsAttributes nsPipeline = new NamespaceAsAttributes(tHandler, this.logger);
ch = nsPipeline;
} else {
ch = tHandler;
}
return new TraxSerializer(tHandler, ch, getOutputFormat(), getDoctypePublic(), getDoctypeSystem());
}
use of javax.xml.transform.sax.TransformerHandler in project sling by apache.
the class AbstractTraxSerializerFactory method needsNamespacesAsAttributes.
/**
* Checks if the used Trax implementation correctly handles namespaces set using
* <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes.
* <p>
* The check consists in sending SAX events representing a minimal namespaced document
* with namespaces defined only with calls to <code>startPrefixMapping</code> (no
* xmlns:xxx attributes) and check if they are present in the resulting text.
*/
protected boolean needsNamespacesAsAttributes() throws Exception {
// Serialize a minimal document to check how namespaces are handled.
final StringWriter writer = new StringWriter();
final String uri = "namespaceuri";
final String prefix = "nsp";
final String check = "xmlns:" + prefix + "='" + uri + "'";
final TransformerHandler handler = this.tfactory.newTransformerHandler();
handler.setResult(new StreamResult(writer));
// Output a single element
handler.startDocument();
handler.startPrefixMapping(prefix, uri);
handler.startElement(uri, "element", "element", new AttributesImpl());
handler.endElement(uri, "element", "element");
handler.endPrefixMapping(prefix);
handler.endDocument();
final String text = writer.toString();
// Check if the namespace is there (replace " by ' to be sure of what we search in)
return (text.replace('"', '\'').indexOf(check) == -1);
}
use of javax.xml.transform.sax.TransformerHandler in project bnd by bndtools.
the class SAXUtil method buildPipeline.
public static XMLReader buildPipeline(Result output, ContentFilter... filters) throws Exception {
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.setResult(output);
ContentHandler last = handler;
if (filters != null)
for (ContentFilter filter : filters) {
filter.setParent(last);
last = filter;
}
XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
reader.setContentHandler(last);
return reader;
}
use of javax.xml.transform.sax.TransformerHandler in project ddf by codice.
the class EXIEncoderTest method testEncode.
/**
* Tests that the encode method converts xml into exi-compressed xml.
*
* @throws Exception
*/
@Test
public void testEncode() throws Exception {
ByteArrayOutputStream exiStream = new ByteArrayOutputStream();
InputStream xmlStream = getClass().getResourceAsStream(TEST_FILE);
EXIEncoder.encode(xmlStream, exiStream);
StringWriter stringWriter = new StringWriter();
GrammarCache grammarCache;
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(true);
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
EXIReader reader = new EXIReader();
grammarCache = new GrammarCache(null, GrammarOptions.DEFAULT_OPTIONS);
reader.setGrammarCache(grammarCache);
transformerHandler.setResult(new StreamResult(stringWriter));
reader.setContentHandler(transformerHandler);
reader.parse(new InputSource(new ByteArrayInputStream(exiStream.toByteArray())));
XMLUnit.setNormalize(true);
XMLUnit.setNormalizeWhitespace(true);
InputStream stream = getClass().getResourceAsStream(TEST_FILE);
Diff diff = XMLUnit.compareXML(IOUtils.toString(stream), stringWriter.getBuffer().toString());
IOUtils.closeQuietly(stream);
assertTrue("The XML input file (" + TEST_FILE + ") did not match the EXI-decoded output", diff.similar());
}
use of javax.xml.transform.sax.TransformerHandler in project camel by apache.
the class TikaProducer method getTransformerHandler.
private TransformerHandler getTransformerHandler(OutputStream output, String method, boolean prettyPrint) throws TransformerConfigurationException, UnsupportedEncodingException {
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method);
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, prettyPrint ? "yes" : "no");
if (this.encoding != null) {
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, this.encoding);
}
handler.setResult(new StreamResult(new OutputStreamWriter(output, this.encoding)));
return handler;
}
Aggregations