/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.lang.Character
 *  java.lang.Class
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.reflect.Method
 *  java.util.ArrayList
 *  java.util.HashMap
 *  java.util.List
 *  java.util.Map
 *  org.xml.sax.Attributes
 *  org.xml.sax.DTDHandler
 *  org.xml.sax.EntityResolver
 *  org.xml.sax.InputSource
 *  org.xml.sax.Locator
 *  org.xml.sax.SAXException
 *  org.xml.sax.SAXParseException
 *  org.xml.sax.ext.DeclHandler
 *  org.xml.sax.ext.LexicalHandler
 *  org.xml.sax.helpers.DefaultHandler
 */
package org.dom4j.io;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Branch;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.dtd.AttributeDecl;
import org.dom4j.dtd.ElementDecl;
import org.dom4j.dtd.ExternalEntityDecl;
import org.dom4j.dtd.InternalEntityDecl;
import org.dom4j.io.DispatchHandler;
import org.dom4j.io.ElementStack;
import org.dom4j.tree.AbstractElement;
import org.dom4j.tree.NamespaceStack;
import org.xml.sax.Attributes;
import org.xml.sax.DTDHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

public class SAXContentHandler
extends DefaultHandler
implements DTDHandler,
DeclHandler,
LexicalHandler {
    private Map availableNamespaceMap;
    private StringBuffer cdataText;
    private Element currentElement;
    private int declaredNamespaceIndex;
    private List declaredNamespaceList;
    private Document document;
    private DocumentFactory documentFactory;
    private ElementHandler elementHandler;
    private ElementStack elementStack;
    private String entity;
    private int entityLevel;
    private EntityResolver entityResolver;
    private List externalDTDDeclarations;
    private boolean ignoreComments;
    private boolean includeExternalDTDDeclarations;
    private boolean includeInternalDTDDeclarations;
    private InputSource inputSource;
    private boolean insideCDATASection;
    private boolean insideDTDSection;
    private List internalDTDDeclarations;
    private boolean internalDTDsubset;
    private Locator locator;
    private boolean mergeAdjacentText;
    private NamespaceStack namespaceStack;
    private boolean stripWhitespaceText;
    private StringBuffer textBuffer;
    private boolean textInTextBuffer;

    public SAXContentHandler() {
        this(DocumentFactory.getInstance());
    }

    public SAXContentHandler(DocumentFactory documentFactory) {
        super(documentFactory, null);
    }

    public SAXContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler) {
        super(documentFactory, elementHandler, null);
        this.elementStack = this.createElementStack();
    }

    public SAXContentHandler(DocumentFactory documentFactory, ElementHandler elementHandler, ElementStack elementStack) {
        this.availableNamespaceMap = new HashMap();
        this.declaredNamespaceList = new ArrayList();
        this.includeInternalDTDDeclarations = false;
        this.includeExternalDTDDeclarations = false;
        this.internalDTDsubset = false;
        this.mergeAdjacentText = false;
        this.textInTextBuffer = false;
        this.ignoreComments = false;
        this.stripWhitespaceText = false;
        this.documentFactory = documentFactory;
        this.elementHandler = elementHandler;
        this.elementStack = elementStack;
        this.namespaceStack = new NamespaceStack(documentFactory);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private String getEncoding() {
        if (this.locator == null) {
            return null;
        }
        Method method = this.locator.getClass().getMethod("getEncoding", new Class[0]);
        if (method == null) return null;
        try {
            return (String)method.invoke((Object)this.locator, null);
        }
        catch (Exception var1_3) {
            // empty catch block
        }
        return null;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected void addAttributes(Element element, Attributes attributes) {
        if (element instanceof AbstractElement) {
            ((AbstractElement)element).setAttributes(attributes, this.namespaceStack, false);
            return;
        }
        int n = attributes.getLength();
        int n2 = 0;
        while (n2 < n) {
            String string2 = attributes.getQName(n2);
            if (false || !string2.startsWith("xmlns")) {
                String string3 = attributes.getURI(n2);
                String string4 = attributes.getLocalName(n2);
                String string5 = attributes.getValue(n2);
                element.addAttribute(this.namespaceStack.getAttributeQName(string3, string4, string2), string5);
            }
            ++n2;
        }
    }

    protected void addDTDDeclaration(Object object) {
        if (this.internalDTDDeclarations == null) {
            this.internalDTDDeclarations = new ArrayList();
        }
        this.internalDTDDeclarations.add(object);
    }

    protected void addDeclaredNamespaces(Element element) {
        element.getNamespace();
        int n = this.namespaceStack.size();
        while (this.declaredNamespaceIndex < n) {
            element.add(this.namespaceStack.getNamespace(this.declaredNamespaceIndex));
            this.declaredNamespaceIndex = 1 + this.declaredNamespaceIndex;
        }
    }

    protected void addExternalDTDDeclaration(Object object) {
        if (this.externalDTDDeclarations == null) {
            this.externalDTDDeclarations = new ArrayList();
        }
        this.externalDTDDeclarations.add(object);
    }

    /*
     * Enabled aggressive block sorting
     */
    public void attributeDecl(String string2, String string3, String string4, String string5, String string6) throws SAXException {
        if (this.internalDTDsubset) {
            if (!this.includeInternalDTDDeclarations) return;
            {
                this.addDTDDeclaration(new AttributeDecl(string2, string3, string4, string5, string6));
                return;
            }
        } else {
            if (!this.includeExternalDTDDeclarations) return;
            {
                this.addExternalDTDDeclaration(new AttributeDecl(string2, string3, string4, string5, string6));
                return;
            }
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    public void characters(char[] arrc, int n, int n2) throws SAXException {
        if (n2 == 0 || this.currentElement == null) {
            return;
        }
        if (this.entity != null) {
            if (this.mergeAdjacentText && this.textInTextBuffer) {
                this.completeCurrentTextNode();
            }
            this.currentElement.addEntity(this.entity, new String(arrc, n, n2));
            this.entity = null;
            return;
        }
        if (this.insideCDATASection) {
            if (this.mergeAdjacentText && this.textInTextBuffer) {
                this.completeCurrentTextNode();
            }
            this.cdataText.append(new String(arrc, n, n2));
            return;
        }
        if (this.mergeAdjacentText) {
            this.textBuffer.append(arrc, n, n2);
            this.textInTextBuffer = true;
            return;
        }
        this.currentElement.addText(new String(arrc, n, n2));
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    public void comment(char[] arrc, int n, int n2) throws SAXException {
        if (this.ignoreComments) return;
        if (this.mergeAdjacentText && this.textInTextBuffer) {
            this.completeCurrentTextNode();
        }
        String string2 = new String(arrc, n, n2);
        if (this.insideDTDSection || string2.length() <= 0) return;
        if (this.currentElement != null) {
            this.currentElement.addComment(string2);
            return;
        }
        this.getDocument().addComment(string2);
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Lifted jumps to return sites
     */
    protected void completeCurrentTextNode() {
        if (this.stripWhitespaceText) ** GOTO lbl4
        this.currentElement.addText(this.textBuffer.toString());
        ** GOTO lbl13
lbl4: // 1 sources:
        var2_1 = true;
        var3_2 = 0;
        var4_3 = this.textBuffer.length();
        do {
            if (var3_2 >= var4_3) ** GOTO lbl11
            if (!Character.isWhitespace((char)this.textBuffer.charAt(var3_2))) {
                var2_1 = false;
lbl11: // 2 sources:
                if (!var2_1) {
                    this.currentElement.addText(this.textBuffer.toString());
                }
lbl13: // 4 sources:
                this.textBuffer.setLength(0);
                this.textInTextBuffer = false;
                return;
            }
            ++var3_2;
        } while (true);
    }

    protected Document createDocument() {
        String string2 = this.getEncoding();
        Document document = this.documentFactory.createDocument(string2);
        document.setEntityResolver(this.entityResolver);
        if (this.inputSource != null) {
            document.setName(this.inputSource.getSystemId());
        }
        return document;
    }

    protected ElementStack createElementStack() {
        return new ElementStack();
    }

    /*
     * Enabled aggressive block sorting
     */
    public void elementDecl(String string2, String string3) throws SAXException {
        if (this.internalDTDsubset) {
            if (!this.includeInternalDTDDeclarations) return;
            {
                this.addDTDDeclaration(new ElementDecl(string2, string3));
                return;
            }
        } else {
            if (!this.includeExternalDTDDeclarations) return;
            {
                this.addExternalDTDDeclaration(new ElementDecl(string2, string3));
                return;
            }
        }
    }

    public void endCDATA() throws SAXException {
        this.insideCDATASection = false;
        this.currentElement.addCDATA(this.cdataText.toString());
    }

    public void endDTD() throws SAXException {
        this.insideDTDSection = false;
        DocumentType documentType = this.getDocument().getDocType();
        if (documentType != null) {
            if (this.internalDTDDeclarations != null) {
                documentType.setInternalDeclarations(this.internalDTDDeclarations);
            }
            if (this.externalDTDDeclarations != null) {
                documentType.setExternalDeclarations(this.externalDTDDeclarations);
            }
        }
        this.internalDTDDeclarations = null;
        this.externalDTDDeclarations = null;
    }

    public void endDocument() throws SAXException {
        this.namespaceStack.clear();
        this.elementStack.clear();
        this.currentElement = null;
        this.textBuffer = null;
    }

    public void endElement(String string2, String string3, String string4) throws SAXException {
        if (this.mergeAdjacentText && this.textInTextBuffer) {
            this.completeCurrentTextNode();
        }
        if (this.elementHandler != null && this.currentElement != null) {
            this.elementHandler.onEnd(this.elementStack);
        }
        this.elementStack.popElement();
        this.currentElement = this.elementStack.peekElement();
    }

    public void endEntity(String string2) throws SAXException {
        this.entityLevel = -1 + this.entityLevel;
        this.entity = null;
        if (this.entityLevel == 0) {
            this.internalDTDsubset = true;
        }
    }

    public void endPrefixMapping(String string2) throws SAXException {
        this.namespaceStack.pop(string2);
        this.declaredNamespaceIndex = this.namespaceStack.size();
    }

    public void error(SAXParseException sAXParseException) throws SAXException {
        throw sAXParseException;
    }

    /*
     * Enabled aggressive block sorting
     */
    public void externalEntityDecl(String string2, String string3, String string4) throws SAXException {
        ExternalEntityDecl externalEntityDecl = new ExternalEntityDecl(string2, string3, string4);
        if (this.internalDTDsubset) {
            if (!this.includeInternalDTDDeclarations) return;
            {
                this.addDTDDeclaration(externalEntityDecl);
                return;
            }
        } else {
            if (!this.includeExternalDTDDeclarations) return;
            {
                this.addExternalDTDDeclaration(externalEntityDecl);
                return;
            }
        }
    }

    public void fatalError(SAXParseException sAXParseException) throws SAXException {
        throw sAXParseException;
    }

    public Document getDocument() {
        if (this.document == null) {
            this.document = this.createDocument();
        }
        return this.document;
    }

    public ElementStack getElementStack() {
        return this.elementStack;
    }

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

    public InputSource getInputSource() {
        return this.inputSource;
    }

    /*
     * Enabled aggressive block sorting
     */
    public void internalEntityDecl(String string2, String string3) throws SAXException {
        if (this.internalDTDsubset) {
            if (!this.includeInternalDTDDeclarations) return;
            {
                this.addDTDDeclaration(new InternalEntityDecl(string2, string3));
                return;
            }
        } else {
            if (!this.includeExternalDTDDeclarations) return;
            {
                this.addExternalDTDDeclaration(new InternalEntityDecl(string2, string3));
                return;
            }
        }
    }

    protected boolean isIgnorableEntity(String string2) {
        if ("amp".equals((Object)string2) || "apos".equals((Object)string2) || "gt".equals((Object)string2) || "lt".equals((Object)string2) || "quot".equals((Object)string2)) {
            return true;
        }
        return false;
    }

    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 isStripWhitespaceText() {
        return this.stripWhitespaceText;
    }

    public void notationDecl(String string2, String string3, String string4) throws SAXException {
    }

    public void processingInstruction(String string2, String string3) throws SAXException {
        if (this.mergeAdjacentText && this.textInTextBuffer) {
            this.completeCurrentTextNode();
        }
        if (this.currentElement != null) {
            this.currentElement.addProcessingInstruction(string2, string3);
            return;
        }
        this.getDocument().addProcessingInstruction(string2, string3);
    }

    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    public void setElementStack(ElementStack elementStack) {
        this.elementStack = elementStack;
    }

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

    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 setInputSource(InputSource inputSource) {
        this.inputSource = inputSource;
    }

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

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

    public void startCDATA() throws SAXException {
        this.insideCDATASection = true;
        this.cdataText = new StringBuffer();
    }

    public void startDTD(String string2, String string3, String string4) throws SAXException {
        this.getDocument().addDocType(string2, string3, string4);
        this.insideDTDSection = true;
        this.internalDTDsubset = true;
    }

    public void startDocument() throws SAXException {
        this.document = null;
        this.currentElement = null;
        this.elementStack.clear();
        if (this.elementHandler != null && this.elementHandler instanceof DispatchHandler) {
            this.elementStack.setDispatchHandler((DispatchHandler)this.elementHandler);
        }
        this.namespaceStack.clear();
        this.declaredNamespaceIndex = 0;
        if (this.mergeAdjacentText && this.textBuffer == null) {
            this.textBuffer = new StringBuffer();
        }
        this.textInTextBuffer = false;
    }

    public void startElement(String string2, String string3, String string4, Attributes attributes) throws SAXException {
        if (this.mergeAdjacentText && this.textInTextBuffer) {
            this.completeCurrentTextNode();
        }
        QName qName = this.namespaceStack.getQName(string2, string3, string4);
        Branch branch = this.currentElement;
        if (branch == null) {
            branch = this.getDocument();
        }
        Element element = branch.addElement(qName);
        this.addDeclaredNamespaces(element);
        this.addAttributes(element, attributes);
        this.elementStack.pushElement(element);
        this.currentElement = element;
        this.entity = null;
        if (this.elementHandler != null) {
            this.elementHandler.onStart(this.elementStack);
        }
    }

    public void startEntity(String string2) throws SAXException {
        this.entityLevel = 1 + this.entityLevel;
        this.entity = null;
        if (!this.insideDTDSection && !this.isIgnorableEntity(string2)) {
            this.entity = string2;
        }
        this.internalDTDsubset = false;
    }

    public void startPrefixMapping(String string2, String string3) throws SAXException {
        this.namespaceStack.push(string2, string3);
    }

    public void unparsedEntityDecl(String string2, String string3, String string4, String string5) throws SAXException {
    }

    public void warning(SAXParseException sAXParseException) throws SAXException {
    }
}

