use of javax.xml.transform.sax.SAXTransformerFactory in project tika by apache.
the class OutlookParserTest method testOutlookForwarded.
@Test
public void testOutlookForwarded() throws Exception {
Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
// Check the HTML version
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
try (InputStream stream = OutlookParserTest.class.getResourceAsStream("/test-documents/testMSG_forwarded.msg")) {
parser.parse(stream, handler, metadata, new ParseContext());
}
// Make sure we don't have nested docs
String content = sw.toString();
assertEquals(2, content.split("<body>").length);
assertEquals(2, content.split("<\\/body>").length);
}
use of javax.xml.transform.sax.SAXTransformerFactory in project sling by apache.
the class SlingTransformer method createTransformerHandler.
private TransformerHandler createTransformerHandler() throws Exception {
SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler();
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(templatesHandler);
InputSource inputSource = new InputSource(getXsltSource());
xmlReader.parse(inputSource);
// Create transformer handler
final TransformerHandler handler = transformerFactory.newTransformerHandler(templatesHandler.getTemplates());
return handler;
}
use of javax.xml.transform.sax.SAXTransformerFactory in project sling by apache.
the class SimpleXmlSerializationManager method buildSerializationData.
@Override
public SerializationData buildSerializationData(File contentSyncRoot, ResourceProxy resource) throws SerializationException {
if (resource == null) {
return null;
}
Map<String, Object> content = resource.getProperties();
if (content == null || content.isEmpty()) {
return null;
}
try {
SAXTransformerFactory f = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
ByteArrayOutputStream result = new ByteArrayOutputStream();
StreamResult sr = new StreamResult(result);
TransformerHandler handler = f.newTransformerHandler();
Transformer t = handler.getTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(sr);
handler.startDocument();
startElement(handler, TAG_RESOURCE);
Set<Entry<String, Object>> entrySet = new TreeMap<>(content).entrySet();
for (Map.Entry<String, Object> property : entrySet) {
Object value = property.getValue();
if (value instanceof String) {
String tagName = property.getKey();
String tagValue = (String) value;
AttributesImpl attributes = new AttributesImpl();
attributes.addAttribute("", ATT_PROPERTY_NAME, ATT_PROPERTY_NAME, null, tagName);
handler.startElement("", TAG_PROPERTY, TAG_PROPERTY, attributes);
handler.characters(tagValue.toCharArray(), 0, tagValue.length());
handler.endElement("", TAG_PROPERTY, TAG_PROPERTY);
} else {
// TODO multi-valued properties, other primitives
System.err.println("Can't yet handle property " + property.getKey() + " of type " + value.getClass());
}
}
endElement(handler, TAG_RESOURCE);
handler.endDocument();
// TODO - also add the serialization type
return new SerializationData(resource.getPath(), CONTENT_XML, result.toByteArray(), null);
} catch (TransformerConfigurationException | TransformerFactoryConfigurationError | SAXException e) {
// TODO proper exception handling
throw new RuntimeException(e);
}
}
use of javax.xml.transform.sax.SAXTransformerFactory 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.SAXTransformerFactory 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());
}
Aggregations