Virtual Trading v2.0.95版本的 MD5 值为:ef0f95d706f7364286da0edbbd29a27d

以下内容为反编译后的 NodeUtils.java 源代码,内容仅作参考


package gnu.xquery.util;

import gnu.bytecode.ClassType;
import gnu.kawa.reflect.ClassMethods;
import gnu.kawa.xml.Document;
import gnu.kawa.xml.KDocument;
import gnu.kawa.xml.KElement;
import gnu.kawa.xml.KNode;
import gnu.kawa.xml.Nodes;
import gnu.kawa.xml.SortedNodes;
import gnu.kawa.xml.UntypedAtomic;
import gnu.lists.Consumer;
import gnu.lists.PositionConsumer;
import gnu.mapping.CallContext;
import gnu.mapping.Environment;
import gnu.mapping.Procedure;
import gnu.mapping.Symbol;
import gnu.mapping.Values;
import gnu.mapping.WrongType;
import gnu.text.Path;
import gnu.xml.NamespaceBinding;
import gnu.xml.NodeTree;
import gnu.xml.TextUtils;
import gnu.xml.XName;
import gnu.xquery.lang.XQuery;
import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.Stack;

public class NodeUtils {
    static String collectionNamespace = "http://gnu.org/kawa/cached-collections";
    public static final Symbol collectionResolverSymbol = Symbol.make(XQuery.LOCAL_NAMESPACE, "collection-resolver", "qexo");

    public static Object nodeName(Object obj) {
        if (obj == Values.empty || obj == null) {
            return obj;
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("node-name", 1, obj, "node()?");
        }
        Symbol nodeSymbol = ((KNode) obj).getNodeSymbol();
        return nodeSymbol == null ? Values.empty : nodeSymbol;
    }

    public static String name(Object obj) {
        Object nodeNameObject;
        return (obj == Values.empty || obj == null || (nodeNameObject = ((KNode) obj).getNodeNameObject()) == null || nodeNameObject == Values.empty) ? "" : nodeNameObject.toString();
    }

    public static String localName(Object obj) {
        if (obj == Values.empty || obj == null) {
            return "";
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("local-name", 1, obj, "node()?");
        }
        Object nodeNameObject = ((KNode) obj).getNodeNameObject();
        if (nodeNameObject == null || nodeNameObject == Values.empty) {
            return "";
        }
        if (nodeNameObject instanceof Symbol) {
            return ((Symbol) nodeNameObject).getName();
        }
        return nodeNameObject.toString();
    }

    public static Object namespaceURI(Object obj) {
        if (obj == Values.empty || obj == null) {
            return "";
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("namespace-uri", 1, obj, "node()?");
        }
        Object nodeNameObject = ((KNode) obj).getNodeNameObject();
        return nodeNameObject instanceof Symbol ? QNameUtils.namespaceURIFromQName(nodeNameObject) : "";
    }

    public static void prefixesFromNodetype(XName xName, Consumer consumer) {
        NamespaceBinding namespaceNodes = xName.getNamespaceNodes();
        for (NamespaceBinding namespaceBinding = namespaceNodes; namespaceBinding != null; namespaceBinding = namespaceBinding.getNext()) {
            if (namespaceBinding.getUri() != null) {
                String prefix = namespaceBinding.getPrefix();
                NamespaceBinding namespaceBinding2 = namespaceNodes;
                while (true) {
                    if (namespaceBinding2 == namespaceBinding) {
                        if (prefix == null) {
                            prefix = "";
                        }
                        consumer.writeObject(prefix);
                    } else if (namespaceBinding2.getPrefix() == prefix) {
                        break;
                    } else {
                        namespaceBinding2 = namespaceBinding2.getNext();
                    }
                }
            }
        }
    }

    public static void inScopePrefixes$X(Object obj, CallContext callContext) {
        Object nodeNameObject = ((KElement) obj).getNodeNameObject();
        if (nodeNameObject instanceof XName) {
            prefixesFromNodetype((XName) nodeNameObject, callContext.consumer);
        } else {
            callContext.consumer.writeObject("xml");
        }
    }

    public static void data$X(Object obj, CallContext callContext) {
        Consumer consumer = callContext.consumer;
        if (obj instanceof Values) {
            Values values = (Values) obj;
            int startPos = values.startPos();
            while (true) {
                startPos = values.nextPos(startPos);
                if (startPos == 0) {
                    return;
                } else {
                    consumer.writeObject(KNode.atomicValue(values.getPosPrevious(startPos)));
                }
            }
        } else {
            consumer.writeObject(KNode.atomicValue(obj));
        }
    }

