KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > ConstructorFunctionLibrary


1 package net.sf.saxon.functions;
2
3 import net.sf.saxon.Configuration;
4 import net.sf.saxon.expr.CastExpression;
5 import net.sf.saxon.expr.Expression;
6 import net.sf.saxon.om.NamespaceConstant;
7 import net.sf.saxon.style.StandardNames;
8 import net.sf.saxon.trans.StaticError;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.type.AtomicType;
11 import net.sf.saxon.type.SchemaType;
12 import net.sf.saxon.type.Type;
13
14 /**
15  * The ConstructorFunctionLibrary represents the collection of constructor functions for atomic types. These
16  * are provided for the built-in types such as xs:integer and xs:date, and also for user-defined atomic types.
17  */

18
19 public class ConstructorFunctionLibrary implements FunctionLibrary {
20
21     private Configuration config;
22
23     /**
24      * Create a SystemFunctionLibrary
25      * @param config the Configuration
26      */

27
28     public ConstructorFunctionLibrary(Configuration config) {
29         this.config = config;
30     }
31
32     /**
33      * Test whether a system function with a given name and arity is available. This supports
34      * the function-available() function in XSLT. This method may be called either at compile time
35      * or at run time.
36      * @param uri The URI of the function name
37      * @param local The local part of the function name
38      * @param arity The number of arguments. This is set to -1 in the case of the single-argument
39      * function-available() function; in this case the method should return true if there is some
40      * matching extension function, regardless of its arity.
41      */

42
43     public boolean isAvailable(int fingerprint, String JavaDoc uri, String JavaDoc local, int arity) {
44         if (arity != 1 && arity != -1) {
45             return false;
46         }
47         if (uri.equals(NamespaceConstant.SCHEMA)) {
48             AtomicType type = (AtomicType)Type.getBuiltInItemType(uri, local);
49             return type != null && type.getFingerprint() != StandardNames.XS_NOTATION;
50         } else if (NamespaceConstant.isXDTNamespace(uri)) {
51             AtomicType type = (AtomicType)Type.getBuiltInItemType(NamespaceConstant.XDT, local);
52             return type != null && type.getFingerprint() != StandardNames.XDT_ANY_ATOMIC_TYPE;
53         }
54
55         SchemaType st = config.getSchemaType(fingerprint);
56         return (st != null && st instanceof AtomicType);
57     }
58
59     /**
60      * Bind an extension function, given the URI and local parts of the function name,
61      * and the list of expressions supplied as arguments. This method is called at compile
62      * time.
63      * @param uri The URI of the function name
64      * @param localName The local part of the function name
65      * @param arguments The expressions supplied statically in the function call. The intention is
66      * that the static type of the arguments (obtainable via getItemType() and getCardinality() may
67      * be used as part of the binding algorithm.
68      * @return An object representing the extension function to be called, if one is found;
69      * null if no extension function was found matching the required name and arity.
70      * @throws net.sf.saxon.trans.XPathException if a function is found with the required name and arity, but
71      * the implementation of the function cannot be loaded or used; or if an error occurs
72      * while searching for the function; or if this function library "owns" the namespace containing
73      * the function call, but no function was found.
74      */

75
76     public Expression bind(int nameCode, String JavaDoc uri, String JavaDoc localName, Expression[] arguments)
77             throws XPathException {
78         String JavaDoc targetURI = uri;
79         boolean builtInNamespace = uri.equals(NamespaceConstant.SCHEMA);
80         if (!builtInNamespace && NamespaceConstant.isXDTNamespace(uri)) {
81             targetURI = NamespaceConstant.XDT;
82             builtInNamespace = true;
83         }
84         if (builtInNamespace) {
85             // it's a constructor function: treat it as shorthand for a cast expression
86
if (arguments.length != 1) {
87                 throw new StaticError("A constructor function must have exactly one argument");
88             }
89             AtomicType type = (AtomicType)Type.getBuiltInItemType(targetURI, localName);
90             if (type==null || type.getFingerprint() == StandardNames.XDT_ANY_ATOMIC_TYPE ||
91                     type.getFingerprint() == StandardNames.XS_NOTATION) {
92                 StaticError err = new StaticError("Unknown constructor function: {" + uri + '}' + localName);
93                 err.setErrorCode("XPST0017");
94                 throw err;
95             }
96
97             return new CastExpression(arguments[0], type, true);
98         }
99
100         // Now see if it's a constructor function for a user-defined type
101

102         if (arguments.length == 1) {
103             SchemaType st = config.getSchemaType(nameCode & 0xfffff);
104             if (st != null && st instanceof AtomicType) {
105                 return new CastExpression(arguments[0], (AtomicType)st, true);
106             }
107         }
108
109         return null;
110     }
111
112     /**
113      * This method creates a copy of a FunctionLibrary: if the original FunctionLibrary allows
114      * new functions to be added, then additions to this copy will not affect the original, or
115      * vice versa.
116      *
117      * @return a copy of this function library. This must be an instance of the original class.
118      */

119
120     public FunctionLibrary copy() {
121         return this;
122     }
123
124 }
125 //
126
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
127
// you may not use this file except in compliance with the License. You may obtain a copy of the
128
// License at http://www.mozilla.org/MPL/
129
//
130
// Software distributed under the License is distributed on an "AS IS" basis,
131
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
132
// See the License for the specific language governing rights and limitations under the License.
133
//
134
// The Original Code is: all this file.
135
//
136
// The Initial Developer of the Original Code is Michael H. Kay.
137
//
138
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
139
//
140
// Contributor(s): none.
141
//
Popular Tags