Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemastypes.c : implementation of the XML Schema Datatypes |
| 3 | * definition and validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
| 10 | #define IN_LIBXML |
| 11 | #include "libxml.h" |
| 12 | |
| 13 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14 | |
| 15 | #include <string.h> |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/parser.h> |
| 18 | #include <libxml/parserInternals.h> |
| 19 | #include <libxml/hash.h> |
| 20 | #include <libxml/valid.h> |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 21 | #include <libxml/xpath.h> |
| 22 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 23 | |
| 24 | #include <libxml/xmlschemas.h> |
| 25 | #include <libxml/schemasInternals.h> |
| 26 | #include <libxml/xmlschemastypes.h> |
| 27 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 28 | #ifdef HAVE_MATH_H |
| 29 | #include <math.h> |
| 30 | #endif |
| 31 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 32 | #define DEBUG |
| 33 | |
| 34 | #define TODO \ |
| 35 | xmlGenericError(xmlGenericErrorContext, \ |
| 36 | "Unimplemented block at %s:%d\n", \ |
| 37 | __FILE__, __LINE__); |
| 38 | |
| 39 | #define XML_SCHEMAS_NAMESPACE_NAME \ |
| 40 | (const xmlChar *)"http://www.w3.org/2001/XMLSchema" |
| 41 | |
| 42 | typedef enum { |
| 43 | XML_SCHEMAS_UNKNOWN = 0, |
| 44 | XML_SCHEMAS_STRING, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 45 | XML_SCHEMAS_NORMSTRING, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 46 | XML_SCHEMAS_DECIMAL, |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 47 | XML_SCHEMAS_TIME, |
| 48 | XML_SCHEMAS_GDAY, |
| 49 | XML_SCHEMAS_GMONTH, |
| 50 | XML_SCHEMAS_GMONTHDAY, |
| 51 | XML_SCHEMAS_GYEAR, |
| 52 | XML_SCHEMAS_GYEARMONTH, |
| 53 | XML_SCHEMAS_DATE, |
| 54 | XML_SCHEMAS_DATETIME, |
| 55 | XML_SCHEMAS_DURATION, |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 56 | XML_SCHEMAS_FLOAT, |
| 57 | XML_SCHEMAS_DOUBLE, |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 58 | XML_SCHEMAS_BOOLEAN, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 59 | XML_SCHEMAS_TOKEN, |
| 60 | XML_SCHEMAS_LANGUAGE, |
| 61 | XML_SCHEMAS_NMTOKEN, |
| 62 | XML_SCHEMAS_NMTOKENS, |
| 63 | XML_SCHEMAS_NAME, |
| 64 | XML_SCHEMAS_QNAME, |
| 65 | XML_SCHEMAS_NCNAME, |
| 66 | XML_SCHEMAS_ID, |
| 67 | XML_SCHEMAS_IDREF, |
| 68 | XML_SCHEMAS_IDREFS, |
| 69 | XML_SCHEMAS_ENTITY, |
| 70 | XML_SCHEMAS_ENTITIES, |
| 71 | XML_SCHEMAS_NOTATION, |
| 72 | XML_SCHEMAS_ANYURI, |
| 73 | XML_SCHEMAS_INTEGER, |
| 74 | XML_SCHEMAS_NPINTEGER, |
| 75 | XML_SCHEMAS_NINTEGER, |
| 76 | XML_SCHEMAS_NNINTEGER, |
| 77 | XML_SCHEMAS_PINTEGER, |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 78 | XML_SCHEMAS_INT, |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 79 | XML_SCHEMAS_UINT, |
| 80 | XML_SCHEMAS_LONG, |
| 81 | XML_SCHEMAS_ULONG, |
| 82 | XML_SCHEMAS_SHORT, |
| 83 | XML_SCHEMAS_USHORT, |
| 84 | XML_SCHEMAS_BYTE, |
| 85 | XML_SCHEMAS_UBYTE |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 86 | } xmlSchemaValType; |
| 87 | |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 88 | static unsigned long powten[10] = { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L, |
| 90 | 100000000L, 1000000000L |
| 91 | }; |
| 92 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 93 | /* Date value */ |
| 94 | typedef struct _xmlSchemaValDate xmlSchemaValDate; |
| 95 | typedef xmlSchemaValDate *xmlSchemaValDatePtr; |
| 96 | struct _xmlSchemaValDate { |
| 97 | long year; |
| 98 | unsigned int mon :4; /* 1 <= mon <= 12 */ |
| 99 | unsigned int day :5; /* 1 <= day <= 31 */ |
| 100 | unsigned int hour :5; /* 0 <= hour <= 23 */ |
| 101 | unsigned int min :6; /* 0 <= min <= 59 */ |
| 102 | double sec; |
| 103 | int tz_flag :1; /* is tzo explicitely set? */ |
| 104 | int tzo :11; /* -1440 <= tzo <= 1440 */ |
| 105 | }; |
| 106 | |
| 107 | /* Duration value */ |
| 108 | typedef struct _xmlSchemaValDuration xmlSchemaValDuration; |
| 109 | typedef xmlSchemaValDuration *xmlSchemaValDurationPtr; |
| 110 | struct _xmlSchemaValDuration { |
| 111 | long mon; /* mon stores years also */ |
| 112 | long day; |
| 113 | double sec; /* sec stores min and hour also */ |
| 114 | }; |
| 115 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 116 | typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal; |
| 117 | typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr; |
| 118 | struct _xmlSchemaValDecimal { |
| 119 | /* would use long long but not portable */ |
| 120 | unsigned long base; |
| 121 | unsigned int extra; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 122 | unsigned int sign:1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 123 | int frac:7; |
| 124 | int total:8; |
| 125 | }; |
| 126 | |
| 127 | struct _xmlSchemaVal { |
| 128 | xmlSchemaValType type; |
| 129 | union { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 130 | xmlSchemaValDecimal decimal; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 131 | xmlSchemaValDate date; |
| 132 | xmlSchemaValDuration dur; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 133 | float f; |
| 134 | double d; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 135 | int b; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 136 | } value; |
| 137 | }; |
| 138 | |
| 139 | static int xmlSchemaTypesInitialized = 0; |
| 140 | static xmlHashTablePtr xmlSchemaTypesBank = NULL; |
| 141 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 142 | /* |
| 143 | * Basic types |
| 144 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 145 | static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL; |
| 146 | static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL; |
| 147 | static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL; |
| 148 | static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 149 | static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 150 | static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 151 | static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL; |
| 152 | static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL; |
| 153 | static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL; |
| 154 | static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL; |
| 155 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL; |
| 156 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL; |
| 157 | static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 158 | static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 159 | static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 160 | static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 161 | static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 162 | |
| 163 | /* |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 164 | * Derived types |
| 165 | */ |
| 166 | static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL; |
| 167 | static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL; |
| 168 | static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL; |
| 169 | static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL; |
| 170 | static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL; |
| 171 | static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL; |
| 172 | static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL; |
| 173 | static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL; |
| 174 | static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL; |
| 175 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL; |
| 176 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL; |
| 177 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL; |
| 178 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 179 | static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL; |
| 180 | static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL; |
| 181 | static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL; |
| 182 | static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL; |
| 183 | static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 184 | static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 185 | static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL; |
| 186 | static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL; |
| 187 | static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL; |
| 188 | static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL; |
| 189 | static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 190 | |
| 191 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 192 | * xmlSchemaInitBasicType: |
| 193 | * @name: the type name |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 194 | * @type: the value type associated |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 195 | * |
| 196 | * Initialize one default type |
| 197 | */ |
| 198 | static xmlSchemaTypePtr |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 199 | xmlSchemaInitBasicType(const char *name, xmlSchemaValType type) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 200 | xmlSchemaTypePtr ret; |
| 201 | |
| 202 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 203 | if (ret == NULL) { |
| 204 | xmlGenericError(xmlGenericErrorContext, |
| 205 | "Could not initilize type %s: out of memory\n", name); |
| 206 | return(NULL); |
| 207 | } |
| 208 | memset(ret, 0, sizeof(xmlSchemaType)); |
| 209 | ret->name = xmlStrdup((const xmlChar *)name); |
| 210 | ret->type = XML_SCHEMA_TYPE_BASIC; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 211 | ret->flags = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 212 | ret->contentType = XML_SCHEMA_CONTENT_BASIC; |
| 213 | xmlHashAddEntry2(xmlSchemaTypesBank, ret->name, |
| 214 | XML_SCHEMAS_NAMESPACE_NAME, ret); |
| 215 | return(ret); |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * xmlSchemaInitTypes: |
| 220 | * |
| 221 | * Initialize the default XML Schemas type library |
| 222 | */ |
| 223 | void |
| 224 | xmlSchemaInitTypes(void) { |
| 225 | if (xmlSchemaTypesInitialized != 0) |
| 226 | return; |
| 227 | xmlSchemaTypesBank = xmlHashCreate(40); |
| 228 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 229 | /* |
| 230 | * primitive datatypes |
| 231 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 232 | xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string", |
| 233 | XML_SCHEMAS_STRING); |
| 234 | xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType", |
| 235 | XML_SCHEMAS_UNKNOWN); |
| 236 | xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType", |
| 237 | XML_SCHEMAS_UNKNOWN); |
| 238 | xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal", |
| 239 | XML_SCHEMAS_DECIMAL); |
| 240 | xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date", |
| 241 | XML_SCHEMAS_DATE); |
| 242 | xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime", |
| 243 | XML_SCHEMAS_DATETIME); |
| 244 | xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time", |
| 245 | XML_SCHEMAS_TIME); |
| 246 | xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear", |
| 247 | XML_SCHEMAS_GYEAR); |
| 248 | xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth", |
| 249 | XML_SCHEMAS_GYEARMONTH); |
| 250 | xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth", |
| 251 | XML_SCHEMAS_GMONTH); |
| 252 | xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay", |
| 253 | XML_SCHEMAS_GMONTHDAY); |
| 254 | xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay", |
| 255 | XML_SCHEMAS_GDAY); |
| 256 | xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration", |
| 257 | XML_SCHEMAS_DURATION); |
| 258 | xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float", |
| 259 | XML_SCHEMAS_FLOAT); |
| 260 | xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double", |
| 261 | XML_SCHEMAS_DOUBLE); |
| 262 | xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean", |
| 263 | XML_SCHEMAS_BOOLEAN); |
| 264 | xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI", |
| 265 | XML_SCHEMAS_ANYURI); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 266 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 267 | /* |
| 268 | * derived datatypes |
| 269 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 270 | xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer", |
| 271 | XML_SCHEMAS_INTEGER);; |
| 272 | xmlSchemaTypeNonPositiveIntegerDef = xmlSchemaInitBasicType("nonPositiveInteger", |
| 273 | XML_SCHEMAS_NPINTEGER);; |
| 274 | xmlSchemaTypeNegativeIntegerDef = xmlSchemaInitBasicType("negativeInteger", |
| 275 | XML_SCHEMAS_NINTEGER);; |
| 276 | xmlSchemaTypeLongDef = xmlSchemaInitBasicType("long", |
| 277 | XML_SCHEMAS_LONG);; |
| 278 | xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", |
| 279 | XML_SCHEMAS_INT);; |
| 280 | xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short", |
| 281 | XML_SCHEMAS_SHORT);; |
| 282 | xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte", |
| 283 | XML_SCHEMAS_BYTE);; |
| 284 | xmlSchemaTypeNonNegativeIntegerDef = xmlSchemaInitBasicType("nonNegativeInteger", |
| 285 | XML_SCHEMAS_NNINTEGER); |
| 286 | xmlSchemaTypeUnsignedLongDef = xmlSchemaInitBasicType("unsignedLong", |
| 287 | XML_SCHEMAS_ULONG);; |
| 288 | xmlSchemaTypeUnsignedIntDef = xmlSchemaInitBasicType("unsignedInt", |
| 289 | XML_SCHEMAS_UINT);; |
| 290 | xmlSchemaTypeUnsignedShortDef = xmlSchemaInitBasicType("insignedShort", |
| 291 | XML_SCHEMAS_USHORT);; |
| 292 | xmlSchemaTypeUnsignedByteDef = xmlSchemaInitBasicType("unsignedByte", |
| 293 | XML_SCHEMAS_UBYTE);; |
| 294 | xmlSchemaTypePositiveIntegerDef = xmlSchemaInitBasicType("positiveInteger", |
| 295 | XML_SCHEMAS_PINTEGER); |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 296 | |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 297 | xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString", |
| 298 | XML_SCHEMAS_NORMSTRING); |
| 299 | xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token", |
| 300 | XML_SCHEMAS_TOKEN); |
| 301 | xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language", |
| 302 | XML_SCHEMAS_LANGUAGE); |
| 303 | xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", |
| 304 | XML_SCHEMAS_ID); |
| 305 | xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF", |
| 306 | XML_SCHEMAS_IDREF); |
| 307 | xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS", |
| 308 | XML_SCHEMAS_IDREFS); |
| 309 | xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name", |
| 310 | XML_SCHEMAS_NAME); |
| 311 | xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName", |
| 312 | XML_SCHEMAS_QNAME); |
| 313 | xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName", |
| 314 | XML_SCHEMAS_NCNAME); |
| 315 | xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN", |
| 316 | XML_SCHEMAS_NMTOKEN); |
| 317 | xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS", |
| 318 | XML_SCHEMAS_NMTOKENS); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 319 | xmlSchemaTypesInitialized = 1; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * xmlSchemaCleanupTypes: |
| 324 | * |
| 325 | * Cleanup the default XML Schemas type library |
| 326 | */ |
| 327 | void |
| 328 | xmlSchemaCleanupTypes(void) { |
| 329 | if (xmlSchemaTypesInitialized == 0) |
| 330 | return; |
| 331 | xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType); |
| 332 | xmlSchemaTypesInitialized = 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * xmlSchemaNewValue: |
| 337 | * @type: the value type |
| 338 | * |
| 339 | * Allocate a new simple type value |
| 340 | * |
| 341 | * Returns a pointer to the new value or NULL in case of error |
| 342 | */ |
| 343 | static xmlSchemaValPtr |
| 344 | xmlSchemaNewValue(xmlSchemaValType type) { |
| 345 | xmlSchemaValPtr value; |
| 346 | |
| 347 | value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 348 | if (value == NULL) { |
| 349 | return(NULL); |
| 350 | } |
| 351 | memset(value, 0, sizeof(xmlSchemaVal)); |
| 352 | value->type = type; |
| 353 | return(value); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * xmlSchemaFreeValue: |
| 358 | * @value: the value to free |
| 359 | * |
| 360 | * Cleanup the default XML Schemas type library |
| 361 | */ |
| 362 | void |
| 363 | xmlSchemaFreeValue(xmlSchemaValPtr value) { |
| 364 | if (value == NULL) |
| 365 | return; |
| 366 | xmlFree(value); |
| 367 | } |
| 368 | |
| 369 | /** |
| 370 | * xmlSchemaGetPredefinedType: |
| 371 | * @name: the type name |
| 372 | * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema" |
| 373 | * |
| 374 | * Lookup a type in the default XML Schemas type library |
| 375 | * |
| 376 | * Returns the type if found, NULL otherwise |
| 377 | */ |
| 378 | xmlSchemaTypePtr |
| 379 | xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) { |
| 380 | if (xmlSchemaTypesInitialized == 0) |
| 381 | xmlSchemaInitTypes(); |
| 382 | if (name == NULL) |
| 383 | return(NULL); |
| 384 | return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns)); |
| 385 | } |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 386 | |
| 387 | /**************************************************************** |
| 388 | * * |
| 389 | * Convenience macros and functions * |
| 390 | * * |
| 391 | ****************************************************************/ |
| 392 | |
| 393 | #define IS_TZO_CHAR(c) \ |
| 394 | ((c == 0) || (c == 'Z') || (c == '+') || (c == '-')) |
| 395 | |
| 396 | #define VALID_YEAR(yr) (yr != 0) |
| 397 | #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12)) |
| 398 | /* VALID_DAY should only be used when month is unknown */ |
| 399 | #define VALID_DAY(day) ((day >= 1) && (day <= 31)) |
| 400 | #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23)) |
| 401 | #define VALID_MIN(min) ((min >= 0) && (min <= 59)) |
| 402 | #define VALID_SEC(sec) ((sec >= 0) && (sec < 60)) |
| 403 | #define VALID_TZO(tzo) ((tzo > -1440) && (tzo < 1440)) |
| 404 | #define IS_LEAP(y) \ |
| 405 | (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) |
| 406 | |
| 407 | static const long daysInMonth[12] = |
| 408 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 409 | static const long daysInMonthLeap[12] = |
| 410 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 411 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 412 | #define MAX_DAYINMONTH(yr,mon) \ |
| 413 | (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1]) |
| 414 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 415 | #define VALID_MDAY(dt) \ |
| 416 | (IS_LEAP(dt->year) ? \ |
| 417 | (dt->day <= daysInMonthLeap[dt->mon - 1]) : \ |
| 418 | (dt->day <= daysInMonth[dt->mon - 1])) |
| 419 | |
| 420 | #define VALID_DATE(dt) \ |
| 421 | (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt)) |
| 422 | |
| 423 | #define VALID_TIME(dt) \ |
| 424 | (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \ |
| 425 | VALID_SEC(dt->sec) && VALID_TZO(dt->tzo)) |
| 426 | |
| 427 | #define VALID_DATETIME(dt) \ |
| 428 | (VALID_DATE(dt) && VALID_TIME(dt)) |
| 429 | |
| 430 | #define SECS_PER_MIN (60) |
| 431 | #define SECS_PER_HOUR (60 * SECS_PER_MIN) |
| 432 | #define SECS_PER_DAY (24 * SECS_PER_HOUR) |
| 433 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 434 | static const long dayInYearByMonth[12] = |
| 435 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 436 | static const long dayInLeapYearByMonth[12] = |
| 437 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; |
| 438 | |
| 439 | #define DAY_IN_YEAR(day, month, year) \ |
| 440 | ((IS_LEAP(year) ? \ |
| 441 | dayInLeapYearByMonth[month - 1] : \ |
| 442 | dayInYearByMonth[month - 1]) + day) |
| 443 | |
| 444 | #ifdef DEBUG |
| 445 | #define DEBUG_DATE(dt) \ |
| 446 | xmlGenericError(xmlGenericErrorContext, \ |
| 447 | "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \ |
| 448 | dt->type,dt->value.date.year,dt->value.date.mon, \ |
| 449 | dt->value.date.day,dt->value.date.hour,dt->value.date.min, \ |
| 450 | dt->value.date.sec); \ |
| 451 | if (dt->value.date.tz_flag) \ |
| 452 | if (dt->value.date.tzo != 0) \ |
| 453 | xmlGenericError(xmlGenericErrorContext, \ |
| 454 | "%+05d\n",dt->value.date.tzo); \ |
| 455 | else \ |
| 456 | xmlGenericError(xmlGenericErrorContext, "Z\n"); \ |
| 457 | else \ |
| 458 | xmlGenericError(xmlGenericErrorContext,"\n") |
| 459 | #else |
| 460 | #define DEBUG_DATE(dt) |
| 461 | #endif |
| 462 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 463 | /** |
| 464 | * _xmlSchemaParseGYear: |
| 465 | * @dt: pointer to a date structure |
| 466 | * @str: pointer to the string to analyze |
| 467 | * |
| 468 | * Parses a xs:gYear without time zone and fills in the appropriate |
| 469 | * field of the @dt structure. @str is updated to point just after the |
| 470 | * xs:gYear. It is supposed that @dt->year is big enough to contain |
| 471 | * the year. |
| 472 | * |
| 473 | * Returns 0 or the error code |
| 474 | */ |
| 475 | static int |
| 476 | _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 477 | const xmlChar *cur = *str, *firstChar; |
| 478 | int isneg = 0, digcnt = 0; |
| 479 | |
| 480 | if (((*cur < '0') || (*cur > '9')) && |
| 481 | (*cur != '-') && (*cur != '+')) |
| 482 | return -1; |
| 483 | |
| 484 | if (*cur == '-') { |
| 485 | isneg = 1; |
| 486 | cur++; |
| 487 | } |
| 488 | |
| 489 | firstChar = cur; |
| 490 | |
| 491 | while ((*cur >= '0') && (*cur <= '9')) { |
| 492 | dt->year = dt->year * 10 + (*cur - '0'); |
| 493 | cur++; |
| 494 | digcnt++; |
| 495 | } |
| 496 | |
| 497 | /* year must be at least 4 digits (CCYY); over 4 |
| 498 | * digits cannot have a leading zero. */ |
| 499 | if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0'))) |
| 500 | return 1; |
| 501 | |
| 502 | if (isneg) |
| 503 | dt->year = - dt->year; |
| 504 | |
| 505 | if (!VALID_YEAR(dt->year)) |
| 506 | return 2; |
| 507 | |
| 508 | *str = cur; |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * PARSE_2_DIGITS: |
| 514 | * @num: the integer to fill in |
| 515 | * @cur: an #xmlChar * |
| 516 | * @invalid: an integer |
| 517 | * |
| 518 | * Parses a 2-digits integer and updates @num with the value. @cur is |
| 519 | * updated to point just after the integer. |
| 520 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 521 | * @cur are undefined. |
| 522 | */ |
| 523 | #define PARSE_2_DIGITS(num, cur, invalid) \ |
| 524 | if ((cur[0] < '0') || (cur[0] > '9') || \ |
| 525 | (cur[1] < '0') || (cur[1] > '9')) \ |
| 526 | invalid = 1; \ |
| 527 | else \ |
| 528 | num = (cur[0] - '0') * 10 + (cur[1] - '0'); \ |
| 529 | cur += 2; |
| 530 | |
| 531 | /** |
| 532 | * PARSE_FLOAT: |
| 533 | * @num: the double to fill in |
| 534 | * @cur: an #xmlChar * |
| 535 | * @invalid: an integer |
| 536 | * |
| 537 | * Parses a float and updates @num with the value. @cur is |
| 538 | * updated to point just after the float. The float must have a |
| 539 | * 2-digits integer part and may or may not have a decimal part. |
| 540 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 541 | * @cur are undefined. |
| 542 | */ |
| 543 | #define PARSE_FLOAT(num, cur, invalid) \ |
| 544 | PARSE_2_DIGITS(num, cur, invalid); \ |
| 545 | if (!invalid && (*cur == '.')) { \ |
| 546 | double mult = 1; \ |
| 547 | cur++; \ |
| 548 | if ((*cur < '0') || (*cur > '9')) \ |
| 549 | invalid = 1; \ |
| 550 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 551 | mult /= 10; \ |
| 552 | num += (*cur - '0') * mult; \ |
| 553 | cur++; \ |
| 554 | } \ |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * _xmlSchemaParseGMonth: |
| 559 | * @dt: pointer to a date structure |
| 560 | * @str: pointer to the string to analyze |
| 561 | * |
| 562 | * Parses a xs:gMonth without time zone and fills in the appropriate |
| 563 | * field of the @dt structure. @str is updated to point just after the |
| 564 | * xs:gMonth. |
| 565 | * |
| 566 | * Returns 0 or the error code |
| 567 | */ |
| 568 | static int |
| 569 | _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 570 | const xmlChar *cur = *str; |
| 571 | int ret = 0; |
| 572 | |
| 573 | PARSE_2_DIGITS(dt->mon, cur, ret); |
| 574 | if (ret != 0) |
| 575 | return ret; |
| 576 | |
| 577 | if (!VALID_MONTH(dt->mon)) |
| 578 | return 2; |
| 579 | |
| 580 | *str = cur; |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /** |
| 585 | * _xmlSchemaParseGDay: |
| 586 | * @dt: pointer to a date structure |
| 587 | * @str: pointer to the string to analyze |
| 588 | * |
| 589 | * Parses a xs:gDay without time zone and fills in the appropriate |
| 590 | * field of the @dt structure. @str is updated to point just after the |
| 591 | * xs:gDay. |
| 592 | * |
| 593 | * Returns 0 or the error code |
| 594 | */ |
| 595 | static int |
| 596 | _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 597 | const xmlChar *cur = *str; |
| 598 | int ret = 0; |
| 599 | |
| 600 | PARSE_2_DIGITS(dt->day, cur, ret); |
| 601 | if (ret != 0) |
| 602 | return ret; |
| 603 | |
| 604 | if (!VALID_DAY(dt->day)) |
| 605 | return 2; |
| 606 | |
| 607 | *str = cur; |
| 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * _xmlSchemaParseTime: |
| 613 | * @dt: pointer to a date structure |
| 614 | * @str: pointer to the string to analyze |
| 615 | * |
| 616 | * Parses a xs:time without time zone and fills in the appropriate |
| 617 | * fields of the @dt structure. @str is updated to point just after the |
| 618 | * xs:time. |
| 619 | * In case of error, values of @dt fields are undefined. |
| 620 | * |
| 621 | * Returns 0 or the error code |
| 622 | */ |
| 623 | static int |
| 624 | _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 625 | const xmlChar *cur = *str; |
| 626 | unsigned int hour = 0; /* use temp var in case str is not xs:time */ |
| 627 | int ret = 0; |
| 628 | |
| 629 | PARSE_2_DIGITS(hour, cur, ret); |
| 630 | if (ret != 0) |
| 631 | return ret; |
| 632 | |
| 633 | if (*cur != ':') |
| 634 | return 1; |
| 635 | cur++; |
| 636 | |
| 637 | /* the ':' insures this string is xs:time */ |
| 638 | dt->hour = hour; |
| 639 | |
| 640 | PARSE_2_DIGITS(dt->min, cur, ret); |
| 641 | if (ret != 0) |
| 642 | return ret; |
| 643 | |
| 644 | if (*cur != ':') |
| 645 | return 1; |
| 646 | cur++; |
| 647 | |
| 648 | PARSE_FLOAT(dt->sec, cur, ret); |
| 649 | if (ret != 0) |
| 650 | return ret; |
| 651 | |
| 652 | if (!VALID_TIME(dt)) |
| 653 | return 2; |
| 654 | |
| 655 | *str = cur; |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * _xmlSchemaParseTimeZone: |
| 661 | * @dt: pointer to a date structure |
| 662 | * @str: pointer to the string to analyze |
| 663 | * |
| 664 | * Parses a time zone without time zone and fills in the appropriate |
| 665 | * field of the @dt structure. @str is updated to point just after the |
| 666 | * time zone. |
| 667 | * |
| 668 | * Returns 0 or the error code |
| 669 | */ |
| 670 | static int |
| 671 | _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 672 | const xmlChar *cur = *str; |
| 673 | int ret = 0; |
| 674 | |
| 675 | if (str == NULL) |
| 676 | return -1; |
| 677 | |
| 678 | switch (*cur) { |
| 679 | case 0: |
| 680 | dt->tz_flag = 0; |
| 681 | dt->tzo = 0; |
| 682 | break; |
| 683 | |
| 684 | case 'Z': |
| 685 | dt->tz_flag = 1; |
| 686 | dt->tzo = 0; |
| 687 | cur++; |
| 688 | break; |
| 689 | |
| 690 | case '+': |
| 691 | case '-': { |
| 692 | int isneg = 0, tmp = 0; |
| 693 | isneg = (*cur == '-'); |
| 694 | |
| 695 | cur++; |
| 696 | |
| 697 | PARSE_2_DIGITS(tmp, cur, ret); |
| 698 | if (ret != 0) |
| 699 | return ret; |
| 700 | if (!VALID_HOUR(tmp)) |
| 701 | return 2; |
| 702 | |
| 703 | if (*cur != ':') |
| 704 | return 1; |
| 705 | cur++; |
| 706 | |
| 707 | dt->tzo = tmp * 60; |
| 708 | |
| 709 | PARSE_2_DIGITS(tmp, cur, ret); |
| 710 | if (ret != 0) |
| 711 | return ret; |
| 712 | if (!VALID_MIN(tmp)) |
| 713 | return 2; |
| 714 | |
| 715 | dt->tzo += tmp; |
| 716 | if (isneg) |
| 717 | dt->tzo = - dt->tzo; |
| 718 | |
| 719 | if (!VALID_TZO(dt->tzo)) |
| 720 | return 2; |
| 721 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 722 | dt->tz_flag = 1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 723 | break; |
| 724 | } |
| 725 | default: |
| 726 | return 1; |
| 727 | } |
| 728 | |
| 729 | *str = cur; |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | /**************************************************************** |
| 734 | * * |
| 735 | * XML Schema Dates/Times Datatypes Handling * |
| 736 | * * |
| 737 | ****************************************************************/ |
| 738 | |
| 739 | /** |
| 740 | * PARSE_DIGITS: |
| 741 | * @num: the integer to fill in |
| 742 | * @cur: an #xmlChar * |
| 743 | * @num_type: an integer flag |
| 744 | * |
| 745 | * Parses a digits integer and updates @num with the value. @cur is |
| 746 | * updated to point just after the integer. |
| 747 | * In case of error, @num_type is set to -1, values of @num and |
| 748 | * @cur are undefined. |
| 749 | */ |
| 750 | #define PARSE_DIGITS(num, cur, num_type) \ |
| 751 | if ((*cur < '0') || (*cur > '9')) \ |
| 752 | num_type = -1; \ |
| 753 | else \ |
| 754 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 755 | num = num * 10 + (*cur - '0'); \ |
| 756 | cur++; \ |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * PARSE_NUM: |
| 761 | * @num: the double to fill in |
| 762 | * @cur: an #xmlChar * |
| 763 | * @num_type: an integer flag |
| 764 | * |
| 765 | * Parses a float or integer and updates @num with the value. @cur is |
| 766 | * updated to point just after the number. If the number is a float, |
| 767 | * then it must have an integer part and a decimal part; @num_type will |
| 768 | * be set to 1. If there is no decimal part, @num_type is set to zero. |
| 769 | * In case of error, @num_type is set to -1, values of @num and |
| 770 | * @cur are undefined. |
| 771 | */ |
| 772 | #define PARSE_NUM(num, cur, num_type) \ |
| 773 | num = 0; \ |
| 774 | PARSE_DIGITS(num, cur, num_type); \ |
| 775 | if (!num_type && (*cur == '.')) { \ |
| 776 | double mult = 1; \ |
| 777 | cur++; \ |
| 778 | if ((*cur < '0') || (*cur > '9')) \ |
| 779 | num_type = -1; \ |
| 780 | else \ |
| 781 | num_type = 1; \ |
| 782 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 783 | mult /= 10; \ |
| 784 | num += (*cur - '0') * mult; \ |
| 785 | cur++; \ |
| 786 | } \ |
| 787 | } |
| 788 | |
| 789 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 790 | * xmlSchemaValidateDates: |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 791 | * @type: the predefined type |
| 792 | * @dateTime: string to analyze |
| 793 | * @val: the return computed value |
| 794 | * |
| 795 | * Check that @dateTime conforms to the lexical space of one of the date types. |
| 796 | * if true a value is computed and returned in @val. |
| 797 | * |
| 798 | * Returns 0 if this validates, a positive error code number otherwise |
| 799 | * and -1 in case of internal or API error. |
| 800 | */ |
| 801 | static int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 802 | xmlSchemaValidateDates (xmlSchemaTypePtr type ATTRIBUTE_UNUSED, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 803 | const xmlChar *dateTime, xmlSchemaValPtr *val) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 804 | xmlSchemaValPtr dt; |
| 805 | int ret; |
| 806 | const xmlChar *cur = dateTime; |
| 807 | |
| 808 | #define RETURN_TYPE_IF_VALID(t) \ |
| 809 | if (IS_TZO_CHAR(*cur)) { \ |
| 810 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \ |
| 811 | if (ret == 0) { \ |
| 812 | if (*cur != 0) \ |
| 813 | goto error; \ |
| 814 | dt->type = t; \ |
| 815 | if (val != NULL) \ |
| 816 | *val = dt; \ |
| 817 | return 0; \ |
| 818 | } \ |
| 819 | } |
| 820 | |
| 821 | if (dateTime == NULL) |
| 822 | return -1; |
| 823 | |
| 824 | if ((*cur != '-') && (*cur < '0') && (*cur > '9')) |
| 825 | return 1; |
| 826 | |
| 827 | dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN); |
| 828 | if (dt == NULL) |
| 829 | return -1; |
| 830 | |
| 831 | if ((cur[0] == '-') && (cur[1] == '-')) { |
| 832 | /* |
| 833 | * It's an incomplete date (xs:gMonthDay, xs:gMonth or |
| 834 | * xs:gDay) |
| 835 | */ |
| 836 | cur += 2; |
| 837 | |
| 838 | /* is it an xs:gDay? */ |
| 839 | if (*cur == '-') { |
| 840 | ++cur; |
| 841 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 842 | if (ret != 0) |
| 843 | goto error; |
| 844 | |
| 845 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY); |
| 846 | |
| 847 | goto error; |
| 848 | } |
| 849 | |
| 850 | /* |
| 851 | * it should be an xs:gMonthDay or xs:gMonth |
| 852 | */ |
| 853 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 854 | if (ret != 0) |
| 855 | goto error; |
| 856 | |
| 857 | if (*cur != '-') |
| 858 | goto error; |
| 859 | cur++; |
| 860 | |
| 861 | /* is it an xs:gMonth? */ |
| 862 | if (*cur == '-') { |
| 863 | cur++; |
| 864 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH); |
| 865 | goto error; |
| 866 | } |
| 867 | |
| 868 | /* it should be an xs:gMonthDay */ |
| 869 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 870 | if (ret != 0) |
| 871 | goto error; |
| 872 | |
| 873 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY); |
| 874 | |
| 875 | goto error; |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * It's a right-truncated date or an xs:time. |
| 880 | * Try to parse an xs:time then fallback on right-truncated dates. |
| 881 | */ |
| 882 | if ((*cur >= '0') && (*cur <= '9')) { |
| 883 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 884 | if (ret == 0) { |
| 885 | /* it's an xs:time */ |
| 886 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /* fallback on date parsing */ |
| 891 | cur = dateTime; |
| 892 | |
| 893 | ret = _xmlSchemaParseGYear(&(dt->value.date), &cur); |
| 894 | if (ret != 0) |
| 895 | goto error; |
| 896 | |
| 897 | /* is it an xs:gYear? */ |
| 898 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR); |
| 899 | |
| 900 | if (*cur != '-') |
| 901 | goto error; |
| 902 | cur++; |
| 903 | |
| 904 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 905 | if (ret != 0) |
| 906 | goto error; |
| 907 | |
| 908 | /* is it an xs:gYearMonth? */ |
| 909 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH); |
| 910 | |
| 911 | if (*cur != '-') |
| 912 | goto error; |
| 913 | cur++; |
| 914 | |
| 915 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 916 | if ((ret != 0) || !VALID_DATE((&(dt->value.date)))) |
| 917 | goto error; |
| 918 | |
| 919 | /* is it an xs:date? */ |
| 920 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE); |
| 921 | |
| 922 | if (*cur != 'T') |
| 923 | goto error; |
| 924 | cur++; |
| 925 | |
| 926 | /* it should be an xs:dateTime */ |
| 927 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 928 | if (ret != 0) |
| 929 | goto error; |
| 930 | |
| 931 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); |
| 932 | if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date)))) |
| 933 | goto error; |
| 934 | |
| 935 | dt->type = XML_SCHEMAS_DATETIME; |
| 936 | |
| 937 | if (val != NULL) |
| 938 | *val = dt; |
| 939 | |
| 940 | return 0; |
| 941 | |
| 942 | error: |
| 943 | if (dt != NULL) |
| 944 | xmlSchemaFreeValue(dt); |
| 945 | return 1; |
| 946 | } |
| 947 | |
| 948 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 949 | * xmlSchemaValidateDuration: |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 950 | * @type: the predefined type |
| 951 | * @duration: string to analyze |
| 952 | * @val: the return computed value |
| 953 | * |
| 954 | * Check that @duration conforms to the lexical space of the duration type. |
| 955 | * if true a value is computed and returned in @val. |
| 956 | * |
| 957 | * Returns 0 if this validates, a positive error code number otherwise |
| 958 | * and -1 in case of internal or API error. |
| 959 | */ |
| 960 | static int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 961 | xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 962 | const xmlChar *duration, xmlSchemaValPtr *val) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 963 | const xmlChar *cur = duration; |
| 964 | xmlSchemaValPtr dur; |
| 965 | int isneg = 0; |
| 966 | unsigned int seq = 0; |
| 967 | |
| 968 | if (duration == NULL) |
| 969 | return -1; |
| 970 | |
| 971 | if (*cur == '-') { |
| 972 | isneg = 1; |
| 973 | cur++; |
| 974 | } |
| 975 | |
| 976 | /* duration must start with 'P' (after sign) */ |
| 977 | if (*cur++ != 'P') |
| 978 | return 1; |
| 979 | |
| 980 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 981 | if (dur == NULL) |
| 982 | return -1; |
| 983 | |
| 984 | while (*cur != 0) { |
| 985 | double num; |
| 986 | int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */ |
| 987 | const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'}; |
| 988 | const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0}; |
| 989 | |
| 990 | /* input string should be empty or invalid date/time item */ |
| 991 | if (seq >= sizeof(desig)) |
| 992 | goto error; |
| 993 | |
| 994 | /* T designator must be present for time items */ |
| 995 | if (*cur == 'T') { |
| 996 | if (seq <= 3) { |
| 997 | seq = 3; |
| 998 | cur++; |
| 999 | } else |
| 1000 | return 1; |
| 1001 | } else if (seq == 3) |
| 1002 | goto error; |
| 1003 | |
| 1004 | /* parse the number portion of the item */ |
| 1005 | PARSE_NUM(num, cur, num_type); |
| 1006 | |
| 1007 | if ((num_type == -1) || (*cur == 0)) |
| 1008 | goto error; |
| 1009 | |
| 1010 | /* update duration based on item type */ |
| 1011 | while (seq < sizeof(desig)) { |
| 1012 | if (*cur == desig[seq]) { |
| 1013 | |
| 1014 | /* verify numeric type; only seconds can be float */ |
| 1015 | if ((num_type != 0) && (seq < (sizeof(desig)-1))) |
| 1016 | goto error; |
| 1017 | |
| 1018 | switch (seq) { |
| 1019 | case 0: |
| 1020 | dur->value.dur.mon = (long)num * 12; |
| 1021 | break; |
| 1022 | case 1: |
| 1023 | dur->value.dur.mon += (long)num; |
| 1024 | break; |
| 1025 | default: |
| 1026 | /* convert to seconds using multiplier */ |
| 1027 | dur->value.dur.sec += num * multi[seq]; |
| 1028 | seq++; |
| 1029 | break; |
| 1030 | } |
| 1031 | |
| 1032 | break; /* exit loop */ |
| 1033 | } |
| 1034 | /* no date designators found? */ |
| 1035 | if (++seq == 3) |
| 1036 | goto error; |
| 1037 | } |
| 1038 | cur++; |
| 1039 | } |
| 1040 | |
| 1041 | if (isneg) { |
| 1042 | dur->value.dur.mon = -dur->value.dur.mon; |
| 1043 | dur->value.dur.day = -dur->value.dur.day; |
| 1044 | dur->value.dur.sec = -dur->value.dur.sec; |
| 1045 | } |
| 1046 | |
| 1047 | if (val != NULL) |
| 1048 | *val = dur; |
| 1049 | |
| 1050 | return 0; |
| 1051 | |
| 1052 | error: |
| 1053 | if (dur != NULL) |
| 1054 | xmlSchemaFreeValue(dur); |
| 1055 | return 1; |
| 1056 | } |
| 1057 | |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1058 | |
| 1059 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame^] | 1060 | * xmlSchemaValPredefTypeNode: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1061 | * @type: the predefined type |
| 1062 | * @value: the value to check |
| 1063 | * @val: the return computed value |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame^] | 1064 | * @node: the node containing the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1065 | * |
| 1066 | * Check that a value conforms to the lexical space of the predefined type. |
| 1067 | * if true a value is computed and returned in @val. |
| 1068 | * |
| 1069 | * Returns 0 if this validates, a positive error code number otherwise |
| 1070 | * and -1 in case of internal or API error. |
| 1071 | */ |
| 1072 | int |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame^] | 1073 | xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 1074 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1075 | xmlSchemaValPtr v; |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1076 | int ret; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1077 | |
| 1078 | if (xmlSchemaTypesInitialized == 0) |
| 1079 | return(-1); |
| 1080 | if (type == NULL) |
| 1081 | return(-1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1082 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1083 | if (val != NULL) |
| 1084 | *val = NULL; |
| 1085 | if (type == xmlSchemaTypeStringDef) { |
| 1086 | return(0); |
| 1087 | } else if (type == xmlSchemaTypeAnyTypeDef) { |
| 1088 | return(0); |
| 1089 | } else if (type == xmlSchemaTypeAnySimpleTypeDef) { |
| 1090 | return(0); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 1091 | } else if (type == xmlSchemaTypeNmtokenDef) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1092 | if (xmlValidateNmtokenValue(value)) |
| 1093 | return(0); |
| 1094 | return(1); |
| 1095 | } else if (type == xmlSchemaTypeDecimalDef) { |
| 1096 | const xmlChar *cur = value, *tmp; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1097 | int frac = 0, len, neg = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1098 | unsigned long base = 0; |
| 1099 | if (cur == NULL) |
| 1100 | return(1); |
| 1101 | if (*cur == '+') |
| 1102 | cur++; |
| 1103 | else if (*cur == '-') { |
| 1104 | neg = 1; |
| 1105 | cur++; |
| 1106 | } |
| 1107 | tmp = cur; |
| 1108 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1109 | base = base * 10 + (*cur - '0'); |
| 1110 | cur++; |
| 1111 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1112 | len = cur - tmp; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1113 | if (*cur == '.') { |
| 1114 | cur++; |
| 1115 | tmp = cur; |
| 1116 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1117 | base = base * 10 + (*cur - '0'); |
| 1118 | cur++; |
| 1119 | } |
| 1120 | frac = cur - tmp; |
| 1121 | } |
| 1122 | if (*cur != 0) |
| 1123 | return(1); |
| 1124 | if (val != NULL) { |
| 1125 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 1126 | if (v != NULL) { |
| 1127 | v->value.decimal.base = base; |
| 1128 | v->value.decimal.sign = neg; |
| 1129 | v->value.decimal.frac = frac; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1130 | v->value.decimal.total = frac + len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1131 | *val = v; |
| 1132 | } |
| 1133 | } |
| 1134 | return(0); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1135 | } else if (type == xmlSchemaTypeDurationDef) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1136 | return xmlSchemaValidateDuration(type, value, val); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1137 | } else if ((type == xmlSchemaTypeDatetimeDef) || |
| 1138 | (type == xmlSchemaTypeTimeDef) || |
| 1139 | (type == xmlSchemaTypeDateDef) || |
| 1140 | (type == xmlSchemaTypeGYearDef) || |
| 1141 | (type == xmlSchemaTypeGYearMonthDef) || |
| 1142 | (type == xmlSchemaTypeGMonthDef) || |
| 1143 | (type == xmlSchemaTypeGMonthDayDef) || |
| 1144 | (type == xmlSchemaTypeGDayDef)) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1145 | return xmlSchemaValidateDates(type, value, val); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1146 | } else if (type == xmlSchemaTypePositiveIntegerDef) { |
| 1147 | const xmlChar *cur = value; |
| 1148 | unsigned long base = 0; |
| 1149 | int total = 0; |
| 1150 | if (cur == NULL) |
| 1151 | return(1); |
| 1152 | if (*cur == '+') |
| 1153 | cur++; |
| 1154 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1155 | base = base * 10 + (*cur - '0'); |
| 1156 | total++; |
| 1157 | cur++; |
| 1158 | } |
| 1159 | if (*cur != 0) |
| 1160 | return(1); |
| 1161 | if (val != NULL) { |
| 1162 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 1163 | if (v != NULL) { |
| 1164 | v->value.decimal.base = base; |
| 1165 | v->value.decimal.sign = 0; |
| 1166 | v->value.decimal.frac = 0; |
| 1167 | v->value.decimal.total = total; |
| 1168 | *val = v; |
| 1169 | } |
| 1170 | } |
| 1171 | return(0); |
| 1172 | } else if (type == xmlSchemaTypeNonNegativeIntegerDef) { |
| 1173 | const xmlChar *cur = value; |
| 1174 | unsigned long base = 0; |
| 1175 | int total = 0; |
| 1176 | int sign = 0; |
| 1177 | if (cur == NULL) |
| 1178 | return(1); |
| 1179 | if (*cur == '-') { |
| 1180 | sign = 1; |
| 1181 | cur++; |
| 1182 | } else if (*cur == '+') |
| 1183 | cur++; |
| 1184 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1185 | base = base * 10 + (*cur - '0'); |
| 1186 | total++; |
| 1187 | cur++; |
| 1188 | } |
| 1189 | if (*cur != 0) |
| 1190 | return(1); |
| 1191 | if ((sign == 1) && (base != 0)) |
| 1192 | return(1); |
| 1193 | if (val != NULL) { |
| 1194 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 1195 | if (v != NULL) { |
| 1196 | v->value.decimal.base = base; |
| 1197 | v->value.decimal.sign = 0; |
| 1198 | v->value.decimal.frac = 0; |
| 1199 | v->value.decimal.total = total; |
| 1200 | *val = v; |
| 1201 | } |
| 1202 | } |
| 1203 | return(0); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1204 | } else if (type == xmlSchemaTypeIntDef) { |
| 1205 | const xmlChar *cur = value; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1206 | unsigned long base = 0; |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1207 | int total = 0; |
| 1208 | int sign = 0; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1209 | if (cur == NULL) |
| 1210 | return(1); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1211 | if (*cur == '-') { |
| 1212 | sign = 1; |
| 1213 | cur++; |
| 1214 | } else if (*cur == '+') |
| 1215 | cur++; |
| 1216 | while (*cur == '0') { |
| 1217 | total++; |
| 1218 | cur++; |
| 1219 | } |
| 1220 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1221 | base = base * 10 + (*cur - '0'); |
| 1222 | total++; |
| 1223 | cur++; |
| 1224 | } |
| 1225 | if (*cur != 0) |
| 1226 | return(1); |
| 1227 | if ((sign == 1) && (total == 0)) |
| 1228 | return(1); |
| 1229 | if (val != NULL) { |
| 1230 | v = xmlSchemaNewValue(XML_SCHEMAS_INT); |
| 1231 | if (v != NULL) { |
| 1232 | v->value.decimal.base = base; |
| 1233 | v->value.decimal.sign = sign; |
| 1234 | v->value.decimal.frac = 0; |
| 1235 | v->value.decimal.total = total; |
| 1236 | *val = v; |
| 1237 | } |
| 1238 | } |
| 1239 | return(0); |
| 1240 | } else if ((type == xmlSchemaTypeFloatDef) || |
| 1241 | (type == xmlSchemaTypeDoubleDef)) { |
| 1242 | const xmlChar *cur = value; |
| 1243 | int neg = 0; |
| 1244 | if (cur == NULL) |
| 1245 | return(1); |
| 1246 | if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) { |
| 1247 | cur += 3; |
| 1248 | if (*cur != 0) |
| 1249 | return(1); |
| 1250 | if (val != NULL) { |
| 1251 | if (type == xmlSchemaTypeFloatDef) { |
| 1252 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 1253 | if (v != NULL) { |
| 1254 | v->value.f = (float) xmlXPathNAN; |
| 1255 | } else { |
| 1256 | xmlSchemaFreeValue(v); |
| 1257 | return(-1); |
| 1258 | } |
| 1259 | } else { |
| 1260 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 1261 | if (v != NULL) { |
| 1262 | v->value.d = xmlXPathNAN; |
| 1263 | } else { |
| 1264 | xmlSchemaFreeValue(v); |
| 1265 | return(-1); |
| 1266 | } |
| 1267 | } |
| 1268 | *val = v; |
| 1269 | } |
| 1270 | return(0); |
| 1271 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1272 | if (*cur == '+') |
| 1273 | cur++; |
| 1274 | else if (*cur == '-') { |
| 1275 | neg = 1; |
| 1276 | cur++; |
| 1277 | } |
Daniel Veillard | d431074 | 2003-02-18 21:12:46 +0000 | [diff] [blame] | 1278 | if (cur[0] == 0) |
| 1279 | return(1); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1280 | if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) { |
| 1281 | cur += 3; |
| 1282 | if (*cur != 0) |
| 1283 | return(1); |
| 1284 | if (val != NULL) { |
| 1285 | if (type == xmlSchemaTypeFloatDef) { |
| 1286 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 1287 | if (v != NULL) { |
| 1288 | if (neg) |
| 1289 | v->value.f = (float) xmlXPathNINF; |
| 1290 | else |
| 1291 | v->value.f = (float) xmlXPathPINF; |
| 1292 | } else { |
| 1293 | xmlSchemaFreeValue(v); |
| 1294 | return(-1); |
| 1295 | } |
| 1296 | } else { |
| 1297 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 1298 | if (v != NULL) { |
| 1299 | if (neg) |
| 1300 | v->value.d = xmlXPathNINF; |
| 1301 | else |
| 1302 | v->value.d = xmlXPathPINF; |
| 1303 | } else { |
| 1304 | xmlSchemaFreeValue(v); |
| 1305 | return(-1); |
| 1306 | } |
| 1307 | } |
| 1308 | *val = v; |
| 1309 | } |
| 1310 | return(0); |
| 1311 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1312 | while ((*cur >= '0') && (*cur <= '9')) { |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1313 | cur++; |
| 1314 | } |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1315 | if (*cur == '.') { |
| 1316 | cur++; |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1317 | while ((*cur >= '0') && (*cur <= '9')) |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1318 | cur++; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 1319 | } |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1320 | if ((*cur == 'e') || (*cur == 'E')) { |
| 1321 | cur++; |
| 1322 | if (*cur == '-') |
| 1323 | cur++; |
| 1324 | while ((*cur >= '0') && (*cur <= '9')) |
| 1325 | cur++; |
| 1326 | } |
| 1327 | if (*cur != 0) |
| 1328 | return(1); |
| 1329 | if (val != NULL) { |
| 1330 | if (type == xmlSchemaTypeFloatDef) { |
| 1331 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 1332 | if (v != NULL) { |
| 1333 | if (sscanf((const char *)value, "%f", &(v->value.f))==1) { |
| 1334 | *val = v; |
| 1335 | } else { |
| 1336 | xmlGenericError(xmlGenericErrorContext, |
| 1337 | "failed to scanf float %s\n", value); |
| 1338 | xmlSchemaFreeValue(v); |
| 1339 | return(1); |
| 1340 | } |
| 1341 | } else { |
| 1342 | return(-1); |
| 1343 | } |
| 1344 | } else { |
| 1345 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 1346 | if (v != NULL) { |
| 1347 | if (sscanf((const char *)value, "%lf", &(v->value.d))==1) { |
| 1348 | *val = v; |
| 1349 | } else { |
| 1350 | xmlGenericError(xmlGenericErrorContext, |
| 1351 | "failed to scanf double %s\n", value); |
| 1352 | xmlSchemaFreeValue(v); |
| 1353 | return(1); |
| 1354 | } |
| 1355 | } else { |
| 1356 | return(-1); |
| 1357 | } |
| 1358 | } |
| 1359 | } |
Daniel Veillard | b5c0573 | 2002-09-20 13:36:25 +0000 | [diff] [blame] | 1360 | return(0); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1361 | } else if (type == xmlSchemaTypeNameDef) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1362 | ret = xmlValidateName(value, 1); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1363 | if ((ret == 0) && (val != NULL)) { |
| 1364 | TODO; |
| 1365 | } |
| 1366 | return(ret); |
| 1367 | } else if (type == xmlSchemaTypeQNameDef) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1368 | ret = xmlValidateQName(value, 1); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1369 | if ((ret == 0) && (val != NULL)) { |
| 1370 | TODO; |
| 1371 | } |
| 1372 | return(ret); |
| 1373 | } else if (type == xmlSchemaTypeNCNameDef) { |
Daniel Veillard | d229879 | 2003-02-14 16:54:11 +0000 | [diff] [blame] | 1374 | ret = xmlValidateNCName(value, 1); |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1375 | if ((ret == 0) && (val != NULL)) { |
| 1376 | TODO; |
| 1377 | } |
| 1378 | return(ret); |
| 1379 | } else if (type == xmlSchemaTypeAnyURIDef) { |
| 1380 | xmlURIPtr uri; |
| 1381 | |
| 1382 | uri = xmlParseURI((const char *) value); |
| 1383 | if (uri == NULL) |
| 1384 | return(1); |
| 1385 | if (val != NULL) { |
| 1386 | TODO; |
| 1387 | } |
| 1388 | xmlFreeURI(uri); |
Daniel Veillard | b5c0573 | 2002-09-20 13:36:25 +0000 | [diff] [blame] | 1389 | return(0); |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 1390 | } else if (type == xmlSchemaTypeBooleanDef) { |
| 1391 | const xmlChar *cur = value; |
| 1392 | |
| 1393 | if ((cur[0] == '0') && (cur[1] == 0)) |
| 1394 | ret = 0; |
| 1395 | else if ((cur[0] == '1') && (cur[1] == 0)) |
| 1396 | ret = 1; |
| 1397 | else if ((cur[0] == 't') && (cur[1] == 'r') && (cur[2] == 'u') && |
| 1398 | (cur[3] == 'e') && (cur[4] == 0)) |
| 1399 | ret = 1; |
| 1400 | else if ((cur[0] == 'f') && (cur[1] == 'a') && (cur[2] == 'l') && |
| 1401 | (cur[3] == 's') && (cur[4] == 'e') && (cur[5] == 0)) |
| 1402 | ret = 0; |
| 1403 | else |
| 1404 | return(1); |
| 1405 | if (val != NULL) { |
| 1406 | v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN); |
| 1407 | if (v != NULL) { |
| 1408 | v->value.b = ret; |
| 1409 | *val = v; |
| 1410 | } else { |
| 1411 | return(-1); |
| 1412 | } |
| 1413 | } |
| 1414 | return(0); |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame^] | 1415 | } else if (type == xmlSchemaTypeIdrefDef) { |
| 1416 | ret = xmlValidateNCName(value, 1); |
| 1417 | if ((ret == 0) && (val != NULL)) { |
| 1418 | TODO; |
| 1419 | } |
| 1420 | if ((ret == 0) && (node != NULL) && |
| 1421 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 1422 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 1423 | |
| 1424 | xmlAddRef(NULL, node->doc, value, attr); |
| 1425 | attr->atype = XML_ATTRIBUTE_IDREF; |
| 1426 | } |
| 1427 | return(ret); |
| 1428 | } else if (type == xmlSchemaTypeIdDef) { |
| 1429 | ret = xmlValidateNCName(value, 1); |
| 1430 | if ((ret == 0) && (val != NULL)) { |
| 1431 | TODO; |
| 1432 | } |
| 1433 | if ((ret == 0) && (node != NULL) && |
| 1434 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 1435 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 1436 | xmlIDPtr res; |
| 1437 | |
| 1438 | res = xmlAddID(NULL, node->doc, value, attr); |
| 1439 | if (res == NULL) { |
| 1440 | ret = 2; |
| 1441 | } else { |
| 1442 | attr->atype = XML_ATTRIBUTE_ID; |
| 1443 | } |
| 1444 | } |
| 1445 | return(ret); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1446 | } else { |
| 1447 | TODO |
| 1448 | return(0); |
| 1449 | } |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1450 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame^] | 1454 | * xmlSchemaValidatePredefinedType: |
| 1455 | * @type: the predefined type |
| 1456 | * @value: the value to check |
| 1457 | * @val: the return computed value |
| 1458 | * |
| 1459 | * Check that a value conforms to the lexical space of the predefined type. |
| 1460 | * if true a value is computed and returned in @val. |
| 1461 | * |
| 1462 | * Returns 0 if this validates, a positive error code number otherwise |
| 1463 | * and -1 in case of internal or API error. |
| 1464 | */ |
| 1465 | int |
| 1466 | xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value, |
| 1467 | xmlSchemaValPtr *val) { |
| 1468 | return(xmlSchemaValPredefTypeNode(type, value, val, NULL)); |
| 1469 | } |
| 1470 | |
| 1471 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1472 | * xmlSchemaCompareDecimals: |
| 1473 | * @x: a first decimal value |
| 1474 | * @y: a second decimal value |
| 1475 | * |
| 1476 | * Compare 2 decimals |
| 1477 | * |
| 1478 | * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error |
| 1479 | */ |
| 1480 | static int |
| 1481 | xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 1482 | { |
| 1483 | xmlSchemaValPtr swp; |
| 1484 | int order = 1; |
| 1485 | unsigned long tmp; |
| 1486 | |
| 1487 | if ((x->value.decimal.sign) && (x->value.decimal.sign)) |
| 1488 | order = -1; |
| 1489 | else if (x->value.decimal.sign) |
| 1490 | return (-1); |
| 1491 | else if (y->value.decimal.sign) |
| 1492 | return (1); |
| 1493 | if (x->value.decimal.frac == y->value.decimal.frac) { |
| 1494 | if (x->value.decimal.base < y->value.decimal.base) |
| 1495 | return (-1); |
| 1496 | return (x->value.decimal.base > y->value.decimal.base); |
| 1497 | } |
| 1498 | if (y->value.decimal.frac > x->value.decimal.frac) { |
| 1499 | swp = y; |
| 1500 | y = x; |
| 1501 | x = swp; |
| 1502 | order = -order; |
| 1503 | } |
| 1504 | tmp = |
| 1505 | x->value.decimal.base / powten[x->value.decimal.frac - |
| 1506 | y->value.decimal.frac]; |
| 1507 | if (tmp > y->value.decimal.base) |
| 1508 | return (order); |
| 1509 | if (tmp < y->value.decimal.base) |
| 1510 | return (-order); |
| 1511 | tmp = |
| 1512 | y->value.decimal.base * powten[x->value.decimal.frac - |
| 1513 | y->value.decimal.frac]; |
| 1514 | if (x->value.decimal.base < tmp) |
| 1515 | return (-order); |
| 1516 | if (x->value.decimal.base == tmp) |
| 1517 | return (0); |
| 1518 | return (order); |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1522 | * xmlSchemaCompareDurations: |
| 1523 | * @x: a first duration value |
| 1524 | * @y: a second duration value |
| 1525 | * |
| 1526 | * Compare 2 durations |
| 1527 | * |
| 1528 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 1529 | * case of error |
| 1530 | */ |
| 1531 | static int |
| 1532 | xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 1533 | { |
| 1534 | long carry, mon, day; |
| 1535 | double sec; |
| 1536 | long xmon, xday, myear, lyear, minday, maxday; |
| 1537 | static const long dayRange [2][12] = { |
| 1538 | { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, }, |
| 1539 | { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} }; |
| 1540 | |
| 1541 | if ((x == NULL) || (y == NULL)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1542 | return -2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1543 | |
| 1544 | /* months */ |
| 1545 | mon = x->value.dur.mon - y->value.dur.mon; |
| 1546 | |
| 1547 | /* seconds */ |
| 1548 | sec = x->value.dur.sec - y->value.dur.sec; |
| 1549 | carry = (long)sec / SECS_PER_DAY; |
| 1550 | sec -= (double)(carry * SECS_PER_DAY); |
| 1551 | |
| 1552 | /* days */ |
| 1553 | day = x->value.dur.day - y->value.dur.day + carry; |
| 1554 | |
| 1555 | /* easy test */ |
| 1556 | if (mon == 0) { |
| 1557 | if (day == 0) |
| 1558 | if (sec == 0.0) |
| 1559 | return 0; |
| 1560 | else if (sec < 0.0) |
| 1561 | return -1; |
| 1562 | else |
| 1563 | return 1; |
| 1564 | else if (day < 0) |
| 1565 | return -1; |
| 1566 | else |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | if (mon > 0) { |
| 1571 | if ((day >= 0) && (sec >= 0.0)) |
| 1572 | return 1; |
| 1573 | else { |
| 1574 | xmon = mon; |
| 1575 | xday = -day; |
| 1576 | } |
| 1577 | } else if ((day <= 0) && (sec <= 0.0)) { |
| 1578 | return -1; |
| 1579 | } else { |
| 1580 | xmon = -mon; |
| 1581 | xday = day; |
| 1582 | } |
| 1583 | |
| 1584 | myear = xmon / 12; |
| 1585 | lyear = myear / 4; |
| 1586 | minday = (myear * 365) + (lyear != 0 ? lyear - 1 : 0); |
| 1587 | maxday = (myear * 365) + (lyear != 0 ? lyear + 1 : 0); |
| 1588 | |
| 1589 | xmon = xmon % 12; |
| 1590 | minday += dayRange[0][xmon]; |
| 1591 | maxday += dayRange[1][xmon]; |
| 1592 | |
| 1593 | if (maxday < xday) |
| 1594 | return 1; |
| 1595 | else if (minday > xday) |
| 1596 | return -1; |
| 1597 | |
| 1598 | /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1599 | return 2; |
| 1600 | } |
| 1601 | |
| 1602 | /* |
| 1603 | * macros for adding date/times and durations |
| 1604 | */ |
| 1605 | #define FQUOTIENT(a,b) (floor(((double)a/(double)b))) |
| 1606 | #define MODULO(a,b) (a - FQUOTIENT(a,b) * b) |
| 1607 | #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low))) |
| 1608 | #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low) |
| 1609 | |
| 1610 | /** |
| 1611 | * _xmlSchemaDateAdd: |
| 1612 | * @dt: an #xmlSchemaValPtr |
| 1613 | * @dur: an #xmlSchemaValPtr of type #XS_DURATION |
| 1614 | * |
| 1615 | * Compute a new date/time from @dt and @dur. This function assumes @dt |
| 1616 | * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH, |
| 1617 | * or #XML_SCHEMAS_GYEAR. |
| 1618 | * |
| 1619 | * Returns date/time pointer or NULL. |
| 1620 | */ |
| 1621 | static xmlSchemaValPtr |
| 1622 | _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur) |
| 1623 | { |
| 1624 | xmlSchemaValPtr ret; |
| 1625 | long carry, tempdays, temp; |
| 1626 | xmlSchemaValDatePtr r, d; |
| 1627 | xmlSchemaValDurationPtr u; |
| 1628 | |
| 1629 | if ((dt == NULL) || (dur == NULL)) |
| 1630 | return NULL; |
| 1631 | |
| 1632 | ret = xmlSchemaNewValue(dt->type); |
| 1633 | if (ret == NULL) |
| 1634 | return NULL; |
| 1635 | |
| 1636 | r = &(ret->value.date); |
| 1637 | d = &(dt->value.date); |
| 1638 | u = &(dur->value.dur); |
| 1639 | |
| 1640 | /* normalization */ |
| 1641 | if (d->mon == 0) |
| 1642 | d->mon = 1; |
| 1643 | |
| 1644 | /* normalize for time zone offset */ |
| 1645 | u->sec -= (d->tzo * 60); |
| 1646 | d->tzo = 0; |
| 1647 | |
| 1648 | /* normalization */ |
| 1649 | if (d->day == 0) |
| 1650 | d->day = 1; |
| 1651 | |
| 1652 | /* month */ |
| 1653 | carry = d->mon + u->mon; |
| 1654 | r->mon = MODULO_RANGE(carry, 1, 13); |
| 1655 | carry = FQUOTIENT_RANGE(carry, 1, 13); |
| 1656 | |
| 1657 | /* year (may be modified later) */ |
| 1658 | r->year = d->year + carry; |
| 1659 | if (r->year == 0) { |
| 1660 | if (d->year > 0) |
| 1661 | r->year--; |
| 1662 | else |
| 1663 | r->year++; |
| 1664 | } |
| 1665 | |
| 1666 | /* time zone */ |
| 1667 | r->tzo = d->tzo; |
| 1668 | r->tz_flag = d->tz_flag; |
| 1669 | |
| 1670 | /* seconds */ |
| 1671 | r->sec = d->sec + u->sec; |
| 1672 | carry = FQUOTIENT((long)r->sec, 60); |
| 1673 | if (r->sec != 0.0) { |
| 1674 | r->sec = MODULO(r->sec, 60.0); |
| 1675 | } |
| 1676 | |
| 1677 | /* minute */ |
| 1678 | carry += d->min; |
| 1679 | r->min = MODULO(carry, 60); |
| 1680 | carry = FQUOTIENT(carry, 60); |
| 1681 | |
| 1682 | /* hours */ |
| 1683 | carry += d->hour; |
| 1684 | r->hour = MODULO(carry, 24); |
| 1685 | carry = FQUOTIENT(carry, 24); |
| 1686 | |
| 1687 | /* |
| 1688 | * days |
| 1689 | * Note we use tempdays because the temporary values may need more |
| 1690 | * than 5 bits |
| 1691 | */ |
| 1692 | if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) && |
| 1693 | (d->day > MAX_DAYINMONTH(r->year, r->mon))) |
| 1694 | tempdays = MAX_DAYINMONTH(r->year, r->mon); |
| 1695 | else if (d->day < 1) |
| 1696 | tempdays = 1; |
| 1697 | else |
| 1698 | tempdays = d->day; |
| 1699 | |
| 1700 | tempdays += u->day + carry; |
| 1701 | |
| 1702 | while (1) { |
| 1703 | if (tempdays < 1) { |
| 1704 | long tmon = MODULO_RANGE(r->mon-1, 1, 13); |
| 1705 | long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13); |
| 1706 | if (tyr == 0) |
| 1707 | tyr--; |
| 1708 | tempdays += MAX_DAYINMONTH(tyr, tmon); |
| 1709 | carry = -1; |
| 1710 | } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) { |
| 1711 | tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon); |
| 1712 | carry = 1; |
| 1713 | } else |
| 1714 | break; |
| 1715 | |
| 1716 | temp = r->mon + carry; |
| 1717 | r->mon = MODULO_RANGE(temp, 1, 13); |
| 1718 | r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13); |
| 1719 | if (r->year == 0) { |
| 1720 | if (temp < 1) |
| 1721 | r->year--; |
| 1722 | else |
| 1723 | r->year++; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | r->day = tempdays; |
| 1728 | |
| 1729 | /* |
| 1730 | * adjust the date/time type to the date values |
| 1731 | */ |
| 1732 | if (ret->type != XML_SCHEMAS_DATETIME) { |
| 1733 | if ((r->hour) || (r->min) || (r->sec)) |
| 1734 | ret->type = XML_SCHEMAS_DATETIME; |
| 1735 | else if (ret->type != XML_SCHEMAS_DATE) { |
| 1736 | if ((r->mon != 1) && (r->day != 1)) |
| 1737 | ret->type = XML_SCHEMAS_DATE; |
| 1738 | else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1)) |
| 1739 | ret->type = XML_SCHEMAS_GYEARMONTH; |
| 1740 | } |
| 1741 | } |
| 1742 | |
| 1743 | return ret; |
| 1744 | } |
| 1745 | |
| 1746 | /** |
| 1747 | * xmlSchemaDupVal: |
| 1748 | * @v: value to duplicate |
| 1749 | * |
| 1750 | * returns a duplicated value. |
| 1751 | */ |
| 1752 | static xmlSchemaValPtr |
| 1753 | xmlSchemaDupVal (xmlSchemaValPtr v) |
| 1754 | { |
| 1755 | xmlSchemaValPtr ret = xmlSchemaNewValue(v->type); |
| 1756 | if (ret == NULL) |
| 1757 | return ret; |
| 1758 | |
| 1759 | memcpy(ret, v, sizeof(xmlSchemaVal)); |
| 1760 | return ret; |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * xmlSchemaDateNormalize: |
| 1765 | * @dt: an #xmlSchemaValPtr |
| 1766 | * |
| 1767 | * Normalize @dt to GMT time. |
| 1768 | * |
| 1769 | */ |
| 1770 | static xmlSchemaValPtr |
| 1771 | xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset) |
| 1772 | { |
| 1773 | xmlSchemaValPtr dur, ret; |
| 1774 | |
| 1775 | if (dt == NULL) |
| 1776 | return NULL; |
| 1777 | |
| 1778 | if (((dt->type != XML_SCHEMAS_TIME) && |
| 1779 | (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0)) |
| 1780 | return xmlSchemaDupVal(dt); |
| 1781 | |
| 1782 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 1783 | if (dur == NULL) |
| 1784 | return NULL; |
| 1785 | |
| 1786 | dur->value.date.sec -= offset; |
| 1787 | |
| 1788 | ret = _xmlSchemaDateAdd(dt, dur); |
| 1789 | if (ret == NULL) |
| 1790 | return NULL; |
| 1791 | |
| 1792 | xmlSchemaFreeValue(dur); |
| 1793 | |
| 1794 | /* ret->value.date.tzo = 0; */ |
| 1795 | return ret; |
| 1796 | } |
| 1797 | |
| 1798 | /** |
| 1799 | * _xmlSchemaDateCastYMToDays: |
| 1800 | * @dt: an #xmlSchemaValPtr |
| 1801 | * |
| 1802 | * Convert mon and year of @dt to total number of days. Take the |
| 1803 | * number of years since (or before) 1 AD and add the number of leap |
| 1804 | * years. This is a function because negative |
| 1805 | * years must be handled a little differently and there is no zero year. |
| 1806 | * |
| 1807 | * Returns number of days. |
| 1808 | */ |
| 1809 | static long |
| 1810 | _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt) |
| 1811 | { |
| 1812 | long ret; |
| 1813 | |
| 1814 | if (dt->value.date.year < 0) |
| 1815 | ret = (dt->value.date.year * 365) + |
| 1816 | (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+ |
| 1817 | ((dt->value.date.year+1)/400)) + |
| 1818 | DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year); |
| 1819 | else |
| 1820 | ret = ((dt->value.date.year-1) * 365) + |
| 1821 | (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+ |
| 1822 | ((dt->value.date.year-1)/400)) + |
| 1823 | DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year); |
| 1824 | |
| 1825 | return ret; |
| 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * TIME_TO_NUMBER: |
| 1830 | * @dt: an #xmlSchemaValPtr |
| 1831 | * |
| 1832 | * Calculates the number of seconds in the time portion of @dt. |
| 1833 | * |
| 1834 | * Returns seconds. |
| 1835 | */ |
| 1836 | #define TIME_TO_NUMBER(dt) \ |
| 1837 | ((double)((dt->value.date.hour * SECS_PER_HOUR) + \ |
| 1838 | (dt->value.date.min * SECS_PER_MIN)) + dt->value.date.sec) |
| 1839 | |
| 1840 | /** |
| 1841 | * xmlSchemaCompareDates: |
| 1842 | * @x: a first date/time value |
| 1843 | * @y: a second date/time value |
| 1844 | * |
| 1845 | * Compare 2 date/times |
| 1846 | * |
| 1847 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 1848 | * case of error |
| 1849 | */ |
| 1850 | static int |
| 1851 | xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 1852 | { |
| 1853 | unsigned char xmask, ymask, xor_mask, and_mask; |
| 1854 | xmlSchemaValPtr p1, p2, q1, q2; |
| 1855 | long p1d, p2d, q1d, q2d; |
| 1856 | |
| 1857 | if ((x == NULL) || (y == NULL)) |
| 1858 | return -2; |
| 1859 | |
| 1860 | if (x->value.date.tz_flag) { |
| 1861 | |
| 1862 | if (!y->value.date.tz_flag) { |
| 1863 | p1 = xmlSchemaDateNormalize(x, 0); |
| 1864 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 1865 | /* normalize y + 14:00 */ |
| 1866 | q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR)); |
| 1867 | |
| 1868 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1869 | if (p1d < q1d) { |
| 1870 | xmlSchemaFreeValue(p1); |
| 1871 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1872 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1873 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1874 | double sec; |
| 1875 | |
| 1876 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1877 | if (sec < 0.0) { |
| 1878 | xmlSchemaFreeValue(p1); |
| 1879 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1880 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1881 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1882 | /* normalize y - 14:00 */ |
| 1883 | q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR)); |
| 1884 | q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1885 | xmlSchemaFreeValue(p1); |
| 1886 | xmlSchemaFreeValue(q1); |
| 1887 | xmlSchemaFreeValue(q2); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1888 | if (p1d > q2d) |
| 1889 | return 1; |
| 1890 | else if (p1d == q2d) { |
| 1891 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2); |
| 1892 | if (sec > 0.0) |
| 1893 | return 1; |
| 1894 | else |
| 1895 | return 2; /* indeterminate */ |
| 1896 | } |
| 1897 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1898 | } else { |
| 1899 | xmlSchemaFreeValue(p1); |
| 1900 | xmlSchemaFreeValue(q1); |
| 1901 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1902 | } |
| 1903 | } else if (y->value.date.tz_flag) { |
| 1904 | q1 = xmlSchemaDateNormalize(y, 0); |
| 1905 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 1906 | |
| 1907 | /* normalize x - 14:00 */ |
| 1908 | p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR)); |
| 1909 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 1910 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1911 | if (p1d < q1d) { |
| 1912 | xmlSchemaFreeValue(p1); |
| 1913 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1914 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1915 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1916 | double sec; |
| 1917 | |
| 1918 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1919 | if (sec < 0.0) { |
| 1920 | xmlSchemaFreeValue(p1); |
| 1921 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1922 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1923 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1924 | /* normalize x + 14:00 */ |
| 1925 | p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR)); |
| 1926 | p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day; |
| 1927 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1928 | xmlSchemaFreeValue(p1); |
| 1929 | xmlSchemaFreeValue(q1); |
| 1930 | xmlSchemaFreeValue(p2); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1931 | if (p2d > q1d) |
| 1932 | return 1; |
| 1933 | else if (p2d == q1d) { |
| 1934 | sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1); |
| 1935 | if (sec > 0.0) |
| 1936 | return 1; |
| 1937 | else |
| 1938 | return 2; /* indeterminate */ |
| 1939 | } |
| 1940 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1941 | } else { |
| 1942 | xmlSchemaFreeValue(p1); |
| 1943 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | /* |
| 1948 | * if the same type then calculate the difference |
| 1949 | */ |
| 1950 | if (x->type == y->type) { |
| 1951 | q1 = xmlSchemaDateNormalize(y, 0); |
| 1952 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 1953 | |
| 1954 | p1 = xmlSchemaDateNormalize(x, 0); |
| 1955 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 1956 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1957 | if (p1d < q1d) { |
| 1958 | xmlSchemaFreeValue(p1); |
| 1959 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1960 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1961 | } else if (p1d > q1d) { |
| 1962 | xmlSchemaFreeValue(p1); |
| 1963 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1964 | return 1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1965 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1966 | double sec; |
| 1967 | |
| 1968 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 1969 | xmlSchemaFreeValue(p1); |
| 1970 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1971 | if (sec < 0.0) |
| 1972 | return -1; |
| 1973 | else if (sec > 0.0) |
| 1974 | return 1; |
| 1975 | |
| 1976 | } |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
| 1980 | switch (x->type) { |
| 1981 | case XML_SCHEMAS_DATETIME: |
| 1982 | xmask = 0xf; |
| 1983 | break; |
| 1984 | case XML_SCHEMAS_DATE: |
| 1985 | xmask = 0x7; |
| 1986 | break; |
| 1987 | case XML_SCHEMAS_GYEAR: |
| 1988 | xmask = 0x1; |
| 1989 | break; |
| 1990 | case XML_SCHEMAS_GMONTH: |
| 1991 | xmask = 0x2; |
| 1992 | break; |
| 1993 | case XML_SCHEMAS_GDAY: |
| 1994 | xmask = 0x3; |
| 1995 | break; |
| 1996 | case XML_SCHEMAS_GYEARMONTH: |
| 1997 | xmask = 0x3; |
| 1998 | break; |
| 1999 | case XML_SCHEMAS_GMONTHDAY: |
| 2000 | xmask = 0x6; |
| 2001 | break; |
| 2002 | case XML_SCHEMAS_TIME: |
| 2003 | xmask = 0x8; |
| 2004 | break; |
| 2005 | default: |
| 2006 | xmask = 0; |
| 2007 | break; |
| 2008 | } |
| 2009 | |
| 2010 | switch (y->type) { |
| 2011 | case XML_SCHEMAS_DATETIME: |
| 2012 | ymask = 0xf; |
| 2013 | break; |
| 2014 | case XML_SCHEMAS_DATE: |
| 2015 | ymask = 0x7; |
| 2016 | break; |
| 2017 | case XML_SCHEMAS_GYEAR: |
| 2018 | ymask = 0x1; |
| 2019 | break; |
| 2020 | case XML_SCHEMAS_GMONTH: |
| 2021 | ymask = 0x2; |
| 2022 | break; |
| 2023 | case XML_SCHEMAS_GDAY: |
| 2024 | ymask = 0x3; |
| 2025 | break; |
| 2026 | case XML_SCHEMAS_GYEARMONTH: |
| 2027 | ymask = 0x3; |
| 2028 | break; |
| 2029 | case XML_SCHEMAS_GMONTHDAY: |
| 2030 | ymask = 0x6; |
| 2031 | break; |
| 2032 | case XML_SCHEMAS_TIME: |
| 2033 | ymask = 0x8; |
| 2034 | break; |
| 2035 | default: |
| 2036 | ymask = 0; |
| 2037 | break; |
| 2038 | } |
| 2039 | |
| 2040 | xor_mask = xmask ^ ymask; /* mark type differences */ |
| 2041 | and_mask = xmask & ymask; /* mark field specification */ |
| 2042 | |
| 2043 | /* year */ |
| 2044 | if (xor_mask & 1) |
| 2045 | return 2; /* indeterminate */ |
| 2046 | else if (and_mask & 1) { |
| 2047 | if (x->value.date.year < y->value.date.year) |
| 2048 | return -1; |
| 2049 | else if (x->value.date.year > y->value.date.year) |
| 2050 | return 1; |
| 2051 | } |
| 2052 | |
| 2053 | /* month */ |
| 2054 | if (xor_mask & 2) |
| 2055 | return 2; /* indeterminate */ |
| 2056 | else if (and_mask & 2) { |
| 2057 | if (x->value.date.mon < y->value.date.mon) |
| 2058 | return -1; |
| 2059 | else if (x->value.date.mon > y->value.date.mon) |
| 2060 | return 1; |
| 2061 | } |
| 2062 | |
| 2063 | /* day */ |
| 2064 | if (xor_mask & 4) |
| 2065 | return 2; /* indeterminate */ |
| 2066 | else if (and_mask & 4) { |
| 2067 | if (x->value.date.day < y->value.date.day) |
| 2068 | return -1; |
| 2069 | else if (x->value.date.day > y->value.date.day) |
| 2070 | return 1; |
| 2071 | } |
| 2072 | |
| 2073 | /* time */ |
| 2074 | if (xor_mask & 8) |
| 2075 | return 2; /* indeterminate */ |
| 2076 | else if (and_mask & 8) { |
| 2077 | if (x->value.date.hour < y->value.date.hour) |
| 2078 | return -1; |
| 2079 | else if (x->value.date.hour > y->value.date.hour) |
| 2080 | return 1; |
| 2081 | else if (x->value.date.min < y->value.date.min) |
| 2082 | return -1; |
| 2083 | else if (x->value.date.min > y->value.date.min) |
| 2084 | return 1; |
| 2085 | else if (x->value.date.sec < y->value.date.sec) |
| 2086 | return -1; |
| 2087 | else if (x->value.date.sec > y->value.date.sec) |
| 2088 | return 1; |
| 2089 | } |
| 2090 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2095 | * xmlSchemaCompareValues: |
| 2096 | * @x: a first value |
| 2097 | * @y: a second value |
| 2098 | * |
| 2099 | * Compare 2 values |
| 2100 | * |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2101 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 2102 | * case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2103 | */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2104 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2105 | xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 2106 | if ((x == NULL) || (y == NULL)) |
| 2107 | return(-2); |
| 2108 | |
| 2109 | switch (x->type) { |
| 2110 | case XML_SCHEMAS_STRING: |
| 2111 | TODO |
| 2112 | case XML_SCHEMAS_DECIMAL: |
| 2113 | if (y->type == XML_SCHEMAS_DECIMAL) |
| 2114 | return(xmlSchemaCompareDecimals(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2115 | return(-2); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2116 | case XML_SCHEMAS_DURATION: |
| 2117 | if (y->type == XML_SCHEMAS_DURATION) |
| 2118 | return(xmlSchemaCompareDurations(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2119 | return(-2); |
| 2120 | case XML_SCHEMAS_TIME: |
| 2121 | case XML_SCHEMAS_GDAY: |
| 2122 | case XML_SCHEMAS_GMONTH: |
| 2123 | case XML_SCHEMAS_GMONTHDAY: |
| 2124 | case XML_SCHEMAS_GYEAR: |
| 2125 | case XML_SCHEMAS_GYEARMONTH: |
| 2126 | case XML_SCHEMAS_DATE: |
| 2127 | case XML_SCHEMAS_DATETIME: |
| 2128 | if ((y->type == XML_SCHEMAS_DATETIME) || |
| 2129 | (y->type == XML_SCHEMAS_TIME) || |
| 2130 | (y->type == XML_SCHEMAS_GDAY) || |
| 2131 | (y->type == XML_SCHEMAS_GMONTH) || |
| 2132 | (y->type == XML_SCHEMAS_GMONTHDAY) || |
| 2133 | (y->type == XML_SCHEMAS_GYEAR) || |
| 2134 | (y->type == XML_SCHEMAS_DATE) || |
| 2135 | (y->type == XML_SCHEMAS_GYEARMONTH)) |
| 2136 | return (xmlSchemaCompareDates(x, y)); |
| 2137 | |
| 2138 | return (-2); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2139 | default: |
| 2140 | TODO |
| 2141 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2142 | return -2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2143 | } |
| 2144 | |
| 2145 | /** |
| 2146 | * xmlSchemaValidateFacet: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2147 | * @base: the base type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2148 | * @facet: the facet to check |
| 2149 | * @value: the lexical repr of the value to validate |
| 2150 | * @val: the precomputed value |
| 2151 | * |
| 2152 | * Check a value against a facet condition |
| 2153 | * |
| 2154 | * Returns 0 if the element is schemas valid, a positive error code |
| 2155 | * number otherwise and -1 in case of internal or API error. |
| 2156 | */ |
| 2157 | int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 2158 | xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 2159 | xmlSchemaFacetPtr facet, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2160 | const xmlChar *value, xmlSchemaValPtr val) |
| 2161 | { |
| 2162 | int ret; |
| 2163 | |
| 2164 | switch (facet->type) { |
| 2165 | case XML_SCHEMA_FACET_PATTERN: |
| 2166 | ret = xmlRegexpExec(facet->regexp, value); |
| 2167 | if (ret == 1) |
| 2168 | return(0); |
| 2169 | if (ret == 0) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2170 | /* TODO error code */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2171 | return(1); |
| 2172 | } |
| 2173 | return(ret); |
| 2174 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 2175 | ret = xmlSchemaCompareValues(val, facet->val); |
| 2176 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2177 | /* TODO error code */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2178 | return(-1); |
| 2179 | } |
| 2180 | if (ret == -1) |
| 2181 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2182 | /* error code */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2183 | return(1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2184 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 2185 | ret = xmlSchemaCompareValues(val, facet->val); |
| 2186 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2187 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2188 | return(-1); |
| 2189 | } |
| 2190 | if ((ret == -1) || (ret == 0)) |
| 2191 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2192 | /* error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2193 | return(1); |
| 2194 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 2195 | ret = xmlSchemaCompareValues(val, facet->val); |
| 2196 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2197 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2198 | return(-1); |
| 2199 | } |
| 2200 | if (ret == 1) |
| 2201 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2202 | /* error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2203 | return(1); |
| 2204 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 2205 | ret = xmlSchemaCompareValues(val, facet->val); |
| 2206 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2207 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2208 | return(-1); |
| 2209 | } |
| 2210 | if ((ret == 1) || (ret == 0)) |
| 2211 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2212 | /* error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2213 | return(1); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 2214 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2215 | /* TODO whitespaces */ |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 2216 | return(0); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 2217 | case XML_SCHEMA_FACET_ENUMERATION: |
| 2218 | if ((facet->value != NULL) && |
| 2219 | (xmlStrEqual(facet->value, value))) |
| 2220 | return(0); |
| 2221 | return(1); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 2222 | case XML_SCHEMA_FACET_LENGTH: |
| 2223 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 2224 | case XML_SCHEMA_FACET_MINLENGTH: { |
| 2225 | unsigned int len = 0; |
| 2226 | |
| 2227 | if ((facet->val == NULL) || |
| 2228 | (facet->val->type != XML_SCHEMAS_DECIMAL) || |
| 2229 | (facet->val->value.decimal.frac != 0)) { |
| 2230 | return(-1); |
| 2231 | } |
| 2232 | switch (base->flags) { |
| 2233 | case XML_SCHEMAS_STRING: |
| 2234 | case XML_SCHEMAS_NORMSTRING: |
| 2235 | case XML_SCHEMAS_TOKEN: |
| 2236 | case XML_SCHEMAS_LANGUAGE: |
| 2237 | case XML_SCHEMAS_NMTOKEN: |
| 2238 | case XML_SCHEMAS_NAME: |
| 2239 | case XML_SCHEMAS_NCNAME: |
| 2240 | case XML_SCHEMAS_ID: |
| 2241 | case XML_SCHEMAS_IDREF: { |
| 2242 | len = xmlUTF8Strlen(value); |
| 2243 | break; |
| 2244 | } |
| 2245 | default: |
| 2246 | TODO |
| 2247 | } |
| 2248 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 2249 | if (len != facet->val->value.decimal.base) |
| 2250 | return(1); |
| 2251 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 2252 | if (len < facet->val->value.decimal.base) |
| 2253 | return(1); |
| 2254 | } else { |
| 2255 | if (len > facet->val->value.decimal.base) |
| 2256 | return(1); |
| 2257 | } |
| 2258 | break; |
| 2259 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2260 | default: |
| 2261 | TODO |
| 2262 | } |
| 2263 | return(0); |
| 2264 | } |
| 2265 | |
| 2266 | #endif /* LIBXML_SCHEMAS_ENABLED */ |