/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.File
 *  java.io.FileInputStream
 *  java.io.FileNotFoundException
 *  java.io.InputStream
 *  java.io.Reader
 *  java.io.Serializable
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.Throwable
 *  java.net.URL
 *  org.xml.sax.ContentHandler
 *  org.xml.sax.EntityResolver
 *  org.xml.sax.ErrorHandler
 *  org.xml.sax.InputSource
 *  org.xml.sax.SAXException
 *  org.xml.sax.SAXParseException
 *  org.xml.sax.XMLFilter
 *  org.xml.sax.XMLReader
 *  org.xml.sax.helpers.DefaultHandler
 *  org.xml.sax.helpers.XMLReaderFactory
 */
package org.dom4j.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.ElementHandler;
import org.dom4j.io.DispatchHandler;
import org.dom4j.io.SAXContentHandler;
import org.dom4j.io.SAXHelper;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLFilter;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;

public class SAXReader {
    private static final String SAX_DECL_HANDLER = "http://xml.org/sax/properties/declaration-handler";
    private static final String SAX_LEXICALHANDLER = "http://xml.org/sax/handlers/LexicalHandler";
    private static final String SAX_LEXICAL_HANDLER = "http://xml.org/sax/properties/lexical-handler";
    private static final String SAX_NAMESPACES = "http://xml.org/sax/features/namespaces";
    private static final String SAX_NAMESPACE_PREFIXES = "http://xml.org/sax/features/namespace-prefixes";
    private static final String SAX_STRING_INTERNING = "http://xml.org/sax/features/string-interning";
    private DispatchHandler dispatchHandler;
    private String encoding;
    private EntityResolver entityResolver;
    private ErrorHandler errorHandler;
    private DocumentFactory factory;
    private boolean ignoreComments;
    private boolean includeExternalDTDDeclarations;
    private boolean includeInternalDTDDeclarations;
    private boolean mergeAdjacentText;
    private boolean stringInternEnabled;
    private boolean stripWhitespaceText;
    private boolean validating;
    private XMLFilter xmlFilter;
    private XMLReader xmlReader;

    public SAXReader() {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
    }

    public SAXReader(String string2) throws SAXException {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        if (string2 != null) {
            this.xmlReader = XMLReaderFactory.createXMLReader((String)string2);
        }
    }

    public SAXReader(String string2, boolean bl) throws SAXException {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        if (string2 != null) {
            this.xmlReader = XMLReaderFactory.createXMLReader((String)string2);
        }
        this.validating = bl;
    }

    public SAXReader(DocumentFactory documentFactory) {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        this.factory = documentFactory;
    }

    public SAXReader(DocumentFactory documentFactory, boolean bl) {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        this.factory = documentFactory;
        this.validating = bl;
    }

    public SAXReader(XMLReader xMLReader) {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        this.xmlReader = xMLReader;
    }

    public SAXReader(XMLReader xMLReader, boolean bl) {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        this.xmlReader = xMLReader;
        this.validating = bl;
    }

    public SAXReader(boolean bl) {
        this.stringInternEnabled = true;
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.mergeAdjacentText = false;
        this.stripWhitespaceText = false;
        this.ignoreComments = false;
        this.encoding = null;
        this.validating = bl;
    }

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

    protected void configureReader(XMLReader xMLReader, DefaultHandler defaultHandler) throws DocumentException {
        SAXHelper.setParserProperty(xMLReader, "http://xml.org/sax/handlers/LexicalHandler", (Object)defaultHandler);
        SAXHelper.setParserProperty(xMLReader, "http://xml.org/sax/properties/lexical-handler", (Object)defaultHandler);
        if (this.includeInternalDTDDeclarations || this.includeExternalDTDDeclarations) {
            SAXHelper.setParserProperty(xMLReader, "http://xml.org/sax/properties/declaration-handler", (Object)defaultHandler);
        }
        SAXHelper.setParserFeature(xMLReader, "http://xml.org/sax/features/namespaces", true);
        SAXHelper.setParserFeature(xMLReader, "http://xml.org/sax/features/namespace-prefixes", false);
        SAXHelper.setParserFeature(xMLReader, "http://xml.org/sax/features/string-interning", this.isStringInternEnabled());
        SAXHelper.setParserFeature(xMLReader, "http://xml.org/sax/features/use-locator2", true);
        try {
            xMLReader.setFeature("http://xml.org/sax/features/validation", this.isValidating());
            if (this.errorHandler != null) {
                xMLReader.setErrorHandler(this.errorHandler);
                return;
            }
            xMLReader.setErrorHandler((ErrorHandler)defaultHandler);
            return;
        }
        catch (Exception var10_3) {
            if (this.isValidating()) {
                throw new DocumentException(new StringBuffer().append("Validation not supported for XMLReader: ").append((Object)xMLReader).toString(), (Throwable)var10_3);
            }
            return;
        }
    }

