Example usage for org.dom4j.io XMLWriter flush

List of usage examples for org.dom4j.io XMLWriter flush

Introduction

In this page you can find the example usage for org.dom4j.io XMLWriter flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes the underlying Writer

Usage

From source file:net.sf.jvifm.model.BookmarkManager.java

License:Open Source License

@SuppressWarnings("unchecked")
public void store() {

    try {/*  w  w  w. ja  v a  2 s .c  o  m*/

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("bookmarks");

        for (Iterator it = bookmarkList.iterator(); it.hasNext();) {

            Bookmark bm = (Bookmark) it.next();

            Element bookmarkElement = root.addElement("bookmark");

            bookmarkElement.addElement("name").addText(bm.getName());
            bookmarkElement.addElement("path").addText(bm.getPath());
            if (bm.getKey() != null)
                bookmarkElement.addElement("key").addText(bm.getKey());

        }

        FileOutputStream fos = new FileOutputStream(storePath);
        OutputFormat outformat = OutputFormat.createPrettyPrint();

        outformat.setEncoding("UTF-8");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        XMLWriter writer = new XMLWriter(out, outformat);

        writer.write(document);
        writer.flush();
        writer.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.sf.jvifm.model.MimeManager.java

License:Open Source License

@SuppressWarnings("unchecked")
public void store() {

    try {/*ww  w .j  a va  2  s.c om*/
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("mimes");
        for (Iterator it = mimeInfo.keySet().iterator(); it.hasNext();) {
            String postfix = (String) it.next();
            Element filetypeEle = root.addElement("filetype");
            filetypeEle.addAttribute("postfix", postfix);
            List<String> appPathList = mimeInfo.get(postfix);
            for (String appPath : appPathList) {
                filetypeEle.addElement("appPath").addText(appPath);
            }
        }

        FileOutputStream fos = new FileOutputStream(storePath);
        OutputFormat outformat = OutputFormat.createPrettyPrint();

        outformat.setEncoding("UTF-8");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        XMLWriter writer = new XMLWriter(out, outformat);

        writer.write(document);
        writer.flush();
        writer.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.sf.jvifm.model.ShortcutsManager.java

License:Open Source License

public void store() {

    try {/*from  ww  w .j  a v  a2 s. co m*/

        Document document = DocumentHelper.createDocument();
        Element root = document.addElement("commands");

        for (Shortcut shortcut : shortcutsList) {

            Element shortcutElement = root.addElement("command");

            shortcutElement.addElement("name").addText(shortcut.getName());
            shortcutElement.addElement("text").addText(shortcut.getText());

        }

        FileOutputStream fos = new FileOutputStream(storePath);
        OutputFormat outformat = OutputFormat.createPrettyPrint();

        outformat.setEncoding("UTF-8");
        BufferedOutputStream out = new BufferedOutputStream(fos);
        XMLWriter writer = new XMLWriter(out, outformat);

        writer.write(document);
        writer.flush();
        writer.close();
        fos.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:net.sourceforge.sqlexplorer.XMLUtils.java

License:Open Source License

public static void save(Element pRoot, File pFile) {
    try {/*from   w w w .  ja v  a  2 s. c o m*/
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(pFile), OutputFormat.createPrettyPrint());
        xmlWriter.startDocument();
        xmlWriter.write(pRoot);
        xmlWriter.endDocument();
        xmlWriter.flush();
        xmlWriter.close();
    } catch (Exception e) {
        SQLExplorerPlugin.error("Couldn't save: " + pFile.getAbsolutePath(), e);
    }

}

From source file:nl.nn.ibistesttool.PipeDescriptionProvider.java

License:Apache License

/**
 * Get a PipeDescription object for the specified pipe. The returned object
 * is cached./*  w w w  .  jav  a  2s. c  om*/
 */
public PipeDescription getPipeDescription(PipeLine pipeLine, IPipe pipe) {
    PipeDescription pipeDescription;
    String adapterName = pipeLine.getOwner().getName();
    String pipeName = pipe.getName();
    String checkpointName = null;
    String xpathExpression = null;
    if (pipeLine.getPipe(pipeName) == null) {
        if (PipeLine.INPUT_VALIDATOR_NAME.equals(pipeName)) {
            checkpointName = INPUT_VALIDATOR_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/inputValidator";
        } else if (PipeLine.OUTPUT_VALIDATOR_NAME.equals(pipeName)) {
            checkpointName = OUTPUT_VALIDATOR_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/outputValidator";
        } else if (PipeLine.INPUT_WRAPPER_NAME.equals(pipeName)) {
            checkpointName = INPUT_WRAPPER_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/inputWrapper";
        } else if (PipeLine.OUTPUT_WRAPPER_NAME.equals(pipeName)) {
            checkpointName = OUTPUT_WRAPPER_CHECKPOINT_NAME;
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/outputWrapper";
        } else if (pipeName.startsWith(MessageSendingPipe.INPUT_VALIDATOR_NAME_PREFIX)
                && pipeName.endsWith(MessageSendingPipe.INPUT_VALIDATOR_NAME_SUFFIX)) {
            checkpointName = INPUT_VALIDATOR_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.INPUT_VALIDATOR_NAME_PREFIX,
                    MessageSendingPipe.INPUT_VALIDATOR_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\""
                    + parentPipeName + "\"]/inputValidator";
        } else if (pipeName.startsWith(MessageSendingPipe.OUTPUT_VALIDATOR_NAME_PREFIX)
                && pipeName.endsWith(MessageSendingPipe.OUTPUT_VALIDATOR_NAME_SUFFIX)) {
            checkpointName = OUTPUT_VALIDATOR_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.OUTPUT_VALIDATOR_NAME_PREFIX,
                    MessageSendingPipe.OUTPUT_VALIDATOR_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\""
                    + parentPipeName + "\"]/outputValidator";
        } else if (pipeName.startsWith(MessageSendingPipe.INPUT_WRAPPER_NAME_PREFIX)
                && pipeName.endsWith(MessageSendingPipe.INPUT_WRAPPER_NAME_SUFFIX)) {
            checkpointName = INPUT_WRAPPER_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.INPUT_WRAPPER_NAME_PREFIX,
                    MessageSendingPipe.INPUT_WRAPPER_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\""
                    + parentPipeName + "\"]/inputWrapper";
        } else if (pipeName.startsWith(MessageSendingPipe.OUTPUT_WRAPPER_NAME_PREFIX)
                && pipeName.endsWith(MessageSendingPipe.OUTPUT_WRAPPER_NAME_SUFFIX)) {
            checkpointName = OUTPUT_WRAPPER_CHECKPOINT_NAME;
            String parentPipeName = getParentPipeName(pipeName, MessageSendingPipe.OUTPUT_WRAPPER_NAME_PREFIX,
                    MessageSendingPipe.OUTPUT_WRAPPER_NAME_SUFFIX);
            xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\""
                    + parentPipeName + "\"]/outputWrapper";
        }
    } else {
        xpathExpression = "//*/adapter[@name=\"" + adapterName + "\"]/pipeline/pipe[@name=\"" + pipeName
                + "\"]";
    }
    synchronized (pipeDescriptionCaches) {
        // When a configuration is changed (reloaded) a new configuration
        // object will be created. The old configuration object will be
        // removed from pipeDescriptionCaches by the garbage collection as
        // this is a WeakHashMap.
        Configuration configuration = pipeLine.getAdapter().getConfiguration();
        Map<String, PipeDescription> pipeDescriptionCache = pipeDescriptionCaches.get(configuration);
        if (pipeDescriptionCache == null) {
            pipeDescriptionCache = new HashMap<String, PipeDescription>();
            pipeDescriptionCaches.put(configuration, pipeDescriptionCache);
        }
        pipeDescription = pipeDescriptionCache.get(xpathExpression);
        if (pipeDescription == null) {
            pipeDescription = new PipeDescription();
            pipeDescription.setCheckpointName(getCheckpointName(pipe, checkpointName));
            if (xpathExpression == null) {
                pipeDescription.setDescription("Could not create xpath to extract pipe from configuration");
                pipeDescriptionCache.put(xpathExpression, pipeDescription);
            } else {
                Document document = documents.get(configuration);
                if (document == null) {
                    try {
                        document = DocumentHelper.parseText(configuration.getLoadedConfiguration());
                        documents.put(configuration, document);
                    } catch (DocumentException e) {
                        pipeDescription = new PipeDescription();
                        pipeDescription.setCheckpointName(getCheckpointName(pipe, checkpointName));
                        pipeDescription.setDescription("Could not parse configuration: " + e.getMessage());
                        pipeDescriptionCache.put(xpathExpression, pipeDescription);
                    }
                }
                if (document != null) {
                    Node node = document.selectSingleNode(xpathExpression);
                    if (node != null) {
                        StringWriter stringWriter = new StringWriter();
                        OutputFormat outputFormat = OutputFormat.createPrettyPrint();
                        XMLWriter xmlWriter = new XMLWriter(stringWriter, outputFormat);
                        try {
                            xmlWriter.write(node);
                            xmlWriter.flush();
                            pipeDescription.setDescription(stringWriter.toString());
                        } catch (IOException e) {
                            pipeDescription.setDescription("IOException: " + e.getMessage());
                        }
                        addResourceNamesToPipeDescription((Element) node, pipeDescription);
                    } else {
                        pipeDescription.setDescription("Pipe not found in configuration.");
                    }
                }
            }
            pipeDescriptionCache.put(xpathExpression, pipeDescription);
        }
    }
    return pipeDescription;
}

From source file:nl.tue.gale.common.GaleUtil.java

License:Open Source License

public static String serializeXML(Element element) {
    StringWriter out = new StringWriter();
    OutputFormat of = new OutputFormat();
    of.setExpandEmptyElements(true);/*from   w  ww.  ja  va  2s  . c  om*/
    XMLWriter writer = new XMLWriter(out, of) {
        @Override
        protected void writeEmptyElementClose(String qualifiedName) throws IOException {
            if (omitCloseSet.contains(qualifiedName.toUpperCase())) {
                writer.write(" />");
            } else {
                super.writeEmptyElementClose(qualifiedName);
            }
        }
    };
    try {
        writer.write(element);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
        throw new IllegalArgumentException("unable to serialize XML: " + e.getMessage());
    }
    return out.toString();
}

From source file:org.alfresco.repo.security.permissions.impl.model.PermissionModel.java

License:Open Source License

private InputStream processModelDocType(InputStream is, String dtdSchemaUrl)
        throws DocumentException, IOException {
    SAXReader reader = new SAXReader();
    // read document without validation
    Document doc = reader.read(is);
    DocumentType docType = doc.getDocType();
    if (docType != null) {
        // replace DOCTYPE setting the full path to the xsd
        docType.setSystemID(dtdSchemaUrl);
    } else {//from w ww . j a  v a2 s.c om
        // add the DOCTYPE
        docType = new DefaultDocumentType(doc.getRootElement().getName(), dtdSchemaUrl);
        doc.setDocType(docType);
    }

    ByteArrayOutputStream fos = new ByteArrayOutputStream();
    try {
        OutputFormat format = OutputFormat.createPrettyPrint(); // uses UTF-8
        XMLWriter writer = new XMLWriter(fos, format);
        writer.write(doc);
        writer.flush();
    } finally {
        fos.close();
    }

    return new ByteArrayInputStream(fos.toByteArray());
}

From source file:org.apache.cxf.jaxrs.provider.dom4j.DOM4JProvider.java

License:Apache License

public void writeTo(org.dom4j.Document doc, Class<?> cls, Type type, Annotation[] anns, MediaType mt,
        MultivaluedMap<String, Object> headers, OutputStream os) throws IOException, WebApplicationException {
    if (!convertAlways && mt.getSubtype().contains("xml")) {

        XMLWriter writer;
        if (MessageUtils.getContextualBoolean(getCurrentMessage(), SUPPRESS_XML_DECLARATION, false)) {
            OutputFormat format = new org.dom4j.io.OutputFormat();
            format.setSuppressDeclaration(true);
            writer = new org.dom4j.io.XMLWriter(os, format);
        } else {/* w w w.j  av a  2s  .co  m*/
            writer = new org.dom4j.io.XMLWriter(os);
        }
        writer.write(doc);
        writer.flush();
    } else {
        org.w3c.dom.Document domDoc = convertToDOM(doc);

        MessageBodyWriter<org.w3c.dom.Document> writer = providers.getMessageBodyWriter(DOM_DOC_CLS,
                DOM_DOC_CLS, anns, mt);
        if (writer == null) {
            throw ExceptionUtils.toNotAcceptableException(null, null);
        }
        writer.writeTo(domDoc, DOM_DOC_CLS, DOM_DOC_CLS, anns, mt, headers, os);
    }
}

From source file:org.apache.directory.studio.connection.core.io.ConnectionIO.java

License:Apache License

/**
 * Saves the connections using the writer.
 *
 * @param connections the connections/*from   w  w  w.  j  a v  a  2  s . c  o m*/
 * @param stream the OutputStream
 * @throws IOException if an I/O error occurs
 */
public static void save(Set<ConnectionParameter> connections, OutputStream stream) throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

    // Creating the root element
    Element root = document.addElement(CONNECTIONS_TAG);

    if (connections != null) {
        for (ConnectionParameter connection : connections) {
            addConnection(root, connection);
        }
    }

    // Writing the file to disk
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding("UTF-8"); //$NON-NLS-1$
    XMLWriter writer = new XMLWriter(stream, outformat);
    writer.write(document);
    writer.flush();
}

From source file:org.apache.directory.studio.connection.core.io.ConnectionIO.java

License:Apache License

/**
 * Saves the connection folders using the writer.
 *
 * @param connectionFolders the connection folders
 * @param stream the OutputStream/* w ww  . ja  v a 2s.c  o  m*/
 * @throws IOException if an I/O error occurs
 */
public static void saveConnectionFolders(Set<ConnectionFolder> connectionFolders, OutputStream stream)
        throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

    // Creating the root element
    Element root = document.addElement(CONNECTION_FOLDERS_TAG);

    if (connectionFolders != null) {
        for (ConnectionFolder connectionFolder : connectionFolders) {
            addFolderConnection(root, connectionFolder);
        }
    }

    // Writing the file to disk
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding("UTF-8"); //$NON-NLS-1$
    XMLWriter writer = new XMLWriter(stream, outformat);
    writer.write(document);
    writer.flush();
}