use of javax.xml.transform.TransformerConfigurationException in project sling by apache.
the class XmlRenderer method getStringFromElement.
/**
* Convert an Element to a String representation
* @param element
* @return a String representation
*/
public static String getStringFromElement(Element element) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
StringWriter sw = new StringWriter();
trans.transform(new DOMSource(element), new StreamResult(sw));
String elementString = sw.toString();
return elementString;
} catch (TransformerConfigurationException e) {
System.err.println(getException(e));
} catch (TransformerException e) {
System.err.println(getException(e));
}
return "";
}
use of javax.xml.transform.TransformerConfigurationException in project kernel by exoplatform.
the class ConfigurationUnmarshaller method unmarshall.
public Configuration unmarshall(final URL url) throws Exception {
if (PropertyManager.isDevelopping()) {
boolean valid = isValid(url);
if (!valid) {
LOG.info("The configuration file " + url + " was not found valid according to its XSD");
}
}
//
DocumentBuilderFactory factory = null;
try {
// With Java 6, it's safer to precise the builder factory class name as it may result:
// java.lang.AbstractMethodError: org.apache.xerces.dom.DeferredDocumentImpl.getXmlStandalone()Z
// at com.sun.org.apache.xalan.internal.xsltc.trax.DOM2TO.setDocumentInfo(Unknown Source)
Method dbfniMethod = DocumentBuilderFactory.class.getMethod("newInstance", String.class, ClassLoader.class);
factory = (DocumentBuilderFactory) dbfniMethod.invoke(null, "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", Thread.currentThread().getContextClassLoader());
} catch (InvocationTargetException e) {
Throwable cause = e.getCause();
if (cause instanceof FactoryConfigurationError) {
// do nothing and let try to instantiate later
LOG.debug("Was not able to find document builder factory class in Java > 5, will use default", cause);
} else {
// Rethrow
throw e;
}
} catch (NoSuchMethodException e) {
if (LOG.isTraceEnabled()) {
LOG.trace("An exception occurred: " + e.getMessage());
}
}
//
if (factory == null) {
factory = DocumentBuilderFactory.newInstance();
}
//
factory.setNamespaceAware(true);
final DocumentBuilderFactory builderFactory = factory;
try {
return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Configuration>() {
public Configuration run() throws Exception {
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document doc = builder.parse(url.openStream());
// Filter DOM
ProfileDOMFilter filter = new ProfileDOMFilter(profiles);
filter.process(doc.getDocumentElement());
// SAX event stream -> String
StringWriter buffer = new StringWriter();
SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler hd = tf.newTransformerHandler();
StreamResult result = new StreamResult(buffer);
hd.setResult(result);
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
// Transform -> SAX event stream
SAXResult saxResult = new SAXResult(new NoKernelNamespaceSAXFilter(hd));
// DOM -> Transform
serializer.transform(new DOMSource(doc), saxResult);
// Reuse the parsed document
String document = buffer.toString();
// Debug
if (LOG.isTraceEnabled())
LOG.trace("About to parse configuration file " + document);
//
IBindingFactory bfact = BindingDirectory.getFactory(Configuration.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
return (Configuration) uctx.unmarshalDocument(new StringReader(document), null);
}
});
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();
if (cause instanceof JiBXException) {
throw (JiBXException) cause;
} else if (cause instanceof ParserConfigurationException) {
throw (ParserConfigurationException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
} else if (cause instanceof SAXException) {
throw (SAXException) cause;
} else if (cause instanceof IllegalArgumentException) {
throw (IllegalArgumentException) cause;
} else if (cause instanceof TransformerException) {
throw (TransformerException) cause;
} else if (cause instanceof TransformerConfigurationException) {
throw (TransformerConfigurationException) cause;
} else if (cause instanceof TransformerFactoryConfigurationError) {
throw (TransformerFactoryConfigurationError) cause;
} else if (cause instanceof RuntimeException) {
throw (RuntimeException) cause;
} else {
throw new RuntimeException(cause);
}
}
}
use of javax.xml.transform.TransformerConfigurationException in project HongsCORE by ihongs.
the class Unit method saveDocument.
private void saveDocument(String path, Document docm) throws HongsException {
File file = new File(path);
File fold = file.getParentFile();
if (!fold.exists()) {
fold.mkdirs();
}
TransformerFactory tf = TransformerFactory.newInstance();
try {
Transformer tr = tf.newTransformer();
DOMSource ds = new DOMSource(docm);
StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
tr.transform(ds, sr);
} catch (TransformerConfigurationException e) {
throw new HongsException.Common(e);
} catch (IllegalArgumentException e) {
throw new HongsException.Common(e);
} catch (TransformerException e) {
throw new HongsException.Common(e);
} catch (FileNotFoundException e) {
throw new HongsException.Common(e);
} catch (UnsupportedEncodingException e) {
throw new HongsException.Common(e);
}
}
use of javax.xml.transform.TransformerConfigurationException in project nutch by apache.
the class DomUtil method saveDom.
/**
* save dom into ouputstream
*
* @param os
* @param e
*/
public static void saveDom(OutputStream os, Element e) {
DOMSource source = new DOMSource(e);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
os.flush();
} catch (UnsupportedEncodingException e1) {
LOG.error("Error: ", e1);
} catch (IOException e1) {
LOG.error("Error: ", e1);
} catch (TransformerConfigurationException e2) {
LOG.error("Error: ", e2);
} catch (TransformerException ex) {
LOG.error("Error: ", ex);
}
}
use of javax.xml.transform.TransformerConfigurationException in project MassBank-web by MassBank.
the class UpdateConfig method saveConf.
/**
* 設定情報の書き込み処理
* @return 結果
*/
private boolean saveConf() {
Transformer tf = null;
try {
TransformerFactory factory = TransformerFactory.newInstance();
tf = factory.newTransformer();
tf.setOutputProperty("indent", "yes");
tf.setOutputProperty("encoding", "utf-8");
} catch (TransformerConfigurationException e) {
e.printStackTrace();
return false;
}
// 書き出し
try {
tf.transform(new DOMSource(doc), new StreamResult(new File(confPath)));
} catch (TransformerException e) {
e.printStackTrace();
return false;
}
return true;
}
Aggregations