Example usage for javax.xml.stream XMLStreamReader getNamespacePrefix

List of usage examples for javax.xml.stream XMLStreamReader getNamespacePrefix

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader getNamespacePrefix.

Prototype

public String getNamespacePrefix(int index);

Source Link

Document

Returns the prefix for the namespace declared at the index.

Usage

From source file:Main.java

private static void printNamespace(XMLStreamReader xmlr, int index, StringBuffer result) {
    String prefix = xmlr.getNamespacePrefix(index);
    String uri = xmlr.getNamespaceURI(index);
    result.append(" ");
    if (prefix == null)
        result.append("xmlns='").append(uri).append("'");
    else/*from  ww  w .  j a  v a 2 s.c  om*/
        result.append("xmlns:").append(prefix).append("='").append(uri).append("'");
}

From source file:edu.utah.further.core.api.xml.XmlUtil.java

/**
 * @param xmlReader//from  w  w w .  j  a va2  s .  c  o m
 * @param index
 */
private static void printNamespace(final PrintStream os, final XMLStreamReader xmlReader, final int index) {
    final String prefix = xmlReader.getNamespacePrefix(index);
    final String uri = xmlReader.getNamespaceURI(index);
    os.print(Strings.SPACE_STRING);
    if (StringUtils.isBlank(prefix)) {
        os.print("xmlns=" + quote(uri));
    } else {
        os.print("xmlns:" + prefix + "=" + quote(uri));
    }
}

From source file:Main.java

public static void printAttributes(XMLStreamReader xmlr) {
    int count = xmlr.getAttributeCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print(xmlr.getAttributeName(i).toString());
            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getAttributeValue(i));
            System.out.print("\"");
        }//from ww  w .j  ava 2 s. c  o  m
    }

    count = xmlr.getNamespaceCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print("xmlns");

            if (xmlr.getNamespacePrefix(i) != null) {
                System.out.print(":" + xmlr.getNamespacePrefix(i));
            }

            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getNamespaceURI(i));
            System.out.print("\"");
        }
    }
}

From source file:StaxCursorTest.java

private static void printAttributes(XMLStreamReader xmlr) {
    int count = xmlr.getAttributeCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print(xmlr.getAttributeName(i).toString());
            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getAttributeValue(i));
            System.out.print("\"");
        }// ww  w  . ja v  a2  s .c  o m
    }

    count = xmlr.getNamespaceCount();

    if (count > 0) {
        for (int i = 0; i < count; i++) {
            System.out.print(" ");
            System.out.print("xmlns");

            if (xmlr.getNamespacePrefix(i) != null) {
                System.out.print(":" + xmlr.getNamespacePrefix(i));
            }

            System.out.print("=");
            System.out.print("\"");
            System.out.print(xmlr.getNamespaceURI(i));
            System.out.print("\"");
        }
    }
}

From source file:XmlStreamUtils.java

