/*
 * 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.HashMap
 */
package org.dom4j.rule;

import java.io.PrintStream;
import java.util.HashMap;
import org.dom4j.Node;
import org.dom4j.rule.Action;
import org.dom4j.rule.Mode;
import org.dom4j.rule.Pattern;
import org.dom4j.rule.Rule;
import org.dom4j.rule.RuleManager$1;
import org.dom4j.rule.pattern.NodeTypePattern;

public class RuleManager {
    private int appearenceCount;
    private HashMap modes = new HashMap();
    private Action valueOfAction;

    protected void addDefaultRule(Mode mode, Pattern pattern, Action action) {
        mode.addRule(this.createDefaultRule(pattern, action));
    }

    protected void addDefaultRules(Mode mode) {
        RuleManager$1 ruleManager$1 = new RuleManager$1((RuleManager)this, mode);
        Action action = this.getValueOfAction();
        this.addDefaultRule(mode, NodeTypePattern.ANY_DOCUMENT, ruleManager$1);
        this.addDefaultRule(mode, NodeTypePattern.ANY_ELEMENT, ruleManager$1);
        if (action != null) {
            this.addDefaultRule(mode, NodeTypePattern.ANY_ATTRIBUTE, action);
            this.addDefaultRule(mode, NodeTypePattern.ANY_TEXT, action);
        }
    }

    public void addRule(Rule rule) {
        int n;
        this.appearenceCount = n = 1 + this.appearenceCount;
        rule.setAppearenceCount(n);
        Mode mode = this.getMode(rule.getMode());
        Rule[] arrrule = rule.getUnionRules();
        if (arrrule != null) {
            int n2 = arrrule.length;
            for (int i = 0; i < n2; ++i) {
                mode.addRule(arrrule[i]);
            }
        } else {
            mode.addRule(rule);
        }
    }

    public void clear() {
        this.modes.clear();
        this.appearenceCount = 0;
    }

    protected Rule createDefaultRule(Pattern pattern, Action action) {
        Rule rule = new Rule(pattern, action);
        rule.setImportPrecedence(-1);
        return rule;
    }

    protected Mode createMode() {
        Mode mode = new Mode();
        this.addDefaultRules(mode);
        return mode;
    }

    public Rule getMatchingRule(String string2, Node node) {
        Mode mode = (Mode)this.modes.get((Object)string2);
        if (mode != null) {
            return mode.getMatchingRule(node);
        }
        System.out.println(new StringBuffer().append("Warning: No Mode for mode: ").append((Object)mode).toString());
        return null;
    }

    public Mode getMode(String string2) {
        Mode mode = (Mode)this.modes.get((Object)string2);
        if (mode == null) {
            mode = this.createMode();
            this.modes.put((Object)string2, (Object)mode);
        }
        return mode;
    }

    public Action getValueOfAction() {
        return this.valueOfAction;
    }

    public void removeRule(Rule rule) {
        Mode mode = this.getMode(rule.getMode());
        Rule[] arrrule = rule.getUnionRules();
        if (arrrule != null) {
            int n = arrrule.length;
            for (int i = 0; i < n; ++i) {
                mode.removeRule(arrrule[i]);
            }
        } else {
            mode.removeRule(rule);
        }
    }

    public void setValueOfAction(Action action) {
        this.valueOfAction = action;
    }
}

