/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.BufferedReader
 *  java.io.CharArrayReader
 *  java.io.File
 *  java.io.FileReader
 *  java.io.IOException
 *  java.io.InputStream
 *  java.io.InputStreamReader
 *  java.io.Reader
 *  java.lang.Object
 *  java.lang.String
 *  java.net.URL
 *  org.xmlpull.v1.XmlPullParser
 *  org.xmlpull.v1.XmlPullParserException
 *  org.xmlpull.v1.XmlPullParserFactory
 */
package org.dom4j.io;

import java.io.BufferedReader;
import java.io.CharArrayReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.QName;
import org.dom4j.io.DispatchHandler;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class XPP3Reader {
    private DispatchHandler dispatchHandler;
    private DocumentFactory factory;
    private XmlPullParserFactory xppFactory;
    private XmlPullParser xppParser;

    public XPP3Reader() {
    }

    public XPP3Reader(DocumentFactory documentFactory) {
        this.factory = documentFactory;
    }

    public void addHandler(String string2, ElementHandler elementHandler) {
        this.getDispatchHandler().addHandler(string2, elementHandler);
    }

    protected Reader createReader(InputStream inputStream) throws IOException {
        return new BufferedReader((Reader)new InputStreamReader(inputStream));
    }

    protected DispatchHandler getDispatchHandler() {
        if (this.dispatchHandler == null) {
            this.dispatchHandler = new DispatchHandler();
        }
        return this.dispatchHandler;
    }

    public DocumentFactory getDocumentFactory() {
        if (this.factory == null) {
            this.factory = DocumentFactory.getInstance();
        }
        return this.factory;
    }

    public XmlPullParserFactory getXPPFactory() throws XmlPullParserException {
        if (this.xppFactory == null) {
            this.xppFactory = XmlPullParserFactory.newInstance();
        }
        this.xppFactory.setNamespaceAware(true);
        return this.xppFactory;
    }

    public XmlPullParser getXPPParser() throws XmlPullParserException {
        if (this.xppParser == null) {
            this.xppParser = this.getXPPFactory().newPullParser();
        }
        return this.xppParser;
    }

    /*
     * Enabled aggressive block sorting
     */
    protected Document parseDocument() throws DocumentException, IOException, XmlPullParserException {
        DocumentFactory documentFactory = this.getDocumentFactory();
        Document document = documentFactory.createDocument();
        Element element = null;
        XmlPullParser xmlPullParser = this.getXPPParser();
        xmlPullParser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
        block10 : do {
            switch (xmlPullParser.nextToken()) {
                case 6: {
                    continue block10;
                }
                default: {
                    continue block10;
                }
                case 1: {
                    return document;
                }
                case 8: {
                    String string2 = xmlPullParser.getText();
                    int n = string2.indexOf(" ");
                    if (n >= 0) {
                        document.addProcessingInstruction(string2.substring(0, n), string2.substring(n + 1));
                        continue block10;
                    }
                    document.addProcessingInstruction(string2, "");
                    continue block10;
                }
                case 9: {
                    if (element != null) {
                        element.addComment(xmlPullParser.getText());
                        continue block10;
                    }
                    document.addComment(xmlPullParser.getText());
                    continue block10;
                }
                case 5: {
                    if (element == null) {
                        DocumentException documentException = new DocumentException("Cannot have text content outside of the root document");
                        throw documentException;
                    }
                    element.addCDATA(xmlPullParser.getText());
                    continue block10;
                }
                case 2: {
                    QName qName = xmlPullParser.getPrefix() == null ? documentFactory.createQName(xmlPullParser.getName(), xmlPullParser.getNamespace()) : documentFactory.createQName(xmlPullParser.getName(), xmlPullParser.getPrefix(), xmlPullParser.getNamespace());
                    Element element2 = documentFactory.createElement(qName);
                    int n = xmlPullParser.getNamespaceCount(-1 + xmlPullParser.getDepth());
                    int n2 = xmlPullParser.getNamespaceCount(xmlPullParser.getDepth());
                    for (int i = n; i < n2; ++i) {
                        if (xmlPullParser.getNamespacePrefix(i) == null) continue;
                        element2.addNamespace(xmlPullParser.getNamespacePrefix(i), xmlPullParser.getNamespaceUri(i));
                    }
                    for (int j = 0; j < xmlPullParser.getAttributeCount(); ++j) {
                        QName qName2 = xmlPullParser.getAttributePrefix(j) == null ? documentFactory.createQName(xmlPullParser.getAttributeName(j)) : documentFactory.createQName(xmlPullParser.getAttributeName(j), xmlPullParser.getAttributePrefix(j), xmlPullParser.getAttributeNamespace(j));
                        element2.addAttribute(qName2, xmlPullParser.getAttributeValue(j));
                    }
                    if (element != null) {
                        element.add(element2);
                    } else {
                        document.add(element2);
                    }
                    element = element2;
                    continue block10;
                }
                case 3: {
                    if (element == null) continue block10;
                    element = element.getParent();
                    continue block10;
                }
                case 4: 
            }
            String string3 = xmlPullParser.getText();
            if (element == null) {
                DocumentException documentException = new DocumentException("Cannot have text content outside of the root document");
                throw documentException;
            }
            element.addText(string3);
        } while (true);
    }

    public Document read(File file) throws DocumentException, IOException, XmlPullParserException {
        String string2 = file.getAbsolutePath();
        return this.read((Reader)new BufferedReader((Reader)new FileReader(file)), string2);
    }

    public Document read(InputStream inputStream) throws DocumentException, IOException, XmlPullParserException {
        return this.read(this.createReader(inputStream));
    }

    public Document read(InputStream inputStream, String string2) throws DocumentException, IOException, XmlPullParserException {
        return this.read(this.createReader(inputStream), string2);
    }

    public Document read(Reader reader) throws DocumentException, IOException, XmlPullParserException {
        this.getXPPParser().setInput(reader);
        return this.parseDocument();
    }

    public Document read(Reader reader, String string2) throws DocumentException, IOException, XmlPullParserException {
        Document document = this.read(reader);
        document.setName(string2);
        return document;
    }

    public Document read(String string2) throws DocumentException, IOException, XmlPullParserException {
        if (string2.indexOf(58) >= 0) {
            return this.read(new URL(string2));
        }
        return this.read(new File(string2));
    }

    public Document read(URL uRL) throws DocumentException, IOException, XmlPullParserException {
        String string2 = uRL.toExternalForm();
        return this.read(this.createReader(uRL.openStream()), string2);
    }

    public Document read(char[] arrc) throws DocumentException, IOException, XmlPullParserException {
        this.getXPPParser().setInput((Reader)new CharArrayReader(arrc));
        return this.parseDocument();
    }

    public void removeHandler(String string2) {
        this.getDispatchHandler().removeHandler(string2);
    }

    public void setDefaultHandler(ElementHandler elementHandler) {
        this.getDispatchHandler().setDefaultHandler(elementHandler);
    }

    protected void setDispatchHandler(DispatchHandler dispatchHandler) {
        this.dispatchHandler = dispatchHandler;
    }

    public void setDocumentFactory(DocumentFactory documentFactory) {
        this.factory = documentFactory;
    }

    public void setXPPFactory(XmlPullParserFactory xmlPullParserFactory) {
        this.xppFactory = xmlPullParserFactory;
    }
}

