KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.style;
2
3 import net.sf.saxon.Configuration;
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.*;
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 javax.xml.transform.TransformerException JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 /**
18  * This class implements the static context used for evaluating use-when expressions in XSLT 2.0
19  */

20
21 public class UseWhenStaticContext implements XSLTStaticContext {
22
23     public Configuration config;
24     public NamespaceResolver namespaceContext;
25     public FunctionLibrary functionLibrary;
26     public LocationMap locationMap;
27     public StyleNodeFactory nodeFactory;
28     public String JavaDoc baseURI;
29     public short defaultXPathNamespace;
30
31     public UseWhenStaticContext(Configuration config, NamespaceResolver namespaceContext) {
32         this.config = config;
33         this.namespaceContext = namespaceContext;
34         this.locationMap = new LocationMap();
35
36         FunctionLibraryList lib = new FunctionLibraryList();
37         lib.addFunctionLibrary(new SystemFunctionLibrary(SystemFunctionLibrary.USE_WHEN));
38         lib.addFunctionLibrary(getConfiguration().getVendorFunctionLibrary());
39         lib.addFunctionLibrary(new ConstructorFunctionLibrary(getConfiguration()));
40         if (config.isAllowExternalFunctions()) {
41             lib.addFunctionLibrary(config.getExtensionBinder());
42         }
43         functionLibrary = lib;
44     }
45
46     /**
47      * Get the system configuration
48      */

49
50     public Configuration getConfiguration() {
51         return config;
52     }
53
54     /**
55      * Get the location map
56      */

57
58     public LocationMap getLocationMap() {
59         return locationMap;
60     }
61
62     /**
63      * Issue a compile-time warning
64      */

65
66     public void issueWarning(String JavaDoc s, SourceLocator JavaDoc locator) {
67         StaticError err = new StaticError(s);
68         err.setLocator(locator);
69         try {
70             config.getErrorListener().warning(err);
71         } catch (TransformerException JavaDoc e) {
72             // ignore response
73
}
74     }
75
76     /**
77      * Get the System ID of the container of the expression. This is the containing
78      * entity (file) and is therefore useful for diagnostics. Use getBaseURI() to get
79      * the base URI, which may be different.
80      */

81
82     public String JavaDoc getSystemId() {
83         return baseURI;
84     }
85
86     /**
87      * Get the line number of the expression within its containing entity
88      * Returns -1 if no line number is available
89      */

90
91     public int getLineNumber() {
92         return -1;
93     }
94
95     /**
96      * Get the Base URI of the stylesheet element, for resolving any relative URI's used
97      * in the expression.
98      * Used by the document(), doc(), resolve-uri(), and base-uri() functions.
99      * May return null if the base URI is not known.
100      */

101
102     public String JavaDoc getBaseURI() {
103         return baseURI;
104     }
105
106     /**
107      * Get the URI for a namespace prefix. The default namespace is NOT used
108      * when the prefix is empty.
109      *
110      * @param prefix The prefix
111      * @throws net.sf.saxon.trans.XPathException
112      * if the prefix is not declared
113      */

114
115     public String JavaDoc getURIForPrefix(String JavaDoc prefix) throws XPathException {
116         return namespaceContext.getURIForPrefix(prefix, false);
117     }
118
119     /**
120      * Get the NamePool used for compiling expressions
121      */

122
123     public NamePool getNamePool() {
124         return getConfiguration().getNamePool();
125     }
126
127     /**
128      * Bind a variable used in this element to the XSLVariable element in which it is declared
129      */

130
131     public VariableDeclaration bindVariable(int fingerprint) throws StaticError {
132         StaticError err = new StaticError("Variables cannot be used in a use-when expression");
133         err.setErrorCode("XO0008");
134         throw err;
135     }
136
137     /**
138      * Get the function library containing all the in-scope functions available in this static
139      * context
140      */

141
142     public FunctionLibrary getFunctionLibrary() {
143         return functionLibrary;
144     }
145
146     /**
147      * Get a named collation.
148      *
149      * @param name The name of the required collation. Supply null to get the default collation.
150      * @return the collation; or null if the required collation is not found.
151      */

152
153     public Comparator JavaDoc getCollation(String JavaDoc name) throws XPathException {
154         return null;
155     }
156
157     /**
158      * Get the name of the default collation.
159      *
160      * @return the name of the default collation; or the name of the codepoint collation
161      * if no default collation has been defined
162      */

163
164     public String JavaDoc getDefaultCollationName() {
165         return NamespaceConstant.CODEPOINT_COLLATION_URI;
166     }
167
168     /**
169      * Get the default XPath namespace, as a namespace code that can be looked up in the NamePool
170      */

171
172     public short getDefaultElementNamespace() {
173         return defaultXPathNamespace;
174     }
175
176     /**
177      * Get the default function namespace
178      */

179
180     public String JavaDoc getDefaultFunctionNamespace() {
181         return NamespaceConstant.FN;
182     }
183
184     /**
185      * Determine whether Backwards Compatible Mode is used
186      */

187
188     public boolean isInBackwardsCompatibleMode() {
189         return false;
190     }
191
192     /**
193      * Determine whether a Schema for a given target namespace has been imported. Note that the
194      * in-scope element declarations, attribute declarations and schema types are the types registered
195      * with the (schema-aware) configuration, provided that their namespace URI is registered
196      * in the static context as being an imported schema namespace. (A consequence of this is that
197      * within a Configuration, there can only be one schema for any given namespace, including the
198      * null namespace).
199      */

200
201     public boolean isImportedSchema(String JavaDoc namespace) {
202         return false;
203     }
204
205     /**
206      * Determine whether a built-in type is available in this context. This method caters for differences
207      * between host languages as to which set of types are built in.
208      *
209      * @param type the supposedly built-in type. This will always be a type in the
210      * XS or XDT namespace.
211      * @return true if this type can be used in this static context
212      */

213
214     public boolean isAllowedBuiltInType(AtomicType type) {
215         if (getConfiguration().isSchemaAware(Configuration.XSLT)) {
216             return true;
217         } else if (type instanceof BuiltInAtomicType) {
218             return ((BuiltInAtomicType)type).isAllowedInBasicXSLT();
219         } else {
220             return false;
221         }
222     }
223
224     /**
225      * Get a namespace resolver to resolve the namespaces declared in this static context.
226      *
227      * @return a namespace resolver.
228      */

229
230     public NamespaceResolver getNamespaceResolver() {
231         return namespaceContext;
232     }
233
234     /**
235     * Determine if an extension element is available
236     * @throws net.sf.saxon.trans.XPathException if the name is invalid or the prefix is not declared
237     */

238
239     public boolean isElementAvailable(String JavaDoc qname) throws XPathException {
240         try {
241             String JavaDoc[] parts = Name.getQNameParts(qname);
242             String JavaDoc uri = getURIForPrefix(parts[0]);
243             if (nodeFactory == null) {
244                 nodeFactory = new StyleNodeFactory(config);
245             }
246             return nodeFactory.isElementAvailable(uri, parts[1]);
247         } catch (QNameException e) {
248             StaticError err = new StaticError("Invalid element name. " + e.getMessage());
249             err.setErrorCode("XTDE1440");
250             throw err;
251         }
252     }
253
254     /**
255      * Set the base URI
256      */

257
258     public void setBaseURI(String JavaDoc uri) {
259         baseURI = uri;
260     }
261
262     /**
263      * Set the default namespace for elements and types
264      */

265
266     public void setDefaultElementNamespace(short code) {
267         defaultXPathNamespace = code;
268     }
269
270 }
271
Popular Tags