    protected SAXContentHandler createContentHandler(XMLReader xMLReader) {
        return new SAXContentHandler(this.getDocumentFactory(), this.dispatchHandler);
    }

    protected EntityResolver createDefaultEntityResolver(String string2) {
        String string3 = null;
        if (string2 != null) {
            int n = string2.length();
            string3 = null;
            if (n > 0) {
                int n2 = string2.lastIndexOf(47);
                string3 = null;
                if (n2 > 0) {
                    string3 = string2.substring(0, n2 + 1);
                }
            }
        }
        return new SAXEntityResolver(string3);
    }

    protected XMLReader createXMLReader() throws SAXException {
        return SAXHelper.createXMLReader(this.isValidating());
    }

    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 String getEncoding() {
        return this.encoding;
    }

    public EntityResolver getEntityResolver() {
        return this.entityResolver;
    }

    public ErrorHandler getErrorHandler() {
        return this.errorHandler;
    }

    public XMLFilter getXMLFilter() {
        return this.xmlFilter;
    }

    public XMLReader getXMLReader() throws SAXException {
        if (this.xmlReader == null) {
            this.xmlReader = this.createXMLReader();
        }
        return this.xmlReader;
    }

    protected XMLReader installXMLFilter(XMLReader xMLReader) {
        XMLFilter xMLFilter = this.getXMLFilter();
        if (xMLFilter != null) {
            XMLReader xMLReader2;
            XMLFilter xMLFilter2 = xMLFilter;
            while ((xMLReader2 = xMLFilter2.getParent()) instanceof XMLFilter) {
                xMLFilter2 = (XMLFilter)xMLReader2;
            }
            xMLFilter2.setParent(xMLReader);
            return xMLFilter;
        }
        return xMLReader;
    }

    public boolean isIgnoreComments() {
        return this.ignoreComments;
    }

    public boolean isIncludeExternalDTDDeclarations() {
        return this.includeExternalDTDDeclarations;
    }

    public boolean isIncludeInternalDTDDeclarations() {
        return this.includeInternalDTDDeclarations;
    }

    public boolean isMergeAdjacentText() {
        return this.mergeAdjacentText;
    }

    public boolean isStringInternEnabled() {
        return this.stringInternEnabled;
    }

    public boolean isStripWhitespaceText() {
        return this.stripWhitespaceText;
    }