public static String printEvent(XMLStreamReader xmlr) {
    StringBuffer b = new StringBuffer();
    b.append("EVENT:[" + xmlr.getLocation().getLineNumber() + "][" + xmlr.getLocation().getColumnNumber()
            + "] ");
    b.append(getName(xmlr.getEventType()));
    b.append(" [");
    switch (xmlr.getEventType()) {
    case XMLStreamReader.START_ELEMENT:
        b.append("<");
        printName(xmlr, b);/*from  ww  w.ja va  2 s  . co  m*/
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }

        for (int i = 0; i < xmlr.getAttributeCount(); i++) {
            b.append(" ");
            printName(xmlr.getAttributePrefix(i), xmlr.getAttributeNamespace(i), xmlr.getAttributeLocalName(i),
                    b);
            b.append("=\"");
            b.append(xmlr.getAttributeValue(i));
            b.append("\"");
        }

        b.append(">");
        break;
    case XMLStreamReader.END_ELEMENT:
        b.append("</");
        printName(xmlr, b);
        for (int i = 0; i < xmlr.getNamespaceCount(); i++) {
            b.append(" ");
            String n = xmlr.getNamespacePrefix(i);
            if ("xmlns".equals(n)) {
                b.append("xmlns=\"" + xmlr.getNamespaceURI(i) + "\"");
            } else {
                b.append("xmlns:" + n);
                b.append("=\"");
                b.append(xmlr.getNamespaceURI(i));
                b.append("\"");
            }
        }
        b.append(">");
        break;
    case XMLStreamReader.SPACE:
    case XMLStreamReader.CHARACTERS:
        // b.append(xmlr.getText());
        int start = xmlr.getTextStart();
        int length = xmlr.getTextLength();
        b.append(new String(xmlr.getTextCharacters(), start, length));
        break;
    case XMLStreamReader.PROCESSING_INSTRUCTION:
        String target = xmlr.getPITarget();
        if (target == null)
            target = "";
        String data = xmlr.getPIData();
        if (data == null)
            data = "";
        b.append("<?");
        b.append(target + " " + data);
        b.append("?>");
        break;
    case XMLStreamReader.CDATA:
        b.append("<![CDATA[");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("]]&gt;");
        break;

    case XMLStreamReader.COMMENT:
        b.append("<!--");
        if (xmlr.hasText())
            b.append(xmlr.getText());
        b.append("-->");
        break;
    case XMLStreamReader.ENTITY_REFERENCE:
        b.append(xmlr.getLocalName() + "=");
        if (xmlr.hasText())
            b.append("[" + xmlr.getText() + "]");
        break;
    case XMLStreamReader.START_DOCUMENT:
        b.append("<?xml");
        b.append(" version='" + xmlr.getVersion() + "'");
        b.append(" encoding='" + xmlr.getCharacterEncodingScheme() + "'");
        if (xmlr.isStandalone())
            b.append(" standalone='yes'");
        else
            b.append(" standalone='no'");
        b.append("?>");
        break;

    }
    b.append("]");
    return b.toString();
}

From source file:XmlReaderToWriter.java

public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null)
                writer.writeStartElement(prefix, localName, namespaceURI);
            else/*w w  w.  ja va  2  s .co  m*/
                writer.writeStartElement(namespaceURI, localName);
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            writer.writeNamespace(xmlr.getNamespacePrefix(i), xmlr.getNamespaceURI(i));
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            String attUri = xmlr.getAttributeNamespace(i);
            if (attUri != null)
                writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            else
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        writer.writeCharacters(xmlr.getTextCharacters(), xmlr.getTextStart(), xmlr.getTextLength());
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;

    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null)
            writer.writeStartDocument(encoding, version);
        else if (version != null)
            writer.writeStartDocument(xmlr.getVersion());
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:Main.java

/**
 * Borrowed from org.apache.xml.security.test.stax.utils.XmlReaderToWriter
 *///from   w ww .j a  v  a 2s.co  m
