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.events.XMLEvent;

public class Main {
    public static boolean nextTagIsEndOf(String name, XMLEventReader xml) throws XMLStreamException {
        skipWhitespaceAndComments(xml);
        return isEndTag(xml.peek(), name);
    }

    public static void skipWhitespaceAndComments(XMLEventReader xml) {
        XMLEvent peek = peek(xml);
        while (isWhitespace(peek) || isComment(peek)) {
            nextEvent(xml);
            peek = peek(xml);
        }
    }

    public static boolean isEndTag(XMLEvent e, String localName) {
        return e.isEndElement() && localNameOf(e).equals(localName);
    }

    /**
     * Calls xml.peek() and unchecks the exception. 
     * @param xml
     * @return
     */
    public static XMLEvent peek(XMLEventReader xml) {
        try {
            return xml.peek();
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }
    }

    public static boolean isWhitespace(XMLEvent e) {
        if (e.isCharacters() && e.asCharacters().isWhiteSpace()) {
            return true;
        }
        return false;
    }

    public static boolean isComment(XMLEvent e) {
        if (e.getEventType() == XMLEvent.COMMENT) {
            return true;
        }
        return false;
    }

    /** 
     * Calls xml.nextEvent() and unchecks the exception.
     * 
     * @param xml
     * @return xml.nextEvent(), or throws a RuntimeException.
     */
    public static XMLEvent nextEvent(XMLEventReader xml) {
        try {
            return xml.nextEvent();
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }
    }

    public static String localNameOf(XMLEvent e) {
        if (e.isStartElement()) {
            return e.asStartElement().getName().getLocalPart();
        } else if (e.isEndElement()) {
            return e.asEndElement().getName().getLocalPart();
        }
        throw new RuntimeException(e + " isn't a tag to get the name of.");
    }
}