    public boolean isValidating() {
        return this.validating;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public Document read(File file) throws DocumentException {
        InputSource inputSource;
        String string2;
        try {
            inputSource = new InputSource((InputStream)new FileInputStream(file));
            if (this.encoding != null) {
                inputSource.setEncoding(this.encoding);
            }
            if ((string2 = file.getAbsolutePath()) == null) return this.read(inputSource);
        }
        catch (FileNotFoundException var3_6) {
            throw new DocumentException(var3_6.getMessage(), (Throwable)var3_6);
        }
        StringBuffer stringBuffer = new StringBuffer("file://");
        if (!string2.startsWith(File.separator)) {
            stringBuffer.append("/");
        }
        stringBuffer.append(string2.replace('\\', '/'));
        inputSource.setSystemId(stringBuffer.toString());
        return this.read(inputSource);
    }

    public Document read(InputStream inputStream) throws DocumentException {
        InputSource inputSource = new InputSource(inputStream);
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    public Document read(InputStream inputStream, String string2) throws DocumentException {
        InputSource inputSource = new InputSource(inputStream);
        inputSource.setSystemId(string2);
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    public Document read(Reader reader) throws DocumentException {
        InputSource inputSource = new InputSource(reader);
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    public Document read(Reader reader, String string2) throws DocumentException {
        InputSource inputSource = new InputSource(reader);
        inputSource.setSystemId(string2);
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    public Document read(String string2) throws DocumentException {
        InputSource inputSource = new InputSource(string2);
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    public Document read(URL uRL) throws DocumentException {
        InputSource inputSource = new InputSource(uRL.toExternalForm());
        if (this.encoding != null) {
            inputSource.setEncoding(this.encoding);
        }
        return this.read(inputSource);
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    public Document read(InputSource inputSource) throws DocumentException {
        try {
            XMLReader xMLReader = this.installXMLFilter(this.getXMLReader());
            EntityResolver entityResolver = this.entityResolver;
            if (entityResolver == null) {
                this.entityResolver = entityResolver = this.createDefaultEntityResolver(inputSource.getSystemId());
            }
            xMLReader.setEntityResolver(entityResolver);
            SAXContentHandler sAXContentHandler = this.createContentHandler(xMLReader);
            sAXContentHandler.setEntityResolver(entityResolver);
            sAXContentHandler.setInputSource(inputSource);
            boolean bl = this.isIncludeInternalDTDDeclarations();
            boolean bl2 = this.isIncludeExternalDTDDeclarations();
            sAXContentHandler.setIncludeInternalDTDDeclarations(bl);
            sAXContentHandler.setIncludeExternalDTDDeclarations(bl2);
            sAXContentHandler.setMergeAdjacentText(this.isMergeAdjacentText());
            sAXContentHandler.setStripWhitespaceText(this.isStripWhitespaceText());
            sAXContentHandler.setIgnoreComments(this.isIgnoreComments());
            xMLReader.setContentHandler((ContentHandler)sAXContentHandler);
            this.configureReader(xMLReader, sAXContentHandler);
            xMLReader.parse(inputSource);
            return sAXContentHandler.getDocument();
        }
        catch (Exception var2_8) {
            if (!(var2_8 instanceof SAXParseException)) {
                throw new DocumentException(var2_8.getMessage(), (Throwable)var2_8);
            }
            SAXParseException sAXParseException = (SAXParseException)var2_8;
            String string2 = sAXParseException.getSystemId();
            if (string2 != null) throw new DocumentException(new StringBuffer().append("Error on line ").append(sAXParseException.getLineNumber()).append(" of document ").append(string2).append(" : ").append(sAXParseException.getMessage()).toString(), (Throwable)var2_8);
            string2 = "";
            throw new DocumentException(new StringBuffer().append("Error on line ").append(sAXParseException.getLineNumber()).append(" of document ").append(string2).append(" : ").append(sAXParseException.getMessage()).toString(), (Throwable)var2_8);
        }
    }

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

    public void resetHandlers() {
        this.getDispatchHandler().resetHandlers();
    }

    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 setEncoding(String string2) {
        this.encoding = string2;
    }

    public void setEntityResolver(EntityResolver entityResolver) {
        this.entityResolver = entityResolver;
    }

    public void setErrorHandler(ErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    public void setFeature(String string2, boolean bl) throws SAXException {
        this.getXMLReader().setFeature(string2, bl);
    }

    public void setIgnoreComments(boolean bl) {
        this.ignoreComments = bl;
    }

    public void setIncludeExternalDTDDeclarations(boolean bl) {
        this.includeExternalDTDDeclarations = bl;
    }

    public void setIncludeInternalDTDDeclarations(boolean bl) {
        this.includeInternalDTDDeclarations = bl;
    }

    public void setMergeAdjacentText(boolean bl) {
        this.mergeAdjacentText = bl;
    }

    public void setProperty(String string2, Object object) throws SAXException {
        this.getXMLReader().setProperty(string2, object);
    }

    public void setStringInternEnabled(boolean bl) {
        this.stringInternEnabled = bl;
    }

    public void setStripWhitespaceText(boolean bl) {
        this.stripWhitespaceText = bl;
    }

    public void setValidation(boolean bl) {
        this.validating = bl;
    }

    public void setXMLFilter(XMLFilter xMLFilter) {
        this.xmlFilter = xMLFilter;
    }

    public void setXMLReader(XMLReader xMLReader) {
        this.xmlReader = xMLReader;
    }

    public void setXMLReaderClassName(String string2) throws SAXException {
        this.setXMLReader(XMLReaderFactory.createXMLReader((String)string2));
    }

    protected static class SAXEntityResolver
    implements Serializable,
    EntityResolver {
        protected String uriPrefix;

        public SAXEntityResolver(String string2) {
            this.uriPrefix = string2;
        }

        public InputSource resolveEntity(String string2, String string3) {
            if (string3 != null && string3.length() > 0 && this.uriPrefix != null && string3.indexOf(58) <= 0) {
                string3 = new StringBuffer().append(this.uriPrefix).append(string3).toString();
            }
            return new InputSource(string3);
        }
    }

}

