📄 xqresolvenames.java
字号:
// Copyright (c) 2003, 2004, 2006 Per M.A. Bothner.// This is free software; for terms and warranty disclaimer see ./COPYING.package gnu.xquery.lang;import gnu.expr.*;import gnu.kawa.xml.*;import gnu.xml.*;import gnu.mapping.*;import gnu.bytecode.*;import gnu.kawa.reflect.StaticFieldLocation;import gnu.kawa.functions.GetNamedPart;import gnu.xquery.util.NamedCollator;import gnu.xquery.util.QNameUtils;import java.util.Vector;import gnu.math.DateTime;import gnu.math.IntNum;public class XQResolveNames extends ResolveNames{ XQParser parser; /** Code number for the special <code>last</code> function. */ public static final int LAST_BUILTIN = -1; /** Code number for the special <code>position</code> function. */ public static final int POSITION_BUILTIN = -2; /** Value of {@code xs:QName()} constructor. */ public static final int XS_QNAME_BUILTIN = -3; /** Code number for the special <code>compare</code> function. */ public static final int COMPARE_BUILTIN = -4; /** Code number for the special <code>distinct-values</code> function. */ public static final int DISTINCT_VALUES_BUILTIN = -5; /** Code number for the special <code>local-name</code> function. */ public static final int LOCAL_NAME_BUILTIN = -6; /** Code number for the special <code>namespace-uri</code> function. */ public static final int NAMESPACE_URI_BUILTIN = -7; /** Code number for the special <code>collection</code> function. */ public static final int COLLECTION_BUILTIN = -8; /** Code number for the special <code>doc</code> function. */ public static final int DOC_BUILTIN = -9; /** Code number for the special <code>doc-available</code> function. */ public static final int DOC_AVAILABLE_BUILTIN = -10; /** Code number for the special <code>doc-available</code> function. */ public static final int BASE_URI_BUILTIN = -11; /** Code number for the special <code>ressolve-uri</code> function. */ public static final int RESOLVE_URI_BUILTIN = -12; /** Code number for internal function that maps prefix to uri. */ public static final int RESOLVE_PREFIX_BUILTIN = -13; /** Code number for the special <code>static-base-uri</code> function. */ public static final int STATIC_BASE_URI_BUILTIN = -14; /** Code number for the special <code>index-of</code> function. */ public static final int INDEX_OF_BUILTIN = -15; /** Code number for the special <code>string</code> function. */ public static final int STRING_BUILTIN = -16; /** Code number for the special <code>normalize-space</code> function. */ public static final int NORMALIZE_SPACE_BUILTIN = -17; /** Code number for the special <code>unordered</code> function. */ public static final int UNORDERED_BUILTIN = -18; /** Code number for the special <code>lang</code> function. */ public static final int LANG_BUILTIN = -23; /** Code number for the special <code>name</code> function. */ public static final int NAME_BUILTIN = -24; /** Code number for the special <code>deep-equal</code> function. */ public static final int DEEP_EQUAL_BUILTIN = -25; /** Code number for the special <code>min</code> function. */ public static final int MIN_BUILTIN = -26; /** Code number for the special <code>max</code> function. */ public static final int MAX_BUILTIN = -27; /** Code number for the special <code>number</code> function. */ public static final int NUMBER_BUILTIN = -28; /** Code number for the special <code>default-collation</code> function. */ public static final int DEFAULT_COLLATION_BUILTIN = -29; /** Code number for the special <code>id</code> function. */ public static final int ID_BUILTIN = -30; /** Code number for the special <code>idref</code> function. */ public static final int IDREF_BUILTIN = -31; /** Code number for the special <code>root</code> function. */ public static final int ROOT_BUILTIN = -32; public static final int CAST_AS_BUILTIN = -33; public static final int CASTABLE_AS_BUILTIN = -34; /** Code number for internal function to handle extensions. */ public static final int HANDLE_EXTENSION_BUILTIN = -35; public static final Declaration handleExtensionDecl = makeBuiltin("(extension)", HANDLE_EXTENSION_BUILTIN); public static final Declaration castAsDecl = makeBuiltin("(cast as)", CAST_AS_BUILTIN); public static final Declaration castableAsDecl = makeBuiltin("(castable as)", CASTABLE_AS_BUILTIN); /** Declaration for the <code>fn:last()</code> function. */ public static final Declaration lastDecl = makeBuiltin("last", LAST_BUILTIN); public static final Declaration xsQNameDecl = makeBuiltin(Symbol.make(XQuery.SCHEMA_NAMESPACE, "QName"), XS_QNAME_BUILTIN); public static final Declaration staticBaseUriDecl = makeBuiltin("static-base-uri", STATIC_BASE_URI_BUILTIN); public static final Declaration resolvePrefixDecl = makeBuiltin(Symbol.make(XQuery.SCHEMA_NAMESPACE, "(resolve-prefix)"), RESOLVE_PREFIX_BUILTIN); /** Create a <code>Declaration</code> for a builtin function. */ public static Declaration makeBuiltin (String name, int code) { return makeBuiltin (Symbol.make(XQuery.XQUERY_FUNCTION_NAMESPACE, name, "fn"), code); } /** Create a <code>Declaration</code> for a builtin function. */ public static Declaration makeBuiltin (Symbol name, int code) { Declaration decl = new Declaration(name); decl.setProcedureDecl(true); decl.setCode(code); return decl; } public XQResolveNames () { this(null); } void pushBuiltin (String name, int code) { lookup.push(makeBuiltin(name, code)); } public XQResolveNames (Compilation comp) { super(comp); lookup.push(lastDecl); lookup.push(xsQNameDecl); lookup.push(staticBaseUriDecl); pushBuiltin("position", POSITION_BUILTIN); pushBuiltin("compare", COMPARE_BUILTIN); pushBuiltin("distinct-values", DISTINCT_VALUES_BUILTIN); pushBuiltin("local-name", LOCAL_NAME_BUILTIN); pushBuiltin("name", NAME_BUILTIN); pushBuiltin("namespace-uri", NAMESPACE_URI_BUILTIN); pushBuiltin("root", ROOT_BUILTIN); pushBuiltin("base-uri", BASE_URI_BUILTIN); pushBuiltin("lang", LANG_BUILTIN); pushBuiltin("resolve-uri", RESOLVE_URI_BUILTIN); pushBuiltin("collection", COLLECTION_BUILTIN); pushBuiltin("doc", DOC_BUILTIN); pushBuiltin("document", DOC_BUILTIN); // Obsolete pushBuiltin("doc-available", DOC_AVAILABLE_BUILTIN); pushBuiltin("index-of", INDEX_OF_BUILTIN); pushBuiltin("string", STRING_BUILTIN); pushBuiltin("normalize-space", NORMALIZE_SPACE_BUILTIN); pushBuiltin("unordered", UNORDERED_BUILTIN); pushBuiltin("deep-equal", DEEP_EQUAL_BUILTIN); pushBuiltin("min", MIN_BUILTIN); pushBuiltin("max", MAX_BUILTIN); pushBuiltin("number", NUMBER_BUILTIN); pushBuiltin("default-collation", DEFAULT_COLLATION_BUILTIN); pushBuiltin("id", ID_BUILTIN); pushBuiltin("idref", IDREF_BUILTIN); } public Namespace[] functionNamespacePath = XQuery.defaultFunctionNamespacePath; protected void push (ScopeExp exp) { for (Declaration decl = exp.firstDecl(); decl != null; decl = decl.nextDecl()) { push(decl); } } void push (Declaration decl) { Compilation comp = getCompilation(); Object name = decl.getSymbol(); boolean function = decl.isProcedureDecl(); if (name instanceof String) { int line = decl.getLineNumber(); if (line > 0 && comp != null) { String saveFilename = comp.getFileName(); int saveLine = comp.getLineNumber(); int saveColumn = comp.getColumnNumber(); comp.setLocation(decl); name = parser.namespaceResolve((String) name, function); comp.setLine(saveFilename, saveLine, saveColumn); } else name = parser.namespaceResolve((String) name, function); if (name == null) return; decl.setName(name); } Declaration old = lookup.lookup(name, XQuery.instance.getNamespaceOf(decl)); if (old != null) { if (decl.context == old.context) ScopeExp.duplicateDeclarationError(old, decl, comp); else if (XQParser.warnHidePreviousDeclaration && (! (name instanceof Symbol) || ((Symbol) name).getNamespace() != null)) comp.error('w', decl, "declaration ", " hides previous declaration"); } lookup.push(decl); } Declaration flookup (Symbol sym) { Environment env = XQuery.xqEnvironment; gnu.mapping.Location loc = env.lookup(sym, EnvironmentKey.FUNCTION); if (loc == null) return null; loc = loc.getBase(); if (loc instanceof StaticFieldLocation) { Declaration decl = ((StaticFieldLocation) loc).getDeclaration(); if (decl != null) return decl; } Object val = loc.get(null); if (val != null) return procToDecl(sym, val); return null; } protected Expression walkReferenceExp (ReferenceExp exp) { return walkReferenceExp(exp, null); } protected Expression walkReferenceExp (ReferenceExp exp, ApplyExp call) { if (exp.getBinding() == null) { Object symbol = exp.getSymbol(); boolean function = exp.isProcedureName(); int namespace = call == null ? XQuery.VALUE_NAMESPACE : XQuery.namespaceForFunctions(call.getArgCount()); Declaration decl = lookup.lookup(symbol, namespace); Symbol sym; if (decl != null) ; else if (symbol instanceof Symbol && "".equals((sym = (Symbol) symbol).getNamespaceURI())) { // kludge - use xxx_BUILTIN mechanism? FIXME String name = sym.getLocalName(); Expression f; if ("request".equals(name)) f = XQParser.makeFunctionExp("gnu.kawa.servlet.GetRequest", "getRequest"); else if ("response".equals(name)) f = XQParser.makeFunctionExp("gnu.kawa.servlet.GetResponse", "getResponse"); else f = null; if (f != null) return new ApplyExp(f, Expression.noExpressions); } else if (symbol instanceof Symbol) { // Never happens, I believe. decl = flookup((Symbol) symbol); } else // if (symbol instanceof String) { String name = (String) symbol; if (name.indexOf(':') < 0) { name = name.intern(); if (function) { for (int i = 0; i < functionNamespacePath.length; i++) { sym = functionNamespacePath[i].getSymbol(name); decl = lookup.lookup(sym, function); if (decl != null) break; if (! function) continue; decl = flookup(sym);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -