/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.PrintStream
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  org.xml.sax.EntityResolver
 *  org.xml.sax.InputSource
 */
package org.dom4j.datatype;

import java.io.PrintStream;
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.DatatypeElementFactory;
import org.dom4j.datatype.InvalidSchemaException;
import org.dom4j.datatype.SchemaParser;
import org.dom4j.io.SAXReader;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

public class DatatypeDocumentFactory
extends DocumentFactory {
    private static final boolean DO_INTERN_QNAME;
    private static final Namespace XSI_NAMESPACE;
    private static final QName XSI_NO_SCHEMA_LOCATION;
    private static final QName XSI_SCHEMA_LOCATION;
    protected static transient DatatypeDocumentFactory singleton;
    private boolean autoLoadSchema = true;
    private SchemaParser schemaBuilder;
    private SAXReader xmlSchemaReader = new SAXReader();

    static {
        singleton = new DatatypeDocumentFactory();
        XSI_NAMESPACE = Namespace.get("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        XSI_SCHEMA_LOCATION = QName.get("schemaLocation", XSI_NAMESPACE);
        XSI_NO_SCHEMA_LOCATION = QName.get("noNamespaceSchemaLocation", XSI_NAMESPACE);
    }

    public DatatypeDocumentFactory() {
        this.schemaBuilder = new SchemaParser(this);
    }

    public static DocumentFactory getInstance() {
        return singleton;
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    public Attribute createAttribute(Element element, QName qName, String string2) {
        if (this.autoLoadSchema && qName.equals(XSI_NO_SCHEMA_LOCATION)) {
            Document document = null;
            if (element != null) {
                document = element.getDocument();
            }
            this.loadSchema(document, string2);
            return super.createAttribute(element, qName, string2);
        }
        if (!this.autoLoadSchema) return super.createAttribute(element, qName, string2);
        if (!qName.equals(XSI_SCHEMA_LOCATION)) return super.createAttribute(element, qName, string2);
        Document document = null;
        if (element != null) {
            document = element.getDocument();
        }
        Namespace namespace = element.getNamespaceForURI(string2.substring(0, string2.indexOf(32)));
        this.loadSchema(document, string2.substring(1 + string2.indexOf(32)), namespace);
        return super.createAttribute(element, qName, string2);
    }

    public DatatypeElementFactory getElementFactory(QName qName) {
        DocumentFactory documentFactory = qName.getDocumentFactory();
        boolean bl = documentFactory instanceof DatatypeElementFactory;
        DatatypeElementFactory datatypeElementFactory = null;
        if (bl) {
            datatypeElementFactory = (DatatypeElementFactory)documentFactory;
        }
        return datatypeElementFactory;
    }

    public void loadSchema(Document document) {
        this.schemaBuilder.build(document);
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    protected void loadSchema(Document document, String string2) {
        InputSource inputSource;
        try {
            EntityResolver entityResolver = document.getEntityResolver();
            if (entityResolver == null) {
                throw new InvalidSchemaException(new StringBuffer().append("No EntityResolver available for resolving URI: ").append(string2).toString());
            }
            inputSource = entityResolver.resolveEntity(null, string2);
            if (entityResolver == null) {
                throw new InvalidSchemaException(new StringBuffer().append("Could not resolve the URI: ").append(string2).toString());
            }
        }
        catch (Exception var3_4) {
            System.out.println(new StringBuffer().append("Failed to load schema: ").append(string2).toString());
            System.out.println(new StringBuffer().append("Caught: ").append((Object)var3_4).toString());
            var3_4.printStackTrace();
            throw new InvalidSchemaException(new StringBuffer().append("Failed to load schema: ").append(string2).toString());
        }
        this.loadSchema(this.xmlSchemaReader.read(inputSource));
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    protected void loadSchema(Document document, String string2, Namespace namespace) {
        InputSource inputSource;
        try {
            EntityResolver entityResolver = document.getEntityResolver();
            if (entityResolver == null) {
                throw new InvalidSchemaException(new StringBuffer().append("No EntityResolver available for resolving URI: ").append(string2).toString());
            }
            inputSource = entityResolver.resolveEntity(null, string2);
            if (entityResolver == null) {
                throw new InvalidSchemaException(new StringBuffer().append("Could not resolve the URI: ").append(string2).toString());
            }
        }
        catch (Exception var4_5) {
            System.out.println(new StringBuffer().append("Failed to load schema: ").append(string2).toString());
            System.out.println(new StringBuffer().append("Caught: ").append((Object)var4_5).toString());
            var4_5.printStackTrace();
            throw new InvalidSchemaException(new StringBuffer().append("Failed to load schema: ").append(string2).toString());
        }
        this.loadSchema(this.xmlSchemaReader.read(inputSource), namespace);
    }

    public void loadSchema(Document document, Namespace namespace) {
        this.schemaBuilder.build(document, namespace);
    }
}

