use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.
the class DOMUtils method writeXml.
/**
* This method writes a DOM document to the given output stream.
*
* @param doc
* a DOM <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
* @param out
* an <code>OutputStream</code> where to write the DOM document
*/
public static void writeXml(Document doc, String encoding, String docTypeDef, OutputStream out) {
try {
// prepare the DOM document
Source source = new DOMSource(doc);
// prepare the output
Result result = new StreamResult(out);
// write the DOM document to the file
// get Transformer
Transformer xformer = TransformerFactory.newInstance().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);
}
// write to a file
xformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// error generated during transformer configuration
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 transformer
System.err.println(te.getMessage());
// use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
}
}
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 stanbol by apache.
the class DOMUtils method printXML.
/**
* This prints the given DOM document to System.out with indentation and
* utf-8 encoding.
*
* @param doc
* a DOM <code>Document</code>
*/
public static void printXML(Document doc) {
try {
// prepare the DOM document for writing
Source source = new DOMSource(doc);
// prepare the output
Result result = new StreamResult(System.out);
// write the DOM document to the file
// get Transformer
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
xformer.setOutputProperty(OutputKeys.METHOD, "xml");
// write to System.out
xformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// error generated during transformer configuration
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 transformer
System.err.println(te.getMessage());
// use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
}
}
use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.
the class DOMUtils method writeXml.
/**
* This method writes a DOM document to the given output stream.
*
* @param doc
* a DOM <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
* @param out
* an <code>OutputStream</code> where to write the DOM document
*/
public static void writeXml(Document doc, String encoding, String docTypeDef, OutputStream out) {
try {
// prepare the DOM document
Source source = new DOMSource(doc);
// prepare the output
Result result = new StreamResult(out);
// write the DOM document to the file
// get Transformer
Transformer xformer = TransformerFactory.newInstance().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);
}
// write to a file
xformer.transform(source, result);
} catch (TransformerConfigurationException tce) {
// error generated during transformer configuration
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 transformer
System.err.println(te.getMessage());
// use the contained exception, if any
Throwable x = te;
if (te.getException() != null) {
x = te.getException();
}
x.printStackTrace();
}
}
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;
}
Aggregations