Example usage for org.dom4j.io XMLWriter write

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

Introduction

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

Prototype

public void write(Object object) throws IOException 

Source Link

Document

Writes the given object which should be a String, a Node or a List of Nodes.

Usage

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// w  w  w.ja  v  a  2 s  .  co 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  . jav a  2  s . co 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();
}

From source file:org.apache.directory.studio.ldapbrowser.core.BrowserConnectionIO.java

License:Apache License

/**
 * Saves the browser connections using the output stream.
 *
 * @param stream//from  ww w . j  a v  a2 s  .  com
 *      the OutputStream
 * @param browserConnectionMap
 *      the map of browser connections
 * @throws IOException
 *      if an I/O error occurs
 */
public static void save(OutputStream stream, Map<String, IBrowserConnection> browserConnectionMap)
        throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

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

    if (browserConnectionMap != null) {
        for (IBrowserConnection browserConnection : browserConnectionMap.values()) {
            writeBrowserConnection(root, browserConnection);
        }
    }

    // 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.ldapservers.LdapServersManagerIO.java

License:Apache License

/**
 * Writes the list of servers to the given stream.
 *
 * @param servers/*w w w. j  a  v a 2s.c  o  m*/
 *      the servers
 * @param outputStream
 *      the output stream
 * @throws IOException
 *      if an error occurs when writing to the stream
 */
public static void write(List<LdapServer> servers, OutputStream outputStream) throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

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

    if (servers != null) {
        for (LdapServer server : servers) {
            addLdapServer(server, root);
        }
    }

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

From source file:org.apache.directory.studio.schemaeditor.model.io.XMLSchemaFileExporter.java

License:Apache License

/**
 * Converts the given schema to its source code representation
 * in XML file format.// ww  w.j  a  v  a 2 s .  c om
 *
 * @param schema
 *      the schema to convert
 * @return
 *      the corresponding source code representation
 * @throws IOException
 */
public static String toXml(Schema schema) throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

    // Adding the schema
    addSchema(schema, document);

    // Creating the output stream we're going to put the XML in
    OutputStream os = new ByteArrayOutputStream();
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding("UTF-8"); //$NON-NLS-1$

    // Writing the XML.
    XMLWriter writer = new XMLWriter(os, outformat);
    writer.write(document);
    writer.flush();
    writer.close();

    return os.toString();
}

From source file:org.apache.directory.studio.schemaeditor.model.io.XMLSchemaFileExporter.java

License:Apache License

/**
 * Converts the given schemas to their source code representation
 * in one XML file format.// w  ww  . ja  v a2s .c o  m
 *
 * @param schemas
 *      the array of schemas to convert
 * @return
 *      the corresponding source code representation
 * @throws IOException
 */
public static String toXml(Schema[] schemas) throws IOException {
    // Creating the Document and the 'root' Element
    Document document = DocumentHelper.createDocument();

    addSchemas(schemas, document);

    // Creating the output stream we're going to put the XML in
    OutputStream os = new ByteArrayOutputStream();
    OutputFormat outformat = OutputFormat.createPrettyPrint();
    outformat.setEncoding("UTF-8"); //$NON-NLS-1$

    // Writing the XML.
    XMLWriter writer = new XMLWriter(os, outformat);
    writer.write(document);
    writer.flush();
    writer.close();

    return os.toString();
}

From source file:org.apache.directory.studio.schemaeditor.PluginUtils.java

License:Apache License

/**
 * Saves the projects in the Projects File.
 *//*from   w ww . ja  v  a  2  s .  c o  m*/
public static void saveProjects() {
    try {
        // Saving the projects to the temp projects file
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        outformat.setEncoding("UTF-8"); //$NON-NLS-1$
        XMLWriter writer = new XMLWriter(new FileOutputStream(getTempProjectsFile()), outformat);
        writer.write(ProjectsExporter
                .toDocument(Activator.getDefault().getProjectsHandler().getProjects().toArray(new Project[0])));
        writer.flush();

        // Copying the temp projects file to the final location
        String content = FileUtils.readFileToString(getTempProjectsFile(), "UTF-8"); //$NON-NLS-1$
        FileUtils.writeStringToFile(getProjectsFile(), content, "UTF-8"); //$NON-NLS-1$
    } catch (IOException e) {
        // If an error occurs when saving to the temp projects file or
        // when copying the temp projects file to the final location,
        // we try to save the projects directly to the final location.
        try {
            OutputFormat outformat = OutputFormat.createPrettyPrint();
            outformat.setEncoding("UTF-8"); //$NON-NLS-1$
            XMLWriter writer = new XMLWriter(new FileOutputStream(getProjectsFile()), outformat);
            writer.write(ProjectsExporter.toDocument(
                    Activator.getDefault().getProjectsHandler().getProjects().toArray(new Project[0])));
            writer.flush();
        } catch (IOException e2) {
            // If another error occur, we display an error
            reportError(Messages.getString("PluginUtils.ErrorSavingProject"), e2, Messages //$NON-NLS-1$
                    .getString("PluginUtils.ProjectsSavingError"), //$NON-NLS-1$
                    Messages.getString("PluginUtils.ErrorSavingProject")); //$NON-NLS-1$
        }
    }
}

