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(java.io.OutputStream stream, String encoding)
        throws XMLStreamException;

Source Link

Document

Create a new XMLStreamWriter that writes to a stream

Usage

From source file:org.deegree.services.wpvs.controller.WPVSController.java

@SuppressWarnings("unchecked")
private void sendCapabilities(Map<String, String> map, HttpServletRequest request, HttpResponseBuffer response)
        throws IOException {

    GetCapabilities req = GetCapabilitiesKVPParser.parse(map);

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(IS_REPAIRING_NAMESPACES, true);
    try {//from www.j ava  2 s.  c  o  m
        XMLStreamWriter xsw = factory.createXMLStreamWriter(response.getOutputStream(), "UTF-8");
        IndentingXMLStreamWriter xmlWriter = new IndentingXMLStreamWriter(xsw);
        List<Operation> operations = new ArrayList<Operation>();
        List<DCP> dcps = Collections.singletonList(new DCP(new URL(OGCFrontController.getHttpGetURL()), null));
        List<Domain> params = Collections.emptyList();
        List<Domain> constraints = Collections.emptyList();
        for (String operation : allowedOperations) {
            operations.add(new Operation(operation, dcps, params, constraints, EMPTY_LIST));
        }
        OperationsMetadata operationsMd = new OperationsMetadata(operations, params, constraints, null);
        new CapabilitiesXMLAdapter().export040(xmlWriter, req, identification, provider, operationsMd,
                service.getServiceConfiguration());
        xmlWriter.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}

From source file:org.eclipse.gyrex.logback.config.internal.LogbackConfigGenerator.java

public File generateConfig() {
    // get state location
    if (!parentFolder.isDirectory() && !parentFolder.mkdirs()) {
        throw new IllegalStateException(
                String.format("Unable to create configs directory (%s).", parentFolder));
    }/* w ww. jav  a 2  s.  co m*/

    // save file
    final File configFile = new File(parentFolder,
            String.format("logback.%s.xml", DateFormatUtils.format(lastModified, "yyyyMMdd-HHmmssSSS")));
    OutputStream outputStream = null;
    XMLStreamWriter xmlStreamWriter = null;
    try {
        outputStream = new BufferedOutputStream(FileUtils.openOutputStream(configFile));
        final XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        xmlStreamWriter = outputFactory.createXMLStreamWriter(outputStream, CharEncoding.UTF_8);
        // try to format the output
        try {
            final Class<?> clazz = getClass().getClassLoader()
                    .loadClass("com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter");
            xmlStreamWriter = (XMLStreamWriter) clazz.getConstructor(XMLStreamWriter.class)
                    .newInstance(xmlStreamWriter);
        } catch (final Exception e) {
            // ignore
        }
        config.toXml(xmlStreamWriter);
        xmlStreamWriter.flush();
    } catch (final IOException e) {
        throw new IllegalStateException(
                String.format("Unable to create config file (%s).", ExceptionUtils.getRootCauseMessage(e)), e);
    } catch (final XMLStreamException e) {
        throw new IllegalStateException(
                String.format("Error writing config (%s).", ExceptionUtils.getRootCauseMessage(e)), e);
    } finally {
        if (null != xmlStreamWriter) {
            try {
                xmlStreamWriter.close();
            } catch (final Exception e) {
                // ignore
            }
        }
        IOUtils.closeQuietly(outputStream);
    }

    // cleanup directory
    removeOldFiles(parentFolder);

    return configFile;
}

From source file:org.graphipedia.dataextract.ExtractData.java

@Override
public void run() {
    logger.info("Start extracting data...");
    long startTime = System.currentTimeMillis();

    DisambiguationPageExtractor dpExtractor = new DisambiguationPageExtractor(settings, this.language,
            dpRootCategory, checkpoint, loggerMessageSuffix);
    dpExtractor.start();//w ww  . j  a  v a2 s  . com
    ExtractNamespaces nsExtractor = new ExtractNamespaces(settings, language, loggerMessageSuffix,
            new File(settings.wikipediaEditionDirectory(language), ExtractNamespaces.NAMESPACE_FILE),
            checkpoint);
    nsExtractor.start();
    InfoboxTemplatesExtractor itExtractor = new InfoboxTemplatesExtractor(settings, language, itRootCategory,
            checkpoint, loggerMessageSuffix);
    itExtractor.start();
    ExtractGeoTags geotagsExtractor = new ExtractGeoTags(settings, language, loggerMessageSuffix);
    geotagsExtractor.start();
    try {
        dpExtractor.join();
        nsExtractor.join();
        this.ns = nsExtractor.namespaces();
        itExtractor.join();
        geotagsExtractor.join();
        this.geotags = geotagsExtractor.getGeoTags();
    } catch (InterruptedException e) {
        logger.severe("Problems with the threads.");
        e.printStackTrace();
        System.exit(-1);
    }
    XMLOutputFactory outputFactory = XMLOutputFactory2.newInstance();
    File outputFile = new File(settings.wikipediaEditionDirectory(language), TEMPORARY_LINK_FILE);
    if (checkpoint.isLinksExtracted(this.language)) {
        logger.info("Using pages and links from a previous computation");
        return;
    }
    try {
        FileOutputStream fout = new FileOutputStream(outputFile.getAbsolutePath());
        BufferedOutputStream bos = new BufferedOutputStream(fout);
        CompressorOutputStream output = new CompressorStreamFactory()
                .createCompressorOutputStream(CompressorStreamFactory.BZIP2, bos);
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(output, "UTF-8");
        writer.writeStartDocument();
        writer.writeStartElement("d");

        LinkExtractor linkExtractor = new LinkExtractor(writer, logger, settings, language,
                dpExtractor.disambiguationPages(), itExtractor.infoboxTemplates(), this.ns);
        linkExtractor.parse(settings.getWikipediaXmlFile(language).getAbsolutePath());
        writer.writeEndElement();
        writer.writeEndDocument();
        output.close();
        fout.close();
        bos.close();
        writer.close();
        long elapsed = System.currentTimeMillis() - startTime;
        logger.info("Data extracted in " + ReadableTime.readableTime(elapsed));
    } catch (Exception e) {
        logger.severe("Error while parsing the XML file ");
        e.printStackTrace();
        System.exit(-1);
    }
    try {
        checkpoint.addLinksExtracted(this.language, true);
    } catch (IOException e) {
        logger.severe("Error while saving the checkpoint to file");
        e.printStackTrace();
        System.exit(-1);
    }
    settings.getWikipediaXmlFile(language).delete();
}

From source file:org.rhq.enterprise.server.sync.ExportingInputStream.java

private void exporterMain() {
    XMLStreamWriter wrt = null;/*from  w ww.  j  a  v a 2 s.co  m*/
    OutputStream out = null;
    try {
        XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
        ofactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

        try {
            out = exportOutput;
            if (zipOutput) {
                out = new GZIPOutputStream(out);
            }
            wrt = new IndentingXMLStreamWriter(ofactory.createXMLStreamWriter(out, "UTF-8"));
            //wrt = ofactory.createXMLStreamWriter(out, "UTF-8");
        } catch (XMLStreamException e) {
            LOG.error("Failed to create the XML stream writer to output the export file to.", e);
            return;
        }

        exportPrologue(wrt);

        for (Synchronizer<?, ?> exp : synchronizers) {
            exportSingle(wrt, exp);
        }

        exportEpilogue(wrt);

        wrt.flush();
    } catch (Exception e) {
        LOG.error("Error while exporting.", e);
        unexpectedExporterException = e;
    } finally {
        if (wrt != null) {
            try {
                wrt.close();
            } catch (XMLStreamException e) {
                LOG.warn("Failed to close the exporter XML stream.", e);
            }
        }
        safeClose(out);
    }
}

From source file:org.slc.sli.modeling.tools.xsdgen.Uml2XsdWriter.java

public static final void writeSchema(final List<PsmDocument<Type>> documents, final ModelIndex model,
        final Uml2XsdPlugin plugin, final OutputStream outstream) {
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    try {//from   w  ww.ja  v  a 2 s .  c  o  m
        final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8"));
        xsw.writeStartDocument("UTF-8", "1.0");
        try {
            writeSchema(documents, model, plugin, xsw);
        } finally {
            xsw.writeEndDocument();
        }
        xsw.flush();
        // We close the stream writer knowing (from the documentation) that it will not close
        // the underlying output stream.
        xsw.close();
    } catch (final XMLStreamException e) {
        throw new XsdGenRuntimeException(e);
    }
}

From source file:org.slc.sli.modeling.wadl.writer.WadlWriter.java

public static final void writeDocument(final Application app, final Map<String, String> prefixMappings,
        final OutputStream outstream) {
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    try {//w  w  w  .  j a v a2  s. com
        final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8"));
        xsw.writeStartDocument("UTF-8", "1.0");
        try {
            writeApplication(app, prefixMappings, xsw);
        } finally {
            xsw.writeEndDocument();
        }
        xsw.flush();
    } catch (final XMLStreamException e) {
        throw new WadlRuntimeException(e);
    }
}

From source file:org.slc.sli.modeling.xmi.comp.XmiMappingWriter.java

public static final void writeMappingDocument(final XmiComparison document, final OutputStream outstream) {
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    try {//  w  w w. ja v a  2 s.co  m
        final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8"));
        xsw.writeStartDocument("UTF-8", "1.0");
        try {
            writeMappingDocument(document, xsw);
        } finally {
            xsw.writeEndDocument();
        }
        xsw.flush();
        xsw.close();
    } catch (final XMLStreamException e) {
        throw new XmiCompRuntimeException(e);
    }
}

From source file:org.slc.sli.modeling.xmi.writer.XmiWriter.java

public static final void writeDocument(final Model model, final ModelIndex mapper,
        final OutputStream outstream) {
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    try {//from   w w w . j av a 2  s. com
        final XMLStreamWriter xsw = new IndentingXMLStreamWriter(xof.createXMLStreamWriter(outstream, "UTF-8"));
        xsw.writeStartDocument("UTF-8", "1.0");
        try {
            XmiWriter.writeXMI(model, mapper, xsw);
        } finally {
            xsw.writeEndDocument();
        }
        xsw.flush();
    } catch (final XMLStreamException e) {
        throw new XmiRuntimeException(e);
    }
}