KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > simpletype > SimpleTypeUsage


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package simpletype;
18
19 import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
20 import org.apache.xerces.impl.dv.SchemaDVFactory;
21 import org.apache.xerces.impl.dv.ValidatedInfo;
22 import org.apache.xerces.impl.dv.ValidationContext;
23 import org.apache.xerces.impl.dv.XSFacets;
24 import org.apache.xerces.impl.dv.XSSimpleType;
25 import org.apache.xerces.impl.validation.ValidationState;
26 import org.apache.xerces.xs.XSConstants;
27 import org.apache.xerces.xs.XSObjectList;
28 import org.apache.xerces.xs.XSTypeDefinition;
29
30 /**
31  * It demonstrates how to use the interfaces defined in 'org.apache.xerces.impl.dv'
32  * package for the purpose of
33  * 1. how to query property information of Simple Type Definition Schema Component.
34  * 2. how to get instance of SchemaDVFactory implementation.
35  * 3. how to get built-in type/s and create new types as derived by restriction, list
36  * or union, using factory methods of SchemaDVFactory.
37  * 4. how to use those simple type (built-in/created) to validate the values.
38  * This class is useful for any application which wants to use the simple type
39  * implementation directly as separate module.
40  *
41  * @author Neeraj Bajaj Sun Microsystems, inc.
42  *
43  */

