/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.PrintStream
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.System
 *  java.util.ArrayList
 *  java.util.HashMap
 *  java.util.Map
 */
package org.dom4j.tree;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.dom4j.DocumentFactory;
import org.dom4j.Namespace;
import org.dom4j.QName;

public class NamespaceStack {
    private Map currentNamespaceCache;
    private Namespace defaultNamespace;
    private DocumentFactory documentFactory;
    private ArrayList namespaceCacheList;
    private ArrayList namespaceStack;
    private Map rootNamespaceCache;

    public NamespaceStack() {
        this.namespaceStack = new ArrayList();
        this.namespaceCacheList = new ArrayList();
        this.rootNamespaceCache = new HashMap();
        this.documentFactory = DocumentFactory.getInstance();
    }

    public NamespaceStack(DocumentFactory documentFactory) {
        this.namespaceStack = new ArrayList();
        this.namespaceCacheList = new ArrayList();
        this.rootNamespaceCache = new HashMap();
        this.documentFactory = documentFactory;
    }

    public Namespace addNamespace(String string2, String string3) {
        Namespace namespace = this.createNamespace(string2, string3);
        this.push(namespace);
        return namespace;
    }

    public void clear() {
        this.namespaceStack.clear();
        this.namespaceCacheList.clear();
        this.rootNamespaceCache.clear();
        this.currentNamespaceCache = null;
    }

    /*
     * Enabled aggressive block sorting
     */
    public boolean contains(Namespace namespace) {
        String string2 = namespace.getPrefix();
        Namespace namespace2 = string2 == null || string2.length() == 0 ? this.getDefaultNamespace() : this.getNamespaceForPrefix(string2);
        if (namespace2 == null) {
            return false;
        }
        if (namespace2 == namespace) {
            return true;
        }
        return namespace.getURI().equals((Object)namespace2.getURI());
    }

    protected Namespace createNamespace(String string2, String string3) {
        return this.documentFactory.createNamespace(string2, string3);
    }

    protected QName createQName(String string2, String string3, Namespace namespace) {
        return this.documentFactory.createQName(string2, namespace);
    }

    protected Namespace findDefaultNamespace() {
        for (int i = -1 + this.namespaceStack.size(); i >= 0; --i) {
            Namespace namespace = (Namespace)this.namespaceStack.get(i);
            if (namespace == null || namespace.getPrefix() != null && namespace.getPrefix().length() != 0) continue;
            return namespace;
        }
        return null;
    }

    /*
     * Enabled aggressive block sorting
     */
    public QName getAttributeQName(String string2, String string3, String string4) {
        QName qName;
        Namespace namespace;
        Map map;
        if (string4 == null) {
            string4 = string3;
        }
        if ((qName = (QName)(map = this.getNamespaceCache()).get((Object)string4)) != null) {
            return qName;
        }
        if (string3 == null) {
            string3 = string4;
        }
        if (string2 == null) {
            string2 = "";
        }
        String string5 = "";
        int n = string4.indexOf(":");
        if (n > 0) {
            string5 = string4.substring(0, n);
            namespace = this.createNamespace(string5, string2);
            if (string3.trim().length() == 0) {
                string3 = string4.substring(n + 1);
            }
        } else {
            namespace = Namespace.NO_NAMESPACE;
            if (string3.trim().length() == 0) {
                string3 = string4;
            }
        }
        QName qName2 = this.pushQName(string3, string4, namespace, string5);
        map.put((Object)string4, (Object)qName2);
        return qName2;
    }

    public Namespace getDefaultNamespace() {
        if (this.defaultNamespace == null) {
            this.defaultNamespace = this.findDefaultNamespace();
        }
        return this.defaultNamespace;
    }

    public DocumentFactory getDocumentFactory() {
        return this.documentFactory;
    }

    public Namespace getNamespace(int n) {
        return (Namespace)this.namespaceStack.get(n);
    }

