/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.lang.Exception
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.Throwable
 *  java.util.ArrayList
 *  java.util.List
 *  org.jaxen.Context
 *  org.jaxen.ContextSupport
 *  org.jaxen.FunctionContext
 *  org.jaxen.JaxenException
 *  org.jaxen.NamespaceContext
 *  org.jaxen.Navigator
 *  org.jaxen.SimpleNamespaceContext
 *  org.jaxen.SimpleVariableContext
 *  org.jaxen.VariableContext
 *  org.jaxen.XPathFunctionContext
 *  org.jaxen.dom4j.DocumentNavigator
 *  org.jaxen.pattern.Pattern
 *  org.jaxen.pattern.PatternParser
 *  org.jaxen.saxpath.SAXPathException
 */
package org.dom4j.xpath;

import java.util.ArrayList;
import java.util.List;
import org.dom4j.InvalidXPathException;
import org.dom4j.Node;
import org.dom4j.XPathException;
import org.dom4j.rule.Pattern;
import org.jaxen.Context;
import org.jaxen.ContextSupport;
import org.jaxen.FunctionContext;
import org.jaxen.JaxenException;
import org.jaxen.NamespaceContext;
import org.jaxen.Navigator;
import org.jaxen.SimpleNamespaceContext;
import org.jaxen.SimpleVariableContext;
import org.jaxen.VariableContext;
import org.jaxen.XPathFunctionContext;
import org.jaxen.dom4j.DocumentNavigator;
import org.jaxen.pattern.PatternParser;
import org.jaxen.saxpath.SAXPathException;

public class XPathPattern
implements Pattern {
    private Context context;
    private org.jaxen.pattern.Pattern pattern;
    private String text;

    public XPathPattern(String string2) {
        this.text = string2;
        this.context = new Context(this.getContextSupport());
        try {
            this.pattern = PatternParser.parse((String)string2);
            return;
        }
        catch (SAXPathException var3_2) {
            throw new InvalidXPathException(string2, var3_2.getMessage());
        }
        catch (Throwable var2_3) {
            throw new InvalidXPathException(string2, var2_3);
        }
    }

    public XPathPattern(org.jaxen.pattern.Pattern pattern) {
        this.pattern = pattern;
        this.text = pattern.getText();
        this.context = new Context(this.getContextSupport());
    }

    protected ContextSupport getContextSupport() {
        return new ContextSupport((NamespaceContext)new SimpleNamespaceContext(), XPathFunctionContext.getInstance(), (VariableContext)new SimpleVariableContext(), DocumentNavigator.getInstance());
    }

    @Override
    public short getMatchType() {
        return this.pattern.getMatchType();
    }

    @Override
    public String getMatchesNodeName() {
        return this.pattern.getMatchesNodeName();
    }

    @Override
    public double getPriority() {
        return this.pattern.getPriority();
    }

    public String getText() {
        return this.text;
    }

    @Override
    public Pattern[] getUnionPatterns() {
        XPathPattern[] arrxPathPattern;
        org.jaxen.pattern.Pattern[] arrpattern = this.pattern.getUnionPatterns();
        if (arrpattern != null) {
            int n = arrpattern.length;
            arrxPathPattern = new XPathPattern[n];
            for (int i = 0; i < n; ++i) {
                arrxPathPattern[i] = new XPathPattern(arrpattern[i]);
            }
        } else {
            arrxPathPattern = null;
        }
        return arrxPathPattern;
    }

    protected void handleJaxenException(JaxenException jaxenException) throws XPathException {
        throw new XPathException(this.text, (Exception)jaxenException);
    }

    @Override
    public boolean matches(Node node) {
        try {
            ArrayList arrayList = new ArrayList(1);
            arrayList.add((Object)node);
            this.context.setNodeSet((List)arrayList);
            boolean bl = this.pattern.matches((Object)node, this.context);
            return bl;
        }
        catch (JaxenException var3_4) {
            this.handleJaxenException(var3_4);
            return false;
        }
    }

    public void setVariableContext(VariableContext variableContext) {
        this.context.getContextSupport().setVariableContext(variableContext);
    }

    public String toString() {
        return new StringBuffer().append("[XPathPattern: text: ").append(this.text).append(" Pattern: ").append((Object)this.pattern).append("]").toString();
    }
}

