/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.util.HashMap
 *  java.util.Map
 */
package org.dom4j.rule;

import java.util.HashMap;
import java.util.Map;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.rule.Action;
import org.dom4j.rule.Rule;
import org.dom4j.rule.RuleSet;

public class Mode {
    private Map attributeNameRuleSets;
    private Map elementNameRuleSets;
    private RuleSet[] ruleSets = new RuleSet[14];

    /*
     * Enabled aggressive block sorting
     */
    public void addRule(Rule rule) {
        short s = rule.getMatchType();
        String string2 = rule.getMatchesNodeName();
        if (string2 != null) {
            if (s == 1) {
                this.elementNameRuleSets = this.addToNameMap(this.elementNameRuleSets, string2, rule);
            } else if (s == 2) {
                this.attributeNameRuleSets = this.addToNameMap(this.attributeNameRuleSets, string2, rule);
            }
        }
        if (s >= 14) {
            s = 0;
        }
        if (s == 0) {
            int n = this.ruleSets.length;
            for (int i = 1; i < n; ++i) {
                RuleSet ruleSet = this.ruleSets[i];
                if (ruleSet == null) continue;
                ruleSet.addRule(rule);
            }
        }
        this.getRuleSet(s).addRule(rule);
    }

    protected Map addToNameMap(Map map, String string2, Rule rule) {
        RuleSet ruleSet;
        if (map == null) {
            map = new HashMap();
        }
        if ((ruleSet = (RuleSet)map.get((Object)string2)) == null) {
            ruleSet = new RuleSet();
            map.put((Object)string2, (Object)ruleSet);
        }
        ruleSet.addRule(rule);
        return map;
    }

    public void applyTemplates(Document document) throws Exception {
        int n = document.nodeCount();
        for (int i = 0; i < n; ++i) {
            this.fireRule(document.node(i));
        }
    }

    public void applyTemplates(Element element) throws Exception {
        int n = element.attributeCount();
        for (int i = 0; i < n; ++i) {
            this.fireRule(element.attribute(i));
        }
        int n2 = element.nodeCount();
        for (int j = 0; j < n2; ++j) {
            this.fireRule(element.node(j));
        }
    }

    public void fireRule(Node node) throws Exception {
        Rule rule;
        Action action;
        if (node != null && (rule = this.getMatchingRule(node)) != null && (action = rule.getAction()) != null) {
            action.run(node);
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    public Rule getMatchingRule(Node node) {
        RuleSet ruleSet;
        String string2;
        RuleSet ruleSet2;
        Rule rule;
        short s = node.getNodeType();
        if (s == 1) {
            RuleSet ruleSet3;
            String string3;
            if (this.elementNameRuleSets != null && (ruleSet3 = (RuleSet)this.elementNameRuleSets.get((Object)(string3 = node.getName()))) != null && (rule = ruleSet3.getMatchingRule(node)) != null) {
                return rule;
            }
        } else if (s == 2 && this.attributeNameRuleSets != null && (ruleSet = (RuleSet)this.attributeNameRuleSets.get((Object)(string2 = node.getName()))) != null && (rule = ruleSet.getMatchingRule(node)) != null) return rule;
        if (s < 0 || s >= this.ruleSets.length) {
            s = 0;
        }
        RuleSet ruleSet4 = this.ruleSets[s];
        rule = null;
        if ((ruleSet4 == null || (rule = ruleSet4.getMatchingRule(node)) == null) && s != 0 && (ruleSet2 = this.ruleSets[0]) != null) return ruleSet2.getMatchingRule(node);
        return rule;
    }

    protected RuleSet getRuleSet(int n) {
        RuleSet ruleSet = this.ruleSets[n];
        if (ruleSet == null) {
            RuleSet ruleSet2;
            this.ruleSets[n] = ruleSet = new RuleSet();
            if (n != 0 && (ruleSet2 = this.ruleSets[0]) != null) {
                ruleSet.addAll(ruleSet2);
            }
        }
        return ruleSet;
    }

    protected void removeFromNameMap(Map map, String string2, Rule rule) {
        RuleSet ruleSet;
        if (map != null && (ruleSet = (RuleSet)map.get((Object)string2)) != null) {
            ruleSet.removeRule(rule);
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    public void removeRule(Rule rule) {
        short s = rule.getMatchType();
        String string2 = rule.getMatchesNodeName();
        if (string2 != null) {
            if (s == 1) {
                this.removeFromNameMap(this.elementNameRuleSets, string2, rule);
            } else if (s == 2) {
                this.removeFromNameMap(this.attributeNameRuleSets, string2, rule);
            }
        }
        if (s >= 14) {
            s = 0;
        }
        this.getRuleSet(s).removeRule(rule);
        if (s != 0) {
            this.getRuleSet(0).removeRule(rule);
        }
    }
}

