Example usage for javax.xml.stream XMLStreamWriter writeCharacters

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

Introduction

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

Prototype

public void writeCharacters(String text) throws XMLStreamException;

Source Link

Document

Write text to the output

Usage

From source file:org.apache.synapse.commons.json.JsonDataSource.java

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();//  w  w  w.  jav  a  2 s  .c o m
    while (reader.hasNext()) {
        int x = reader.next();
        switch (x) {
        case XMLStreamConstants.START_ELEMENT:
            xmlWriter.writeStartElement(reader.getPrefix(), reader.getLocalName(), reader.getNamespaceURI());
            int namespaceCount = reader.getNamespaceCount();
            for (int i = namespaceCount - 1; i >= 0; i--) {
                xmlWriter.writeNamespace(reader.getNamespacePrefix(i), reader.getNamespaceURI(i));
            }
            int attributeCount = reader.getAttributeCount();
            for (int i = 0; i < attributeCount; i++) {
                xmlWriter.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                        reader.getAttributeLocalName(i), reader.getAttributeValue(i));
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        case XMLStreamConstants.CHARACTERS:
            xmlWriter.writeCharacters(reader.getText());
            break;
        case XMLStreamConstants.CDATA:
            xmlWriter.writeCData(reader.getText());
            break;
        case XMLStreamConstants.END_ELEMENT:
            xmlWriter.writeEndElement();
            break;
        case XMLStreamConstants.END_DOCUMENT:
            xmlWriter.writeEndDocument();
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            xmlWriter.writeComment(reader.getText());
            break;
        case XMLStreamConstants.DTD:
            xmlWriter.writeDTD(reader.getText());
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            xmlWriter.writeProcessingInstruction(reader.getPITarget(), reader.getPIData());
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            xmlWriter.writeEntityRef(reader.getLocalName());
            break;
        default:
            throw new OMException();
        }
    }
    xmlWriter.writeEndDocument();
    xmlWriter.flush();
    xmlWriter.close();
}

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

public void writeSubject(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Subject", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "NameID", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("Format", getSubjectNameIdFormat());
    writer.writeAttribute("NameQualifier", getIssuer());
    writer.writeCharacters(getSubjectNameId());
    writer.writeEndElement(); // </NameID>

    // TODO: Consider whether SubjectConfirmation should also be included or not

    writer.writeEndElement(); // </Subject>
}

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

public void writeConditions(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Conditions", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("NotAfter", getCondNotAfter());
    writer.writeAttribute("NotBefore", getCondNotBefore());

    writer.writeStartElement("saml2", "AudienceRestriction", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "Audience", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(getAudience());
    writer.writeEndElement(); // </Audience>

    writer.writeEndElement(); // </AudienceRestriction>

    writer.writeEndElement(); // </Conditions>
}

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

public void writeAuthnStatement(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "AuthnStatement", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("AuthnInstant", getIssueInstant());
    writer.writeAttribute("SessionIndex", getSessionIndex());
    writer.writeAttribute("SessionNotOnOrAfter", getSessionNotOnOrAfter());

    writer.writeStartElement("saml2", "AuthnContext", "urn:oasis:names:tc:SAML:2.0:assertion");

    writer.writeStartElement("saml2", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(getAuthnContextClassRef());
    writer.writeEndElement(); // </AuthnContextClassRef>

    writer.writeEndElement(); // </AuthnContext>

    writer.writeEndElement(); // </AuthnStatement>
}

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

public void writeAttribute(XMLStreamWriter writer, String key, String value) throws XMLStreamException {
    writer.writeStartElement("saml2", "Attribute", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("Name", key);
    writer.writeAttribute("NameFormat", "urn:oasis:names:tc:SAML:2.0:attrname-format:unspecified");

    writer.writeStartElement("saml2", "AttributeValue", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
    writer.writeAttribute("xsi", "http://www.w3.org/2001/XMLSchema-instance", "type", "xs:string");
    writer.writeCharacters(value);
    writer.writeEndElement(); // </AttributeValue>

    writer.writeEndElement(); // </Attribute>
}

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);
    writer.writeEndElement();//from  ww  w .  java  2 s  . c om
}

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;// w  w  w.j  ava  2s. c o  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.chorusbdd.chorus.tools.webagent.jettyhandler.XmlStreamingHandler.java

protected void writeSimpleTextElement(XMLStreamWriter writer, String element, String elementText)
        throws XMLStreamException {
    writer.writeStartElement(element);/*from w  ww  . j  av  a2s. c  o  m*/
    writer.writeCharacters(elementText);
    writer.writeEndElement();
}

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

protected void addStylesheetInstruction(XMLStreamWriter writer, String stylesheetName)
        throws XMLStreamException {
    writer.writeProcessingInstruction("xml-stylesheet", "type='text/xsl' href='/" + stylesheetName + "'");
    writer.writeCharacters("\n");
}

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

/**
 * Writes a Salt project to the file given by {@link #getPath()}.
 * /*ww w  .  j a  v a  2s.com*/
 * @param project
 *            the Salt project to be written
 */
public void writeSaltProject(SaltProject project) {
    XMLStreamWriter xml = null;
    try (OutputStream output = new FileOutputStream(path)) {
        xml = xmlFactory.createXMLStreamWriter(output, "UTF-8");
        xml.writeStartDocument("1.0");
        if (isPrettyPrint) {
            xml.writeCharacters("\n");
        }
        writeSaltProject(xml, project);

        xml.writeEndDocument();

    } catch (XMLStreamException | IOException e) {
        throw new SaltResourceException("Cannot store salt project to file '" + getLocationStr() + "'. ", e);
    } finally {
        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);
            }
        }
    }
}