    /*
     * Enabled aggressive block sorting
     */
    protected Map getNamespaceCache() {
        if (this.currentNamespaceCache != null) return this.currentNamespaceCache;
        int n = -1 + this.namespaceStack.size();
        if (n < 0) {
            this.currentNamespaceCache = this.rootNamespaceCache;
            return this.currentNamespaceCache;
        }
        this.currentNamespaceCache = (Map)this.namespaceCacheList.get(n);
        if (this.currentNamespaceCache != null) return this.currentNamespaceCache;
        this.currentNamespaceCache = new HashMap();
        this.namespaceCacheList.set(n, (Object)this.currentNamespaceCache);
        return this.currentNamespaceCache;
    }

    public Namespace getNamespaceForPrefix(String string2) {
        if (string2 == null) {
            string2 = "";
        }
        for (int i = -1 + this.namespaceStack.size(); i >= 0; --i) {
            Namespace namespace = (Namespace)this.namespaceStack.get(i);
            if (!string2.equals((Object)namespace.getPrefix())) continue;
            return namespace;
        }
        return null;
    }

    /*
     * Enabled aggressive block sorting
     */
    public QName getQName(String string2, String string3, String string4) {
        if (string3 == null) {
            string3 = string4;
        } else if (string4 == null) {
            string4 = string3;
        }
        if (string2 == null) {
            string2 = "";
        }
        String string5 = "";
        int n = string4.indexOf(":");
        if (n > 0) {
            string5 = string4.substring(0, n);
            if (string3.trim().length() != 0) return this.pushQName(string3, string4, this.createNamespace(string5, string2), string5);
            string3 = string4.substring(n + 1);
            return this.pushQName(string3, string4, this.createNamespace(string5, string2), string5);
        }
        if (string3.trim().length() != 0) return this.pushQName(string3, string4, this.createNamespace(string5, string2), string5);
        string3 = string4;
        return this.pushQName(string3, string4, this.createNamespace(string5, string2), string5);
    }

    public String getURI(String string2) {
        Namespace namespace = this.getNamespaceForPrefix(string2);
        if (namespace != null) {
            return namespace.getURI();
        }
        return null;
    }

    public Namespace pop() {
        return this.remove(-1 + this.namespaceStack.size());
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Lifted jumps to return sites
     */
    public Namespace pop(String var1) {
        if (var1 == null) {
            var1 = "";
        }
        var2_2 = -1 + this.namespaceStack.size();
        do {
            var3_4 = null;
            if (var2_2 < 0) ** GOTO lbl12
            var4_3 = (Namespace)this.namespaceStack.get(var2_2);
            if (var1.equals((Object)var4_3.getPrefix())) {
                this.remove(var2_2);
                var3_4 = var4_3;
                if (var3_4 != null) return var3_4;
lbl12: // 2 sources:
                System.out.println(new StringBuffer().append("Warning: missing namespace prefix ignored: ").append(var1).toString());
                return var3_4;
            }
            --var2_2;
        } while (true);
    }

    public void push(String string2, String string3) {
        if (string3 == null) {
            string3 = "";
        }
        this.push(this.createNamespace(string2, string3));
    }

    public void push(Namespace namespace) {
        this.namespaceStack.add((Object)namespace);
        this.namespaceCacheList.add((Object)null);
        this.currentNamespaceCache = null;
        String string2 = namespace.getPrefix();
        if (string2 == null || string2.length() == 0) {
            this.defaultNamespace = namespace;
        }
    }

    protected QName pushQName(String string2, String string3, Namespace namespace, String string4) {
        if (string4 == null || string4.length() == 0) {
            this.defaultNamespace = null;
        }
        return this.createQName(string2, string3, namespace);
    }

    protected Namespace remove(int n) {
        Namespace namespace = (Namespace)this.namespaceStack.remove(n);
        this.namespaceCacheList.remove(n);
        this.defaultNamespace = null;
        this.currentNamespaceCache = null;
        return namespace;
    }

    public void setDocumentFactory(DocumentFactory documentFactory) {
        this.documentFactory = documentFactory;
    }

    public int size() {
        return this.namespaceStack.size();
    }

    public String toString() {
        return new StringBuffer().append(super.toString()).append(" Stack: ").append(this.namespaceStack.toString()).toString();
    }
}

