Java XML Parse Stream parseNoContent(final XMLStreamReader reader)

Here you can find the source of parseNoContent(final XMLStreamReader reader)

Description

parse No Content

License

Apache License

Declaration

public static void parseNoContent(final XMLStreamReader reader) throws XMLStreamException 

Method Source Code

//package com.java2s;
/*//ww w .j av  a 2  s .c o  m
 * Copyright 2016 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class Main {
    public static void parseNoContent(final XMLStreamReader reader) throws XMLStreamException {
        while (reader.hasNext()) {
            switch (reader.nextTag()) {
            case XMLStreamConstants.END_ELEMENT: {
                return;
            }
            default: {
                throw unexpectedContent(reader);
            }
            }
        }
        throw endOfDocument(reader.getLocation());
    }

    public static XMLStreamException unexpectedContent(final XMLStreamReader reader) {
        final String kind;
        switch (reader.getEventType()) {
        case XMLStreamConstants.ATTRIBUTE:
            kind = "attribute";
            break;
        case XMLStreamConstants.CDATA:
            kind = "cdata";
            break;
        case XMLStreamConstants.CHARACTERS:
            kind = "characters";
            break;
        case XMLStreamConstants.COMMENT:
            kind = "comment";
            break;
        case XMLStreamConstants.DTD:
            kind = "dtd";
            break;
        case XMLStreamConstants.END_DOCUMENT:
            kind = "document end";
            break;
        case XMLStreamConstants.END_ELEMENT:
            kind = "element end";
            break;
        case XMLStreamConstants.ENTITY_DECLARATION:
            kind = "entity declaration";
            break;
        case XMLStreamConstants.ENTITY_REFERENCE:
            kind = "entity ref";
            break;
        case XMLStreamConstants.NAMESPACE:
            kind = "namespace";
            break;
        case XMLStreamConstants.NOTATION_DECLARATION:
            kind = "notation declaration";
            break;
        case XMLStreamConstants.PROCESSING_INSTRUCTION:
            kind = "processing instruction";
            break;
        case XMLStreamConstants.SPACE:
            kind = "whitespace";
            break;
        case XMLStreamConstants.START_DOCUMENT:
            kind = "document start";
            break;
        case XMLStreamConstants.START_ELEMENT:
            kind = "element start";
            break;
        default:
            kind = "unknown";
            break;
        }

        return new XMLStreamException("unexpected content: " + kind + (reader.hasName() ? reader.getName() : null)
                + (reader.hasText() ? reader.getText() : null), reader.getLocation());
    }

    public static XMLStreamException endOfDocument(final Location location) {
        return new XMLStreamException("Unexpected end of document ", location);
    }
}

Related

  1. parseDoc(final InputStream is)
  2. parseInputStream(InputStream in, boolean namespaces, boolean validating)
  3. parseInputStream(InputStream is)
  4. parseMulXML( InputStream is, String[] tagNames)
  5. parseNamespace(XMLStreamReader reader)
  6. parseNumber(XMLEventReader stream)
  7. parser(InputStream inputStream)
  8. parseStream(InputStream stream, boolean validate, boolean expandEntityRefs)
  9. parseStreamToXML(InputStream in)