/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.BufferedReader
 *  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.lang.StringBuffer
 *  java.net.URL
 *  org.gjt.xpp.XmlEndTag
 *  org.gjt.xpp.XmlPullParser
 *  org.gjt.xpp.XmlPullParserException
 *  org.gjt.xpp.XmlPullParserFactory
 *  org.gjt.xpp.XmlStartTag
 */
package org.dom4j.io;

import java.io.BufferedReader;
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.io.DispatchHandler;
import org.dom4j.xpp.ProxyXmlStartTag;
import org.gjt.xpp.XmlEndTag;
import org.gjt.xpp.XmlPullParser;
import org.gjt.xpp.XmlPullParserException;
import org.gjt.xpp.XmlPullParserFactory;
import org.gjt.xpp.XmlStartTag;

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

    public XPPReader() {
    }

    public XPPReader(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();
        }
        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 {
        Document document = this.getDocumentFactory().createDocument();
        Element element = null;
        XmlPullParser xmlPullParser = this.getXPPParser();
        xmlPullParser.setNamespaceAware(true);
        ProxyXmlStartTag proxyXmlStartTag = new ProxyXmlStartTag();
        XmlEndTag xmlEndTag = this.xppFactory.newEndTag();
        block6 : do {
            byte by = xmlPullParser.next();
            switch (by) {
                default: {
                    throw new DocumentException(new StringBuffer().append("Error: unknown type: ").append((int)by).toString());
                }
                case 2: {
                    xmlPullParser.readStartTag((XmlStartTag)proxyXmlStartTag);
                    Element element2 = proxyXmlStartTag.getElement();
                    if (element != null) {
                        element.add(element2);
                    } else {
                        document.add(element2);
                    }
                    element = element2;
                    continue block6;
                }
                case 3: {
                    xmlPullParser.readEndTag(xmlEndTag);
                    if (element == null) continue block6;
                    element = element.getParent();
                    continue block6;
                }
                case 4: {
                    String string2 = xmlPullParser.readContent();
                    if (element == null) {
                        throw new DocumentException("Cannot have text content outside of the root document");
                    }
                    element.addText(string2);
                    continue block6;
                }
                case 1: 
            }
            break;
        } while (true);
        return document;
    }

    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(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;
    }
}

