File: SimpleTypeUsage.java

package info (click to toggle)
libxerces2-java 2.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 12,608 kB
  • sloc: java: 127,779; xml: 13,596; sh: 39; makefile: 10
file content (391 lines) | stat: -rw-r--r-- 14,476 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package simpletype;

import org.apache.xerces.impl.dv.InvalidDatatypeValueException;
import org.apache.xerces.impl.dv.SchemaDVFactory;
import org.apache.xerces.impl.dv.ValidatedInfo;
import org.apache.xerces.impl.dv.ValidationContext;
import org.apache.xerces.impl.dv.XSFacets;
import org.apache.xerces.impl.dv.XSSimpleType;
import org.apache.xerces.impl.validation.ValidationState;
import org.apache.xerces.xs.XSConstants;
import org.apache.xerces.xs.XSObjectList;
import org.apache.xerces.xs.XSTypeDefinition;

/**
 *  It demonstrates how to use the interfaces defined in 'org.apache.xerces.impl.dv'
 *  package for the purpose of
 * 1. how to query property information of Simple Type Definition Schema Component.
 * 2. how to get instance of SchemaDVFactory implementation.
 * 3. how to get built-in type/s and create new types as derived by restriction, list
 *      or union, using factory methods of SchemaDVFactory.
 * 4. how to use those simple type (built-in/created) to validate the values.
 *    This class is  useful for any application which wants to use the simple type
 * implementation directly as separate module.
 *
 * @author Neeraj Bajaj     Sun Microsystems, inc.
 * @version $Id: SimpleTypeUsage.java 950355 2010-06-02 03:43:24Z mrglavas $
 */

