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 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 j2objc by google.
the class TransformerFactoryImpl method newTransformerHandler.
/**
* Get a TransformerHandler object that can process SAX
* ContentHandler events into a Result, based on the Templates argument.
*
* @param templates The source of the transformation instructions.
*
* @return TransformerHandler ready to transform SAX events.
* @throws TransformerConfigurationException
*/
public TransformerHandler newTransformerHandler(Templates templates) throws TransformerConfigurationException {
try {
TransformerImpl transformer = (TransformerImpl) templates.newTransformer();
transformer.setURIResolver(m_uriResolver);
TransformerHandler th = (TransformerHandler) transformer.getInputContentHandler(true);
return th;
} catch (TransformerConfigurationException ex) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(ex);
return null;
} catch (TransformerConfigurationException ex1) {
throw ex1;
} catch (TransformerException ex1) {
throw new TransformerConfigurationException(ex1);
}
}
throw ex;
}
}
use of javax.xml.transform.TransformerConfigurationException in project j2objc by google.
the class TransformerFactoryImpl method processFromNode.
public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException {
try {
TemplatesHandler builder = newTemplatesHandler();
TreeWalker walker = new TreeWalker(builder, new org.apache.xml.utils.DOM2Helper(), builder.getSystemId());
walker.traverse(node);
return builder.getTemplates();
} catch (org.xml.sax.SAXException se) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(se));
} catch (TransformerConfigurationException ex) {
throw ex;
} catch (TransformerException ex) {
throw new TransformerConfigurationException(ex);
}
return null;
} else {
// se.printStackTrace();
throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se);
// "processFromNode failed", se);
}
} catch (TransformerConfigurationException tce) {
// Assume it's already been reported to the error listener.
throw tce;
}/* catch (TransformerException tce)
{
// Assume it's already been reported to the error listener.
throw new TransformerConfigurationException(tce.getMessage(), tce);
}*/
catch (Exception e) {
if (m_errorListener != null) {
try {
m_errorListener.fatalError(new TransformerException(e));
} catch (TransformerConfigurationException ex) {
throw ex;
} catch (TransformerException ex) {
throw new TransformerConfigurationException(ex);
}
return null;
} else {
// "processFromNode failed",
throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e);
// e);
}
}
}
use of javax.xml.transform.TransformerConfigurationException in project tomee by apache.
the class XSLTJaxbProvider method marshalToOutputStream.
@Override
protected void marshalToOutputStream(Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt) throws Exception {
Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
if (t == null && supportJaxbOnly) {
super.marshalToOutputStream(ms, obj, os, anns, mt);
return;
}
org.apache.cxf.common.jaxb.JAXBUtils.setMinimumEscapeHandler(ms);
TransformerHandler th;
try {
th = factory.newTransformerHandler(t);
} catch (TransformerConfigurationException ex) {
TemplatesImpl ti = (TemplatesImpl) t;
th = factory.newTransformerHandler(ti.getTemplates());
this.trySettingProperties(th, ti);
}
Result result = getStreamResult(os, anns, mt);
if (systemId != null) {
result.setSystemId(systemId);
}
th.setResult(result);
if (getContext() == null) {
th.startDocument();
}
ms.marshal(obj, th);
if (getContext() == null) {
th.endDocument();
}
}
Aggregations