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 |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 31 | #ifdef HAVE_FLOAT_H |
| 32 | #include <float.h> |
| 33 | #endif |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 34 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 35 | #define DEBUG |
| 36 | |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 37 | #ifndef LIBXML_XPATH_ENABLED |
| 38 | extern double xmlXPathNAN; |
| 39 | extern double xmlXPathPINF; |
| 40 | extern double xmlXPathNINF; |
| 41 | #endif |
| 42 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 43 | #define TODO \ |
| 44 | xmlGenericError(xmlGenericErrorContext, \ |
| 45 | "Unimplemented block at %s:%d\n", \ |
| 46 | __FILE__, __LINE__); |
| 47 | |
| 48 | #define XML_SCHEMAS_NAMESPACE_NAME \ |
| 49 | (const xmlChar *)"http://www.w3.org/2001/XMLSchema" |
| 50 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 51 | #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \ |
| 52 | ((c) == 0xd)) |
| 53 | |
| 54 | #define IS_WSP_SPACE_CH(c) ((c) == 0x20) |
| 55 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 56 | #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c) |
| 57 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 58 | /* Date value */ |
| 59 | typedef struct _xmlSchemaValDate xmlSchemaValDate; |
| 60 | typedef xmlSchemaValDate *xmlSchemaValDatePtr; |
| 61 | struct _xmlSchemaValDate { |
| 62 | long year; |
| 63 | unsigned int mon :4; /* 1 <= mon <= 12 */ |
| 64 | unsigned int day :5; /* 1 <= day <= 31 */ |
| 65 | unsigned int hour :5; /* 0 <= hour <= 23 */ |
| 66 | unsigned int min :6; /* 0 <= min <= 59 */ |
| 67 | double sec; |
Daniel Veillard | a77cf71 | 2003-05-09 23:09:55 +0000 | [diff] [blame] | 68 | unsigned int tz_flag :1; /* is tzo explicitely set? */ |
Kasimier T. Buchcik | 285b367 | 2005-05-12 13:10:22 +0000 | [diff] [blame] | 69 | signed int tzo :12; /* -1440 <= tzo <= 1440; |
| 70 | currently only -840 to +840 are needed */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | /* Duration value */ |
| 74 | typedef struct _xmlSchemaValDuration xmlSchemaValDuration; |
| 75 | typedef xmlSchemaValDuration *xmlSchemaValDurationPtr; |
| 76 | struct _xmlSchemaValDuration { |
| 77 | long mon; /* mon stores years also */ |
| 78 | long day; |
| 79 | double sec; /* sec stores min and hour also */ |
| 80 | }; |
| 81 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 82 | typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal; |
| 83 | typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr; |
| 84 | struct _xmlSchemaValDecimal { |
| 85 | /* would use long long but not portable */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 86 | unsigned long lo; |
| 87 | unsigned long mi; |
| 88 | unsigned long hi; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 89 | unsigned int extra; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 90 | unsigned int sign:1; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 91 | unsigned int frac:7; |
| 92 | unsigned int total:8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 95 | typedef struct _xmlSchemaValQName xmlSchemaValQName; |
| 96 | typedef xmlSchemaValQName *xmlSchemaValQNamePtr; |
| 97 | struct _xmlSchemaValQName { |
| 98 | xmlChar *name; |
| 99 | xmlChar *uri; |
| 100 | }; |
| 101 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 102 | typedef struct _xmlSchemaValHex xmlSchemaValHex; |
| 103 | typedef xmlSchemaValHex *xmlSchemaValHexPtr; |
| 104 | struct _xmlSchemaValHex { |
| 105 | xmlChar *str; |
| 106 | unsigned int total; |
| 107 | }; |
| 108 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 109 | typedef struct _xmlSchemaValBase64 xmlSchemaValBase64; |
| 110 | typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr; |
| 111 | struct _xmlSchemaValBase64 { |
| 112 | xmlChar *str; |
| 113 | unsigned int total; |
| 114 | }; |
| 115 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 116 | struct _xmlSchemaVal { |
| 117 | xmlSchemaValType type; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 118 | struct _xmlSchemaVal *next; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 119 | union { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 120 | xmlSchemaValDecimal decimal; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 121 | xmlSchemaValDate date; |
| 122 | xmlSchemaValDuration dur; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 123 | xmlSchemaValQName qname; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 124 | xmlSchemaValHex hex; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 125 | xmlSchemaValBase64 base64; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 126 | float f; |
| 127 | double d; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 128 | int b; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 129 | xmlChar *str; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 130 | } value; |
| 131 | }; |
| 132 | |
| 133 | static int xmlSchemaTypesInitialized = 0; |
| 134 | static xmlHashTablePtr xmlSchemaTypesBank = NULL; |
| 135 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 136 | /* |
| 137 | * Basic types |
| 138 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 139 | static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL; |
| 140 | static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL; |
| 141 | static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL; |
| 142 | static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 143 | static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 144 | static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 145 | static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL; |
| 146 | static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL; |
| 147 | static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL; |
| 148 | static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL; |
| 149 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL; |
| 150 | static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL; |
| 151 | static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 152 | static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL; |
Daniel Veillard | c5a70f2 | 2003-02-06 23:41:59 +0000 | [diff] [blame] | 153 | static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL; |
Daniel Veillard | 84d70a4 | 2002-09-16 10:51:38 +0000 | [diff] [blame] | 154 | static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 155 | static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 156 | static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 157 | static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 160 | * Derived types |
| 161 | */ |
| 162 | static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL; |
| 163 | static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL; |
| 164 | static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL; |
| 165 | static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL; |
| 166 | static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL; |
| 167 | static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL; |
| 168 | static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL; |
| 169 | static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL; |
| 170 | static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL; |
| 171 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL; |
| 172 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL; |
| 173 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL; |
| 174 | static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 175 | static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL; |
| 176 | static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL; |
| 177 | static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL; |
| 178 | static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL; |
| 179 | static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL; |
Daniel Veillard | e5b110b | 2003-02-04 14:43:39 +0000 | [diff] [blame] | 180 | static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 181 | static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL; |
| 182 | static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL; |
| 183 | static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 184 | static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL; |
| 185 | static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 186 | static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL; |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 187 | static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL; |
| 188 | static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL; |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 189 | |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 190 | /************************************************************************ |
| 191 | * * |
| 192 | * Datatype error handlers * |
| 193 | * * |
| 194 | ************************************************************************/ |
| 195 | /** |
| 196 | * xmlSchemaTypeErrMemory: |
| 197 | * @extra: extra informations |
| 198 | * |
| 199 | * Handle an out of memory condition |
| 200 | */ |
| 201 | static void |
| 202 | xmlSchemaTypeErrMemory(xmlNodePtr node, const char *extra) |
| 203 | { |
| 204 | __xmlSimpleError(XML_FROM_DATATYPE, XML_ERR_NO_MEMORY, node, NULL, extra); |
| 205 | } |
| 206 | |
| 207 | /************************************************************************ |
| 208 | * * |
| 209 | * Base types support * |
| 210 | * * |
| 211 | ************************************************************************/ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 212 | |
| 213 | /** |
| 214 | * xmlSchemaNewValue: |
| 215 | * @type: the value type |
| 216 | * |
| 217 | * Allocate a new simple type value |
| 218 | * |
| 219 | * Returns a pointer to the new value or NULL in case of error |
| 220 | */ |
| 221 | static xmlSchemaValPtr |
| 222 | xmlSchemaNewValue(xmlSchemaValType type) { |
| 223 | xmlSchemaValPtr value; |
| 224 | |
| 225 | value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 226 | if (value == NULL) { |
| 227 | return(NULL); |
| 228 | } |
| 229 | memset(value, 0, sizeof(xmlSchemaVal)); |
| 230 | value->type = type; |
| 231 | return(value); |
| 232 | } |
| 233 | |
| 234 | static xmlSchemaFacetPtr |
| 235 | xmlSchemaNewMinLengthFacet(int value) |
| 236 | { |
| 237 | xmlSchemaFacetPtr ret; |
| 238 | |
| 239 | ret = xmlSchemaNewFacet(); |
| 240 | ret->type = XML_SCHEMA_FACET_MINLENGTH; |
| 241 | ret->val = xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER); |
| 242 | ret->val->value.decimal.lo = value; |
| 243 | return (ret); |
| 244 | } |
| 245 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 246 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 247 | * xmlSchemaInitBasicType: |
| 248 | * @name: the type name |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 249 | * @type: the value type associated |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 250 | * |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 251 | * Initialize one primitive built-in type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 252 | */ |
| 253 | static xmlSchemaTypePtr |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 254 | xmlSchemaInitBasicType(const char *name, xmlSchemaValType type, |
| 255 | xmlSchemaTypePtr baseType) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 256 | xmlSchemaTypePtr ret; |
| 257 | |
| 258 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 259 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 260 | xmlSchemaTypeErrMemory(NULL, "could not initialize basic types"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 261 | return(NULL); |
| 262 | } |
| 263 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 264 | ret->name = (const xmlChar *)name; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 265 | ret->targetNamespace = XML_SCHEMAS_NAMESPACE_NAME; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 266 | ret->type = XML_SCHEMA_TYPE_BASIC; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 267 | ret->baseType = baseType; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 268 | ret->contentType = XML_SCHEMA_CONTENT_BASIC; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 269 | /* |
| 270 | * Primitive types. |
| 271 | */ |
| 272 | switch (type) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 273 | case XML_SCHEMAS_STRING: |
| 274 | case XML_SCHEMAS_DECIMAL: |
| 275 | case XML_SCHEMAS_DATE: |
| 276 | case XML_SCHEMAS_DATETIME: |
| 277 | case XML_SCHEMAS_TIME: |
| 278 | case XML_SCHEMAS_GYEAR: |
| 279 | case XML_SCHEMAS_GYEARMONTH: |
| 280 | case XML_SCHEMAS_GMONTH: |
| 281 | case XML_SCHEMAS_GMONTHDAY: |
| 282 | case XML_SCHEMAS_GDAY: |
| 283 | case XML_SCHEMAS_DURATION: |
| 284 | case XML_SCHEMAS_FLOAT: |
| 285 | case XML_SCHEMAS_DOUBLE: |
| 286 | case XML_SCHEMAS_BOOLEAN: |
| 287 | case XML_SCHEMAS_ANYURI: |
| 288 | case XML_SCHEMAS_HEXBINARY: |
| 289 | case XML_SCHEMAS_BASE64BINARY: |
| 290 | case XML_SCHEMAS_QNAME: |
| 291 | case XML_SCHEMAS_NOTATION: |
| 292 | ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 293 | break; |
William M. Brack | 96d2eff | 2004-06-30 11:48:47 +0000 | [diff] [blame] | 294 | default: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 295 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 296 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 297 | /* |
| 298 | * Set variety. |
| 299 | */ |
| 300 | switch (type) { |
| 301 | case XML_SCHEMAS_ANYTYPE: |
| 302 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 303 | break; |
| 304 | case XML_SCHEMAS_IDREFS: |
| 305 | case XML_SCHEMAS_NMTOKENS: |
| 306 | case XML_SCHEMAS_ENTITIES: |
| 307 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST; |
| 308 | ret->facets = xmlSchemaNewMinLengthFacet(1); |
| 309 | ret->flags |= XML_SCHEMAS_TYPE_HAS_FACETS; |
| 310 | break; |
| 311 | default: |
| 312 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC; |
| 313 | break; |
| 314 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 315 | xmlHashAddEntry2(xmlSchemaTypesBank, ret->name, |
| 316 | XML_SCHEMAS_NAMESPACE_NAME, ret); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 317 | ret->builtInType = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 318 | return(ret); |
| 319 | } |
| 320 | |
| 321 | /* |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 322 | * WARNING: Those type reside normally in xmlschemas.c but are |
| 323 | * redefined here locally in oder of being able to use them for xs:anyType- |
| 324 | * TODO: Remove those definition if we move the types to a header file. |
| 325 | * TODO: Always keep those structs up-to-date with the originals. |
| 326 | */ |
| 327 | #define UNBOUNDED (1 << 30) |
| 328 | |
| 329 | typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem; |
| 330 | typedef xmlSchemaTreeItem *xmlSchemaTreeItemPtr; |
| 331 | struct _xmlSchemaTreeItem { |
| 332 | xmlSchemaTypeType type; |
| 333 | xmlSchemaAnnotPtr annot; |
| 334 | xmlSchemaTreeItemPtr next; |
| 335 | xmlSchemaTreeItemPtr children; |
| 336 | }; |
| 337 | |
| 338 | typedef struct _xmlSchemaParticle xmlSchemaParticle; |
| 339 | typedef xmlSchemaParticle *xmlSchemaParticlePtr; |
| 340 | struct _xmlSchemaParticle { |
| 341 | xmlSchemaTypeType type; |
| 342 | xmlSchemaAnnotPtr annot; |
| 343 | xmlSchemaTreeItemPtr next; |
| 344 | xmlSchemaTreeItemPtr children; |
| 345 | int minOccurs; |
| 346 | int maxOccurs; |
| 347 | xmlNodePtr node; |
| 348 | }; |
| 349 | |
| 350 | typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup; |
| 351 | typedef xmlSchemaModelGroup *xmlSchemaModelGroupPtr; |
| 352 | struct _xmlSchemaModelGroup { |
| 353 | xmlSchemaTypeType type; |
| 354 | xmlSchemaAnnotPtr annot; |
| 355 | xmlSchemaTreeItemPtr next; |
| 356 | xmlSchemaTreeItemPtr children; |
| 357 | xmlNodePtr node; |
| 358 | }; |
| 359 | |
| 360 | static xmlSchemaParticlePtr |
| 361 | xmlSchemaAddParticle(void) |
| 362 | { |
| 363 | xmlSchemaParticlePtr ret = NULL; |
| 364 | |
| 365 | ret = (xmlSchemaParticlePtr) |
| 366 | xmlMalloc(sizeof(xmlSchemaParticle)); |
| 367 | if (ret == NULL) { |
| 368 | xmlSchemaTypeErrMemory(NULL, "allocating particle component"); |
| 369 | return (NULL); |
| 370 | } |
| 371 | memset(ret, 0, sizeof(xmlSchemaParticle)); |
| 372 | ret->type = XML_SCHEMA_TYPE_PARTICLE; |
| 373 | ret->minOccurs = 1; |
| 374 | ret->maxOccurs = 1; |
| 375 | return (ret); |
| 376 | } |
| 377 | |
| 378 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 379 | * xmlSchemaInitTypes: |
| 380 | * |
| 381 | * Initialize the default XML Schemas type library |
| 382 | */ |
| 383 | void |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 384 | xmlSchemaInitTypes(void) |
| 385 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 386 | if (xmlSchemaTypesInitialized != 0) |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 387 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 388 | xmlSchemaTypesBank = xmlHashCreate(40); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 389 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 390 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 391 | /* |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 392 | * 3.4.7 Built-in Complex Type Definition |
| 393 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 394 | xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType", |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 395 | XML_SCHEMAS_ANYTYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 396 | NULL); |
| 397 | xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef; |
| 398 | xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 399 | /* |
| 400 | * Init the content type. |
| 401 | */ |
| 402 | xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 403 | { |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 404 | xmlSchemaParticlePtr particle; |
| 405 | xmlSchemaModelGroupPtr sequence; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 406 | xmlSchemaWildcardPtr wild; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 407 | /* First particle. */ |
| 408 | particle = xmlSchemaAddParticle(); |
| 409 | if (particle == NULL) |
| 410 | return; |
| 411 | xmlSchemaTypeAnyTypeDef->subtypes = (xmlSchemaTypePtr) particle; |
| 412 | /* Sequence model group. */ |
| 413 | sequence = (xmlSchemaModelGroupPtr) |
| 414 | xmlMalloc(sizeof(xmlSchemaModelGroup)); |
| 415 | if (sequence == NULL) { |
| 416 | xmlSchemaTypeErrMemory(NULL, "allocating model group component"); |
| 417 | return; |
| 418 | } |
| 419 | memset(sequence, 0, sizeof(xmlSchemaModelGroup)); |
| 420 | sequence->type = XML_SCHEMA_TYPE_SEQUENCE; |
| 421 | particle->children = (xmlSchemaTreeItemPtr) sequence; |
| 422 | /* Second particle. */ |
| 423 | particle = xmlSchemaAddParticle(); |
| 424 | if (particle == NULL) |
| 425 | return; |
| 426 | particle->minOccurs = 0; |
| 427 | particle->maxOccurs = UNBOUNDED; |
| 428 | sequence->children = (xmlSchemaTreeItemPtr) particle; |
| 429 | /* The wildcard */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 430 | wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard)); |
| 431 | if (wild == NULL) { |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 432 | xmlSchemaTypeErrMemory(NULL, "allocating wildcard component"); |
| 433 | return; |
| 434 | } |
| 435 | memset(wild, 0, sizeof(xmlSchemaWildcard)); |
| 436 | wild->type = XML_SCHEMA_TYPE_ANY; |
Kasimier T. Buchcik | 69dea3a | 2005-11-07 14:02:44 +0000 | [diff] [blame] | 437 | wild->any = 1; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 438 | wild->processContents = XML_SCHEMAS_ANY_LAX; |
| 439 | particle->children = (xmlSchemaTreeItemPtr) wild; |
| 440 | /* |
| 441 | * Create the attribute wildcard. |
| 442 | */ |
| 443 | wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard)); |
| 444 | if (wild == NULL) { |
| 445 | xmlSchemaTypeErrMemory(NULL, "could not create an attribute " |
| 446 | "wildcard on anyType"); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | memset(wild, 0, sizeof(xmlSchemaWildcard)); |
| 450 | wild->any = 1; |
Kasimier T. Buchcik | 69dea3a | 2005-11-07 14:02:44 +0000 | [diff] [blame] | 451 | wild->processContents = XML_SCHEMAS_ANY_LAX; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 452 | xmlSchemaTypeAnyTypeDef->attributeWildcard = wild; |
| 453 | } |
| 454 | xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType", |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 455 | XML_SCHEMAS_ANYSIMPLETYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 456 | xmlSchemaTypeAnyTypeDef); |
| 457 | /* |
| 458 | * primitive datatypes |
| 459 | */ |
| 460 | xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string", |
| 461 | XML_SCHEMAS_STRING, |
| 462 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 463 | xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 464 | XML_SCHEMAS_DECIMAL, |
| 465 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 466 | xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 467 | XML_SCHEMAS_DATE, |
| 468 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 469 | xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 470 | XML_SCHEMAS_DATETIME, |
| 471 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 472 | xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 473 | XML_SCHEMAS_TIME, |
| 474 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 475 | xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 476 | XML_SCHEMAS_GYEAR, |
| 477 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 478 | xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 479 | XML_SCHEMAS_GYEARMONTH, |
| 480 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 481 | xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 482 | XML_SCHEMAS_GMONTH, |
| 483 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 484 | xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 485 | XML_SCHEMAS_GMONTHDAY, |
| 486 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 487 | xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 488 | XML_SCHEMAS_GDAY, |
| 489 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 490 | xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 491 | XML_SCHEMAS_DURATION, |
| 492 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 493 | xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 494 | XML_SCHEMAS_FLOAT, |
| 495 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 496 | xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 497 | XML_SCHEMAS_DOUBLE, |
| 498 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 499 | xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 500 | XML_SCHEMAS_BOOLEAN, |
| 501 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 502 | xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 503 | XML_SCHEMAS_ANYURI, |
| 504 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 505 | xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 506 | XML_SCHEMAS_HEXBINARY, |
| 507 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 508 | xmlSchemaTypeBase64BinaryDef |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 509 | = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY, |
| 510 | xmlSchemaTypeAnySimpleTypeDef); |
| 511 | xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION", |
| 512 | XML_SCHEMAS_NOTATION, |
| 513 | xmlSchemaTypeAnySimpleTypeDef); |
| 514 | xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName", |
| 515 | XML_SCHEMAS_QNAME, |
| 516 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 517 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 518 | /* |
| 519 | * derived datatypes |
| 520 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 521 | xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 522 | XML_SCHEMAS_INTEGER, |
| 523 | xmlSchemaTypeDecimalDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 524 | xmlSchemaTypeNonPositiveIntegerDef = |
| 525 | xmlSchemaInitBasicType("nonPositiveInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 526 | XML_SCHEMAS_NPINTEGER, |
| 527 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 528 | xmlSchemaTypeNegativeIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 529 | xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER, |
| 530 | xmlSchemaTypeNonPositiveIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 531 | xmlSchemaTypeLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 532 | xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG, |
| 533 | xmlSchemaTypeIntegerDef); |
| 534 | xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT, |
| 535 | xmlSchemaTypeLongDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 536 | xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 537 | XML_SCHEMAS_SHORT, |
| 538 | xmlSchemaTypeIntDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 539 | xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 540 | XML_SCHEMAS_BYTE, |
| 541 | xmlSchemaTypeShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 542 | xmlSchemaTypeNonNegativeIntegerDef = |
| 543 | xmlSchemaInitBasicType("nonNegativeInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 544 | XML_SCHEMAS_NNINTEGER, |
| 545 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 546 | xmlSchemaTypeUnsignedLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 547 | xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG, |
| 548 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 549 | xmlSchemaTypeUnsignedIntDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 550 | xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT, |
| 551 | xmlSchemaTypeUnsignedLongDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 552 | xmlSchemaTypeUnsignedShortDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 553 | xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT, |
| 554 | xmlSchemaTypeUnsignedIntDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 555 | xmlSchemaTypeUnsignedByteDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 556 | xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE, |
| 557 | xmlSchemaTypeUnsignedShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 558 | xmlSchemaTypePositiveIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 559 | xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER, |
| 560 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 561 | xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 562 | XML_SCHEMAS_NORMSTRING, |
| 563 | xmlSchemaTypeStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 564 | xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 565 | XML_SCHEMAS_TOKEN, |
| 566 | xmlSchemaTypeNormStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 567 | xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 568 | XML_SCHEMAS_LANGUAGE, |
| 569 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 570 | xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 571 | XML_SCHEMAS_NAME, |
| 572 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 573 | xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 574 | XML_SCHEMAS_NMTOKEN, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 575 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 576 | xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName", |
| 577 | XML_SCHEMAS_NCNAME, |
| 578 | xmlSchemaTypeNameDef); |
| 579 | xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 580 | xmlSchemaTypeNCNameDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 581 | xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF", |
| 582 | XML_SCHEMAS_IDREF, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 583 | xmlSchemaTypeNCNameDef); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 584 | xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY", |
| 585 | XML_SCHEMAS_ENTITY, |
| 586 | xmlSchemaTypeNCNameDef); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 587 | /* |
| 588 | * Derived list types. |
| 589 | */ |
| 590 | /* ENTITIES */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 591 | xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES", |
| 592 | XML_SCHEMAS_ENTITIES, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 593 | xmlSchemaTypeAnySimpleTypeDef); |
| 594 | xmlSchemaTypeEntitiesDef->subtypes = xmlSchemaTypeEntityDef; |
| 595 | /* IDREFS */ |
| 596 | xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS", |
| 597 | XML_SCHEMAS_IDREFS, |
| 598 | xmlSchemaTypeAnySimpleTypeDef); |
| 599 | xmlSchemaTypeIdrefsDef->subtypes = xmlSchemaTypeIdrefDef; |
| 600 | |
| 601 | /* NMTOKENS */ |
| 602 | xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS", |
| 603 | XML_SCHEMAS_NMTOKENS, |
| 604 | xmlSchemaTypeAnySimpleTypeDef); |
| 605 | xmlSchemaTypeNmtokensDef->subtypes = xmlSchemaTypeNmtokenDef; |
| 606 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 607 | xmlSchemaTypesInitialized = 1; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * xmlSchemaCleanupTypes: |
| 612 | * |
| 613 | * Cleanup the default XML Schemas type library |
| 614 | */ |
| 615 | void |
| 616 | xmlSchemaCleanupTypes(void) { |
| 617 | if (xmlSchemaTypesInitialized == 0) |
| 618 | return; |
Kasimier T. Buchcik | 11162b7 | 2005-07-28 00:50:22 +0000 | [diff] [blame] | 619 | /* |
| 620 | * Free xs:anyType. |
| 621 | */ |
| 622 | { |
| 623 | xmlSchemaParticlePtr particle; |
| 624 | /* Attribute wildcard. */ |
| 625 | xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard); |
| 626 | /* Content type. */ |
| 627 | particle = (xmlSchemaParticlePtr) xmlSchemaTypeAnyTypeDef->subtypes; |
| 628 | /* Wildcard. */ |
| 629 | xmlSchemaFreeWildcard((xmlSchemaWildcardPtr) |
| 630 | particle->children->children->children); |
| 631 | xmlFree((xmlSchemaParticlePtr) particle->children->children); |
| 632 | /* Sequence model group. */ |
| 633 | xmlFree((xmlSchemaModelGroupPtr) particle->children); |
| 634 | xmlFree((xmlSchemaParticlePtr) particle); |
| 635 | xmlSchemaTypeAnyTypeDef->subtypes = NULL; |
| 636 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 637 | xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType); |
| 638 | xmlSchemaTypesInitialized = 0; |
| 639 | } |
| 640 | |
| 641 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 642 | * xmlSchemaIsBuiltInTypeFacet: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 643 | * @type: the built-in type |
| 644 | * @facetType: the facet type |
| 645 | * |
| 646 | * Evaluates if a specific facet can be |
| 647 | * used in conjunction with a type. |
| 648 | * |
| 649 | * Returns 1 if the facet can be used with the given built-in type, |
| 650 | * 0 otherwise and -1 in case the type is not a built-in type. |
| 651 | */ |
| 652 | int |
| 653 | xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType) |
| 654 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 655 | if (type == NULL) |
| 656 | return (-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 657 | if (type->type != XML_SCHEMA_TYPE_BASIC) |
| 658 | return (-1); |
| 659 | switch (type->builtInType) { |
| 660 | case XML_SCHEMAS_BOOLEAN: |
| 661 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 662 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 663 | return (1); |
| 664 | else |
| 665 | return (0); |
| 666 | case XML_SCHEMAS_STRING: |
| 667 | case XML_SCHEMAS_NOTATION: |
| 668 | case XML_SCHEMAS_QNAME: |
| 669 | case XML_SCHEMAS_ANYURI: |
| 670 | case XML_SCHEMAS_BASE64BINARY: |
| 671 | case XML_SCHEMAS_HEXBINARY: |
| 672 | if ((facetType == XML_SCHEMA_FACET_LENGTH) || |
| 673 | (facetType == XML_SCHEMA_FACET_MINLENGTH) || |
| 674 | (facetType == XML_SCHEMA_FACET_MAXLENGTH) || |
| 675 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 676 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 677 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 678 | return (1); |
| 679 | else |
| 680 | return (0); |
| 681 | case XML_SCHEMAS_DECIMAL: |
| 682 | if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) || |
| 683 | (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) || |
| 684 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 685 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 686 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 687 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 688 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 689 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 690 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 691 | return (1); |
| 692 | else |
| 693 | return (0); |
| 694 | case XML_SCHEMAS_TIME: |
| 695 | case XML_SCHEMAS_GDAY: |
| 696 | case XML_SCHEMAS_GMONTH: |
| 697 | case XML_SCHEMAS_GMONTHDAY: |
| 698 | case XML_SCHEMAS_GYEAR: |
| 699 | case XML_SCHEMAS_GYEARMONTH: |
| 700 | case XML_SCHEMAS_DATE: |
| 701 | case XML_SCHEMAS_DATETIME: |
| 702 | case XML_SCHEMAS_DURATION: |
| 703 | case XML_SCHEMAS_FLOAT: |
| 704 | case XML_SCHEMAS_DOUBLE: |
| 705 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 706 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 707 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 708 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 709 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 710 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 711 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 712 | return (1); |
| 713 | else |
| 714 | return (0); |
| 715 | default: |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 716 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 717 | } |
| 718 | return (0); |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * xmlSchemaGetBuiltInType: |
| 723 | * @type: the type of the built in type |
| 724 | * |
| 725 | * Gives you the type struct for a built-in |
| 726 | * type by its type id. |
| 727 | * |
| 728 | * Returns the type if found, NULL otherwise. |
| 729 | */ |
| 730 | xmlSchemaTypePtr |
| 731 | xmlSchemaGetBuiltInType(xmlSchemaValType type) |
| 732 | { |
| 733 | if (xmlSchemaTypesInitialized == 0) |
| 734 | xmlSchemaInitTypes(); |
| 735 | switch (type) { |
| 736 | |
| 737 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 738 | return (xmlSchemaTypeAnySimpleTypeDef); |
| 739 | case XML_SCHEMAS_STRING: |
| 740 | return (xmlSchemaTypeStringDef); |
| 741 | case XML_SCHEMAS_NORMSTRING: |
| 742 | return (xmlSchemaTypeNormStringDef); |
| 743 | case XML_SCHEMAS_DECIMAL: |
| 744 | return (xmlSchemaTypeDecimalDef); |
| 745 | case XML_SCHEMAS_TIME: |
| 746 | return (xmlSchemaTypeTimeDef); |
| 747 | case XML_SCHEMAS_GDAY: |
| 748 | return (xmlSchemaTypeGDayDef); |
| 749 | case XML_SCHEMAS_GMONTH: |
| 750 | return (xmlSchemaTypeGMonthDef); |
| 751 | case XML_SCHEMAS_GMONTHDAY: |
| 752 | return (xmlSchemaTypeGMonthDayDef); |
| 753 | case XML_SCHEMAS_GYEAR: |
| 754 | return (xmlSchemaTypeGYearDef); |
| 755 | case XML_SCHEMAS_GYEARMONTH: |
| 756 | return (xmlSchemaTypeGYearMonthDef); |
| 757 | case XML_SCHEMAS_DATE: |
| 758 | return (xmlSchemaTypeDateDef); |
| 759 | case XML_SCHEMAS_DATETIME: |
| 760 | return (xmlSchemaTypeDatetimeDef); |
| 761 | case XML_SCHEMAS_DURATION: |
| 762 | return (xmlSchemaTypeDurationDef); |
| 763 | case XML_SCHEMAS_FLOAT: |
| 764 | return (xmlSchemaTypeFloatDef); |
| 765 | case XML_SCHEMAS_DOUBLE: |
| 766 | return (xmlSchemaTypeDoubleDef); |
| 767 | case XML_SCHEMAS_BOOLEAN: |
| 768 | return (xmlSchemaTypeBooleanDef); |
| 769 | case XML_SCHEMAS_TOKEN: |
| 770 | return (xmlSchemaTypeTokenDef); |
| 771 | case XML_SCHEMAS_LANGUAGE: |
| 772 | return (xmlSchemaTypeLanguageDef); |
| 773 | case XML_SCHEMAS_NMTOKEN: |
| 774 | return (xmlSchemaTypeNmtokenDef); |
| 775 | case XML_SCHEMAS_NMTOKENS: |
| 776 | return (xmlSchemaTypeNmtokensDef); |
| 777 | case XML_SCHEMAS_NAME: |
| 778 | return (xmlSchemaTypeNameDef); |
| 779 | case XML_SCHEMAS_QNAME: |
| 780 | return (xmlSchemaTypeQNameDef); |
| 781 | case XML_SCHEMAS_NCNAME: |
| 782 | return (xmlSchemaTypeNCNameDef); |
| 783 | case XML_SCHEMAS_ID: |
| 784 | return (xmlSchemaTypeIdDef); |
| 785 | case XML_SCHEMAS_IDREF: |
| 786 | return (xmlSchemaTypeIdrefDef); |
| 787 | case XML_SCHEMAS_IDREFS: |
| 788 | return (xmlSchemaTypeIdrefsDef); |
| 789 | case XML_SCHEMAS_ENTITY: |
| 790 | return (xmlSchemaTypeEntityDef); |
| 791 | case XML_SCHEMAS_ENTITIES: |
| 792 | return (xmlSchemaTypeEntitiesDef); |
| 793 | case XML_SCHEMAS_NOTATION: |
| 794 | return (xmlSchemaTypeNotationDef); |
| 795 | case XML_SCHEMAS_ANYURI: |
| 796 | return (xmlSchemaTypeAnyURIDef); |
| 797 | case XML_SCHEMAS_INTEGER: |
| 798 | return (xmlSchemaTypeIntegerDef); |
| 799 | case XML_SCHEMAS_NPINTEGER: |
| 800 | return (xmlSchemaTypeNonPositiveIntegerDef); |
| 801 | case XML_SCHEMAS_NINTEGER: |
| 802 | return (xmlSchemaTypeNegativeIntegerDef); |
| 803 | case XML_SCHEMAS_NNINTEGER: |
| 804 | return (xmlSchemaTypeNonNegativeIntegerDef); |
| 805 | case XML_SCHEMAS_PINTEGER: |
| 806 | return (xmlSchemaTypePositiveIntegerDef); |
| 807 | case XML_SCHEMAS_INT: |
| 808 | return (xmlSchemaTypeIntDef); |
| 809 | case XML_SCHEMAS_UINT: |
| 810 | return (xmlSchemaTypeUnsignedIntDef); |
| 811 | case XML_SCHEMAS_LONG: |
| 812 | return (xmlSchemaTypeLongDef); |
| 813 | case XML_SCHEMAS_ULONG: |
| 814 | return (xmlSchemaTypeUnsignedLongDef); |
| 815 | case XML_SCHEMAS_SHORT: |
| 816 | return (xmlSchemaTypeShortDef); |
| 817 | case XML_SCHEMAS_USHORT: |
| 818 | return (xmlSchemaTypeUnsignedShortDef); |
| 819 | case XML_SCHEMAS_BYTE: |
| 820 | return (xmlSchemaTypeByteDef); |
| 821 | case XML_SCHEMAS_UBYTE: |
| 822 | return (xmlSchemaTypeUnsignedByteDef); |
| 823 | case XML_SCHEMAS_HEXBINARY: |
| 824 | return (xmlSchemaTypeHexBinaryDef); |
| 825 | case XML_SCHEMAS_BASE64BINARY: |
| 826 | return (xmlSchemaTypeBase64BinaryDef); |
| 827 | case XML_SCHEMAS_ANYTYPE: |
| 828 | return (xmlSchemaTypeAnyTypeDef); |
| 829 | default: |
| 830 | return (NULL); |
| 831 | } |
| 832 | } |
| 833 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 834 | /** |
| 835 | * xmlSchemaValueAppend: |
| 836 | * @prev: the value |
| 837 | * @cur: the value to be appended |
| 838 | * |
| 839 | * Appends a next sibling to a list of computed values. |
| 840 | * |
| 841 | * Returns 0 if succeeded and -1 on API errors. |
| 842 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 843 | int |
| 844 | xmlSchemaValueAppend(xmlSchemaValPtr prev, xmlSchemaValPtr cur) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 845 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 846 | if ((prev == NULL) || (cur == NULL)) |
| 847 | return (-1); |
| 848 | prev->next = cur; |
| 849 | return (0); |
| 850 | } |
| 851 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 852 | /** |
| 853 | * xmlSchemaValueGetNext: |
| 854 | * @cur: the value |
| 855 | * |
| 856 | * Accessor for the next sibling of a list of computed values. |
| 857 | * |
| 858 | * Returns the next value or NULL if there was none, or on |
| 859 | * API errors. |
| 860 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 861 | xmlSchemaValPtr |
| 862 | xmlSchemaValueGetNext(xmlSchemaValPtr cur) { |
| 863 | |
| 864 | if (cur == NULL) |
| 865 | return (NULL); |
| 866 | return (cur->next); |
| 867 | } |
| 868 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 869 | /** |
| 870 | * xmlSchemaValueGetAsString: |
| 871 | * @val: the value |
| 872 | * |
| 873 | * Accessor for the string value of a computed value. |
| 874 | * |
| 875 | * Returns the string value or NULL if there was none, or on |
| 876 | * API errors. |
| 877 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 878 | const xmlChar * |
| 879 | xmlSchemaValueGetAsString(xmlSchemaValPtr val) |
| 880 | { |
| 881 | if (val == NULL) |
| 882 | return (NULL); |
| 883 | switch (val->type) { |
| 884 | case XML_SCHEMAS_STRING: |
| 885 | case XML_SCHEMAS_NORMSTRING: |
| 886 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 887 | case XML_SCHEMAS_TOKEN: |
| 888 | case XML_SCHEMAS_LANGUAGE: |
| 889 | case XML_SCHEMAS_NMTOKEN: |
| 890 | case XML_SCHEMAS_NAME: |
| 891 | case XML_SCHEMAS_NCNAME: |
| 892 | case XML_SCHEMAS_ID: |
| 893 | case XML_SCHEMAS_IDREF: |
| 894 | case XML_SCHEMAS_ENTITY: |
| 895 | case XML_SCHEMAS_ANYURI: |
| 896 | return (BAD_CAST val->value.str); |
| 897 | default: |
| 898 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 899 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 900 | return (NULL); |
| 901 | } |
| 902 | |
Kasimier T. Buchcik | 7f6e024 | 2005-06-15 13:36:10 +0000 | [diff] [blame] | 903 | /** |
| 904 | * xmlSchemaValueGetAsBoolean: |
| 905 | * @val: the value |
| 906 | * |
| 907 | * Accessor for the boolean value of a computed value. |
| 908 | * |
| 909 | * Returns 1 if true and 0 if false, or in case of an error. Hmm. |
| 910 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 911 | int |
| 912 | xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val) |
| 913 | { |
| 914 | if ((val == NULL) || (val->type != XML_SCHEMAS_BOOLEAN)) |
| 915 | return (0); |
| 916 | return (val->value.b); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 920 | * xmlSchemaNewStringValue: |
| 921 | * @type: the value type |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 922 | * @value: the value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 923 | * |
| 924 | * Allocate a new simple type value. The type can be |
Kasimier T. Buchcik | 2ee855d | 2005-03-07 11:14:14 +0000 | [diff] [blame] | 925 | * of XML_SCHEMAS_STRING. |
| 926 | * WARNING: This one is intended to be expanded for other |
| 927 | * string based types. We need this for anySimpleType as well. |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 928 | * The given value is consumed and freed with the struct. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 929 | * |
| 930 | * Returns a pointer to the new value or NULL in case of error |
| 931 | */ |
| 932 | xmlSchemaValPtr |
| 933 | xmlSchemaNewStringValue(xmlSchemaValType type, |
| 934 | const xmlChar *value) |
| 935 | { |
| 936 | xmlSchemaValPtr val; |
| 937 | |
| 938 | if (type != XML_SCHEMAS_STRING) |
| 939 | return(NULL); |
| 940 | val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 941 | if (val == NULL) { |
| 942 | return(NULL); |
| 943 | } |
| 944 | memset(val, 0, sizeof(xmlSchemaVal)); |
| 945 | val->type = type; |
| 946 | val->value.str = (xmlChar *) value; |
| 947 | return(val); |
| 948 | } |
| 949 | |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 950 | /** |
| 951 | * xmlSchemaNewNOTATIONValue: |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 952 | * @name: the notation name |
| 953 | * @ns: the notation namespace name or NULL |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 954 | * |
| 955 | * Allocate a new NOTATION value. |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 956 | * The given values are consumed and freed with the struct. |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 957 | * |
| 958 | * Returns a pointer to the new value or NULL in case of error |
| 959 | */ |
| 960 | xmlSchemaValPtr |
| 961 | xmlSchemaNewNOTATIONValue(const xmlChar *name, |
| 962 | const xmlChar *ns) |
| 963 | { |
| 964 | xmlSchemaValPtr val; |
| 965 | |
| 966 | val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 967 | if (val == NULL) |
| 968 | return (NULL); |
| 969 | |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 970 | val->value.qname.name = (xmlChar *)name; |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 971 | if (ns != NULL) |
William M. Brack | 12d37ab | 2005-02-21 13:54:07 +0000 | [diff] [blame] | 972 | val->value.qname.uri = (xmlChar *)ns; |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 973 | return(val); |
| 974 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 975 | |
| 976 | /** |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 977 | * xmlSchemaNewQNameValue: |
| 978 | * @namespaceName: the namespace name |
| 979 | * @localName: the local name |
| 980 | * |
| 981 | * Allocate a new QName value. |
| 982 | * The given values are consumed and freed with the struct. |
| 983 | * |
| 984 | * Returns a pointer to the new value or NULL in case of an error. |
| 985 | */ |
| 986 | xmlSchemaValPtr |
| 987 | xmlSchemaNewQNameValue(const xmlChar *namespaceName, |
| 988 | const xmlChar *localName) |
| 989 | { |
| 990 | xmlSchemaValPtr val; |
| 991 | |
| 992 | val = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
| 993 | if (val == NULL) |
| 994 | return (NULL); |
| 995 | |
| 996 | val->value.qname.name = (xmlChar *) localName; |
| 997 | val->value.qname.uri = (xmlChar *) namespaceName; |
| 998 | return(val); |
| 999 | } |
| 1000 | |
| 1001 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1002 | * xmlSchemaFreeValue: |
| 1003 | * @value: the value to free |
| 1004 | * |
| 1005 | * Cleanup the default XML Schemas type library |
| 1006 | */ |
| 1007 | void |
| 1008 | xmlSchemaFreeValue(xmlSchemaValPtr value) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1009 | xmlSchemaValPtr prev; |
| 1010 | |
| 1011 | while (value != NULL) { |
| 1012 | switch (value->type) { |
| 1013 | case XML_SCHEMAS_STRING: |
| 1014 | case XML_SCHEMAS_NORMSTRING: |
| 1015 | case XML_SCHEMAS_TOKEN: |
| 1016 | case XML_SCHEMAS_LANGUAGE: |
| 1017 | case XML_SCHEMAS_NMTOKEN: |
| 1018 | case XML_SCHEMAS_NMTOKENS: |
| 1019 | case XML_SCHEMAS_NAME: |
| 1020 | case XML_SCHEMAS_NCNAME: |
| 1021 | case XML_SCHEMAS_ID: |
| 1022 | case XML_SCHEMAS_IDREF: |
| 1023 | case XML_SCHEMAS_IDREFS: |
| 1024 | case XML_SCHEMAS_ENTITY: |
| 1025 | case XML_SCHEMAS_ENTITIES: |
| 1026 | case XML_SCHEMAS_ANYURI: |
| 1027 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 1028 | if (value->value.str != NULL) |
| 1029 | xmlFree(value->value.str); |
| 1030 | break; |
| 1031 | case XML_SCHEMAS_NOTATION: |
| 1032 | case XML_SCHEMAS_QNAME: |
| 1033 | if (value->value.qname.uri != NULL) |
| 1034 | xmlFree(value->value.qname.uri); |
| 1035 | if (value->value.qname.name != NULL) |
| 1036 | xmlFree(value->value.qname.name); |
| 1037 | break; |
| 1038 | case XML_SCHEMAS_HEXBINARY: |
| 1039 | if (value->value.hex.str != NULL) |
| 1040 | xmlFree(value->value.hex.str); |
| 1041 | break; |
| 1042 | case XML_SCHEMAS_BASE64BINARY: |
| 1043 | if (value->value.base64.str != NULL) |
| 1044 | xmlFree(value->value.base64.str); |
| 1045 | break; |
| 1046 | default: |
| 1047 | break; |
| 1048 | } |
| 1049 | prev = value; |
| 1050 | value = value->next; |
| 1051 | xmlFree(prev); |
| 1052 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * xmlSchemaGetPredefinedType: |
| 1057 | * @name: the type name |
| 1058 | * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema" |
| 1059 | * |
| 1060 | * Lookup a type in the default XML Schemas type library |
| 1061 | * |
| 1062 | * Returns the type if found, NULL otherwise |
| 1063 | */ |
| 1064 | xmlSchemaTypePtr |
| 1065 | xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) { |
| 1066 | if (xmlSchemaTypesInitialized == 0) |
| 1067 | xmlSchemaInitTypes(); |
| 1068 | if (name == NULL) |
| 1069 | return(NULL); |
| 1070 | return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns)); |
| 1071 | } |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1072 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1073 | /** |
| 1074 | * xmlSchemaGetBuiltInListSimpleTypeItemType: |
| 1075 | * @type: the built-in simple type. |
| 1076 | * |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 1077 | * Lookup function |
| 1078 | * |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 1079 | * Returns the item type of @type as defined by the built-in datatype |
| 1080 | * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1081 | */ |
| 1082 | xmlSchemaTypePtr |
| 1083 | xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type) |
| 1084 | { |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 1085 | if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC)) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1086 | return (NULL); |
| 1087 | switch (type->builtInType) { |
| 1088 | case XML_SCHEMAS_NMTOKENS: |
| 1089 | return (xmlSchemaTypeNmtokenDef ); |
| 1090 | case XML_SCHEMAS_IDREFS: |
| 1091 | return (xmlSchemaTypeIdrefDef); |
| 1092 | case XML_SCHEMAS_ENTITIES: |
| 1093 | return (xmlSchemaTypeEntityDef); |
| 1094 | default: |
| 1095 | return (NULL); |
| 1096 | } |
| 1097 | } |
| 1098 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1099 | /**************************************************************** |
| 1100 | * * |
| 1101 | * Convenience macros and functions * |
| 1102 | * * |
| 1103 | ****************************************************************/ |
| 1104 | |
| 1105 | #define IS_TZO_CHAR(c) \ |
| 1106 | ((c == 0) || (c == 'Z') || (c == '+') || (c == '-')) |
| 1107 | |
| 1108 | #define VALID_YEAR(yr) (yr != 0) |
| 1109 | #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12)) |
| 1110 | /* VALID_DAY should only be used when month is unknown */ |
| 1111 | #define VALID_DAY(day) ((day >= 1) && (day <= 31)) |
| 1112 | #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23)) |
| 1113 | #define VALID_MIN(min) ((min >= 0) && (min <= 59)) |
| 1114 | #define VALID_SEC(sec) ((sec >= 0) && (sec < 60)) |
Kasimier T. Buchcik | 690a680 | 2005-05-12 13:16:01 +0000 | [diff] [blame] | 1115 | #define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1116 | #define IS_LEAP(y) \ |
| 1117 | (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) |
| 1118 | |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 1119 | static const unsigned int daysInMonth[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1120 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 1121 | static const unsigned int daysInMonthLeap[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1122 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 1123 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1124 | #define MAX_DAYINMONTH(yr,mon) \ |
| 1125 | (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1]) |
| 1126 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1127 | #define VALID_MDAY(dt) \ |
| 1128 | (IS_LEAP(dt->year) ? \ |
| 1129 | (dt->day <= daysInMonthLeap[dt->mon - 1]) : \ |
| 1130 | (dt->day <= daysInMonth[dt->mon - 1])) |
| 1131 | |
| 1132 | #define VALID_DATE(dt) \ |
| 1133 | (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt)) |
| 1134 | |
| 1135 | #define VALID_TIME(dt) \ |
| 1136 | (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \ |
| 1137 | VALID_SEC(dt->sec) && VALID_TZO(dt->tzo)) |
| 1138 | |
| 1139 | #define VALID_DATETIME(dt) \ |
| 1140 | (VALID_DATE(dt) && VALID_TIME(dt)) |
| 1141 | |
| 1142 | #define SECS_PER_MIN (60) |
| 1143 | #define SECS_PER_HOUR (60 * SECS_PER_MIN) |
| 1144 | #define SECS_PER_DAY (24 * SECS_PER_HOUR) |
| 1145 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1146 | static const long dayInYearByMonth[12] = |
| 1147 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 1148 | static const long dayInLeapYearByMonth[12] = |
| 1149 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; |
| 1150 | |
| 1151 | #define DAY_IN_YEAR(day, month, year) \ |
| 1152 | ((IS_LEAP(year) ? \ |
| 1153 | dayInLeapYearByMonth[month - 1] : \ |
| 1154 | dayInYearByMonth[month - 1]) + day) |
| 1155 | |
| 1156 | #ifdef DEBUG |
| 1157 | #define DEBUG_DATE(dt) \ |
| 1158 | xmlGenericError(xmlGenericErrorContext, \ |
| 1159 | "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \ |
| 1160 | dt->type,dt->value.date.year,dt->value.date.mon, \ |
| 1161 | dt->value.date.day,dt->value.date.hour,dt->value.date.min, \ |
| 1162 | dt->value.date.sec); \ |
| 1163 | if (dt->value.date.tz_flag) \ |
| 1164 | if (dt->value.date.tzo != 0) \ |
| 1165 | xmlGenericError(xmlGenericErrorContext, \ |
| 1166 | "%+05d\n",dt->value.date.tzo); \ |
| 1167 | else \ |
| 1168 | xmlGenericError(xmlGenericErrorContext, "Z\n"); \ |
| 1169 | else \ |
| 1170 | xmlGenericError(xmlGenericErrorContext,"\n") |
| 1171 | #else |
| 1172 | #define DEBUG_DATE(dt) |
| 1173 | #endif |
| 1174 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1175 | /** |
| 1176 | * _xmlSchemaParseGYear: |
| 1177 | * @dt: pointer to a date structure |
| 1178 | * @str: pointer to the string to analyze |
| 1179 | * |
| 1180 | * Parses a xs:gYear without time zone and fills in the appropriate |
| 1181 | * field of the @dt structure. @str is updated to point just after the |
| 1182 | * xs:gYear. It is supposed that @dt->year is big enough to contain |
| 1183 | * the year. |
| 1184 | * |
| 1185 | * Returns 0 or the error code |
| 1186 | */ |
| 1187 | static int |
| 1188 | _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1189 | const xmlChar *cur = *str, *firstChar; |
| 1190 | int isneg = 0, digcnt = 0; |
| 1191 | |
| 1192 | if (((*cur < '0') || (*cur > '9')) && |
| 1193 | (*cur != '-') && (*cur != '+')) |
| 1194 | return -1; |
| 1195 | |
| 1196 | if (*cur == '-') { |
| 1197 | isneg = 1; |
| 1198 | cur++; |
| 1199 | } |
| 1200 | |
| 1201 | firstChar = cur; |
| 1202 | |
| 1203 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1204 | dt->year = dt->year * 10 + (*cur - '0'); |
| 1205 | cur++; |
| 1206 | digcnt++; |
| 1207 | } |
| 1208 | |
| 1209 | /* year must be at least 4 digits (CCYY); over 4 |
| 1210 | * digits cannot have a leading zero. */ |
| 1211 | if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0'))) |
| 1212 | return 1; |
| 1213 | |
| 1214 | if (isneg) |
| 1215 | dt->year = - dt->year; |
| 1216 | |
| 1217 | if (!VALID_YEAR(dt->year)) |
| 1218 | return 2; |
| 1219 | |
| 1220 | *str = cur; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * PARSE_2_DIGITS: |
| 1226 | * @num: the integer to fill in |
| 1227 | * @cur: an #xmlChar * |
| 1228 | * @invalid: an integer |
| 1229 | * |
| 1230 | * Parses a 2-digits integer and updates @num with the value. @cur is |
| 1231 | * updated to point just after the integer. |
| 1232 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 1233 | * @cur are undefined. |
| 1234 | */ |
| 1235 | #define PARSE_2_DIGITS(num, cur, invalid) \ |
| 1236 | if ((cur[0] < '0') || (cur[0] > '9') || \ |
| 1237 | (cur[1] < '0') || (cur[1] > '9')) \ |
| 1238 | invalid = 1; \ |
| 1239 | else \ |
| 1240 | num = (cur[0] - '0') * 10 + (cur[1] - '0'); \ |
| 1241 | cur += 2; |
| 1242 | |
| 1243 | /** |
| 1244 | * PARSE_FLOAT: |
| 1245 | * @num: the double to fill in |
| 1246 | * @cur: an #xmlChar * |
| 1247 | * @invalid: an integer |
| 1248 | * |
| 1249 | * Parses a float and updates @num with the value. @cur is |
| 1250 | * updated to point just after the float. The float must have a |
| 1251 | * 2-digits integer part and may or may not have a decimal part. |
| 1252 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 1253 | * @cur are undefined. |
| 1254 | */ |
| 1255 | #define PARSE_FLOAT(num, cur, invalid) \ |
| 1256 | PARSE_2_DIGITS(num, cur, invalid); \ |
| 1257 | if (!invalid && (*cur == '.')) { \ |
| 1258 | double mult = 1; \ |
| 1259 | cur++; \ |
| 1260 | if ((*cur < '0') || (*cur > '9')) \ |
| 1261 | invalid = 1; \ |
| 1262 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1263 | mult /= 10; \ |
| 1264 | num += (*cur - '0') * mult; \ |
| 1265 | cur++; \ |
| 1266 | } \ |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * _xmlSchemaParseGMonth: |
| 1271 | * @dt: pointer to a date structure |
| 1272 | * @str: pointer to the string to analyze |
| 1273 | * |
| 1274 | * Parses a xs:gMonth without time zone and fills in the appropriate |
| 1275 | * field of the @dt structure. @str is updated to point just after the |
| 1276 | * xs:gMonth. |
| 1277 | * |
| 1278 | * Returns 0 or the error code |
| 1279 | */ |
| 1280 | static int |
| 1281 | _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1282 | const xmlChar *cur = *str; |
| 1283 | int ret = 0; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1284 | unsigned int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1285 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1286 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1287 | if (ret != 0) |
| 1288 | return ret; |
| 1289 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1290 | if (!VALID_MONTH(value)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1291 | return 2; |
| 1292 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1293 | dt->mon = value; |
| 1294 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1295 | *str = cur; |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | /** |
| 1300 | * _xmlSchemaParseGDay: |
| 1301 | * @dt: pointer to a date structure |
| 1302 | * @str: pointer to the string to analyze |
| 1303 | * |
| 1304 | * Parses a xs:gDay without time zone and fills in the appropriate |
| 1305 | * field of the @dt structure. @str is updated to point just after the |
| 1306 | * xs:gDay. |
| 1307 | * |
| 1308 | * Returns 0 or the error code |
| 1309 | */ |
| 1310 | static int |
| 1311 | _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1312 | const xmlChar *cur = *str; |
| 1313 | int ret = 0; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1314 | unsigned int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1315 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1316 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1317 | if (ret != 0) |
| 1318 | return ret; |
| 1319 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1320 | if (!VALID_DAY(value)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1321 | return 2; |
| 1322 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1323 | dt->day = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1324 | *str = cur; |
| 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | /** |
| 1329 | * _xmlSchemaParseTime: |
| 1330 | * @dt: pointer to a date structure |
| 1331 | * @str: pointer to the string to analyze |
| 1332 | * |
| 1333 | * Parses a xs:time without time zone and fills in the appropriate |
| 1334 | * fields of the @dt structure. @str is updated to point just after the |
| 1335 | * xs:time. |
| 1336 | * In case of error, values of @dt fields are undefined. |
| 1337 | * |
| 1338 | * Returns 0 or the error code |
| 1339 | */ |
| 1340 | static int |
| 1341 | _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) { |
Kasimier T. Buchcik | 285b367 | 2005-05-12 13:10:22 +0000 | [diff] [blame] | 1342 | const xmlChar *cur = *str; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1343 | int ret = 0; |
Kasimier T. Buchcik | 285b367 | 2005-05-12 13:10:22 +0000 | [diff] [blame] | 1344 | int value = 0; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1345 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1346 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1347 | if (ret != 0) |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1348 | return ret; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1349 | if (*cur != ':') |
| 1350 | return 1; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1351 | if (!VALID_HOUR(value)) |
| 1352 | return 2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1353 | cur++; |
| 1354 | |
| 1355 | /* the ':' insures this string is xs:time */ |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1356 | dt->hour = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1357 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1358 | PARSE_2_DIGITS(value, cur, ret); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1359 | if (ret != 0) |
| 1360 | return ret; |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1361 | if (!VALID_MIN(value)) |
| 1362 | return 2; |
| 1363 | dt->min = value; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1364 | |
| 1365 | if (*cur != ':') |
| 1366 | return 1; |
| 1367 | cur++; |
| 1368 | |
| 1369 | PARSE_FLOAT(dt->sec, cur, ret); |
| 1370 | if (ret != 0) |
| 1371 | return ret; |
| 1372 | |
Kasimier T. Buchcik | 64dc4d2 | 2005-04-19 15:58:31 +0000 | [diff] [blame] | 1373 | if ((!VALID_SEC(dt->sec)) || (!VALID_TZO(dt->tzo))) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1374 | return 2; |
| 1375 | |
| 1376 | *str = cur; |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * _xmlSchemaParseTimeZone: |
| 1382 | * @dt: pointer to a date structure |
| 1383 | * @str: pointer to the string to analyze |
| 1384 | * |
| 1385 | * Parses a time zone without time zone and fills in the appropriate |
| 1386 | * field of the @dt structure. @str is updated to point just after the |
| 1387 | * time zone. |
| 1388 | * |
| 1389 | * Returns 0 or the error code |
| 1390 | */ |
| 1391 | static int |
| 1392 | _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1393 | const xmlChar *cur = *str; |
| 1394 | int ret = 0; |
| 1395 | |
| 1396 | if (str == NULL) |
| 1397 | return -1; |
| 1398 | |
| 1399 | switch (*cur) { |
| 1400 | case 0: |
| 1401 | dt->tz_flag = 0; |
| 1402 | dt->tzo = 0; |
| 1403 | break; |
| 1404 | |
| 1405 | case 'Z': |
| 1406 | dt->tz_flag = 1; |
| 1407 | dt->tzo = 0; |
| 1408 | cur++; |
| 1409 | break; |
| 1410 | |
| 1411 | case '+': |
| 1412 | case '-': { |
| 1413 | int isneg = 0, tmp = 0; |
| 1414 | isneg = (*cur == '-'); |
| 1415 | |
| 1416 | cur++; |
| 1417 | |
| 1418 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1419 | if (ret != 0) |
| 1420 | return ret; |
| 1421 | if (!VALID_HOUR(tmp)) |
| 1422 | return 2; |
| 1423 | |
| 1424 | if (*cur != ':') |
| 1425 | return 1; |
| 1426 | cur++; |
| 1427 | |
| 1428 | dt->tzo = tmp * 60; |
| 1429 | |
| 1430 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1431 | if (ret != 0) |
| 1432 | return ret; |
| 1433 | if (!VALID_MIN(tmp)) |
| 1434 | return 2; |
| 1435 | |
| 1436 | dt->tzo += tmp; |
| 1437 | if (isneg) |
| 1438 | dt->tzo = - dt->tzo; |
| 1439 | |
| 1440 | if (!VALID_TZO(dt->tzo)) |
| 1441 | return 2; |
| 1442 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1443 | dt->tz_flag = 1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1444 | break; |
| 1445 | } |
| 1446 | default: |
| 1447 | return 1; |
| 1448 | } |
| 1449 | |
| 1450 | *str = cur; |
| 1451 | return 0; |
| 1452 | } |
| 1453 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1454 | /** |
| 1455 | * _xmlSchemaBase64Decode: |
| 1456 | * @ch: a character |
| 1457 | * |
| 1458 | * Converts a base64 encoded character to its base 64 value. |
| 1459 | * |
| 1460 | * Returns 0-63 (value), 64 (pad), or -1 (not recognized) |
| 1461 | */ |
| 1462 | static int |
| 1463 | _xmlSchemaBase64Decode (const xmlChar ch) { |
| 1464 | if (('A' <= ch) && (ch <= 'Z')) return ch - 'A'; |
| 1465 | if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26; |
| 1466 | if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52; |
| 1467 | if ('+' == ch) return 62; |
| 1468 | if ('/' == ch) return 63; |
| 1469 | if ('=' == ch) return 64; |
| 1470 | return -1; |
| 1471 | } |
| 1472 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1473 | /**************************************************************** |
| 1474 | * * |
| 1475 | * XML Schema Dates/Times Datatypes Handling * |
| 1476 | * * |
| 1477 | ****************************************************************/ |
| 1478 | |
| 1479 | /** |
| 1480 | * PARSE_DIGITS: |
| 1481 | * @num: the integer to fill in |
| 1482 | * @cur: an #xmlChar * |
| 1483 | * @num_type: an integer flag |
| 1484 | * |
| 1485 | * Parses a digits integer and updates @num with the value. @cur is |
| 1486 | * updated to point just after the integer. |
| 1487 | * In case of error, @num_type is set to -1, values of @num and |
| 1488 | * @cur are undefined. |
| 1489 | */ |
| 1490 | #define PARSE_DIGITS(num, cur, num_type) \ |
| 1491 | if ((*cur < '0') || (*cur > '9')) \ |
| 1492 | num_type = -1; \ |
| 1493 | else \ |
| 1494 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1495 | num = num * 10 + (*cur - '0'); \ |
| 1496 | cur++; \ |
| 1497 | } |
| 1498 | |
| 1499 | /** |
| 1500 | * PARSE_NUM: |
| 1501 | * @num: the double to fill in |
| 1502 | * @cur: an #xmlChar * |
| 1503 | * @num_type: an integer flag |
| 1504 | * |
| 1505 | * Parses a float or integer and updates @num with the value. @cur is |
| 1506 | * updated to point just after the number. If the number is a float, |
| 1507 | * then it must have an integer part and a decimal part; @num_type will |
| 1508 | * be set to 1. If there is no decimal part, @num_type is set to zero. |
| 1509 | * In case of error, @num_type is set to -1, values of @num and |
| 1510 | * @cur are undefined. |
| 1511 | */ |
| 1512 | #define PARSE_NUM(num, cur, num_type) \ |
| 1513 | num = 0; \ |
| 1514 | PARSE_DIGITS(num, cur, num_type); \ |
| 1515 | if (!num_type && (*cur == '.')) { \ |
| 1516 | double mult = 1; \ |
| 1517 | cur++; \ |
| 1518 | if ((*cur < '0') || (*cur > '9')) \ |
| 1519 | num_type = -1; \ |
| 1520 | else \ |
| 1521 | num_type = 1; \ |
| 1522 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1523 | mult /= 10; \ |
| 1524 | num += (*cur - '0') * mult; \ |
| 1525 | cur++; \ |
| 1526 | } \ |
| 1527 | } |
| 1528 | |
| 1529 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1530 | * xmlSchemaValidateDates: |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1531 | * @type: the expected type or XML_SCHEMAS_UNKNOWN |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1532 | * @dateTime: string to analyze |
| 1533 | * @val: the return computed value |
| 1534 | * |
| 1535 | * Check that @dateTime conforms to the lexical space of one of the date types. |
| 1536 | * if true a value is computed and returned in @val. |
| 1537 | * |
| 1538 | * Returns 0 if this validates, a positive error code number otherwise |
| 1539 | * and -1 in case of internal or API error. |
| 1540 | */ |
| 1541 | static int |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1542 | xmlSchemaValidateDates (xmlSchemaValType type, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1543 | const xmlChar *dateTime, xmlSchemaValPtr *val, |
| 1544 | int collapse) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1545 | xmlSchemaValPtr dt; |
| 1546 | int ret; |
| 1547 | const xmlChar *cur = dateTime; |
| 1548 | |
| 1549 | #define RETURN_TYPE_IF_VALID(t) \ |
| 1550 | if (IS_TZO_CHAR(*cur)) { \ |
| 1551 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \ |
| 1552 | if (ret == 0) { \ |
| 1553 | if (*cur != 0) \ |
| 1554 | goto error; \ |
| 1555 | dt->type = t; \ |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1556 | goto done; \ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1557 | } \ |
| 1558 | } |
| 1559 | |
| 1560 | if (dateTime == NULL) |
| 1561 | return -1; |
| 1562 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1563 | if (collapse) |
| 1564 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 1565 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1566 | if ((*cur != '-') && (*cur < '0') && (*cur > '9')) |
| 1567 | return 1; |
| 1568 | |
| 1569 | dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN); |
| 1570 | if (dt == NULL) |
| 1571 | return -1; |
| 1572 | |
| 1573 | if ((cur[0] == '-') && (cur[1] == '-')) { |
| 1574 | /* |
| 1575 | * It's an incomplete date (xs:gMonthDay, xs:gMonth or |
| 1576 | * xs:gDay) |
| 1577 | */ |
| 1578 | cur += 2; |
| 1579 | |
| 1580 | /* is it an xs:gDay? */ |
| 1581 | if (*cur == '-') { |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1582 | if (type == XML_SCHEMAS_GMONTH) |
| 1583 | goto error; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1584 | ++cur; |
| 1585 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1586 | if (ret != 0) |
| 1587 | goto error; |
| 1588 | |
| 1589 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY); |
| 1590 | |
| 1591 | goto error; |
| 1592 | } |
| 1593 | |
| 1594 | /* |
| 1595 | * it should be an xs:gMonthDay or xs:gMonth |
| 1596 | */ |
| 1597 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1598 | if (ret != 0) |
| 1599 | goto error; |
| 1600 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1601 | /* |
| 1602 | * a '-' char could indicate this type is xs:gMonthDay or |
| 1603 | * a negative time zone offset. Check for xs:gMonthDay first. |
| 1604 | * Also the first three char's of a negative tzo (-MM:SS) can |
| 1605 | * appear to be a valid day; so even if the day portion |
| 1606 | * of the xs:gMonthDay verifies, we must insure it was not |
| 1607 | * a tzo. |
| 1608 | */ |
| 1609 | if (*cur == '-') { |
| 1610 | const xmlChar *rewnd = cur; |
| 1611 | cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1612 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1613 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1614 | if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) { |
| 1615 | |
| 1616 | /* |
| 1617 | * we can use the VALID_MDAY macro to validate the month |
| 1618 | * and day because the leap year test will flag year zero |
| 1619 | * as a leap year (even though zero is an invalid year). |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1620 | * FUTURE TODO: Zero will become valid in XML Schema 1.1 |
| 1621 | * probably. |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1622 | */ |
| 1623 | if (VALID_MDAY((&(dt->value.date)))) { |
| 1624 | |
| 1625 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY); |
| 1626 | |
| 1627 | goto error; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * not xs:gMonthDay so rewind and check if just xs:gMonth |
| 1633 | * with an optional time zone. |
| 1634 | */ |
| 1635 | cur = rewnd; |
| 1636 | } |
| 1637 | |
| 1638 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1639 | |
| 1640 | goto error; |
| 1641 | } |
| 1642 | |
| 1643 | /* |
| 1644 | * It's a right-truncated date or an xs:time. |
| 1645 | * Try to parse an xs:time then fallback on right-truncated dates. |
| 1646 | */ |
| 1647 | if ((*cur >= '0') && (*cur <= '9')) { |
| 1648 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1649 | if (ret == 0) { |
| 1650 | /* it's an xs:time */ |
| 1651 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME); |
| 1652 | } |
| 1653 | } |
| 1654 | |
| 1655 | /* fallback on date parsing */ |
| 1656 | cur = dateTime; |
| 1657 | |
| 1658 | ret = _xmlSchemaParseGYear(&(dt->value.date), &cur); |
| 1659 | if (ret != 0) |
| 1660 | goto error; |
| 1661 | |
| 1662 | /* is it an xs:gYear? */ |
| 1663 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR); |
| 1664 | |
| 1665 | if (*cur != '-') |
| 1666 | goto error; |
| 1667 | cur++; |
| 1668 | |
| 1669 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1670 | if (ret != 0) |
| 1671 | goto error; |
| 1672 | |
| 1673 | /* is it an xs:gYearMonth? */ |
| 1674 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH); |
| 1675 | |
| 1676 | if (*cur != '-') |
| 1677 | goto error; |
| 1678 | cur++; |
| 1679 | |
| 1680 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1681 | if ((ret != 0) || !VALID_DATE((&(dt->value.date)))) |
| 1682 | goto error; |
| 1683 | |
| 1684 | /* is it an xs:date? */ |
| 1685 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE); |
| 1686 | |
| 1687 | if (*cur != 'T') |
| 1688 | goto error; |
| 1689 | cur++; |
| 1690 | |
| 1691 | /* it should be an xs:dateTime */ |
| 1692 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1693 | if (ret != 0) |
| 1694 | goto error; |
| 1695 | |
| 1696 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1697 | if (collapse) |
| 1698 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1699 | if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date)))) |
| 1700 | goto error; |
| 1701 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1702 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1703 | dt->type = XML_SCHEMAS_DATETIME; |
| 1704 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1705 | done: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1706 | #if 1 |
| 1707 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) |
| 1708 | goto error; |
| 1709 | #else |
| 1710 | /* |
| 1711 | * insure the parsed type is equal to or less significant (right |
| 1712 | * truncated) than the desired type. |
| 1713 | */ |
| 1714 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) { |
| 1715 | |
| 1716 | /* time only matches time */ |
| 1717 | if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME)) |
| 1718 | goto error; |
| 1719 | |
| 1720 | if ((type == XML_SCHEMAS_DATETIME) && |
| 1721 | ((dt->type != XML_SCHEMAS_DATE) || |
| 1722 | (dt->type != XML_SCHEMAS_GYEARMONTH) || |
| 1723 | (dt->type != XML_SCHEMAS_GYEAR))) |
| 1724 | goto error; |
| 1725 | |
| 1726 | if ((type == XML_SCHEMAS_DATE) && |
| 1727 | ((dt->type != XML_SCHEMAS_GYEAR) || |
| 1728 | (dt->type != XML_SCHEMAS_GYEARMONTH))) |
| 1729 | goto error; |
| 1730 | |
| 1731 | if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR)) |
| 1732 | goto error; |
| 1733 | |
| 1734 | if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH)) |
| 1735 | goto error; |
| 1736 | } |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1737 | #endif |
| 1738 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1739 | if (val != NULL) |
| 1740 | *val = dt; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1741 | else |
| 1742 | xmlSchemaFreeValue(dt); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1743 | |
| 1744 | return 0; |
| 1745 | |
| 1746 | error: |
| 1747 | if (dt != NULL) |
| 1748 | xmlSchemaFreeValue(dt); |
| 1749 | return 1; |
| 1750 | } |
| 1751 | |
| 1752 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1753 | * xmlSchemaValidateDuration: |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1754 | * @type: the predefined type |
| 1755 | * @duration: string to analyze |
| 1756 | * @val: the return computed value |
| 1757 | * |
| 1758 | * Check that @duration conforms to the lexical space of the duration type. |
| 1759 | * if true a value is computed and returned in @val. |
| 1760 | * |
| 1761 | * Returns 0 if this validates, a positive error code number otherwise |
| 1762 | * and -1 in case of internal or API error. |
| 1763 | */ |
| 1764 | static int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 1765 | xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1766 | const xmlChar *duration, xmlSchemaValPtr *val, |
| 1767 | int collapse) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1768 | const xmlChar *cur = duration; |
| 1769 | xmlSchemaValPtr dur; |
| 1770 | int isneg = 0; |
| 1771 | unsigned int seq = 0; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1772 | double num; |
| 1773 | int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */ |
| 1774 | const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'}; |
| 1775 | const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0}; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1776 | |
| 1777 | if (duration == NULL) |
| 1778 | return -1; |
| 1779 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1780 | if (collapse) |
| 1781 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 1782 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1783 | if (*cur == '-') { |
| 1784 | isneg = 1; |
| 1785 | cur++; |
| 1786 | } |
| 1787 | |
| 1788 | /* duration must start with 'P' (after sign) */ |
| 1789 | if (*cur++ != 'P') |
| 1790 | return 1; |
| 1791 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1792 | if (*cur == 0) |
| 1793 | return 1; |
| 1794 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1795 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 1796 | if (dur == NULL) |
| 1797 | return -1; |
| 1798 | |
| 1799 | while (*cur != 0) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1800 | |
| 1801 | /* input string should be empty or invalid date/time item */ |
| 1802 | if (seq >= sizeof(desig)) |
| 1803 | goto error; |
| 1804 | |
| 1805 | /* T designator must be present for time items */ |
| 1806 | if (*cur == 'T') { |
| 1807 | if (seq <= 3) { |
| 1808 | seq = 3; |
| 1809 | cur++; |
| 1810 | } else |
| 1811 | return 1; |
| 1812 | } else if (seq == 3) |
| 1813 | goto error; |
| 1814 | |
| 1815 | /* parse the number portion of the item */ |
| 1816 | PARSE_NUM(num, cur, num_type); |
| 1817 | |
| 1818 | if ((num_type == -1) || (*cur == 0)) |
| 1819 | goto error; |
| 1820 | |
| 1821 | /* update duration based on item type */ |
| 1822 | while (seq < sizeof(desig)) { |
| 1823 | if (*cur == desig[seq]) { |
| 1824 | |
| 1825 | /* verify numeric type; only seconds can be float */ |
| 1826 | if ((num_type != 0) && (seq < (sizeof(desig)-1))) |
| 1827 | goto error; |
| 1828 | |
| 1829 | switch (seq) { |
| 1830 | case 0: |
| 1831 | dur->value.dur.mon = (long)num * 12; |
| 1832 | break; |
| 1833 | case 1: |
| 1834 | dur->value.dur.mon += (long)num; |
| 1835 | break; |
| 1836 | default: |
| 1837 | /* convert to seconds using multiplier */ |
| 1838 | dur->value.dur.sec += num * multi[seq]; |
| 1839 | seq++; |
| 1840 | break; |
| 1841 | } |
| 1842 | |
| 1843 | break; /* exit loop */ |
| 1844 | } |
| 1845 | /* no date designators found? */ |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 1846 | if ((++seq == 3) || (seq == 6)) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1847 | goto error; |
| 1848 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 1849 | cur++; |
| 1850 | if (collapse) |
| 1851 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | if (isneg) { |
| 1855 | dur->value.dur.mon = -dur->value.dur.mon; |
| 1856 | dur->value.dur.day = -dur->value.dur.day; |
| 1857 | dur->value.dur.sec = -dur->value.dur.sec; |
| 1858 | } |
| 1859 | |
| 1860 | if (val != NULL) |
| 1861 | *val = dur; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1862 | else |
| 1863 | xmlSchemaFreeValue(dur); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1864 | |
| 1865 | return 0; |
| 1866 | |
| 1867 | error: |
| 1868 | if (dur != NULL) |
| 1869 | xmlSchemaFreeValue(dur); |
| 1870 | return 1; |
| 1871 | } |
| 1872 | |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1873 | /** |
| 1874 | * xmlSchemaStrip: |
| 1875 | * @value: a value |
| 1876 | * |
| 1877 | * Removes the leading and ending spaces of a string |
| 1878 | * |
| 1879 | * Returns the new string or NULL if no change was required. |
| 1880 | */ |
| 1881 | static xmlChar * |
| 1882 | xmlSchemaStrip(const xmlChar *value) { |
| 1883 | const xmlChar *start = value, *end, *f; |
| 1884 | |
| 1885 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1886 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1887 | end = start; |
| 1888 | while (*end != 0) end++; |
| 1889 | f = end; |
| 1890 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1891 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1892 | end++; |
| 1893 | if ((start == value) && (f == end)) return(NULL); |
| 1894 | return(xmlStrndup(start, end - start)); |
| 1895 | } |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1896 | |
| 1897 | /** |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1898 | * xmlSchemaWhiteSpaceReplace: |
| 1899 | * @value: a value |
| 1900 | * |
| 1901 | * Replaces 0xd, 0x9 and 0xa with a space. |
| 1902 | * |
| 1903 | * Returns the new string or NULL if no change was required. |
| 1904 | */ |
| 1905 | xmlChar * |
| 1906 | xmlSchemaWhiteSpaceReplace(const xmlChar *value) { |
| 1907 | const xmlChar *cur = value; |
| 1908 | xmlChar *ret = NULL, *mcur; |
| 1909 | |
| 1910 | if (value == NULL) |
| 1911 | return(NULL); |
| 1912 | |
| 1913 | while ((*cur != 0) && |
| 1914 | (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) { |
| 1915 | cur++; |
| 1916 | } |
| 1917 | if (*cur == 0) |
| 1918 | return (NULL); |
| 1919 | ret = xmlStrdup(value); |
| 1920 | /* TODO FIXME: I guess gcc will bark at this. */ |
| 1921 | mcur = (xmlChar *) (ret + (cur - value)); |
| 1922 | do { |
| 1923 | if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) ) |
| 1924 | *mcur = ' '; |
| 1925 | mcur++; |
| 1926 | } while (*mcur != 0); |
| 1927 | return(ret); |
| 1928 | } |
| 1929 | |
| 1930 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1931 | * xmlSchemaCollapseString: |
| 1932 | * @value: a value |
| 1933 | * |
| 1934 | * Removes and normalize white spaces in the string |
| 1935 | * |
| 1936 | * Returns the new string or NULL if no change was required. |
| 1937 | */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1938 | xmlChar * |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1939 | xmlSchemaCollapseString(const xmlChar *value) { |
| 1940 | const xmlChar *start = value, *end, *f; |
| 1941 | xmlChar *g; |
| 1942 | int col = 0; |
| 1943 | |
| 1944 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1945 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1946 | end = start; |
| 1947 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1948 | if ((*end == ' ') && (IS_BLANK_CH(end[1]))) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1949 | col = end - start; |
| 1950 | break; |
| 1951 | } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) { |
| 1952 | col = end - start; |
| 1953 | break; |
| 1954 | } |
| 1955 | end++; |
| 1956 | } |
| 1957 | if (col == 0) { |
| 1958 | f = end; |
| 1959 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1960 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1961 | end++; |
| 1962 | if ((start == value) && (f == end)) return(NULL); |
| 1963 | return(xmlStrndup(start, end - start)); |
| 1964 | } |
| 1965 | start = xmlStrdup(start); |
| 1966 | if (start == NULL) return(NULL); |
| 1967 | g = (xmlChar *) (start + col); |
| 1968 | end = g; |
| 1969 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1970 | if (IS_BLANK_CH(*end)) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1971 | end++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1972 | while (IS_BLANK_CH(*end)) end++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1973 | if (*end != 0) |
| 1974 | *g++ = ' '; |
| 1975 | } else |
| 1976 | *g++ = *end++; |
| 1977 | } |
| 1978 | *g = 0; |
| 1979 | return((xmlChar *) start); |
| 1980 | } |
| 1981 | |
| 1982 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1983 | * xmlSchemaValAtomicListNode: |
| 1984 | * @type: the predefined atomic type for a token in the list |
| 1985 | * @value: the list value to check |
| 1986 | * @ret: the return computed value |
| 1987 | * @node: the node containing the value |
| 1988 | * |
| 1989 | * Check that a value conforms to the lexical space of the predefined |
| 1990 | * list type. if true a value is computed and returned in @ret. |
| 1991 | * |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1992 | * Returns the number of items if this validates, a negative error code |
| 1993 | * number otherwise |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1994 | */ |
| 1995 | static int |
| 1996 | xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 1997 | xmlSchemaValPtr *ret, xmlNodePtr node) { |
| 1998 | xmlChar *val, *cur, *endval; |
| 1999 | int nb_values = 0; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 2000 | int tmp = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2001 | |
| 2002 | if (value == NULL) { |
| 2003 | return(-1); |
| 2004 | } |
| 2005 | val = xmlStrdup(value); |
| 2006 | if (val == NULL) { |
| 2007 | return(-1); |
| 2008 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2009 | if (ret != NULL) { |
| 2010 | *ret = NULL; |
| 2011 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2012 | cur = val; |
| 2013 | /* |
| 2014 | * Split the list |
| 2015 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2016 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2017 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2018 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2019 | *cur = 0; |
| 2020 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2021 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2022 | } else { |
| 2023 | nb_values++; |
| 2024 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2025 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2026 | } |
| 2027 | } |
| 2028 | if (nb_values == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2029 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 2030 | return(nb_values); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2031 | } |
| 2032 | endval = cur; |
| 2033 | cur = val; |
| 2034 | while ((*cur == 0) && (cur != endval)) cur++; |
| 2035 | while (cur != endval) { |
| 2036 | tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node); |
| 2037 | if (tmp != 0) |
| 2038 | break; |
| 2039 | while (*cur != 0) cur++; |
| 2040 | while ((*cur == 0) && (cur != endval)) cur++; |
| 2041 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2042 | /* TODO what return value ? c.f. bug #158628 |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2043 | if (ret != NULL) { |
| 2044 | TODO |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 2045 | } */ |
| 2046 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 2047 | if (tmp == 0) |
| 2048 | return(nb_values); |
| 2049 | return(-1); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /** |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2053 | * xmlSchemaParseUInt: |
| 2054 | * @str: pointer to the string R/W |
| 2055 | * @llo: pointer to the low result |
| 2056 | * @lmi: pointer to the mid result |
| 2057 | * @lhi: pointer to the high result |
| 2058 | * |
| 2059 | * Parse an unsigned long into 3 fields. |
| 2060 | * |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 2061 | * Returns the number of significant digits in the number or |
| 2062 | * -1 if overflow of the capacity |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2063 | */ |
| 2064 | static int |
| 2065 | xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2066 | unsigned long *lmi, unsigned long *lhi) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2067 | unsigned long lo = 0, mi = 0, hi = 0; |
| 2068 | const xmlChar *tmp, *cur = *str; |
| 2069 | int ret = 0, i = 0; |
| 2070 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2071 | while (*cur == '0') { /* ignore leading zeroes */ |
| 2072 | cur++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2073 | } |
| 2074 | tmp = cur; |
| 2075 | while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2076 | i++;tmp++;ret++; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2077 | } |
| 2078 | if (i > 24) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2079 | *str = tmp; |
| 2080 | return(-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2081 | } |
| 2082 | while (i > 16) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2083 | hi = hi * 10 + (*cur++ - '0'); |
| 2084 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2085 | } |
| 2086 | while (i > 8) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2087 | mi = mi * 10 + (*cur++ - '0'); |
| 2088 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2089 | } |
| 2090 | while (i > 0) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 2091 | lo = lo * 10 + (*cur++ - '0'); |
| 2092 | i--; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | *str = cur; |
| 2096 | *llo = lo; |
| 2097 | *lmi = mi; |
| 2098 | *lhi = hi; |
| 2099 | return(ret); |
| 2100 | } |
| 2101 | |
| 2102 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2103 | * xmlSchemaValAtomicType: |
| 2104 | * @type: the predefined type |
| 2105 | * @value: the value to check |
| 2106 | * @val: the return computed value |
| 2107 | * @node: the node containing the value |
| 2108 | * flags: flags to control the vlidation |
| 2109 | * |
| 2110 | * Check that a value conforms to the lexical space of the atomic type. |
| 2111 | * if true a value is computed and returned in @val. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2112 | * This checks the value space for list types as well (IDREFS, NMTOKENS). |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2113 | * |
| 2114 | * Returns 0 if this validates, a positive error code number otherwise |
| 2115 | * and -1 in case of internal or API error. |
| 2116 | */ |
| 2117 | static int |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2118 | xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value, |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2119 | xmlSchemaValPtr * val, xmlNodePtr node, int flags, |
| 2120 | xmlSchemaWhitespaceValueType ws, |
| 2121 | int normOnTheFly, int applyNorm, int createStringValue) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2122 | { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2123 | xmlSchemaValPtr v; |
| 2124 | xmlChar *norm = NULL; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 2125 | int ret = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2126 | |
| 2127 | if (xmlSchemaTypesInitialized == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2128 | xmlSchemaInitTypes(); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2129 | if (type == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2130 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2131 | |
Daniel Veillard | eebd633 | 2004-08-26 10:30:44 +0000 | [diff] [blame] | 2132 | /* |
| 2133 | * validating a non existant text node is similar to validating |
| 2134 | * an empty one. |
| 2135 | */ |
| 2136 | if (value == NULL) |
| 2137 | value = BAD_CAST ""; |
| 2138 | |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2139 | if (val != NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2140 | *val = NULL; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2141 | if ((flags == 0) && (value != NULL)) { |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 2142 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2143 | if ((type->builtInType != XML_SCHEMAS_STRING) && |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 2144 | (type->builtInType != XML_SCHEMAS_ANYTYPE) && |
| 2145 | (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) { |
| 2146 | if (type->builtInType == XML_SCHEMAS_NORMSTRING) |
| 2147 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2148 | else |
| 2149 | norm = xmlSchemaCollapseString(value); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2150 | if (norm != NULL) |
| 2151 | value = norm; |
| 2152 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2155 | switch (type->builtInType) { |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 2156 | case XML_SCHEMAS_UNKNOWN: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2157 | goto error; |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 2158 | case XML_SCHEMAS_ANYTYPE: |
| 2159 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2160 | if ((createStringValue) && (val != NULL)) { |
| 2161 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE); |
| 2162 | if (v != NULL) { |
| 2163 | v->value.str = xmlStrdup(value); |
| 2164 | *val = v; |
| 2165 | } else { |
| 2166 | goto error; |
| 2167 | } |
| 2168 | } |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 2169 | goto return0; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2170 | case XML_SCHEMAS_STRING: |
| 2171 | if (! normOnTheFly) { |
| 2172 | const xmlChar *cur = value; |
| 2173 | |
| 2174 | if (ws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 2175 | while (*cur != 0) { |
| 2176 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2177 | goto return1; |
| 2178 | } else { |
| 2179 | cur++; |
| 2180 | } |
| 2181 | } |
| 2182 | } else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 2183 | while (*cur != 0) { |
| 2184 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2185 | goto return1; |
| 2186 | } else if IS_WSP_SPACE_CH(*cur) { |
| 2187 | cur++; |
| 2188 | if IS_WSP_SPACE_CH(*cur) |
| 2189 | goto return1; |
| 2190 | } else { |
| 2191 | cur++; |
| 2192 | } |
| 2193 | } |
| 2194 | } |
| 2195 | } |
| 2196 | if (createStringValue && (val != NULL)) { |
| 2197 | if (applyNorm) { |
| 2198 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 2199 | norm = xmlSchemaCollapseString(value); |
| 2200 | else if (ws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 2201 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2202 | if (norm != NULL) |
| 2203 | value = norm; |
| 2204 | } |
| 2205 | v = xmlSchemaNewValue(XML_SCHEMAS_STRING); |
| 2206 | if (v != NULL) { |
| 2207 | v->value.str = xmlStrdup(value); |
| 2208 | *val = v; |
| 2209 | } else { |
| 2210 | goto error; |
| 2211 | } |
| 2212 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2213 | goto return0; |
Daniel Veillard | 1516d5b | 2004-01-22 07:27:45 +0000 | [diff] [blame] | 2214 | case XML_SCHEMAS_NORMSTRING:{ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2215 | if (normOnTheFly) { |
| 2216 | if (applyNorm) { |
| 2217 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 2218 | norm = xmlSchemaCollapseString(value); |
| 2219 | else |
| 2220 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 2221 | if (norm != NULL) |
| 2222 | value = norm; |
| 2223 | } |
| 2224 | } else { |
| 2225 | const xmlChar *cur = value; |
| 2226 | while (*cur != 0) { |
| 2227 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2228 | goto return1; |
| 2229 | } else { |
| 2230 | cur++; |
| 2231 | } |
| 2232 | } |
| 2233 | } |
Daniel Veillard | 1516d5b | 2004-01-22 07:27:45 +0000 | [diff] [blame] | 2234 | if (val != NULL) { |
| 2235 | v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING); |
| 2236 | if (v != NULL) { |
| 2237 | v->value.str = xmlStrdup(value); |
| 2238 | *val = v; |
| 2239 | } else { |
| 2240 | goto error; |
| 2241 | } |
| 2242 | } |
| 2243 | goto return0; |
| 2244 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2245 | case XML_SCHEMAS_DECIMAL:{ |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2246 | const xmlChar *cur = value; |
| 2247 | unsigned int len, neg = 0; |
| 2248 | xmlChar cval[25]; |
| 2249 | xmlChar *cptr = cval; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 2250 | unsigned int dec = ~0u; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2251 | |
| 2252 | if (cur == NULL) |
| 2253 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2254 | |
| 2255 | if (normOnTheFly) |
| 2256 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2257 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2258 | /* First we handle an optional sign */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2259 | if (*cur == '+') |
| 2260 | cur++; |
| 2261 | else if (*cur == '-') { |
| 2262 | neg = 1; |
| 2263 | cur++; |
| 2264 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2265 | /* |
| 2266 | * Next we "pre-parse" the number, in preparation for calling |
| 2267 | * the common routine xmlSchemaParseUInt. We get rid of any |
| 2268 | * leading zeroes (because we have reserved only 25 chars), |
| 2269 | * and note the position of any decimal point. |
| 2270 | */ |
| 2271 | len = 0; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2272 | /* |
| 2273 | * Skip leading zeroes. |
| 2274 | */ |
| 2275 | while (*cur == '0') |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2276 | cur++; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2277 | if (*cur != 0) { |
| 2278 | while (len < 24) { |
| 2279 | if ((*cur >= '0') && (*cur <= '9')) { |
| 2280 | *cptr++ = *cur++; |
| 2281 | len++; |
| 2282 | } else if (*cur == '.') { |
Daniel Veillard | 4f917e2 | 2005-08-22 16:01:43 +0000 | [diff] [blame] | 2283 | if (len == 0) |
| 2284 | len++; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 2285 | if (dec != ~0u) |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2286 | goto return1; /* multiple decimal points */ |
| 2287 | cur++; |
| 2288 | if ((*cur == 0) && (cur -1 == value)) |
| 2289 | goto return1; |
| 2290 | |
| 2291 | dec = len; |
| 2292 | while ((len < 24) && (*cur >= '0') && |
| 2293 | (*cur <= '9')) { |
| 2294 | *cptr++ = *cur++; |
| 2295 | len++; |
| 2296 | } |
| 2297 | break; |
| 2298 | } else |
| 2299 | break; |
| 2300 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2301 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2302 | if (normOnTheFly) |
| 2303 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2304 | if (*cur != 0) |
| 2305 | goto return1; /* error if any extraneous chars */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2306 | if (val != NULL) { |
| 2307 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 2308 | if (v != NULL) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2309 | /* |
| 2310 | * If a mixed decimal, get rid of trailing zeroes |
| 2311 | */ |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 2312 | if (dec != ~0u) { |
| 2313 | while ((len > dec) && (cptr > cval) && |
Kasimier T. Buchcik | 6d30ff2 | 2005-07-06 11:44:51 +0000 | [diff] [blame] | 2314 | (*(cptr-1) == '0')) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2315 | cptr--; |
| 2316 | len--; |
| 2317 | } |
| 2318 | } |
| 2319 | *cptr = 0; /* Terminate our (preparsed) string */ |
| 2320 | cptr = cval; |
| 2321 | /* |
| 2322 | * Now evaluate the significant digits of the number |
| 2323 | */ |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2324 | if (*cptr != 0) |
| 2325 | xmlSchemaParseUInt((const xmlChar **)&cptr, |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2326 | &v->value.decimal.lo, |
| 2327 | &v->value.decimal.mi, |
| 2328 | &v->value.decimal.hi); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2329 | /* |
| 2330 | * Set the total digits to 1 if a zero value. |
| 2331 | */ |
| 2332 | if (len == 0) |
| 2333 | len++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2334 | v->value.decimal.sign = neg; |
Daniel Veillard | 9e2110b | 2005-08-08 20:33:54 +0000 | [diff] [blame] | 2335 | if (dec == ~0u) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2336 | v->value.decimal.frac = 0; |
| 2337 | v->value.decimal.total = len; |
| 2338 | } else { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2339 | v->value.decimal.frac = len - dec; |
| 2340 | v->value.decimal.total = len; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2341 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2342 | *val = v; |
| 2343 | } |
| 2344 | } |
| 2345 | goto return0; |
| 2346 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2347 | case XML_SCHEMAS_TIME: |
| 2348 | case XML_SCHEMAS_GDAY: |
| 2349 | case XML_SCHEMAS_GMONTH: |
| 2350 | case XML_SCHEMAS_GMONTHDAY: |
| 2351 | case XML_SCHEMAS_GYEAR: |
| 2352 | case XML_SCHEMAS_GYEARMONTH: |
| 2353 | case XML_SCHEMAS_DATE: |
| 2354 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2355 | ret = xmlSchemaValidateDates(type->builtInType, value, val, |
| 2356 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2357 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2358 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2359 | ret = xmlSchemaValidateDuration(type, value, val, |
| 2360 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2361 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2362 | case XML_SCHEMAS_FLOAT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2363 | case XML_SCHEMAS_DOUBLE:{ |
| 2364 | const xmlChar *cur = value; |
| 2365 | int neg = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2366 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2367 | if (cur == NULL) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2368 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2369 | |
| 2370 | if (normOnTheFly) |
| 2371 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2372 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2373 | if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) { |
| 2374 | cur += 3; |
| 2375 | if (*cur != 0) |
| 2376 | goto return1; |
| 2377 | if (val != NULL) { |
| 2378 | if (type == xmlSchemaTypeFloatDef) { |
| 2379 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2380 | if (v != NULL) { |
| 2381 | v->value.f = (float) xmlXPathNAN; |
| 2382 | } else { |
| 2383 | xmlSchemaFreeValue(v); |
| 2384 | goto error; |
| 2385 | } |
| 2386 | } else { |
| 2387 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2388 | if (v != NULL) { |
| 2389 | v->value.d = xmlXPathNAN; |
| 2390 | } else { |
| 2391 | xmlSchemaFreeValue(v); |
| 2392 | goto error; |
| 2393 | } |
| 2394 | } |
| 2395 | *val = v; |
| 2396 | } |
| 2397 | goto return0; |
| 2398 | } |
| 2399 | if (*cur == '-') { |
| 2400 | neg = 1; |
| 2401 | cur++; |
| 2402 | } |
| 2403 | if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) { |
| 2404 | cur += 3; |
| 2405 | if (*cur != 0) |
| 2406 | goto return1; |
| 2407 | if (val != NULL) { |
| 2408 | if (type == xmlSchemaTypeFloatDef) { |
| 2409 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2410 | if (v != NULL) { |
| 2411 | if (neg) |
| 2412 | v->value.f = (float) xmlXPathNINF; |
| 2413 | else |
| 2414 | v->value.f = (float) xmlXPathPINF; |
| 2415 | } else { |
| 2416 | xmlSchemaFreeValue(v); |
| 2417 | goto error; |
| 2418 | } |
| 2419 | } else { |
| 2420 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2421 | if (v != NULL) { |
| 2422 | if (neg) |
| 2423 | v->value.d = xmlXPathNINF; |
| 2424 | else |
| 2425 | v->value.d = xmlXPathPINF; |
| 2426 | } else { |
| 2427 | xmlSchemaFreeValue(v); |
| 2428 | goto error; |
| 2429 | } |
| 2430 | } |
| 2431 | *val = v; |
| 2432 | } |
| 2433 | goto return0; |
| 2434 | } |
| 2435 | if ((neg == 0) && (*cur == '+')) |
| 2436 | cur++; |
| 2437 | if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-')) |
| 2438 | goto return1; |
| 2439 | while ((*cur >= '0') && (*cur <= '9')) { |
| 2440 | cur++; |
| 2441 | } |
| 2442 | if (*cur == '.') { |
| 2443 | cur++; |
| 2444 | while ((*cur >= '0') && (*cur <= '9')) |
| 2445 | cur++; |
| 2446 | } |
| 2447 | if ((*cur == 'e') || (*cur == 'E')) { |
| 2448 | cur++; |
| 2449 | if ((*cur == '-') || (*cur == '+')) |
| 2450 | cur++; |
| 2451 | while ((*cur >= '0') && (*cur <= '9')) |
| 2452 | cur++; |
| 2453 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2454 | if (normOnTheFly) |
| 2455 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2456 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2457 | if (*cur != 0) |
| 2458 | goto return1; |
| 2459 | if (val != NULL) { |
| 2460 | if (type == xmlSchemaTypeFloatDef) { |
| 2461 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2462 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2463 | /* |
| 2464 | * TODO: sscanf seems not to give the correct |
| 2465 | * value for extremely high/low values. |
| 2466 | * E.g. "1E-149" results in zero. |
| 2467 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2468 | if (sscanf((const char *) value, "%f", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2469 | &(v->value.f)) == 1) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2470 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2471 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2472 | xmlSchemaFreeValue(v); |
| 2473 | goto return1; |
| 2474 | } |
| 2475 | } else { |
| 2476 | goto error; |
| 2477 | } |
| 2478 | } else { |
| 2479 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2480 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2481 | /* |
| 2482 | * TODO: sscanf seems not to give the correct |
| 2483 | * value for extremely high/low values. |
| 2484 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2485 | if (sscanf((const char *) value, "%lf", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2486 | &(v->value.d)) == 1) { |
| 2487 | *val = v; |
| 2488 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2489 | xmlSchemaFreeValue(v); |
| 2490 | goto return1; |
| 2491 | } |
| 2492 | } else { |
| 2493 | goto error; |
| 2494 | } |
| 2495 | } |
| 2496 | } |
| 2497 | goto return0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2498 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2499 | case XML_SCHEMAS_BOOLEAN:{ |
| 2500 | const xmlChar *cur = value; |
| 2501 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2502 | if (normOnTheFly) { |
| 2503 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2504 | if (*cur == '0') { |
| 2505 | ret = 0; |
| 2506 | cur++; |
| 2507 | } else if (*cur == '1') { |
| 2508 | ret = 1; |
| 2509 | cur++; |
| 2510 | } else if (*cur == 't') { |
| 2511 | cur++; |
| 2512 | if ((*cur++ == 'r') && (*cur++ == 'u') && |
| 2513 | (*cur++ == 'e')) { |
| 2514 | ret = 1; |
| 2515 | } else |
| 2516 | goto return1; |
| 2517 | } else if (*cur == 'f') { |
| 2518 | cur++; |
| 2519 | if ((*cur++ == 'a') && (*cur++ == 'l') && |
| 2520 | (*cur++ == 's') && (*cur++ == 'e')) { |
| 2521 | ret = 0; |
| 2522 | } else |
| 2523 | goto return1; |
| 2524 | } |
| 2525 | if (*cur != 0) { |
| 2526 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2527 | if (*cur != 0) |
| 2528 | goto return1; |
| 2529 | } |
| 2530 | } else { |
| 2531 | if ((cur[0] == '0') && (cur[1] == 0)) |
| 2532 | ret = 0; |
| 2533 | else if ((cur[0] == '1') && (cur[1] == 0)) |
| 2534 | ret = 1; |
| 2535 | else if ((cur[0] == 't') && (cur[1] == 'r') |
| 2536 | && (cur[2] == 'u') && (cur[3] == 'e') |
| 2537 | && (cur[4] == 0)) |
| 2538 | ret = 1; |
| 2539 | else if ((cur[0] == 'f') && (cur[1] == 'a') |
| 2540 | && (cur[2] == 'l') && (cur[3] == 's') |
| 2541 | && (cur[4] == 'e') && (cur[5] == 0)) |
| 2542 | ret = 0; |
| 2543 | else |
| 2544 | goto return1; |
| 2545 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2546 | if (val != NULL) { |
| 2547 | v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN); |
| 2548 | if (v != NULL) { |
| 2549 | v->value.b = ret; |
| 2550 | *val = v; |
| 2551 | } else { |
| 2552 | goto error; |
| 2553 | } |
| 2554 | } |
| 2555 | goto return0; |
| 2556 | } |
| 2557 | case XML_SCHEMAS_TOKEN:{ |
| 2558 | const xmlChar *cur = value; |
| 2559 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2560 | if (! normOnTheFly) { |
| 2561 | while (*cur != 0) { |
| 2562 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2563 | goto return1; |
| 2564 | } else if (*cur == ' ') { |
| 2565 | cur++; |
| 2566 | if (*cur == 0) |
| 2567 | goto return1; |
| 2568 | if (*cur == ' ') |
| 2569 | goto return1; |
| 2570 | } else { |
| 2571 | cur++; |
| 2572 | } |
| 2573 | } |
| 2574 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2575 | if (val != NULL) { |
| 2576 | v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN); |
| 2577 | if (v != NULL) { |
| 2578 | v->value.str = xmlStrdup(value); |
| 2579 | *val = v; |
| 2580 | } else { |
| 2581 | goto error; |
| 2582 | } |
| 2583 | } |
| 2584 | goto return0; |
| 2585 | } |
| 2586 | case XML_SCHEMAS_LANGUAGE: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2587 | if (normOnTheFly) { |
| 2588 | norm = xmlSchemaCollapseString(value); |
| 2589 | if (norm != NULL) |
| 2590 | value = norm; |
| 2591 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2592 | if (xmlCheckLanguageID(value) == 1) { |
| 2593 | if (val != NULL) { |
| 2594 | v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE); |
| 2595 | if (v != NULL) { |
| 2596 | v->value.str = xmlStrdup(value); |
| 2597 | *val = v; |
| 2598 | } else { |
| 2599 | goto error; |
| 2600 | } |
| 2601 | } |
| 2602 | goto return0; |
| 2603 | } |
| 2604 | goto return1; |
| 2605 | case XML_SCHEMAS_NMTOKEN: |
| 2606 | if (xmlValidateNMToken(value, 1) == 0) { |
| 2607 | if (val != NULL) { |
| 2608 | v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN); |
| 2609 | if (v != NULL) { |
| 2610 | v->value.str = xmlStrdup(value); |
| 2611 | *val = v; |
| 2612 | } else { |
| 2613 | goto error; |
| 2614 | } |
| 2615 | } |
| 2616 | goto return0; |
| 2617 | } |
| 2618 | goto return1; |
| 2619 | case XML_SCHEMAS_NMTOKENS: |
| 2620 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef, |
| 2621 | value, val, node); |
| 2622 | if (ret > 0) |
| 2623 | ret = 0; |
| 2624 | else |
| 2625 | ret = 1; |
| 2626 | goto done; |
| 2627 | case XML_SCHEMAS_NAME: |
| 2628 | ret = xmlValidateName(value, 1); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 2629 | if ((ret == 0) && (val != NULL) && (value != NULL)) { |
| 2630 | v = xmlSchemaNewValue(XML_SCHEMAS_NAME); |
| 2631 | if (v != NULL) { |
| 2632 | const xmlChar *start = value, *end; |
| 2633 | while (IS_BLANK_CH(*start)) start++; |
| 2634 | end = start; |
| 2635 | while ((*end != 0) && (!IS_BLANK_CH(*end))) end++; |
| 2636 | v->value.str = xmlStrndup(start, end - start); |
| 2637 | *val = v; |
| 2638 | } else { |
| 2639 | goto error; |
| 2640 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2641 | } |
| 2642 | goto done; |
| 2643 | case XML_SCHEMAS_QNAME:{ |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2644 | const xmlChar *uri = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2645 | xmlChar *local = NULL; |
| 2646 | |
| 2647 | ret = xmlValidateQName(value, 1); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2648 | if (ret != 0) |
| 2649 | goto done; |
| 2650 | if (node != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2651 | xmlChar *prefix; |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2652 | xmlNsPtr ns; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2653 | |
| 2654 | local = xmlSplitQName2(value, &prefix); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2655 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2656 | if ((ns == NULL) && (prefix != NULL)) { |
| 2657 | xmlFree(prefix); |
| 2658 | if (local != NULL) |
| 2659 | xmlFree(local); |
| 2660 | goto return1; |
| 2661 | } |
| 2662 | if (ns != NULL) |
| 2663 | uri = ns->href; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2664 | if (prefix != NULL) |
| 2665 | xmlFree(prefix); |
| 2666 | } |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2667 | if (val != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2668 | v = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2669 | if (v == NULL) { |
| 2670 | if (local != NULL) |
| 2671 | xmlFree(local); |
| 2672 | goto error; |
| 2673 | } |
| 2674 | if (local != NULL) |
| 2675 | v->value.qname.name = local; |
| 2676 | else |
| 2677 | v->value.qname.name = xmlStrdup(value); |
| 2678 | if (uri != NULL) |
| 2679 | v->value.qname.uri = xmlStrdup(uri); |
| 2680 | *val = v; |
| 2681 | } else |
| 2682 | if (local != NULL) |
| 2683 | xmlFree(local); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2684 | goto done; |
| 2685 | } |
| 2686 | case XML_SCHEMAS_NCNAME: |
| 2687 | ret = xmlValidateNCName(value, 1); |
| 2688 | if ((ret == 0) && (val != NULL)) { |
| 2689 | v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME); |
| 2690 | if (v != NULL) { |
| 2691 | v->value.str = xmlStrdup(value); |
| 2692 | *val = v; |
| 2693 | } else { |
| 2694 | goto error; |
| 2695 | } |
| 2696 | } |
| 2697 | goto done; |
| 2698 | case XML_SCHEMAS_ID: |
| 2699 | ret = xmlValidateNCName(value, 1); |
| 2700 | if ((ret == 0) && (val != NULL)) { |
| 2701 | v = xmlSchemaNewValue(XML_SCHEMAS_ID); |
| 2702 | if (v != NULL) { |
| 2703 | v->value.str = xmlStrdup(value); |
| 2704 | *val = v; |
| 2705 | } else { |
| 2706 | goto error; |
| 2707 | } |
| 2708 | } |
| 2709 | if ((ret == 0) && (node != NULL) && |
| 2710 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2711 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2712 | |
| 2713 | /* |
| 2714 | * NOTE: the IDness might have already be declared in the DTD |
| 2715 | */ |
| 2716 | if (attr->atype != XML_ATTRIBUTE_ID) { |
| 2717 | xmlIDPtr res; |
| 2718 | xmlChar *strip; |
| 2719 | |
| 2720 | strip = xmlSchemaStrip(value); |
| 2721 | if (strip != NULL) { |
| 2722 | res = xmlAddID(NULL, node->doc, strip, attr); |
| 2723 | xmlFree(strip); |
| 2724 | } else |
| 2725 | res = xmlAddID(NULL, node->doc, value, attr); |
| 2726 | if (res == NULL) { |
| 2727 | ret = 2; |
| 2728 | } else { |
| 2729 | attr->atype = XML_ATTRIBUTE_ID; |
| 2730 | } |
| 2731 | } |
| 2732 | } |
| 2733 | goto done; |
| 2734 | case XML_SCHEMAS_IDREF: |
| 2735 | ret = xmlValidateNCName(value, 1); |
| 2736 | if ((ret == 0) && (val != NULL)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 2737 | v = xmlSchemaNewValue(XML_SCHEMAS_IDREF); |
| 2738 | if (v == NULL) |
| 2739 | goto error; |
| 2740 | v->value.str = xmlStrdup(value); |
| 2741 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2742 | } |
| 2743 | if ((ret == 0) && (node != NULL) && |
| 2744 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2745 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2746 | xmlChar *strip; |
| 2747 | |
| 2748 | strip = xmlSchemaStrip(value); |
| 2749 | if (strip != NULL) { |
| 2750 | xmlAddRef(NULL, node->doc, strip, attr); |
| 2751 | xmlFree(strip); |
| 2752 | } else |
| 2753 | xmlAddRef(NULL, node->doc, value, attr); |
| 2754 | attr->atype = XML_ATTRIBUTE_IDREF; |
| 2755 | } |
| 2756 | goto done; |
| 2757 | case XML_SCHEMAS_IDREFS: |
| 2758 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef, |
| 2759 | value, val, node); |
| 2760 | if (ret < 0) |
| 2761 | ret = 2; |
| 2762 | else |
| 2763 | ret = 0; |
| 2764 | if ((ret == 0) && (node != NULL) && |
| 2765 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2766 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2767 | |
| 2768 | attr->atype = XML_ATTRIBUTE_IDREFS; |
| 2769 | } |
| 2770 | goto done; |
| 2771 | case XML_SCHEMAS_ENTITY:{ |
| 2772 | xmlChar *strip; |
| 2773 | |
| 2774 | ret = xmlValidateNCName(value, 1); |
| 2775 | if ((node == NULL) || (node->doc == NULL)) |
| 2776 | ret = 3; |
| 2777 | if (ret == 0) { |
| 2778 | xmlEntityPtr ent; |
| 2779 | |
| 2780 | strip = xmlSchemaStrip(value); |
| 2781 | if (strip != NULL) { |
| 2782 | ent = xmlGetDocEntity(node->doc, strip); |
| 2783 | xmlFree(strip); |
| 2784 | } else { |
| 2785 | ent = xmlGetDocEntity(node->doc, value); |
| 2786 | } |
| 2787 | if ((ent == NULL) || |
| 2788 | (ent->etype != |
| 2789 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) |
| 2790 | ret = 4; |
| 2791 | } |
| 2792 | if ((ret == 0) && (val != NULL)) { |
| 2793 | TODO; |
| 2794 | } |
| 2795 | if ((ret == 0) && (node != NULL) && |
| 2796 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2797 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2798 | |
| 2799 | attr->atype = XML_ATTRIBUTE_ENTITY; |
| 2800 | } |
| 2801 | goto done; |
| 2802 | } |
| 2803 | case XML_SCHEMAS_ENTITIES: |
| 2804 | if ((node == NULL) || (node->doc == NULL)) |
| 2805 | goto return3; |
| 2806 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef, |
| 2807 | value, val, node); |
| 2808 | if (ret <= 0) |
| 2809 | ret = 1; |
| 2810 | else |
| 2811 | ret = 0; |
| 2812 | if ((ret == 0) && (node != NULL) && |
| 2813 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2814 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2815 | |
| 2816 | attr->atype = XML_ATTRIBUTE_ENTITIES; |
| 2817 | } |
| 2818 | goto done; |
| 2819 | case XML_SCHEMAS_NOTATION:{ |
| 2820 | xmlChar *uri = NULL; |
| 2821 | xmlChar *local = NULL; |
| 2822 | |
| 2823 | ret = xmlValidateQName(value, 1); |
| 2824 | if ((ret == 0) && (node != NULL)) { |
| 2825 | xmlChar *prefix; |
| 2826 | |
| 2827 | local = xmlSplitQName2(value, &prefix); |
| 2828 | if (prefix != NULL) { |
| 2829 | xmlNsPtr ns; |
| 2830 | |
| 2831 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2832 | if (ns == NULL) |
| 2833 | ret = 1; |
| 2834 | else if (val != NULL) |
| 2835 | uri = xmlStrdup(ns->href); |
| 2836 | } |
| 2837 | if ((local != NULL) && ((val == NULL) || (ret != 0))) |
| 2838 | xmlFree(local); |
| 2839 | if (prefix != NULL) |
| 2840 | xmlFree(prefix); |
| 2841 | } |
| 2842 | if ((node == NULL) || (node->doc == NULL)) |
| 2843 | ret = 3; |
| 2844 | if (ret == 0) { |
| 2845 | ret = xmlValidateNotationUse(NULL, node->doc, value); |
| 2846 | if (ret == 1) |
| 2847 | ret = 0; |
| 2848 | else |
| 2849 | ret = 1; |
| 2850 | } |
| 2851 | if ((ret == 0) && (val != NULL)) { |
| 2852 | v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 2853 | if (v != NULL) { |
| 2854 | if (local != NULL) |
| 2855 | v->value.qname.name = local; |
| 2856 | else |
| 2857 | v->value.qname.name = xmlStrdup(value); |
| 2858 | if (uri != NULL) |
| 2859 | v->value.qname.uri = uri; |
| 2860 | |
| 2861 | *val = v; |
| 2862 | } else { |
| 2863 | if (local != NULL) |
| 2864 | xmlFree(local); |
| 2865 | if (uri != NULL) |
| 2866 | xmlFree(uri); |
| 2867 | goto error; |
| 2868 | } |
| 2869 | } |
| 2870 | goto done; |
| 2871 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2872 | case XML_SCHEMAS_ANYURI:{ |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2873 | if (*value != 0) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2874 | xmlURIPtr uri; |
| 2875 | if (normOnTheFly) { |
| 2876 | norm = xmlSchemaCollapseString(value); |
| 2877 | if (norm != NULL) |
| 2878 | value = norm; |
| 2879 | } |
| 2880 | uri = xmlParseURI((const char *) value); |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2881 | if (uri == NULL) |
| 2882 | goto return1; |
| 2883 | xmlFreeURI(uri); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2884 | } |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2885 | |
| 2886 | if (val != NULL) { |
| 2887 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI); |
| 2888 | if (v == NULL) |
| 2889 | goto error; |
| 2890 | v->value.str = xmlStrdup(value); |
| 2891 | *val = v; |
| 2892 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2893 | goto return0; |
| 2894 | } |
| 2895 | case XML_SCHEMAS_HEXBINARY:{ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2896 | const xmlChar *cur = value, *start; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2897 | xmlChar *base; |
| 2898 | int total, i = 0; |
| 2899 | |
Daniel Veillard | f34a20e | 2004-08-31 08:42:17 +0000 | [diff] [blame] | 2900 | if (cur == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2901 | goto return1; |
| 2902 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2903 | if (normOnTheFly) |
| 2904 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2905 | |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2906 | start = cur; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2907 | while (((*cur >= '0') && (*cur <= '9')) || |
| 2908 | ((*cur >= 'A') && (*cur <= 'F')) || |
| 2909 | ((*cur >= 'a') && (*cur <= 'f'))) { |
| 2910 | i++; |
| 2911 | cur++; |
| 2912 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2913 | if (normOnTheFly) |
| 2914 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2915 | |
| 2916 | if (*cur != 0) |
| 2917 | goto return1; |
| 2918 | if ((i % 2) != 0) |
| 2919 | goto return1; |
| 2920 | |
| 2921 | if (val != NULL) { |
| 2922 | |
| 2923 | v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY); |
| 2924 | if (v == NULL) |
| 2925 | goto error; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2926 | /* |
| 2927 | * Copy only the normalized piece. |
| 2928 | * CRITICAL TODO: Check this. |
| 2929 | */ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2930 | cur = xmlStrndup(start, i); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2931 | if (cur == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2932 | xmlSchemaTypeErrMemory(node, "allocating hexbin data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2933 | xmlFree(v); |
| 2934 | goto return1; |
| 2935 | } |
| 2936 | |
| 2937 | total = i / 2; /* number of octets */ |
| 2938 | |
| 2939 | base = (xmlChar *) cur; |
| 2940 | while (i-- > 0) { |
| 2941 | if (*base >= 'a') |
| 2942 | *base = *base - ('a' - 'A'); |
| 2943 | base++; |
| 2944 | } |
| 2945 | |
| 2946 | v->value.hex.str = (xmlChar *) cur; |
| 2947 | v->value.hex.total = total; |
| 2948 | *val = v; |
| 2949 | } |
| 2950 | goto return0; |
| 2951 | } |
| 2952 | case XML_SCHEMAS_BASE64BINARY:{ |
| 2953 | /* ISSUE: |
| 2954 | * |
| 2955 | * Ignore all stray characters? (yes, currently) |
| 2956 | * Worry about long lines? (no, currently) |
| 2957 | * |
| 2958 | * rfc2045.txt: |
| 2959 | * |
| 2960 | * "The encoded output stream must be represented in lines of |
| 2961 | * no more than 76 characters each. All line breaks or other |
| 2962 | * characters not found in Table 1 must be ignored by decoding |
| 2963 | * software. In base64 data, characters other than those in |
| 2964 | * Table 1, line breaks, and other white space probably |
| 2965 | * indicate a transmission error, about which a warning |
| 2966 | * message or even a message rejection might be appropriate |
| 2967 | * under some circumstances." */ |
| 2968 | const xmlChar *cur = value; |
| 2969 | xmlChar *base; |
| 2970 | int total, i = 0, pad = 0; |
| 2971 | |
| 2972 | if (cur == NULL) |
| 2973 | goto return1; |
| 2974 | |
| 2975 | for (; *cur; ++cur) { |
| 2976 | int decc; |
| 2977 | |
| 2978 | decc = _xmlSchemaBase64Decode(*cur); |
| 2979 | if (decc < 0) ; |
| 2980 | else if (decc < 64) |
| 2981 | i++; |
| 2982 | else |
| 2983 | break; |
| 2984 | } |
| 2985 | for (; *cur; ++cur) { |
| 2986 | int decc; |
| 2987 | |
| 2988 | decc = _xmlSchemaBase64Decode(*cur); |
| 2989 | if (decc < 0) ; |
| 2990 | else if (decc < 64) |
| 2991 | goto return1; |
| 2992 | if (decc == 64) |
| 2993 | pad++; |
| 2994 | } |
| 2995 | |
| 2996 | /* rfc2045.txt: "Special processing is performed if fewer than |
| 2997 | * 24 bits are available at the end of the data being encoded. |
| 2998 | * A full encoding quantum is always completed at the end of a |
| 2999 | * body. When fewer than 24 input bits are available in an |
| 3000 | * input group, zero bits are added (on the right) to form an |
| 3001 | * integral number of 6-bit groups. Padding at the end of the |
| 3002 | * data is performed using the "=" character. Since all |
| 3003 | * base64 input is an integral number of octets, only the |
| 3004 | * following cases can arise: (1) the final quantum of |
| 3005 | * encoding input is an integral multiple of 24 bits; here, |
| 3006 | * the final unit of encoded output will be an integral |
| 3007 | * multiple ofindent: Standard input:701: Warning:old style |
| 3008 | * assignment ambiguity in "=*". Assuming "= *" 4 characters |
| 3009 | * with no "=" padding, (2) the final |
| 3010 | * quantum of encoding input is exactly 8 bits; here, the |
| 3011 | * final unit of encoded output will be two characters |
| 3012 | * followed by two "=" padding characters, or (3) the final |
| 3013 | * quantum of encoding input is exactly 16 bits; here, the |
| 3014 | * final unit of encoded output will be three characters |
| 3015 | * followed by one "=" padding character." */ |
| 3016 | |
| 3017 | total = 3 * (i / 4); |
| 3018 | if (pad == 0) { |
| 3019 | if (i % 4 != 0) |
| 3020 | goto return1; |
| 3021 | } else if (pad == 1) { |
| 3022 | int decc; |
| 3023 | |
| 3024 | if (i % 4 != 3) |
| 3025 | goto return1; |
| 3026 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3027 | (decc < 0) || (decc > 63); |
| 3028 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3029 | --cur; |
| 3030 | /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/ |
| 3031 | /* 00111100 -> 0x3c */ |
| 3032 | if (decc & ~0x3c) |
| 3033 | goto return1; |
| 3034 | total += 2; |
| 3035 | } else if (pad == 2) { |
| 3036 | int decc; |
| 3037 | |
| 3038 | if (i % 4 != 2) |
| 3039 | goto return1; |
| 3040 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3041 | (decc < 0) || (decc > 63); |
| 3042 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3043 | --cur; |
| 3044 | /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */ |
| 3045 | /* 00110000 -> 0x30 */ |
| 3046 | if (decc & ~0x30) |
| 3047 | goto return1; |
| 3048 | total += 1; |
| 3049 | } else |
| 3050 | goto return1; |
| 3051 | |
| 3052 | if (val != NULL) { |
| 3053 | v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY); |
| 3054 | if (v == NULL) |
| 3055 | goto error; |
| 3056 | base = |
| 3057 | (xmlChar *) xmlMallocAtomic((i + pad + 1) * |
| 3058 | sizeof(xmlChar)); |
| 3059 | if (base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3060 | xmlSchemaTypeErrMemory(node, "allocating base64 data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3061 | xmlFree(v); |
| 3062 | goto return1; |
| 3063 | } |
| 3064 | v->value.base64.str = base; |
| 3065 | for (cur = value; *cur; ++cur) |
| 3066 | if (_xmlSchemaBase64Decode(*cur) >= 0) { |
| 3067 | *base = *cur; |
| 3068 | ++base; |
| 3069 | } |
| 3070 | *base = 0; |
| 3071 | v->value.base64.total = total; |
| 3072 | *val = v; |
| 3073 | } |
| 3074 | goto return0; |
| 3075 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3076 | case XML_SCHEMAS_INTEGER: |
| 3077 | case XML_SCHEMAS_PINTEGER: |
| 3078 | case XML_SCHEMAS_NPINTEGER: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3079 | case XML_SCHEMAS_NINTEGER: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3080 | case XML_SCHEMAS_NNINTEGER:{ |
| 3081 | const xmlChar *cur = value; |
| 3082 | unsigned long lo, mi, hi; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3083 | int sign = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3084 | |
| 3085 | if (cur == NULL) |
| 3086 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3087 | if (normOnTheFly) |
| 3088 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3089 | if (*cur == '-') { |
| 3090 | sign = 1; |
| 3091 | cur++; |
| 3092 | } else if (*cur == '+') |
| 3093 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3094 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3095 | if (ret == -1) |
| 3096 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3097 | if (normOnTheFly) |
| 3098 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3099 | if (*cur != 0) |
| 3100 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3101 | if (type->builtInType == XML_SCHEMAS_NPINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3102 | if ((sign == 0) && |
| 3103 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3104 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3105 | } else if (type->builtInType == XML_SCHEMAS_PINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3106 | if (sign == 1) |
| 3107 | goto return1; |
| 3108 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3109 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3110 | } else if (type->builtInType == XML_SCHEMAS_NINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3111 | if (sign == 0) |
| 3112 | goto return1; |
| 3113 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3114 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3115 | } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3116 | if ((sign == 1) && |
| 3117 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3118 | goto return1; |
| 3119 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3120 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3121 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3122 | if (v != NULL) { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3123 | if (ret == 0) |
| 3124 | ret++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3125 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3126 | v->value.decimal.mi = mi; |
| 3127 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3128 | v->value.decimal.sign = sign; |
| 3129 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3130 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3131 | *val = v; |
| 3132 | } |
| 3133 | } |
| 3134 | goto return0; |
| 3135 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3136 | case XML_SCHEMAS_LONG: |
| 3137 | case XML_SCHEMAS_BYTE: |
| 3138 | case XML_SCHEMAS_SHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3139 | case XML_SCHEMAS_INT:{ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 3140 | const xmlChar *cur = value; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3141 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3142 | int sign = 0; |
| 3143 | |
| 3144 | if (cur == NULL) |
| 3145 | goto return1; |
| 3146 | if (*cur == '-') { |
| 3147 | sign = 1; |
| 3148 | cur++; |
| 3149 | } else if (*cur == '+') |
| 3150 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3151 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3152 | if (ret < 0) |
| 3153 | goto return1; |
| 3154 | if (*cur != 0) |
| 3155 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3156 | if (type->builtInType == XML_SCHEMAS_LONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3157 | if (hi >= 922) { |
| 3158 | if (hi > 922) |
| 3159 | goto return1; |
| 3160 | if (mi >= 33720368) { |
| 3161 | if (mi > 33720368) |
| 3162 | goto return1; |
| 3163 | if ((sign == 0) && (lo > 54775807)) |
| 3164 | goto return1; |
| 3165 | if ((sign == 1) && (lo > 54775808)) |
| 3166 | goto return1; |
| 3167 | } |
| 3168 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3169 | } else if (type->builtInType == XML_SCHEMAS_INT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3170 | if (hi != 0) |
| 3171 | goto return1; |
| 3172 | if (mi >= 21) { |
| 3173 | if (mi > 21) |
| 3174 | goto return1; |
| 3175 | if ((sign == 0) && (lo > 47483647)) |
| 3176 | goto return1; |
| 3177 | if ((sign == 1) && (lo > 47483648)) |
| 3178 | goto return1; |
| 3179 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3180 | } else if (type->builtInType == XML_SCHEMAS_SHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3181 | if ((mi != 0) || (hi != 0)) |
| 3182 | goto return1; |
| 3183 | if ((sign == 1) && (lo > 32768)) |
| 3184 | goto return1; |
| 3185 | if ((sign == 0) && (lo > 32767)) |
| 3186 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3187 | } else if (type->builtInType == XML_SCHEMAS_BYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3188 | if ((mi != 0) || (hi != 0)) |
| 3189 | goto return1; |
| 3190 | if ((sign == 1) && (lo > 128)) |
| 3191 | goto return1; |
| 3192 | if ((sign == 0) && (lo > 127)) |
| 3193 | goto return1; |
| 3194 | } |
| 3195 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3196 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3197 | if (v != NULL) { |
| 3198 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3199 | v->value.decimal.mi = mi; |
| 3200 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3201 | v->value.decimal.sign = sign; |
| 3202 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3203 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3204 | *val = v; |
| 3205 | } |
| 3206 | } |
| 3207 | goto return0; |
| 3208 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3209 | case XML_SCHEMAS_UINT: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3210 | case XML_SCHEMAS_ULONG: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3211 | case XML_SCHEMAS_USHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3212 | case XML_SCHEMAS_UBYTE:{ |
| 3213 | const xmlChar *cur = value; |
| 3214 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3215 | |
| 3216 | if (cur == NULL) |
| 3217 | goto return1; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3218 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3219 | if (ret < 0) |
| 3220 | goto return1; |
| 3221 | if (*cur != 0) |
| 3222 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3223 | if (type->builtInType == XML_SCHEMAS_ULONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3224 | if (hi >= 1844) { |
| 3225 | if (hi > 1844) |
| 3226 | goto return1; |
| 3227 | if (mi >= 67440737) { |
| 3228 | if (mi > 67440737) |
| 3229 | goto return1; |
| 3230 | if (lo > 9551615) |
| 3231 | goto return1; |
| 3232 | } |
| 3233 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3234 | } else if (type->builtInType == XML_SCHEMAS_UINT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3235 | if (hi != 0) |
| 3236 | goto return1; |
| 3237 | if (mi >= 42) { |
| 3238 | if (mi > 42) |
| 3239 | goto return1; |
| 3240 | if (lo > 94967295) |
| 3241 | goto return1; |
| 3242 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3243 | } else if (type->builtInType == XML_SCHEMAS_USHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3244 | if ((mi != 0) || (hi != 0)) |
| 3245 | goto return1; |
| 3246 | if (lo > 65535) |
| 3247 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3248 | } else if (type->builtInType == XML_SCHEMAS_UBYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3249 | if ((mi != 0) || (hi != 0)) |
| 3250 | goto return1; |
| 3251 | if (lo > 255) |
| 3252 | goto return1; |
| 3253 | } |
| 3254 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3255 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3256 | if (v != NULL) { |
| 3257 | v->value.decimal.lo = lo; |
| 3258 | v->value.decimal.mi = mi; |
| 3259 | v->value.decimal.hi = hi; |
| 3260 | v->value.decimal.sign = 0; |
| 3261 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3262 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3263 | *val = v; |
| 3264 | } |
| 3265 | } |
| 3266 | goto return0; |
| 3267 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3268 | } |
| 3269 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3270 | done: |
| 3271 | if (norm != NULL) |
| 3272 | xmlFree(norm); |
| 3273 | return (ret); |
| 3274 | return3: |
| 3275 | if (norm != NULL) |
| 3276 | xmlFree(norm); |
| 3277 | return (3); |
| 3278 | return1: |
| 3279 | if (norm != NULL) |
| 3280 | xmlFree(norm); |
| 3281 | return (1); |
| 3282 | return0: |
| 3283 | if (norm != NULL) |
| 3284 | xmlFree(norm); |
| 3285 | return (0); |
| 3286 | error: |
| 3287 | if (norm != NULL) |
| 3288 | xmlFree(norm); |
| 3289 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3293 | * xmlSchemaValPredefTypeNode: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3294 | * @type: the predefined type |
| 3295 | * @value: the value to check |
| 3296 | * @val: the return computed value |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3297 | * @node: the node containing the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3298 | * |
| 3299 | * Check that a value conforms to the lexical space of the predefined type. |
| 3300 | * if true a value is computed and returned in @val. |
| 3301 | * |
| 3302 | * Returns 0 if this validates, a positive error code number otherwise |
| 3303 | * and -1 in case of internal or API error. |
| 3304 | */ |
| 3305 | int |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3306 | xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 3307 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3308 | return(xmlSchemaValAtomicType(type, value, val, node, 0, |
| 3309 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | /** |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3313 | * xmlSchemaValPredefTypeNodeNoNorm: |
| 3314 | * @type: the predefined type |
| 3315 | * @value: the value to check |
| 3316 | * @val: the return computed value |
| 3317 | * @node: the node containing the value |
| 3318 | * |
| 3319 | * Check that a value conforms to the lexical space of the predefined type. |
| 3320 | * if true a value is computed and returned in @val. |
| 3321 | * This one does apply any normalization to the value. |
| 3322 | * |
| 3323 | * Returns 0 if this validates, a positive error code number otherwise |
| 3324 | * and -1 in case of internal or API error. |
| 3325 | */ |
| 3326 | int |
| 3327 | xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value, |
| 3328 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3329 | return(xmlSchemaValAtomicType(type, value, val, node, 1, |
| 3330 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3334 | * xmlSchemaValidatePredefinedType: |
| 3335 | * @type: the predefined type |
| 3336 | * @value: the value to check |
| 3337 | * @val: the return computed value |
| 3338 | * |
| 3339 | * Check that a value conforms to the lexical space of the predefined type. |
| 3340 | * if true a value is computed and returned in @val. |
| 3341 | * |
| 3342 | * Returns 0 if this validates, a positive error code number otherwise |
| 3343 | * and -1 in case of internal or API error. |
| 3344 | */ |
| 3345 | int |
| 3346 | xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value, |
| 3347 | xmlSchemaValPtr *val) { |
| 3348 | return(xmlSchemaValPredefTypeNode(type, value, val, NULL)); |
| 3349 | } |
| 3350 | |
| 3351 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3352 | * xmlSchemaCompareDecimals: |
| 3353 | * @x: a first decimal value |
| 3354 | * @y: a second decimal value |
| 3355 | * |
| 3356 | * Compare 2 decimals |
| 3357 | * |
| 3358 | * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error |
| 3359 | */ |
| 3360 | static int |
| 3361 | xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3362 | { |
| 3363 | xmlSchemaValPtr swp; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3364 | int order = 1, integx, integy, dlen; |
| 3365 | unsigned long hi, mi, lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3366 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3367 | /* |
| 3368 | * First test: If x is -ve and not zero |
| 3369 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3370 | if ((x->value.decimal.sign) && |
| 3371 | ((x->value.decimal.lo != 0) || |
| 3372 | (x->value.decimal.mi != 0) || |
| 3373 | (x->value.decimal.hi != 0))) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3374 | /* |
| 3375 | * Then if y is -ve and not zero reverse the compare |
| 3376 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3377 | if ((y->value.decimal.sign) && |
| 3378 | ((y->value.decimal.lo != 0) || |
| 3379 | (y->value.decimal.mi != 0) || |
| 3380 | (y->value.decimal.hi != 0))) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3381 | order = -1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3382 | /* |
| 3383 | * Otherwise (y >= 0) we have the answer |
| 3384 | */ |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3385 | else |
| 3386 | return (-1); |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3387 | /* |
| 3388 | * If x is not -ve and y is -ve we have the answer |
| 3389 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3390 | } else if ((y->value.decimal.sign) && |
| 3391 | ((y->value.decimal.lo != 0) || |
| 3392 | (y->value.decimal.mi != 0) || |
| 3393 | (y->value.decimal.hi != 0))) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3394 | return (1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3395 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3396 | /* |
| 3397 | * If it's not simply determined by a difference in sign, |
| 3398 | * then we need to compare the actual values of the two nums. |
| 3399 | * To do this, we start by looking at the integral parts. |
| 3400 | * If the number of integral digits differ, then we have our |
| 3401 | * answer. |
| 3402 | */ |
| 3403 | integx = x->value.decimal.total - x->value.decimal.frac; |
| 3404 | integy = y->value.decimal.total - y->value.decimal.frac; |
| 3405 | if (integx > integy) |
| 3406 | return order; |
| 3407 | else if (integy > integx) |
| 3408 | return -order; |
| 3409 | /* |
| 3410 | * If the number of integral digits is the same for both numbers, |
| 3411 | * then things get a little more complicated. We need to "normalize" |
| 3412 | * the numbers in order to properly compare them. To do this, we |
| 3413 | * look at the total length of each number (length => number of |
| 3414 | * significant digits), and divide the "shorter" by 10 (decreasing |
| 3415 | * the length) until they are of equal length. |
| 3416 | */ |
| 3417 | dlen = x->value.decimal.total - y->value.decimal.total; |
| 3418 | if (dlen < 0) { /* y has more digits than x */ |
| 3419 | swp = x; |
| 3420 | hi = y->value.decimal.hi; |
| 3421 | mi = y->value.decimal.mi; |
| 3422 | lo = y->value.decimal.lo; |
| 3423 | dlen = -dlen; |
| 3424 | order = -order; |
| 3425 | } else { /* x has more digits than y */ |
| 3426 | swp = y; |
| 3427 | hi = x->value.decimal.hi; |
| 3428 | mi = x->value.decimal.mi; |
| 3429 | lo = x->value.decimal.lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3430 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3431 | while (dlen > 8) { /* in effect, right shift by 10**8 */ |
| 3432 | lo = mi; |
| 3433 | mi = hi; |
| 3434 | hi = 0; |
| 3435 | dlen -= 8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3436 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3437 | while (dlen > 0) { |
| 3438 | unsigned long rem1, rem2; |
| 3439 | rem1 = (hi % 10) * 100000000L; |
| 3440 | hi = hi / 10; |
| 3441 | rem2 = (mi % 10) * 100000000L; |
| 3442 | mi = (mi + rem1) / 10; |
| 3443 | lo = (lo + rem2) / 10; |
| 3444 | dlen--; |
| 3445 | } |
| 3446 | if (hi > swp->value.decimal.hi) { |
| 3447 | return order; |
| 3448 | } else if (hi == swp->value.decimal.hi) { |
| 3449 | if (mi > swp->value.decimal.mi) { |
| 3450 | return order; |
| 3451 | } else if (mi == swp->value.decimal.mi) { |
| 3452 | if (lo > swp->value.decimal.lo) { |
| 3453 | return order; |
| 3454 | } else if (lo == swp->value.decimal.lo) { |
| 3455 | if (x->value.decimal.total == y->value.decimal.total) { |
| 3456 | return 0; |
| 3457 | } else { |
| 3458 | return order; |
| 3459 | } |
| 3460 | } |
| 3461 | } |
| 3462 | } |
| 3463 | return -order; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3464 | } |
| 3465 | |
| 3466 | /** |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3467 | * xmlSchemaCompareDurations: |
| 3468 | * @x: a first duration value |
| 3469 | * @y: a second duration value |
| 3470 | * |
| 3471 | * Compare 2 durations |
| 3472 | * |
| 3473 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3474 | * case of error |
| 3475 | */ |
| 3476 | static int |
| 3477 | xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3478 | { |
| 3479 | long carry, mon, day; |
| 3480 | double sec; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3481 | int invert = 1; |
| 3482 | long xmon, xday, myear, minday, maxday; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3483 | static const long dayRange [2][12] = { |
| 3484 | { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, }, |
| 3485 | { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} }; |
| 3486 | |
| 3487 | if ((x == NULL) || (y == NULL)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3488 | return -2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3489 | |
| 3490 | /* months */ |
| 3491 | mon = x->value.dur.mon - y->value.dur.mon; |
| 3492 | |
| 3493 | /* seconds */ |
| 3494 | sec = x->value.dur.sec - y->value.dur.sec; |
| 3495 | carry = (long)sec / SECS_PER_DAY; |
| 3496 | sec -= (double)(carry * SECS_PER_DAY); |
| 3497 | |
| 3498 | /* days */ |
| 3499 | day = x->value.dur.day - y->value.dur.day + carry; |
| 3500 | |
| 3501 | /* easy test */ |
| 3502 | if (mon == 0) { |
| 3503 | if (day == 0) |
| 3504 | if (sec == 0.0) |
| 3505 | return 0; |
| 3506 | else if (sec < 0.0) |
| 3507 | return -1; |
| 3508 | else |
| 3509 | return 1; |
| 3510 | else if (day < 0) |
| 3511 | return -1; |
| 3512 | else |
| 3513 | return 1; |
| 3514 | } |
| 3515 | |
| 3516 | if (mon > 0) { |
| 3517 | if ((day >= 0) && (sec >= 0.0)) |
| 3518 | return 1; |
| 3519 | else { |
| 3520 | xmon = mon; |
| 3521 | xday = -day; |
| 3522 | } |
| 3523 | } else if ((day <= 0) && (sec <= 0.0)) { |
| 3524 | return -1; |
| 3525 | } else { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3526 | invert = -1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3527 | xmon = -mon; |
| 3528 | xday = day; |
| 3529 | } |
| 3530 | |
| 3531 | myear = xmon / 12; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3532 | if (myear == 0) { |
| 3533 | minday = 0; |
| 3534 | maxday = 0; |
| 3535 | } else { |
| 3536 | maxday = 366 * ((myear + 3) / 4) + |
| 3537 | 365 * ((myear - 1) % 4); |
| 3538 | minday = maxday - 1; |
| 3539 | } |
| 3540 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3541 | xmon = xmon % 12; |
| 3542 | minday += dayRange[0][xmon]; |
| 3543 | maxday += dayRange[1][xmon]; |
| 3544 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3545 | if ((maxday == minday) && (maxday == xday)) |
| 3546 | return(0); /* can this really happen ? */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3547 | if (maxday < xday) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3548 | return(-invert); |
| 3549 | if (minday > xday) |
| 3550 | return(invert); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3551 | |
| 3552 | /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3553 | return 2; |
| 3554 | } |
| 3555 | |
| 3556 | /* |
| 3557 | * macros for adding date/times and durations |
| 3558 | */ |
| 3559 | #define FQUOTIENT(a,b) (floor(((double)a/(double)b))) |
| 3560 | #define MODULO(a,b) (a - FQUOTIENT(a,b) * b) |
| 3561 | #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low))) |
| 3562 | #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low) |
| 3563 | |
| 3564 | /** |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3565 | * xmlSchemaDupVal: |
| 3566 | * @v: the #xmlSchemaValPtr value to duplicate |
| 3567 | * |
| 3568 | * Makes a copy of @v. The calling program is responsible for freeing |
| 3569 | * the returned value. |
| 3570 | * |
| 3571 | * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error. |
| 3572 | */ |
| 3573 | static xmlSchemaValPtr |
| 3574 | xmlSchemaDupVal (xmlSchemaValPtr v) |
| 3575 | { |
| 3576 | xmlSchemaValPtr ret = xmlSchemaNewValue(v->type); |
| 3577 | if (ret == NULL) |
| 3578 | return NULL; |
| 3579 | |
| 3580 | memcpy(ret, v, sizeof(xmlSchemaVal)); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3581 | ret->next = NULL; |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3582 | return ret; |
| 3583 | } |
| 3584 | |
| 3585 | /** |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3586 | * xmlSchemaCopyValue: |
| 3587 | * @val: the precomputed value to be copied |
| 3588 | * |
| 3589 | * Copies the precomputed value. This duplicates any string within. |
| 3590 | * |
| 3591 | * Returns the copy or NULL if a copy for a data-type is not implemented. |
| 3592 | */ |
| 3593 | xmlSchemaValPtr |
| 3594 | xmlSchemaCopyValue(xmlSchemaValPtr val) |
| 3595 | { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3596 | xmlSchemaValPtr ret = NULL, prev = NULL, cur; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3597 | |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3598 | /* |
| 3599 | * Copy the string values. |
| 3600 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3601 | while (val != NULL) { |
| 3602 | switch (val->type) { |
| 3603 | case XML_SCHEMAS_ANYTYPE: |
| 3604 | case XML_SCHEMAS_IDREFS: |
| 3605 | case XML_SCHEMAS_ENTITIES: |
| 3606 | case XML_SCHEMAS_NMTOKENS: |
| 3607 | xmlSchemaFreeValue(ret); |
| 3608 | return (NULL); |
| 3609 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 3610 | case XML_SCHEMAS_STRING: |
| 3611 | case XML_SCHEMAS_NORMSTRING: |
| 3612 | case XML_SCHEMAS_TOKEN: |
| 3613 | case XML_SCHEMAS_LANGUAGE: |
| 3614 | case XML_SCHEMAS_NAME: |
| 3615 | case XML_SCHEMAS_NCNAME: |
| 3616 | case XML_SCHEMAS_ID: |
| 3617 | case XML_SCHEMAS_IDREF: |
| 3618 | case XML_SCHEMAS_ENTITY: |
| 3619 | case XML_SCHEMAS_NMTOKEN: |
| 3620 | case XML_SCHEMAS_ANYURI: |
| 3621 | cur = xmlSchemaDupVal(val); |
| 3622 | if (val->value.str != NULL) |
| 3623 | cur->value.str = xmlStrdup(BAD_CAST val->value.str); |
| 3624 | break; |
| 3625 | case XML_SCHEMAS_QNAME: |
| 3626 | case XML_SCHEMAS_NOTATION: |
| 3627 | cur = xmlSchemaDupVal(val); |
| 3628 | if (val->value.qname.name != NULL) |
| 3629 | cur->value.qname.name = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3630 | xmlStrdup(BAD_CAST val->value.qname.name); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3631 | if (val->value.qname.uri != NULL) |
| 3632 | cur->value.qname.uri = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3633 | xmlStrdup(BAD_CAST val->value.qname.uri); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3634 | break; |
| 3635 | case XML_SCHEMAS_HEXBINARY: |
| 3636 | cur = xmlSchemaDupVal(val); |
| 3637 | if (val->value.hex.str != NULL) |
| 3638 | cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str); |
| 3639 | break; |
| 3640 | case XML_SCHEMAS_BASE64BINARY: |
| 3641 | cur = xmlSchemaDupVal(val); |
| 3642 | if (val->value.base64.str != NULL) |
| 3643 | cur->value.base64.str = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3644 | xmlStrdup(BAD_CAST val->value.base64.str); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3645 | break; |
| 3646 | default: |
| 3647 | cur = xmlSchemaDupVal(val); |
| 3648 | break; |
| 3649 | } |
| 3650 | if (ret == NULL) |
| 3651 | ret = cur; |
| 3652 | else |
| 3653 | prev->next = cur; |
| 3654 | prev = cur; |
| 3655 | val = val->next; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3656 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3657 | return (ret); |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3658 | } |
| 3659 | |
| 3660 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3661 | * _xmlSchemaDateAdd: |
| 3662 | * @dt: an #xmlSchemaValPtr |
| 3663 | * @dur: an #xmlSchemaValPtr of type #XS_DURATION |
| 3664 | * |
| 3665 | * Compute a new date/time from @dt and @dur. This function assumes @dt |
| 3666 | * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH, |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3667 | * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as |
| 3668 | * @dt. The calling program is responsible for freeing the returned value. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3669 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3670 | * Returns a pointer to a new #xmlSchemaVal or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3671 | */ |
| 3672 | static xmlSchemaValPtr |
| 3673 | _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur) |
| 3674 | { |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3675 | xmlSchemaValPtr ret, tmp; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3676 | long carry, tempdays, temp; |
| 3677 | xmlSchemaValDatePtr r, d; |
| 3678 | xmlSchemaValDurationPtr u; |
| 3679 | |
| 3680 | if ((dt == NULL) || (dur == NULL)) |
| 3681 | return NULL; |
| 3682 | |
| 3683 | ret = xmlSchemaNewValue(dt->type); |
| 3684 | if (ret == NULL) |
| 3685 | return NULL; |
| 3686 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3687 | /* make a copy so we don't alter the original value */ |
| 3688 | tmp = xmlSchemaDupVal(dt); |
| 3689 | if (tmp == NULL) { |
| 3690 | xmlSchemaFreeValue(ret); |
| 3691 | return NULL; |
| 3692 | } |
| 3693 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3694 | r = &(ret->value.date); |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3695 | d = &(tmp->value.date); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3696 | u = &(dur->value.dur); |
| 3697 | |
| 3698 | /* normalization */ |
| 3699 | if (d->mon == 0) |
| 3700 | d->mon = 1; |
| 3701 | |
| 3702 | /* normalize for time zone offset */ |
| 3703 | u->sec -= (d->tzo * 60); |
| 3704 | d->tzo = 0; |
| 3705 | |
| 3706 | /* normalization */ |
| 3707 | if (d->day == 0) |
| 3708 | d->day = 1; |
| 3709 | |
| 3710 | /* month */ |
| 3711 | carry = d->mon + u->mon; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3712 | r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13); |
| 3713 | carry = (long) FQUOTIENT_RANGE(carry, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3714 | |
| 3715 | /* year (may be modified later) */ |
| 3716 | r->year = d->year + carry; |
| 3717 | if (r->year == 0) { |
| 3718 | if (d->year > 0) |
| 3719 | r->year--; |
| 3720 | else |
| 3721 | r->year++; |
| 3722 | } |
| 3723 | |
| 3724 | /* time zone */ |
| 3725 | r->tzo = d->tzo; |
| 3726 | r->tz_flag = d->tz_flag; |
| 3727 | |
| 3728 | /* seconds */ |
| 3729 | r->sec = d->sec + u->sec; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3730 | carry = (long) FQUOTIENT((long)r->sec, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3731 | if (r->sec != 0.0) { |
| 3732 | r->sec = MODULO(r->sec, 60.0); |
| 3733 | } |
| 3734 | |
| 3735 | /* minute */ |
| 3736 | carry += d->min; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3737 | r->min = (unsigned int) MODULO(carry, 60); |
| 3738 | carry = (long) FQUOTIENT(carry, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3739 | |
| 3740 | /* hours */ |
| 3741 | carry += d->hour; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3742 | r->hour = (unsigned int) MODULO(carry, 24); |
| 3743 | carry = (long)FQUOTIENT(carry, 24); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3744 | |
| 3745 | /* |
| 3746 | * days |
| 3747 | * Note we use tempdays because the temporary values may need more |
| 3748 | * than 5 bits |
| 3749 | */ |
| 3750 | if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) && |
| 3751 | (d->day > MAX_DAYINMONTH(r->year, r->mon))) |
| 3752 | tempdays = MAX_DAYINMONTH(r->year, r->mon); |
| 3753 | else if (d->day < 1) |
| 3754 | tempdays = 1; |
| 3755 | else |
| 3756 | tempdays = d->day; |
| 3757 | |
| 3758 | tempdays += u->day + carry; |
| 3759 | |
| 3760 | while (1) { |
| 3761 | if (tempdays < 1) { |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3762 | long tmon = (long) MODULO_RANGE(r->mon-1, 1, 13); |
| 3763 | long tyr = r->year + (long)FQUOTIENT_RANGE(r->mon-1, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3764 | if (tyr == 0) |
| 3765 | tyr--; |
| 3766 | tempdays += MAX_DAYINMONTH(tyr, tmon); |
| 3767 | carry = -1; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3768 | } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3769 | tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon); |
| 3770 | carry = 1; |
| 3771 | } else |
| 3772 | break; |
| 3773 | |
| 3774 | temp = r->mon + carry; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3775 | r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13); |
| 3776 | r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3777 | if (r->year == 0) { |
| 3778 | if (temp < 1) |
| 3779 | r->year--; |
| 3780 | else |
| 3781 | r->year++; |
| 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | r->day = tempdays; |
| 3786 | |
| 3787 | /* |
| 3788 | * adjust the date/time type to the date values |
| 3789 | */ |
| 3790 | if (ret->type != XML_SCHEMAS_DATETIME) { |
| 3791 | if ((r->hour) || (r->min) || (r->sec)) |
| 3792 | ret->type = XML_SCHEMAS_DATETIME; |
| 3793 | else if (ret->type != XML_SCHEMAS_DATE) { |
| 3794 | if ((r->mon != 1) && (r->day != 1)) |
| 3795 | ret->type = XML_SCHEMAS_DATE; |
| 3796 | else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1)) |
| 3797 | ret->type = XML_SCHEMAS_GYEARMONTH; |
| 3798 | } |
| 3799 | } |
| 3800 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3801 | xmlSchemaFreeValue(tmp); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3802 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3803 | return ret; |
| 3804 | } |
| 3805 | |
| 3806 | /** |
| 3807 | * xmlSchemaDateNormalize: |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3808 | * @dt: an #xmlSchemaValPtr of a date/time type value. |
| 3809 | * @offset: number of seconds to adjust @dt by. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3810 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3811 | * Normalize @dt to GMT time. The @offset parameter is subtracted from |
| 3812 | * the return value is a time-zone offset is present on @dt. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3813 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3814 | * Returns a normalized copy of @dt or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3815 | */ |
| 3816 | static xmlSchemaValPtr |
| 3817 | xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset) |
| 3818 | { |
| 3819 | xmlSchemaValPtr dur, ret; |
| 3820 | |
| 3821 | if (dt == NULL) |
| 3822 | return NULL; |
| 3823 | |
| 3824 | if (((dt->type != XML_SCHEMAS_TIME) && |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3825 | (dt->type != XML_SCHEMAS_DATETIME) && |
| 3826 | (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3827 | return xmlSchemaDupVal(dt); |
| 3828 | |
| 3829 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 3830 | if (dur == NULL) |
| 3831 | return NULL; |
| 3832 | |
| 3833 | dur->value.date.sec -= offset; |
| 3834 | |
| 3835 | ret = _xmlSchemaDateAdd(dt, dur); |
| 3836 | if (ret == NULL) |
| 3837 | return NULL; |
| 3838 | |
| 3839 | xmlSchemaFreeValue(dur); |
| 3840 | |
| 3841 | /* ret->value.date.tzo = 0; */ |
| 3842 | return ret; |
| 3843 | } |
| 3844 | |
| 3845 | /** |
| 3846 | * _xmlSchemaDateCastYMToDays: |
| 3847 | * @dt: an #xmlSchemaValPtr |
| 3848 | * |
| 3849 | * Convert mon and year of @dt to total number of days. Take the |
| 3850 | * number of years since (or before) 1 AD and add the number of leap |
| 3851 | * years. This is a function because negative |
| 3852 | * years must be handled a little differently and there is no zero year. |
| 3853 | * |
| 3854 | * Returns number of days. |
| 3855 | */ |
| 3856 | static long |
| 3857 | _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt) |
| 3858 | { |
| 3859 | long ret; |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3860 | int mon; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3861 | |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3862 | mon = dt->value.date.mon; |
| 3863 | if (mon <= 0) mon = 1; /* normalization */ |
| 3864 | |
| 3865 | if (dt->value.date.year <= 0) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3866 | ret = (dt->value.date.year * 365) + |
| 3867 | (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+ |
| 3868 | ((dt->value.date.year+1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3869 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3870 | else |
| 3871 | ret = ((dt->value.date.year-1) * 365) + |
| 3872 | (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+ |
| 3873 | ((dt->value.date.year-1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3874 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3875 | |
| 3876 | return ret; |
| 3877 | } |
| 3878 | |
| 3879 | /** |
| 3880 | * TIME_TO_NUMBER: |
| 3881 | * @dt: an #xmlSchemaValPtr |
| 3882 | * |
| 3883 | * Calculates the number of seconds in the time portion of @dt. |
| 3884 | * |
| 3885 | * Returns seconds. |
| 3886 | */ |
| 3887 | #define TIME_TO_NUMBER(dt) \ |
| 3888 | ((double)((dt->value.date.hour * SECS_PER_HOUR) + \ |
Daniel Veillard | b3721c2 | 2003-03-31 11:22:25 +0000 | [diff] [blame] | 3889 | (dt->value.date.min * SECS_PER_MIN) + \ |
| 3890 | (dt->value.date.tzo * SECS_PER_MIN)) + \ |
| 3891 | dt->value.date.sec) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3892 | |
| 3893 | /** |
| 3894 | * xmlSchemaCompareDates: |
| 3895 | * @x: a first date/time value |
| 3896 | * @y: a second date/time value |
| 3897 | * |
| 3898 | * Compare 2 date/times |
| 3899 | * |
| 3900 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3901 | * case of error |
| 3902 | */ |
| 3903 | static int |
| 3904 | xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3905 | { |
| 3906 | unsigned char xmask, ymask, xor_mask, and_mask; |
| 3907 | xmlSchemaValPtr p1, p2, q1, q2; |
| 3908 | long p1d, p2d, q1d, q2d; |
| 3909 | |
| 3910 | if ((x == NULL) || (y == NULL)) |
| 3911 | return -2; |
| 3912 | |
| 3913 | if (x->value.date.tz_flag) { |
| 3914 | |
| 3915 | if (!y->value.date.tz_flag) { |
| 3916 | p1 = xmlSchemaDateNormalize(x, 0); |
| 3917 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3918 | /* normalize y + 14:00 */ |
| 3919 | q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR)); |
| 3920 | |
| 3921 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3922 | if (p1d < q1d) { |
| 3923 | xmlSchemaFreeValue(p1); |
| 3924 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3925 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3926 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3927 | double sec; |
| 3928 | |
| 3929 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3930 | if (sec < 0.0) { |
| 3931 | xmlSchemaFreeValue(p1); |
| 3932 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3933 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3934 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3935 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3936 | /* normalize y - 14:00 */ |
| 3937 | q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR)); |
| 3938 | q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day; |
| 3939 | if (p1d > q2d) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3940 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3941 | else if (p1d == q2d) { |
| 3942 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2); |
| 3943 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3944 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3945 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3946 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3947 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3948 | xmlSchemaFreeValue(p1); |
| 3949 | xmlSchemaFreeValue(q1); |
| 3950 | xmlSchemaFreeValue(q2); |
| 3951 | if (ret != 0) |
| 3952 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3953 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3954 | } else { |
| 3955 | xmlSchemaFreeValue(p1); |
| 3956 | xmlSchemaFreeValue(q1); |
| 3957 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3958 | } |
| 3959 | } else if (y->value.date.tz_flag) { |
| 3960 | q1 = xmlSchemaDateNormalize(y, 0); |
| 3961 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 3962 | |
| 3963 | /* normalize x - 14:00 */ |
| 3964 | p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR)); |
| 3965 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3966 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3967 | if (p1d < q1d) { |
| 3968 | xmlSchemaFreeValue(p1); |
| 3969 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3970 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3971 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3972 | double sec; |
| 3973 | |
| 3974 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3975 | if (sec < 0.0) { |
| 3976 | xmlSchemaFreeValue(p1); |
| 3977 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3978 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3979 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3980 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3981 | /* normalize x + 14:00 */ |
| 3982 | p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR)); |
| 3983 | p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day; |
| 3984 | |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3985 | if (p2d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3986 | ret = 1; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3987 | } else if (p2d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3988 | sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1); |
| 3989 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3990 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3991 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3992 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3993 | } |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3994 | xmlSchemaFreeValue(p1); |
| 3995 | xmlSchemaFreeValue(q1); |
| 3996 | xmlSchemaFreeValue(p2); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3997 | if (ret != 0) |
| 3998 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3999 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4000 | } else { |
| 4001 | xmlSchemaFreeValue(p1); |
| 4002 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | /* |
| 4007 | * if the same type then calculate the difference |
| 4008 | */ |
| 4009 | if (x->type == y->type) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4010 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4011 | q1 = xmlSchemaDateNormalize(y, 0); |
| 4012 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 4013 | |
| 4014 | p1 = xmlSchemaDateNormalize(x, 0); |
| 4015 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4016 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4017 | if (p1d < q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4018 | ret = -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4019 | } else if (p1d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4020 | ret = 1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4021 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4022 | double sec; |
| 4023 | |
| 4024 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
| 4025 | if (sec < 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4026 | ret = -1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4027 | else if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4028 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4029 | |
| 4030 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4031 | xmlSchemaFreeValue(p1); |
| 4032 | xmlSchemaFreeValue(q1); |
| 4033 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | switch (x->type) { |
| 4037 | case XML_SCHEMAS_DATETIME: |
| 4038 | xmask = 0xf; |
| 4039 | break; |
| 4040 | case XML_SCHEMAS_DATE: |
| 4041 | xmask = 0x7; |
| 4042 | break; |
| 4043 | case XML_SCHEMAS_GYEAR: |
| 4044 | xmask = 0x1; |
| 4045 | break; |
| 4046 | case XML_SCHEMAS_GMONTH: |
| 4047 | xmask = 0x2; |
| 4048 | break; |
| 4049 | case XML_SCHEMAS_GDAY: |
| 4050 | xmask = 0x3; |
| 4051 | break; |
| 4052 | case XML_SCHEMAS_GYEARMONTH: |
| 4053 | xmask = 0x3; |
| 4054 | break; |
| 4055 | case XML_SCHEMAS_GMONTHDAY: |
| 4056 | xmask = 0x6; |
| 4057 | break; |
| 4058 | case XML_SCHEMAS_TIME: |
| 4059 | xmask = 0x8; |
| 4060 | break; |
| 4061 | default: |
| 4062 | xmask = 0; |
| 4063 | break; |
| 4064 | } |
| 4065 | |
| 4066 | switch (y->type) { |
| 4067 | case XML_SCHEMAS_DATETIME: |
| 4068 | ymask = 0xf; |
| 4069 | break; |
| 4070 | case XML_SCHEMAS_DATE: |
| 4071 | ymask = 0x7; |
| 4072 | break; |
| 4073 | case XML_SCHEMAS_GYEAR: |
| 4074 | ymask = 0x1; |
| 4075 | break; |
| 4076 | case XML_SCHEMAS_GMONTH: |
| 4077 | ymask = 0x2; |
| 4078 | break; |
| 4079 | case XML_SCHEMAS_GDAY: |
| 4080 | ymask = 0x3; |
| 4081 | break; |
| 4082 | case XML_SCHEMAS_GYEARMONTH: |
| 4083 | ymask = 0x3; |
| 4084 | break; |
| 4085 | case XML_SCHEMAS_GMONTHDAY: |
| 4086 | ymask = 0x6; |
| 4087 | break; |
| 4088 | case XML_SCHEMAS_TIME: |
| 4089 | ymask = 0x8; |
| 4090 | break; |
| 4091 | default: |
| 4092 | ymask = 0; |
| 4093 | break; |
| 4094 | } |
| 4095 | |
| 4096 | xor_mask = xmask ^ ymask; /* mark type differences */ |
| 4097 | and_mask = xmask & ymask; /* mark field specification */ |
| 4098 | |
| 4099 | /* year */ |
| 4100 | if (xor_mask & 1) |
| 4101 | return 2; /* indeterminate */ |
| 4102 | else if (and_mask & 1) { |
| 4103 | if (x->value.date.year < y->value.date.year) |
| 4104 | return -1; |
| 4105 | else if (x->value.date.year > y->value.date.year) |
| 4106 | return 1; |
| 4107 | } |
| 4108 | |
| 4109 | /* month */ |
| 4110 | if (xor_mask & 2) |
| 4111 | return 2; /* indeterminate */ |
| 4112 | else if (and_mask & 2) { |
| 4113 | if (x->value.date.mon < y->value.date.mon) |
| 4114 | return -1; |
| 4115 | else if (x->value.date.mon > y->value.date.mon) |
| 4116 | return 1; |
| 4117 | } |
| 4118 | |
| 4119 | /* day */ |
| 4120 | if (xor_mask & 4) |
| 4121 | return 2; /* indeterminate */ |
| 4122 | else if (and_mask & 4) { |
| 4123 | if (x->value.date.day < y->value.date.day) |
| 4124 | return -1; |
| 4125 | else if (x->value.date.day > y->value.date.day) |
| 4126 | return 1; |
| 4127 | } |
| 4128 | |
| 4129 | /* time */ |
| 4130 | if (xor_mask & 8) |
| 4131 | return 2; /* indeterminate */ |
| 4132 | else if (and_mask & 8) { |
| 4133 | if (x->value.date.hour < y->value.date.hour) |
| 4134 | return -1; |
| 4135 | else if (x->value.date.hour > y->value.date.hour) |
| 4136 | return 1; |
| 4137 | else if (x->value.date.min < y->value.date.min) |
| 4138 | return -1; |
| 4139 | else if (x->value.date.min > y->value.date.min) |
| 4140 | return 1; |
| 4141 | else if (x->value.date.sec < y->value.date.sec) |
| 4142 | return -1; |
| 4143 | else if (x->value.date.sec > y->value.date.sec) |
| 4144 | return 1; |
| 4145 | } |
| 4146 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4147 | return 0; |
| 4148 | } |
| 4149 | |
| 4150 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4151 | * xmlSchemaComparePreserveReplaceStrings: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4152 | * @x: a first string value |
| 4153 | * @y: a second string value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4154 | * @invert: inverts the result if x < y or x > y. |
| 4155 | * |
| 4156 | * Compare 2 string for their normalized values. |
| 4157 | * @x is a string with whitespace of "preserve", @y is |
| 4158 | * a string with a whitespace of "replace". I.e. @x could |
| 4159 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4160 | * |
| 4161 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4162 | * case of error |
| 4163 | */ |
| 4164 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4165 | xmlSchemaComparePreserveReplaceStrings(const xmlChar *x, |
| 4166 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4167 | int invert) |
| 4168 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4169 | int tmp; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4170 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4171 | while ((*x != 0) && (*y != 0)) { |
| 4172 | if (IS_WSP_REPLACE_CH(*y)) { |
| 4173 | if (! IS_WSP_SPACE_CH(*x)) { |
| 4174 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4175 | if (invert) |
| 4176 | return(1); |
| 4177 | else |
| 4178 | return(-1); |
| 4179 | } else { |
| 4180 | if (invert) |
| 4181 | return(-1); |
| 4182 | else |
| 4183 | return(1); |
| 4184 | } |
| 4185 | } |
| 4186 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4187 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4188 | if (tmp < 0) { |
| 4189 | if (invert) |
| 4190 | return(1); |
| 4191 | else |
| 4192 | return(-1); |
| 4193 | } |
| 4194 | if (tmp > 0) { |
| 4195 | if (invert) |
| 4196 | return(-1); |
| 4197 | else |
| 4198 | return(1); |
| 4199 | } |
| 4200 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4201 | x++; |
| 4202 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4203 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4204 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4205 | if (invert) |
| 4206 | return(-1); |
| 4207 | else |
| 4208 | return(1); |
| 4209 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4210 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4211 | if (invert) |
| 4212 | return(1); |
| 4213 | else |
| 4214 | return(-1); |
| 4215 | } |
| 4216 | return(0); |
| 4217 | } |
| 4218 | |
| 4219 | /** |
| 4220 | * xmlSchemaComparePreserveCollapseStrings: |
| 4221 | * @x: a first string value |
| 4222 | * @y: a second string value |
| 4223 | * |
| 4224 | * Compare 2 string for their normalized values. |
| 4225 | * @x is a string with whitespace of "preserve", @y is |
| 4226 | * a string with a whitespace of "collapse". I.e. @x could |
| 4227 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4228 | * |
| 4229 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4230 | * case of error |
| 4231 | */ |
| 4232 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4233 | xmlSchemaComparePreserveCollapseStrings(const xmlChar *x, |
| 4234 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4235 | int invert) |
| 4236 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4237 | int tmp; |
| 4238 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4239 | /* |
| 4240 | * Skip leading blank chars of the collapsed string. |
| 4241 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4242 | while IS_WSP_BLANK_CH(*y) |
| 4243 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4244 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4245 | while ((*x != 0) && (*y != 0)) { |
| 4246 | if IS_WSP_BLANK_CH(*y) { |
| 4247 | if (! IS_WSP_SPACE_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4248 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4249 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4250 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4251 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4252 | if (invert) |
| 4253 | return(1); |
| 4254 | else |
| 4255 | return(-1); |
| 4256 | } else { |
| 4257 | if (invert) |
| 4258 | return(-1); |
| 4259 | else |
| 4260 | return(1); |
| 4261 | } |
| 4262 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4263 | x++; |
| 4264 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4265 | /* |
| 4266 | * Skip contiguous blank chars of the collapsed string. |
| 4267 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4268 | while IS_WSP_BLANK_CH(*y) |
| 4269 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4270 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4271 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4272 | if (tmp < 0) { |
| 4273 | if (invert) |
| 4274 | return(1); |
| 4275 | else |
| 4276 | return(-1); |
| 4277 | } |
| 4278 | if (tmp > 0) { |
| 4279 | if (invert) |
| 4280 | return(-1); |
| 4281 | else |
| 4282 | return(1); |
| 4283 | } |
| 4284 | } |
| 4285 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4286 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4287 | if (invert) |
| 4288 | return(-1); |
| 4289 | else |
| 4290 | return(1); |
| 4291 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4292 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * Skip trailing blank chars of the collapsed string. |
| 4295 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4296 | while IS_WSP_BLANK_CH(*y) |
| 4297 | y++; |
| 4298 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4299 | if (invert) |
| 4300 | return(1); |
| 4301 | else |
| 4302 | return(-1); |
| 4303 | } |
| 4304 | } |
| 4305 | return(0); |
| 4306 | } |
| 4307 | |
| 4308 | /** |
| 4309 | * xmlSchemaComparePreserveCollapseStrings: |
| 4310 | * @x: a first string value |
| 4311 | * @y: a second string value |
| 4312 | * |
| 4313 | * Compare 2 string for their normalized values. |
| 4314 | * @x is a string with whitespace of "preserve", @y is |
| 4315 | * a string with a whitespace of "collapse". I.e. @x could |
| 4316 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4317 | * |
| 4318 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4319 | * case of error |
| 4320 | */ |
| 4321 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4322 | xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x, |
| 4323 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4324 | int invert) |
| 4325 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4326 | int tmp; |
| 4327 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4328 | /* |
| 4329 | * Skip leading blank chars of the collapsed string. |
| 4330 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4331 | while IS_WSP_BLANK_CH(*y) |
| 4332 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4333 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4334 | while ((*x != 0) && (*y != 0)) { |
| 4335 | if IS_WSP_BLANK_CH(*y) { |
| 4336 | if (! IS_WSP_BLANK_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4337 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4338 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4339 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4340 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4341 | if (invert) |
| 4342 | return(1); |
| 4343 | else |
| 4344 | return(-1); |
| 4345 | } else { |
| 4346 | if (invert) |
| 4347 | return(-1); |
| 4348 | else |
| 4349 | return(1); |
| 4350 | } |
| 4351 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4352 | x++; |
| 4353 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4354 | /* |
| 4355 | * Skip contiguous blank chars of the collapsed string. |
| 4356 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4357 | while IS_WSP_BLANK_CH(*y) |
| 4358 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4359 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4360 | if IS_WSP_BLANK_CH(*x) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4361 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4362 | * The xv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4363 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4364 | if ((0x20 - *y) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4365 | if (invert) |
| 4366 | return(1); |
| 4367 | else |
| 4368 | return(-1); |
| 4369 | } else { |
| 4370 | if (invert) |
| 4371 | return(-1); |
| 4372 | else |
| 4373 | return(1); |
| 4374 | } |
| 4375 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4376 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4377 | if (tmp < 0) |
| 4378 | return(-1); |
| 4379 | if (tmp > 0) |
| 4380 | return(1); |
| 4381 | } |
| 4382 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4383 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4384 | if (invert) |
| 4385 | return(-1); |
| 4386 | else |
| 4387 | return(1); |
| 4388 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4389 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4390 | /* |
| 4391 | * Skip trailing blank chars of the collapsed string. |
| 4392 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4393 | while IS_WSP_BLANK_CH(*y) |
| 4394 | y++; |
| 4395 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4396 | if (invert) |
| 4397 | return(1); |
| 4398 | else |
| 4399 | return(-1); |
| 4400 | } |
| 4401 | } |
| 4402 | return(0); |
| 4403 | } |
| 4404 | |
| 4405 | |
| 4406 | /** |
| 4407 | * xmlSchemaCompareReplacedStrings: |
| 4408 | * @x: a first string value |
| 4409 | * @y: a second string value |
| 4410 | * |
| 4411 | * Compare 2 string for their normalized values. |
| 4412 | * |
| 4413 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4414 | * case of error |
| 4415 | */ |
| 4416 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4417 | xmlSchemaCompareReplacedStrings(const xmlChar *x, |
| 4418 | const xmlChar *y) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4419 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4420 | int tmp; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4421 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4422 | while ((*x != 0) && (*y != 0)) { |
| 4423 | if IS_WSP_BLANK_CH(*y) { |
| 4424 | if (! IS_WSP_BLANK_CH(*x)) { |
| 4425 | if ((*x - 0x20) < 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4426 | return(-1); |
| 4427 | else |
| 4428 | return(1); |
| 4429 | } |
| 4430 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4431 | if IS_WSP_BLANK_CH(*x) { |
| 4432 | if ((0x20 - *y) < 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4433 | return(-1); |
| 4434 | else |
| 4435 | return(1); |
| 4436 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4437 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4438 | if (tmp < 0) |
| 4439 | return(-1); |
| 4440 | if (tmp > 0) |
| 4441 | return(1); |
| 4442 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4443 | x++; |
| 4444 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4445 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4446 | if (*x != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4447 | return(1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4448 | if (*y != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4449 | return(-1); |
| 4450 | return(0); |
| 4451 | } |
| 4452 | |
| 4453 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4454 | * xmlSchemaCompareNormStrings: |
| 4455 | * @x: a first string value |
| 4456 | * @y: a second string value |
| 4457 | * |
| 4458 | * Compare 2 string for their normalized values. |
| 4459 | * |
| 4460 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4461 | * case of error |
| 4462 | */ |
| 4463 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4464 | xmlSchemaCompareNormStrings(const xmlChar *x, |
| 4465 | const xmlChar *y) { |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4466 | int tmp; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4467 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4468 | while (IS_BLANK_CH(*x)) x++; |
| 4469 | while (IS_BLANK_CH(*y)) y++; |
| 4470 | while ((*x != 0) && (*y != 0)) { |
| 4471 | if (IS_BLANK_CH(*x)) { |
| 4472 | if (!IS_BLANK_CH(*y)) { |
| 4473 | tmp = *x - *y; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4474 | return(tmp); |
| 4475 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4476 | while (IS_BLANK_CH(*x)) x++; |
| 4477 | while (IS_BLANK_CH(*y)) y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4478 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4479 | tmp = *x++ - *y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4480 | if (tmp < 0) |
| 4481 | return(-1); |
| 4482 | if (tmp > 0) |
| 4483 | return(1); |
| 4484 | } |
| 4485 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4486 | if (*x != 0) { |
| 4487 | while (IS_BLANK_CH(*x)) x++; |
| 4488 | if (*x != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4489 | return(1); |
| 4490 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4491 | if (*y != 0) { |
| 4492 | while (IS_BLANK_CH(*y)) y++; |
| 4493 | if (*y != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4494 | return(-1); |
| 4495 | } |
| 4496 | return(0); |
| 4497 | } |
| 4498 | |
| 4499 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4500 | * xmlSchemaCompareFloats: |
| 4501 | * @x: a first float or double value |
| 4502 | * @y: a second float or double value |
| 4503 | * |
| 4504 | * Compare 2 values |
| 4505 | * |
| 4506 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4507 | * case of error |
| 4508 | */ |
| 4509 | static int |
| 4510 | xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4511 | double d1, d2; |
| 4512 | |
| 4513 | if ((x == NULL) || (y == NULL)) |
| 4514 | return(-2); |
| 4515 | |
| 4516 | /* |
| 4517 | * Cast everything to doubles. |
| 4518 | */ |
| 4519 | if (x->type == XML_SCHEMAS_DOUBLE) |
| 4520 | d1 = x->value.d; |
| 4521 | else if (x->type == XML_SCHEMAS_FLOAT) |
| 4522 | d1 = x->value.f; |
| 4523 | else |
| 4524 | return(-2); |
| 4525 | |
| 4526 | if (y->type == XML_SCHEMAS_DOUBLE) |
| 4527 | d2 = y->value.d; |
| 4528 | else if (y->type == XML_SCHEMAS_FLOAT) |
| 4529 | d2 = y->value.f; |
| 4530 | else |
| 4531 | return(-2); |
| 4532 | |
| 4533 | /* |
| 4534 | * Check for special cases. |
| 4535 | */ |
| 4536 | if (xmlXPathIsNaN(d1)) { |
| 4537 | if (xmlXPathIsNaN(d2)) |
| 4538 | return(0); |
| 4539 | return(1); |
| 4540 | } |
| 4541 | if (xmlXPathIsNaN(d2)) |
| 4542 | return(-1); |
| 4543 | if (d1 == xmlXPathPINF) { |
| 4544 | if (d2 == xmlXPathPINF) |
| 4545 | return(0); |
| 4546 | return(1); |
| 4547 | } |
| 4548 | if (d2 == xmlXPathPINF) |
| 4549 | return(-1); |
| 4550 | if (d1 == xmlXPathNINF) { |
| 4551 | if (d2 == xmlXPathNINF) |
| 4552 | return(0); |
| 4553 | return(-1); |
| 4554 | } |
| 4555 | if (d2 == xmlXPathNINF) |
| 4556 | return(1); |
| 4557 | |
| 4558 | /* |
| 4559 | * basic tests, the last one we should have equality, but |
| 4560 | * portability is more important than speed and handling |
| 4561 | * NaN or Inf in a portable way is always a challenge, so ... |
| 4562 | */ |
| 4563 | if (d1 < d2) |
| 4564 | return(-1); |
| 4565 | if (d1 > d2) |
| 4566 | return(1); |
| 4567 | if (d1 == d2) |
| 4568 | return(0); |
| 4569 | return(2); |
| 4570 | } |
| 4571 | |
| 4572 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4573 | * xmlSchemaCompareValues: |
| 4574 | * @x: a first value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4575 | * @xvalue: the first value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4576 | * @xwtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4577 | * @y: a second value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4578 | * @xvalue: the second value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4579 | * @ywtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4580 | * |
| 4581 | * Compare 2 values |
| 4582 | * |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 4583 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not |
| 4584 | * comparable and -2 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4585 | */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4586 | static int |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4587 | xmlSchemaCompareValuesInternal(xmlSchemaValType xtype, |
| 4588 | xmlSchemaValPtr x, |
| 4589 | const xmlChar *xvalue, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4590 | xmlSchemaWhitespaceValueType xws, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4591 | xmlSchemaValType ytype, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4592 | xmlSchemaValPtr y, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4593 | const xmlChar *yvalue, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4594 | xmlSchemaWhitespaceValueType yws) |
| 4595 | { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4596 | switch (xtype) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4597 | case XML_SCHEMAS_UNKNOWN: |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 4598 | case XML_SCHEMAS_ANYTYPE: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4599 | return(-2); |
| 4600 | case XML_SCHEMAS_INTEGER: |
| 4601 | case XML_SCHEMAS_NPINTEGER: |
| 4602 | case XML_SCHEMAS_NINTEGER: |
| 4603 | case XML_SCHEMAS_NNINTEGER: |
| 4604 | case XML_SCHEMAS_PINTEGER: |
| 4605 | case XML_SCHEMAS_INT: |
| 4606 | case XML_SCHEMAS_UINT: |
| 4607 | case XML_SCHEMAS_LONG: |
| 4608 | case XML_SCHEMAS_ULONG: |
| 4609 | case XML_SCHEMAS_SHORT: |
| 4610 | case XML_SCHEMAS_USHORT: |
| 4611 | case XML_SCHEMAS_BYTE: |
| 4612 | case XML_SCHEMAS_UBYTE: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4613 | case XML_SCHEMAS_DECIMAL: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4614 | if ((x == NULL) || (y == NULL)) |
| 4615 | return(-2); |
| 4616 | if (ytype == xtype) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4617 | return(xmlSchemaCompareDecimals(x, y)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4618 | if ((ytype == XML_SCHEMAS_DECIMAL) || |
| 4619 | (ytype == XML_SCHEMAS_INTEGER) || |
| 4620 | (ytype == XML_SCHEMAS_NPINTEGER) || |
| 4621 | (ytype == XML_SCHEMAS_NINTEGER) || |
| 4622 | (ytype == XML_SCHEMAS_NNINTEGER) || |
| 4623 | (ytype == XML_SCHEMAS_PINTEGER) || |
| 4624 | (ytype == XML_SCHEMAS_INT) || |
| 4625 | (ytype == XML_SCHEMAS_UINT) || |
| 4626 | (ytype == XML_SCHEMAS_LONG) || |
| 4627 | (ytype == XML_SCHEMAS_ULONG) || |
| 4628 | (ytype == XML_SCHEMAS_SHORT) || |
| 4629 | (ytype == XML_SCHEMAS_USHORT) || |
| 4630 | (ytype == XML_SCHEMAS_BYTE) || |
| 4631 | (ytype == XML_SCHEMAS_UBYTE)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4632 | return(xmlSchemaCompareDecimals(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4633 | return(-2); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4634 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4635 | if ((x == NULL) || (y == NULL)) |
| 4636 | return(-2); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4637 | if (ytype == XML_SCHEMAS_DURATION) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4638 | return(xmlSchemaCompareDurations(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4639 | return(-2); |
| 4640 | case XML_SCHEMAS_TIME: |
| 4641 | case XML_SCHEMAS_GDAY: |
| 4642 | case XML_SCHEMAS_GMONTH: |
| 4643 | case XML_SCHEMAS_GMONTHDAY: |
| 4644 | case XML_SCHEMAS_GYEAR: |
| 4645 | case XML_SCHEMAS_GYEARMONTH: |
| 4646 | case XML_SCHEMAS_DATE: |
| 4647 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4648 | if ((x == NULL) || (y == NULL)) |
| 4649 | return(-2); |
| 4650 | if ((ytype == XML_SCHEMAS_DATETIME) || |
| 4651 | (ytype == XML_SCHEMAS_TIME) || |
| 4652 | (ytype == XML_SCHEMAS_GDAY) || |
| 4653 | (ytype == XML_SCHEMAS_GMONTH) || |
| 4654 | (ytype == XML_SCHEMAS_GMONTHDAY) || |
| 4655 | (ytype == XML_SCHEMAS_GYEAR) || |
| 4656 | (ytype == XML_SCHEMAS_DATE) || |
| 4657 | (ytype == XML_SCHEMAS_GYEARMONTH)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4658 | return (xmlSchemaCompareDates(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4659 | return (-2); |
Kasimier T. Buchcik | 2ee855d | 2005-03-07 11:14:14 +0000 | [diff] [blame] | 4660 | /* |
| 4661 | * Note that we will support comparison of string types against |
| 4662 | * anySimpleType as well. |
| 4663 | */ |
| 4664 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4665 | case XML_SCHEMAS_STRING: |
| 4666 | case XML_SCHEMAS_NORMSTRING: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4667 | case XML_SCHEMAS_TOKEN: |
| 4668 | case XML_SCHEMAS_LANGUAGE: |
| 4669 | case XML_SCHEMAS_NMTOKEN: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4670 | case XML_SCHEMAS_NAME: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4671 | case XML_SCHEMAS_NCNAME: |
| 4672 | case XML_SCHEMAS_ID: |
| 4673 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4674 | case XML_SCHEMAS_ENTITY: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4675 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4676 | { |
| 4677 | const xmlChar *xv, *yv; |
| 4678 | |
| 4679 | if (x == NULL) |
| 4680 | xv = xvalue; |
| 4681 | else |
| 4682 | xv = x->value.str; |
| 4683 | if (y == NULL) |
| 4684 | yv = yvalue; |
| 4685 | else |
| 4686 | yv = y->value.str; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4687 | /* |
| 4688 | * TODO: Compare those against QName. |
| 4689 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4690 | if (ytype == XML_SCHEMAS_QNAME) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4691 | TODO |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4692 | if (y == NULL) |
| 4693 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4694 | return (-2); |
| 4695 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4696 | if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) || |
| 4697 | (ytype == XML_SCHEMAS_STRING) || |
| 4698 | (ytype == XML_SCHEMAS_NORMSTRING) || |
| 4699 | (ytype == XML_SCHEMAS_TOKEN) || |
| 4700 | (ytype == XML_SCHEMAS_LANGUAGE) || |
| 4701 | (ytype == XML_SCHEMAS_NMTOKEN) || |
| 4702 | (ytype == XML_SCHEMAS_NAME) || |
| 4703 | (ytype == XML_SCHEMAS_NCNAME) || |
| 4704 | (ytype == XML_SCHEMAS_ID) || |
| 4705 | (ytype == XML_SCHEMAS_IDREF) || |
| 4706 | (ytype == XML_SCHEMAS_ENTITY) || |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4707 | (ytype == XML_SCHEMAS_ANYURI)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4708 | |
| 4709 | if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4710 | |
| 4711 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4712 | /* TODO: What about x < y or x > y. */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4713 | if (xmlStrEqual(xv, yv)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4714 | return (0); |
| 4715 | else |
| 4716 | return (2); |
| 4717 | } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4718 | return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4719 | else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4720 | return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4721 | |
| 4722 | } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 4723 | |
| 4724 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4725 | return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4726 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4727 | return (xmlSchemaCompareReplacedStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4728 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4729 | return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4730 | |
| 4731 | } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 4732 | |
| 4733 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4734 | return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4735 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4736 | return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4737 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4738 | return (xmlSchemaCompareNormStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4739 | } else |
| 4740 | return (-2); |
| 4741 | |
| 4742 | } |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4743 | return (-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4744 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4745 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4746 | case XML_SCHEMAS_NOTATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4747 | if ((x == NULL) || (y == NULL)) |
| 4748 | return(-2); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4749 | if ((ytype == XML_SCHEMAS_QNAME) || |
| 4750 | (ytype == XML_SCHEMAS_NOTATION)) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4751 | if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) && |
| 4752 | (xmlStrEqual(x->value.qname.uri, y->value.qname.uri))) |
| 4753 | return(0); |
| 4754 | return(2); |
| 4755 | } |
| 4756 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4757 | case XML_SCHEMAS_FLOAT: |
| 4758 | case XML_SCHEMAS_DOUBLE: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4759 | if ((x == NULL) || (y == NULL)) |
| 4760 | return(-2); |
| 4761 | if ((ytype == XML_SCHEMAS_FLOAT) || |
| 4762 | (ytype == XML_SCHEMAS_DOUBLE)) |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4763 | return (xmlSchemaCompareFloats(x, y)); |
| 4764 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4765 | case XML_SCHEMAS_BOOLEAN: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4766 | if ((x == NULL) || (y == NULL)) |
| 4767 | return(-2); |
| 4768 | if (ytype == XML_SCHEMAS_BOOLEAN) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4769 | if (x->value.b == y->value.b) |
| 4770 | return(0); |
| 4771 | if (x->value.b == 0) |
| 4772 | return(-1); |
| 4773 | return(1); |
| 4774 | } |
| 4775 | return (-2); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4776 | case XML_SCHEMAS_HEXBINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4777 | if ((x == NULL) || (y == NULL)) |
| 4778 | return(-2); |
| 4779 | if (ytype == XML_SCHEMAS_HEXBINARY) { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4780 | if (x->value.hex.total == y->value.hex.total) { |
| 4781 | int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str); |
| 4782 | if (ret > 0) |
| 4783 | return(1); |
| 4784 | else if (ret == 0) |
| 4785 | return(0); |
| 4786 | } |
| 4787 | else if (x->value.hex.total > y->value.hex.total) |
| 4788 | return(1); |
| 4789 | |
| 4790 | return(-1); |
| 4791 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4792 | return (-2); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4793 | case XML_SCHEMAS_BASE64BINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4794 | if ((x == NULL) || (y == NULL)) |
| 4795 | return(-2); |
| 4796 | if (ytype == XML_SCHEMAS_BASE64BINARY) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4797 | if (x->value.base64.total == y->value.base64.total) { |
| 4798 | int ret = xmlStrcmp(x->value.base64.str, |
| 4799 | y->value.base64.str); |
| 4800 | if (ret > 0) |
| 4801 | return(1); |
| 4802 | else if (ret == 0) |
| 4803 | return(0); |
Daniel Veillard | d369b13 | 2005-07-14 15:54:44 +0000 | [diff] [blame] | 4804 | else |
| 4805 | return(-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4806 | } |
| 4807 | else if (x->value.base64.total > y->value.base64.total) |
| 4808 | return(1); |
| 4809 | else |
| 4810 | return(-1); |
| 4811 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4812 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4813 | case XML_SCHEMAS_IDREFS: |
| 4814 | case XML_SCHEMAS_ENTITIES: |
| 4815 | case XML_SCHEMAS_NMTOKENS: |
| 4816 | TODO |
| 4817 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4818 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4819 | return -2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4823 | * xmlSchemaCompareValues: |
| 4824 | * @x: a first value |
| 4825 | * @y: a second value |
| 4826 | * |
| 4827 | * Compare 2 values |
| 4828 | * |
| 4829 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4830 | * case of error |
| 4831 | */ |
| 4832 | int |
| 4833 | xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4834 | xmlSchemaWhitespaceValueType xws, yws; |
| 4835 | |
Daniel Veillard | 5e09414 | 2005-02-18 19:36:12 +0000 | [diff] [blame] | 4836 | if ((x == NULL) || (y == NULL)) |
| 4837 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4838 | if (x->type == XML_SCHEMAS_STRING) |
| 4839 | xws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4840 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4841 | xws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4842 | else |
| 4843 | xws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4844 | |
| 4845 | if (y->type == XML_SCHEMAS_STRING) |
| 4846 | yws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4847 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4848 | yws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4849 | else |
| 4850 | yws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4851 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4852 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4853 | y, NULL, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4854 | } |
| 4855 | |
| 4856 | /** |
| 4857 | * xmlSchemaCompareValuesWhtsp: |
| 4858 | * @x: a first value |
| 4859 | * @xws: the whitespace value of x |
| 4860 | * @y: a second value |
| 4861 | * @yws: the whitespace value of y |
| 4862 | * |
| 4863 | * Compare 2 values |
| 4864 | * |
| 4865 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4866 | * case of error |
| 4867 | */ |
| 4868 | int |
| 4869 | xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4870 | xmlSchemaWhitespaceValueType xws, |
| 4871 | xmlSchemaValPtr y, |
| 4872 | xmlSchemaWhitespaceValueType yws) |
| 4873 | { |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 4874 | if ((x == NULL) || (y == NULL)) |
| 4875 | return(-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4876 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4877 | y, NULL, yws)); |
| 4878 | } |
| 4879 | |
| 4880 | /** |
| 4881 | * xmlSchemaCompareValuesWhtspExt: |
| 4882 | * @x: a first value |
| 4883 | * @xws: the whitespace value of x |
| 4884 | * @y: a second value |
| 4885 | * @yws: the whitespace value of y |
| 4886 | * |
| 4887 | * Compare 2 values |
| 4888 | * |
| 4889 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4890 | * case of error |
| 4891 | */ |
| 4892 | static int |
| 4893 | xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype, |
| 4894 | xmlSchemaValPtr x, |
| 4895 | const xmlChar *xvalue, |
| 4896 | xmlSchemaWhitespaceValueType xws, |
| 4897 | xmlSchemaValType ytype, |
| 4898 | xmlSchemaValPtr y, |
| 4899 | const xmlChar *yvalue, |
| 4900 | xmlSchemaWhitespaceValueType yws) |
| 4901 | { |
| 4902 | return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y, |
| 4903 | yvalue, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4907 | * xmlSchemaNormLen: |
| 4908 | * @value: a string |
| 4909 | * |
| 4910 | * Computes the UTF8 length of the normalized value of the string |
| 4911 | * |
| 4912 | * Returns the length or -1 in case of error. |
| 4913 | */ |
| 4914 | static int |
| 4915 | xmlSchemaNormLen(const xmlChar *value) { |
| 4916 | const xmlChar *utf; |
| 4917 | int ret = 0; |
| 4918 | |
| 4919 | if (value == NULL) |
| 4920 | return(-1); |
| 4921 | utf = value; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4922 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4923 | while (*utf != 0) { |
| 4924 | if (utf[0] & 0x80) { |
| 4925 | if ((utf[1] & 0xc0) != 0x80) |
| 4926 | return(-1); |
| 4927 | if ((utf[0] & 0xe0) == 0xe0) { |
| 4928 | if ((utf[2] & 0xc0) != 0x80) |
| 4929 | return(-1); |
| 4930 | if ((utf[0] & 0xf0) == 0xf0) { |
| 4931 | if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) |
| 4932 | return(-1); |
| 4933 | utf += 4; |
| 4934 | } else { |
| 4935 | utf += 3; |
| 4936 | } |
| 4937 | } else { |
| 4938 | utf += 2; |
| 4939 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4940 | } else if (IS_BLANK_CH(*utf)) { |
| 4941 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4942 | if (*utf == 0) |
| 4943 | break; |
| 4944 | } else { |
| 4945 | utf++; |
| 4946 | } |
| 4947 | ret++; |
| 4948 | } |
| 4949 | return(ret); |
| 4950 | } |
| 4951 | |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 4952 | /** |
| 4953 | * xmlSchemaGetFacetValueAsULong: |
| 4954 | * @facet: an schemas type facet |
| 4955 | * |
| 4956 | * Extract the value of a facet |
| 4957 | * |
| 4958 | * Returns the value as a long |
| 4959 | */ |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4960 | unsigned long |
| 4961 | xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet) |
| 4962 | { |
| 4963 | /* |
| 4964 | * TODO: Check if this is a decimal. |
| 4965 | */ |
William M. Brack | 094dd86 | 2004-11-14 14:28:34 +0000 | [diff] [blame] | 4966 | if (facet == NULL) |
| 4967 | return 0; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4968 | return ((unsigned long) facet->val->value.decimal.lo); |
| 4969 | } |
| 4970 | |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4971 | /** |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4972 | * xmlSchemaValidateListSimpleTypeFacet: |
| 4973 | * @facet: the facet to check |
| 4974 | * @value: the lexical repr of the value to validate |
| 4975 | * @actualLen: the number of list items |
| 4976 | * @expectedLen: the resulting expected number of list items |
| 4977 | * |
| 4978 | * Checks the value of a list simple type against a facet. |
| 4979 | * |
| 4980 | * Returns 0 if the value is valid, a positive error code |
| 4981 | * number otherwise and -1 in case of an internal error. |
| 4982 | */ |
| 4983 | int |
| 4984 | xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet, |
| 4985 | const xmlChar *value, |
| 4986 | unsigned long actualLen, |
| 4987 | unsigned long *expectedLen) |
| 4988 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4989 | if (facet == NULL) |
| 4990 | return(-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4991 | /* |
| 4992 | * TODO: Check if this will work with large numbers. |
| 4993 | * (compare value.decimal.mi and value.decimal.hi as well?). |
| 4994 | */ |
| 4995 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 4996 | if (actualLen != facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4997 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4998 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4999 | return (XML_SCHEMAV_CVC_LENGTH_VALID); |
| 5000 | } |
| 5001 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5002 | if (actualLen < facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5003 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5004 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5005 | return (XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5006 | } |
| 5007 | } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) { |
| 5008 | if (actualLen > facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5009 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5010 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5011 | return (XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5012 | } |
| 5013 | } else |
| 5014 | /* |
| 5015 | * NOTE: That we can pass NULL as xmlSchemaValPtr to |
| 5016 | * xmlSchemaValidateFacet, since the remaining facet types |
| 5017 | * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION. |
| 5018 | */ |
| 5019 | return(xmlSchemaValidateFacet(NULL, facet, value, NULL)); |
| 5020 | return (0); |
| 5021 | } |
| 5022 | |
| 5023 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 5024 | * xmlSchemaValidateLengthFacet: |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5025 | * @type: the built-in type |
| 5026 | * @facet: the facet to check |
| 5027 | * @value: the lexical repr. of the value to be validated |
| 5028 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5029 | * @ws: the whitespace type of the value |
| 5030 | * @length: the actual length of the value |
| 5031 | * |
| 5032 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5033 | * facet; sets @length to the computed length of @value. |
| 5034 | * |
| 5035 | * Returns 0 if the value is valid, a positive error code |
| 5036 | * otherwise and -1 in case of an internal or API error. |
| 5037 | */ |
| 5038 | static int |
| 5039 | xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet, |
| 5040 | xmlSchemaTypeType valType, |
| 5041 | const xmlChar *value, |
| 5042 | xmlSchemaValPtr val, |
| 5043 | unsigned long *length, |
| 5044 | xmlSchemaWhitespaceValueType ws) |
| 5045 | { |
| 5046 | unsigned int len = 0; |
| 5047 | |
| 5048 | if ((length == NULL) || (facet == NULL)) |
| 5049 | return (-1); |
| 5050 | *length = 0; |
| 5051 | if ((facet->type != XML_SCHEMA_FACET_LENGTH) && |
| 5052 | (facet->type != XML_SCHEMA_FACET_MAXLENGTH) && |
| 5053 | (facet->type != XML_SCHEMA_FACET_MINLENGTH)) |
| 5054 | return (-1); |
| 5055 | |
| 5056 | /* |
| 5057 | * TODO: length, maxLength and minLength must be of type |
| 5058 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5059 | */ |
| 5060 | if ((facet->val == NULL) || |
| 5061 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5062 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5063 | (facet->val->value.decimal.frac != 0)) { |
| 5064 | return(-1); |
| 5065 | } |
| 5066 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
| 5067 | len = val->value.hex.total; |
| 5068 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5069 | len = val->value.base64.total; |
| 5070 | else { |
| 5071 | switch (valType) { |
| 5072 | case XML_SCHEMAS_STRING: |
| 5073 | case XML_SCHEMAS_NORMSTRING: |
| 5074 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5075 | /* |
| 5076 | * This is to ensure API compatibility with the old |
| 5077 | * xmlSchemaValidateLengthFacet(). Anyway, this was and |
| 5078 | * is not the correct handling. |
| 5079 | * TODO: Get rid of this case somehow. |
| 5080 | */ |
| 5081 | if (valType == XML_SCHEMAS_STRING) |
| 5082 | len = xmlUTF8Strlen(value); |
| 5083 | else |
| 5084 | len = xmlSchemaNormLen(value); |
| 5085 | } else if (value != NULL) { |
| 5086 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5087 | len = xmlSchemaNormLen(value); |
| 5088 | else |
| 5089 | /* |
| 5090 | * Should be OK for "preserve" as well. |
| 5091 | */ |
| 5092 | len = xmlUTF8Strlen(value); |
| 5093 | } |
| 5094 | break; |
| 5095 | case XML_SCHEMAS_IDREF: |
| 5096 | case XML_SCHEMAS_TOKEN: |
| 5097 | case XML_SCHEMAS_LANGUAGE: |
| 5098 | case XML_SCHEMAS_NMTOKEN: |
| 5099 | case XML_SCHEMAS_NAME: |
| 5100 | case XML_SCHEMAS_NCNAME: |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5101 | case XML_SCHEMAS_ID: |
| 5102 | /* |
| 5103 | * FIXME: What exactly to do with anyURI? |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5104 | */ |
| 5105 | case XML_SCHEMAS_ANYURI: |
| 5106 | if (value != NULL) |
| 5107 | len = xmlSchemaNormLen(value); |
| 5108 | break; |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5109 | case XML_SCHEMAS_QNAME: |
| 5110 | case XML_SCHEMAS_NOTATION: |
| 5111 | /* |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5112 | * For QName and NOTATION, those facets are |
| 5113 | * deprecated and should be ignored. |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5114 | */ |
| 5115 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5116 | default: |
| 5117 | TODO |
| 5118 | } |
| 5119 | } |
| 5120 | *length = (unsigned long) len; |
| 5121 | /* |
| 5122 | * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi". |
| 5123 | */ |
| 5124 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 5125 | if (len != facet->val->value.decimal.lo) |
| 5126 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
| 5127 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5128 | if (len < facet->val->value.decimal.lo) |
| 5129 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5130 | } else { |
| 5131 | if (len > facet->val->value.decimal.lo) |
| 5132 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5133 | } |
| 5134 | |
| 5135 | return (0); |
| 5136 | } |
| 5137 | |
| 5138 | /** |
| 5139 | * xmlSchemaValidateLengthFacet: |
| 5140 | * @type: the built-in type |
| 5141 | * @facet: the facet to check |
| 5142 | * @value: the lexical repr. of the value to be validated |
| 5143 | * @val: the precomputed value |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5144 | * @length: the actual length of the value |
| 5145 | * |
| 5146 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5147 | * facet; sets @length to the computed length of @value. |
| 5148 | * |
| 5149 | * Returns 0 if the value is valid, a positive error code |
| 5150 | * otherwise and -1 in case of an internal or API error. |
| 5151 | */ |
| 5152 | int |
| 5153 | xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type, |
| 5154 | xmlSchemaFacetPtr facet, |
| 5155 | const xmlChar *value, |
| 5156 | xmlSchemaValPtr val, |
| 5157 | unsigned long *length) |
| 5158 | { |
Daniel Veillard | cc5e233 | 2005-03-16 21:55:35 +0000 | [diff] [blame] | 5159 | if (type == NULL) |
| 5160 | return(-1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5161 | return (xmlSchemaValidateLengthFacetInternal(facet, |
| 5162 | type->builtInType, value, val, length, |
| 5163 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
| 5164 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5165 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5166 | /** |
| 5167 | * xmlSchemaValidateLengthFacetWhtsp: |
| 5168 | * @facet: the facet to check |
| 5169 | * @valType: the built-in type |
| 5170 | * @value: the lexical repr. of the value to be validated |
| 5171 | * @val: the precomputed value |
| 5172 | * @ws: the whitespace type of the value |
| 5173 | * @length: the actual length of the value |
| 5174 | * |
| 5175 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5176 | * facet; sets @length to the computed length of @value. |
| 5177 | * |
| 5178 | * Returns 0 if the value is valid, a positive error code |
| 5179 | * otherwise and -1 in case of an internal or API error. |
| 5180 | */ |
| 5181 | int |
| 5182 | xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5183 | xmlSchemaValType valType, |
| 5184 | const xmlChar *value, |
| 5185 | xmlSchemaValPtr val, |
| 5186 | unsigned long *length, |
| 5187 | xmlSchemaWhitespaceValueType ws) |
| 5188 | { |
| 5189 | return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val, |
| 5190 | length, ws)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5191 | } |
| 5192 | |
| 5193 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5194 | * xmlSchemaValidateFacetInternal: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5195 | * @facet: the facet to check |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5196 | * @fws: the whitespace type of the facet's value |
| 5197 | * @valType: the built-in type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5198 | * @value: the lexical repr of the value to validate |
| 5199 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5200 | * @ws: the whitespace type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5201 | * |
| 5202 | * Check a value against a facet condition |
| 5203 | * |
| 5204 | * Returns 0 if the element is schemas valid, a positive error code |
| 5205 | * number otherwise and -1 in case of internal or API error. |
| 5206 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5207 | static int |
| 5208 | xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet, |
| 5209 | xmlSchemaWhitespaceValueType fws, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5210 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5211 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5212 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5213 | xmlSchemaWhitespaceValueType ws) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5214 | { |
| 5215 | int ret; |
| 5216 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5217 | if (facet == NULL) |
| 5218 | return(-1); |
| 5219 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5220 | switch (facet->type) { |
| 5221 | case XML_SCHEMA_FACET_PATTERN: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5222 | /* |
| 5223 | * NOTE that for patterns, the @value needs to be the normalized |
| 5224 | * value, *not* the lexical initial value or the canonical value. |
| 5225 | */ |
| 5226 | if (value == NULL) |
| 5227 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5228 | ret = xmlRegexpExec(facet->regexp, value); |
| 5229 | if (ret == 1) |
| 5230 | return(0); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5231 | if (ret == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5232 | return(XML_SCHEMAV_CVC_PATTERN_VALID); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5233 | return(ret); |
| 5234 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5235 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5236 | if (ret == -2) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5237 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5238 | if (ret == -1) |
| 5239 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5240 | return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5241 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5242 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5243 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5244 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5245 | if ((ret == -1) || (ret == 0)) |
| 5246 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5247 | return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5248 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5249 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5250 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5251 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5252 | if (ret == 1) |
| 5253 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5254 | return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5255 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5256 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5257 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5258 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5259 | if ((ret == 1) || (ret == 0)) |
| 5260 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5261 | return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5262 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5263 | /* TODO whitespaces */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5264 | /* |
| 5265 | * NOTE: Whitespace should be handled to normalize |
| 5266 | * the value to be validated against a the facets; |
| 5267 | * not to normalize the value in-between. |
| 5268 | */ |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5269 | return(0); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 5270 | case XML_SCHEMA_FACET_ENUMERATION: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5271 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5272 | /* |
| 5273 | * This is to ensure API compatibility with the old |
| 5274 | * xmlSchemaValidateFacet(). |
| 5275 | * TODO: Get rid of this case. |
| 5276 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5277 | if ((facet->value != NULL) && |
| 5278 | (xmlStrEqual(facet->value, value))) |
| 5279 | return(0); |
| 5280 | } else { |
| 5281 | ret = xmlSchemaCompareValuesWhtspExt(facet->val->type, |
| 5282 | facet->val, facet->value, fws, valType, val, |
| 5283 | value, ws); |
| 5284 | if (ret == -2) |
| 5285 | return(-1); |
| 5286 | if (ret == 0) |
| 5287 | return(0); |
| 5288 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5289 | return(XML_SCHEMAV_CVC_ENUMERATION_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5290 | case XML_SCHEMA_FACET_LENGTH: |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5291 | /* |
| 5292 | * SPEC (1.3) "if {primitive type definition} is QName or NOTATION, |
| 5293 | * then any {value} is facet-valid." |
| 5294 | */ |
| 5295 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5296 | (valType == XML_SCHEMAS_NOTATION)) |
| 5297 | return (0); |
| 5298 | /* No break on purpose. */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5299 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5300 | case XML_SCHEMA_FACET_MINLENGTH: { |
| 5301 | unsigned int len = 0; |
| 5302 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5303 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5304 | (valType == XML_SCHEMAS_NOTATION)) |
| 5305 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5306 | /* |
| 5307 | * TODO: length, maxLength and minLength must be of type |
| 5308 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5309 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5310 | if ((facet->val == NULL) || |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5311 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5312 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5313 | (facet->val->value.decimal.frac != 0)) { |
| 5314 | return(-1); |
| 5315 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5316 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 5317 | len = val->value.hex.total; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 5318 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5319 | len = val->value.base64.total; |
| 5320 | else { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5321 | switch (valType) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5322 | case XML_SCHEMAS_STRING: |
| 5323 | case XML_SCHEMAS_NORMSTRING: |
| 5324 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5325 | /* |
| 5326 | * This is to ensure API compatibility with the old |
| 5327 | * xmlSchemaValidateFacet(). Anyway, this was and |
| 5328 | * is not the correct handling. |
| 5329 | * TODO: Get rid of this case somehow. |
| 5330 | */ |
| 5331 | if (valType == XML_SCHEMAS_STRING) |
| 5332 | len = xmlUTF8Strlen(value); |
| 5333 | else |
| 5334 | len = xmlSchemaNormLen(value); |
| 5335 | } else if (value != NULL) { |
| 5336 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5337 | len = xmlSchemaNormLen(value); |
| 5338 | else |
| 5339 | /* |
| 5340 | * Should be OK for "preserve" as well. |
| 5341 | */ |
| 5342 | len = xmlUTF8Strlen(value); |
| 5343 | } |
| 5344 | break; |
| 5345 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5346 | case XML_SCHEMAS_TOKEN: |
| 5347 | case XML_SCHEMAS_LANGUAGE: |
| 5348 | case XML_SCHEMAS_NMTOKEN: |
| 5349 | case XML_SCHEMAS_NAME: |
| 5350 | case XML_SCHEMAS_NCNAME: |
| 5351 | case XML_SCHEMAS_ID: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5352 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5353 | if (value != NULL) |
| 5354 | len = xmlSchemaNormLen(value); |
| 5355 | break; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5356 | default: |
| 5357 | TODO |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 5358 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5359 | } |
| 5360 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5361 | if (len != facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5362 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5363 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5364 | if (len < facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5365 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5366 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5367 | if (len > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5368 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5369 | } |
| 5370 | break; |
| 5371 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5372 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5373 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5374 | |
| 5375 | if ((facet->val == NULL) || |
Kasimier T. Buchcik | 69dea3a | 2005-11-07 14:02:44 +0000 | [diff] [blame] | 5376 | ((facet->val->type != XML_SCHEMAS_PINTEGER) && |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5377 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5378 | (facet->val->value.decimal.frac != 0)) { |
| 5379 | return(-1); |
| 5380 | } |
| 5381 | if ((val == NULL) || |
| 5382 | ((val->type != XML_SCHEMAS_DECIMAL) && |
| 5383 | (val->type != XML_SCHEMAS_INTEGER) && |
| 5384 | (val->type != XML_SCHEMAS_NPINTEGER) && |
| 5385 | (val->type != XML_SCHEMAS_NINTEGER) && |
| 5386 | (val->type != XML_SCHEMAS_NNINTEGER) && |
| 5387 | (val->type != XML_SCHEMAS_PINTEGER) && |
| 5388 | (val->type != XML_SCHEMAS_INT) && |
| 5389 | (val->type != XML_SCHEMAS_UINT) && |
| 5390 | (val->type != XML_SCHEMAS_LONG) && |
| 5391 | (val->type != XML_SCHEMAS_ULONG) && |
| 5392 | (val->type != XML_SCHEMAS_SHORT) && |
| 5393 | (val->type != XML_SCHEMAS_USHORT) && |
| 5394 | (val->type != XML_SCHEMAS_BYTE) && |
| 5395 | (val->type != XML_SCHEMAS_UBYTE))) { |
| 5396 | return(-1); |
| 5397 | } |
| 5398 | if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) { |
| 5399 | if (val->value.decimal.total > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5400 | return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5401 | |
| 5402 | } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) { |
| 5403 | if (val->value.decimal.frac > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5404 | return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5405 | } |
| 5406 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5407 | default: |
| 5408 | TODO |
| 5409 | } |
| 5410 | return(0); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 5411 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5412 | } |
| 5413 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5414 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5415 | * xmlSchemaValidateFacet: |
| 5416 | * @base: the base type |
| 5417 | * @facet: the facet to check |
| 5418 | * @value: the lexical repr of the value to validate |
| 5419 | * @val: the precomputed value |
| 5420 | * |
| 5421 | * Check a value against a facet condition |
| 5422 | * |
| 5423 | * Returns 0 if the element is schemas valid, a positive error code |
| 5424 | * number otherwise and -1 in case of internal or API error. |
| 5425 | */ |
| 5426 | int |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5427 | xmlSchemaValidateFacet(xmlSchemaTypePtr base, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5428 | xmlSchemaFacetPtr facet, |
| 5429 | const xmlChar *value, |
| 5430 | xmlSchemaValPtr val) |
| 5431 | { |
| 5432 | /* |
| 5433 | * This tries to ensure API compatibility regarding the old |
| 5434 | * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and |
| 5435 | * xmlSchemaValidateFacetWhtsp(). |
| 5436 | */ |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5437 | if (val != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5438 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5439 | XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5440 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5441 | else if (base != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5442 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5443 | XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5444 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5445 | return(-1); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
| 5448 | /** |
| 5449 | * xmlSchemaValidateFacetWhtsp: |
| 5450 | * @facet: the facet to check |
| 5451 | * @fws: the whitespace type of the facet's value |
| 5452 | * @valType: the built-in type of the value |
| 5453 | * @value: the lexical (or normalized for pattern) repr of the value to validate |
| 5454 | * @val: the precomputed value |
| 5455 | * @ws: the whitespace type of the value |
| 5456 | * |
| 5457 | * Check a value against a facet condition. This takes value normalization |
| 5458 | * according to the specified whitespace types into account. |
| 5459 | * Note that @value needs to be the *normalized* value if the facet |
| 5460 | * is of type "pattern". |
| 5461 | * |
| 5462 | * Returns 0 if the element is schemas valid, a positive error code |
| 5463 | * number otherwise and -1 in case of internal or API error. |
| 5464 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5465 | int |
| 5466 | xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5467 | xmlSchemaWhitespaceValueType fws, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5468 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5469 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5470 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5471 | xmlSchemaWhitespaceValueType ws) |
| 5472 | { |
| 5473 | return(xmlSchemaValidateFacetInternal(facet, fws, valType, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5474 | value, val, ws)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5475 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5476 | |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5477 | #if 0 |
| 5478 | #ifndef DBL_DIG |
| 5479 | #define DBL_DIG 16 |
| 5480 | #endif |
| 5481 | #ifndef DBL_EPSILON |
| 5482 | #define DBL_EPSILON 1E-9 |
| 5483 | #endif |
| 5484 | |
| 5485 | #define INTEGER_DIGITS DBL_DIG |
| 5486 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 5487 | #define EXPONENT_DIGITS (3 + 2) |
| 5488 | |
| 5489 | /** |
| 5490 | * xmlXPathFormatNumber: |
| 5491 | * @number: number to format |
| 5492 | * @buffer: output buffer |
| 5493 | * @buffersize: size of output buffer |
| 5494 | * |
| 5495 | * Convert the number into a string representation. |
| 5496 | */ |
| 5497 | static void |
| 5498 | xmlSchemaFormatFloat(double number, char buffer[], int buffersize) |
| 5499 | { |
| 5500 | switch (xmlXPathIsInf(number)) { |
| 5501 | case 1: |
| 5502 | if (buffersize > (int)sizeof("INF")) |
| 5503 | snprintf(buffer, buffersize, "INF"); |
| 5504 | break; |
| 5505 | case -1: |
| 5506 | if (buffersize > (int)sizeof("-INF")) |
| 5507 | snprintf(buffer, buffersize, "-INF"); |
| 5508 | break; |
| 5509 | default: |
| 5510 | if (xmlXPathIsNaN(number)) { |
| 5511 | if (buffersize > (int)sizeof("NaN")) |
| 5512 | snprintf(buffer, buffersize, "NaN"); |
| 5513 | } else if (number == 0) { |
| 5514 | snprintf(buffer, buffersize, "0.0E0"); |
| 5515 | } else { |
| 5516 | /* 3 is sign, decimal point, and terminating zero */ |
| 5517 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 5518 | int integer_place, fraction_place; |
| 5519 | char *ptr; |
| 5520 | char *after_fraction; |
| 5521 | double absolute_value; |
| 5522 | int size; |
| 5523 | |
| 5524 | absolute_value = fabs(number); |
| 5525 | |
| 5526 | /* |
| 5527 | * Result is in work, and after_fraction points |
| 5528 | * just past the fractional part. |
| 5529 | * Use scientific notation |
| 5530 | */ |
| 5531 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 5532 | fraction_place = DBL_DIG - 1; |
| 5533 | snprintf(work, sizeof(work),"%*.*e", |
| 5534 | integer_place, fraction_place, number); |
| 5535 | after_fraction = strchr(work + DBL_DIG, 'e'); |
| 5536 | /* Remove fractional trailing zeroes */ |
| 5537 | ptr = after_fraction; |
| 5538 | while (*(--ptr) == '0') |
| 5539 | ; |
| 5540 | if (*ptr != '.') |
| 5541 | ptr++; |
| 5542 | while ((*ptr++ = *after_fraction++) != 0); |
| 5543 | |
| 5544 | /* Finally copy result back to caller */ |
| 5545 | size = strlen(work) + 1; |
| 5546 | if (size > buffersize) { |
| 5547 | work[buffersize - 1] = 0; |
| 5548 | size = buffersize; |
| 5549 | } |
| 5550 | memmove(buffer, work, size); |
| 5551 | } |
| 5552 | break; |
| 5553 | } |
| 5554 | } |
| 5555 | #endif |
| 5556 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5557 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5558 | * xmlSchemaGetCanonValue: |
| 5559 | * @val: the precomputed value |
| 5560 | * @retValue: the returned value |
| 5561 | * |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5562 | * Get a the cononical lexical representation of the value. |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5563 | * The caller has to FREE the returned retValue. |
| 5564 | * |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5565 | * WARNING: Some value types are not supported yet, resulting |
| 5566 | * in a @retValue of "???". |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5567 | * |
| 5568 | * TODO: XML Schema 1.0 does not define canonical representations |
| 5569 | * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay, |
| 5570 | * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5571 | * |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5572 | * |
| 5573 | * Returns 0 if the value could be built, 1 if the value type is |
| 5574 | * not supported yet and -1 in case of API errors. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5575 | */ |
| 5576 | int |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5577 | xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5578 | { |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5579 | if ((retValue == NULL) || (val == NULL)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5580 | return (-1); |
| 5581 | *retValue = NULL; |
| 5582 | switch (val->type) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5583 | case XML_SCHEMAS_STRING: |
| 5584 | if (val->value.str == NULL) |
| 5585 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5586 | else |
| 5587 | *retValue = |
| 5588 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
| 5589 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5590 | case XML_SCHEMAS_NORMSTRING: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5591 | if (val->value.str == NULL) |
| 5592 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5593 | else { |
| 5594 | *retValue = xmlSchemaWhiteSpaceReplace( |
| 5595 | (const xmlChar *) val->value.str); |
| 5596 | if ((*retValue) == NULL) |
| 5597 | *retValue = BAD_CAST xmlStrdup( |
| 5598 | (const xmlChar *) val->value.str); |
| 5599 | } |
| 5600 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5601 | case XML_SCHEMAS_TOKEN: |
| 5602 | case XML_SCHEMAS_LANGUAGE: |
| 5603 | case XML_SCHEMAS_NMTOKEN: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5604 | case XML_SCHEMAS_NAME: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5605 | case XML_SCHEMAS_NCNAME: |
| 5606 | case XML_SCHEMAS_ID: |
| 5607 | case XML_SCHEMAS_IDREF: |
| 5608 | case XML_SCHEMAS_ENTITY: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5609 | case XML_SCHEMAS_NOTATION: /* Unclear */ |
| 5610 | case XML_SCHEMAS_ANYURI: /* Unclear */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5611 | if (val->value.str == NULL) |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5612 | return (-1); |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5613 | *retValue = |
| 5614 | BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str); |
| 5615 | if (*retValue == NULL) |
| 5616 | *retValue = |
| 5617 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5618 | break; |
| 5619 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5620 | /* TODO: Unclear in XML Schema 1.0. */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5621 | if (val->value.qname.uri == NULL) { |
| 5622 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name); |
| 5623 | return (0); |
| 5624 | } else { |
| 5625 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "{"); |
| 5626 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5627 | BAD_CAST val->value.qname.uri); |
| 5628 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5629 | BAD_CAST "}"); |
| 5630 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5631 | BAD_CAST val->value.qname.uri); |
| 5632 | } |
| 5633 | break; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5634 | case XML_SCHEMAS_DECIMAL: |
| 5635 | /* |
| 5636 | * TODO: Lookout for a more simple implementation. |
| 5637 | */ |
| 5638 | if ((val->value.decimal.total == 1) && |
| 5639 | (val->value.decimal.lo == 0)) { |
| 5640 | *retValue = xmlStrdup(BAD_CAST "0.0"); |
| 5641 | } else { |
| 5642 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5643 | int bufsize; |
| 5644 | char *buf = NULL, *offs; |
| 5645 | |
| 5646 | /* Add room for the decimal point as well. */ |
| 5647 | bufsize = dec.total + 2; |
| 5648 | if (dec.sign) |
| 5649 | bufsize++; |
| 5650 | /* Add room for leading/trailing zero. */ |
| 5651 | if ((dec.frac == 0) || (dec.frac == dec.total)) |
| 5652 | bufsize++; |
| 5653 | buf = xmlMalloc(bufsize); |
| 5654 | offs = buf; |
| 5655 | if (dec.sign) |
| 5656 | *offs++ = '-'; |
| 5657 | if (dec.frac == dec.total) { |
| 5658 | *offs++ = '0'; |
| 5659 | *offs++ = '.'; |
| 5660 | } |
| 5661 | if (dec.hi != 0) |
| 5662 | snprintf(offs, bufsize - (offs - buf), |
| 5663 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5664 | else if (dec.mi != 0) |
| 5665 | snprintf(offs, bufsize - (offs - buf), |
| 5666 | "%lu%lu", dec.mi, dec.lo); |
| 5667 | else |
| 5668 | snprintf(offs, bufsize - (offs - buf), |
| 5669 | "%lu", dec.lo); |
| 5670 | |
| 5671 | if (dec.frac != 0) { |
| 5672 | if (dec.frac != dec.total) { |
| 5673 | int diff = dec.total - dec.frac; |
| 5674 | /* |
| 5675 | * Insert the decimal point. |
| 5676 | */ |
| 5677 | memmove(offs + diff + 1, offs + diff, dec.frac +1); |
| 5678 | offs[diff] = '.'; |
| 5679 | } else { |
| 5680 | unsigned int i = 0; |
| 5681 | /* |
| 5682 | * Insert missing zeroes behind the decimal point. |
| 5683 | */ |
| 5684 | while (*(offs + i) != 0) |
| 5685 | i++; |
| 5686 | if (i < dec.total) { |
| 5687 | memmove(offs + (dec.total - i), offs, i +1); |
| 5688 | memset(offs, '0', dec.total - i); |
| 5689 | } |
| 5690 | } |
| 5691 | } else { |
| 5692 | /* |
| 5693 | * Append decimal point and zero. |
| 5694 | */ |
| 5695 | offs = buf + bufsize - 1; |
| 5696 | *offs-- = 0; |
| 5697 | *offs-- = '0'; |
| 5698 | *offs-- = '.'; |
| 5699 | } |
| 5700 | *retValue = BAD_CAST buf; |
| 5701 | } |
| 5702 | break; |
| 5703 | case XML_SCHEMAS_INTEGER: |
| 5704 | case XML_SCHEMAS_PINTEGER: |
| 5705 | case XML_SCHEMAS_NPINTEGER: |
| 5706 | case XML_SCHEMAS_NINTEGER: |
| 5707 | case XML_SCHEMAS_NNINTEGER: |
| 5708 | case XML_SCHEMAS_LONG: |
| 5709 | case XML_SCHEMAS_BYTE: |
| 5710 | case XML_SCHEMAS_SHORT: |
| 5711 | case XML_SCHEMAS_INT: |
| 5712 | case XML_SCHEMAS_UINT: |
| 5713 | case XML_SCHEMAS_ULONG: |
| 5714 | case XML_SCHEMAS_USHORT: |
| 5715 | case XML_SCHEMAS_UBYTE: |
| 5716 | if ((val->value.decimal.total == 1) && |
| 5717 | (val->value.decimal.lo == 0)) |
| 5718 | *retValue = xmlStrdup(BAD_CAST "0"); |
| 5719 | else { |
| 5720 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5721 | int bufsize = dec.total + 1; |
| 5722 | |
| 5723 | /* Add room for the decimal point as well. */ |
| 5724 | if (dec.sign) |
| 5725 | bufsize++; |
| 5726 | *retValue = xmlMalloc(bufsize); |
| 5727 | if (dec.hi != 0) { |
| 5728 | if (dec.sign) |
| 5729 | snprintf((char *) *retValue, bufsize, |
| 5730 | "-%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5731 | else |
| 5732 | snprintf((char *) *retValue, bufsize, |
| 5733 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5734 | } else if (dec.mi != 0) { |
| 5735 | if (dec.sign) |
| 5736 | snprintf((char *) *retValue, bufsize, |
| 5737 | "-%lu%lu", dec.mi, dec.lo); |
| 5738 | else |
| 5739 | snprintf((char *) *retValue, bufsize, |
| 5740 | "%lu%lu", dec.mi, dec.lo); |
| 5741 | } else { |
| 5742 | if (dec.sign) |
| 5743 | snprintf((char *) *retValue, bufsize, "-%lu", dec.lo); |
| 5744 | else |
| 5745 | snprintf((char *) *retValue, bufsize, "%lu", dec.lo); |
| 5746 | } |
| 5747 | } |
| 5748 | break; |
| 5749 | case XML_SCHEMAS_BOOLEAN: |
| 5750 | if (val->value.b) |
| 5751 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "true"); |
| 5752 | else |
| 5753 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "false"); |
| 5754 | break; |
| 5755 | case XML_SCHEMAS_DURATION: { |
| 5756 | char buf[100]; |
| 5757 | unsigned long year; |
| 5758 | unsigned long mon, day, hour = 0, min = 0; |
| 5759 | double sec = 0, left; |
| 5760 | |
| 5761 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5762 | /* |
| 5763 | * TODO: This results in a normalized output of the value |
| 5764 | * - which is NOT conformant to the spec - |
| 5765 | * since the exact values of each property are not |
| 5766 | * recoverable. Think about extending the structure to |
| 5767 | * provide a field for every property. |
| 5768 | */ |
| 5769 | year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12); |
| 5770 | mon = labs(val->value.dur.mon) - 12 * year; |
| 5771 | |
| 5772 | day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400); |
| 5773 | left = fabs(val->value.dur.sec) - day * 86400; |
| 5774 | if (left > 0) { |
| 5775 | hour = (unsigned long) FQUOTIENT(left, 3600); |
| 5776 | left = left - (hour * 3600); |
| 5777 | if (left > 0) { |
| 5778 | min = (unsigned long) FQUOTIENT(left, 60); |
| 5779 | sec = left - (min * 60); |
| 5780 | } |
| 5781 | } |
| 5782 | if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0)) |
| 5783 | snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS", |
| 5784 | year, mon, day, hour, min, sec); |
| 5785 | else |
| 5786 | snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS", |
| 5787 | year, mon, day, hour, min, sec); |
| 5788 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5789 | } |
| 5790 | break; |
| 5791 | case XML_SCHEMAS_GYEAR: { |
| 5792 | char buf[30]; |
| 5793 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5794 | /* TODO: What to do with the timezone? */ |
| 5795 | snprintf(buf, 30, "%04ld", val->value.date.year); |
| 5796 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5797 | } |
| 5798 | break; |
| 5799 | case XML_SCHEMAS_GMONTH: { |
| 5800 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5801 | /* TODO: What to do with the timezone? */ |
| 5802 | *retValue = xmlMalloc(5); |
| 5803 | snprintf((char *) *retValue, 6, "--%02u", |
| 5804 | val->value.date.mon); |
| 5805 | } |
| 5806 | break; |
| 5807 | case XML_SCHEMAS_GDAY: { |
| 5808 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5809 | /* TODO: What to do with the timezone? */ |
| 5810 | *retValue = xmlMalloc(6); |
| 5811 | snprintf((char *) *retValue, 6, "---%02u", |
| 5812 | val->value.date.day); |
| 5813 | } |
| 5814 | break; |
| 5815 | case XML_SCHEMAS_GMONTHDAY: { |
| 5816 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5817 | /* TODO: What to do with the timezone? */ |
| 5818 | *retValue = xmlMalloc(8); |
| 5819 | snprintf((char *) *retValue, 8, "--%02u-%02u", |
| 5820 | val->value.date.mon, val->value.date.day); |
| 5821 | } |
| 5822 | break; |
| 5823 | case XML_SCHEMAS_GYEARMONTH: { |
| 5824 | char buf[35]; |
| 5825 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5826 | /* TODO: What to do with the timezone? */ |
| 5827 | if (val->value.date.year < 0) |
| 5828 | snprintf(buf, 35, "-%04ld-%02u", |
| 5829 | labs(val->value.date.year), |
| 5830 | val->value.date.mon); |
| 5831 | else |
| 5832 | snprintf(buf, 35, "%04ld-%02u", |
| 5833 | val->value.date.year, val->value.date.mon); |
| 5834 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5835 | } |
| 5836 | break; |
| 5837 | case XML_SCHEMAS_TIME: |
| 5838 | { |
| 5839 | char buf[30]; |
| 5840 | |
| 5841 | if (val->value.date.tz_flag) { |
| 5842 | xmlSchemaValPtr norm; |
| 5843 | |
| 5844 | norm = xmlSchemaDateNormalize(val, 0); |
| 5845 | if (norm == NULL) |
| 5846 | return (-1); |
| 5847 | /* |
| 5848 | * TODO: Check if "%.14g" is portable. |
| 5849 | */ |
| 5850 | snprintf(buf, 30, |
| 5851 | "%02u:%02u:%02.14gZ", |
| 5852 | norm->value.date.hour, |
| 5853 | norm->value.date.min, |
| 5854 | norm->value.date.sec); |
| 5855 | xmlSchemaFreeValue(norm); |
| 5856 | } else { |
| 5857 | snprintf(buf, 30, |
| 5858 | "%02u:%02u:%02.14g", |
| 5859 | val->value.date.hour, |
| 5860 | val->value.date.min, |
| 5861 | val->value.date.sec); |
| 5862 | } |
| 5863 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5864 | } |
| 5865 | break; |
| 5866 | case XML_SCHEMAS_DATE: |
| 5867 | { |
| 5868 | char buf[30]; |
| 5869 | |
| 5870 | if (val->value.date.tz_flag) { |
| 5871 | xmlSchemaValPtr norm; |
| 5872 | |
| 5873 | norm = xmlSchemaDateNormalize(val, 0); |
| 5874 | if (norm == NULL) |
| 5875 | return (-1); |
| 5876 | /* |
| 5877 | * TODO: Append the canonical value of the |
| 5878 | * recoverable timezone and not "Z". |
| 5879 | */ |
| 5880 | snprintf(buf, 30, |
| 5881 | "%04ld:%02u:%02uZ", |
| 5882 | norm->value.date.year, norm->value.date.mon, |
| 5883 | norm->value.date.day); |
| 5884 | xmlSchemaFreeValue(norm); |
| 5885 | } else { |
| 5886 | snprintf(buf, 30, |
| 5887 | "%04ld:%02u:%02u", |
| 5888 | val->value.date.year, val->value.date.mon, |
| 5889 | val->value.date.day); |
| 5890 | } |
| 5891 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5892 | } |
| 5893 | break; |
| 5894 | case XML_SCHEMAS_DATETIME: |
| 5895 | { |
| 5896 | char buf[50]; |
| 5897 | |
| 5898 | if (val->value.date.tz_flag) { |
| 5899 | xmlSchemaValPtr norm; |
| 5900 | |
| 5901 | norm = xmlSchemaDateNormalize(val, 0); |
| 5902 | if (norm == NULL) |
| 5903 | return (-1); |
| 5904 | /* |
| 5905 | * TODO: Check if "%.14g" is portable. |
| 5906 | */ |
| 5907 | snprintf(buf, 50, |
| 5908 | "%04ld:%02u:%02uT%02u:%02u:%02.14gZ", |
| 5909 | norm->value.date.year, norm->value.date.mon, |
| 5910 | norm->value.date.day, norm->value.date.hour, |
| 5911 | norm->value.date.min, norm->value.date.sec); |
| 5912 | xmlSchemaFreeValue(norm); |
| 5913 | } else { |
| 5914 | snprintf(buf, 50, |
| 5915 | "%04ld:%02u:%02uT%02u:%02u:%02.14g", |
| 5916 | val->value.date.year, val->value.date.mon, |
| 5917 | val->value.date.day, val->value.date.hour, |
| 5918 | val->value.date.min, val->value.date.sec); |
| 5919 | } |
| 5920 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5921 | } |
| 5922 | break; |
| 5923 | case XML_SCHEMAS_HEXBINARY: |
| 5924 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str); |
| 5925 | break; |
| 5926 | case XML_SCHEMAS_BASE64BINARY: |
| 5927 | /* |
| 5928 | * TODO: Is the following spec piece implemented?: |
| 5929 | * SPEC: "Note: For some values the canonical form defined |
| 5930 | * above does not conform to [RFC 2045], which requires breaking |
| 5931 | * with linefeeds at appropriate intervals." |
| 5932 | */ |
| 5933 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str); |
| 5934 | break; |
| 5935 | case XML_SCHEMAS_FLOAT: { |
| 5936 | char buf[30]; |
| 5937 | /* |
| 5938 | * |m| < 16777216, -149 <= e <= 104. |
| 5939 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 5940 | * yet conformant. The c type float does not cover |
| 5941 | * the whole range. |
| 5942 | */ |
| 5943 | snprintf(buf, 30, "%01.14e", val->value.f); |
| 5944 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5945 | } |
| 5946 | break; |
| 5947 | case XML_SCHEMAS_DOUBLE: { |
| 5948 | char buf[40]; |
| 5949 | /* |m| < 9007199254740992, -1075 <= e <= 970 */ |
| 5950 | /* |
| 5951 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 5952 | * yet conformant. The c type float does not cover |
| 5953 | * the whole range. |
| 5954 | */ |
| 5955 | snprintf(buf, 40, "%01.14e", val->value.d); |
| 5956 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5957 | } |
| 5958 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5959 | default: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5960 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "???"); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5961 | return (1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5962 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5963 | return (0); |
| 5964 | } |
| 5965 | |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 5966 | /** |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5967 | * xmlSchemaGetCanonValueWhtsp: |
| 5968 | * @val: the precomputed value |
| 5969 | * @retValue: the returned value |
| 5970 | * @ws: the whitespace type of the value |
| 5971 | * |
| 5972 | * Get a the cononical representation of the value. |
| 5973 | * The caller has to free the returned @retValue. |
| 5974 | * |
| 5975 | * Returns 0 if the value could be built, 1 if the value type is |
| 5976 | * not supported yet and -1 in case of API errors. |
| 5977 | */ |
| 5978 | int |
| 5979 | xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val, |
| 5980 | const xmlChar **retValue, |
| 5981 | xmlSchemaWhitespaceValueType ws) |
| 5982 | { |
| 5983 | if ((retValue == NULL) || (val == NULL)) |
| 5984 | return (-1); |
| 5985 | if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) || |
| 5986 | (ws > XML_SCHEMA_WHITESPACE_COLLAPSE)) |
| 5987 | return (-1); |
| 5988 | |
| 5989 | *retValue = NULL; |
| 5990 | switch (val->type) { |
| 5991 | case XML_SCHEMAS_STRING: |
| 5992 | if (val->value.str == NULL) |
| 5993 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5994 | else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5995 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 5996 | else if (ws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 5997 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 5998 | if ((*retValue) == NULL) |
| 5999 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6000 | break; |
| 6001 | case XML_SCHEMAS_NORMSTRING: |
| 6002 | if (val->value.str == NULL) |
| 6003 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 6004 | else { |
| 6005 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 6006 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 6007 | else |
| 6008 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 6009 | if ((*retValue) == NULL) |
| 6010 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6011 | } |
| 6012 | break; |
| 6013 | default: |
| 6014 | return (xmlSchemaGetCanonValue(val, retValue)); |
| 6015 | } |
| 6016 | return (0); |
| 6017 | } |
| 6018 | |
| 6019 | /** |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6020 | * xmlSchemaGetValType: |
| 6021 | * @val: a schemas value |
| 6022 | * |
| 6023 | * Accessor for the type of a value |
| 6024 | * |
| 6025 | * Returns the xmlSchemaValType of the value |
| 6026 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6027 | xmlSchemaValType |
| 6028 | xmlSchemaGetValType(xmlSchemaValPtr val) |
| 6029 | { |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6030 | if (val == NULL) |
| 6031 | return(XML_SCHEMAS_UNKNOWN); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6032 | return (val->type); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6033 | } |
| 6034 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 6035 | #define bottom_xmlschemastypes |
| 6036 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6037 | #endif /* LIBXML_SCHEMAS_ENABLED */ |