Java XMLStreamReader logReader(Logger logger, String msg, XMLStreamReader reader)

Here you can find the source of logReader(Logger logger, String msg, XMLStreamReader reader)

Description

log Reader

License

Open Source License

Declaration

public static void logReader(Logger logger, String msg, XMLStreamReader reader) 

Method Source Code


//package com.java2s;
/*//from  w w w . j a v a 2 s  .  c om
 * sulky-modules - several general-purpose modules.
 * Copyright (C) 2007-2014 Joern Huxhorn
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.slf4j.Logger;

import javax.xml.stream.XMLStreamConstants;

import javax.xml.stream.XMLStreamReader;

public class Main {
    private static final char SPACE = 0x20;
    private static final String NEWLINE = "\n";
    private static final String INDENT = "\t";

    public static void logReader(Logger logger, String msg, XMLStreamReader reader) {
        if (logger.isDebugEnabled()) {
            logger.debug(readerStatus(msg, reader));
        }

    }

    public static String readerStatus(String msg, XMLStreamReader reader) {
        int type = reader.getEventType();
        StringBuilder msgBuf = new StringBuilder(msg);
        msgBuf.append(NEWLINE);
        msgBuf.append(INDENT).append("eventType=").append(getEventTypeString(type)).append(NEWLINE);
        if (type == XMLStreamConstants.START_ELEMENT || type == XMLStreamConstants.END_ELEMENT) {
            msgBuf.append(INDENT).append("localName=").append(reader.getLocalName()).append(NEWLINE);
            msgBuf.append(INDENT).append("namespaceURI=").append(reader.getNamespaceURI()).append(NEWLINE);
        }
        if (type == XMLStreamConstants.START_ELEMENT) {
            int attCount = reader.getAttributeCount();
            msgBuf.append(INDENT).append("attributeCount=").append(attCount).append(NEWLINE);
            for (int i = 0; i < attCount; i++) {
                msgBuf.append(INDENT).append(INDENT).append("#####\n");
                msgBuf.append(INDENT).append(INDENT).append("attributeNamespace=")
                        .append(reader.getAttributeNamespace(i)).append(NEWLINE);
                msgBuf.append(INDENT).append(INDENT).append("attributeLocalName=")
                        .append(reader.getAttributeLocalName(i)).append(NEWLINE);
                msgBuf.append(INDENT).append(INDENT).append("attributeValue=").append(reader.getAttributeValue(i))
                        .append(NEWLINE);
                msgBuf.append(INDENT).append(INDENT).append("attributePrefix=").append(reader.getAttributePrefix(i))
                        .append(NEWLINE);
            }

        }
        msgBuf.append(INDENT).append("readerClass: ").append(reader.getClass().getName());
        return msgBuf.toString();
    }

    public static String getEventTypeString(int eventType) {
        switch (eventType) {
        case XMLStreamConstants.ATTRIBUTE:
            return "ATTRIBUTE";
        case XMLStreamConstants.CDATA:
            return "CDATA";
        case XMLStreamConstants.CHARACTERS:
            return "CHARACTERS";
        case XMLStreamConstants.COMMENT:
            return "COMMENT";
        case XMLStreamConstants.DTD:
            return "DTD";
        case XMLStreamConstants.END_DOCUMENT:
            return "END_DOCUMENT";
        case XMLStreamConstants.END_ELEMENT:
            return "END_ELEMENT";
        case XMLStreamConstants.ENTITY_DECLARATION:
            return "ENTITY_DECLARATION";
        case XMLStreamConstants.ENTITY_REFERENCE:
            return "ENTITY_REFERENCE";
        case XMLStreamConstants.NAMESPACE:
            return "NAMESPACE";
        case XMLStreamConstants.NOTATION_DECLARATION:
            return "NOTATION_DECLARATION";
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            return "PROCESSING_INSTRUCTION";
        case XMLStreamConstants.SPACE:
            return "SPACE";
        case XMLStreamConstants.START_DOCUMENT:
            return "START_DOCUMENT";
        case XMLStreamConstants.START_ELEMENT:
            return "START_ELEMENT";
        default:
            return "<unknown type!>";
        }
    }
}

Related

  1. getXMLStreamReader(InputStream is)
  2. getXSIType(XMLStreamReader reader)
  3. isContextual(XMLStreamReader input)
  4. isEndTag(XMLStreamReader reader, String tagLocalName)
  5. isStartTag(XMLStreamReader reader)
  6. move(XMLStreamReader reader)
  7. moveReaderToEndCurrentTag(XMLStreamReader reader)
  8. printEvent(XMLStreamReader xmlr)
  9. printEvent(XMLStreamReader xmlr, boolean showEvents)