KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > StandaloneContext


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.pattern.NameTest;
5 import com.icl.saxon.pattern.NamespaceTest;
6
7 import java.util.Hashtable JavaDoc;
8
9 /**
10 * A StandaloneContext provides a context for parsing an expression or pattern appearing
11 * in a context other than a stylesheet.
12 */

13
14 public class StandaloneContext implements StaticContext {
15
16     private NamePool namePool;
17     private Hashtable JavaDoc namespaces = new Hashtable JavaDoc();
18
19     /**
20     * Create a StandaloneContext using the default NamePool
21     */

22     
23     public StandaloneContext() {
24         this(NamePool.getDefaultNamePool());
25     }
26
27     /**
28     * Create a StandaloneContext using a specific NamePool
29     */

30     
31     public StandaloneContext(NamePool pool) {
32         namePool = pool;
33         declareNamespace("xml", Namespace.XML);
34         declareNamespace("xsl", Namespace.XSLT);
35         declareNamespace("saxon", Namespace.SAXON);
36         declareNamespace("", "");
37     }
38
39     /**
40     * Declare a namespace whose prefix can be used in expressions
41     */

42     
43     public void declareNamespace(String JavaDoc prefix, String JavaDoc uri) {
44         namespaces.put(prefix, uri);
45         namePool.allocateNamespaceCode(prefix, uri);
46     }
47
48
49     /**
50     * Copy the context with a different namepool. Not implemented, returns null.
51     */

52     
53     public StaticContext makeRuntimeContext(NamePool pool) {
54         return null;
55     }
56
57     /**
58     * Get the system ID of the container of the expression
59     * @return "" always
60     */

61
62     public String JavaDoc getSystemId() {
63         return "";
64     }
65
66     /**
67     * Get the Base URI of the stylesheet element, for resolving any relative URI's used
68     * in the expression.
69     * Used by the document() function.
70     * @return "" always
71     */

72     
73     public String JavaDoc getBaseURI() {
74         return "";
75     }
76
77     /**
78     * Get the line number of the expression within that container
79     * @return -1 always
80     */

81
82     public int getLineNumber() {
83         return -1;
84     }
85
86     /**
87     * Get the URI for a prefix, using this Element as the context for namespace resolution
88     * @param prefix The prefix
89     * @throw XPathException if the prefix is not declared
90     */

91
92     public String JavaDoc getURIForPrefix(String JavaDoc prefix) throws XPathException {
93         String JavaDoc uri = (String JavaDoc)namespaces.get(prefix);
94         if (uri==null) {
95             throw new XPathException("Prefix " + prefix + " has not been declared");
96         }
97         return uri;
98     }
99     
100     /**
101     * Make a NameCode, using this Element as the context for namespace resolution
102     * @param qname The name as written, in the form "[prefix:]localname"
103     * @boolean useDefault Defines the action when there is no prefix. If true, use
104     * the default namespace URI (as for element names). If false, use no namespace URI
105     * (as for attribute names).
106     */

107
108     public final int makeNameCode(String JavaDoc qname, boolean useDefault) throws XPathException {
109         String JavaDoc prefix = Name.getPrefix(qname);
110         String JavaDoc localName = Name.getLocalName(qname);
111         String JavaDoc uri;
112         if (prefix.equals("") && useDefault) {
113             uri = "";
114         } else {
115             uri = getURIForPrefix(prefix);
116         }
117         return namePool.allocate(prefix, uri, localName);
118     }
119
120     /**
121     * Make a fingerprint, using this Element as the context for namespace resolution
122     * @param qname The name as written, in the form "[prefix:]localname"
123     * @boolean useDefault Defines the action when there is no prefix. If true, use
124     * the default namespace URI (as for element names). If false, use no namespace URI
125     * (as for attribute names).
126     * @throw XPathException if the name is not already present in the namepool.
127     */

128
129     public final int getFingerprint(String JavaDoc qname, boolean useDefault) throws XPathException {
130         String JavaDoc prefix = Name.getPrefix(qname);
131         String JavaDoc localName = Name.getLocalName(qname);
132         String JavaDoc uri;
133         if (prefix.equals("") && useDefault) {
134             uri = "";
135         } else {
136             uri = getURIForPrefix(prefix);
137         }
138         return namePool.getFingerprint(uri, localName);
139
140     }
141
142     /**
143     * Make a NameTest, using this element as the context for namespace resolution
144     */

145     
146     public NameTest makeNameTest(short nodeType, String JavaDoc qname, boolean useDefault)
147         throws XPathException {
148         return new NameTest(nodeType, makeNameCode(qname, useDefault));
149     }
150
151     /**
152     * Make a NamespaceTest, using this element as the context for namespace resolution
153     */

154     
155     public NamespaceTest makeNamespaceTest(short nodeType, String JavaDoc prefix)
156             throws XPathException {
157         return new NamespaceTest(namePool, nodeType, getURICodeForPrefix(prefix));
158     }
159     
160    /**
161     * Search the NamespaceList for a given prefix, returning the corresponding URI code.
162     * @param prefix The prefix to be matched. To find the default namespace, supply ""
163     * @return The URI code corresponding to this namespace. If it is an unnamed default namespace,
164     * return "".
165     * @throws XPathException if the prefix has not been declared on this element or a containing
166     * element.
167     */

168
169     private short getURICodeForPrefix(String JavaDoc prefix) throws XPathException {
170         String JavaDoc uri = getURIForPrefix(prefix);
171         return namePool.getCodeForURI(uri);
172     }
173
174     /**
175     * Bind a variable used in this element to the XSLVariable element in which it is declared
176     */

177
178     public Binding bindVariable(int fingerprint) throws XPathException {
179         throw new XPathException("Variables are not allowed in a standalone expression");
180     }
181
182     /**
183     * Determine whether a given URI identifies an extension element namespace
184     */

185
186     public boolean isExtensionNamespace(short uriCode) {
187         return false;
188     }
189
190     /**
191     * Determine whether forwards-compatible mode is enabled
192     */

193
194     public boolean forwardsCompatibleModeIsEnabled() {
195         return false;
196     }
197
198     /*
199     * Get a Function declared using a saxon:function element in the stylesheet
200     * @param fingerprint the fingerprint of the name of the function
201     * @return the Function object represented by this saxon:function; or null if not found
202     */

203
204     public Function getStyleSheetFunction(int fingerprint) throws XPathException {
205         return null;
206     }
207
208     /**
209     * Get an external Java class corresponding to a given namespace prefix, if there is
210     * one.
211     * @param uri The namespace URI corresponding to the prefix used in the function call.
212     * @return the Java class name if a suitable class exists, otherwise return null. This
213     * implementation always returns null.
214     */

215     
216     public Class JavaDoc getExternalJavaClass(String JavaDoc uri) {
217         return null;
218     }
219
220     /**
221     * Determine if an extension element is available
222     */

223     
224     public boolean isElementAvailable(String JavaDoc qname) throws XPathException {
225         return false;
226     }
227
228     /**
229     * Determine if a function is available
230     */

231     
232     public boolean isFunctionAvailable(String JavaDoc qname) throws XPathException {
233         
234         String JavaDoc prefix = Name.getPrefix(qname);
235         if (prefix.equals("")) {
236             return ExpressionParser.makeSystemFunction(qname)!=null;
237         }
238         
239         return false; // no user functions allowed in standalone context.
240

241         //String uri = getURIForPrefix(prefix);
242
//String localName = Name.getLocalName(qname);
243
//
244
//FunctionProxy fp = new FunctionProxy();
245
//return fp.setMethod(uri, localName);
246

247     }
248
249     /**
250     * Determine whether the key() function is permmitted in this context
251     */

252     
253     public boolean allowsKeyFunction() {
254         return false;
255     }
256
257     /**
258     * Get the effective XSLT version in this region of the stylesheet
259     */

260     
261     public String JavaDoc getVersion() {
262         return "1.1";
263     }
264
265 }
266
267 //
268
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
269
// you may not use this file except in compliance with the License. You may obtain a copy of the
270
// License at http://www.mozilla.org/MPL/
271
//
272
// Software distributed under the License is distributed on an "AS IS" basis,
273
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
274
// See the License for the specific language governing rights and limitations under the License.
275
//
276
// The Original Code is: all this file.
277
//
278
// The Initial Developer of the Original Code is
279
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
280
//
281
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
282
//
283
// Contributor(s): none.
284
//
285
Popular Tags