/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.InputStream
 *  java.io.Reader
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.util.Iterator
 *  javax.xml.namespace.QName
 *  javax.xml.stream.Location
 *  javax.xml.stream.XMLEventReader
 *  javax.xml.stream.XMLInputFactory
 *  javax.xml.stream.XMLStreamException
 *  javax.xml.stream.events.Attribute
 *  javax.xml.stream.events.Characters
 *  javax.xml.stream.events.Comment
 *  javax.xml.stream.events.EndElement
 *  javax.xml.stream.events.EntityDeclaration
 *  javax.xml.stream.events.EntityReference
 *  javax.xml.stream.events.Namespace
 *  javax.xml.stream.events.ProcessingInstruction
 *  javax.xml.stream.events.StartDocument
 *  javax.xml.stream.events.StartElement
 *  javax.xml.stream.events.XMLEvent
 */
package org.dom4j.io;

import java.io.InputStream;
import java.io.Reader;
import java.util.Iterator;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.EntityDeclaration;
import javax.xml.stream.events.EntityReference;
import javax.xml.stream.events.StartDocument;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.dom4j.Attribute;
import org.dom4j.CDATA;
import org.dom4j.CharacterData;
import org.dom4j.Comment;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Entity;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.QName;
import org.dom4j.Text;

public class STAXEventReader {
    private DocumentFactory factory;
    private XMLInputFactory inputFactory;

    public STAXEventReader() {
        this.inputFactory = XMLInputFactory.newInstance();
        this.factory = DocumentFactory.getInstance();
    }

    public STAXEventReader(DocumentFactory documentFactory) {
        this.inputFactory = XMLInputFactory.newInstance();
        if (documentFactory != null) {
            this.factory = documentFactory;
            return;
        }
        this.factory = DocumentFactory.getInstance();
    }

    public Attribute createAttribute(Element element, javax.xml.stream.events.Attribute attribute) {
        return this.factory.createAttribute(element, this.createQName(attribute.getName()), attribute.getValue());
    }

    public CharacterData createCharacterData(Characters characters) {
        String string2 = characters.getData();
        if (characters.isCData()) {
            return this.factory.createCDATA(string2);
        }
        return this.factory.createText(string2);
    }

    public Comment createComment(javax.xml.stream.events.Comment comment) {
        return this.factory.createComment(comment.getText());
    }

    public Element createElement(StartElement startElement) {
        QName qName = this.createQName(startElement.getName());
        Element element = this.factory.createElement(qName);
        Iterator iterator = startElement.getAttributes();
        while (iterator.hasNext()) {
            javax.xml.stream.events.Attribute attribute = (javax.xml.stream.events.Attribute)iterator.next();
            element.addAttribute(this.createQName(attribute.getName()), attribute.getValue());
        }
        Iterator iterator2 = startElement.getNamespaces();
        while (iterator2.hasNext()) {
            javax.xml.stream.events.Namespace namespace = (javax.xml.stream.events.Namespace)iterator2.next();
            element.addNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
        }
        return element;
    }

    public Entity createEntity(EntityReference entityReference) {
        return this.factory.createEntity(entityReference.getName(), entityReference.getDeclaration().getReplacementText());
    }

    public Namespace createNamespace(javax.xml.stream.events.Namespace namespace) {
        return this.factory.createNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
    }

    public ProcessingInstruction createProcessingInstruction(javax.xml.stream.events.ProcessingInstruction processingInstruction) {
        return this.factory.createProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
    }

    public QName createQName(javax.xml.namespace.QName qName) {
        return this.factory.createQName(qName.getLocalPart(), qName.getPrefix(), qName.getNamespaceURI());
    }

    public Attribute readAttribute(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isAttribute()) {
            return this.createAttribute(null, (javax.xml.stream.events.Attribute)xMLEventReader.nextEvent());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected Attribute event, found: ").append((Object)xMLEvent).toString());
    }

    public CharacterData readCharacters(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isCharacters()) {
            return this.createCharacterData(xMLEventReader.nextEvent().asCharacters());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected Characters event, found: ").append((Object)xMLEvent).toString());
    }

    public Comment readComment(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent instanceof javax.xml.stream.events.Comment) {
            return this.createComment((javax.xml.stream.events.Comment)xMLEventReader.nextEvent());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected Comment event, found: ").append((Object)xMLEvent).toString());
    }

    public Document readDocument(InputStream inputStream) throws XMLStreamException {
        return this.readDocument(inputStream, null);
    }

