Example usage for javax.xml.stream XMLStreamWriter writeEndElement

List of usage examples for javax.xml.stream XMLStreamWriter writeEndElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter writeEndElement.

Prototype

public void writeEndElement() throws XMLStreamException;

Source Link

Document

Writes an end tag to the output relying on the internal state of the writer to determine the prefix and local name of the event.

Usage

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

/**
 * Get String with the SAML2 AuthnRequest message
 * @param format -1=plain, 1=base64/*from   ww  w .ja v  a 2  s .co m*/
 * @return
 * @throws XMLStreamException
 * @throws IOException
 */
public String getRequest(int format) throws XMLStreamException, IOException {
    _logger.info("For ID: " + this._id);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Deflater compresser = new Deflater(Deflater.BEST_COMPRESSION, true);
    DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(baos, compresser);
    StringWriter sw = new StringWriter();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;

    // ugly but effective:
    if (format == base64) {
        writer = factory.createXMLStreamWriter(deflaterOutputStream);
    } else {
        writer = factory.createXMLStreamWriter(sw);
    }

    writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("ID", _id);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", this._issueInstant);
    writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    writer.writeAttribute("AssertionConsumerServiceURL", _acsUrl);

    writeIssuer(writer);

    writeNameIDPolicy(writer);

    writeRequestedAuthnContext(writer);

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

    if (format == base64) {
        deflaterOutputStream.close();
        byte[] bain = baos.toByteArray();
        byte[] encoded = Base64.encodeBase64(bain, false);
        String result = new String(encoded, Charset.forName("UTF-8"));

        return result;
    } else {
        return sw.toString();
    }

}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeIssuer(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(_issuer);/*  ww  w .  j  a  va2  s. c om*/
    writer.writeEndElement();
}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeNameIDPolicy(XMLStreamWriter writer) throws XMLStreamException {
    if (_requestedNameIdFormat == null) {
        _logger.info("Skipping NameIDPolicy in request");
        return;//from   w  w w .  ja v a 2 s  .c o m
    }

    _logger.info("Adding {} as NameIDPolicy@Format", _requestedNameIdFormat);

    writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("Format", _requestedNameIdFormat);
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();
}

From source file:org.asimba.wa.integrationtest.saml2.model.AuthnRequest.java

