Search in sources :

Example 71 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project apex-core by apache.

the class DTConfiguration method writeToFile.

public void writeToFile(File file, Scope scope, String comment) throws IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    Date date = new Date();
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException ex) {
        throw new RuntimeException(ex);
    }
    Element rootElement = doc.createElement("configuration");
    rootElement.appendChild(doc.createComment(" WARNING: Do not edit this file. Your changes will be overwritten. "));
    rootElement.appendChild(doc.createComment(" Written by dtgateway on " + sdf.format(date)));
    rootElement.appendChild(doc.createComment(" " + comment + " "));
    doc.appendChild(rootElement);
    for (Map.Entry<String, ValueEntry> entry : map.entrySet()) {
        ValueEntry valueEntry = entry.getValue();
        if (scope == null || valueEntry.scope == scope) {
            Element property = doc.createElement("property");
            rootElement.appendChild(property);
            Element name = doc.createElement("name");
            name.appendChild(doc.createTextNode(entry.getKey()));
            property.appendChild(name);
            Element value = doc.createElement("value");
            value.appendChild(doc.createTextNode(valueEntry.value));
            property.appendChild(value);
            if (valueEntry.description != null) {
                Element description = doc.createElement("description");
                description.appendChild(doc.createTextNode(valueEntry.description));
                property.appendChild(description);
            }
            if (valueEntry.isFinal) {
                Element isFinal = doc.createElement("final");
                isFinal.appendChild(doc.createTextNode("true"));
                property.appendChild(isFinal);
            }
        }
    }
    rootElement.appendChild(doc.createComment(" WARNING: Do not edit this file. Your changes will be overwritten. "));
    // write the content into xml file
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(file);
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(source, result);
    } catch (TransformerConfigurationException ex) {
        throw new RuntimeException(ex);
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SimpleDateFormat(java.text.SimpleDateFormat) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TransformerException(javax.xml.transform.TransformerException)

Example 72 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project jdk8u_jdk by JetBrains.

the class TransformXSLT method enginePerformTransform.

protected XMLSignatureInput enginePerformTransform(XMLSignatureInput input, OutputStream baos, Transform transformObject) throws IOException, TransformationException {
    try {
        Element transformElement = transformObject.getElement();
        Element xsltElement = XMLUtils.selectNode(transformElement.getFirstChild(), XSLTSpecNS, "stylesheet", 0);
        if (xsltElement == null) {
            Object[] exArgs = { "xslt:stylesheet", "Transform" };
            throw new TransformationException("xml.WrongContent", exArgs);
        }
        TransformerFactory tFactory = TransformerFactory.newInstance();
        // Process XSLT stylesheets in a secure manner
        tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
        /*
             * This transform requires an octet stream as input. If the actual
             * input is an XPath node-set, then the signature application should
             * attempt to convert it to octets (apply Canonical XML]) as described
             * in the Reference Processing Model (section 4.3.3.2).
             */
        Source xmlSource = new StreamSource(new ByteArrayInputStream(input.getBytes()));
        Source stylesheet;
        /*
             * This complicated transformation of the stylesheet itself is necessary
             * because of the need to get the pure style sheet. If we simply say
             * Source stylesheet = new DOMSource(this.xsltElement);
             * whereby this.xsltElement is not the rootElement of the Document,
             * this causes problems;
             * so we convert the stylesheet to byte[] and use this as input stream
             */
        {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            Transformer transformer = tFactory.newTransformer();
            DOMSource source = new DOMSource(xsltElement);
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
            stylesheet = new StreamSource(new ByteArrayInputStream(os.toByteArray()));
        }
        Transformer transformer = tFactory.newTransformer(stylesheet);
        // implementations.
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n");
        } catch (Exception e) {
            log.log(java.util.logging.Level.WARNING, "Unable to set Xalan line-separator property: " + e.getMessage());
        }
        if (baos == null) {
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            StreamResult outputTarget = new StreamResult(baos1);
            transformer.transform(xmlSource, outputTarget);
            return new XMLSignatureInput(baos1.toByteArray());
        }
        StreamResult outputTarget = new StreamResult(baos);
        transformer.transform(xmlSource, outputTarget);
        XMLSignatureInput output = new XMLSignatureInput((byte[]) null);
        output.setOutputStream(baos);
        return output;
    } catch (XMLSecurityException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    } catch (TransformerConfigurationException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    } catch (TransformerException ex) {
        Object[] exArgs = { ex.getMessage() };
        throw new TransformationException("generic.EmptyMessage", exArgs, ex);
    }
}
Also used : TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) 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) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) XMLSignatureInput(com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) TransformationException(com.sun.org.apache.xml.internal.security.transforms.TransformationException) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 73 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project tdi-studio-se by Talend.

the class HTMLDocGenerator method generateXslFile.