    public static Object root(Object obj) {
        if (obj == null || obj == Values.empty) {
            return obj;
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("root", 1, obj, "node()?");
        }
        KNode kNode = (KNode) obj;
        return Nodes.root((NodeTree) kNode.sequence, kNode.getPos());
    }

    public static KDocument rootDocument(Object obj) {
        if (!(obj instanceof KNode)) {
            throw new WrongType("root-document", 1, obj, "node()?");
        }
        KNode kNode = (KNode) obj;
        KNode root = Nodes.root((NodeTree) kNode.sequence, kNode.getPos());
        if (!(root instanceof KDocument)) {
            throw new WrongType("root-document", 1, obj, "document()");
        }
        return (KDocument) root;
    }

    public static String getLang(KNode kNode) {
        NodeTree nodeTree = (NodeTree) kNode.sequence;
        int ancestorAttribute = nodeTree.ancestorAttribute(kNode.ipos, NamespaceBinding.XML_NAMESPACE, "lang");
        if (ancestorAttribute == 0) {
            return null;
        }
        return KNode.getNodeValue(nodeTree, ancestorAttribute);
    }

    public static boolean lang(Object obj, Object obj2) {
        String stringValue = (obj == null || obj == Values.empty) ? "" : TextUtils.stringValue(obj);
        String lang = getLang((KNode) obj2);
        if (lang == null) {
            return false;
        }
        int length = lang.length();
        int length2 = stringValue.length();
        if (length > length2 && lang.charAt(length2) == '-') {
            lang = lang.substring(0, length2);
        }
        return lang.equalsIgnoreCase(stringValue);
    }

    public static Object documentUri(Object obj) {
        if (obj == null || obj == Values.empty) {
            return obj;
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("xs:document-uri", 1, obj, "node()?");
        }
        KNode kNode = (KNode) obj;
        Object documentUriOfPos = ((NodeTree) kNode.sequence).documentUriOfPos(kNode.ipos);
        return documentUriOfPos == null ? Values.empty : documentUriOfPos;
    }

    public static Object nilled(Object obj) {
        if (obj == null || obj == Values.empty) {
            return obj;
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("nilled", 1, obj, "node()?");
        }
        if (!(obj instanceof KElement)) {
            return Values.empty;
        }
        return Boolean.FALSE;
    }

    public static Object baseUri(Object obj) {
        if (obj == null || obj == Values.empty) {
            return obj;
        }
        if (!(obj instanceof KNode)) {
            throw new WrongType("base-uri", 1, obj, "node()?");
        }
        Path baseURI = ((KNode) obj).baseURI();
        return baseURI == null ? Values.empty : baseURI;
    }

    static Object getIDs(Object obj, Object obj2) {
        int i;
        Stack stack;
        if (obj instanceof KNode) {
            obj = KNode.atomicValue(obj);
        }
        if (obj instanceof Values) {
            Object[] values = ((Values) obj).getValues();
            int length = values.length;
            while (true) {
                length--;
                if (length < 0) {
                    break;
                }
                obj2 = getIDs(values[length], obj2);
            }
        } else {
            String coerceToString = StringUtils.coerceToString(obj, "fn:id", 1, "");
            int length2 = coerceToString.length();
            int i2 = 0;
            while (i2 < length2) {
                int i3 = i2 + 1;
                char charAt = coerceToString.charAt(i2);
                if (Character.isWhitespace(charAt)) {
                    i2 = i3;
                } else {
                    if (XName.isNameStart(charAt)) {
                        i = i3 - 1;
                        while (i3 < length2) {
                            char charAt2 = coerceToString.charAt(i3);
                            if (Character.isWhitespace(charAt2)) {
                                break;
                            }
                            i3++;
                            if (i < length2 && !XName.isNamePart(charAt2)) {
                            }
                        }
                        if (i < length2) {
                            String substring = coerceToString.substring(i, i3);
                            if (obj2 == null) {
                                obj2 = substring;
                            } else {
                                if (obj2 instanceof Stack) {
                                    stack = (Stack) obj2;
                                } else {
                                    stack = new Stack();
                                    stack.push(obj2);
                                    obj2 = stack;
                                }
                                stack.push(substring);
                            }
                        }
                        i2 = i3 + 1;
                    }
                    i = length2;
                }
            }
        }
        return obj2;
    }

