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

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

class PruningElementStack
extends ElementStack {
    private ElementHandler elementHandler;
    private int matchingElementIndex;
    private String[] path;

    public PruningElementStack(String[] arrstring, ElementHandler elementHandler) {
        this.path = arrstring;
        this.elementHandler = elementHandler;
        super.checkPath();
    }

    public PruningElementStack(String[] arrstring, ElementHandler elementHandler, int n) {
        super(n);
        this.path = arrstring;
        this.elementHandler = elementHandler;
        super.checkPath();
    }

    private void checkPath() {
        if (this.path.length < 2) {
            throw new RuntimeException(new StringBuffer().append("Invalid path of length: ").append(this.path.length).append(" it must be greater than 2").toString());
        }
        this.matchingElementIndex = -2 + this.path.length;
    }

    protected void pathMatches(Element element, Element element2) {
        this.elementHandler.onEnd((ElementPath)this);
        element.remove(element2);
    }

    /*
     * Enabled aggressive block sorting
     */
    @Override
    public Element popElement() {
        Element element = super.popElement();
        if (this.lastElementIndex != this.matchingElementIndex || this.lastElementIndex < 0 || !this.validElement(element, 1 + this.lastElementIndex)) {
            return element;
        }
        Element element2 = null;
        int n = 0;
        while (n <= this.lastElementIndex) {
            element2 = this.stack[n];
            if (!this.validElement(element2, n)) {
                return element;
            }
            ++n;
        }
        return element;
    }

    protected boolean validElement(Element element, int n) {
        String string2 = this.path[n];
        String string3 = element.getName();
        if (string2 == string3) {
            return true;
        }
        if (string2 != null && string3 != null) {
            return string2.equals((Object)string3);
        }
        return false;
    }
}

