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:microsoft.exchange.webservices.data.core.EwsServiceXmlWriter.java

/**
 * Initializes a new instance./*w  w w. ja v a 2 s. c  o m*/
 *
 * @param service the service
 * @param stream the stream
 * @throws XMLStreamException the XML stream exception
 */
public EwsServiceXmlWriter(ExchangeServiceBase service, OutputStream stream) throws XMLStreamException {
    this.service = service;
    XMLOutputFactory xmlof = XMLOutputFactory.newInstance();
    xmlWriter = xmlof.createXMLStreamWriter(stream, "utf-8");

}

From source file:com.microfocus.application.automation.tools.octane.tests.xml.TestResultXmlWriter.java

private void initialize(ResultFields resultFields)
        throws IOException, InterruptedException, XMLStreamException {
    if (outputStream == null) {
        outputStream = targetPath.write();
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream);
        writer.writeStartDocument();/*from   ww  w .  j a  v a  2 s  . co  m*/

        writer.writeStartElement("test_result");
        writer.writeStartElement("build");
        writer.writeAttribute("server_id", "to-be-filled-in-SDK");
        writer.writeAttribute("job_id", buildDescriptor.getJobId());
        writer.writeAttribute("build_id", buildDescriptor.getBuildId());
        if (!StringUtils.isEmpty(buildDescriptor.getSubType())) {
            writer.writeAttribute("sub_type", buildDescriptor.getSubType());
        }
        writer.writeEndElement(); // build
        writeFields(resultFields);
        writer.writeStartElement("test_runs");
    }
}

From source file:com.norconex.collector.http.checksum.impl.DefaultHttpDocumentChecksummer.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from w  w  w. j av a2s  .  co  m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("httpDocumentChecksummer");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeStartElement("field");
        writer.writeCharacters(field);
        writer.writeEndElement();
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.tiger.TigerXmlWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    OutputStream docOS = null;/*from ww  w.j a  va 2 s  .  c  o m*/
    try {
        docOS = getOutputStream(aJCas, filenameSuffix);

        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter xmlEventWriter = new IndentingXMLEventWriter(
                xmlOutputFactory.createXMLEventWriter(docOS));

        JAXBContext context = JAXBContext.newInstance(TigerSentence.class);
        Marshaller marshaller = context.createMarshaller();
        // We use the marshaller only for individual sentences. That way, we do not have to 
        // build the whole TIGER object graph before seralizing, which should safe us some
        // memory.
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        XMLEventFactory xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        xmlEventWriter.add(xmlef.createStartElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createStartElement("", "", "body"));

        int sentenceNumber = 1;
        for (Sentence s : select(aJCas, Sentence.class)) {
            TigerSentence ts = convertSentence(s, sentenceNumber);
            marshaller.marshal(new JAXBElement<TigerSentence>(new QName("s"), TigerSentence.class, ts),
                    xmlEventWriter);
            sentenceNumber++;
        }

        xmlEventWriter.add(xmlef.createEndElement("", "", "body"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createEndDocument());
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }
}

From source file:com.predic8.membrane.core.ws.relocator.Relocator.java

public Relocator(OutputStreamWriter osw, String protocol, String host, int port, PathRewriter pathRewriter)
        throws Exception {
    this.writer = XMLOutputFactory.newInstance().createXMLEventWriter(osw);
    this.host = host;
    this.port = port;
    this.protocol = protocol;
    this.pathRewriter = pathRewriter;
}

From source file:eionet.meta.exports.rdf.InspireCodelistXmlWriter.java

/**
 * Inits INSPIRE XML writer./*from  ww  w.  j a  va 2 s.  c om*/
 *
 * @param out
 *            output stream
 * @param voc
 *            Vocabulary
 * @param ctx
 *            context URL for DD
 * @throws XMLStreamException
 *             if streaming fails
 */
public InspireCodelistXmlWriter(OutputStream out, VocabularyFolder voc, String ctx) throws XMLStreamException {
    writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out, ENCODING);
    vocabulary = voc;
    contextRoot = ctx;
}

From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java

