/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  java.io.Serializable
 *  java.lang.Boolean
 *  java.lang.Class
 *  java.lang.Exception
 *  java.lang.Number
 *  java.lang.Object
 *  java.lang.String
 *  java.lang.StringBuffer
 *  java.lang.Throwable
 *  java.util.Collections
 *  java.util.Comparator
 *  java.util.HashMap
 *  java.util.HashSet
 *  java.util.Iterator
 *  java.util.List
 *  java.util.Map
 *  org.jaxen.FunctionContext
 *  org.jaxen.JaxenException
 *  org.jaxen.NamespaceContext
 *  org.jaxen.SimpleNamespaceContext
 *  org.jaxen.VariableContext
 *  org.jaxen.XPath
 *  org.jaxen.dom4j.Dom4jXPath
 */
package org.dom4j.xpath;

import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.InvalidXPathException;
import org.dom4j.Node;
import org.dom4j.NodeFilter;
import org.dom4j.XPath;
import org.dom4j.XPathException;
import org.dom4j.xpath.DefaultNamespaceContext;
import org.dom4j.xpath.DefaultXPath$1;
import org.jaxen.FunctionContext;
import org.jaxen.JaxenException;
import org.jaxen.NamespaceContext;
import org.jaxen.SimpleNamespaceContext;
import org.jaxen.VariableContext;
import org.jaxen.dom4j.Dom4jXPath;

public class DefaultXPath
implements Serializable,
NodeFilter,
XPath {
    private NamespaceContext namespaceContext;
    private String text;
    private org.jaxen.XPath xpath;

    public DefaultXPath(String string2) throws InvalidXPathException {
        this.text = string2;
        this.xpath = DefaultXPath.parse(string2);
    }

    protected static org.jaxen.XPath parse(String string2) {
        try {
            Dom4jXPath dom4jXPath = new Dom4jXPath(string2);
            return dom4jXPath;
        }
        catch (JaxenException var3_2) {
            throw new InvalidXPathException(string2, var3_2.getMessage());
        }
        catch (Throwable var2_3) {
            throw new InvalidXPathException(string2, var2_3);
        }
    }

    @Override
    public boolean booleanValueOf(Object object) {
        try {
            this.setNSContext(object);
            boolean bl = this.xpath.booleanValueOf(object);
            return bl;
        }
        catch (JaxenException var2_3) {
            this.handleJaxenException(var2_3);
            return false;
        }
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public Object evaluate(Object object) {
        List list;
        try {
            this.setNSContext(object);
            list = this.xpath.selectNodes(object);
            if (list == null) return list;
        }
        catch (JaxenException var2_4) {
            this.handleJaxenException(var2_4);
            return null;
        }
        if (list.size() != 1) return list;
        Object object2 = list.get(0);
        return object2;
    }

    protected Object getCompareValue(Node node) {
        return this.valueOf(node);
    }

    @Override
    public FunctionContext getFunctionContext() {
        return this.xpath.getFunctionContext();
    }

    @Override
    public NamespaceContext getNamespaceContext() {
        return this.namespaceContext;
    }

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

    @Override
    public VariableContext getVariableContext() {
        return this.xpath.getVariableContext();
    }

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

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    public boolean matches(Node node) {
        this.setNSContext(node);
        List list = this.xpath.selectNodes((Object)node);
        if (list == null) return false;
        try {
            if (list.size() <= 0) return false;
            Object object = list.get(0);
            if (!(object instanceof Boolean)) return list.contains((Object)node);
            return (Boolean)object;
        }
        catch (JaxenException var2_5) {
            this.handleJaxenException(var2_5);
        }
        return false;
    }

    @Override
    public Number numberValueOf(Object object) {
        try {
            this.setNSContext(object);
            Number number = this.xpath.numberValueOf(object);
            return number;
        }
        catch (JaxenException var2_3) {
            this.handleJaxenException(var2_3);
            return null;
        }
    }

    protected void removeDuplicates(List list, Map map) {
        HashSet hashSet = new HashSet();
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object object = map.get(iterator.next());
            if (hashSet.contains(object)) {
                iterator.remove();
                continue;
            }
            hashSet.add(object);
        }
    }

    @Override
    public List selectNodes(Object object) {
        try {
            this.setNSContext(object);
            List list = this.xpath.selectNodes(object);
            return list;
        }
        catch (JaxenException var2_3) {
            this.handleJaxenException(var2_3);
            return Collections.EMPTY_LIST;
        }
    }

    @Override
    public List selectNodes(Object object, XPath xPath) {
        List list = this.selectNodes(object);
        xPath.sort(list);
        return list;
    }

    @Override
    public List selectNodes(Object object, XPath xPath, boolean bl) {
        List list = this.selectNodes(object);
        xPath.sort(list, bl);
        return list;
    }

    @Override
    public Object selectObject(Object object) {
        return this.evaluate(object);
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public Node selectSingleNode(Object object) {
        Object object2;
        try {
            this.setNSContext(object);
            object2 = this.xpath.selectSingleNode(object);
            if (object2 instanceof Node) {
                return (Node)object2;
            }
        }
        catch (JaxenException var2_3) {
            this.handleJaxenException(var2_3);
            return null;
        }
        if (object2 != null) {
            throw new XPathException(new StringBuffer().append("The result of the XPath expression is not a Node. It was: ").append(object2).append(" of type: ").append(object2.getClass().getName()).toString());
        }
        return null;
    }

    @Override
    public void setFunctionContext(FunctionContext functionContext) {
        this.xpath.setFunctionContext(functionContext);
    }

    protected void setNSContext(Object object) {
        if (this.namespaceContext == null) {
            this.xpath.setNamespaceContext((NamespaceContext)DefaultNamespaceContext.create(object));
        }
    }

    @Override
    public void setNamespaceContext(NamespaceContext namespaceContext) {
        this.namespaceContext = namespaceContext;
        this.xpath.setNamespaceContext(namespaceContext);
    }

    @Override
    public void setNamespaceURIs(Map map) {
        this.setNamespaceContext((NamespaceContext)new SimpleNamespaceContext(map));
    }

    @Override
    public void setVariableContext(VariableContext variableContext) {
        this.xpath.setVariableContext(variableContext);
    }

    @Override
    public void sort(List list) {
        this.sort(list, false);
    }

    protected void sort(List list, Map map) {
        Collections.sort((List)list, (Comparator)new DefaultXPath$1((DefaultXPath)this, map));
    }

    @Override
    public void sort(List list, boolean bl) {
        if (list != null && !list.isEmpty()) {
            int n = list.size();
            HashMap hashMap = new HashMap(n);
            for (int i = 0; i < n; ++i) {
                Object object = list.get(i);
                if (!(object instanceof Node)) continue;
                Node node = (Node)object;
                hashMap.put((Object)node, this.getCompareValue(node));
            }
            this.sort(list, (Map)hashMap);
            if (bl) {
                this.removeDuplicates(list, (Map)hashMap);
            }
        }
    }

    public String toString() {
        return new StringBuffer().append("[XPath: ").append((Object)this.xpath).append("]").toString();
    }

    @Override
    public String valueOf(Object object) {
        try {
            this.setNSContext(object);
            String string2 = this.xpath.stringValueOf(object);
            return string2;
        }
        catch (JaxenException var2_3) {
            this.handleJaxenException(var2_3);
            return "";
        }
    }
}

