Example usage for javax.xml.stream XMLStreamReader getPrefix

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

Introduction

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

Prototype

public String getPrefix();

Source Link

Document

Returns the prefix of the current event or null if the event does not have a prefix

Usage

From source file:Main.java

private static void printName(XMLStreamReader xmlr, StringBuffer b) {
    if (xmlr.hasName()) {
        String prefix = xmlr.getPrefix();
        String uri = xmlr.getNamespaceURI();
        String localName = xmlr.getLocalName();
        printName(prefix, uri, localName, b);
    }//from   w  w  w  .  ja va2s  .  c  om
}

From source file:Main.java

private static void printName(XMLStreamReader xmlr, StringBuffer result) {
    if (xmlr.hasName()) {
        String prefix = xmlr.getPrefix();
        String uri = xmlr.getNamespaceURI();
        String localName = xmlr.getLocalName();
        printName(prefix, uri, localName, result);
    }/*from  www  . j  a v a2s  .  c o m*/
}

From source file:Main.java

private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:
            System.out.println(reader.getLocalName());
            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }//from  w  ww  .j  a v a2  s.com
                System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                            + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        System.out.println("prefix=" + attPrefix + ",URI=" + attURI);
                    }
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            System.out.println("XMLStreamConstants.END_ELEMENT");
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                System.out.println("CD:" + reader.getText());
            }
            break;
        case XMLStreamConstants.DTD:
            System.out.println("DTD:" + reader.getText());
            break;
        case XMLStreamConstants.SPACE:
            System.out.println(" ");
            break;
        case XMLStreamConstants.COMMENT:
            System.out.println(reader.getText());
            break;
        default:
            System.out.println(type);
        }
    }
}

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/*from ww  w .j  ava2s. c o  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
 *//* w w w  . ja  va  2s.  c o  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:edu.utah.further.core.api.xml.XmlUtil.java

/**
 * @param xmlReader//from   w  w  w  . jav  a 2  s  .c om
 * @return
 */
public static String getName(final XMLStreamReader xmlReader) {
    if (xmlReader.hasName()) {
        final String prefix = xmlReader.getPrefix();
        final String uri = xmlReader.getNamespaceURI();
        final String localName = xmlReader.getLocalName();
        return getName(prefix, uri, localName);
    }
    return null;
}

From source file:lux.index.XmlPathMapper.java

private void getEventQName(MutableString buf, XMLStreamReader reader) {
    encodeQName(buf, reader.getLocalName(), reader.getPrefix(), reader.getNamespaceURI());
}

From source file:StAXStreamTreeViewer.java

private void parseRestOfDocument(XMLStreamReader reader, DefaultMutableTreeNode current)
        throws XMLStreamException {

    while (reader.hasNext()) {
        int type = reader.next();
        switch (type) {
        case XMLStreamConstants.START_ELEMENT:

            DefaultMutableTreeNode element = new DefaultMutableTreeNode(reader.getLocalName());
            current.add(element);//www. ja  va2  s  . c om
            current = element;

            if (reader.getNamespaceURI() != null) {
                String prefix = reader.getPrefix();
                if (prefix == null) {
                    prefix = "[None]";
                }
                DefaultMutableTreeNode namespace = new DefaultMutableTreeNode(
                        "prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'");
                current.add(namespace);
            }

            if (reader.getAttributeCount() > 0) {
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(
                            "Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '"
                                    + reader.getAttributeValue(i) + "')");
                    String attURI = reader.getAttributeNamespace(i);
                    if (attURI != null) {
                        String attPrefix = reader.getAttributePrefix(i);
                        if (attPrefix == null || attPrefix.equals("")) {
                            attPrefix = "[None]";
                        }
                        DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode(
                                "prefix=" + attPrefix + ",URI=" + attURI);
                        attribute.add(attNamespace);
                    }
                    current.add(attribute);
                }
            }

            break;
        case XMLStreamConstants.END_ELEMENT:
            current = (DefaultMutableTreeNode) current.getParent();
            break;
        case XMLStreamConstants.CHARACTERS:
            if (!reader.isWhiteSpace()) {
                DefaultMutableTreeNode data = new DefaultMutableTreeNode("CD:" + reader.getText());
                current.add(data);
            }
            break;
        case XMLStreamConstants.DTD:
            DefaultMutableTreeNode dtd = new DefaultMutableTreeNode("DTD:" + reader.getText());
            current.add(dtd);
            break;
        case XMLStreamConstants.SPACE:
            break;
        case XMLStreamConstants.COMMENT:
            DefaultMutableTreeNode comment = new DefaultMutableTreeNode(reader.getText());
            current.add(comment);
            break;
        default:
            System.out.println(type);
        }
    }
}

From source file:lux.search.highlight.XmlHighlighter.java

@Override
public void handleEvent(XMLStreamReader reader, int eventType) throws XMLStreamException {

    switch (eventType) {
    case XMLStreamConstants.START_DOCUMENT:
        xmlStreamReader = reader; // cache the reader so we can pull events
        super.handleEvent(reader, eventType);
        break;/*from w  w w.j  a v a2 s. c  om*/

    case XMLStreamConstants.START_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens
                .pushElement(new QName(reader.getNamespaceURI(), reader.getLocalName(), reader.getPrefix()));
        break;

    case XMLStreamConstants.END_ELEMENT:
        super.handleEvent(reader, eventType);
        xmlStreamTokens.popElement();
        break;

    case XMLStreamConstants.COMMENT:
    case XMLStreamConstants.PROCESSING_INSTRUCTION:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CDATA:
        throw new XMLStreamException("unexpected CDATA event");

    case XMLStreamConstants.SPACE:
        super.handleEvent(reader, eventType);
        break;

    case XMLStreamConstants.CHARACTERS:
        textReader.text();
        try {
            highlightTextNode();
        } catch (IOException e) {
            throw new XMLStreamException(e);
        }
        break;

    case XMLStreamConstants.ENTITY_REFERENCE:
        throw new XMLStreamException("unexpected entity reference event");

    default:
        super.handleEvent(reader, eventType);
    }

}

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/* w w  w.j  a v  a  2 s . 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;
}