public class SimpleTypeUsage{

static SchemaDVFactory factory = null;
XSFacets facets = new XSFacets();

short fPresentFacets ;
short fFixedFacets ;
short fFinalSet ;


public SimpleTypeUsage(){

        //Any application willing to switch to different implementation should
        //call SchemaDVFactory#setFactoryClass() as the first step before calling
        //SchemaDVFactory#getInstance().
        //Suppose application wants to use class 'MySchemaDVFactoryImpl' as SchemaDVFactory
        // implementation which resides in say 'myApp.simpleType' package.
    
        //SchemaDVFactory.setFactoryClass("myApp.simpleType.MySchemaDVFactoryImpl.class");

        //this will give the instance of default implementation (SchemaDVFactoryImpl)
        // in 'org.apache.xerces.impl.dv.xs_new' package.
        factory = SchemaDVFactory.getInstance();

} //SimpleTypeUsage()


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

private ValidationContext getValidationContext(){

        ValidationState validationState = null;
        
    // create an instance of 'ValidationState' providing the information required for the
    // validation of datatypes id, idref, entity, notation, qname.
    //	application can also provide its own implementation of ValidationContext if required, 
    // an implementation of 'ValidationContext' is in 'org.apache.xerces.impl.validation' package.
    validationState = new ValidationState();
    
        // application need to pass validation context while validating string, object or creating simple type (applyFacets)
        // derived by restriction, should set the following information accordingly 
            
    //application should provide the namespace support by calling
    //validationState.setNamespaceSupport(...);

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

        //set proper value (true/false) for the given validation context 
        //validationState.setFacetChecking(true);
        
        //set proper value (true/false) for the given validation context	
        //validationState.setExtraChecking(false);	
                
        return validationState;
        
}

/**
 * this method shows how to validate the content against the given simple type.
 *
 * @param String content to validate
 * @param XSSimpleType SimpleType Definition schema component against which to validate the content.
 *
 * @return ValidatedInfo validatedInfo object.
 */
public ValidatedInfo validateString(String content, XSSimpleType simpleType){

    //create an instance of 'ValidatedInfo' to get back information (like actual value,
    //normalizedValue etc..)after content is validated.
    ValidatedInfo validatedInfo = new ValidatedInfo();

        //get proper validation context , this is very important we need to get appropriate validation context while validating content
        //validation context passed is generally different while validating content and  creating simple type (applyFacets)
        ValidationContext validationState = getValidationContext();

    try{
        simpleType.validate(content, validationState, validatedInfo);
    }catch(InvalidDatatypeValueException ex){
        System.err.println(ex.getMessage());
    }

    //now 'validatedInfo' object contains information

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

    //The normalized value of a string value
    String normalizedValue = validatedInfo.normalizedValue ;

    //If the type is a union type, then the member type which
    //actually validated the string value.
    XSSimpleType memberType = validatedInfo.memberType ;

    return validatedInfo;

}//validateString()

/**
 * this method shows how to query information about the different properties of 'Simple Type'
 * definiton schema component. It prints the values of properties of 'SimpleType Definition
 * Schema Component'.
 *
 * @param simpleType    object of XSSimpleType
 */
public void querySimpleType(XSSimpleType simpleType){

    //getting information about the different properties of 'Simple Type' definition schema component.
    System.err.println();
    System.err.println( "Properties information of 'Simple Type' definiton schema component" );
    System.err.println();
    // 'name' property
    if( simpleType.getAnonymous() )
        System.err.println( "Anonymous Simple Type" );
    else{
        System.err.println("'name' \t\t\t\t: " + simpleType.getName()  );
    }

    //'target namespace' property
    String targetNameSpace = simpleType.getNamespace() ;
    System.err.println("'target namespace' \t\t: " + targetNameSpace  );

    // 'variety' property
    short variety = simpleType.getVariety();
    printVariety(variety);

    //'base type definition' property
    XSTypeDefinition baseType = (XSTypeDefinition)simpleType.getBaseType() ;
    System.err.println("'base type definition' name \t: " + ( baseType != null ? baseType.getName() : "null" )   );
    System.err.println("'base type definition' target namespace : " + ( baseType != null ? baseType.getNamespace() : "null" )   );

    //check if base type is simple or complex
    if(baseType != null && (baseType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) ){
        //now we can get all the details of base type
        XSSimpleType simpleTypeDecl = (XSSimpleType)baseType;
    }

    // 'facets' property
    // gives bit combination of the constants defined in XSSimpleType interface.
    short facets = simpleType.getDefinedFacets() ;
    printFacets(facets);

    //'final' property
    //the explicit values of this property extension, restriction, list and union prevent further
    //derivations by extension (to yield a complex type) and restriction (to yield a simple type)
    //and use in constructing lists and unions respectively.
    short finalSet = simpleType.getFinal() ;
    printFinal(finalSet);

    //if variety is 'list'
    if( variety == XSSimpleType.VARIETY_LIST ){
        // 'Item Type definition' property of List Simple Type Definition Schema Component.
        XSSimpleType listDecl = (XSSimpleType)simpleType.getItemType();
    }else if(variety == XSSimpleType.VARIETY_UNION ){
        // 'Member Type definitions' property of Union Simple Type Definition Schema Component.
        XSObjectList memberTypes = simpleType.getMemberTypes();
    }
    
    //fundamental facet information
    
    //ordered schema component
    short ordered = simpleType.getOrdered();
    printOrdered(ordered);

    //bounded schema component
    boolean bounded = simpleType.getBounded();    
    if(bounded){
        System.err.println("'bounded' \t\t\t\t: true"  );
    }
    else{
        System.err.println("'bounded' \t\t\t\t: false"  );
    }
    
    //cardinality schema component
    boolean isFinite = simpleType.getFinite();
    printCardinality(isFinite);
    
    //numeric schema component
    boolean numeric = simpleType.getNumeric();
    if(numeric){
        System.err.println("'numeric' \t\t\t\t: true"  );
    }
    else{
        System.err.println("'numeric' \t\t\t\t: false"  );
    }
    


}//getInformation()

void printOrdered (short ordered){

    switch(ordered){
    
        case XSSimpleType.ORDERED_FALSE:
            System.err.println("'ordered' \t\t\t\t: false"  );
            break;
            
        case XSSimpleType.ORDERED_PARTIAL:
            System.err.println("'ordered' \t\t\t\t: partial"  );
            break;
            
        case XSSimpleType.ORDERED_TOTAL:
            System.err.println("'ordered' \t\t\t\t: total"  );
            break;
            
    }
}//printOrdered()

void printCardinality (boolean isFinite){
    
    if(!isFinite)
        System.err.println("'cardinality' \t\t\t\t: countably infinite"  );
    else
        System.err.println("'cardinality' \t\t\t\t: finite"  );

}//printCardinality()

void printFacets(short facets){

    System.err.println("'facets' present \t\t: " );

    if(( facets & XSSimpleType.FACET_ENUMERATION) != 0){
        System.err.println("\t\t\t\t ENUMERATION");
    }
    if((facets & XSSimpleType.FACET_LENGTH) != 0){
        System.err.println("\t\t\t\t LENGTH");
    }
    if((facets & XSSimpleType.FACET_MINLENGTH) != 0){
        System.err.println("\t\t\t\t MINLENGTH");
    }
    if((facets & XSSimpleType.FACET_MAXLENGTH) != 0){
        System.err.println("\t\t\t\t MAXLENGTH");
    }
    if((facets & XSSimpleType.FACET_PATTERN) != 0){
        System.err.println("\t\t\t\t PATTERN");
    }
    if((facets & XSSimpleType.FACET_WHITESPACE) != 0){
        System.err.println("\t\t\t\t WHITESPACE");
    }
    if((facets & XSSimpleType.FACET_MAXINCLUSIVE) != 0){
        System.err.println("\t\t\t\t MAXINCLUSIVE");
    }
    if((facets & XSSimpleType.FACET_MAXEXCLUSIVE) != 0){
        System.err.println("\t\t\t\t MAXEXCLUSIVE");
    }
    if((facets & XSSimpleType.FACET_MININCLUSIVE) != 0){
        System.err.println("\t\t\t\t MININCLUSIVE");
    }
    if((facets & XSSimpleType.FACET_MINEXCLUSIVE) != 0){
        System.err.println("\t\t\t\t MINEXCLUSIVE");
    }
    if((facets & XSSimpleType.FACET_TOTALDIGITS) != 0){
        System.err.println("\t\t\t\t TOTALDIGITS");
    }
    if((facets & XSSimpleType.FACET_FRACTIONDIGITS) != 0){
        System.err.println("\t\t\t\t FRACTIONDIGITS");
    }

}//printFacets()

void printFinal(short finalSet){

    System.err.println("'final' values \t\t\t: " );

    if ((finalSet & XSConstants.DERIVATION_EXTENSION ) != 0) {
        System.err.println("\t\t\t\t Extension");
    }
    if ((finalSet & XSConstants.DERIVATION_RESTRICTION) != 0) {
        System.err.println("\t\t\t\t Restriction");
    }
    if ((finalSet & XSConstants.DERIVATION_LIST ) != 0) {
        System.err.println("\t\t\t\t List");
    }
    if ((finalSet & XSConstants.DERIVATION_UNION ) != 0) {
        System.err.println("\t\t\t\t Union");
    }
    if (finalSet == XSConstants.DERIVATION_NONE) {
        System.err.println("\t\t\t\t EMPTY");
    }

}

void printVariety(short variety){

    switch(variety){

    case XSSimpleType.VARIETY_ATOMIC:
        System.err.println("'variety' \t\t\t: ATOMIC");
        break;

    case XSSimpleType.VARIETY_LIST:
        System.err.println("'variety' \t\t\t: LIST");
        break;

    case XSSimpleType.VARIETY_UNION:
        System.err.println("'variety' \t\t\t: UNION");
        break;

    default:
        System.err.println("Invalid value of 'Variety' property , it should be one of atomic, list or union.");
        break;
    }


} //printVariety()


public static void main(String [] args){

    SimpleTypeUsage usage = new SimpleTypeUsage();

    if(args.length == 1 ){         
        XSSimpleType builtInType = factory.getBuiltInType(args[0]);
        if(builtInType == null){
            System.err.println("Invalid built-in Simple datatype given as argument.");
            printUsage();
        }
        else {
            usage.querySimpleType(builtInType);
        }

    }else{
        printUsage();
    }

}//main()

static void printUsage(){
        System.err.println("USAGE: java simpletype.SimpleTypeUsage  'Built-InDatatypeName' ");
        System.err.println();

        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\" .");        
        System.err.println();
}

}//class SimpleTypeUsage