/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  com.sun.msv.datatype.xsd.DatatypeFactory
 *  com.sun.msv.datatype.xsd.TypeIncubator
 *  com.sun.msv.datatype.xsd.XSDatatype
 *  com.sun.msv.datatype.xsd.XSDatatypeImpl
 *  java.io.PrintStream
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.util.HashMap
 *  java.util.Iterator
 *  java.util.Map
 *  org.relaxng.datatype.DatatypeException
 *  org.relaxng.datatype.ValidationContext
 *  org.xml.sax.EntityResolver
 *  org.xml.sax.InputSource
 */
package org.dom4j.datatype;

import com.sun.msv.datatype.xsd.DatatypeFactory;
import com.sun.msv.datatype.xsd.TypeIncubator;
import com.sun.msv.datatype.xsd.XSDatatype;
import com.sun.msv.datatype.xsd.XSDatatypeImpl;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.QName;
import org.dom4j.datatype.DatatypeDocumentFactory;
import org.dom4j.datatype.DatatypeElementFactory;
import org.dom4j.datatype.InvalidSchemaException;
import org.dom4j.datatype.NamedTypeResolver;
import org.dom4j.io.SAXReader;
import org.dom4j.util.AttributeHelper;
import org.relaxng.datatype.DatatypeException;
import org.relaxng.datatype.ValidationContext;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class SchemaParser {
    private static final QName XSD_ALL;
    private static final QName XSD_ATTRIBUTE;
    private static final QName XSD_CHOICE;
    private static final QName XSD_COMPLEXTYPE;
    private static final QName XSD_ELEMENT;
    private static final QName XSD_INCLUDE;
    private static final Namespace XSD_NAMESPACE;
    private static final QName XSD_RESTRICTION;
    private static final QName XSD_SEQUENCE;
    private static final QName XSD_SIMPLETYPE;
    private Map dataTypeCache = new HashMap();
    private DatatypeDocumentFactory documentFactory;
    private NamedTypeResolver namedTypeResolver;
    private Namespace targetNamespace;

    static {
        XSD_NAMESPACE = Namespace.get("xsd", "http://www.w3.org/2001/XMLSchema");
        XSD_ELEMENT = QName.get("element", XSD_NAMESPACE);
        XSD_ATTRIBUTE = QName.get("attribute", XSD_NAMESPACE);
        XSD_SIMPLETYPE = QName.get("simpleType", XSD_NAMESPACE);
        XSD_COMPLEXTYPE = QName.get("complexType", XSD_NAMESPACE);
        XSD_RESTRICTION = QName.get("restriction", XSD_NAMESPACE);
        XSD_SEQUENCE = QName.get("sequence", XSD_NAMESPACE);
        XSD_CHOICE = QName.get("choice", XSD_NAMESPACE);
        XSD_ALL = QName.get("all", XSD_NAMESPACE);
        XSD_INCLUDE = QName.get("include", XSD_NAMESPACE);
    }

    public SchemaParser() {
        this(DatatypeDocumentFactory.singleton);
    }

    public SchemaParser(DatatypeDocumentFactory datatypeDocumentFactory) {
        this.documentFactory = datatypeDocumentFactory;
        this.namedTypeResolver = new NamedTypeResolver(datatypeDocumentFactory);
    }

    private XSDatatype dataTypeForXsdAttribute(Element element) {
        String string2 = element.attributeValue("type");
        if (string2 != null) {
            return super.getTypeByName(string2);
        }
        Element element2 = element.element(XSD_SIMPLETYPE);
        if (element2 == null) {
            String string3 = element.attributeValue("name");
            throw new InvalidSchemaException(new StringBuffer().append("The attribute: ").append(string3).append(" has no type attribute and does not contain a ").append("<simpleType/> element").toString());
        }
        return super.loadXSDatatypeFromSimpleType(element2);
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private XSDatatype deriveSimpleType(XSDatatype xSDatatype, Element element) {
        TypeIncubator typeIncubator = new TypeIncubator(xSDatatype);
        try {
            Iterator iterator = element.elementIterator();
            while (iterator.hasNext()) {
                Element element2 = (Element)iterator.next();
                typeIncubator.addFacet(element2.getName(), element2.attributeValue("value"), AttributeHelper.booleanValue(element2, "fixed"), null);
            }
            return typeIncubator.derive("", null);
        }
        catch (DatatypeException var4_6) {
            super.onSchemaError(new StringBuffer().append("Invalid restriction: ").append(var4_6.getMessage()).append(" when trying to build restriction: ").append((Object)element).toString());
            return null;
        }
    }

    private DatatypeElementFactory getDatatypeElementFactory(QName qName) {
        DatatypeElementFactory datatypeElementFactory = this.documentFactory.getElementFactory(qName);
        if (datatypeElementFactory == null) {
            datatypeElementFactory = new DatatypeElementFactory(qName);
            qName.setDocumentFactory(datatypeElementFactory);
        }
        return datatypeElementFactory;
    }

    private QName getQName(String string2) {
        if (this.targetNamespace == null) {
            return this.documentFactory.createQName(string2);
        }
        return this.documentFactory.createQName(string2, this.targetNamespace);
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive exception aggregation
     */
    private XSDatatype getTypeByName(String var1) {
        var2_2 = (XSDatatype)this.dataTypeCache.get((Object)var1);
        if (var2_2 != null) ** GOTO lbl18
        var3_3 = var1.indexOf(58);
        if (var3_3 >= 0) {
            var8_4 = var1.substring(var3_3 + 1);
            var2_2 = var10_5 = DatatypeFactory.getTypeByName((String)var8_4);
        }
lbl8: // 4 sources:
        do {
            if (var2_2 == null) {
                var2_2 = var7_6 = DatatypeFactory.getTypeByName((String)var1);
            }
lbl12: // 4 sources:
            do {
                if (var2_2 == null) {
                    var5_7 = super.getQName(var1);
                    var2_2 = (XSDatatype)this.namedTypeResolver.simpleTypeMap.get((Object)var5_7);
                }
                if (var2_2 != null) {
                    this.dataTypeCache.put((Object)var1, (Object)var2_2);
                }
lbl18: // 4 sources:
                return var2_2;
                break;
            } while (true);
            break;
        } while (true);
        catch (DatatypeException var9_8) {
            ** continue;
        }
        catch (DatatypeException var6_9) {
            ** continue;
        }
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Converted monitor instructions to comments
     * Lifted jumps to return sites
     */
    private void internalBuild(Document var1) {
        var12_2 = this;
        // MONITORENTER : var12_2
        var3_3 = var1.getRootElement();
        if (var3_3 == null) ** GOTO lbl36
        var4_4 = var3_3.elementIterator(SchemaParser.XSD_INCLUDE);
        while (var4_4.hasNext()) {
            var8_5 = ((Element)var4_4.next()).attributeValue("schemaLocation");
            var9_6 = var1.getEntityResolver();
            if (var9_6 != null) ** GOTO lbl12
            try {
                throw new InvalidSchemaException("No EntityResolver available");
lbl12: // 1 sources:
                var10_7 = var9_6.resolveEntity(null, var8_5);
                if (var10_7 == null) {
                    throw new InvalidSchemaException(new StringBuffer().append("Could not resolve the schema URI: ").append(var8_5).toString());
                }
            }
            catch (Exception var11_8) {
                System.out.println(new StringBuffer().append("Failed to load schema: ").append(var8_5).toString());
                System.out.println(new StringBuffer().append("Caught: ").append((Object)var11_8).toString());
                var11_8.printStackTrace();
                throw new InvalidSchemaException(new StringBuffer().append("Failed to load schema: ").append(var8_5).toString());
            }
            this.build(new SAXReader().read(var10_7));
        }
        var5_9 = var3_3.elementIterator(SchemaParser.XSD_ELEMENT);
        while (var5_9.hasNext()) {
            super.onDatatypeElement((Element)var5_9.next(), this.documentFactory);
        }
        var6_10 = var3_3.elementIterator(SchemaParser.XSD_SIMPLETYPE);
        while (var6_10.hasNext()) {
            super.onNamedSchemaSimpleType((Element)var6_10.next());
        }
        var7_11 = var3_3.elementIterator(SchemaParser.XSD_COMPLEXTYPE);
        while (var7_11.hasNext()) {
            super.onNamedSchemaComplexType((Element)var7_11.next());
        }
        this.namedTypeResolver.resolveNamedTypes();
lbl36: // 2 sources:
        // MONITOREXIT : var12_2
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private XSDatatype loadXSDatatypeFromSimpleType(Element element) {
        Element element2 = element.element(XSD_RESTRICTION);
        if (element2 != null) {
            String string2 = element2.attributeValue("base");
            if (string2 != null) {
                XSDatatype xSDatatype = super.getTypeByName(string2);
                if (xSDatatype != null) return super.deriveSimpleType(xSDatatype, element2);
                super.onSchemaError(new StringBuffer().append("Invalid base type: ").append(string2).append(" when trying to build restriction: ").append((Object)element2).toString());
                do {
                    return null;
                    break;
                } while (true);
            }
            Element element3 = element.element(XSD_SIMPLETYPE);
            if (element3 != null) return super.loadXSDatatypeFromSimpleType(element3);
            super.onSchemaError(new StringBuffer().append("The simpleType element: ").append((Object)element).append(" must contain a base attribute or simpleType").append(" element").toString());
            return null;
        }
        super.onSchemaError(new StringBuffer().append("No <restriction>. Could not create XSDatatype for simpleType: ").append((Object)element).toString());
        return null;
    }

    private void onChildElements(Element element, DatatypeElementFactory datatypeElementFactory) {
        Iterator iterator = element.elementIterator(XSD_ELEMENT);
        while (iterator.hasNext()) {
            super.onDatatypeElement((Element)iterator.next(), datatypeElementFactory);
        }
    }

    private void onDatatypeAttribute(Element element, DatatypeElementFactory datatypeElementFactory, Element element2) {
        String string2 = element2.attributeValue("name");
        QName qName = super.getQName(string2);
        XSDatatype xSDatatype = super.dataTypeForXsdAttribute(element2);
        if (xSDatatype != null) {
            datatypeElementFactory.setAttributeXSDatatype(qName, xSDatatype);
            return;
        }
        String string3 = element2.attributeValue("type");
        System.out.println(new StringBuffer().append("Warning: Couldn't find XSDatatype for type: ").append(string3).append(" attribute: ").append(string2).toString());
    }

    /*
     * Enabled aggressive block sorting
     */
    private void onDatatypeElement(Element element, DocumentFactory documentFactory) {
        String string2 = element.attributeValue("name");
        String string3 = element.attributeValue("type");
        QName qName = super.getQName(string2);
        DatatypeElementFactory datatypeElementFactory = super.getDatatypeElementFactory(qName);
        if (string3 != null) {
            XSDatatype xSDatatype = super.getTypeByName(string3);
            if (xSDatatype == null) {
                QName qName2 = super.getQName(string3);
                this.namedTypeResolver.registerTypedElement(element, qName2, documentFactory);
                return;
            }
            datatypeElementFactory.setChildElementXSDatatype(qName, xSDatatype);
            return;
        } else {
            XSDatatype xSDatatype;
            Element element2;
            Iterator iterator;
            Element element3 = element.element(XSD_SIMPLETYPE);
            if (element3 != null && (xSDatatype = super.loadXSDatatypeFromSimpleType(element3)) != null) {
                datatypeElementFactory.setChildElementXSDatatype(qName, xSDatatype);
            }
            if ((element2 = element.element(XSD_COMPLEXTYPE)) != null) {
                super.onSchemaComplexType(element2, datatypeElementFactory);
            }
            if (!(iterator = element.elementIterator(XSD_ATTRIBUTE)).hasNext()) return;
            {
                do {
                    super.onDatatypeAttribute(element, datatypeElementFactory, (Element)iterator.next());
                } while (iterator.hasNext());
                return;
            }
        }
    }

    private void onNamedSchemaComplexType(Element element) {
        Attribute attribute = element.attribute("name");
        if (attribute == null) {
            return;
        }
        QName qName = super.getQName(attribute.getText());
        DatatypeElementFactory datatypeElementFactory = super.getDatatypeElementFactory(qName);
        super.onSchemaComplexType(element, datatypeElementFactory);
        this.namedTypeResolver.registerComplexType(qName, datatypeElementFactory);
    }

    private void onNamedSchemaSimpleType(Element element) {
        Attribute attribute = element.attribute("name");
        if (attribute == null) {
            return;
        }
        QName qName = super.getQName(attribute.getText());
        XSDatatype xSDatatype = super.loadXSDatatypeFromSimpleType(element);
        this.namedTypeResolver.registerSimpleType(qName, xSDatatype);
    }

    private void onSchemaComplexType(Element element, DatatypeElementFactory datatypeElementFactory) {
        Element element2;
        Element element3;
        Iterator iterator = element.elementIterator(XSD_ATTRIBUTE);
        while (iterator.hasNext()) {
            Element element4 = (Element)iterator.next();
            QName qName = super.getQName(element4.attributeValue("name"));
            XSDatatype xSDatatype = super.dataTypeForXsdAttribute(element4);
            if (xSDatatype == null) continue;
            datatypeElementFactory.setAttributeXSDatatype(qName, xSDatatype);
        }
        Element element5 = element.element(XSD_SEQUENCE);
        if (element5 != null) {
            super.onChildElements(element5, datatypeElementFactory);
        }
        if ((element3 = element.element(XSD_CHOICE)) != null) {
            super.onChildElements(element3, datatypeElementFactory);
        }
        if ((element2 = element.element(XSD_ALL)) != null) {
            super.onChildElements(element2, datatypeElementFactory);
        }
    }

    private void onSchemaError(String string2) {
        throw new InvalidSchemaException(string2);
    }

    public void build(Document document) {
        this.targetNamespace = null;
        super.internalBuild(document);
    }

    public void build(Document document, Namespace namespace) {
        this.targetNamespace = namespace;
        super.internalBuild(document);
    }
}

