Search in sources :

Example 96 with TransformerConfigurationException

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;
}
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 97 with TransformerConfigurationException

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);
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource)

Example 98 with TransformerConfigurationException

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;
    }
}
Also used : TransformerImpl(org.apache.xalan.transformer.TransformerImpl) TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 99 with TransformerConfigurationException

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);
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TreeWalker(org.apache.xml.utils.TreeWalker) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 100 with TransformerConfigurationException

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();
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)233 TransformerException (javax.xml.transform.TransformerException)135 Transformer (javax.xml.transform.Transformer)126 StreamResult (javax.xml.transform.stream.StreamResult)105 IOException (java.io.IOException)91 DOMSource (javax.xml.transform.dom.DOMSource)87 TransformerFactory (javax.xml.transform.TransformerFactory)80 SAXException (org.xml.sax.SAXException)53 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)46 StreamSource (javax.xml.transform.stream.StreamSource)41 Source (javax.xml.transform.Source)37 Document (org.w3c.dom.Document)36 StringWriter (java.io.StringWriter)33 TransformerHandler (javax.xml.transform.sax.TransformerHandler)26 InputStream (java.io.InputStream)25 File (java.io.File)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)23 TransformerFactoryConfigurationError (javax.xml.transform.TransformerFactoryConfigurationError)22 Element (org.w3c.dom.Element)21 Result (javax.xml.transform.Result)20