Example usage for javax.xml.stream XMLStreamWriter getPrefix

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

Introduction

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

Prototype

public String getPrefix(String uri) throws XMLStreamException;

Source Link

Document

Gets the prefix the uri is bound to

Usage

From source file:org.apache.axis2.policy.model.MTOM11Assertion.java

public void serialize(XMLStreamWriter writer) throws XMLStreamException {
    String prefix = writer.getPrefix(NS);

    if (prefix == null) {
        prefix = PREFIX;//  ww  w  . ja  v a 2 s  .  c  o m
        writer.setPrefix(PREFIX, NS);
    }

    writer.writeStartElement(PREFIX, MTOM_LN, NS);

    if (optional) {
        writer.writeAttribute(Constants.ATTR_WSP, null, Constants.Q_ELEM_OPTIONAL_ATTR.getLocalPart(), "true");
    }

    writer.writeNamespace(PREFIX, NS);
    writer.writeEndElement();

}

From source file:org.mule.module.xml.util.XMLUtils.java

private static void writeStartElement(XMLStreamReader reader, XMLStreamWriter writer)
        throws XMLStreamException {
    String local = reader.getLocalName();
    String uri = reader.getNamespaceURI();
    String prefix = reader.getPrefix();
    if (prefix == null) {
        prefix = "";
    }/*  w  ww. j ava  2 s .  c o m*/

    //        System.out.println("STAXUTILS:writeStartElement : node name : " + local +  " namespace URI" + uri);
    boolean writeElementNS = false;
    if (uri != null) {
        String boundPrefix = writer.getPrefix(uri);
        if (boundPrefix == null || !prefix.equals(boundPrefix)) {
            writeElementNS = true;
        }
    }

    // Write out the element name
    if (uri != null) {
        if (prefix.length() == 0 && StringUtils.isEmpty(uri)) {
            writer.writeStartElement(local);
            writer.setDefaultNamespace(uri);

        } else {
            writer.writeStartElement(prefix, local, uri);
            writer.setPrefix(prefix, uri);
        }
    } else {
        writer.writeStartElement(local);
    }

    // Write out the namespaces
    for (int i = 0; i < reader.getNamespaceCount(); i++) {
        String nsURI = reader.getNamespaceURI(i);
        String nsPrefix = reader.getNamespacePrefix(i);
        if (nsPrefix == null) {
            nsPrefix = "";
        }

        if (nsPrefix.length() == 0) {
            writer.writeDefaultNamespace(nsURI);
        } else {
            writer.writeNamespace(nsPrefix, nsURI);
        }

        if (nsURI.equals(uri) && nsPrefix.equals(prefix)) {
            writeElementNS = false;
        }
    }

    // Check if the namespace still needs to be written.
    // We need this check because namespace writing works
    // different on Woodstox and the RI.
    if (writeElementNS) {
        if (prefix.length() == 0) {
            writer.writeDefaultNamespace(uri);
        } else {
            writer.writeNamespace(prefix, uri);
        }
    }

    // Write out attributes
    for (int i = 0; i < reader.getAttributeCount(); i++) {
        String ns = reader.getAttributeNamespace(i);
        String nsPrefix = reader.getAttributePrefix(i);
        if (ns == null || ns.length() == 0) {
            writer.writeAttribute(reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        } else if (nsPrefix == null || nsPrefix.length() == 0) {
            writer.writeAttribute(reader.getAttributeNamespace(i), reader.getAttributeLocalName(i),
                    reader.getAttributeValue(i));
        } else {
            writer.writeAttribute(reader.getAttributePrefix(i), reader.getAttributeNamespace(i),
                    reader.getAttributeLocalName(i), reader.getAttributeValue(i));
        }

    }
}

From source file:org.slc.sli.modeling.wadl.writer.WadlWriter.java

private static final String toLexicalForm(final QName name, final XMLStreamWriter xsw)
        throws XMLStreamException {
    final String namespaceURI = name.getNamespaceURI();
    if (namespaceURI.length() != 0) {
        final String prefix = xsw.getPrefix(namespaceURI);
        if (prefix == null) {
            throw new IllegalArgumentException(name.toString());
        }/* ww w.j a  va 2 s  .c  o m*/
        if (prefix.length() != 0) {
            return prefix.concat(":").concat(name.getLocalPart());
        } else {
            return name.getLocalPart();
        }
    } else {
        return name.getLocalPart();
    }
}