From source file:org.apache.directory.studio.schemaeditor.view.wizards.ExportProjectsWizard.java

License:Apache License

/**
 * {@inheritDoc}/*  w ww .jav a  2s . c  o m*/
 */
public boolean performFinish() {
    // Saving the dialog settings
    page.saveDialogSettings();

    // Getting the projects to be exported and where to export them
    final Project[] selectedProjects = page.getSelectedProjects();
    final String exportDirectory = page.getExportDirectory();
    try {
        getContainer().run(false, false, new IRunnableWithProgress() {
            public void run(IProgressMonitor monitor) {
                monitor.beginTask(Messages.getString("ExportProjectsWizard.ExportingProject"), //$NON-NLS-1$
                        selectedProjects.length);
                for (Project project : selectedProjects) {
                    monitor.subTask(project.getName());

                    try {
                        OutputFormat outformat = OutputFormat.createPrettyPrint();
                        outformat.setEncoding("UTF-8"); //$NON-NLS-1$
                        XMLWriter writer = new XMLWriter(new FileOutputStream(exportDirectory + "/" //$NON-NLS-1$
                                + project.getName() + ".schemaproject"), outformat); //$NON-NLS-1$
                        writer.write(ProjectsExporter.toDocument(project));
                        writer.flush();
                    } catch (UnsupportedEncodingException e) {
                        PluginUtils.logError(
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }),
                                e);
                        ViewUtils.displayErrorMessageDialog(Messages.getString("ExportProjectsWizard.Error"), //$NON-NLS-1$
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }));
                    } catch (FileNotFoundException e) {
                        PluginUtils.logError(
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }),
                                e);
                        ViewUtils.displayErrorMessageDialog(Messages.getString("ExportProjectsWizard.Error"), //$NON-NLS-1$
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }));
                    } catch (IOException e) {
                        PluginUtils.logError(
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }),
                                e);
                        ViewUtils.displayErrorMessageDialog(Messages.getString("ExportProjectsWizard.Error"), //$NON-NLS-1$
                                NLS.bind(Messages.getString("ExportProjectsWizard.ErrorWhenSavingProject"), //$NON-NLS-1$
                                        new String[] { project.getName() }));
                    }
                    monitor.worked(1);
                }
                monitor.done();
            }
        });
    } catch (InvocationTargetException e) {
        // Nothing to do (it will never occur)
    } catch (InterruptedException e) {
        // Nothing to do.
    }

    return true;
}

From source file:org.apache.directory.studio.templateeditor.model.parser.TemplateIO.java

License:Apache License

/**
 * Saves the template using the writer.//from   ww w .  j a  va2  s  . co m
 *
 * @param template
 *      the connections
 * @param stream
 *      the OutputStream
 * @throws IOException
 *      if an I/O error occurs
 */
public static void save(Template template, OutputStream stream) throws IOException {
    // Creating the Document
    Document document = DocumentHelper.createDocument();

    writeTemplate(document, template);

    // 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();
    writer.close();
}

From source file:org.apache.isis.core.runtime.services.memento.Dom4jUtil.java

License:Apache License

static String asString(final Document doc) {
    XMLWriter writer = null;
    final StringWriter sw = new StringWriter();
    try {//from   w w w.  j  av a  2  s .c o m
        // previously this code used pretty print.
        // however, that tripped up on strings with double spaces in them; the double space was normalized 
        // to a single space!
        // OutputFormat outputFormat = OutputFormat.createPrettyPrint();
        // writer = new XMLWriter(sw, outputFormat);
        writer = new XMLWriter(sw);
        writer.write(doc);
        return sw.toString();
    } catch (IOException e) {
        throw new IsisException(e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}