private void generateXslFile(String resource, String xslfile, String cssfile, String folder) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document document = builder.parse(new File(resource));
        org.w3c.dom.Element rootElement = document.getDocumentElement();
        //$NON-NLS-1$
        NodeList list = rootElement.getElementsByTagName("style");
        org.w3c.dom.Element element = (org.w3c.dom.Element) list.item(0);
        String value = element.getChildNodes().item(0).getNodeValue();
        if (value != null) {
            if (folder != null) {
                //$NON-NLS-1$
                CSSParserUtils.createCssFile(value, folder + File.separator + "default.css");
            }
            if (cssfile != null && !cssfile.equals("")) {
                //$NON-NLS-1$
                if (folder != null) {
                    String cssName = new File(cssfile).getName();
                    if (cssName.equalsIgnoreCase("default.css")) {
                        //$NON-NLS-1$
                        //$NON-NLS-1$
                        cssName = "User_" + cssName;
                    }
                    File file = new File(folder + File.separator + cssName);
                    if (!file.exists()) {
                        file.createNewFile();
                    }
                    FileCopyUtils.copy(cssfile, folder + File.separator + cssName);
                }
                CSSRuleList ruleList = CSSParserUtils.parserCSSSelectors(null, cssfile);
                if (ruleList == null) {
                    return;
                } else {
                    String newValue = CSSParserUtils.generateCssStyle(cssfile, ruleList, value);
                    element.getChildNodes().item(0).setNodeValue(newValue);
                    // replace the old value and generate a new xsl file
                    DOMSource ds = new DOMSource(document);
                    StreamResult sr = new StreamResult(new File(xslfile));
                    TransformerFactory.newInstance().newTransformer().transform(ds, sr);
                }
            }
        }
    } catch (ParserConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (SAXException e) {
        ExceptionHandler.process(e);
    } catch (IOException e) {
        ExceptionHandler.process(e);
    } catch (TransformerConfigurationException e) {
        ExceptionHandler.process(e);
    } catch (TransformerException e) {
        ExceptionHandler.process(e);
    } catch (TransformerFactoryConfigurationError e) {
        ExceptionHandler.process(e);
    }
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NodeList(org.w3c.dom.NodeList) Element(org.dom4j.Element) IOException(java.io.IOException) CSSRuleList(org.w3c.dom.css.CSSRuleList) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 74 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project tdi-studio-se by Talend.

the class ComponentFolderManager method writeXMLContent.

/**
     * DOC slanglois Comment method "writeXMLContent".
     * 
     * @param iFile
     * @param document
     * @param enCode
     * @throws CoreException
     */
private void writeXMLContent(IFile iFile, Document document, String enCode) throws CoreException {
    PrintWriter pw = null;
    XMLWriter writer = null;
    byte[] byteArray = null;
    // get xml content as inputstream
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {
        transformer = tf.newTransformer();
    } catch (TransformerConfigurationException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    DOMSource source = new DOMSource(document);
    transformer.setOutputProperty(OutputKeys.ENCODING, enCode);
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    ByteArrayOutputStream sw = new ByteArrayOutputStream();
    pw = new PrintWriter(sw);
    StreamResult result = new StreamResult(pw);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        sw.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (pw != null) {
            pw.close();
        }
    }
    byteArray = sw.toByteArray();
    // format the xml content
    SAXReader saxReader = new SAXReader();
    org.dom4j.Document dom4jDocument = null;
    try {
        dom4jDocument = saxReader.read(new ByteArrayInputStream(byteArray));
    } catch (DocumentException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    /** format the output like the webBrowser */
    OutputFormat format = OutputFormat.createPrettyPrint();
    /** give the xml encoding */
    format.setEncoding(enCode);
    sw = new ByteArrayOutputStream();
    try {
        writer = new XMLWriter(sw, format);
    } catch (UnsupportedEncodingException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    }
    try {
        writer.write(dom4jDocument);
        writer.flush();
    } catch (IOException e1) {
        // e1.printStackTrace();
        org.talend.componentdesigner.exception.ExceptionHandler.process(e1);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // e.printStackTrace();
                org.talend.componentdesigner.exception.ExceptionHandler.process(e);
            }
        }
    }
    byteArray = sw.toByteArray();
    // write content
    iFile.setContents(new ByteArrayInputStream(byteArray), true, false, null);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) SAXReader(org.dom4j.io.SAXReader) OutputFormat(org.dom4j.io.OutputFormat) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) XMLWriter(org.dom4j.io.XMLWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) DocumentException(org.dom4j.DocumentException) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 75 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project ddf by codice.

the class TikaInputTransformer method classLoaderAndBundleContextSetup.

private void classLoaderAndBundleContextSetup(BundleContext bundleContext) {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    try (InputStream stream = TikaMetadataExtractor.class.getResourceAsStream("/metadata.xslt")) {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        templates = TransformerFactory.newInstance(net.sf.saxon.TransformerFactoryImpl.class.getName(), net.sf.saxon.TransformerFactoryImpl.class.getClassLoader()).newTemplates(new StreamSource(stream));
    } catch (TransformerConfigurationException e) {
        LOGGER.debug("Couldn't create XML transformer", e);
    } catch (IOException e) {
        LOGGER.debug("Could not get Tiki metadata XSLT", e);
    } finally {
        Thread.currentThread().setContextClassLoader(tccl);
    }
    if (bundleContext == null) {
        LOGGER.info("Bundle context is null. Unable to register {} as an osgi service.", TikaInputTransformer.class.getSimpleName());
        return;
    }
    registerService(bundleContext);
    IIORegistry.getDefaultInstance().registerServiceProvider(new J2KImageReaderSpi());
    IIORegistry.getDefaultInstance().registerServiceProvider(new TIFFImageReaderSpi());
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) CloseShieldInputStream(org.apache.tika.io.CloseShieldInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) J2KImageReaderSpi(com.github.jaiimageio.jpeg2000.impl.J2KImageReaderSpi) TIFFImageReaderSpi(com.github.jaiimageio.impl.plugins.tiff.TIFFImageReaderSpi)

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