Java XMLStreamReader printEvent(XMLStreamReader xmlr)

Here you can find the source of printEvent(XMLStreamReader xmlr)

Description

print Event

License

Open Source License

Declaration

public static String printEvent(XMLStreamReader xmlr) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.xml.stream.XMLStreamReader;

public class Main {
    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  .  java 2s .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();
    }

    public static String getName(int eventType) {
        switch (eventType) {
        case XMLStreamReader.START_ELEMENT:
            return "START_ELEMENT";
        case XMLStreamReader.END_ELEMENT:
            return "END_ELEMENT";
        case XMLStreamReader.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLStreamReader.CHARACTERS:
            return "CHARACTERS";
        case XMLStreamReader.SPACE:
            return "SPACE";
        case XMLStreamReader.COMMENT:
            return "COMMENT";
        case XMLStreamReader.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLStreamReader.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLStreamReader.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLStreamReader.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLStreamReader.DTD:
            return "DTD";
        case XMLStreamReader.CDATA:
            return "CDATA";
        case XMLStreamReader.NAMESPACE:
            return "NAMESPACE";
        }
        return "UNKNOWN_EVENT_TYPE";
    }

    private static void printName(String prefix, String uri, String localName, StringBuffer b) {
        if (uri != null && !("".equals(uri)))
            b.append("['" + uri + "']:");
        if (prefix != null && !("".equals(prefix)))
            b.append(prefix + ":");
        if (localName != null)
            b.append(localName);
    }

    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);
        }
    }
}

Related

  1. isEndTag(XMLStreamReader reader, String tagLocalName)
  2. isStartTag(XMLStreamReader reader)
  3. logReader(Logger logger, String msg, XMLStreamReader reader)
  4. move(XMLStreamReader reader)
  5. moveReaderToEndCurrentTag(XMLStreamReader reader)
  6. printEvent(XMLStreamReader xmlr, boolean showEvents)
  7. printLiterally(final PrintStream os, final XMLStreamReader xmlReader)
  8. printText(XMLStreamReader xmlr)
  9. readEndTag(XMLStreamReader reader, String localName)