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.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class Main {
    /**
     * Reads the next tag and throws an exception if the next tag out of the 
     * reader is not an end tag corresponding to the given start tag. 
     * 
     * @param start
     * @param xml
     */
    public static void assertClosedTag(StartElement start, XMLEventReader xml) {
        try {
            assertEnd(localNameOf(start), xml.nextTag());
        } catch (XMLStreamException e) {
            throw new RuntimeException(e);
        }
    }

    public static EndElement assertEnd(String name, XMLEvent event) {
        if (!isEndTag(event, name)) {
            throw new RuntimeException("Was expecting an end tag " + name + ", not this: " + event);
        }
        return event.asEndElement();
    }

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

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

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