Search in sources :

Example 6 with OutputFormat

use of com.sun.org.apache.xml.internal.serialize.OutputFormat in project tdi-studio-se by Talend.

the class AutoConvertTypesUtils method save.

public static boolean save(List<AutoConversionType> beans, File file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    OutputStreamWriter output = null;
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setErrorHandler(new ErrorHandler() {

            @Override
            public void error(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void fatalError(final SAXParseException exception) throws SAXException {
                throw exception;
            }

            @Override
            public void warning(final SAXParseException exception) throws SAXException {
                throw exception;
            }
        });
        Document document = builder.newDocument();
        //document.setXmlVersion("1.0");//$NON-NLS-1$
        //$NON-NLS-1$
        Element root = document.createElement("mapping");
        document.appendChild(root);
        for (int i = 0; i < beans.size(); i++) {
            AutoConversionType bean = beans.get(i);
            //$NON-NLS-1$
            Element typeNode = document.createElement("conversionType");
            //$NON-NLS-1$
            typeNode.setAttribute("source", bean.getSourceDataType());
            //$NON-NLS-1$
            typeNode.setAttribute("target", bean.getTargetDataType());
            //$NON-NLS-1$
            typeNode.setAttribute("function", bean.getConversionFunction());
            root.appendChild(typeNode);
        }
        // save into file
        if (document != null) {
            XMLSerializer serializer = new XMLSerializer();
            OutputFormat outputFormat = new OutputFormat();
            outputFormat.setIndenting(true);
            serializer.setOutputFormat(outputFormat);
            output = new OutputStreamWriter(new FileOutputStream(file));
            serializer.setOutputCharStream(output);
            serializer.serialize(document);
            // update
            beanList = new ArrayList<>();
            beanList.addAll(beans);
        }
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } finally {
        if (output != null) {
            output.close();
        }
    }
    return true;
}
Also used : ErrorHandler(org.xml.sax.ErrorHandler) XMLSerializer(com.sun.org.apache.xml.internal.serialize.XMLSerializer) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Element(org.w3c.dom.Element) OutputFormat(com.sun.org.apache.xml.internal.serialize.OutputFormat) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) AutoConversionType(org.talend.core.model.metadata.types.AutoConversionType) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

OutputFormat (com.sun.org.apache.xml.internal.serialize.OutputFormat)6 XMLSerializer (com.sun.org.apache.xml.internal.serialize.XMLSerializer)6 Document (org.w3c.dom.Document)5 DocumentBuilder (javax.xml.parsers.DocumentBuilder)3 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)3 Element (org.w3c.dom.Element)3 ErrorHandler (org.xml.sax.ErrorHandler)3 SAXException (org.xml.sax.SAXException)3 SAXParseException (org.xml.sax.SAXParseException)3 FileOutputStream (java.io.FileOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Attr (org.w3c.dom.Attr)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileWriter (java.io.FileWriter)1 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Path (org.eclipse.core.runtime.Path)1 Bundle (org.osgi.framework.Bundle)1