Example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter

List of usage examples for javax.xml.stream XMLOutputFactory createXMLStreamWriter

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter.

Prototype

public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamWriter that writes to a JAXP result.

Usage

From source file:org.wso2.carbon.registry.core.jdbc.Repository.java

protected void dumpLite(String _path, Writer writer) throws RegistryException {
    String path = _path;/*  w ww . j a v  a 2s  .c  o m*/
    if (!path.equals("/") && path.endsWith("/")) {
        // remove the / suffix
        path = path.substring(0, path.length() - 1);
    }

    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursivelyLight(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RegistryException(msg, e);
    }
}

From source file:org.wso2.carbon.registry.synchronization.Utils.java

private static void addMetadata(String metaFilePath, String fileName, boolean isCollection, String registryPath,
        String registryUrl, boolean root) throws SynchronizationException {
    FileWriter writer = null;//from   ww  w .j  a  va2 s  .  com
    File metaDir;
    File file = new File(metaFilePath);
    if (file.exists()) {
        return;
    }
    try {
        metaDir = new File(file.getParent());
        metaDir.mkdirs();
        file.createNewFile();
        writer = new FileWriter(file);
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlWriter = xof.createXMLStreamWriter(writer);
        xmlWriter.writeStartElement("resource");
        xmlWriter.writeAttribute("name", fileName);
        xmlWriter.writeAttribute("isCollection", String.valueOf(isCollection));
        xmlWriter.writeAttribute("path", registryPath);
        if (registryUrl != null) {
            xmlWriter.writeAttribute("registryUrl", registryUrl);
        }
        xmlWriter.writeAttribute("status", "added");
        xmlWriter.writeEndElement();
        xmlWriter.flush();
    } catch (Exception e) {
        throw new SynchronizationException(MessageCode.ERROR_IN_ADDING_METADATA);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Failed to close the stream", e);
            }
        }
    }
}

From source file:org.wso2.carbon.registry.synchronization.Utils.java

private static void setDelete(String metaFilePath) throws SynchronizationException {
    File metaFile = new File(metaFilePath);
    OMElement resourceElement;//from   ww w.j a  v a 2s  .  c  o m
    FileWriter writer = null;
    try {
        resourceElement = new StAXOMBuilder(new FileInputStream(metaFile)).getDocumentElement();
        resourceElement.addAttribute("status", "deleted", null);
        writer = new FileWriter(metaFile);
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlWriter = xof.createXMLStreamWriter(writer);
        resourceElement.serialize(xmlWriter);
        xmlWriter.flush();

    } catch (FileNotFoundException e) {
        throw new SynchronizationException(MessageCode.RESOURCE_NOT_UNDER_REGISTRY_CONTROL);
    } catch (Exception e) {
        throw new SynchronizationException(MessageCode.RESOURCE_METADATA_CORRUPTED);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                log.error("Failed to close the stream", e);
            }
        }
    }
}

From source file:org.wso2.carbon.repository.core.ResourceStorer.java

/**
 * Method to do a dump.//from www . j  a v a 2 s.  c o  m
 *
 * @param _path   the path to obtain the dump from.
 * @param writer the writer used.
 *
 * @throws RepositoryException if the operation failed.
 */
public void dump(String _path, Writer writer) throws RepositoryException {
    String path = _path;

    if (!path.equals("/") && path.endsWith("/")) {
        path = path.substring(0, path.length() - 1);
    }

    XMLStreamWriter xmlWriter = null;
    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursively(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RepositoryException(msg, e);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (XMLStreamException e) {
            }
        }
    }
}

From source file:org.xronos.orcc.analysis.XronosDynamicWeights.java