    public static void id$X(Object obj, Object obj2, CallContext callContext) {
        KNode kNode = (KNode) obj2;
        NodeTree nodeTree = (NodeTree) kNode.sequence;
        Consumer consumer = callContext.consumer;
        Object iDs = getIDs(obj, null);
        if (iDs == null) {
            return;
        }
        nodeTree.makeIDtableIfNeeded();
        if ((consumer instanceof PositionConsumer) && ((iDs instanceof String) || (consumer instanceof SortedNodes))) {
            idScan(iDs, nodeTree, (PositionConsumer) consumer);
            return;
        }
        if (iDs instanceof String) {
            int lookupID = nodeTree.lookupID((String) iDs);
            if (lookupID != -1) {
                consumer.writeObject(KNode.make(nodeTree, lookupID));
                return;
            }
            return;
        }
        SortedNodes sortedNodes = new SortedNodes();
        idScan(iDs, nodeTree, sortedNodes);
        Values.writeValues(sortedNodes, consumer);
    }

    private static void idScan(Object obj, NodeTree nodeTree, PositionConsumer positionConsumer) {
        if (obj instanceof String) {
            int lookupID = nodeTree.lookupID((String) obj);
            if (lookupID != -1) {
                positionConsumer.writePosition(nodeTree, lookupID);
                return;
            }
            return;
        }
        if (obj instanceof Stack) {
            Stack stack = (Stack) obj;
            int size = stack.size();
            for (int i = 0; i < size; i++) {
                idScan(stack.elementAt(i), nodeTree, positionConsumer);
            }
        }
    }

    public static Object idref(Object obj, Object obj2) {
        KNode kNode = (KNode) obj2;
        return Values.empty;
    }

    public static void setSavedCollection(Object obj, Object obj2, Environment environment) {
        if (obj == null) {
            obj = "#default";
        }
        environment.put(Symbol.make(collectionNamespace, obj.toString()), null, obj2);
    }

    public static void setSavedCollection(Object obj, Object obj2) {
        setSavedCollection(obj, obj2, Environment.getCurrent());
    }

    public static Object getSavedCollection(Object obj, Environment environment) {
        if (obj == null) {
            obj = "#default";
        }
        Object obj2 = environment.get(Symbol.make(collectionNamespace, obj.toString()), null, null);
        if (obj2 != null) {
            return obj2;
        }
        throw new RuntimeException("collection '" + obj + "' not found");
    }

    public static Object getSavedCollection(Object obj) {
        return getSavedCollection(obj, Environment.getCurrent());
    }

    public static Object collection(Object obj, Object obj2) throws Throwable {
        String obj3;
        int indexOf;
        Object resolve = resolve(obj, obj2, "collection");
        Environment current = Environment.getCurrent();
        Symbol symbol = collectionResolverSymbol;
        Object obj4 = current.get(symbol, null, null);
        if (obj4 == null) {
            obj4 = current.get(Symbol.makeWithUnknownNamespace(symbol.getLocalName(), symbol.getPrefix()), null, null);
        }
        if (obj4 == null) {
            return getSavedCollection(resolve);
        }
        if (((obj4 instanceof String) || (obj4 instanceof UntypedAtomic)) && (indexOf = (obj3 = obj4.toString()).indexOf(58)) > 0) {
            String substring = obj3.substring(0, indexOf);
            String substring2 = obj3.substring(indexOf + 1);
            try {
                obj4 = ClassMethods.apply((ClassType) ClassType.make(Class.forName(substring)), substring2, (char) 0, XQuery.instance);
                if (obj4 == null) {
                    throw new RuntimeException("invalid collection-resolver: no method " + substring2 + " in " + substring);
                }
            } catch (ClassNotFoundException unused) {
                throw new RuntimeException("invalid collection-resolver: class " + substring + " not found");
            } catch (Throwable th) {
                throw new RuntimeException("invalid collection-resolver: " + th);
            }
        }
        if (!(obj4 instanceof Procedure)) {
            throw new RuntimeException("invalid collection-resolver: " + obj4);
        }
        return ((Procedure) obj4).apply1(resolve);
    }

    static Object resolve(Object obj, Object obj2, String str) throws Throwable {
        if (!(obj instanceof File) && !(obj instanceof Path) && !(obj instanceof URI) && !(obj instanceof URL)) {
            obj = StringUtils.coerceToString(obj, str, 1, null);
        }
        if (obj == Values.empty || obj == null) {
            return null;
        }
        return Path.currentPath().resolve(Path.valueOf(obj));
    }

    public static Object docCached(Object obj, Object obj2) throws Throwable {
        Object resolve = resolve(obj, obj2, "doc");
        if (resolve == null) {
            return Values.empty;
        }
        return Document.parseCached(resolve);
    }

    public static boolean availableCached(Object obj, Object obj2) throws Throwable {
        Object resolve = resolve(obj, obj2, "doc-available");
        if (resolve == null) {
            return false;
        }
        try {
            Document.parseCached(resolve);
            return true;
        } catch (Throwable unused) {
            return false;
        }
    }
}