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