    public Document readDocument(InputStream inputStream, String string2) throws XMLStreamException {
        XMLEventReader xMLEventReader = this.inputFactory.createXMLEventReader(string2, inputStream);
        try {
            Document document = this.readDocument(xMLEventReader);
            return document;
        }
        finally {
            xMLEventReader.close();
        }
    }

    public Document readDocument(Reader reader) throws XMLStreamException {
        return this.readDocument(reader, null);
    }

    public Document readDocument(Reader reader, String string2) throws XMLStreamException {
        XMLEventReader xMLEventReader = this.inputFactory.createXMLEventReader(string2, reader);
        try {
            Document document = this.readDocument(xMLEventReader);
            return document;
        }
        finally {
            xMLEventReader.close();
        }
    }

    public Document readDocument(XMLEventReader xMLEventReader) throws XMLStreamException {
        Document document = null;
        block4 : while (xMLEventReader.hasNext()) {
            switch (xMLEventReader.peek().getEventType()) {
                default: {
                    if (document == null) {
                        document = this.factory.createDocument();
                    }
                    document.add(this.readNode(xMLEventReader));
                    continue block4;
                }
                case 7: {
                    StartDocument startDocument = (StartDocument)xMLEventReader.nextEvent();
                    if (document == null) {
                        if (startDocument.encodingSet()) {
                            String string2 = startDocument.getCharacterEncodingScheme();
                            document = this.factory.createDocument(string2);
                            continue block4;
                        }
                        document = this.factory.createDocument();
                        continue block4;
                    }
                    throw new XMLStreamException("Unexpected StartDocument event", startDocument.getLocation());
                }
                case 4: 
                case 6: 
                case 8: 
            }
            xMLEventReader.nextEvent();
        }
        return document;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public Element readElement(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (!xMLEvent.isStartElement()) throw new XMLStreamException(new StringBuffer().append("Expected Element event, found: ").append((Object)xMLEvent).toString());
        StartElement startElement = xMLEventReader.nextEvent().asStartElement();
        Element element = this.createElement(startElement);
        do {
            if (!xMLEventReader.hasNext()) {
                throw new XMLStreamException("Unexpected end of stream while reading element content");
            }
            if (xMLEventReader.peek().isEndElement()) {
                EndElement endElement = xMLEventReader.nextEvent().asEndElement();
                if (endElement.getName().equals((Object)startElement.getName())) return element;
                throw new XMLStreamException(new StringBuffer().append("Expected ").append((Object)startElement.getName()).append(" end-tag, but found").append((Object)endElement.getName()).toString());
            }
            element.add(this.readNode(xMLEventReader));
        } while (true);
    }

    public Entity readEntityReference(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isEntityReference()) {
            return this.createEntity((EntityReference)xMLEventReader.nextEvent());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected EntityRef event, found: ").append((Object)xMLEvent).toString());
    }

    public Namespace readNamespace(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isNamespace()) {
            return this.createNamespace((javax.xml.stream.events.Namespace)xMLEventReader.nextEvent());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected Namespace event, found: ").append((Object)xMLEvent).toString());
    }

    public Node readNode(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isStartElement()) {
            return this.readElement(xMLEventReader);
        }
        if (xMLEvent.isCharacters()) {
            return this.readCharacters(xMLEventReader);
        }
        if (xMLEvent.isStartDocument()) {
            return this.readDocument(xMLEventReader);
        }
        if (xMLEvent.isProcessingInstruction()) {
            return this.readProcessingInstruction(xMLEventReader);
        }
        if (xMLEvent.isEntityReference()) {
            return this.readEntityReference(xMLEventReader);
        }
        if (xMLEvent.isAttribute()) {
            return this.readAttribute(xMLEventReader);
        }
        if (xMLEvent.isNamespace()) {
            return this.readNamespace(xMLEventReader);
        }
        throw new XMLStreamException(new StringBuffer().append("Unsupported event: ").append((Object)xMLEvent).toString());
    }

    public ProcessingInstruction readProcessingInstruction(XMLEventReader xMLEventReader) throws XMLStreamException {
        XMLEvent xMLEvent = xMLEventReader.peek();
        if (xMLEvent.isProcessingInstruction()) {
            return this.createProcessingInstruction((javax.xml.stream.events.ProcessingInstruction)xMLEventReader.nextEvent());
        }
        throw new XMLStreamException(new StringBuffer().append("Expected PI event, found: ").append((Object)xMLEvent).toString());
    }

    public void setDocumentFactory(DocumentFactory documentFactory) {
        if (documentFactory != null) {
            this.factory = documentFactory;
            return;
        }
        this.factory = DocumentFactory.getInstance();
    }
}

