Search in sources :

Example 31 with TransformerException

use of javax.xml.transform.TransformerException in project lucene-solr by apache.

the class LibVersionsCheckTask method xmlToString.

private String xmlToString(File ivyXmlFile) {
    StringWriter writer = new StringWriter();
    try {
        StreamSource inputSource = new StreamSource(new FileInputStream(ivyXmlFile.getPath()));
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.transform(inputSource, new StreamResult(writer));
    } catch (TransformerException | IOException e) {
        throw new BuildException("Exception reading " + ivyXmlFile.getPath() + ": " + e.toString(), e);
    }
    return writer.toString();
}
Also used : Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) FileInputStream(java.io.FileInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 32 with TransformerException

use of javax.xml.transform.TransformerException in project poi by apache.

the class StreamHelper method saveXmlInStream.

/**
	 * Save the document object in the specified output stream.
	 *
	 * @param xmlContent
	 *            The XML document.
	 * @param outStream
	 *            The OutputStream in which the XML document will be written.
	 * @return <b>true</b> if the xml is successfully written in the stream,
	 *         else <b>false</b>.
	 */
public static boolean saveXmlInStream(Document xmlContent, OutputStream outStream) {
    try {
        Transformer trans = getIdentityTransformer();
        Source xmlSource = new DOMSource(xmlContent);
        // prevent close of stream by transformer:
        Result outputTarget = new StreamResult(new FilterOutputStream(outStream) {

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                out.write(b, off, len);
            }

            @Override
            public void close() throws IOException {
                // only flush, don't close!
                out.flush();
            }
        });
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.INDENT, "yes");
        trans.setOutputProperty(OutputKeys.STANDALONE, "yes");
        trans.transform(xmlSource, outputTarget);
    } catch (TransformerException e) {
        return false;
    }
    return true;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) FilterOutputStream(java.io.FilterOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 33 with TransformerException

use of javax.xml.transform.TransformerException in project jmeter by apache.

the class XPathUtil method getValueForNode.

/**
     * Return value for node
     * @param node Node
     * @return String
     */
private static String getValueForNode(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException e) {
        sw.write(e.getMessageAndLocation());
    }
    return sw.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Example 34 with TransformerException

use of javax.xml.transform.TransformerException in project jackrabbit by apache.

the class WebdavResponseImpl method sendXmlResponse.

/**
     * Send Xml response body.
     *
     * @param serializable
     * @param status
     * @throws IOException
     * @see DavServletResponse#sendXmlResponse(XmlSerializable, int)
     */
public void sendXmlResponse(XmlSerializable serializable, int status) throws IOException {
    httpResponse.setStatus(status);
    if (serializable != null) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            Document doc = DomUtil.createDocument();
            doc.appendChild(serializable.toXml(doc));
            // JCR-2636: Need to use an explicit OutputStreamWriter
            // instead of relying on the built-in UTF-8 serialization
            // to avoid problems with surrogate pairs on Sun JRE 1.5.
            Writer writer = new OutputStreamWriter(out, "UTF-8");
            DomUtil.transformDocument(doc, writer);
            writer.flush();
            // TODO: Should this be application/xml? See JCR-1621
            httpResponse.setContentType("text/xml; charset=UTF-8");
            httpResponse.setContentLength(out.size());
            out.writeTo(httpResponse.getOutputStream());
        } catch (ParserConfigurationException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        } catch (TransformerException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        } catch (SAXException e) {
            log.error(e.getMessage());
            throw new IOException(e.getMessage());
        }
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) Document(org.w3c.dom.Document) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 35 with TransformerException

use of javax.xml.transform.TransformerException in project intellij-community by JetBrains.

the class IntentionDump method main.

@Override
public void main(String[] args) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        Element intentions = document.createElement("Intentions");
        document.appendChild(intentions);
        while (((IntentionManagerImpl) IntentionManager.getInstance()).hasActiveRequests()) {
            TimeoutUtil.sleep(100);
        }
        for (IntentionActionMetaData actionMetaData : IntentionManagerSettings.getInstance().getMetaData()) {
            Element intention = document.createElement("Intention");
            intention.setAttribute("categories", StringUtil.join(actionMetaData.myCategory, ","));
            intention.setAttribute("name", actionMetaData.getFamily());
            Element description = document.createElement("description");
            CDATASection descriptionSection = document.createCDATASection(escapeCDATA(actionMetaData.getDescription().getText()));
            description.appendChild(descriptionSection);
            intention.appendChild(description);
            TextDescriptor[] beforeDescriptors = actionMetaData.getExampleUsagesBefore();
            if (beforeDescriptors.length > 0) {
                Element before = document.createElement("before");
                CDATASection beforeSection = document.createCDATASection(escapeCDATA(beforeDescriptors[0].getText()));
                before.appendChild(beforeSection);
                intention.appendChild(before);
            }
            TextDescriptor[] afterDescriptors = actionMetaData.getExampleUsagesAfter();
            if (afterDescriptors.length > 0) {
                Element after = document.createElement("after");
                CDATASection afterSection = document.createCDATASection(escapeCDATA(afterDescriptors[0].getText()));
                after.appendChild(afterSection);
                intention.appendChild(after);
            }
            intentions.appendChild(intention);
        }
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(document);
        final String path = args.length == 2 ? args[1] : PathManager.getHomePath() + File.separator + "AllIntentions.xml";
        StreamResult console = new StreamResult(new File(path));
        transformer.transform(source, console);
        System.exit(0);
    } catch (ParserConfigurationException | IOException | TransformerException e) {
        // noinspection CallToPrintStackTrace
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) TextDescriptor(com.intellij.codeInsight.intention.impl.config.TextDescriptor) IOException(java.io.IOException) Document(org.w3c.dom.Document) IntentionManagerImpl(com.intellij.codeInsight.intention.impl.config.IntentionManagerImpl) CDATASection(org.w3c.dom.CDATASection) DocumentBuilder(javax.xml.parsers.DocumentBuilder) IntentionActionMetaData(com.intellij.codeInsight.intention.impl.config.IntentionActionMetaData) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59