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

import org.dom4j.Element;
import org.dom4j.ElementHandler;
import org.dom4j.ElementPath;
import org.dom4j.io.DispatchHandler;

class ElementStack
implements ElementPath {
    private DispatchHandler handler = null;
    protected int lastElementIndex = -1;
    protected Element[] stack;

    public ElementStack() {
        this(50);
    }

    public ElementStack(int n) {
        this.stack = new Element[n];
    }

    private String getHandlerPath(String string2) {
        if (this.handler == null) {
            this.setDispatchHandler(new DispatchHandler());
        }
        if (string2.startsWith("/")) {
            return string2;
        }
        if (this.getPath().equals((Object)"/")) {
            return new StringBuffer().append(this.getPath()).append(string2).toString();
        }
        return new StringBuffer().append(this.getPath()).append("/").append(string2).toString();
    }

    @Override
    public void addHandler(String string2, ElementHandler elementHandler) {
        this.handler.addHandler(super.getHandlerPath(string2), elementHandler);
    }

    public void clear() {
        this.lastElementIndex = -1;
    }

    public boolean containsHandler(String string2) {
        return this.handler.containsHandler(string2);
    }

    @Override
    public Element getCurrent() {
        return this.peekElement();
    }

    public DispatchHandler getDispatchHandler() {
        return this.handler;
    }

    @Override
    public Element getElement(int n) {
        try {
            Element element = this.stack[n];
            return element;
        }
        catch (ArrayIndexOutOfBoundsException var2_3) {
            return null;
        }
    }

    @Override
    public String getPath() {
        if (this.handler == null) {
            this.setDispatchHandler(new DispatchHandler());
        }
        return this.handler.getPath();
    }

    public Element peekElement() {
        if (this.lastElementIndex < 0) {
            return null;
        }
        return this.stack[this.lastElementIndex];
    }

    public Element popElement() {
        if (this.lastElementIndex < 0) {
            return null;
        }
        Element[] arrelement = this.stack;
        int n = this.lastElementIndex;
        this.lastElementIndex = n - 1;
        return arrelement[n];
    }

    public void pushElement(Element element) {
        int n;
        int n2 = this.stack.length;
        this.lastElementIndex = n = 1 + this.lastElementIndex;
        if (n >= n2) {
            this.reallocate(n2 * 2);
        }
        this.stack[this.lastElementIndex] = element;
    }

    protected void reallocate(int n) {
        Element[] arrelement = this.stack;
        this.stack = new Element[n];
        System.arraycopy((Object)arrelement, (int)0, (Object)this.stack, (int)0, (int)arrelement.length);
    }

    @Override
    public void removeHandler(String string2) {
        this.handler.removeHandler(super.getHandlerPath(string2));
    }

    public void setDispatchHandler(DispatchHandler dispatchHandler) {
        this.handler = dispatchHandler;
    }

    @Override
    public int size() {
        return 1 + this.lastElementIndex;
    }
}