protected void writeRequestedAuthnContext(XMLStreamWriter writer) throws XMLStreamException {
    if (_requestedAuthnContext == null) {
        _logger.info("Skipping RequestedAuthnContext in request");
        return;/*from w ww .jav a  2 s  .co m*/
    }

    _logger.info("Adding {} as RequestedAuthnContext@AuthnContextClassRef", _requestedAuthnContext);

    writer.writeStartElement("samlp", "RequestedAuthnContext", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("Comparison", "exact");

    writer.writeStartElement("saml", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(_requestedAuthnContext);
    writer.writeEndElement();

    writer.writeEndElement();
}

From source file:org.asimba.wa.integrationtest.saml2.model.Response.java

protected void writeStatus(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2p", "Status", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeStartElement("saml2p", "StatusCode", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeAttribute("Value", _statusCode);
    writer.writeEndElement();
    writer.writeEndElement();/*from ww  w . java  2s  .c o m*/
}

From source file:org.asimba.wa.integrationtest.saml2.model.Response.java

/**
 * Build response based on current state.<br/>
 * Only do Base64-encoding of the XML-document -- no deflating whatsoever may be done.
 * /*from www. j  a va2 s  .  co  m*/
 * @return
 * @throws XMLStreamException 
 */
public String getResponse(int format) throws XMLStreamException {
    _logger.info("For ID: " + getId());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StringWriter sw = new StringWriter();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;

    // ugly but effective:
    if (format == base64) {
        writer = factory.createXMLStreamWriter(baos);
    } else {
        writer = factory.createXMLStreamWriter(sw);
    }

    writer.writeStartElement("saml2p", "Response", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("saml2p", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("xs", "http://www.w3.org/2001/XMLSchema");

    writer.writeAttribute("Destination", _destination);
    writer.writeAttribute("ID", _id);
    writer.writeAttribute("InResponseTo", _inResponseTo);
    writer.writeAttribute("IssueInstant", _issueInstant);
    writer.writeAttribute("Version", "2.0");

    writeIssuer(writer);

    writeStatus(writer);

    _assertion.writeAssertion(writer);

    writer.writeEndElement(); // SAML2

    writer.flush();

    if (format == base64) {
        byte[] bain = baos.toByteArray();
        byte[] encoded = Base64.encodeBase64(bain, false);
        String result = new String(encoded, Charset.forName("UTF-8"));

        return result;
    } else {
        return sw.toString();
    }
}

From source file:org.chorusbdd.chorus.tools.webagent.jettyhandler.XmlStreamingHandler.java

protected void writeSimpleTextElement(XMLStreamWriter writer, String element, String elementText)
        throws XMLStreamException {
    writer.writeStartElement(element);//from  ww w .ja v  a 2s  .com
    writer.writeCharacters(elementText);
    writer.writeEndElement();
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a salt project to the xml stream.
 * // w ww.  ja  v  a 2s .c  o  m
 * @param xml
 *            A pre-configured {@link XMLStreamWriter}
 * @param project
 */
public void writeSaltProject(XMLStreamWriter xml, SaltProject project) {

    try {

        xml.writeStartElement(NS_SALTCOMMON, TAG_SALT_PROJECT, NS_VALUE_SALTCOMMON);
        xml.writeNamespace(NS_SCORPUSSTRUCTURE, NS_VALUE_SCORPUSSTRUCTURE);
        xml.writeNamespace(NS_XMI, NS_VALUE_XMI);
        xml.writeNamespace(NS_XSI, NS_VALUE_XSI);
        xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE);
        xml.writeNamespace(NS_SALTCOMMON, NS_VALUE_SALTCOMMON);
        xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0");

        Iterator<SCorpusGraph> cGraphs = project.getCorpusGraphs().iterator();
        while (cGraphs.hasNext()) {
            if (isPrettyPrint) {
                xml.writeCharacters("\n");
                xml.writeCharacters("\t");
            }
            writeCorpusGraph(xml, cGraphs.next(), true);
        }

        if (isPrettyPrint) {
            xml.writeCharacters("\n");
        }
        xml.writeEndElement();
    } catch (XMLStreamException e) {
        throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr() + "'. ", e);
    }
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a corpus graph to the xml stream
 * //from ww  w  . ja v a 2 s .  c o m
 * @param graph
 *            the corpus graph to be written
 * @param embedded
 *            determines whether this corpus graph is part of a saltProject
 * @param xml
 *            xml stream to write corpus graph to, if the passed one is
 *            null, a new one will be created
 */
public void writeCorpusGraph(XMLStreamWriter xml, SCorpusGraph graph, boolean embedded) {
    try {
        if (!embedded) {
            xml.writeStartDocument("1.0");
            if (isPrettyPrint) {
                xml.writeCharacters("\n");
                xml.writeStartElement(NS_SALTCOMMON, TAG_SCORPUSGRAPH, NS_VALUE_SALTCOMMON);
                xml.writeNamespace(NS_SCORPUSSTRUCTURE, NS_VALUE_SCORPUSSTRUCTURE);
                xml.writeNamespace(NS_XMI, NS_VALUE_XMI);
                xml.writeNamespace(NS_XSI, NS_VALUE_XSI);
                xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE);
                xml.writeNamespace(NS_SALTCOMMON, NS_VALUE_SALTCOMMON);
                xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0");
            }
        } else {
            xml.writeStartElement(TAG_SCORPUSGRAPH);
        }

        // write all labels
        if (graph.getLabels() != null) {
            Iterator<Label> labelIt = graph.getLabels().iterator();
            while (labelIt.hasNext()) {
                if (isPrettyPrint) {
                    xml.writeCharacters("\n");
                    xml.writeCharacters("\t");
                }
                writeLabel(xml, labelIt.next());
            }
        }
        // stores the position of a single node in the list of nodes to
        // refer them in a relation
        Map<SNode, Integer> nodePositions = new HashMap<>();

        // write all nodes
        if (graph.getNodes() != null) {
            Iterator<SNode> nodeIt = graph.getNodes().iterator();
            Integer position = 0;
            while (nodeIt.hasNext()) {
                SNode node = nodeIt.next();
                writeNode(xml, node, null);
                nodePositions.put(node, position);
                position++;
            }
        }

        // write all relations
        if (graph.getRelations() != null) {
            Iterator<SRelation<SNode, SNode>> relIt = graph.getRelations().iterator();
            while (relIt.hasNext()) {
                SRelation<SNode, SNode> rel = relIt.next();
                writeRelation(xml, rel, nodePositions, null);
            }
        }
        if (isPrettyPrint) {
            xml.writeCharacters("\n");
            xml.writeCharacters("\t");
        }
        xml.writeEndElement();
    } catch (XMLStreamException e) {
        throw new SaltResourceException(
                "Cannot store salt project to file '" + getLocationStr() + "'. " + e.getMessage(), e);
    } finally {
        if (!embedded) {
            if (xml != null) {
                try {
                    xml.flush();
                    xml.close();
                } catch (XMLStreamException e) {
                    throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr()
                            + "', because the opened stream is not closable. ", e);
                }
            }
        }
    }
}

From source file:org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer.java

/**
 * Writes a document graph to the xml stream.
 * //from ww w  . j ava  2  s.c o  m
 * @param xml
 *            A pre-configured {@link XMLStreamWriter}
 * @param graph
 */
public void writeDocumentGraph(XMLStreamWriter xml, SDocumentGraph graph) {
    try {
        xml.writeStartElement(NS_SDOCUMENTSTRUCTURE, TAG_SDOCUMENTSTRUCTURE_SDOCUMENTGRAPH,
                NS_VALUE_SDOCUMENTSTRUCTURE);
        xml.writeNamespace(NS_SDOCUMENTSTRUCTURE, NS_VALUE_SDOCUMENTSTRUCTURE);
        xml.writeNamespace(NS_XMI, NS_VALUE_XMI);
        xml.writeNamespace(NS_XSI, NS_VALUE_XSI);
        xml.writeNamespace(NS_SALTCORE, NS_VALUE_SALTCORE);

        xml.writeAttribute(NS_VALUE_XMI, ATT_XMI_VERSION, "2.0");

        // write all labels
        Iterator<Label> labelIt = graph.getLabels().iterator();
        while (labelIt.hasNext()) {
            if (isPrettyPrint) {
                xml.writeCharacters("\n");
                xml.writeCharacters("\t");
            }
            writeLabel(xml, labelIt.next());
        }

        // stores the position of a single layer in the list of layers to
        // refer them in a relation
        Map<SLayer, Integer> layerPositions = new HashMap<>();
        Iterator<SLayer> layerIt = graph.getLayers().iterator();
        Integer position = 0;
        while (layerIt.hasNext()) {
            layerPositions.put(layerIt.next(), position);
            position++;
        }

        // stores the position of a single node in the list of nodes to
        // refer them in a relation
        Map<SNode, Integer> nodePositions = new HashMap<>();

        // write all nodes
        Iterator<SNode> nodeIt = graph.getNodes().iterator();
        position = 0;
        while (nodeIt.hasNext()) {
            SNode node = nodeIt.next();
            writeNode(xml, node, layerPositions);
            nodePositions.put(node, position);
            position++;
        }

        // stores the position of a single relation in the list of relations
        // to refer them later
        Map<SRelation<SNode, SNode>, Integer> relPositions = new HashMap<>();

        {
            // write all relations
            Iterator<SRelation<SNode, SNode>> relIt = graph.getRelations().iterator();
            position = 0;
            while (relIt.hasNext()) {
                SRelation<SNode, SNode> rel = relIt.next();
                writeRelation(xml, rel, nodePositions, layerPositions);
                relPositions.put(rel, position);
                position++;
            }
        }

        // write layers
        layerIt = graph.getLayers().iterator();
        while (layerIt.hasNext()) {
            writeLayer(xml, layerIt.next(), nodePositions, relPositions);
        }

        if (isPrettyPrint) {
            xml.writeCharacters("\n");
        }
        xml.writeEndElement();
    } catch (XMLStreamException e) {
        throw new SaltResourceException("Cannot store document graph to file '" + getLocationStr() + "'. ", e);
    }
}