use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.
the class DOMUtils method getStringFromDoc.
/**
* This returns a string representation of the given document.
*
* @param doc
* an XML <code>Document</code>
* @param encoding
* a <code>String</code> with the encoding to use
* @param docTypeDef
* a <code>String</code> with the DTD name; use <code>null</code>
* for no DTD
* @return a <code>String</code> with the XML string
*/
public static String getStringFromDoc(Document doc, String encoding, String docTypeDef) {
try {
// use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer xformer = tFactory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
xformer.setOutputProperty(OutputKeys.METHOD, "xml");
if (null != docTypeDef) {
xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeDef);
}
DOMSource source = new DOMSource(doc);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
xformer.transform(source, result);
return sw.toString();
} catch (TransformerConfigurationException tce) {
// error generated by the parser
System.err.println("** Transformer Factory error");
System.err.println(" " + tce.getMessage());
// use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null) {
x = tce.getException();
}
x.printStackTrace();
} catch (TransformerException te) {
// error generated by the parser
System.err.println("** Transformation error");
System.err.println(" " + te.getMessage());
// use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
}
return null;
}
use of javax.xml.transform.TransformerConfigurationException in project tika by apache.
the class TikaResource method produceOutput.
private StreamingOutput produceOutput(final InputStream is, final MultivaluedMap<String, String> httpHeaders, final UriInfo info, final String format) {
final Parser parser = createParser();
final Metadata metadata = new Metadata();
final ParseContext context = new ParseContext();
fillMetadata(parser, metadata, context, httpHeaders);
fillParseContext(context, httpHeaders, parser);
logRequest(LOG, info, metadata);
return new StreamingOutput() {
public void write(OutputStream outputStream) throws IOException, WebApplicationException {
Writer writer = new OutputStreamWriter(outputStream, UTF_8);
ContentHandler content;
try {
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, format);
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
handler.setResult(new StreamResult(writer));
content = new ExpandedTitleContentHandler(handler);
} catch (TransformerConfigurationException e) {
throw new WebApplicationException(e);
}
parse(parser, LOG, info.getPath(), is, content, metadata, context);
}
};
}
use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.
the class XsltExtractor method initialize.
public void initialize(TransformerFactory factory) throws InitializationException {
if (source == null || id == null) {
throw new InitializationException("Missing source or id");
}
if (factory == null) {
factory = TransformerFactory.newInstance();
factory.setURIResolver(new BundleURIResolver());
}
StreamSource xsltSource = new StreamSource(source.toString());
xsltSource.setSystemId(source.toString());
try {
transformer = factory.newTransformer(xsltSource);
} catch (TransformerConfigurationException e) {
throw new InitializationException(e.getMessage(), e);
}
}
use of javax.xml.transform.TransformerConfigurationException 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.TransformerConfigurationException 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());
}
Aggregations