KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > style > ExpressionContext


1 package net.sf.saxon.style;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.Err;
4 import net.sf.saxon.type.AtomicType;
5 import net.sf.saxon.type.BuiltInAtomicType;
6 import net.sf.saxon.expr.VariableDeclaration;
7 import net.sf.saxon.functions.FunctionLibrary;
8 import net.sf.saxon.instruct.LocationMap;
9 import net.sf.saxon.om.*;
10 import net.sf.saxon.trans.StaticError;
11 import net.sf.saxon.trans.XPathException;
12
13 import javax.xml.transform.SourceLocator JavaDoc;
14 import java.util.Comparator JavaDoc;
15
16 /**
17 * An ExpressionContext represents the context for an XPath expression written
18 * in the stylesheet.
19 */

20
21 public class ExpressionContext implements XSLTStaticContext {
22
23     private StyleElement element;
24     private NamePool namePool;
25
26     public ExpressionContext(StyleElement styleElement) {
27         element = styleElement;
28         namePool = styleElement.getTargetNamePool();
29     }
30
31     /**
32      * Get the system configuration
33      */

34
35     public Configuration getConfiguration() {
36         return element.getPreparedStylesheet().getConfiguration();
37     }
38
39     /**
40      * Get the location map
41      */

42
43     public LocationMap getLocationMap() {
44         return element.getPrincipalStylesheet().getLocationMap();
45     }
46
47     /**
48     * Issue a compile-time warning
49     */

50
51     public void issueWarning(String JavaDoc s, SourceLocator JavaDoc locator) {
52         element.issueWarning(s, locator);
53     }
54
55     /**
56     * Get the NamePool used for compiling expressions
57     */

58
59     public NamePool getNamePool() {
60         return namePool;
61     }
62
63     /**
64     * Get the System ID of the entity containing the expression (used for diagnostics)
65     */

66
67     public String JavaDoc getSystemId() {
68         return element.getSystemId();
69     }
70
71     /**
72     * Get the line number of the expression within its containing entity
73     * Returns -1 if no line number is available
74     */

75
76     public int getLineNumber() {
77         return element.getLineNumber();
78     }
79
80     /**
81     * Get the Base URI of the element containing the expression, for resolving any
82     * relative URI's used in the expression.
83     * Used by the document() function.
84     */

85
86     public String JavaDoc getBaseURI() {
87         return element.getBaseURI();
88     }
89
90     /**
91     * Get the URI for a prefix, using this Element as the context for namespace resolution.
92     * The default namespace will not be used when the prefix is empty.
93     * @param prefix The prefix
94     * @throws XPathException if the prefix is not declared
95     */

96
97     public String JavaDoc getURIForPrefix(String JavaDoc prefix) throws XPathException {
98         String JavaDoc uri = element.getURIForPrefix(prefix, false);
99         if (uri == null) {
100             StaticError err = new StaticError("Undeclared namespace prefix " + Err.wrap(prefix));
101             err.setErrorCode("XY0280");
102             throw err;
103         }
104         return uri;
105     }
106
107     /**
108     * Get a copy of the Namespace Context
109     */

110
111     public NamespaceResolver getNamespaceResolver() {
112         return element.makeNamespaceContext();
113     }
114
115     /**
116     * Get a fingerprint for a name, using this as the context for namespace resolution
117     * @param qname The name as written, in the form "[prefix:]localname"
118     * @param useDefault Defines the action when there is no prefix. If true, use
119     * the default namespace URI (as for element names). If false, use no namespace URI
120     * (as for attribute names).
121     * @return -1 if the name is not already present in the name pool
122     */

123
124     public int getFingerprint(String JavaDoc qname, boolean useDefault) throws XPathException {
125
126         String JavaDoc[] parts;
127         try {
128             parts = Name.getQNameParts(qname);
129         } catch (QNameException err) {
130             throw new StaticError(err.getMessage());
131         }
132         String JavaDoc prefix = parts[0];
133         if (prefix.equals("")) {
134             String JavaDoc uri = "";
135
136             if (useDefault) {
137                 uri = getURIForPrefix(prefix);
138             }
139
140             return namePool.getFingerprint(uri, qname);
141
142         } else {
143
144             String JavaDoc uri = getURIForPrefix(prefix);
145             return namePool.getFingerprint(uri, parts[1]);
146         }
147     }
148
149     /**
150     * Bind a variable to an object that can be used to refer to it
151     * @param fingerprint The fingerprint of the variable name
152     * @return a VariableDeclaration object that can be used to identify it in the Bindery
153     * @throws net.sf.saxon.trans.StaticError if the variable has not been declared
154     */

155
156     public VariableDeclaration bindVariable(int fingerprint) throws StaticError {
157         return element.bindVariable(fingerprint);
158     }
159
160     /**
161      * Get the function library containing all the in-scope functions available in this static
162      * context
163      */

164
165     public FunctionLibrary getFunctionLibrary() {
166         return element.getPrincipalStylesheet().getFunctionLibrary();
167     }
168
169     /**
170     * Determine if an extension element is available
171     * @throws XPathException if the name is invalid or the prefix is not declared
172     */

173
174     public boolean isElementAvailable(String JavaDoc qname) throws XPathException {
175         try {
176             String JavaDoc[] parts = Name.getQNameParts(qname);
177             String JavaDoc uri = getURIForPrefix(parts[0]);
178
179             return element.getPreparedStylesheet().
180                                 getStyleNodeFactory().isElementAvailable(uri, parts[1]);
181         } catch (QNameException e) {
182             StaticError err = new StaticError("Invalid element name. " + e.getMessage());
183             err.setErrorCode("XTDE1440");
184             throw err;
185         }
186     }
187
188     /**
189     * Get a named collation.
190     * @param name The name of the required collation. Supply null to get the default collation.
191     * @return the collation; or null if the required collation is not found.
192     */

193
194     public Comparator JavaDoc getCollation(String JavaDoc name) throws XPathException {
195         return element.getPrincipalStylesheet().findCollation(name);
196     }
197
198     /**
199     * Get the default collation. Return null if no default collation has been defined
200     */

201
202     public String JavaDoc getDefaultCollationName() {
203         return element.getDefaultCollationName();
204         //return element.getPrincipalStylesheet().getDefaultCollationName();
205
}
206
207     /**
208     * Get the default XPath namespace, as a namespace code that can be looked up in the NamePool
209     */

210
211     public short getDefaultElementNamespace() {
212         return element.getDefaultXPathNamespace();
213     }
214
215     /**
216      * Get the default function namespace
217      */

218
219     public String JavaDoc getDefaultFunctionNamespace() {
220         return NamespaceConstant.FN;
221     }
222
223     /**
224     * Determine whether Backwards Compatible Mode is used
225     */

226
227     public boolean isInBackwardsCompatibleMode() {
228         return element.backwardsCompatibleModeIsEnabled();
229     }
230
231     /**
232      * Test whether a schema has been imported for a given namespace
233      * @param namespace the target namespace of the required schema
234      * @return true if a schema for this namespace has been imported
235      */

236
237     public boolean isImportedSchema(String JavaDoc namespace) {
238         return getXSLStylesheet().isImportedSchema(namespace);
239     }
240
241     /**
242      * Determine whether a built-in type is available in this context. This method caters for differences
243      * between host languages as to which set of types are built in.
244      *
245      * @param type the supposedly built-in type. This will always be a type in the
246      * XS or XDT namespace.
247      * @return true if this type can be used in this static context
248      */

249
250     public boolean isAllowedBuiltInType(AtomicType type) {
251         if (getConfiguration().isSchemaAware(Configuration.XSLT)) {
252             return true;
253         } else if (type instanceof BuiltInAtomicType) {
254             return ((BuiltInAtomicType)type).isAllowedInBasicXSLT();
255         } else {
256             return false;
257         }
258     }
259
260     /**
261     * Get the XSLStylesheet object
262     */

263
264     public XSLStylesheet getXSLStylesheet() {
265         return element.getPrincipalStylesheet();
266     }
267
268     /**
269      * Get the stylesheet element containing this XPath expression
270      * @return the element in the tree representation of the source stylesheet
271      */

272
273     public StyleElement getStyleElement() {
274         return element;
275     }
276 }
277
278 //
279
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
280
// you may not use this file except in compliance with the License. You may obtain a copy of the
281
// License at http://www.mozilla.org/MPL/
282
//
283
// Software distributed under the License is distributed on an "AS IS" basis,
284
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
285
// See the License for the specific language governing rights and limitations under the License.
286
//
287
// The Original Code is: all this file.
288
//
289
// The Initial Developer of the Original Code is Michael H. Kay.
290
//
291
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
292
//
293
// Contributor(s): none.
294
//
295
Popular Tags