Example usage for javax.xml.stream XMLOutputFactory newInstance

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

Introduction

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

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

From source file:savant.file.Project.java

public static void saveToFile(File f) throws IOException, SavantEmptySessionException, XMLStreamException {
    GZIPOutputStream output = new GZIPOutputStream(new FileOutputStream(f));
    writer = XMLOutputFactory.newInstance().createXMLStreamWriter(output, "UTF-8");
    writer.writeStartDocument();// ww w.  jav  a 2 s . co  m
    writeStartElement(XMLElement.savant, "");
    writeAttribute(XMLAttribute.version, Integer.toString(FILE_VERSION));

    LocationController locationController = LocationController.getInstance();
    Range r = locationController.getRange();
    writeAttribute(XMLAttribute.range,
            String.format("%s:%d-%d", locationController.getReferenceName(), r.getFrom(), r.getTo()));

    writeStartElement(XMLElement.genome, "  ");
    Genome g = GenomeController.getInstance().getGenome();
    writeAttribute(XMLAttribute.name, g.getName());
    URI cytobandURI = g.getCytobandURI();
    if (cytobandURI != null) {
        writeAttribute(XMLAttribute.cytoband, cytobandURI.toString());
    }

    if (g.isSequenceSet()) {
        writeAttribute(XMLAttribute.uri, NetworkUtils.getNeatPathFromURI(g.getDataSource().getURI()));
    } else {
        for (String ref : g.getReferenceNames()) {
            writeEmptyElement(XMLElement.reference, "    ");
            writeAttribute(XMLAttribute.name, ref);
            writeAttribute(XMLAttribute.length, Integer.toString(g.getLength(ref)));
        }
    }
    writer.writeCharacters("\r\n  ");
    writer.writeEndElement();

    for (FrameAdapter fr : FrameController.getInstance().getOrderedFrames()) {
        // Frame may have zero-length track array  if user saves while the "Creating track\u2026 progress-bar is up.
        TrackAdapter[] tracks = fr.getTracks();
        if (tracks.length > 0) {
            TrackAdapter t0 = tracks[0];
            URI uri = t0.getDataSource().getURI();
            if (uri != null) {
                writeEmptyElement(XMLElement.track, "  ");
                writeAttribute(XMLAttribute.uri, NetworkUtils.getNeatPathFromURI(uri));
                writeAttribute(XMLAttribute.mode, t0.getDrawingMode().toString());
            }
        }
    }

    for (Bookmark b : BookmarkController.getInstance().getBookmarks()) {
        writeStartElement(XMLElement.bookmark, "  ");
        writeAttribute(XMLAttribute.range, b.getLocationText());
        writer.writeCharacters(b.getAnnotation());
        writer.writeEndElement();
    }

    for (String p : VariationController.getInstance().getControls()) {
        writeStartElement(XMLElement.control, "   ");
        writer.writeCharacters(p);
        writer.writeEndElement();
    }
    writer.writeCharacters("\r\n");
    writer.writeEndElement();
    writer.writeEndDocument();
    output.finish();
    writer.close();
}

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.
 *//*ww  w. j a  va2  s  .  c  o  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 www  . j a v  a 2s.c om

    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);
            }
        }
    }
}