KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > type > BuiltInListType


1 package net.sf.saxon.type;
2
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.functions.NormalizeSpace;
5 import net.sf.saxon.om.*;
6 import net.sf.saxon.style.StandardNames;
7 import net.sf.saxon.trans.DynamicError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.value.StringValue;
10 import net.sf.saxon.value.Whitespace;
11 import net.sf.saxon.value.Value;
12 import net.sf.saxon.value.SequenceExtent;
13 import net.sf.saxon.ConversionContext;
14
15 import java.io.Serializable JavaDoc;
16
17 /**
18  * <p>This class is used to implement the built-in
19  * list types NMTOKENS, ENTITIES, IDREFS. In the schema-aware product it is also
20  * used to support user-defined list types.</p>
21  *
22 **/

23
24 public class BuiltInListType implements ListType, Serializable JavaDoc {
25
26     private int fingerprint;
27
28     /**
29      * Determine how values of this simple type are whitespace-normalized.
30      *
31      * @return one of {@link net.sf.saxon.value.Whitespace#PRESERVE}, {@link net.sf.saxon.value.Whitespace#COLLAPSE},
32      * {@link net.sf.saxon.value.Whitespace#REPLACE}.
33      */

34
35     public int getWhitespaceAction() {
36         return Whitespace.COLLAPSE;
37     }
38
39     /**
40      * The SimpleType of the items in the list.
41      */

42
43     private BuiltInAtomicType itemType = null;
44
45     /**
46      * Get the most specific possible atomic type that all items in this SimpleType belong to
47      *
48      * @return the lowest common supertype of all member types
49      */

50
51     public AtomicType getCommonAtomicType() {
52         return itemType;
53     }
54
55     /**
56      * Create a new ListType.
57      */

58
59     public BuiltInListType(int fingerprint) {
60         this.fingerprint = fingerprint;
61         switch (fingerprint) {
62             case StandardNames.XS_ENTITIES:
63                 itemType = (BuiltInAtomicType)BuiltInSchemaFactory.getSchemaType(StandardNames.XS_ENTITY);
64                 break;
65             case StandardNames.XS_IDREFS:
66                 itemType = (BuiltInAtomicType)BuiltInSchemaFactory.getSchemaType(StandardNames.XS_IDREF);
67                 break;
68             case StandardNames.XS_NMTOKENS:
69                 itemType = (BuiltInAtomicType)BuiltInSchemaFactory.getSchemaType(StandardNames.XS_NMTOKEN);
70                 break;
71         }
72     }
73
74     /**
75      * Get the validation status - always valid
76      */

77     public int getValidationStatus() {
78         return VALIDATED;
79     }
80
81    /**
82      * Returns the base type that this type inherits from.
83      * If this type is a Simpletype that is a built in primitive type then null is returned.
84      * @return the base type.
85     */

86
87     public SchemaType getBaseType() {
88         return AnySimpleType.getInstance();
89     }
90
91     /**
92      * Test whether this Simple Type is an atomic type
93      * @return false, this is not an atomic type
94      */

95
96     public boolean isAtomicType() {
97         return false;
98     }
99     
100
101    /**
102      * Returns true if this type is derived by list, or if it is derived by restriction
103      * from a list type, or if it is a union that contains a list as one of its members
104      */

105
106     public boolean isListType() {
107         return true;
108     }
109
110     public boolean isUnionType() {
111         return false;
112     }
113
114     public SchemaType getBuiltInBaseType() throws ValidationException {
115         return this;
116     }
117
118     public boolean isNamespaceSensitive() {
119         return false;
120     }
121
122     /**
123      * Get the fingerprint of the name of this type
124      * @return the fingerprint. Returns an invented fingerprint for an anonymous type.
125      */

126
127     public int getFingerprint() {
128         return fingerprint;
129     }
130
131     /**
132      * Get the namecode of the name of this type. Because built-in types don't depend on the namePool,
133      * this actually returns the fingerprint, which contains no information about the namespace prefix
134      */

135
136     public int getNameCode() {
137         return fingerprint;
138     }
139
140     /**
141      * Get the display name of the type: that is, a lexical QName with an arbitrary prefix
142      *
143      * @return a lexical QName identifying the type
144      */

145
146     public String JavaDoc getDisplayName() {
147         return StandardNames.getDisplayName(fingerprint);
148     }
149
150     /**
151      * Test whether this SchemaType is a complex type
152      *
153      * @return true if this SchemaType is a complex type
154      */

155
156     public boolean isComplexType() {
157         return false;
158     }
159
160     /**
161      * Test whether this SchemaType is a simple type
162      * @return true if this SchemaType is a simple type
163      */

164
165     public boolean isSimpleType() {
166         return true;
167     }
168
169     /**
170      * Returns the value of the 'block' attribute for this type, as a bit-signnificant
171      * integer with fields such as {@link SchemaType#DERIVATION_LIST} and {@link SchemaType#DERIVATION_EXTENSION}
172      *
173      * @return the value of the 'block' attribute for this type
174      */

175
176     public int getBlock() {
177         return 0; //To change body of implemented methods use File | Settings | File Templates.
178
}
179
180     /**
181      * Returns the base type that this type inherits from. This method can be used to get the
182      * base type of a type that is known to be valid.
183      * If this type is a Simpletype that is a built in primitive type then null is returned.
184      *
185      * @return the base type.
186      * @throws IllegalStateException if this type is not valid.
187      */

188
189     public SchemaType getKnownBaseType() throws IllegalStateException JavaDoc {
190         return AnySimpleType.getInstance();
191     }
192
193     /**
194      * Gets the integer code of the derivation method used to derive this type from its
195      * parent. Returns zero for primitive types.
196      *
197      * @return a numeric code representing the derivation method, for example {@link SchemaType#DERIVATION_RESTRICTION}
198      */

199
200     public int getDerivationMethod() {
201         return SchemaType.DERIVATION_LIST;
202     }
203
204     /**
205      * Determines whether derivation (of a particular kind)
206      * from this type is allowed, based on the "final" property
207      *
208      * @param derivation the kind of derivation, for example {@link SchemaType#DERIVATION_LIST}
209      * @return true if this kind of derivation is allowed
210      */

211
212     public boolean allowsDerivation(int derivation) {
213         return true;
214     }
215
216     /**
217      * Get the typed value of a node that is annotated with this schema type. This method must be called
218      * only for a valid type.
219      *
220      * @param node the node whose typed value is required
221      * @return a SequenceIterator over the atomic values making up the typed value of the specified
222      * node. The objects returned by this iterator are of type {@link net.sf.saxon.value.AtomicValue}
223      */

224
225     public SequenceIterator getTypedValue(NodeInfo node) throws XPathException {
226         try {
227             return getTypedValue(node.getStringValue(), new InscopeNamespaceResolver(node), node.getConfiguration());
228         } catch (ValidationException err) {
229             throw new DynamicError("Internal error: value doesn't match its type annotation. " + err.getMessage());
230         }
231     }
232
233     /**
234      * Get the typed value of a node that is annotated with this schema type. The result of this method will always be consistent with the method
235      * {@link #getTypedValue}. However, this method is often more convenient and may be
236      * more efficient, especially in the common case where the value is expected to be a singleton.
237      *
238      * @param node the node whose typed value is required
239      * @return the typed value.
240      * @since 8.5
241      */

242
243     public Value atomize(NodeInfo node) throws XPathException {
244         return new SequenceExtent(getTypedValue(node)).simplify();
245     }
246
247     /**
248      * Test whether this is the same type as another type. They are considered to be the same type
249      * if they are derived from the same type definition in the original XML representation (which
250      * can happen when there are multiple includes of the same file)
251      */

252
253     public boolean isSameType(SchemaType other) {
254         return other.getFingerprint() == this.getFingerprint();
255     }
256
257     public String JavaDoc getDescription() {
258         return getDisplayName();
259     }
260
261     /**
262      * Check that this type is validly derived from a given type
263      *
264      * @param type the type from which this type is derived
265      * @param block the derivations that are blocked by the relevant element declaration
266      * @throws SchemaException
267      * if the derivation is not allowed
268      */

269
270     public void isTypeDerivationOK(SchemaType type, int block) throws SchemaException, ValidationException {
271         //To change body of implemented methods use File | Settings | File Templates.
272
}
273
274     /**
275      * Get the local name of this type
276      * @return the local part of the name, or null if the type is anonymous
277      */

278
279     public String JavaDoc getLocalName() {
280        return getDisplayName().substring(3);
281     }
282
283     /**
284      * Returns the simpleType of the items in this ListType.
285      * @return the simpleType of the items in this ListType.
286     */

287
288     public SimpleType getItemType() {
289         return itemType;
290     }
291
292     /**
293      * Apply the whitespace normalization rules for this simple type
294      * @param value the string before whitespace normalization
295      * @return the string after whitespace normalization
296      */

297
298     public String JavaDoc applyWhitespaceNormalization(String JavaDoc value) {
299         return NormalizeSpace.normalize(value).toString();
300     }
301
302     /**
303      * Analyze an expression to see whether the expression is capable of delivering a value of this
304      * type.
305      *
306      * @param expression the expression that delivers the content
307      * @param kind the node kind whose content is being delivered: {@link Type#ELEMENT},
308      * {@link Type#ATTRIBUTE}, or {@link Type#DOCUMENT}
309      * @param env
310      * @throws net.sf.saxon.trans.XPathException
311      * if the expression will never deliver a value of the correct type
312      */

313
314     public void analyzeContentExpression(Expression expression, int kind, StaticContext env) throws XPathException {
315         BuiltInAtomicType.analyzeContentExpression(this, expression, env, kind);
316     }
317
318     /**
319      * Check whether a given input string is valid according to this SimpleType
320      * @param value the input string to be checked
321      * @param nsResolver a namespace resolver used to resolve namespace prefixes if the type
322      * is namespace sensitive. The value supplied may be null; in this case any namespace-sensitive
323      * content will throw an UnsupportedOperationException.
324      * @param conversion
325      * @throws UnsupportedOperationException if the type is namespace-sensitive and no namespace
326      * resolver is supplied
327      */

328
329     public ValidationException validateContent(CharSequence JavaDoc value, NamespaceResolver nsResolver, ConversionContext conversion) {
330         SimpleType base = getItemType();
331         SequenceIterator iter = new StringTokenIterator(value.toString());
332         ValidationException result = null;
333         try {
334             while (true) {
335                 StringValue val = (StringValue)iter.next();
336                 if (val == null) break;
337                 ValidationException v = base.validateContent(val.getStringValue(), nsResolver, conversion);
338                 if (v != null) {
339                     return v;
340                 }
341             }
342         } catch (ValidationException err) {
343             result = err;
344         } catch (XPathException err) {
345             result = new ValidationException(err);
346         }
347         return result;
348     }
349
350     /**
351      * Get the typed value of a given input string. This method assumes that the input value
352      * is valid according to this SimpleType
353      * @param value the string whose typed value is required
354      * @param resolver
355      * @param conversion
356      */

357
358     public SequenceIterator getTypedValue(CharSequence JavaDoc value, NamespaceResolver resolver, ConversionContext conversion) throws ValidationException {
359         SequenceIterator iter = new StringTokenIterator(value.toString());
360         ListTypeMappingFunction map = new ListTypeMappingFunction();
361         map.resolver = resolver;
362         map.atomicType = (AtomicType)getItemType();
363         return new MappingIterator(iter, map, null);
364     }
365
366     private static class ListTypeMappingFunction implements MappingFunction {
367
368         public NamespaceResolver resolver;
369         public AtomicType atomicType;
370
371         /**
372          * The typed value of a list-valued node is obtained by tokenizing the string value and
373          * applying a mapping function to the sequence of tokens.
374          * This method implements the mapping function. It is for internal use only.
375          * For details see {@link net.sf.saxon.expr.MappingFunction}
376         */

377
378         public Object JavaDoc map(Item item, XPathContext context) throws XPathException {
379             try {
380                 return atomicType.getTypedValue(item.getStringValue(), resolver, context);
381             } catch (ValidationException err) {
382                 return new DynamicError(err);
383             }
384         }
385     }
386
387 }
388
389 //
390
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
391
// you may not use this file except in compliance with the License. You may obtain a copy of the
392
// License at http://www.mozilla.org/MPL/
393
//
394
// Software distributed under the License is distributed on an "AS IS" basis,
395
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
396
// See the License for the specific language governing rights and limitations under the License.
397
//
398
// The Original Code is: all this file.
399
//
400
// The Initial Developer of the Original Code is Saxonica Limited.
401
//
402
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
403
//
404
// Contributor(s): none.
405
//
406

407
408
Popular Tags