1 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 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 63 65 factory = SchemaDVFactory.getInstance(); 68 69 } 71 72 78 79 private ValidationContext getValidationContext(){ 80 81 ValidationState validationState = null; 82 83 validationState = new ValidationState(); 88 89 92 95 98 101 104 return validationState; 105 106 } 107 108 116 public ValidatedInfo validateString(String content, XSSimpleType simpleType){ 117 118 ValidatedInfo validatedInfo = new ValidatedInfo(); 121 122 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 134 Object value = validatedInfo.actualValue; 138 141 String normalizedValue = validatedInfo.normalizedValue ; 143 144 XSSimpleType memberType = validatedInfo.memberType ; 147 148 return validatedInfo; 149 150 } 152 159 public void querySimpleType(XSSimpleType simpleType){ 160 161 System.err.println(); 163 System.err.println( "Properties information of 'Simple Type' definiton schema component" ); 164 System.err.println(); 165 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 String targetNameSpace = simpleType.getNamespace() ; 174 System.err.println("'target namespace' \t\t: " + targetNameSpace ); 175 176 short variety = simpleType.getVariety(); 178 printVariety(variety); 179 180 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 if(baseType != null && (baseType.getTypeCategory() == XSTypeDefinition.SIMPLE_TYPE) ){ 187 XSSimpleType simpleTypeDecl = (XSSimpleType)baseType; 189 } 190 191 short facets = simpleType.getDefinedFacets() ; 194 printFacets(facets); 195 196 short finalSet = simpleType.getFinal() ; 201 printFinal(finalSet); 202 203 if( variety == XSSimpleType.VARIETY_LIST ){ 205 XSSimpleType listDecl = (XSSimpleType)simpleType.getItemType(); 207 }else if(variety == XSSimpleType.VARIETY_UNION ){ 208 XSObjectList memberTypes = simpleType.getMemberTypes(); 210 } 211 212 214 short ordered = simpleType.getOrdered(); 216 printOrdered(ordered); 217 218 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 boolean isFinite = simpleType.getFinite(); 229 printCardinality(isFinite); 230 231 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 } 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 } 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 } 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 } 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 } 361 362 public static void main(String [] 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 } 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 } | Popular Tags |