Search in sources :

Example 11 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project jdk8u_jdk by JetBrains.

the class SurrogatesTest method generateAndReadXml.

// Generates xml content with XMLStreamWriter and read it to check
// for correctness of xml and generated data
void generateAndReadXml(String content) throws Exception {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    OutputStreamWriter streamWriter = new OutputStreamWriter(stream);
    XMLStreamWriter writer = factory.createXMLStreamWriter(streamWriter);
    // Generate xml with selected stream writer type
    generateXML(writer, content);
    String output = stream.toString();
    System.out.println("Generated xml: " + output);
    // Read generated xml with StAX parser
    readXML(output.getBytes(), content);
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 12 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project jdk8u_jdk by JetBrains.

the class JAXP15RegTest method testXMLOutputFactory.

public void testXMLOutputFactory() {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    factory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
    success("testXMLOutputFactory passed");
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory)

Example 13 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project tdi-studio-se by Talend.

the class DeviceIdManager method createRegistrationEnvelope.

/**
     * This method helps to create a xml envelope needed for device registration request.
     * 
     * @param prefix Sets the required prefix for the device name.
     * @param applicationId app GUID
     * @param deviceName name of the device
     * @param password password for the device
     * @return xml envelope comprising device add request
     */
private static String createRegistrationEnvelope(String prefix, UUID applicationId, String deviceName, String password) {
    // The format of the envelope is the following:
    // <DeviceAddRequest>
    // <ClientInfo name="[app GUID]" version="1.0"/>
    // <Authentication>
    // <Membername>[prefix][device name]</Membername>
    // <Password>[device password]</Password>
    // </Authentication>
    // </DeviceAddRequest>
    // Instantiate the writer and write the envelope
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    StringWriter envelope = new StringWriter();
    XMLStreamWriter xml = null;
    try {
        xml = factory.createXMLStreamWriter(envelope);
        // Create the node
        xml.writeStartElement("DeviceAddRequest");
        // Write the ClientInfo node
        xml.writeStartElement("ClientInfo");
        xml.writeAttribute("name", applicationId.toString());
        xml.writeAttribute("version", "1.0");
        xml.writeEndElement();
        // Write the Authentication node
        xml.writeStartElement("Authentication");
        xml.writeStartElement("Membername");
        xml.writeCharacters(prefix + deviceName);
        xml.writeEndElement();
        xml.writeStartElement("Password");
        xml.writeCharacters(password);
        xml.writeEndElement();
        // </Authentication>
        xml.writeEndElement();
        // </DeviceAddRequest>
        xml.writeEndElement();
    } catch (XMLStreamException e) {
        Log.error(e.getMessage());
    } finally {
        if (xml != null) {
            try {
                xml.flush();
                xml.close();
            } catch (XMLStreamException e) {
                Log.error(e.getMessage());
            // Ignore if it is already closed
            }
        }
    }
    return envelope.toString();
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) StringWriter(java.io.StringWriter) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 14 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project voltdb by VoltDB.

the class JDBCSQLXML method createStAXResult.

/**
     *  Retrieves a new DOMResult for setting the XML value designated by this
     *  SQLXML instance.
     *
     *  @param resultClass The class of the result, or null.
     *  @throws java.sql.SQLException if there is an error processing the XML
     *          value
     *  @return for setting the XML value designated by this SQLXML instance.
     */
@SuppressWarnings("unchecked")
protected <T extends Result> T createStAXResult(Class<T> resultClass) throws SQLException {
    StAXResult result = null;
    OutputStream outputStream = this.setBinaryStreamImpl();
    Constructor ctor;
    XMLOutputFactory factory;
    XMLStreamWriter xmlStreamWriter;
    try {
        factory = XMLOutputFactory.newInstance();
        xmlStreamWriter = factory.createXMLStreamWriter(outputStream);
        if (resultClass == null) {
            result = new StAXResult(xmlStreamWriter);
        } else {
            ctor = resultClass.getConstructor(XMLStreamWriter.class);
            result = (StAXResult) ctor.newInstance(xmlStreamWriter);
        }
    } catch (SecurityException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalArgumentException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InvocationTargetException ex) {
        throw Exceptions.resultInstantiation(ex.getTargetException());
    } catch (FactoryConfigurationError ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (NoSuchMethodException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (XMLStreamException ex) {
        throw Exceptions.resultInstantiation(ex);
    }
    return (T) result;
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) XMLOutputFactory(javax.xml.stream.XMLOutputFactory) Constructor(java.lang.reflect.Constructor) GZIPOutputStream(java.util.zip.GZIPOutputStream) ClosableByteArrayOutputStream(org.hsqldb_voltpatches.lib.ClosableByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) FactoryConfigurationError(javax.xml.parsers.FactoryConfigurationError)

Example 15 with XMLOutputFactory

use of javax.xml.stream.XMLOutputFactory in project Activiti by Activiti.

the class BpmnXMLConverter method convertToXML.

public byte[] convertToXML(BpmnModel model, String encoding) {
    try {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);
        XMLStreamWriter writer = xof.createXMLStreamWriter(out);
        XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer);
        DefinitionsRootExport.writeRootElement(model, xtw, encoding);
        CollaborationExport.writePools(model, xtw);
        DataStoreExport.writeDataStores(model, xtw);
        SignalAndMessageDefinitionExport.writeSignalsAndMessages(model, xtw);
        for (Process process : model.getProcesses()) {
            if (process.getFlowElements().isEmpty() && process.getLanes().isEmpty()) {
                // empty process, ignore it 
                continue;
            }
            ProcessExport.writeProcess(process, xtw);
            for (FlowElement flowElement : process.getFlowElements()) {
                createXML(flowElement, model, xtw);
            }
            for (Artifact artifact : process.getArtifacts()) {
                createXML(artifact, model, xtw);
            }
            // end process element
            xtw.writeEndElement();
        }
        BPMNDIExport.writeBPMNDI(model, xtw);
        // end definitions root element
        xtw.writeEndElement();
        xtw.writeEndDocument();
        xtw.flush();
        outputStream.close();
        xtw.close();
        return outputStream.toByteArray();
    } catch (Exception e) {
        LOGGER.error("Error writing BPMN XML", e);
        throw new XMLException("Error writing BPMN XML", e);
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) XMLException(org.activiti.bpmn.exceptions.XMLException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) FlowElement(org.activiti.bpmn.model.FlowElement) OutputStreamWriter(java.io.OutputStreamWriter) EventSubProcess(org.activiti.bpmn.model.EventSubProcess) Process(org.activiti.bpmn.model.Process) SubProcess(org.activiti.bpmn.model.SubProcess) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Artifact(org.activiti.bpmn.model.Artifact) XMLStreamException(javax.xml.stream.XMLStreamException) XMLException(org.activiti.bpmn.exceptions.XMLException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException)

Aggregations

XMLOutputFactory (javax.xml.stream.XMLOutputFactory)42 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)28 StringWriter (java.io.StringWriter)18 XMLStreamException (javax.xml.stream.XMLStreamException)15 XMLEventWriter (javax.xml.stream.XMLEventWriter)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Test (org.junit.Test)8 StAXResult (javax.xml.transform.stax.StAXResult)7 IOException (java.io.IOException)6 DOMResult (javax.xml.transform.dom.DOMResult)6 StreamResult (javax.xml.transform.stream.StreamResult)6 Result (javax.xml.transform.Result)5 PcrManifest (com.intel.mountwilson.manifest.data.PcrManifest)3 OutputStream (java.io.OutputStream)3 SAXResult (javax.xml.transform.sax.SAXResult)3 IManifest (com.intel.mountwilson.manifest.data.IManifest)2 Headers (com.sun.net.httpserver.Headers)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Path (java.nio.file.Path)2