Example usage for javax.xml.stream XMLStreamReader getNamespaceURI

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

Introduction

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

Prototype

public String getNamespaceURI(int index);

Source Link

Document

Returns the uri 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 w  w w .ja  va 2s  .  c  o  m
        result.append("xmlns:").append(prefix).append("='").append(uri).append("'");
}

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

/**
 * @param xmlReader//  w  ww . j a  v  a 2s.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: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 w  ww  . j  a  v a2s  .  c  om*/
        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: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 w w w  . ja v a 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("\"");
        }//from   w ww .ja v a  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: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 ww  w .  j  a v  a2s  .com*/
}

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));
    }//from w ww.  j  a va2  s.  co  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//  www  .  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;
}

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

/**
 * Takes an xml with an MX message and detects the specific message type
 * parsing just the namespace from the Document element. If the Document
 * element is not present, or without the namespace or if the namespace url
 * contains invalid content it will return null.
 * <br><br>//from   w w  w .j a  va2s  . c om
 * Example of a recognizable Document element:<br>
 * <Doc:Document xmlns:Doc="urn:swift:xsd:camt.003.001.04" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * <br>
 * The implementation is intended to be lightweight and efficient, based on {@link javax.xml.stream.XMLStreamReader} 
 *
 * @return id with the detected MX message type or null if it cannot be determined.
 * @since 7.7
 */
public MxId detectMessage() {
    if (StringUtils.isBlank(this.buffer)) {
        log.log(Level.SEVERE, "cannot detect message from null or empty content");
        return null;
    }
    final javax.xml.stream.XMLInputFactory xif = javax.xml.stream.XMLInputFactory.newInstance();
    try {
        final javax.xml.stream.XMLStreamReader reader = xif
                .createXMLStreamReader(new StringReader(this.buffer));
        while (reader.hasNext()) {
            int event = reader.next();
            if (javax.xml.stream.XMLStreamConstants.START_ELEMENT == event
                    && reader.getLocalName().equals(DOCUMENT_LOCALNAME)) {
                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)) {
                            String nsId = reader.getNamespaceURI(nsIndex);
                            //log.finest("\tNamepsace prefix: " + nsPrefix + " associated with URI " + nsId);
                            return new MxId(nsId);
                        }
                    }
                }
            }
        }
    } catch (final Exception e) {
        log.log(Level.SEVERE, "error while detecting message", e);
    }
    return null;
}

From source file:com.evolveum.midpoint.prism.lex.dom.DomLexicalProcessor.java

@Override
public void readObjectsIteratively(@NotNull ParserSource source, @NotNull ParsingContext parsingContext,
        RootXNodeHandler handler) throws SchemaException, IOException {
    InputStream is = source.getInputStream();
    XMLStreamReader stream = null;
    try {/*from   www .  ja v a  2 s.c o m*/
        stream = XMLInputFactory.newInstance().createXMLStreamReader(is);

        int eventType = stream.nextTag();
        if (eventType != XMLStreamConstants.START_ELEMENT) {
            throw new SystemException("StAX Malfunction?");
        }
        DOMConverter domConverter = new DOMConverter();
        Map<String, String> rootNamespaceDeclarations = new HashMap<>();

        QName objectsMarker = schemaRegistry.getPrismContext().getObjectsElementName();
        if (objectsMarker != null && !QNameUtil.match(stream.getName(), objectsMarker)) {
            readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler);
        }
        for (int i = 0; i < stream.getNamespaceCount(); i++) {
            rootNamespaceDeclarations.put(stream.getNamespacePrefix(i), stream.getNamespaceURI(i));
        }
        while (stream.hasNext()) {
            eventType = stream.next();
            if (eventType == XMLStreamConstants.START_ELEMENT) {
                if (!readSingleObjectIteratively(stream, rootNamespaceDeclarations, domConverter, handler)) {
                    return;
                }
            }
        }
    } catch (XMLStreamException ex) {
        String lineInfo = stream != null ? " on line " + stream.getLocation().getLineNumber() : "";
        throw new SchemaException("Exception while parsing XML" + lineInfo + ": " + ex.getMessage(), ex);
    } finally {
        if (source.closeStreamAfterParsing()) {
            IOUtils.closeQuietly(is);
        }
    }
}