Search in sources :

Example 31 with TransformerConfigurationException

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 "";
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 32 with TransformerConfigurationException

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);
        }
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.exoplatform.container.xml.Configuration) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IBindingFactory(org.jibx.runtime.IBindingFactory) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError) TransformerException(javax.xml.transform.TransformerException) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) StreamResult(javax.xml.transform.stream.StreamResult) PrivilegedActionException(java.security.PrivilegedActionException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) JiBXException(org.jibx.runtime.JiBXException) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TransformerException(javax.xml.transform.TransformerException) JiBXException(org.jibx.runtime.JiBXException) PrivilegedActionException(java.security.PrivilegedActionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) IUnmarshallingContext(org.jibx.runtime.IUnmarshallingContext) SAXResult(javax.xml.transform.sax.SAXResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder)

Example 33 with TransformerConfigurationException

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HongsException(app.hongs.HongsException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 34 with TransformerConfigurationException

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException)

Example 35 with TransformerConfigurationException

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;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)93 TransformerException (javax.xml.transform.TransformerException)62 Transformer (javax.xml.transform.Transformer)52 StreamResult (javax.xml.transform.stream.StreamResult)49 DOMSource (javax.xml.transform.dom.DOMSource)42 IOException (java.io.IOException)35 TransformerFactory (javax.xml.transform.TransformerFactory)33 StreamSource (javax.xml.transform.stream.StreamSource)23 SAXException (org.xml.sax.SAXException)21 StringWriter (java.io.StringWriter)17 Source (javax.xml.transform.Source)16 TransformerHandler (javax.xml.transform.sax.TransformerHandler)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Document (org.w3c.dom.Document)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InputStream (java.io.InputStream)10 Node (org.w3c.dom.Node)10 File (java.io.File)9 Result (javax.xml.transform.Result)9