Search in sources :

Example 26 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 27 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project tika by apache.

the class TikaResource method produceOutput.

private StreamingOutput produceOutput(final InputStream is, final MultivaluedMap<String, String> httpHeaders, final UriInfo info, final String format) {
    final Parser parser = createParser();
    final Metadata metadata = new Metadata();
    final ParseContext context = new ParseContext();
    fillMetadata(parser, metadata, context, httpHeaders);
    fillParseContext(context, httpHeaders, parser);
    logRequest(LOG, info, metadata);
    return new StreamingOutput() {

        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            Writer writer = new OutputStreamWriter(outputStream, UTF_8);
            ContentHandler content;
            try {
                SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
                TransformerHandler handler = factory.newTransformerHandler();
                handler.getTransformer().setOutputProperty(OutputKeys.METHOD, format);
                handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
                handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
                handler.setResult(new StreamResult(writer));
                content = new ExpandedTitleContentHandler(handler);
            } catch (TransformerConfigurationException e) {
                throw new WebApplicationException(e);
            }
            parse(parser, LOG, info.getPath(), is, content, metadata, context);
        }
    };
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) Metadata(org.apache.tika.metadata.Metadata) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) StreamingOutput(javax.ws.rs.core.StreamingOutput) BoilerpipeContentHandler(org.apache.tika.parser.html.BoilerpipeContentHandler) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) RichTextContentHandler(org.apache.tika.sax.RichTextContentHandler) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler) Parser(org.apache.tika.parser.Parser) HtmlParser(org.apache.tika.parser.html.HtmlParser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DigestingParser(org.apache.tika.parser.DigestingParser) ParseContext(org.apache.tika.parser.ParseContext) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 28 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 29 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project sling by apache.

the class SimpleXmlSerializationManager method buildSerializationData.

@Override
public SerializationData buildSerializationData(File contentSyncRoot, ResourceProxy resource) throws SerializationException {
    if (resource == null) {
        return null;
    }
    Map<String, Object> content = resource.getProperties();
    if (content == null || content.isEmpty()) {
        return null;
    }
    try {
        SAXTransformerFactory f = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        StreamResult sr = new StreamResult(result);
        TransformerHandler handler = f.newTransformerHandler();
        Transformer t = handler.getTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        handler.setResult(sr);
        handler.startDocument();
        startElement(handler, TAG_RESOURCE);
        Set<Entry<String, Object>> entrySet = new TreeMap<>(content).entrySet();
        for (Map.Entry<String, Object> property : entrySet) {
            Object value = property.getValue();
            if (value instanceof String) {
                String tagName = property.getKey();
                String tagValue = (String) value;
                AttributesImpl attributes = new AttributesImpl();
                attributes.addAttribute("", ATT_PROPERTY_NAME, ATT_PROPERTY_NAME, null, tagName);
                handler.startElement("", TAG_PROPERTY, TAG_PROPERTY, attributes);
                handler.characters(tagValue.toCharArray(), 0, tagValue.length());
                handler.endElement("", TAG_PROPERTY, TAG_PROPERTY);
            } else {
                // TODO multi-valued properties, other primitives
                System.err.println("Can't yet handle property " + property.getKey() + " of type " + value.getClass());
            }
        }
        endElement(handler, TAG_RESOURCE);
        handler.endDocument();
        // TODO - also add the serialization type
        return new SerializationData(resource.getPath(), CONTENT_XML, result.toByteArray(), null);
    } catch (TransformerConfigurationException | TransformerFactoryConfigurationError | SAXException e) {
        // TODO proper exception handling
        throw new RuntimeException(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SAXException(org.xml.sax.SAXException) Entry(java.util.Map.Entry) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SerializationData(org.apache.sling.ide.serialization.SerializationData) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 30 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project sling by apache.

the class AbstractTraxSerializerFactory method createSerializer.

/**
     * @see org.apache.sling.rewriter.SerializerFactory#createSerializer()
     */
public Serializer createSerializer() {
    TransformerHandler tHandler = null;
    try {
        tHandler = this.tfactory.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
        logger.error("Unable to create new transformer handler.", e);
    }
    final ContentHandler ch;
    if (this.needsNamespacesAsAttributes) {
        final NamespaceAsAttributes nsPipeline = new NamespaceAsAttributes(tHandler, this.logger);
        ch = nsPipeline;
    } else {
        ch = tHandler;
    }
    return new TraxSerializer(tHandler, ch, getOutputFormat(), getDoctypePublic(), getDoctypeSystem());
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ContentHandler(org.xml.sax.ContentHandler)

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