public static void write(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException {
    switch (xmlr.getEventType()) {
    case XMLEvent.START_ELEMENT:
        final String localName = xmlr.getLocalName();
        final String namespaceURI = xmlr.getNamespaceURI();
        if (namespaceURI != null && namespaceURI.length() > 0) {
            final String prefix = xmlr.getPrefix();
            if (prefix != null) {
                writer.writeStartElement(prefix, localName, namespaceURI);
            } else {
                writer.writeStartElement(namespaceURI, localName);
            }
        } else {
            writer.writeStartElement(localName);
        }

        for (int i = 0, len = xmlr.getNamespaceCount(); i < len; i++) {
            String prefix = xmlr.getNamespacePrefix(i);
            if (prefix == null) {
                writer.writeDefaultNamespace(xmlr.getNamespaceURI(i));
            } else {
                writer.writeNamespace(prefix, xmlr.getNamespaceURI(i));
            }
        }

        for (int i = 0, len = xmlr.getAttributeCount(); i < len; i++) {
            final String attUri = xmlr.getAttributeNamespace(i);

            if (attUri != null && attUri.length() > 0) {
                final String prefix = xmlr.getAttributePrefix(i);
                if (prefix != null) {
                    writer.writeAttribute(prefix, attUri, xmlr.getAttributeLocalName(i),
                            xmlr.getAttributeValue(i));
                } else {
                    writer.writeAttribute(attUri, xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                }
            } else {
                writer.writeAttribute(xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
            }

        }
        break;
    case XMLEvent.END_ELEMENT:
        writer.writeEndElement();
        break;
    case XMLEvent.SPACE:
    case XMLEvent.CHARACTERS:
        char[] text = new char[xmlr.getTextLength()];
        xmlr.getTextCharacters(0, text, 0, xmlr.getTextLength());
        writer.writeCharacters(text, 0, text.length);
        break;
    case XMLEvent.PROCESSING_INSTRUCTION:
        writer.writeProcessingInstruction(xmlr.getPITarget(), xmlr.getPIData());
        break;
    case XMLEvent.CDATA:
        writer.writeCData(xmlr.getText());
        break;
    case XMLEvent.COMMENT:
        writer.writeComment(xmlr.getText());
        break;
    case XMLEvent.ENTITY_REFERENCE:
        writer.writeEntityRef(xmlr.getLocalName());
        break;
    case XMLEvent.START_DOCUMENT:
        String encoding = xmlr.getCharacterEncodingScheme();
        String version = xmlr.getVersion();

        if (encoding != null && version != null) {
            writer.writeStartDocument(encoding, version);
        } else if (version != null) {
            writer.writeStartDocument(xmlr.getVersion());
        }
        break;
    case XMLEvent.END_DOCUMENT:
        writer.writeEndDocument();
        break;
    case XMLEvent.DTD:
        writer.writeDTD(xmlr.getText());
        break;
    }
}

From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLDictionaryParser.java

private void parseNamespaces(SMInputCursor cursor) throws XMLStreamException {
    XMLStreamReader xml = cursor.getStreamReader();

    for (int i = 0; i < xml.getNamespaceCount(); i++) {
        analyzer.analyzeNamespace(xml.getNamespacePrefix(i), xml.getNamespaceURI(i));
    }//from w  w w .  ja va 2 s.c  o  m
}

From source file:de.ii.xtraplatform.feature.provider.wfs.WFSCapabilitiesParser.java

private void parseNamespaces(SMInputCursor cursor) throws XMLStreamException {
    XMLStreamReader xml = cursor.getStreamReader();

    for (int i = 0; i < xml.getNamespaceCount(); i++) {
        metadataConsumer.analyzeNamespace(xml.getNamespacePrefix(i), xml.getNamespaceURI(i));
    }/*  ww  w.  j  a v  a 2s .c  o  m*/
}

From source file:com.prowidesoftware.swift.io.parser.MxParser.java

/**
 * Gets the namespace, if any, from current position in the parameter reader
 * @since 7.8.4//from ww  w .ja  v  a  2s. c  o  m
 */
private String readNamespace(final javax.xml.stream.XMLStreamReader reader) {
    if (reader.getNamespaceCount() > 0) {
        //log.finest("ELEMENT START: " + reader.getLocalName() + " , namespace count is: " + reader.getNamespaceCount());
        for (int nsIndex = 0; nsIndex < reader.getNamespaceCount(); nsIndex++) {
            final String nsPrefix = StringUtils.trimToNull(reader.getNamespacePrefix(nsIndex));
            final String elementPrefix = StringUtils.trimToNull(reader.getPrefix());
            if (StringUtils.equals(nsPrefix, elementPrefix)) {
                //log.finest("\tNamepsace prefix: " + nsPrefix + " associated with URI " + nsId);
                return reader.getNamespaceURI(nsIndex);
            }
        }
    }
    return null;
}