public void getMeanWeights(String outputPath) {

    if (getModelsimWeights()) {
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        try {/*from   ww  w  .  java 2 s. c  o  m*/
            XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter(
                    outputPath + File.separator + network.getSimpleName() + "_dynamicWeights.xml"));
            writer.writeStartDocument();
            writer.writeStartElement("actors");
            for (Actor actor : statistics.keySet()) {
                writer.writeStartElement("actor");
                writer.writeAttribute("name", actor.getSimpleName().toLowerCase());
                Map<Action, SummaryStatistics> actionWeight = statistics.get(actor);
                writer.writeStartElement("actions");
                for (Action action : actionWeight.keySet()) {
                    writer.writeStartElement("action");
                    writer.writeAttribute("name", action.getName().toLowerCase());

                    double min = Double.isNaN(actionWeight.get(action).getMin()) ? 0
                            : actionWeight.get(action).getMin();
                    double mean = Double.isNaN(actionWeight.get(action).getMean()) ? 0
                            : actionWeight.get(action).getMean();
                    double max = Double.isNaN(actionWeight.get(action).getMax()) ? 0
                            : actionWeight.get(action).getMax();
                    double variance = Double.isNaN(actionWeight.get(action).getVariance()) ? 0
                            : actionWeight.get(action).getVariance();

                    writer.writeAttribute("min", Double.toString(min));
                    writer.writeAttribute("mean", Double.toString(mean));
                    writer.writeAttribute("max", Double.toString(max));
                    writer.writeAttribute("variance", Double.toString(variance));

                    writer.writeEndElement();
                }
                writer.writeEndElement();
                writer.writeEndElement();
            }
            writer.writeEndElement();
            writer.writeEndDocument();

            writer.flush();
            writer.close();
        } catch (XMLStreamException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:solidbase.core.DBVersion.java

/**
 * Dumps the current log in XML format to the given output stream, with the given character set.
 *
 * @param out The outputstream to which the xml will be written.
 * @param charSet The requested character set.
 *//*from  ww w  .  j  ava  2  s .co  m*/
protected void logToXML(OutputStream out, Charset charSet) {
    // This method does not care about staleness

    boolean spec11 = SPEC11.equals(this.effectiveSpec);

    try {
        Connection connection = this.database.getDefaultConnection();
        Statement stat = connection.createStatement();
        try {
            ResultSet result = stat.executeQuery("SELECT " + (spec11 ? "TYPE, " : "")
                    + "SOURCE, TARGET, STATEMENT, STAMP, COMMAND, RESULT FROM " + this.logTableName
                    + " ORDER BY STAMP");

            XMLOutputFactory xof = XMLOutputFactory.newInstance();
            XMLStreamWriter xml = xof.createXMLStreamWriter(new OutputStreamWriter(out, charSet));
            xml.writeStartDocument("UTF-8", SPEC10);
            xml.writeStartElement("log");
            while (result.next()) {
                int i = 1;
                xml.writeStartElement("record");
                if (spec11)
                    xml.writeAttribute("type", result.getString(i++));
                xml.writeAttribute("source", StringUtils.defaultString(result.getString(i++)));
                xml.writeAttribute("target", result.getString(i++));
                xml.writeAttribute("statement", String.valueOf(result.getInt(i++)));
                xml.writeAttribute("stamp", String.valueOf(result.getTimestamp(i++)));
                String sql = result.getString(i++);
                if (sql != null) {
                    xml.writeStartElement("command");
                    xml.writeCharacters(sql);
                    xml.writeEndElement();
                }
                String res = result.getString(i++);
                if (res != null) {
                    xml.writeStartElement("result");
                    xml.writeCharacters(res);
                    xml.writeEndElement();
                }
                xml.writeEndElement();
            }
            xml.writeEndElement();
            xml.writeEndDocument();
            xml.close();
        } finally {
            stat.close();
            connection.commit();
        }
    } catch (XMLStreamException e) {
        throw new SystemException(e);
    } catch (SQLException e) {
        throw new SystemException(e);
    }
}

From source file:spypunk.tailthis.configuration.xml.ConfigurationXMLWriter.java

public void save(final Configuration configuration) throws Exception {

    FileUtils.forceMkdir(configurationFile.getParentFile());
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;/*from w ww .j  a  v  a  2s . c  o  m*/

    try {
        xtw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(new FileWriter(configurationFile)));

        xtw.writeStartElement(ConfigurationXMLConstants.TAILTHIS_ELEMENT_NAME);

        xtw.writeStartElement(ConfigurationXMLConstants.REMEMBER_LAST_OPENED_FILES_ELEMENT_NAME);
        xtw.writeCharacters(configuration.getRememberLastOpenedFiles().toString());
        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.LAST_FOLDER_ELEMENT_NAME);
        xtw.writeCharacters(configuration.getLastFolder().getAbsolutePath());
        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILES_ELEMENT_NAME);

        for (final File lastOpenedFile : configuration.getLastOpenedFiles()) {
            xtw.writeStartElement(ConfigurationXMLConstants.LAST_OPENED_FILE_ELEMENT_NAME);
            xtw.writeCharacters(lastOpenedFile.getAbsolutePath());
            xtw.writeEndElement();
        }

        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.UPDATE_PERIOD_ELEMENT_NAME);
        xtw.writeCharacters(configuration.getUpdatePeriod().toString());
        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.ALWAYS_ON_TOP_ELEMENT_NAME);
        xtw.writeCharacters(configuration.getAlwaysOnTop().toString());
        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.LOCALE_ELEMENT_NAME);
        xtw.writeCharacters(configuration.getLocale().getKey());
        xtw.writeEndElement();

        xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTINGS_ELEMENT_NAME);

        for (final Highlighting highlighting : configuration.getHighlightings()) {
            xtw.writeStartElement(ConfigurationXMLConstants.HIGHLIGHTING_ELEMENT_NAME);
            xtw.writeAttribute(ConfigurationXMLConstants.BACKGROUND_COLOR_ATTRIBUTE_NAME,
                    highlighting.getBackgroundColor().getKey());
            xtw.writeAttribute(ConfigurationXMLConstants.FOREGROUND_COLOR_ATTRIBUTE_NAME,
                    highlighting.getForegroundColor().getKey());
            xtw.writeCharacters(highlighting.getPattern().pattern());
            xtw.writeEndElement();
        }

        xtw.writeEndElement();

        xtw.writeEndElement();

        xtw.flush();
    } finally {
        if (xtw != null) {
            try {
                xtw.close();
            } catch (final XMLStreamException e) {
                LOGGER.warn(e.getMessage(), e);
            }
        }
    }
}