1 10 11 package com.hp.hpl.jena.datatypes.xsd; 12 13 import java.math.BigDecimal ; 14 import java.math.BigInteger ; 15 import java.io.Reader ; 16 import java.util.*; 17 18 import com.hp.hpl.jena.datatypes.*; 19 import com.hp.hpl.jena.datatypes.xsd.impl.*; 20 import com.hp.hpl.jena.graph.impl.LiteralLabel; 21 22 import org.apache.xerces.impl.dv.util.Base64; 23 import org.apache.xerces.impl.dv.util.HexBin; 24 import org.apache.xerces.impl.dv.xs.DecimalDV; 25 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl; 26 import org.apache.xerces.impl.dv.*; 27 import org.apache.xerces.impl.validation.ValidationState; 28 import org.apache.xerces.util.SymbolHash; 29 30 import org.apache.xerces.parsers.XMLGrammarPreparser; 31 import org.apache.xerces.xni.grammars.XMLGrammarDescription; 32 import org.apache.xerces.xni.parser.XMLInputSource; 33 import org.apache.xerces.xs.XSConstants; 34 import org.apache.xerces.xs.XSTypeDefinition; 35 import org.apache.xerces.xs.XSNamedMap; 36 import org.apache.xerces.xni.grammars.XSGrammar; 37 38 45 public class XSDDatatype extends BaseDatatype { 46 47 50 51 public static final String XSD = "http://www.w3.org/2001/XMLSchema"; 52 53 54 public static final XSDDatatype XSDfloat = new XSDFloat("float", Float .class); 55 56 57 public static final XSDDatatype XSDdouble = new XSDDouble("double", Double .class); 58 59 60 public static final XSDDatatype XSDint = new XSDBaseNumericType("int", Integer .class); 61 62 63 public static final XSDDatatype XSDlong = new XSDBaseNumericType("long", Long .class); 64 65 66 public static final XSDDatatype XSDshort = new XSDBaseNumericType("short", Short .class); 67 68 69 public static final XSDDatatype XSDbyte = new XSDByteType("byte", Byte .class); 70 71 72 public static final XSDDatatype XSDunsignedByte = new XSDBaseNumericType("unsignedByte"); 73 74 75 public static final XSDDatatype XSDunsignedShort = new XSDBaseNumericType("unsignedShort"); 76 77 78 public static final XSDDatatype XSDunsignedInt = new XSDBaseNumericType("unsignedInt"); 79 80 81 public static final XSDDatatype XSDunsignedLong = new XSDBaseNumericType("unsignedLong"); 82 83 84 public static final XSDDatatype XSDdecimal = new XSDBaseNumericType("decimal", BigDecimal .class); 85 86 87 public static final XSDDatatype XSDinteger = new XSDBaseNumericType("integer", BigInteger .class); 88 89 90 public static final XSDDatatype XSDnonPositiveInteger = new XSDBaseNumericType("nonPositiveInteger"); 91 92 93 public static final XSDDatatype XSDnonNegativeInteger = new XSDBaseNumericType("nonNegativeInteger"); 94 95 96 public static final XSDDatatype XSDpositiveInteger = new XSDBaseNumericType("positiveInteger"); 97 98 99 public static final XSDDatatype XSDnegativeInteger = new XSDBaseNumericType("negativeInteger"); 100 101 102 public static final XSDDatatype XSDboolean = new XSDDatatype("boolean", Boolean .class); 103 104 105 public static final XSDDatatype XSDstring = new XSDBaseStringType("string", String .class); 106 107 108 public static final XSDDatatype XSDnormalizedString = new XSDBaseStringType("normalizedString", String .class); 109 110 111 public static final XSDDatatype XSDanyURI = new XSDDatatype("anyURI"); 112 113 114 public static final XSDDatatype XSDtoken = new XSDBaseStringType("token"); 115 116 117 public static final XSDDatatype XSDName = new XSDBaseStringType("Name"); 118 119 120 public static final XSDDatatype XSDQName = new XSDDatatype("QName"); 121 122 123 public static final XSDDatatype XSDlanguage = new XSDBaseStringType("language"); 124 125 126 public static final XSDDatatype XSDNMTOKEN = new XSDBaseStringType("NMTOKEN"); 127 128 129 public static final XSDDatatype XSDENTITY = new XSDBaseStringType("ENTITY"); 130 131 132 public static final XSDDatatype XSDID = new XSDBaseStringType("ID"); 133 134 135 public static final XSDDatatype XSDNCName = new XSDBaseStringType("NCName"); 136 137 138 public static final XSDDatatype XSDIDREF = new XSDDatatype("IDREF"); 139 140 141 public static final XSDDatatype XSDNOTATION = new XSDDatatype("NOTATION"); 142 143 144 public static final XSDDatatype XSDhexBinary = new XSDhexBinary("hexBinary"); 145 146 147 public static final XSDDatatype XSDbase64Binary = new XSDbase64Binary("base64Binary"); 148 149 150 public static final XSDDatatype XSDdate = new XSDDateType("date"); 151 152 153 public static final XSDDatatype XSDtime = new XSDTimeType("time"); 154 155 156 public static final XSDDatatype XSDdateTime = new XSDDateTimeType("dateTime"); 157 158 159 public static final XSDDatatype XSDduration = new XSDDurationType(); 160 161 162 public static final XSDDatatype XSDgDay = new XSDDayType("gDay"); 163 164 165 public static final XSDDatatype XSDgMonth = new XSDMonthType("gMonth"); 166 167 168 public static final XSDDatatype XSDgYear = new XSDYearType("gYear"); 169 170 171 public static final XSDDatatype XSDgYearMonth = new XSDYearMonthType("gYearMonth"); 172 173 174 public static final XSDDatatype XSDgMonthDay = new XSDMonthDayType("gMonthDay"); 175 176 178 187 190 191 protected XSSimpleType typeDeclaration; 192 193 194 protected Class javaClass = null; 195 196 197 static final DecimalDV decimalDV = new DecimalDV(); 198 199 202 207 public XSDDatatype(String typeName) { 208 super(""); 209 typeDeclaration = SchemaDVFactory.getInstance().getBuiltInType(typeName); 210 uri = typeDeclaration.getNamespace() + "#" + typeDeclaration.getName(); 211 } 212 213 220 public XSDDatatype(String typeName, Class javaClass) { 221 this(typeName); 222 this.javaClass = javaClass; 223 } 224 225 233 public XSDDatatype(XSSimpleType xstype, String namespace) { 234 super(""); 235 typeDeclaration = xstype; 236 this.uri = namespace + "#" + typeDeclaration.getName(); 237 } 238 239 243 public Object parse(String lexicalForm) throws DatatypeFormatException { 244 try { 245 ValidationContext context = new ValidationState(); 246 ValidatedInfo resultInfo = new ValidatedInfo(); 247 Object result = typeDeclaration.validate(lexicalForm, context, resultInfo); 248 return convertValidatedDataValue(resultInfo); 249 } catch (InvalidDatatypeValueException e) { 250 throw new DatatypeFormatException(lexicalForm, this, "during parse -" + e); 251 } 252 } 253 254 258 public String unparse(Object value) { 259 return value.toString(); 260 } 261 262 267 public boolean isEqual(LiteralLabel value1, LiteralLabel value2) { 268 return typeDeclaration.isEqual(value1.getValue(), value2.getValue()); 269 } 270 271 276 public Class getJavaClass() { 277 return javaClass; 278 } 279 280 284 public Object extendedTypeDefinition() { 285 return typeDeclaration; 286 } 287 288 302 public static List loadUserDefined(String uri, Reader reader, String encoding, TypeMapper tm) throws DatatypeFormatException { 303 return loadUserDefined(new XMLInputSource(null, uri, uri, reader, encoding), tm); 304 } 305 306 319 public static List loadUserDefined(String uri, String encoding, TypeMapper tm) throws DatatypeFormatException { 320 return loadUserDefined(new XMLInputSource(null, uri, uri), tm); 321 } 322 323 334 private static List loadUserDefined(XMLInputSource source, TypeMapper tm) throws DatatypeFormatException { 335 XMLGrammarPreparser parser = new XMLGrammarPreparser(); 336 parser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); 337 try { 338 XSGrammar xsg = (XSGrammar) parser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, source); 339 org.apache.xerces.xs.XSModel xsm = xsg.toXSModel(); 340 XSNamedMap map = xsm.getComponents(XSTypeDefinition.SIMPLE_TYPE); 341 int numDefs = map.getLength(); 342 ArrayList names = new ArrayList(numDefs); 343 for (int i = 0; i < numDefs; i++) { 344 XSSimpleType xstype = (XSSimpleType) map.item(i); 345 if ( ! XSD.equals(xstype.getNamespace()) ) { 347 XSDDatatype definedType = new XSDGenericType(xstype, source.getSystemId()); 349 tm.registerDatatype(definedType); 350 names.add(definedType.getURI()); 351 } 352 } 353 return names; 354 } catch (Exception e) { 355 e.printStackTrace(); throw new DatatypeFormatException(e.toString()); 357 } 358 } 359 360 369 public Object convertValidatedDataValue(ValidatedInfo validatedInfo) throws DatatypeFormatException { 370 switch (validatedInfo.actualValueType) { 371 case XSConstants.BASE64BINARY_DT: 372 byte[] decoded = Base64.decode(validatedInfo.normalizedValue); 373 return (Object )(decoded); 374 375 case XSConstants.BOOLEAN_DT: 376 return (Boolean )validatedInfo.actualValue; 377 378 case XSConstants.HEXBINARY_DT: 379 decoded = HexBin.decode(validatedInfo.normalizedValue); 380 return (Object )(decoded); 381 382 case XSConstants.UNSIGNEDSHORT_DT: 383 case XSConstants.INT_DT: 384 return Integer.valueOf(trimPlus(validatedInfo.normalizedValue)); 385 386 case XSConstants.UNSIGNEDINT_DT: 387 case XSConstants.LONG_DT: 388 return Long.valueOf(trimPlus(validatedInfo.normalizedValue)); 389 390 case XSConstants.UNSIGNEDBYTE_DT: 391 case XSConstants.SHORT_DT: 392 return Short.valueOf(trimPlus(validatedInfo.normalizedValue)); 393 394 case XSConstants.BYTE_DT: 395 return Byte.valueOf(trimPlus(validatedInfo.normalizedValue)); 396 397 case XSConstants.UNSIGNEDLONG_DT: 398 case XSConstants.INTEGER_DT: 399 case XSConstants.NONNEGATIVEINTEGER_DT: 400 case XSConstants.NONPOSITIVEINTEGER_DT: 401 case XSConstants.POSITIVEINTEGER_DT: 402 case XSConstants.NEGATIVEINTEGER_DT: 403 case XSConstants.DECIMAL_DT: 404 Object xsdValue = validatedInfo.actualValue; 405 if (decimalDV.getTotalDigits(xsdValue) == 0) { 406 return new Long (0); 407 } 408 if (decimalDV.getFractionDigits(xsdValue) >= 1) { 409 return new BigDecimal (trimPlus(validatedInfo.normalizedValue)); 410 } 411 String lexical = trimPlus(validatedInfo.normalizedValue); 413 int dotx = lexical.indexOf('.'); 414 if (dotx != -1) { 415 lexical = lexical.substring(0, dotx); 416 } 417 if (decimalDV.getTotalDigits(xsdValue) > 18) { 418 return new BigInteger (lexical); 419 } else { 420 return new Long (lexical); 421 } 422 423 default: 424 return parseValidated(validatedInfo.normalizedValue); 425 } 426 } 427 428 433 public Object parseValidated(String lexical) { 434 return lexical; 435 } 436 437 444 public boolean isValidLiteral(LiteralLabel lit) { 445 return isBaseTypeCompatible(lit) && isValid(lit.getLexicalForm()); 446 } 447 448 453 public boolean isBaseTypeCompatible(LiteralLabel lit) { 454 XSTypeDefinition base = getFoundingType(); 455 RDFDatatype litDT = lit.getDatatype(); 456 if (litDT instanceof XSDDatatype) { 457 XSTypeDefinition litBase = ((XSDDatatype)litDT).getFoundingType(); 458 return base.equals(litBase); 459 460 } else if (litDT == null && lit.language().equals("")) { 461 return base.equals(XSDstring.typeDeclaration); 463 } else { 464 return false; 465 } 466 } 467 468 471 private XSTypeDefinition getFoundingType() { 472 XSTypeDefinition founding = typeDeclaration; 473 XSTypeDefinition parent = founding.getBaseType(); 474 while (parent.getBaseType() != null) { 475 founding = parent; 476 parent = founding.getBaseType(); 477 } 478 return founding; 479 } 480 481 485 public static String trimPlus(String str) { 486 int i = str.indexOf('+'); 487 if (i == -1) { 488 return str; 489 } else { 490 return str.substring(i+1); 491 } 492 } 493 494 498 public static void loadXSDSimpleTypes(TypeMapper tm) { 499 tm.registerDatatype(new XSDDatatype("anySimpleType")); 500 501 tm.registerDatatype(XSDdecimal); 502 tm.registerDatatype(XSDinteger); 503 tm.registerDatatype(XSDnonPositiveInteger); 504 tm.registerDatatype(XSDnonNegativeInteger); 505 tm.registerDatatype(XSDpositiveInteger); 506 tm.registerDatatype(XSDnegativeInteger); 507 508 tm.registerDatatype(XSDbyte); 509 tm.registerDatatype(XSDunsignedByte); 510 tm.registerDatatype(XSDdouble); 511 tm.registerDatatype(XSDfloat); 512 tm.registerDatatype(XSDlong); 513 tm.registerDatatype(XSDunsignedInt); 514 tm.registerDatatype(XSDunsignedShort); 515 tm.registerDatatype(XSDunsignedLong); 516 tm.registerDatatype(XSDint); 517 tm.registerDatatype(XSDshort); 518 519 tm.registerDatatype(XSDboolean); 520 tm.registerDatatype(XSDbase64Binary); 521 tm.registerDatatype(XSDhexBinary); 522 523 tm.registerDatatype(XSDdate); 524 tm.registerDatatype(XSDtime); 525 tm.registerDatatype(XSDdateTime); 526 tm.registerDatatype(XSDduration); 527 tm.registerDatatype(XSDgYearMonth); 528 tm.registerDatatype(XSDgMonthDay); 529 tm.registerDatatype(XSDgMonth); 530 tm.registerDatatype(XSDgDay); 531 tm.registerDatatype(XSDgYear); 532 533 tm.registerDatatype(XSDnormalizedString); 534 tm.registerDatatype(XSDstring); 535 tm.registerDatatype(XSDanyURI); 536 537 tm.registerDatatype(XSDtoken); 538 tm.registerDatatype(XSDName); 539 tm.registerDatatype(XSDlanguage); 540 tm.registerDatatype(XSDQName); 541 tm.registerDatatype(XSDNMTOKEN); 542 tm.registerDatatype(XSDID); 543 tm.registerDatatype(XSDENTITY); 544 tm.registerDatatype(XSDNCName); 545 tm.registerDatatype(XSDNOTATION); 546 tm.registerDatatype(XSDIDREF); 547 548 } 552 553 public static void main(String [] args) { 555 SymbolHash types = SchemaDVFactory.getInstance().getBuiltInTypes(); 556 int len = types.getLength(); 557 Object [] values = new Object [len]; 558 types.getValues(values, 0); 559 for (int i = 0; i < values.length; i++) { 560 if (values[i] instanceof XSSimpleTypeDecl) { 561 XSSimpleTypeDecl decl = (XSSimpleTypeDecl)values[i]; 562 System.out.println("tm.registerDatatype(new XSDDatatype(\"" 563 + decl.getName() 564 + "\"));"); 565 } else { 566 System.out.println(" - " + values[i]); 567 } 568 } 569 } 570 571 } 572 573 602 | Popular Tags |