Example usage for javax.xml.stream XMLStreamWriter writeNamespace

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

Introduction

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

Prototype

public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException;

Source Link

Document

Writes a namespace to the output stream If the prefix argument to this method is the empty string, "xmlns", or null this method will delegate to writeDefaultNamespace

Usage

From source file:org.apache.olingo.client.core.serialization.AtomSerializer.java

private void reference(final Writer outWriter, final ResWrap<URI> container) throws XMLStreamException {
    final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);

    writer.writeStartDocument();//w w w .j av a2s  . c o m

    writer.writeStartElement(Constants.ATTR_METADATA, Constants.ATTR_REF, Constants.NS_METADATA);
    writer.writeNamespace(Constants.ATTR_METADATA, Constants.NS_METADATA);
    writer.writeAttribute(Constants.ATTR_METADATA, Constants.NS_METADATA, Constants.CONTEXT,
            container.getContextURL().toASCIIString());
    writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().toASCIIString());
    writer.writeEndElement();

    writer.writeEndDocument();
}

From source file:org.apache.olingo.commons.core.data.AbstractAtomDealer.java

protected void namespaces(final XMLStreamWriter writer) throws XMLStreamException {
    writer.writeNamespace(StringUtils.EMPTY, Constants.NS_ATOM);
    writer.writeNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    writer.writeNamespace(Constants.PREFIX_METADATA,
            version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
    writer.writeNamespace(Constants.PREFIX_DATASERVICES,
            version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES));
    writer.writeNamespace(Constants.PREFIX_GML, Constants.NS_GML);
    writer.writeNamespace(Constants.PREFIX_GEORSS, Constants.NS_GEORSS);
}

From source file:org.apache.olingo.commons.core.serialization.AbstractAtomDealer.java

protected void namespaces(final XMLStreamWriter writer) throws XMLStreamException {
    writer.writeNamespace(StringUtils.EMPTY, Constants.NS_ATOM);
    writer.writeNamespace(XMLConstants.XML_NS_PREFIX, XMLConstants.XML_NS_URI);
    writer.writeNamespace(Constants.PREFIX_METADATA,
            version.getNamespace(ODataServiceVersion.NamespaceKey.METADATA));
    writer.writeNamespace(Constants.PREFIX_DATASERVICES,
            version.getNamespace(ODataServiceVersion.NamespaceKey.DATASERVICES));
    writer.writeNamespace(Constants.PREFIX_GML, Constants.NS_GML);
    writer.writeNamespace(Constants.PREFIX_GEORSS, Constants.NS_GEORSS);
}

From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java

private void entityRef(final XMLStreamWriter writer, final Entity entity) throws XMLStreamException {
    writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF);
    writer.writeNamespace(StringUtils.EMPTY, namespaceMetadata);
    writer.writeAttribute(Constants.ATOM_ATTR_ID, entity.getId().toASCIIString());
}

From source file:org.apache.olingo.commons.core.serialization.AtomSerializer.java

private void entityRef(final XMLStreamWriter writer, final ResWrap<Entity> container)
        throws XMLStreamException {
    writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF);
    writer.writeNamespace(StringUtils.EMPTY, namespaceMetadata);
    addContextInfo(writer, container);//from  w w  w . ja  v  a  2s. c  o m
    writer.writeAttribute(Constants.ATOM_ATTR_ID, container.getPayload().getId().toASCIIString());
}

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

public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
    XMLStreamReader reader = getReader();
    xmlWriter.writeStartDocument();/*  w  ww.j av a2  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 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);//from   ww  w  .  ja  v a 2 s  .co m
    writer.writeEndElement(); // </AttributeValue>

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

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

public void writeAssertion(XMLStreamWriter writer) throws XMLStreamException {
    writer.writeStartElement("saml2", "Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml2", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeAttribute("ID", getId());
    writer.writeAttribute("IssueInstant", getIssueInstant());
    writer.writeAttribute("Version", "2.0");

    writeIssuer(writer);//from   w  ww. j a v  a  2  s.co m

    writeSubject(writer);

    writeConditions(writer);

    writeAuthnStatement(writer);

    writeAttributeStatement(writer);

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

}

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

/**
 * Get String with the SAML2 AuthnRequest message
 * @param format -1=plain, 1=base64// ww w  .j a va2s.  c  o  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);//w  ww .j  a  v a2 s .  c om
    writer.writeEndElement();
}