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:org.simbasecurity.core.saml.SAMLServiceImpl.java

@Override
public String createLogoutRequest(String logoutRequestId, Date issueInstant, String nameId, String sessionIndex)
        throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "LogoutRequest", NS_SAMLP);
    writer.writeNamespace("samlp", NS_SAMLP);

    writer.writeAttribute("ID", "_" + logoutRequestId);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", SAML_DATE_FORMAT.format(issueInstant));

    writer.writeStartElement("saml", "Issuer", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeCharacters("https://iamapps.belgium.be/");
    writer.writeEndElement();/*ww  w  . j a v  a  2 s .c  o  m*/

    writer.writeStartElement("saml", "NameID", NS_SAML);
    writer.writeNamespace("saml", NS_SAML);
    writer.writeAttribute("NameQualifier",
            configurationService.getValue(SimbaConfigurationParameter.SAML_IDP_SLO_TARGET_URL));
    writer.writeAttribute("SPNameQualifier", "https://iamapps.belgium.be/");
    writer.writeAttribute("Format", NAMEID_TRANSIENT);
    writer.writeCharacters(nameId);
    writer.writeEndElement();

    writer.writeStartElement("samlp", "SessionIndex", NS_SAMLP);
    writer.writeNamespace("saml", NS_SAMLP);
    writer.writeCharacters(sessionIndex);
    writer.writeEndElement();

    writer.writeEndElement();
    writer.flush();

    return encodeSAMLRequest(baos.toByteArray());
}

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 {//w w w. j  a  v  a  2s. 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  av a2s  . c  o  m
        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 {//from w  ww.  j  av a 2s. 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 ww  w. j a  v a  2 s .  c  om*/
        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);
    }
}

From source file:org.slc.sli.modeling.xml.IndentingXMLStreamWriterTest.java

@Before
public void setup() throws XMLStreamException {
    outputStream = new ByteArrayOutputStream();
    streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(outputStream, "UTF-8");
    out = new IndentingXMLStreamWriter(streamWriter);
    out.setDefaultNamespace(DEFAULT_NAMESPACE);
    out.setPrefix(DEFAULT_PREFIX, DEFAULT_NAMESPACE);
    out.setPrefix(PREFIX, NAMESPACE);//from   w w w . j  av a 2  s  .co  m

    NamespaceContext context = mock(NamespaceContext.class);
    when(context.getNamespaceURI(anyString())).thenReturn(NAMESPACE);
    out.setNamespaceContext(context);
}

From source file:org.springframework.batch.item.xml.StaxEventItemWriter.java

/**
 * Subclasses can override to customize the factory.
 * @return a factory for the xml output/*from  w  w  w  .  j  a  va 2s  .  c  o  m*/
 * @throws FactoryConfigurationError
 */
protected XMLOutputFactory createXmlOutputFactory() throws FactoryConfigurationError {
    return XMLOutputFactory.newInstance();
}

From source file:org.squidy.nodes.laserpointer.config.ConfigConnection.java

public void run() {
    try {/*from   www  .  j a  va  2  s.  c o m*/
        xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(socket.getInputStream());
        xmlEventReader.nextEvent(); // startDocument
        xmlEventReader.nextTag(); // protocol element
        identifier = "" + xmlEventReader.nextEvent();
        identifier = identifier.substring(4, identifier.length() - 3);
        connectionToConfigClient = identifier.startsWith("config");

        if (LOG.isDebugEnabled()) {
            LOG.debug("Connection from camera #" + identifier);
        }
        configManager.attachConnection(this);

        xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(socket.getOutputStream());
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(PROTOCOL_ELEM);
        xmlStreamWriter.writeComment("skipme");
        xmlStreamWriter.flush();

        // write xml for the first time
        marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
        xmlStreamWriter.flush();

        if (connectionToConfigClient) {
            while (running && xmlEventReader.peek().isStartElement()) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receiving configuration from config client.");
                }

                // Reading configuration from config client.
                Configuration configuration = (Configuration) unmarshaller.unmarshal(xmlEventReader);

                // Send update to cameras.
                configManager.updateConfig(this, configuration);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Writing result back to config client.");
                }

                // Write result back to config client.
                marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
                xmlStreamWriter.flush();
            }
            close();
        }
    } catch (Exception e) {
        if (LOG.isErrorEnabled()) {
            LOG.error(e.getMessage(), e);
        }
        close();
    }
}

From source file:org.squidy.nodes.laserpointer.configclient.service.comm.TcpIpCommService.java

private void newConnection(InputStream in, OutputStream out) throws CommException {
    try {//from   ww  w  .  j a va 2  s.co  m

        xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
        xsw.writeStartDocument();
        xsw.writeStartElement(PROTOCOL_ELEM);
        xsw.writeComment(PROTOCOL_COMMENT); // arbitrary
        xsw.flush();

        xer = XMLInputFactory.newInstance().createXMLEventReader(in);
        xer.nextEvent(); // startDocument
        xer.nextTag(); // protocol element TODO: protocol check
        xer.nextEvent(); // arbitrary comment

    } catch (Exception e) {
        // XMLStreamException
        // FactoryConfigurationError
        String msg = "Unable to initialize protocol.";
        LOG.error(msg, e);
        throw new CommException(msg);
    }
}

From source file:org.squidy.nodes.tracking.config.ConfigConnection.java

public void run() {
    try {//from   w w w  .jav  a2  s .c o  m
        xmlEventReader = XMLInputFactory.newInstance().createXMLEventReader(socket.getInputStream());
        xmlEventReader.nextEvent(); // startDocument
        xmlEventReader.nextTag(); // protocol element
        identifier = "" + xmlEventReader.nextEvent();
        identifier = identifier.substring(4, identifier.length() - 3);
        connectionToConfigClient = identifier.startsWith("config");

        if (LOG.isDebugEnabled()) {
            LOG.debug("Connection from camera #" + identifier);
        }
        configManager.attachConnection(this);

        xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(socket.getOutputStream());
        xmlStreamWriter.writeStartDocument();
        xmlStreamWriter.writeStartElement(PROTOCOL_ELEM);
        xmlStreamWriter.writeComment("skipme");
        xmlStreamWriter.flush();

        // write xml for the first time
        marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
        xmlStreamWriter.flush();

        if (connectionToConfigClient) {
            while (running && xmlEventReader.peek().isStartElement()) {

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Receiving configuration from config client.");
                }

                // Reading configuration from config client.
                Configuration configuration = (Configuration) unmarshaller.unmarshal(xmlEventReader);

                // Send update to cameras.
                configManager.updateConfig(this, configuration);

                if (LOG.isDebugEnabled()) {
                    LOG.debug("Writing result back to config client.");
                }

                // Write result back to config client.
                marshaller.marshal(configManager.getConfig(), xmlStreamWriter);
                xmlStreamWriter.flush();
            }
            close();
        }
    } catch (Exception e) {
        if (running) {
            if (LOG.isErrorEnabled()) {
                LOG.error(e.getMessage(), e);
            }
            close();
        }
    }
}