44
45 public class SimpleTypeUsage{
46
47 static SchemaDVFactory factory = null;
48 XSFacets facets = new XSFacets();
49
50 short fPresentFacets ;
51 short fFixedFacets ;
52 short fFinalSet ;
53
54
55 public SimpleTypeUsage(){
56
57         //Any application willing to switch to different implementation should
58
//call SchemaDVFactory#setFactoryClass() as the first step before calling
59
//SchemaDVFactory#getInstance().
60
//Suppose application wants to use class 'MySchemaDVFactoryImpl' as SchemaDVFactory
61
// implementation which resides in say 'myApp.simpleType' package.
62

63         //SchemaDVFactory.setFactoryClass("myApp.simpleType.MySchemaDVFactoryImpl.class");
64

65         //this will give the instance of default implementation (SchemaDVFactoryImpl)
66
// in 'org.apache.xerces.impl.dv.xs_new' package.
67
factory = SchemaDVFactory.getInstance();
68
69 } //SimpleTypeUsage()
70

71
72 /**
73 * Get proper validation context, it provides the information required for the validation of datatypes id, idref,
74 * entity, notation, qname , we need to get appropriate validation context for validating the content or creating
75 * simple type (applyFacets).
76 * @return ValidationContext
77 */

78
79 private ValidationContext getValidationContext(){
80
81         ValidationState validationState = null;
82         
83     // create an instance of 'ValidationState' providing the information required for the
84
// validation of datatypes id, idref, entity, notation, qname.
85
// application can also provide its own implementation of ValidationContext if required,
86
// an implementation of 'ValidationContext' is in 'org.apache.xerces.impl.validation' package.
87
validationState = new ValidationState();
88     
89         // application need to pass validation context while validating string, object or creating simple type (applyFacets)
90
// derived by restriction, should set the following information accordingly
91

92     //application should provide the namespace support by calling
93
//validationState.setNamespaceSupport(...);
94

95     //application can also provide 'SymbolTable' (org.apache.xerces.util.SymbolTable) like
96
//validationState.setSymbolTable(....);
97

98         //set proper value (true/false) for the given validation context
99
//validationState.setFacetChecking(true);
100

101         //set proper value (true/false) for the given validation context
102
//validationState.setExtraChecking(false);
103

104         return validationState;
105         
106 }
107
108 /**
109  * this method shows how to validate the content against the given simple type.
110  *
111  * @param String content to validate
112  * @param XSSimpleType SimpleType Definition schema component against which to validate the content.
113  *
114  * @return ValidatedInfo validatedInfo object.
115  */

116 public ValidatedInfo validateString(String JavaDoc content, XSSimpleType simpleType){
117
118     //create an instance of 'ValidatedInfo' to get back information (like actual value,
119
//normalizedValue etc..)after content is validated.
120
ValidatedInfo validatedInfo = new ValidatedInfo();
121
122         //get proper validation context , this is very important we need to get appropriate validation context while validating content
123
//validation context passed is generally different while validating content and creating simple type (applyFacets)
124
ValidationContext validationState = getValidationContext();
125
126     try{
127         simpleType.validate(content, validationState, validatedInfo);
128     }catch(InvalidDatatypeValueException ex){
129         System.err.println(ex.getMessage());
130     }
131
132     //now 'validatedInfo' object contains information
133

134     // for number types (decimal, double, float, and types derived from them),
135
// Object return is BigDecimal, Double, Float respectively.
136
// for some types (string and derived), they just return the string itself
137
Object JavaDoc value = validatedInfo.actualValue;
138     //so returned Object can be casted to actual java object like..
139
//Boolean booleanDT = (Boolean)value;
140

141     //The normalized value of a string value
142
String JavaDoc normalizedValue = validatedInfo.normalizedValue ;
143
144     //If the type is a union type, then the member type which
145
//actually validated the string value.
146
XSSimpleType memberType = validatedInfo.memberType ;
147
148     return validatedInfo;
149
150 }//validateString()
151

152 /**
153  * this method shows how to query information about the different properties of 'Simple Type'
154  * definiton schema component. It prints the values of properties of 'SimpleType Definition
155  * Schema Component'.
156  *
157  * @param simpleType object of XSSimpleType
158  */

159 public void querySimpleType(XSSimpleType simpleType){
160
161     //getting information about the different properties of 'Simple Type' definition schema component.
162
System.err.println();
163     System.err.println( "Properties information of 'Simple Type' definiton schema component" );
164     System.err.println();
165     // 'name' property
166
if( simpleType.getAnonymous() )
167         System.err.println( "Anonymous Simple Type" );
168     else{
169         System.err.println("'name' \t\t\t\t: " + simpleType.getName() );
170     }
171
172     //'target namespace' property
173
String JavaDoc targetNameSpace = simpleType.getNamespace() ;
174     System.err.println("'target namespace' \t\t: " + targetNameSpace );
175
176     // 'variety' property
177
short variety = simpleType.getVariety();
178     printVariety(variety);
179
180     //'base type definition' property
181
XSTypeDefinition baseType = (XSTypeDefinition)simpleType.getBaseType() ;
182     System.err.println("'base type definition' name \t: " + ( baseType != null ? baseType.getName() : "null" ) );
183     System.err.println("'base type definition' target namespace : " + ( baseType != null ? baseType.getNamespace() : "null" ) );
184
185     //check if base type is simple or complex
186
if(baseType != null && (baseType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) ){
187         //now we can get all the details of base type
188
XSSimpleType simpleTypeDecl = (XSSimpleType)baseType;
189     }
190
191     // 'facets' property
192
// gives bit combination of the constants defined in XSSimpleType interface.
193
short facets = simpleType.getDefinedFacets() ;
194     printFacets(facets);
195
196     //'final' property
197
//the explicit values of this property extension, restriction, list and union prevent further
198
//derivations by extension (to yield a complex type) and restriction (to yield a simple type)
199
//and use in constructing lists and unions respectively.
200
short finalSet = simpleType.getFinal() ;
201     printFinal(finalSet);
202
203     //if variety is 'list'
204
if( variety == XSSimpleType.VARIETY_LIST ){
205         // 'Item Type definition' property of List Simple Type Definition Schema Component.
206
XSSimpleType listDecl = (XSSimpleType)simpleType.getItemType();
207     }else if(variety == XSSimpleType.VARIETY_UNION ){
208         // 'Member Type definitions' property of Union Simple Type Definition Schema Component.
209
XSObjectList memberTypes = simpleType.getMemberTypes();
210     }
211     
212     //fundamental facet information
213

214     //ordered schema component
215
short ordered = simpleType.getOrdered();
216     printOrdered(ordered);
217
218     //bounded schema component
219
boolean bounded = simpleType.getBounded();
220     if(bounded){
221         System.err.println("'bounded' \t\t\t\t: true" );
222     }
223     else{
224         System.err.println("'bounded' \t\t\t\t: false" );
225     }
226     
227     //cardinality schema component
228
boolean isFinite = simpleType.getFinite();
229     printCardinality(isFinite);
230     
231     //numeric schema component
232
boolean numeric = simpleType.getNumeric();
233     if(numeric){
234         System.err.println("'numeric' \t\t\t\t: true" );
235     }
236     else{
237         System.err.println("'numeric' \t\t\t\t: false" );
238     }
239     
240
241
242 }//getInformation()
243

244 void printOrdered (short ordered){
245
246     switch(ordered){
247     
248         case XSSimpleType.ORDERED_FALSE:
249             System.err.println("'ordered' \t\t\t\t: false" );
250             break;
251             
252         case XSSimpleType.ORDERED_PARTIAL:
253             System.err.println("'ordered' \t\t\t\t: partial" );
254             break;
255             
256         case XSSimpleType.ORDERED_TOTAL:
257             System.err.println("'ordered' \t\t\t\t: total" );
258             break;
259             
260     }
261 }//printOrdered()
262

263 void printCardinality (boolean isFinite){
264     
265     if(!isFinite)
266         System.err.println("'cardinality' \t\t\t\t: countably infinite" );
267     else
268         System.err.println("'cardinality' \t\t\t\t: finite" );
269
270 }//printCardinality()
271

272 void printFacets(short facets){
273
274     System.err.println("'facets' present \t\t: " );
275
276     if(( facets & XSSimpleType.FACET_ENUMERATION) != 0){
277         System.err.println("\t\t\t\t ENUMERATION");
278     }
279     if((facets & XSSimpleType.FACET_LENGTH) != 0){
280         System.err.println("\t\t\t\t LENGTH");
281     }
282     if((facets & XSSimpleType.FACET_MINLENGTH) != 0){
283         System.err.println("\t\t\t\t MINLENGTH");
284     }
285     if((facets & XSSimpleType.FACET_MAXLENGTH) != 0){
286         System.err.println("\t\t\t\t MAXLENGTH");
287     }
288     if((facets & XSSimpleType.FACET_PATTERN) != 0){
289         System.err.println("\t\t\t\t PATTERN");
290     }
291     if((facets & XSSimpleType.FACET_WHITESPACE) != 0){
292         System.err.println("\t\t\t\t WHITESPACE");
293     }
294     if((facets & XSSimpleType.FACET_MAXINCLUSIVE) != 0){
295         System.err.println("\t\t\t\t MAXINCLUSIVE");
296     }
297     if((facets & XSSimpleType.FACET_MAXEXCLUSIVE) != 0){
298         System.err.println("\t\t\t\t MAXEXCLUSIVE");
299     }
300     if((facets & XSSimpleType.FACET_MININCLUSIVE) != 0){
301         System.err.println("\t\t\t\t MININCLUSIVE");
302     }
303     if((facets & XSSimpleType.FACET_MINEXCLUSIVE) != 0){
304         System.err.println("\t\t\t\t MINEXCLUSIVE");
305     }
306     if((facets & XSSimpleType.FACET_TOTALDIGITS) != 0){
307         System.err.println("\t\t\t\t TOTALDIGITS");
308     }
309     if((facets & XSSimpleType.FACET_FRACTIONDIGITS) != 0){
310         System.err.println("\t\t\t\t FRACTIONDIGITS");
311     }
312
313 }//printFacets()
314

315 void printFinal(short finalSet){
316
317     System.err.println("'final' values \t\t\t: " );
318
319     if ((finalSet & XSConstants.DERIVATION_EXTENSION ) != 0) {
320         System.err.println("\t\t\t\t Extension");
321     }
322     if ((finalSet & XSConstants.DERIVATION_RESTRICTION) != 0) {
323         System.err.println("\t\t\t\t Restriction");
324     }
325     if ((finalSet & XSConstants.DERIVATION_LIST ) != 0) {
326         System.err.println("\t\t\t\t List");
327     }
328     if ((finalSet & XSConstants.DERIVATION_UNION ) != 0) {
329         System.err.println("\t\t\t\t Union");
330     }
331     if (finalSet == XSConstants.DERIVATION_NONE) {
332         System.err.println("\t\t\t\t EMPTY");
333     }
334
335 }
336
337 void printVariety(short variety){
338
339     switch(variety){
340
341     case XSSimpleType.VARIETY_ATOMIC:
342         System.err.println("'variety' \t\t\t: ATOMIC");
343         break;
344
345     case XSSimpleType.VARIETY_LIST:
346         System.err.println("'variety' \t\t\t: LIST");
347         break;
348
349     case XSSimpleType.VARIETY_UNION:
350         System.err.println("'variety' \t\t\t: UNION");
351         break;
352
353     default:
354         System.err.println("Invalid value of 'Variety' property , it should be one of atomic, list or union.");
355         break;
356     }
357
358
359 } //printVariety()
360

361
362 public static void main(String JavaDoc [] args){
363
364     SimpleTypeUsage usage = new SimpleTypeUsage();
365
366     if(args.length == 1 ){
367         XSSimpleType builtInType = factory.getBuiltInType(args[0]);
368         if(builtInType == null){
369             System.err.println("Invalid built-in Simple datatype given as argument.");
370             printUsage();
371         }
372         else {
373             usage.querySimpleType(builtInType);
374         }
375
376     }else{
377         printUsage();
378     }
379
380 }//main()
381

382 static void printUsage(){
383         System.err.println("USAGE: java simpletype.SimpleTypeUsage 'Built-InDatatypeName' ");
384         System.err.println();
385
386         System.err.println(" Built-InDatatypeName \t\tBuilt-In Datatype name as defined by W3C Schema Spec, \n\t\t\t\t\t \"http://www.w3.org/TR/xmlschema-2/#built-in-datatypes\" .");
387         System.err.println();
388 }
389
390 }//class SimpleTypeUsage
391
Popular Tags