public static void write(List<Scale> modeList, String fileName) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;//  www  .  ja va 2 s.  c  o m

    // <mode>
    // <name>Peruvian tritonic 2</name>
    // <interval>3</interval>
    // <interval>4</interval>
    // <interval>5</interval>
    // </mode>

    String ns = "http://rockhoppertech.com/modes-1.0";

    // StringWriter sw = new StringWriter();
    try {
        xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
        // xtw = xof.createXMLStreamWriter(sw);
        xtw.writeComment("all elements here are explicitly in the mode namespace");
        xtw.writeStartDocument("utf-8", "1.0");
        xtw.setPrefix("modes", ns);
        xtw.writeStartElement(ns, "modes");
        xtw.writeNamespace("modes", ns);

        for (Scale scale : modeList) {
            // xtw.writeStartElement(ns, "mode");
            xtw.writeStartElement("mode");

            // xtw.writeStartElement(ns, "name");
            xtw.writeStartElement("name");
            xtw.writeCharacters(scale.getName());
            xtw.writeEndElement();

            // xtw.writeStartElement(ns, "intervals");
            // xtw.writeStartElement(ns, "interval");
            xtw.writeStartElement("intervals");
            int[] intervals = scale.getIntervals();
            for (int i = 0; i < intervals.length; i++) {
                xtw.writeStartElement("interval");
                xtw.writeCharacters("" + intervals[i]);
                xtw.writeEndElement();
            }
            xtw.writeEndElement(); // intervals

            xtw.writeEndElement(); // mode
        }

        xtw.writeEndElement(); // modes
        xtw.writeEndDocument();
        xtw.flush();
        xtw.close();
        // System.err.println(sw.toString());

    } catch (XMLStreamException | IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        e.printStackTrace();
    }

}

From source file:io.mapzone.controller.catalog.csw.CswResponse.java

protected XMLStreamWriter out() throws XMLStreamException, FactoryConfigurationError, IOException {
    if (writer == null) {
        writer = XMLOutputFactory.newInstance()
                .createXMLStreamWriter(request().httpResponse().getOutputStream(), "UTF8");
        writer = new IndentingXMLStreamWriter(writer);
    }/*from  w w w .  j a v  a  2s. c o  m*/
    return writer;
}

From source file:com.flexive.core.IMParser.java

/**
 * Parse an identify stdOut result (from in) and convert it to an XML content
 *
 * @param in identify response//from  w  ww.  ja v  a 2  s  . co m
 * @return XML content
 * @throws XMLStreamException on errors
 * @throws IOException        on errors
 */
public static String parse(InputStream in) throws XMLStreamException, IOException {
    StringWriter sw = new StringWriter(2000);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(sw);
    writer.writeStartDocument();

    int lastLevel = 0, level, lastNonValueLevel = 1;
    boolean valueEntry;
    String curr = null;
    String[] entry;
    try {
        while ((curr = br.readLine()) != null) {
            if (curr.indexOf(':') == -1 || curr.trim().length() == 0) {
                System.out.println("skipping: [" + curr + "]");
                continue; //ignore lines without ':'
            }
            level = getLevel(curr);
            curr = curr.trim();
            if (level == 0 && curr.startsWith("Image:")) {
                writer.writeStartElement("Image");
                entry = curr.split(": ");
                if (entry.length >= 2)
                    writer.writeAttribute("source", entry[1]);
                lastLevel = level;
                continue;
            }
            if (!(valueEntry = pNumeric.matcher(curr).matches())) {
                while (level < lastLevel--)
                    writer.writeEndElement();
                lastNonValueLevel = level;
            } else
                level = lastNonValueLevel + 1;
            if (curr.endsWith(":")) {
                writer.writeStartElement(cleanElement(curr.substring(0, curr.lastIndexOf(':'))));
                lastLevel = level + 1;
                continue;
            } else if (pColormap.matcher(curr).matches()) {
                writer.writeStartElement(cleanElement(curr.substring(0, curr.lastIndexOf(':'))));
                writer.writeAttribute("colors", curr.split(": ")[1].trim());
                lastLevel = level + 1;
                continue;
            }
            entry = curr.split(": ");
            if (entry.length == 2) {
                if (!valueEntry) {
                    writer.writeStartElement(cleanElement(entry[0]));
                    writer.writeCharacters(entry[1]);
                    writer.writeEndElement();
                } else {
                    writer.writeEmptyElement("value");
                    writer.writeAttribute("key", cleanElement(entry[0]));
                    writer.writeAttribute("data", entry[1]);
                    //                        writer.writeEndElement();
                }
            } else {
                //                    System.out.println("unknown line: "+curr);
            }
            lastLevel = level;
        }
    } catch (Exception e) {
        System.err.println("Error at [" + curr + "]:" + e.getMessage());
        e.printStackTrace();
    }

    writer.writeEndDocument();
    writer.flush();
    writer.close();
    return sw.getBuffer().toString();
}

From source file:com.norconex.collector.core.filter.impl.RegexReferenceFilter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from   w  ww .ja  v a2s  .  co  m*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("filter");
        writer.writeAttribute("class", getClass().getCanonicalName());
        super.saveToXML(writer);
        writer.writeAttribute("caseSensitive", Boolean.toString(caseSensitive));
        writer.writeCharacters(regex == null ? "" : regex);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}