Search in sources :

Example 46 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project knime-core by knime.

the class WorkflowSetMetaInfoEditor method doSave.

/**
 * {@inheritDoc}
 */
@Override
public void doSave(final IProgressMonitor monitor) {
    try {
        URI path = ((IURIEditorInput) getEditorInput()).getURI();
        File inputFile = new File(path);
        SAXTransformerFactory fac = (SAXTransformerFactory) TransformerFactory.newInstance();
        TransformerHandler handler = fac.newTransformerHandler();
        Transformer t = handler.getTransformer();
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        OutputStream out = new FileOutputStream(inputFile);
        handler.setResult(new StreamResult(out));
        handler.startDocument();
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute(null, null, "nrOfElements", "CDATA", "" + m_elements.size());
        handler.startElement(null, null, "KNIMEMetaInfo", atts);
        monitor.beginTask("Saving meta information...", m_elements.size());
        for (MetaGUIElement element : m_elements) {
            element.saveTo(handler);
            monitor.worked(1);
        }
        handler.endElement(null, null, "KNIMEMetaInfo");
        handler.endDocument();
        out.close();
        setDirty(false);
    } catch (Exception e) {
        LOGGER.error("An error ocurred while saving " + getEditorInput().toString(), e);
    } finally {
        monitor.done();
    }
}
Also used : MetaGUIElement(org.knime.workbench.ui.metainfo.model.MetaGUIElement) IURIEditorInput(org.eclipse.ui.IURIEditorInput) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StreamResult(javax.xml.transform.stream.StreamResult) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) URI(java.net.URI) File(java.io.File) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)46 StreamResult (javax.xml.transform.stream.StreamResult)33 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)29 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 Transformer (javax.xml.transform.Transformer)16 SAXException (org.xml.sax.SAXException)13 StringWriter (java.io.StringWriter)11 AttributesImpl (org.xml.sax.helpers.AttributesImpl)11 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 Metadata (org.apache.tika.metadata.Metadata)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 OutputStream (java.io.OutputStream)5 FileOutputStream (java.io.FileOutputStream)4 TransformerException (javax.xml.transform.TransformerException)4 ParseContext (org.apache.tika.parser.ParseContext)4 Test (org.junit.Test)4 OutputStreamWriter (java.io.OutputStreamWriter)3