Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import javax.xml.stream.XMLEventReader;

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

import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.util.XMLEventConsumer;

public class Main {
    /**
     * Skips all events within a single element, including its start and end
     * tags. The provided reader must be positioned directly in front of a
     * <code>StartElement</code> event or it will have no effect. After this
     * method completes, the reader will be positioned before the event
     * following the end tag (the end tag will have been read).
     * 
     * @param reader The event stream to read.
     * @throws XMLStreamException If an error occurs reading events.
     */
    public static final void skipElement(XMLEventReader reader) throws XMLStreamException {

        copyElement(reader, null);

    }

    /**
     * Skips the complete content of the element at the specified reader's
     * cursor. The reader's current event type must be START_ELEMENT, otherwise
     * this method will have no effect. Upon completion, the reader's cursor
     * will be at the END_ELEMENT event for the skipped element.
     * 
     * @param reader An XML stream reader currently in the START_ELEMENT event.
     */
    public static final void skipElement(XMLStreamReader reader) throws XMLStreamException {

        if (reader.isStartElement()) {

            skipElementContent(reader);

        }

    }

    /**
     * Copies an element and all its content from the provided event reader, to
     * the provided event consumer. The event reader must be positioned before a
     * start element event, or this method has no effect.
     * 
     * @param reader The reader from which to read the events.
     * @param consumer The destination for read events, or <code>null</code> to
     *       ignore all events.
     * @throws XMLStreamException If an error occurs reading or writing the
     *             events.
     */
    public static final void copyElement(XMLEventReader reader, XMLEventConsumer consumer)
            throws XMLStreamException {

        if (!reader.hasNext())
            return;

        XMLEvent event = reader.peek();
        if (!event.isStartElement())
            return;

        int depth = 0;
        do {

            XMLEvent currEvt = reader.nextEvent();
            if (currEvt.isStartElement()) {

                depth++;

            } else if (currEvt.isEndElement()) {

                depth--;

            }

            if (consumer != null) {

                consumer.add(currEvt);

            }

        } while (depth > 0 && reader.hasNext());

    }

    /**
     * Skips all events within a <code>StartElement</code> until the matching
     * <code>EndElement</code> is reached. This method assumes that the reader
     * is positioned after the <code>StartElement</code> event, and when the
     * method completes, the stream will be positioned before the
     * <code>EndElement</code> event, but it will not consume the end tag.
     * 
     * @param reader The event stream to read, positioned after the
     *       <code>StartElement</code>
     * @throws XMLStreamException If an error occurs reading events.
     */
    public static final void skipElementContent(XMLEventReader reader) throws XMLStreamException {

        copyElementContent(reader, null);

    }

    /**
     * Skips an element's complete content. This method assumes that the
     * <code>START_ELEMENT</code> has already be passed, and when it terminates,
     * the stream will be positioned at the <code>END_ELEMENT</code>.
     * 
     * @param reader The stream reader to read.
     * @throws XMLStreamException If an error occurs reading the stream.
     */
    public static final void skipElementContent(XMLStreamReader reader) throws XMLStreamException {

        int depth = 0;
        while (depth >= 0) {

            reader.next();
            if (reader.isStartElement()) {

                depth++;

            } else if (reader.isEndElement()) {

                depth--;

            }

        }

    }

    /**
     * Copies all events within a <code>StartElement</code> until the matching
     * <code>EndElement</code> is reached. This method assumes that the reader
     * is positioned after the <code>StartElement</code> event, and when the
     * method completes, the stream will be positioned before the 
     * <code>EndElement</code> event, but it will not consume the end tag.
     * 
     * @param reader The event stream to read, positioned after the
     *       <code>StartElement</code>
     * @param consumer The destination for events read from teh stream, or
     *       <code>null</code> to ignore the events completely.
     * @throws XMLStreamException If an error occurs reading events.
     */
    public static final void copyElementContent(XMLEventReader reader, XMLEventConsumer consumer)
            throws XMLStreamException {

        if (!reader.hasNext())
            return;

        for (int depth = 1; true;) {

            // peek and see if we're at the end element
            XMLEvent currEvt = reader.peek();
            if (currEvt.isEndElement()) {

                depth--;
                if (depth == 0) {

                    break;

                }

            } else if (currEvt.isStartElement()) {

                depth++;

            }

            // consume the event
            currEvt = reader.nextEvent();

            if (consumer != null) {

                consumer.add(currEvt);

            }

        }

    }
}