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

import java.util.ArrayList;
import java.util.HashMap;
import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;

class DispatchHandler
implements ElementHandler {
    private boolean atRoot = true;
    private ElementHandler defaultHandler;
    private ArrayList handlerStack = new ArrayList();
    private HashMap handlers = new HashMap();
    private String path = "/";
    private ArrayList pathStack = new ArrayList();

    public void addHandler(String string2, ElementHandler elementHandler) {
        this.handlers.put((Object)string2, (Object)elementHandler);
    }

    public boolean containsHandler(String string2) {
        return this.handlers.containsKey((Object)string2);
    }

    public int getActiveHandlerCount() {
        return this.handlerStack.size();
    }

    public ElementHandler getHandler(String string2) {
        return (ElementHandler)this.handlers.get((Object)string2);
    }

    public String getPath() {
        return this.path;
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    public void onEnd(ElementPath elementPath) {
        if (this.handlers != null && this.handlers.containsKey((Object)this.path)) {
            ElementHandler elementHandler = (ElementHandler)this.handlers.get((Object)this.path);
            this.handlerStack.remove(-1 + this.handlerStack.size());
            elementHandler.onEnd(elementPath);
        } else if (this.handlerStack.isEmpty() && this.defaultHandler != null) {
            this.defaultHandler.onEnd(elementPath);
        }
        this.path = (String)this.pathStack.remove(-1 + this.pathStack.size());
        if (this.pathStack.size() == 0) {
            this.atRoot = true;
        }
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    public void onStart(ElementPath elementPath) {
        Element element = elementPath.getCurrent();
        this.pathStack.add((Object)this.path);
        if (this.atRoot) {
            this.path = new StringBuffer().append(this.path).append(element.getName()).toString();
            this.atRoot = false;
        } else {
            this.path = new StringBuffer().append(this.path).append("/").append(element.getName()).toString();
        }
        if (this.handlers != null && this.handlers.containsKey((Object)this.path)) {
            ElementHandler elementHandler = (ElementHandler)this.handlers.get((Object)this.path);
            this.handlerStack.add((Object)elementHandler);
            elementHandler.onStart(elementPath);
            return;
        } else {
            if (!this.handlerStack.isEmpty() || this.defaultHandler == null) return;
            {
                this.defaultHandler.onStart(elementPath);
                return;
            }
        }
    }

    public ElementHandler removeHandler(String string2) {
        return (ElementHandler)this.handlers.remove((Object)string2);
    }

    public void resetHandlers() {
        this.atRoot = true;
        this.path = "/";
        this.pathStack.clear();
        this.handlerStack.clear();
        this.handlers.clear();
        this.defaultHandler = null;
    }

    public void setDefaultHandler(ElementHandler elementHandler) {
        this.defaultHandler = elementHandler;
    }
}

