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 | 6a0baa0 | 2005-12-10 11:11:12 +0000 | [diff] [blame] | 1699 | if ((ret != 0) || (*cur != 0) || (!(VALID_DATETIME((&(dt->value.date)))))) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 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; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2247 | unsigned int len, neg, integ, hasLeadingZeroes; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2248 | xmlChar cval[25]; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2249 | xmlChar *cptr = cval; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2250 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2251 | if ((cur == NULL) || (*cur == 0)) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2252 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2253 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2254 | /* |
| 2255 | * xs:decimal has a whitespace-facet value of 'collapse'. |
| 2256 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2257 | if (normOnTheFly) |
| 2258 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2259 | |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2260 | /* |
| 2261 | * First we handle an optional sign. |
| 2262 | */ |
| 2263 | neg = 0; |
| 2264 | if (*cur == '-') { |
| 2265 | neg = 1; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2266 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2267 | } else if (*cur == '+') |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2268 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2269 | /* |
| 2270 | * Disallow: "", "-", "- " |
| 2271 | */ |
| 2272 | if (*cur == 0) |
| 2273 | goto return1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2274 | /* |
| 2275 | * Next we "pre-parse" the number, in preparation for calling |
| 2276 | * the common routine xmlSchemaParseUInt. We get rid of any |
| 2277 | * leading zeroes (because we have reserved only 25 chars), |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2278 | * and note the position of a decimal point. |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2279 | */ |
| 2280 | len = 0; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2281 | integ = ~0u; |
| 2282 | hasLeadingZeroes = 0; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2283 | /* |
| 2284 | * Skip leading zeroes. |
| 2285 | */ |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2286 | while (*cur == '0') { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2287 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2288 | hasLeadingZeroes = 1; |
| 2289 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2290 | if (*cur != 0) { |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2291 | do { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2292 | if ((*cur >= '0') && (*cur <= '9')) { |
| 2293 | *cptr++ = *cur++; |
| 2294 | len++; |
| 2295 | } else if (*cur == '.') { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2296 | cur++; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2297 | integ = len; |
| 2298 | do { |
| 2299 | if ((*cur >= '0') && (*cur <= '9')) { |
| 2300 | *cptr++ = *cur++; |
| 2301 | len++; |
| 2302 | } else |
| 2303 | break; |
| 2304 | } while (len < 24); |
| 2305 | /* |
| 2306 | * Disallow "." but allow "00." |
| 2307 | */ |
| 2308 | if ((len == 0) && (!hasLeadingZeroes)) |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2309 | goto return1; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | } else |
| 2312 | break; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2313 | } while (len < 24); |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2314 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2315 | if (normOnTheFly) |
| 2316 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2317 | if (*cur != 0) |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2318 | goto return1; /* error if any extraneous chars */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2319 | if (val != NULL) { |
| 2320 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 2321 | if (v != NULL) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2322 | /* |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2323 | * Now evaluate the significant digits of the number |
| 2324 | */ |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2325 | if (len != 0) { |
| 2326 | |
| 2327 | if (integ != ~0u) { |
| 2328 | /* |
| 2329 | * Get rid of trailing zeroes in the |
| 2330 | * fractional part. |
| 2331 | */ |
| 2332 | while ((len != integ) && (*(cptr-1) == '0')) { |
| 2333 | cptr--; |
| 2334 | len--; |
| 2335 | } |
| 2336 | } |
| 2337 | /* |
| 2338 | * Terminate the (preparsed) string. |
| 2339 | */ |
| 2340 | if (len != 0) { |
| 2341 | *cptr = 0; |
| 2342 | cptr = cval; |
| 2343 | |
| 2344 | xmlSchemaParseUInt((const xmlChar **)&cptr, |
| 2345 | &v->value.decimal.lo, |
| 2346 | &v->value.decimal.mi, |
| 2347 | &v->value.decimal.hi); |
| 2348 | } |
| 2349 | } |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2350 | /* |
| 2351 | * Set the total digits to 1 if a zero value. |
| 2352 | */ |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2353 | v->value.decimal.sign = neg; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2354 | if (len == 0) { |
| 2355 | /* Speedup for zero values. */ |
| 2356 | v->value.decimal.total = 1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2357 | } else { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 2358 | v->value.decimal.total = len; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 2359 | if (integ == ~0u) |
| 2360 | v->value.decimal.frac = 0; |
| 2361 | else |
| 2362 | v->value.decimal.frac = len - integ; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 2363 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2364 | *val = v; |
| 2365 | } |
| 2366 | } |
| 2367 | goto return0; |
| 2368 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2369 | case XML_SCHEMAS_TIME: |
| 2370 | case XML_SCHEMAS_GDAY: |
| 2371 | case XML_SCHEMAS_GMONTH: |
| 2372 | case XML_SCHEMAS_GMONTHDAY: |
| 2373 | case XML_SCHEMAS_GYEAR: |
| 2374 | case XML_SCHEMAS_GYEARMONTH: |
| 2375 | case XML_SCHEMAS_DATE: |
| 2376 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2377 | ret = xmlSchemaValidateDates(type->builtInType, value, val, |
| 2378 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2379 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2380 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2381 | ret = xmlSchemaValidateDuration(type, value, val, |
| 2382 | normOnTheFly); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2383 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2384 | case XML_SCHEMAS_FLOAT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2385 | case XML_SCHEMAS_DOUBLE:{ |
| 2386 | const xmlChar *cur = value; |
| 2387 | int neg = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2388 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2389 | if (cur == NULL) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2390 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2391 | |
| 2392 | if (normOnTheFly) |
| 2393 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2394 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2395 | if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) { |
| 2396 | cur += 3; |
| 2397 | if (*cur != 0) |
| 2398 | goto return1; |
| 2399 | if (val != NULL) { |
| 2400 | if (type == xmlSchemaTypeFloatDef) { |
| 2401 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2402 | if (v != NULL) { |
| 2403 | v->value.f = (float) xmlXPathNAN; |
| 2404 | } else { |
| 2405 | xmlSchemaFreeValue(v); |
| 2406 | goto error; |
| 2407 | } |
| 2408 | } else { |
| 2409 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2410 | if (v != NULL) { |
| 2411 | v->value.d = xmlXPathNAN; |
| 2412 | } else { |
| 2413 | xmlSchemaFreeValue(v); |
| 2414 | goto error; |
| 2415 | } |
| 2416 | } |
| 2417 | *val = v; |
| 2418 | } |
| 2419 | goto return0; |
| 2420 | } |
| 2421 | if (*cur == '-') { |
| 2422 | neg = 1; |
| 2423 | cur++; |
| 2424 | } |
| 2425 | if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) { |
| 2426 | cur += 3; |
| 2427 | if (*cur != 0) |
| 2428 | goto return1; |
| 2429 | if (val != NULL) { |
| 2430 | if (type == xmlSchemaTypeFloatDef) { |
| 2431 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2432 | if (v != NULL) { |
| 2433 | if (neg) |
| 2434 | v->value.f = (float) xmlXPathNINF; |
| 2435 | else |
| 2436 | v->value.f = (float) xmlXPathPINF; |
| 2437 | } else { |
| 2438 | xmlSchemaFreeValue(v); |
| 2439 | goto error; |
| 2440 | } |
| 2441 | } else { |
| 2442 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2443 | if (v != NULL) { |
| 2444 | if (neg) |
| 2445 | v->value.d = xmlXPathNINF; |
| 2446 | else |
| 2447 | v->value.d = xmlXPathPINF; |
| 2448 | } else { |
| 2449 | xmlSchemaFreeValue(v); |
| 2450 | goto error; |
| 2451 | } |
| 2452 | } |
| 2453 | *val = v; |
| 2454 | } |
| 2455 | goto return0; |
| 2456 | } |
| 2457 | if ((neg == 0) && (*cur == '+')) |
| 2458 | cur++; |
| 2459 | if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-')) |
| 2460 | goto return1; |
| 2461 | while ((*cur >= '0') && (*cur <= '9')) { |
| 2462 | cur++; |
| 2463 | } |
| 2464 | if (*cur == '.') { |
| 2465 | cur++; |
| 2466 | while ((*cur >= '0') && (*cur <= '9')) |
| 2467 | cur++; |
| 2468 | } |
| 2469 | if ((*cur == 'e') || (*cur == 'E')) { |
| 2470 | cur++; |
| 2471 | if ((*cur == '-') || (*cur == '+')) |
| 2472 | cur++; |
| 2473 | while ((*cur >= '0') && (*cur <= '9')) |
| 2474 | cur++; |
| 2475 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2476 | if (normOnTheFly) |
| 2477 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2478 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2479 | if (*cur != 0) |
| 2480 | goto return1; |
| 2481 | if (val != NULL) { |
| 2482 | if (type == xmlSchemaTypeFloatDef) { |
| 2483 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2484 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2485 | /* |
| 2486 | * TODO: sscanf seems not to give the correct |
| 2487 | * value for extremely high/low values. |
| 2488 | * E.g. "1E-149" results in zero. |
| 2489 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2490 | if (sscanf((const char *) value, "%f", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2491 | &(v->value.f)) == 1) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2492 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2493 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2494 | xmlSchemaFreeValue(v); |
| 2495 | goto return1; |
| 2496 | } |
| 2497 | } else { |
| 2498 | goto error; |
| 2499 | } |
| 2500 | } else { |
| 2501 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2502 | if (v != NULL) { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 2503 | /* |
| 2504 | * TODO: sscanf seems not to give the correct |
| 2505 | * value for extremely high/low values. |
| 2506 | */ |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2507 | if (sscanf((const char *) value, "%lf", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2508 | &(v->value.d)) == 1) { |
| 2509 | *val = v; |
| 2510 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2511 | xmlSchemaFreeValue(v); |
| 2512 | goto return1; |
| 2513 | } |
| 2514 | } else { |
| 2515 | goto error; |
| 2516 | } |
| 2517 | } |
| 2518 | } |
| 2519 | goto return0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2520 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2521 | case XML_SCHEMAS_BOOLEAN:{ |
| 2522 | const xmlChar *cur = value; |
| 2523 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2524 | if (normOnTheFly) { |
| 2525 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2526 | if (*cur == '0') { |
| 2527 | ret = 0; |
| 2528 | cur++; |
| 2529 | } else if (*cur == '1') { |
| 2530 | ret = 1; |
| 2531 | cur++; |
| 2532 | } else if (*cur == 't') { |
| 2533 | cur++; |
| 2534 | if ((*cur++ == 'r') && (*cur++ == 'u') && |
| 2535 | (*cur++ == 'e')) { |
| 2536 | ret = 1; |
| 2537 | } else |
| 2538 | goto return1; |
| 2539 | } else if (*cur == 'f') { |
| 2540 | cur++; |
| 2541 | if ((*cur++ == 'a') && (*cur++ == 'l') && |
| 2542 | (*cur++ == 's') && (*cur++ == 'e')) { |
| 2543 | ret = 0; |
| 2544 | } else |
| 2545 | goto return1; |
| 2546 | } |
| 2547 | if (*cur != 0) { |
| 2548 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2549 | if (*cur != 0) |
| 2550 | goto return1; |
| 2551 | } |
| 2552 | } else { |
| 2553 | if ((cur[0] == '0') && (cur[1] == 0)) |
| 2554 | ret = 0; |
| 2555 | else if ((cur[0] == '1') && (cur[1] == 0)) |
| 2556 | ret = 1; |
| 2557 | else if ((cur[0] == 't') && (cur[1] == 'r') |
| 2558 | && (cur[2] == 'u') && (cur[3] == 'e') |
| 2559 | && (cur[4] == 0)) |
| 2560 | ret = 1; |
| 2561 | else if ((cur[0] == 'f') && (cur[1] == 'a') |
| 2562 | && (cur[2] == 'l') && (cur[3] == 's') |
| 2563 | && (cur[4] == 'e') && (cur[5] == 0)) |
| 2564 | ret = 0; |
| 2565 | else |
| 2566 | goto return1; |
| 2567 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2568 | if (val != NULL) { |
| 2569 | v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN); |
| 2570 | if (v != NULL) { |
| 2571 | v->value.b = ret; |
| 2572 | *val = v; |
| 2573 | } else { |
| 2574 | goto error; |
| 2575 | } |
| 2576 | } |
| 2577 | goto return0; |
| 2578 | } |
| 2579 | case XML_SCHEMAS_TOKEN:{ |
| 2580 | const xmlChar *cur = value; |
| 2581 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2582 | if (! normOnTheFly) { |
| 2583 | while (*cur != 0) { |
| 2584 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2585 | goto return1; |
| 2586 | } else if (*cur == ' ') { |
| 2587 | cur++; |
| 2588 | if (*cur == 0) |
| 2589 | goto return1; |
| 2590 | if (*cur == ' ') |
| 2591 | goto return1; |
| 2592 | } else { |
| 2593 | cur++; |
| 2594 | } |
| 2595 | } |
| 2596 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2597 | if (val != NULL) { |
| 2598 | v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN); |
| 2599 | if (v != NULL) { |
| 2600 | v->value.str = xmlStrdup(value); |
| 2601 | *val = v; |
| 2602 | } else { |
| 2603 | goto error; |
| 2604 | } |
| 2605 | } |
| 2606 | goto return0; |
| 2607 | } |
| 2608 | case XML_SCHEMAS_LANGUAGE: |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2609 | if (normOnTheFly) { |
| 2610 | norm = xmlSchemaCollapseString(value); |
| 2611 | if (norm != NULL) |
| 2612 | value = norm; |
| 2613 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2614 | if (xmlCheckLanguageID(value) == 1) { |
| 2615 | if (val != NULL) { |
| 2616 | v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE); |
| 2617 | if (v != NULL) { |
| 2618 | v->value.str = xmlStrdup(value); |
| 2619 | *val = v; |
| 2620 | } else { |
| 2621 | goto error; |
| 2622 | } |
| 2623 | } |
| 2624 | goto return0; |
| 2625 | } |
| 2626 | goto return1; |
| 2627 | case XML_SCHEMAS_NMTOKEN: |
| 2628 | if (xmlValidateNMToken(value, 1) == 0) { |
| 2629 | if (val != NULL) { |
| 2630 | v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN); |
| 2631 | if (v != NULL) { |
| 2632 | v->value.str = xmlStrdup(value); |
| 2633 | *val = v; |
| 2634 | } else { |
| 2635 | goto error; |
| 2636 | } |
| 2637 | } |
| 2638 | goto return0; |
| 2639 | } |
| 2640 | goto return1; |
| 2641 | case XML_SCHEMAS_NMTOKENS: |
| 2642 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef, |
| 2643 | value, val, node); |
| 2644 | if (ret > 0) |
| 2645 | ret = 0; |
| 2646 | else |
| 2647 | ret = 1; |
| 2648 | goto done; |
| 2649 | case XML_SCHEMAS_NAME: |
| 2650 | ret = xmlValidateName(value, 1); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 2651 | if ((ret == 0) && (val != NULL) && (value != NULL)) { |
| 2652 | v = xmlSchemaNewValue(XML_SCHEMAS_NAME); |
| 2653 | if (v != NULL) { |
| 2654 | const xmlChar *start = value, *end; |
| 2655 | while (IS_BLANK_CH(*start)) start++; |
| 2656 | end = start; |
| 2657 | while ((*end != 0) && (!IS_BLANK_CH(*end))) end++; |
| 2658 | v->value.str = xmlStrndup(start, end - start); |
| 2659 | *val = v; |
| 2660 | } else { |
| 2661 | goto error; |
| 2662 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2663 | } |
| 2664 | goto done; |
| 2665 | case XML_SCHEMAS_QNAME:{ |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2666 | const xmlChar *uri = NULL; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2667 | xmlChar *local = NULL; |
| 2668 | |
| 2669 | ret = xmlValidateQName(value, 1); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2670 | if (ret != 0) |
| 2671 | goto done; |
| 2672 | if (node != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2673 | xmlChar *prefix; |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2674 | xmlNsPtr ns; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2675 | |
| 2676 | local = xmlSplitQName2(value, &prefix); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2677 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2678 | if ((ns == NULL) && (prefix != NULL)) { |
| 2679 | xmlFree(prefix); |
| 2680 | if (local != NULL) |
| 2681 | xmlFree(local); |
| 2682 | goto return1; |
| 2683 | } |
| 2684 | if (ns != NULL) |
| 2685 | uri = ns->href; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2686 | if (prefix != NULL) |
| 2687 | xmlFree(prefix); |
| 2688 | } |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2689 | if (val != NULL) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2690 | v = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
Kasimier T. Buchcik | 1c720df | 2005-06-09 14:54:59 +0000 | [diff] [blame] | 2691 | if (v == NULL) { |
| 2692 | if (local != NULL) |
| 2693 | xmlFree(local); |
| 2694 | goto error; |
| 2695 | } |
| 2696 | if (local != NULL) |
| 2697 | v->value.qname.name = local; |
| 2698 | else |
| 2699 | v->value.qname.name = xmlStrdup(value); |
| 2700 | if (uri != NULL) |
| 2701 | v->value.qname.uri = xmlStrdup(uri); |
| 2702 | *val = v; |
| 2703 | } else |
| 2704 | if (local != NULL) |
| 2705 | xmlFree(local); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2706 | goto done; |
| 2707 | } |
| 2708 | case XML_SCHEMAS_NCNAME: |
| 2709 | ret = xmlValidateNCName(value, 1); |
| 2710 | if ((ret == 0) && (val != NULL)) { |
| 2711 | v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME); |
| 2712 | if (v != NULL) { |
| 2713 | v->value.str = xmlStrdup(value); |
| 2714 | *val = v; |
| 2715 | } else { |
| 2716 | goto error; |
| 2717 | } |
| 2718 | } |
| 2719 | goto done; |
| 2720 | case XML_SCHEMAS_ID: |
| 2721 | ret = xmlValidateNCName(value, 1); |
| 2722 | if ((ret == 0) && (val != NULL)) { |
| 2723 | v = xmlSchemaNewValue(XML_SCHEMAS_ID); |
| 2724 | if (v != NULL) { |
| 2725 | v->value.str = xmlStrdup(value); |
| 2726 | *val = v; |
| 2727 | } else { |
| 2728 | goto error; |
| 2729 | } |
| 2730 | } |
| 2731 | if ((ret == 0) && (node != NULL) && |
| 2732 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2733 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2734 | |
| 2735 | /* |
| 2736 | * NOTE: the IDness might have already be declared in the DTD |
| 2737 | */ |
| 2738 | if (attr->atype != XML_ATTRIBUTE_ID) { |
| 2739 | xmlIDPtr res; |
| 2740 | xmlChar *strip; |
| 2741 | |
| 2742 | strip = xmlSchemaStrip(value); |
| 2743 | if (strip != NULL) { |
| 2744 | res = xmlAddID(NULL, node->doc, strip, attr); |
| 2745 | xmlFree(strip); |
| 2746 | } else |
| 2747 | res = xmlAddID(NULL, node->doc, value, attr); |
| 2748 | if (res == NULL) { |
| 2749 | ret = 2; |
| 2750 | } else { |
| 2751 | attr->atype = XML_ATTRIBUTE_ID; |
| 2752 | } |
| 2753 | } |
| 2754 | } |
| 2755 | goto done; |
| 2756 | case XML_SCHEMAS_IDREF: |
| 2757 | ret = xmlValidateNCName(value, 1); |
| 2758 | if ((ret == 0) && (val != NULL)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 2759 | v = xmlSchemaNewValue(XML_SCHEMAS_IDREF); |
| 2760 | if (v == NULL) |
| 2761 | goto error; |
| 2762 | v->value.str = xmlStrdup(value); |
| 2763 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2764 | } |
| 2765 | if ((ret == 0) && (node != NULL) && |
| 2766 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2767 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2768 | xmlChar *strip; |
| 2769 | |
| 2770 | strip = xmlSchemaStrip(value); |
| 2771 | if (strip != NULL) { |
| 2772 | xmlAddRef(NULL, node->doc, strip, attr); |
| 2773 | xmlFree(strip); |
| 2774 | } else |
| 2775 | xmlAddRef(NULL, node->doc, value, attr); |
| 2776 | attr->atype = XML_ATTRIBUTE_IDREF; |
| 2777 | } |
| 2778 | goto done; |
| 2779 | case XML_SCHEMAS_IDREFS: |
| 2780 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef, |
| 2781 | value, val, node); |
| 2782 | if (ret < 0) |
| 2783 | ret = 2; |
| 2784 | else |
| 2785 | ret = 0; |
| 2786 | if ((ret == 0) && (node != NULL) && |
| 2787 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2788 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2789 | |
| 2790 | attr->atype = XML_ATTRIBUTE_IDREFS; |
| 2791 | } |
| 2792 | goto done; |
| 2793 | case XML_SCHEMAS_ENTITY:{ |
| 2794 | xmlChar *strip; |
| 2795 | |
| 2796 | ret = xmlValidateNCName(value, 1); |
| 2797 | if ((node == NULL) || (node->doc == NULL)) |
| 2798 | ret = 3; |
| 2799 | if (ret == 0) { |
| 2800 | xmlEntityPtr ent; |
| 2801 | |
| 2802 | strip = xmlSchemaStrip(value); |
| 2803 | if (strip != NULL) { |
| 2804 | ent = xmlGetDocEntity(node->doc, strip); |
| 2805 | xmlFree(strip); |
| 2806 | } else { |
| 2807 | ent = xmlGetDocEntity(node->doc, value); |
| 2808 | } |
| 2809 | if ((ent == NULL) || |
| 2810 | (ent->etype != |
| 2811 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) |
| 2812 | ret = 4; |
| 2813 | } |
| 2814 | if ((ret == 0) && (val != NULL)) { |
| 2815 | TODO; |
| 2816 | } |
| 2817 | if ((ret == 0) && (node != NULL) && |
| 2818 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2819 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2820 | |
| 2821 | attr->atype = XML_ATTRIBUTE_ENTITY; |
| 2822 | } |
| 2823 | goto done; |
| 2824 | } |
| 2825 | case XML_SCHEMAS_ENTITIES: |
| 2826 | if ((node == NULL) || (node->doc == NULL)) |
| 2827 | goto return3; |
| 2828 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef, |
| 2829 | value, val, node); |
| 2830 | if (ret <= 0) |
| 2831 | ret = 1; |
| 2832 | else |
| 2833 | ret = 0; |
| 2834 | if ((ret == 0) && (node != NULL) && |
| 2835 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2836 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2837 | |
| 2838 | attr->atype = XML_ATTRIBUTE_ENTITIES; |
| 2839 | } |
| 2840 | goto done; |
| 2841 | case XML_SCHEMAS_NOTATION:{ |
| 2842 | xmlChar *uri = NULL; |
| 2843 | xmlChar *local = NULL; |
| 2844 | |
| 2845 | ret = xmlValidateQName(value, 1); |
| 2846 | if ((ret == 0) && (node != NULL)) { |
| 2847 | xmlChar *prefix; |
| 2848 | |
| 2849 | local = xmlSplitQName2(value, &prefix); |
| 2850 | if (prefix != NULL) { |
| 2851 | xmlNsPtr ns; |
| 2852 | |
| 2853 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2854 | if (ns == NULL) |
| 2855 | ret = 1; |
| 2856 | else if (val != NULL) |
| 2857 | uri = xmlStrdup(ns->href); |
| 2858 | } |
| 2859 | if ((local != NULL) && ((val == NULL) || (ret != 0))) |
| 2860 | xmlFree(local); |
| 2861 | if (prefix != NULL) |
| 2862 | xmlFree(prefix); |
| 2863 | } |
| 2864 | if ((node == NULL) || (node->doc == NULL)) |
| 2865 | ret = 3; |
| 2866 | if (ret == 0) { |
| 2867 | ret = xmlValidateNotationUse(NULL, node->doc, value); |
| 2868 | if (ret == 1) |
| 2869 | ret = 0; |
| 2870 | else |
| 2871 | ret = 1; |
| 2872 | } |
| 2873 | if ((ret == 0) && (val != NULL)) { |
| 2874 | v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 2875 | if (v != NULL) { |
| 2876 | if (local != NULL) |
| 2877 | v->value.qname.name = local; |
| 2878 | else |
| 2879 | v->value.qname.name = xmlStrdup(value); |
| 2880 | if (uri != NULL) |
| 2881 | v->value.qname.uri = uri; |
| 2882 | |
| 2883 | *val = v; |
| 2884 | } else { |
| 2885 | if (local != NULL) |
| 2886 | xmlFree(local); |
| 2887 | if (uri != NULL) |
| 2888 | xmlFree(uri); |
| 2889 | goto error; |
| 2890 | } |
| 2891 | } |
| 2892 | goto done; |
| 2893 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2894 | case XML_SCHEMAS_ANYURI:{ |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2895 | if (*value != 0) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2896 | xmlURIPtr uri; |
| 2897 | if (normOnTheFly) { |
| 2898 | norm = xmlSchemaCollapseString(value); |
| 2899 | if (norm != NULL) |
| 2900 | value = norm; |
| 2901 | } |
| 2902 | uri = xmlParseURI((const char *) value); |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2903 | if (uri == NULL) |
| 2904 | goto return1; |
| 2905 | xmlFreeURI(uri); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2906 | } |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2907 | |
| 2908 | if (val != NULL) { |
| 2909 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI); |
| 2910 | if (v == NULL) |
| 2911 | goto error; |
| 2912 | v->value.str = xmlStrdup(value); |
| 2913 | *val = v; |
| 2914 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2915 | goto return0; |
| 2916 | } |
| 2917 | case XML_SCHEMAS_HEXBINARY:{ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2918 | const xmlChar *cur = value, *start; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2919 | xmlChar *base; |
| 2920 | int total, i = 0; |
| 2921 | |
Daniel Veillard | f34a20e | 2004-08-31 08:42:17 +0000 | [diff] [blame] | 2922 | if (cur == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2923 | goto return1; |
| 2924 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2925 | if (normOnTheFly) |
| 2926 | while IS_WSP_BLANK_CH(*cur) cur++; |
| 2927 | |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2928 | start = cur; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2929 | while (((*cur >= '0') && (*cur <= '9')) || |
| 2930 | ((*cur >= 'A') && (*cur <= 'F')) || |
| 2931 | ((*cur >= 'a') && (*cur <= 'f'))) { |
| 2932 | i++; |
| 2933 | cur++; |
| 2934 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2935 | if (normOnTheFly) |
| 2936 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2937 | |
| 2938 | if (*cur != 0) |
| 2939 | goto return1; |
| 2940 | if ((i % 2) != 0) |
| 2941 | goto return1; |
| 2942 | |
| 2943 | if (val != NULL) { |
| 2944 | |
| 2945 | v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY); |
| 2946 | if (v == NULL) |
| 2947 | goto error; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 2948 | /* |
| 2949 | * Copy only the normalized piece. |
| 2950 | * CRITICAL TODO: Check this. |
| 2951 | */ |
Kasimier T. Buchcik | 8dd1e1b | 2005-06-09 13:14:38 +0000 | [diff] [blame] | 2952 | cur = xmlStrndup(start, i); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2953 | if (cur == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2954 | xmlSchemaTypeErrMemory(node, "allocating hexbin data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2955 | xmlFree(v); |
| 2956 | goto return1; |
| 2957 | } |
| 2958 | |
| 2959 | total = i / 2; /* number of octets */ |
| 2960 | |
| 2961 | base = (xmlChar *) cur; |
| 2962 | while (i-- > 0) { |
| 2963 | if (*base >= 'a') |
| 2964 | *base = *base - ('a' - 'A'); |
| 2965 | base++; |
| 2966 | } |
| 2967 | |
| 2968 | v->value.hex.str = (xmlChar *) cur; |
| 2969 | v->value.hex.total = total; |
| 2970 | *val = v; |
| 2971 | } |
| 2972 | goto return0; |
| 2973 | } |
| 2974 | case XML_SCHEMAS_BASE64BINARY:{ |
| 2975 | /* ISSUE: |
| 2976 | * |
| 2977 | * Ignore all stray characters? (yes, currently) |
| 2978 | * Worry about long lines? (no, currently) |
| 2979 | * |
| 2980 | * rfc2045.txt: |
| 2981 | * |
| 2982 | * "The encoded output stream must be represented in lines of |
| 2983 | * no more than 76 characters each. All line breaks or other |
| 2984 | * characters not found in Table 1 must be ignored by decoding |
| 2985 | * software. In base64 data, characters other than those in |
| 2986 | * Table 1, line breaks, and other white space probably |
| 2987 | * indicate a transmission error, about which a warning |
| 2988 | * message or even a message rejection might be appropriate |
| 2989 | * under some circumstances." */ |
| 2990 | const xmlChar *cur = value; |
| 2991 | xmlChar *base; |
| 2992 | int total, i = 0, pad = 0; |
| 2993 | |
| 2994 | if (cur == NULL) |
| 2995 | goto return1; |
| 2996 | |
| 2997 | for (; *cur; ++cur) { |
| 2998 | int decc; |
| 2999 | |
| 3000 | decc = _xmlSchemaBase64Decode(*cur); |
| 3001 | if (decc < 0) ; |
| 3002 | else if (decc < 64) |
| 3003 | i++; |
| 3004 | else |
| 3005 | break; |
| 3006 | } |
| 3007 | for (; *cur; ++cur) { |
| 3008 | int decc; |
| 3009 | |
| 3010 | decc = _xmlSchemaBase64Decode(*cur); |
| 3011 | if (decc < 0) ; |
| 3012 | else if (decc < 64) |
| 3013 | goto return1; |
| 3014 | if (decc == 64) |
| 3015 | pad++; |
| 3016 | } |
| 3017 | |
| 3018 | /* rfc2045.txt: "Special processing is performed if fewer than |
| 3019 | * 24 bits are available at the end of the data being encoded. |
| 3020 | * A full encoding quantum is always completed at the end of a |
| 3021 | * body. When fewer than 24 input bits are available in an |
| 3022 | * input group, zero bits are added (on the right) to form an |
| 3023 | * integral number of 6-bit groups. Padding at the end of the |
| 3024 | * data is performed using the "=" character. Since all |
| 3025 | * base64 input is an integral number of octets, only the |
| 3026 | * following cases can arise: (1) the final quantum of |
| 3027 | * encoding input is an integral multiple of 24 bits; here, |
| 3028 | * the final unit of encoded output will be an integral |
| 3029 | * multiple ofindent: Standard input:701: Warning:old style |
| 3030 | * assignment ambiguity in "=*". Assuming "= *" 4 characters |
| 3031 | * with no "=" padding, (2) the final |
| 3032 | * quantum of encoding input is exactly 8 bits; here, the |
| 3033 | * final unit of encoded output will be two characters |
| 3034 | * followed by two "=" padding characters, or (3) the final |
| 3035 | * quantum of encoding input is exactly 16 bits; here, the |
| 3036 | * final unit of encoded output will be three characters |
| 3037 | * followed by one "=" padding character." */ |
| 3038 | |
| 3039 | total = 3 * (i / 4); |
| 3040 | if (pad == 0) { |
| 3041 | if (i % 4 != 0) |
| 3042 | goto return1; |
| 3043 | } else if (pad == 1) { |
| 3044 | int decc; |
| 3045 | |
| 3046 | if (i % 4 != 3) |
| 3047 | goto return1; |
| 3048 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3049 | (decc < 0) || (decc > 63); |
| 3050 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3051 | --cur; |
| 3052 | /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/ |
| 3053 | /* 00111100 -> 0x3c */ |
| 3054 | if (decc & ~0x3c) |
| 3055 | goto return1; |
| 3056 | total += 2; |
| 3057 | } else if (pad == 2) { |
| 3058 | int decc; |
| 3059 | |
| 3060 | if (i % 4 != 2) |
| 3061 | goto return1; |
| 3062 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 3063 | (decc < 0) || (decc > 63); |
| 3064 | decc = _xmlSchemaBase64Decode(*cur)) |
| 3065 | --cur; |
| 3066 | /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */ |
| 3067 | /* 00110000 -> 0x30 */ |
| 3068 | if (decc & ~0x30) |
| 3069 | goto return1; |
| 3070 | total += 1; |
| 3071 | } else |
| 3072 | goto return1; |
| 3073 | |
| 3074 | if (val != NULL) { |
| 3075 | v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY); |
| 3076 | if (v == NULL) |
| 3077 | goto error; |
| 3078 | base = |
| 3079 | (xmlChar *) xmlMallocAtomic((i + pad + 1) * |
| 3080 | sizeof(xmlChar)); |
| 3081 | if (base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 3082 | xmlSchemaTypeErrMemory(node, "allocating base64 data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3083 | xmlFree(v); |
| 3084 | goto return1; |
| 3085 | } |
| 3086 | v->value.base64.str = base; |
| 3087 | for (cur = value; *cur; ++cur) |
| 3088 | if (_xmlSchemaBase64Decode(*cur) >= 0) { |
| 3089 | *base = *cur; |
| 3090 | ++base; |
| 3091 | } |
| 3092 | *base = 0; |
| 3093 | v->value.base64.total = total; |
| 3094 | *val = v; |
| 3095 | } |
| 3096 | goto return0; |
| 3097 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3098 | case XML_SCHEMAS_INTEGER: |
| 3099 | case XML_SCHEMAS_PINTEGER: |
| 3100 | case XML_SCHEMAS_NPINTEGER: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3101 | case XML_SCHEMAS_NINTEGER: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3102 | case XML_SCHEMAS_NNINTEGER:{ |
| 3103 | const xmlChar *cur = value; |
| 3104 | unsigned long lo, mi, hi; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3105 | int sign = 0; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3106 | |
| 3107 | if (cur == NULL) |
| 3108 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3109 | if (normOnTheFly) |
| 3110 | while IS_WSP_BLANK_CH(*cur) cur++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3111 | if (*cur == '-') { |
| 3112 | sign = 1; |
| 3113 | cur++; |
| 3114 | } else if (*cur == '+') |
| 3115 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3116 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3117 | if (ret == -1) |
| 3118 | goto return1; |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3119 | if (normOnTheFly) |
| 3120 | while IS_WSP_BLANK_CH(*cur) cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3121 | if (*cur != 0) |
| 3122 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3123 | if (type->builtInType == XML_SCHEMAS_NPINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3124 | if ((sign == 0) && |
| 3125 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3126 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3127 | } else if (type->builtInType == XML_SCHEMAS_PINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3128 | if (sign == 1) |
| 3129 | goto return1; |
| 3130 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3131 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3132 | } else if (type->builtInType == XML_SCHEMAS_NINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3133 | if (sign == 0) |
| 3134 | goto return1; |
| 3135 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 3136 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3137 | } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3138 | if ((sign == 1) && |
| 3139 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 3140 | goto return1; |
| 3141 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3142 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3143 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3144 | if (v != NULL) { |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3145 | if (ret == 0) |
| 3146 | ret++; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3147 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3148 | v->value.decimal.mi = mi; |
| 3149 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3150 | v->value.decimal.sign = sign; |
| 3151 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3152 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3153 | *val = v; |
| 3154 | } |
| 3155 | } |
| 3156 | goto return0; |
| 3157 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3158 | case XML_SCHEMAS_LONG: |
| 3159 | case XML_SCHEMAS_BYTE: |
| 3160 | case XML_SCHEMAS_SHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3161 | case XML_SCHEMAS_INT:{ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 3162 | const xmlChar *cur = value; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3163 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3164 | int sign = 0; |
| 3165 | |
| 3166 | if (cur == NULL) |
| 3167 | goto return1; |
| 3168 | if (*cur == '-') { |
| 3169 | sign = 1; |
| 3170 | cur++; |
| 3171 | } else if (*cur == '+') |
| 3172 | cur++; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3173 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3174 | if (ret < 0) |
| 3175 | goto return1; |
| 3176 | if (*cur != 0) |
| 3177 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3178 | if (type->builtInType == XML_SCHEMAS_LONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3179 | if (hi >= 922) { |
| 3180 | if (hi > 922) |
| 3181 | goto return1; |
| 3182 | if (mi >= 33720368) { |
| 3183 | if (mi > 33720368) |
| 3184 | goto return1; |
| 3185 | if ((sign == 0) && (lo > 54775807)) |
| 3186 | goto return1; |
| 3187 | if ((sign == 1) && (lo > 54775808)) |
| 3188 | goto return1; |
| 3189 | } |
| 3190 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3191 | } else if (type->builtInType == XML_SCHEMAS_INT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3192 | if (hi != 0) |
| 3193 | goto return1; |
| 3194 | if (mi >= 21) { |
| 3195 | if (mi > 21) |
| 3196 | goto return1; |
| 3197 | if ((sign == 0) && (lo > 47483647)) |
| 3198 | goto return1; |
| 3199 | if ((sign == 1) && (lo > 47483648)) |
| 3200 | goto return1; |
| 3201 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3202 | } else if (type->builtInType == XML_SCHEMAS_SHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3203 | if ((mi != 0) || (hi != 0)) |
| 3204 | goto return1; |
| 3205 | if ((sign == 1) && (lo > 32768)) |
| 3206 | goto return1; |
| 3207 | if ((sign == 0) && (lo > 32767)) |
| 3208 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3209 | } else if (type->builtInType == XML_SCHEMAS_BYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3210 | if ((mi != 0) || (hi != 0)) |
| 3211 | goto return1; |
| 3212 | if ((sign == 1) && (lo > 128)) |
| 3213 | goto return1; |
| 3214 | if ((sign == 0) && (lo > 127)) |
| 3215 | goto return1; |
| 3216 | } |
| 3217 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3218 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3219 | if (v != NULL) { |
| 3220 | v->value.decimal.lo = lo; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 3221 | v->value.decimal.mi = mi; |
| 3222 | v->value.decimal.hi = hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3223 | v->value.decimal.sign = sign; |
| 3224 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3225 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3226 | *val = v; |
| 3227 | } |
| 3228 | } |
| 3229 | goto return0; |
| 3230 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3231 | case XML_SCHEMAS_UINT: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3232 | case XML_SCHEMAS_ULONG: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3233 | case XML_SCHEMAS_USHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3234 | case XML_SCHEMAS_UBYTE:{ |
| 3235 | const xmlChar *cur = value; |
| 3236 | unsigned long lo, mi, hi; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3237 | |
| 3238 | if (cur == NULL) |
| 3239 | goto return1; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3240 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 3241 | if (ret < 0) |
| 3242 | goto return1; |
| 3243 | if (*cur != 0) |
| 3244 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3245 | if (type->builtInType == XML_SCHEMAS_ULONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3246 | if (hi >= 1844) { |
| 3247 | if (hi > 1844) |
| 3248 | goto return1; |
| 3249 | if (mi >= 67440737) { |
| 3250 | if (mi > 67440737) |
| 3251 | goto return1; |
| 3252 | if (lo > 9551615) |
| 3253 | goto return1; |
| 3254 | } |
| 3255 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3256 | } else if (type->builtInType == XML_SCHEMAS_UINT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3257 | if (hi != 0) |
| 3258 | goto return1; |
| 3259 | if (mi >= 42) { |
| 3260 | if (mi > 42) |
| 3261 | goto return1; |
| 3262 | if (lo > 94967295) |
| 3263 | goto return1; |
| 3264 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3265 | } else if (type->builtInType == XML_SCHEMAS_USHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3266 | if ((mi != 0) || (hi != 0)) |
| 3267 | goto return1; |
| 3268 | if (lo > 65535) |
| 3269 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3270 | } else if (type->builtInType == XML_SCHEMAS_UBYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3271 | if ((mi != 0) || (hi != 0)) |
| 3272 | goto return1; |
| 3273 | if (lo > 255) |
| 3274 | goto return1; |
| 3275 | } |
| 3276 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 3277 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3278 | if (v != NULL) { |
| 3279 | v->value.decimal.lo = lo; |
| 3280 | v->value.decimal.mi = mi; |
| 3281 | v->value.decimal.hi = hi; |
| 3282 | v->value.decimal.sign = 0; |
| 3283 | v->value.decimal.frac = 0; |
William M. Brack | ec3b4b7 | 2005-03-15 15:50:17 +0000 | [diff] [blame] | 3284 | v->value.decimal.total = ret; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3285 | *val = v; |
| 3286 | } |
| 3287 | } |
| 3288 | goto return0; |
| 3289 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3290 | } |
| 3291 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 3292 | done: |
| 3293 | if (norm != NULL) |
| 3294 | xmlFree(norm); |
| 3295 | return (ret); |
| 3296 | return3: |
| 3297 | if (norm != NULL) |
| 3298 | xmlFree(norm); |
| 3299 | return (3); |
| 3300 | return1: |
| 3301 | if (norm != NULL) |
| 3302 | xmlFree(norm); |
| 3303 | return (1); |
| 3304 | return0: |
| 3305 | if (norm != NULL) |
| 3306 | xmlFree(norm); |
| 3307 | return (0); |
| 3308 | error: |
| 3309 | if (norm != NULL) |
| 3310 | xmlFree(norm); |
| 3311 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3312 | } |
| 3313 | |
| 3314 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3315 | * xmlSchemaValPredefTypeNode: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3316 | * @type: the predefined type |
| 3317 | * @value: the value to check |
| 3318 | * @val: the return computed value |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3319 | * @node: the node containing the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3320 | * |
| 3321 | * Check that a value conforms to the lexical space of the predefined type. |
| 3322 | * if true a value is computed and returned in @val. |
| 3323 | * |
| 3324 | * Returns 0 if this validates, a positive error code number otherwise |
| 3325 | * and -1 in case of internal or API error. |
| 3326 | */ |
| 3327 | int |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3328 | xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 3329 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3330 | return(xmlSchemaValAtomicType(type, value, val, node, 0, |
| 3331 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 1, 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | /** |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3335 | * xmlSchemaValPredefTypeNodeNoNorm: |
| 3336 | * @type: the predefined type |
| 3337 | * @value: the value to check |
| 3338 | * @val: the return computed value |
| 3339 | * @node: the node containing the value |
| 3340 | * |
| 3341 | * Check that a value conforms to the lexical space of the predefined type. |
| 3342 | * if true a value is computed and returned in @val. |
| 3343 | * This one does apply any normalization to the value. |
| 3344 | * |
| 3345 | * Returns 0 if this validates, a positive error code number otherwise |
| 3346 | * and -1 in case of internal or API error. |
| 3347 | */ |
| 3348 | int |
| 3349 | xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value, |
| 3350 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3351 | return(xmlSchemaValAtomicType(type, value, val, node, 1, |
| 3352 | XML_SCHEMA_WHITESPACE_UNKNOWN, 1, 0, 1)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 3356 | * xmlSchemaValidatePredefinedType: |
| 3357 | * @type: the predefined type |
| 3358 | * @value: the value to check |
| 3359 | * @val: the return computed value |
| 3360 | * |
| 3361 | * Check that a value conforms to the lexical space of the predefined type. |
| 3362 | * if true a value is computed and returned in @val. |
| 3363 | * |
| 3364 | * Returns 0 if this validates, a positive error code number otherwise |
| 3365 | * and -1 in case of internal or API error. |
| 3366 | */ |
| 3367 | int |
| 3368 | xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value, |
| 3369 | xmlSchemaValPtr *val) { |
| 3370 | return(xmlSchemaValPredefTypeNode(type, value, val, NULL)); |
| 3371 | } |
| 3372 | |
| 3373 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3374 | * xmlSchemaCompareDecimals: |
| 3375 | * @x: a first decimal value |
| 3376 | * @y: a second decimal value |
| 3377 | * |
| 3378 | * Compare 2 decimals |
| 3379 | * |
| 3380 | * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error |
| 3381 | */ |
| 3382 | static int |
| 3383 | xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3384 | { |
| 3385 | xmlSchemaValPtr swp; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3386 | int order = 1, integx, integy, dlen; |
| 3387 | unsigned long hi, mi, lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3388 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3389 | /* |
| 3390 | * First test: If x is -ve and not zero |
| 3391 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3392 | if ((x->value.decimal.sign) && |
| 3393 | ((x->value.decimal.lo != 0) || |
| 3394 | (x->value.decimal.mi != 0) || |
| 3395 | (x->value.decimal.hi != 0))) { |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3396 | /* |
| 3397 | * Then if y is -ve and not zero reverse the compare |
| 3398 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3399 | if ((y->value.decimal.sign) && |
| 3400 | ((y->value.decimal.lo != 0) || |
| 3401 | (y->value.decimal.mi != 0) || |
| 3402 | (y->value.decimal.hi != 0))) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3403 | order = -1; |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3404 | /* |
| 3405 | * Otherwise (y >= 0) we have the answer |
| 3406 | */ |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3407 | else |
| 3408 | return (-1); |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3409 | /* |
| 3410 | * If x is not -ve and y is -ve we have the answer |
| 3411 | */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 3412 | } else if ((y->value.decimal.sign) && |
| 3413 | ((y->value.decimal.lo != 0) || |
| 3414 | (y->value.decimal.mi != 0) || |
| 3415 | (y->value.decimal.hi != 0))) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3416 | return (1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3417 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3418 | /* |
| 3419 | * If it's not simply determined by a difference in sign, |
| 3420 | * then we need to compare the actual values of the two nums. |
| 3421 | * To do this, we start by looking at the integral parts. |
| 3422 | * If the number of integral digits differ, then we have our |
| 3423 | * answer. |
| 3424 | */ |
| 3425 | integx = x->value.decimal.total - x->value.decimal.frac; |
| 3426 | integy = y->value.decimal.total - y->value.decimal.frac; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 3427 | /* |
| 3428 | * NOTE: We changed the "total" for values like "0.1" |
| 3429 | * (or "-0.1" or ".1") to be 1, which was 2 previously. |
| 3430 | * Therefore the special case, when such values are |
| 3431 | * compared with 0, needs to be handled separately; |
| 3432 | * otherwise a zero would be recognized incorrectly as |
| 3433 | * greater than those values. This has the nice side effect |
| 3434 | * that we gain an overall optimized comparison with zeroes. |
| 3435 | * Note that a "0" has a "total" of 1 already. |
| 3436 | */ |
| 3437 | if (integx == 1) { |
| 3438 | if (x->value.decimal.lo == 0) { |
| 3439 | if (integy != 1) |
| 3440 | return -order; |
| 3441 | else if (y->value.decimal.lo != 0) |
| 3442 | return -order; |
| 3443 | else |
| 3444 | return(0); |
| 3445 | } |
| 3446 | } |
| 3447 | if (integy == 1) { |
| 3448 | if (y->value.decimal.lo == 0) { |
| 3449 | if (integx != 1) |
| 3450 | return order; |
| 3451 | else if (x->value.decimal.lo != 0) |
| 3452 | return order; |
| 3453 | else |
| 3454 | return(0); |
| 3455 | } |
| 3456 | } |
| 3457 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3458 | if (integx > integy) |
| 3459 | return order; |
| 3460 | else if (integy > integx) |
| 3461 | return -order; |
Kasimier T. Buchcik | 72f50c1 | 2005-11-28 16:36:30 +0000 | [diff] [blame] | 3462 | |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3463 | /* |
| 3464 | * If the number of integral digits is the same for both numbers, |
| 3465 | * then things get a little more complicated. We need to "normalize" |
| 3466 | * the numbers in order to properly compare them. To do this, we |
| 3467 | * look at the total length of each number (length => number of |
| 3468 | * significant digits), and divide the "shorter" by 10 (decreasing |
| 3469 | * the length) until they are of equal length. |
| 3470 | */ |
| 3471 | dlen = x->value.decimal.total - y->value.decimal.total; |
| 3472 | if (dlen < 0) { /* y has more digits than x */ |
| 3473 | swp = x; |
| 3474 | hi = y->value.decimal.hi; |
| 3475 | mi = y->value.decimal.mi; |
| 3476 | lo = y->value.decimal.lo; |
| 3477 | dlen = -dlen; |
| 3478 | order = -order; |
| 3479 | } else { /* x has more digits than y */ |
| 3480 | swp = y; |
| 3481 | hi = x->value.decimal.hi; |
| 3482 | mi = x->value.decimal.mi; |
| 3483 | lo = x->value.decimal.lo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3484 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3485 | while (dlen > 8) { /* in effect, right shift by 10**8 */ |
| 3486 | lo = mi; |
| 3487 | mi = hi; |
| 3488 | hi = 0; |
| 3489 | dlen -= 8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3490 | } |
William M. Brack | 273670f | 2005-03-11 15:55:14 +0000 | [diff] [blame] | 3491 | while (dlen > 0) { |
| 3492 | unsigned long rem1, rem2; |
| 3493 | rem1 = (hi % 10) * 100000000L; |
| 3494 | hi = hi / 10; |
| 3495 | rem2 = (mi % 10) * 100000000L; |
| 3496 | mi = (mi + rem1) / 10; |
| 3497 | lo = (lo + rem2) / 10; |
| 3498 | dlen--; |
| 3499 | } |
| 3500 | if (hi > swp->value.decimal.hi) { |
| 3501 | return order; |
| 3502 | } else if (hi == swp->value.decimal.hi) { |
| 3503 | if (mi > swp->value.decimal.mi) { |
| 3504 | return order; |
| 3505 | } else if (mi == swp->value.decimal.mi) { |
| 3506 | if (lo > swp->value.decimal.lo) { |
| 3507 | return order; |
| 3508 | } else if (lo == swp->value.decimal.lo) { |
| 3509 | if (x->value.decimal.total == y->value.decimal.total) { |
| 3510 | return 0; |
| 3511 | } else { |
| 3512 | return order; |
| 3513 | } |
| 3514 | } |
| 3515 | } |
| 3516 | } |
| 3517 | return -order; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | /** |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3521 | * xmlSchemaCompareDurations: |
| 3522 | * @x: a first duration value |
| 3523 | * @y: a second duration value |
| 3524 | * |
| 3525 | * Compare 2 durations |
| 3526 | * |
| 3527 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3528 | * case of error |
| 3529 | */ |
| 3530 | static int |
| 3531 | xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3532 | { |
| 3533 | long carry, mon, day; |
| 3534 | double sec; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3535 | int invert = 1; |
| 3536 | long xmon, xday, myear, minday, maxday; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3537 | static const long dayRange [2][12] = { |
| 3538 | { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, }, |
| 3539 | { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} }; |
| 3540 | |
| 3541 | if ((x == NULL) || (y == NULL)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3542 | return -2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3543 | |
| 3544 | /* months */ |
| 3545 | mon = x->value.dur.mon - y->value.dur.mon; |
| 3546 | |
| 3547 | /* seconds */ |
| 3548 | sec = x->value.dur.sec - y->value.dur.sec; |
| 3549 | carry = (long)sec / SECS_PER_DAY; |
| 3550 | sec -= (double)(carry * SECS_PER_DAY); |
| 3551 | |
| 3552 | /* days */ |
| 3553 | day = x->value.dur.day - y->value.dur.day + carry; |
| 3554 | |
| 3555 | /* easy test */ |
| 3556 | if (mon == 0) { |
| 3557 | if (day == 0) |
| 3558 | if (sec == 0.0) |
| 3559 | return 0; |
| 3560 | else if (sec < 0.0) |
| 3561 | return -1; |
| 3562 | else |
| 3563 | return 1; |
| 3564 | else if (day < 0) |
| 3565 | return -1; |
| 3566 | else |
| 3567 | return 1; |
| 3568 | } |
| 3569 | |
| 3570 | if (mon > 0) { |
| 3571 | if ((day >= 0) && (sec >= 0.0)) |
| 3572 | return 1; |
| 3573 | else { |
| 3574 | xmon = mon; |
| 3575 | xday = -day; |
| 3576 | } |
| 3577 | } else if ((day <= 0) && (sec <= 0.0)) { |
| 3578 | return -1; |
| 3579 | } else { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3580 | invert = -1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3581 | xmon = -mon; |
| 3582 | xday = day; |
| 3583 | } |
| 3584 | |
| 3585 | myear = xmon / 12; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3586 | if (myear == 0) { |
| 3587 | minday = 0; |
| 3588 | maxday = 0; |
| 3589 | } else { |
| 3590 | maxday = 366 * ((myear + 3) / 4) + |
| 3591 | 365 * ((myear - 1) % 4); |
| 3592 | minday = maxday - 1; |
| 3593 | } |
| 3594 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3595 | xmon = xmon % 12; |
| 3596 | minday += dayRange[0][xmon]; |
| 3597 | maxday += dayRange[1][xmon]; |
| 3598 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3599 | if ((maxday == minday) && (maxday == xday)) |
| 3600 | return(0); /* can this really happen ? */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3601 | if (maxday < xday) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3602 | return(-invert); |
| 3603 | if (minday > xday) |
| 3604 | return(invert); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3607 | return 2; |
| 3608 | } |
| 3609 | |
| 3610 | /* |
| 3611 | * macros for adding date/times and durations |
| 3612 | */ |
| 3613 | #define FQUOTIENT(a,b) (floor(((double)a/(double)b))) |
| 3614 | #define MODULO(a,b) (a - FQUOTIENT(a,b) * b) |
| 3615 | #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low))) |
| 3616 | #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low) |
| 3617 | |
| 3618 | /** |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3619 | * xmlSchemaDupVal: |
| 3620 | * @v: the #xmlSchemaValPtr value to duplicate |
| 3621 | * |
| 3622 | * Makes a copy of @v. The calling program is responsible for freeing |
| 3623 | * the returned value. |
| 3624 | * |
| 3625 | * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error. |
| 3626 | */ |
| 3627 | static xmlSchemaValPtr |
| 3628 | xmlSchemaDupVal (xmlSchemaValPtr v) |
| 3629 | { |
| 3630 | xmlSchemaValPtr ret = xmlSchemaNewValue(v->type); |
| 3631 | if (ret == NULL) |
| 3632 | return NULL; |
| 3633 | |
| 3634 | memcpy(ret, v, sizeof(xmlSchemaVal)); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3635 | ret->next = NULL; |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3636 | return ret; |
| 3637 | } |
| 3638 | |
| 3639 | /** |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3640 | * xmlSchemaCopyValue: |
| 3641 | * @val: the precomputed value to be copied |
| 3642 | * |
| 3643 | * Copies the precomputed value. This duplicates any string within. |
| 3644 | * |
| 3645 | * Returns the copy or NULL if a copy for a data-type is not implemented. |
| 3646 | */ |
| 3647 | xmlSchemaValPtr |
| 3648 | xmlSchemaCopyValue(xmlSchemaValPtr val) |
| 3649 | { |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3650 | xmlSchemaValPtr ret = NULL, prev = NULL, cur; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3651 | |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3652 | /* |
| 3653 | * Copy the string values. |
| 3654 | */ |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3655 | while (val != NULL) { |
| 3656 | switch (val->type) { |
| 3657 | case XML_SCHEMAS_ANYTYPE: |
| 3658 | case XML_SCHEMAS_IDREFS: |
| 3659 | case XML_SCHEMAS_ENTITIES: |
| 3660 | case XML_SCHEMAS_NMTOKENS: |
| 3661 | xmlSchemaFreeValue(ret); |
| 3662 | return (NULL); |
| 3663 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 3664 | case XML_SCHEMAS_STRING: |
| 3665 | case XML_SCHEMAS_NORMSTRING: |
| 3666 | case XML_SCHEMAS_TOKEN: |
| 3667 | case XML_SCHEMAS_LANGUAGE: |
| 3668 | case XML_SCHEMAS_NAME: |
| 3669 | case XML_SCHEMAS_NCNAME: |
| 3670 | case XML_SCHEMAS_ID: |
| 3671 | case XML_SCHEMAS_IDREF: |
| 3672 | case XML_SCHEMAS_ENTITY: |
| 3673 | case XML_SCHEMAS_NMTOKEN: |
| 3674 | case XML_SCHEMAS_ANYURI: |
| 3675 | cur = xmlSchemaDupVal(val); |
| 3676 | if (val->value.str != NULL) |
| 3677 | cur->value.str = xmlStrdup(BAD_CAST val->value.str); |
| 3678 | break; |
| 3679 | case XML_SCHEMAS_QNAME: |
| 3680 | case XML_SCHEMAS_NOTATION: |
| 3681 | cur = xmlSchemaDupVal(val); |
| 3682 | if (val->value.qname.name != NULL) |
| 3683 | cur->value.qname.name = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3684 | xmlStrdup(BAD_CAST val->value.qname.name); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3685 | if (val->value.qname.uri != NULL) |
| 3686 | cur->value.qname.uri = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3687 | xmlStrdup(BAD_CAST val->value.qname.uri); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3688 | break; |
| 3689 | case XML_SCHEMAS_HEXBINARY: |
| 3690 | cur = xmlSchemaDupVal(val); |
| 3691 | if (val->value.hex.str != NULL) |
| 3692 | cur->value.hex.str = xmlStrdup(BAD_CAST val->value.hex.str); |
| 3693 | break; |
| 3694 | case XML_SCHEMAS_BASE64BINARY: |
| 3695 | cur = xmlSchemaDupVal(val); |
| 3696 | if (val->value.base64.str != NULL) |
| 3697 | cur->value.base64.str = |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3698 | xmlStrdup(BAD_CAST val->value.base64.str); |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3699 | break; |
| 3700 | default: |
| 3701 | cur = xmlSchemaDupVal(val); |
| 3702 | break; |
| 3703 | } |
| 3704 | if (ret == NULL) |
| 3705 | ret = cur; |
| 3706 | else |
| 3707 | prev->next = cur; |
| 3708 | prev = cur; |
| 3709 | val = val->next; |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3710 | } |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 3711 | return (ret); |
Kasimier T. Buchcik | 285ebab | 2005-03-04 18:04:59 +0000 | [diff] [blame] | 3712 | } |
| 3713 | |
| 3714 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3715 | * _xmlSchemaDateAdd: |
| 3716 | * @dt: an #xmlSchemaValPtr |
| 3717 | * @dur: an #xmlSchemaValPtr of type #XS_DURATION |
| 3718 | * |
| 3719 | * Compute a new date/time from @dt and @dur. This function assumes @dt |
| 3720 | * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH, |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3721 | * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as |
| 3722 | * @dt. The calling program is responsible for freeing the returned value. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3723 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3724 | * Returns a pointer to a new #xmlSchemaVal or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3725 | */ |
| 3726 | static xmlSchemaValPtr |
| 3727 | _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur) |
| 3728 | { |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3729 | xmlSchemaValPtr ret, tmp; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3730 | long carry, tempdays, temp; |
| 3731 | xmlSchemaValDatePtr r, d; |
| 3732 | xmlSchemaValDurationPtr u; |
| 3733 | |
| 3734 | if ((dt == NULL) || (dur == NULL)) |
| 3735 | return NULL; |
| 3736 | |
| 3737 | ret = xmlSchemaNewValue(dt->type); |
| 3738 | if (ret == NULL) |
| 3739 | return NULL; |
| 3740 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3741 | /* make a copy so we don't alter the original value */ |
| 3742 | tmp = xmlSchemaDupVal(dt); |
| 3743 | if (tmp == NULL) { |
| 3744 | xmlSchemaFreeValue(ret); |
| 3745 | return NULL; |
| 3746 | } |
| 3747 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3748 | r = &(ret->value.date); |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3749 | d = &(tmp->value.date); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3750 | u = &(dur->value.dur); |
| 3751 | |
| 3752 | /* normalization */ |
| 3753 | if (d->mon == 0) |
| 3754 | d->mon = 1; |
| 3755 | |
| 3756 | /* normalize for time zone offset */ |
| 3757 | u->sec -= (d->tzo * 60); |
| 3758 | d->tzo = 0; |
| 3759 | |
| 3760 | /* normalization */ |
| 3761 | if (d->day == 0) |
| 3762 | d->day = 1; |
| 3763 | |
| 3764 | /* month */ |
| 3765 | carry = d->mon + u->mon; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3766 | r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13); |
| 3767 | carry = (long) FQUOTIENT_RANGE(carry, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3768 | |
| 3769 | /* year (may be modified later) */ |
| 3770 | r->year = d->year + carry; |
| 3771 | if (r->year == 0) { |
| 3772 | if (d->year > 0) |
| 3773 | r->year--; |
| 3774 | else |
| 3775 | r->year++; |
| 3776 | } |
| 3777 | |
| 3778 | /* time zone */ |
| 3779 | r->tzo = d->tzo; |
| 3780 | r->tz_flag = d->tz_flag; |
| 3781 | |
| 3782 | /* seconds */ |
| 3783 | r->sec = d->sec + u->sec; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3784 | carry = (long) FQUOTIENT((long)r->sec, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3785 | if (r->sec != 0.0) { |
| 3786 | r->sec = MODULO(r->sec, 60.0); |
| 3787 | } |
| 3788 | |
| 3789 | /* minute */ |
| 3790 | carry += d->min; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3791 | r->min = (unsigned int) MODULO(carry, 60); |
| 3792 | carry = (long) FQUOTIENT(carry, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3793 | |
| 3794 | /* hours */ |
| 3795 | carry += d->hour; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3796 | r->hour = (unsigned int) MODULO(carry, 24); |
| 3797 | carry = (long)FQUOTIENT(carry, 24); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3798 | |
| 3799 | /* |
| 3800 | * days |
| 3801 | * Note we use tempdays because the temporary values may need more |
| 3802 | * than 5 bits |
| 3803 | */ |
| 3804 | if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) && |
| 3805 | (d->day > MAX_DAYINMONTH(r->year, r->mon))) |
| 3806 | tempdays = MAX_DAYINMONTH(r->year, r->mon); |
| 3807 | else if (d->day < 1) |
| 3808 | tempdays = 1; |
| 3809 | else |
| 3810 | tempdays = d->day; |
| 3811 | |
| 3812 | tempdays += u->day + carry; |
| 3813 | |
| 3814 | while (1) { |
| 3815 | if (tempdays < 1) { |
Daniel Veillard | 5e9576a | 2005-11-21 11:23:47 +0000 | [diff] [blame] | 3816 | long tmon = (long) MODULO_RANGE((int)r->mon-1, 1, 13); |
| 3817 | long tyr = r->year + (long)FQUOTIENT_RANGE((int)r->mon-1, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3818 | if (tyr == 0) |
| 3819 | tyr--; |
| 3820 | tempdays += MAX_DAYINMONTH(tyr, tmon); |
| 3821 | carry = -1; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3822 | } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3823 | tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon); |
| 3824 | carry = 1; |
| 3825 | } else |
| 3826 | break; |
| 3827 | |
| 3828 | temp = r->mon + carry; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3829 | r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13); |
| 3830 | r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3831 | if (r->year == 0) { |
| 3832 | if (temp < 1) |
| 3833 | r->year--; |
| 3834 | else |
| 3835 | r->year++; |
| 3836 | } |
| 3837 | } |
| 3838 | |
| 3839 | r->day = tempdays; |
| 3840 | |
| 3841 | /* |
| 3842 | * adjust the date/time type to the date values |
| 3843 | */ |
| 3844 | if (ret->type != XML_SCHEMAS_DATETIME) { |
| 3845 | if ((r->hour) || (r->min) || (r->sec)) |
| 3846 | ret->type = XML_SCHEMAS_DATETIME; |
| 3847 | else if (ret->type != XML_SCHEMAS_DATE) { |
| 3848 | if ((r->mon != 1) && (r->day != 1)) |
| 3849 | ret->type = XML_SCHEMAS_DATE; |
| 3850 | else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1)) |
| 3851 | ret->type = XML_SCHEMAS_GYEARMONTH; |
| 3852 | } |
| 3853 | } |
| 3854 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3855 | xmlSchemaFreeValue(tmp); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3856 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3857 | return ret; |
| 3858 | } |
| 3859 | |
| 3860 | /** |
| 3861 | * xmlSchemaDateNormalize: |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3862 | * @dt: an #xmlSchemaValPtr of a date/time type value. |
| 3863 | * @offset: number of seconds to adjust @dt by. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3864 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3865 | * Normalize @dt to GMT time. The @offset parameter is subtracted from |
| 3866 | * the return value is a time-zone offset is present on @dt. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3867 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3868 | * Returns a normalized copy of @dt or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3869 | */ |
| 3870 | static xmlSchemaValPtr |
| 3871 | xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset) |
| 3872 | { |
| 3873 | xmlSchemaValPtr dur, ret; |
| 3874 | |
| 3875 | if (dt == NULL) |
| 3876 | return NULL; |
| 3877 | |
| 3878 | if (((dt->type != XML_SCHEMAS_TIME) && |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 3879 | (dt->type != XML_SCHEMAS_DATETIME) && |
| 3880 | (dt->type != XML_SCHEMAS_DATE)) || (dt->value.date.tzo == 0)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3881 | return xmlSchemaDupVal(dt); |
| 3882 | |
| 3883 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 3884 | if (dur == NULL) |
| 3885 | return NULL; |
| 3886 | |
| 3887 | dur->value.date.sec -= offset; |
| 3888 | |
| 3889 | ret = _xmlSchemaDateAdd(dt, dur); |
| 3890 | if (ret == NULL) |
| 3891 | return NULL; |
| 3892 | |
| 3893 | xmlSchemaFreeValue(dur); |
| 3894 | |
| 3895 | /* ret->value.date.tzo = 0; */ |
| 3896 | return ret; |
| 3897 | } |
| 3898 | |
| 3899 | /** |
| 3900 | * _xmlSchemaDateCastYMToDays: |
| 3901 | * @dt: an #xmlSchemaValPtr |
| 3902 | * |
| 3903 | * Convert mon and year of @dt to total number of days. Take the |
| 3904 | * number of years since (or before) 1 AD and add the number of leap |
| 3905 | * years. This is a function because negative |
| 3906 | * years must be handled a little differently and there is no zero year. |
| 3907 | * |
| 3908 | * Returns number of days. |
| 3909 | */ |
| 3910 | static long |
| 3911 | _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt) |
| 3912 | { |
| 3913 | long ret; |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3914 | int mon; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3915 | |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3916 | mon = dt->value.date.mon; |
| 3917 | if (mon <= 0) mon = 1; /* normalization */ |
| 3918 | |
| 3919 | if (dt->value.date.year <= 0) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3920 | ret = (dt->value.date.year * 365) + |
| 3921 | (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+ |
| 3922 | ((dt->value.date.year+1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3923 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3924 | else |
| 3925 | ret = ((dt->value.date.year-1) * 365) + |
| 3926 | (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+ |
| 3927 | ((dt->value.date.year-1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3928 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3929 | |
| 3930 | return ret; |
| 3931 | } |
| 3932 | |
| 3933 | /** |
| 3934 | * TIME_TO_NUMBER: |
| 3935 | * @dt: an #xmlSchemaValPtr |
| 3936 | * |
| 3937 | * Calculates the number of seconds in the time portion of @dt. |
| 3938 | * |
| 3939 | * Returns seconds. |
| 3940 | */ |
| 3941 | #define TIME_TO_NUMBER(dt) \ |
| 3942 | ((double)((dt->value.date.hour * SECS_PER_HOUR) + \ |
Daniel Veillard | b3721c2 | 2003-03-31 11:22:25 +0000 | [diff] [blame] | 3943 | (dt->value.date.min * SECS_PER_MIN) + \ |
| 3944 | (dt->value.date.tzo * SECS_PER_MIN)) + \ |
| 3945 | dt->value.date.sec) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3946 | |
| 3947 | /** |
| 3948 | * xmlSchemaCompareDates: |
| 3949 | * @x: a first date/time value |
| 3950 | * @y: a second date/time value |
| 3951 | * |
| 3952 | * Compare 2 date/times |
| 3953 | * |
| 3954 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3955 | * case of error |
| 3956 | */ |
| 3957 | static int |
| 3958 | xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3959 | { |
| 3960 | unsigned char xmask, ymask, xor_mask, and_mask; |
| 3961 | xmlSchemaValPtr p1, p2, q1, q2; |
| 3962 | long p1d, p2d, q1d, q2d; |
| 3963 | |
| 3964 | if ((x == NULL) || (y == NULL)) |
| 3965 | return -2; |
| 3966 | |
| 3967 | if (x->value.date.tz_flag) { |
| 3968 | |
| 3969 | if (!y->value.date.tz_flag) { |
| 3970 | p1 = xmlSchemaDateNormalize(x, 0); |
| 3971 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3972 | /* normalize y + 14:00 */ |
| 3973 | q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR)); |
| 3974 | |
| 3975 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3976 | if (p1d < q1d) { |
| 3977 | xmlSchemaFreeValue(p1); |
| 3978 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3979 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3980 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3981 | double sec; |
| 3982 | |
| 3983 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3984 | if (sec < 0.0) { |
| 3985 | xmlSchemaFreeValue(p1); |
| 3986 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3987 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3988 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3989 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3990 | /* normalize y - 14:00 */ |
| 3991 | q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR)); |
| 3992 | q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day; |
| 3993 | if (p1d > q2d) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3994 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3995 | else if (p1d == q2d) { |
| 3996 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2); |
| 3997 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3998 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3999 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4000 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4001 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4002 | xmlSchemaFreeValue(p1); |
| 4003 | xmlSchemaFreeValue(q1); |
| 4004 | xmlSchemaFreeValue(q2); |
| 4005 | if (ret != 0) |
| 4006 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4007 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4008 | } else { |
| 4009 | xmlSchemaFreeValue(p1); |
| 4010 | xmlSchemaFreeValue(q1); |
| 4011 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4012 | } |
| 4013 | } else if (y->value.date.tz_flag) { |
| 4014 | q1 = xmlSchemaDateNormalize(y, 0); |
| 4015 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 4016 | |
| 4017 | /* normalize x - 14:00 */ |
| 4018 | p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR)); |
| 4019 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4020 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4021 | if (p1d < q1d) { |
| 4022 | xmlSchemaFreeValue(p1); |
| 4023 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4024 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4025 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4026 | double sec; |
| 4027 | |
| 4028 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4029 | if (sec < 0.0) { |
| 4030 | xmlSchemaFreeValue(p1); |
| 4031 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4032 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4033 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4034 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4035 | /* normalize x + 14:00 */ |
| 4036 | p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR)); |
| 4037 | p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day; |
| 4038 | |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4039 | if (p2d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4040 | ret = 1; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4041 | } else if (p2d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4042 | sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1); |
| 4043 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4044 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4045 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4046 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4047 | } |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 4048 | xmlSchemaFreeValue(p1); |
| 4049 | xmlSchemaFreeValue(q1); |
| 4050 | xmlSchemaFreeValue(p2); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4051 | if (ret != 0) |
| 4052 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4053 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4054 | } else { |
| 4055 | xmlSchemaFreeValue(p1); |
| 4056 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4057 | } |
| 4058 | } |
| 4059 | |
| 4060 | /* |
| 4061 | * if the same type then calculate the difference |
| 4062 | */ |
| 4063 | if (x->type == y->type) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4064 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4065 | q1 = xmlSchemaDateNormalize(y, 0); |
| 4066 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 4067 | |
| 4068 | p1 = xmlSchemaDateNormalize(x, 0); |
| 4069 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 4070 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4071 | if (p1d < q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4072 | ret = -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4073 | } else if (p1d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4074 | ret = 1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 4075 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4076 | double sec; |
| 4077 | |
| 4078 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
| 4079 | if (sec < 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4080 | ret = -1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4081 | else if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4082 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4083 | |
| 4084 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 4085 | xmlSchemaFreeValue(p1); |
| 4086 | xmlSchemaFreeValue(q1); |
| 4087 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4088 | } |
| 4089 | |
| 4090 | switch (x->type) { |
| 4091 | case XML_SCHEMAS_DATETIME: |
| 4092 | xmask = 0xf; |
| 4093 | break; |
| 4094 | case XML_SCHEMAS_DATE: |
| 4095 | xmask = 0x7; |
| 4096 | break; |
| 4097 | case XML_SCHEMAS_GYEAR: |
| 4098 | xmask = 0x1; |
| 4099 | break; |
| 4100 | case XML_SCHEMAS_GMONTH: |
| 4101 | xmask = 0x2; |
| 4102 | break; |
| 4103 | case XML_SCHEMAS_GDAY: |
| 4104 | xmask = 0x3; |
| 4105 | break; |
| 4106 | case XML_SCHEMAS_GYEARMONTH: |
| 4107 | xmask = 0x3; |
| 4108 | break; |
| 4109 | case XML_SCHEMAS_GMONTHDAY: |
| 4110 | xmask = 0x6; |
| 4111 | break; |
| 4112 | case XML_SCHEMAS_TIME: |
| 4113 | xmask = 0x8; |
| 4114 | break; |
| 4115 | default: |
| 4116 | xmask = 0; |
| 4117 | break; |
| 4118 | } |
| 4119 | |
| 4120 | switch (y->type) { |
| 4121 | case XML_SCHEMAS_DATETIME: |
| 4122 | ymask = 0xf; |
| 4123 | break; |
| 4124 | case XML_SCHEMAS_DATE: |
| 4125 | ymask = 0x7; |
| 4126 | break; |
| 4127 | case XML_SCHEMAS_GYEAR: |
| 4128 | ymask = 0x1; |
| 4129 | break; |
| 4130 | case XML_SCHEMAS_GMONTH: |
| 4131 | ymask = 0x2; |
| 4132 | break; |
| 4133 | case XML_SCHEMAS_GDAY: |
| 4134 | ymask = 0x3; |
| 4135 | break; |
| 4136 | case XML_SCHEMAS_GYEARMONTH: |
| 4137 | ymask = 0x3; |
| 4138 | break; |
| 4139 | case XML_SCHEMAS_GMONTHDAY: |
| 4140 | ymask = 0x6; |
| 4141 | break; |
| 4142 | case XML_SCHEMAS_TIME: |
| 4143 | ymask = 0x8; |
| 4144 | break; |
| 4145 | default: |
| 4146 | ymask = 0; |
| 4147 | break; |
| 4148 | } |
| 4149 | |
| 4150 | xor_mask = xmask ^ ymask; /* mark type differences */ |
| 4151 | and_mask = xmask & ymask; /* mark field specification */ |
| 4152 | |
| 4153 | /* year */ |
| 4154 | if (xor_mask & 1) |
| 4155 | return 2; /* indeterminate */ |
| 4156 | else if (and_mask & 1) { |
| 4157 | if (x->value.date.year < y->value.date.year) |
| 4158 | return -1; |
| 4159 | else if (x->value.date.year > y->value.date.year) |
| 4160 | return 1; |
| 4161 | } |
| 4162 | |
| 4163 | /* month */ |
| 4164 | if (xor_mask & 2) |
| 4165 | return 2; /* indeterminate */ |
| 4166 | else if (and_mask & 2) { |
| 4167 | if (x->value.date.mon < y->value.date.mon) |
| 4168 | return -1; |
| 4169 | else if (x->value.date.mon > y->value.date.mon) |
| 4170 | return 1; |
| 4171 | } |
| 4172 | |
| 4173 | /* day */ |
| 4174 | if (xor_mask & 4) |
| 4175 | return 2; /* indeterminate */ |
| 4176 | else if (and_mask & 4) { |
| 4177 | if (x->value.date.day < y->value.date.day) |
| 4178 | return -1; |
| 4179 | else if (x->value.date.day > y->value.date.day) |
| 4180 | return 1; |
| 4181 | } |
| 4182 | |
| 4183 | /* time */ |
| 4184 | if (xor_mask & 8) |
| 4185 | return 2; /* indeterminate */ |
| 4186 | else if (and_mask & 8) { |
| 4187 | if (x->value.date.hour < y->value.date.hour) |
| 4188 | return -1; |
| 4189 | else if (x->value.date.hour > y->value.date.hour) |
| 4190 | return 1; |
| 4191 | else if (x->value.date.min < y->value.date.min) |
| 4192 | return -1; |
| 4193 | else if (x->value.date.min > y->value.date.min) |
| 4194 | return 1; |
| 4195 | else if (x->value.date.sec < y->value.date.sec) |
| 4196 | return -1; |
| 4197 | else if (x->value.date.sec > y->value.date.sec) |
| 4198 | return 1; |
| 4199 | } |
| 4200 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4201 | return 0; |
| 4202 | } |
| 4203 | |
| 4204 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4205 | * xmlSchemaComparePreserveReplaceStrings: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4206 | * @x: a first string value |
| 4207 | * @y: a second string value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4208 | * @invert: inverts the result if x < y or x > y. |
| 4209 | * |
| 4210 | * Compare 2 string for their normalized values. |
| 4211 | * @x is a string with whitespace of "preserve", @y is |
| 4212 | * a string with a whitespace of "replace". I.e. @x could |
| 4213 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4214 | * |
| 4215 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4216 | * case of error |
| 4217 | */ |
| 4218 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4219 | xmlSchemaComparePreserveReplaceStrings(const xmlChar *x, |
| 4220 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4221 | int invert) |
| 4222 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4223 | int tmp; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4224 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4225 | while ((*x != 0) && (*y != 0)) { |
| 4226 | if (IS_WSP_REPLACE_CH(*y)) { |
| 4227 | if (! IS_WSP_SPACE_CH(*x)) { |
| 4228 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4229 | if (invert) |
| 4230 | return(1); |
| 4231 | else |
| 4232 | return(-1); |
| 4233 | } else { |
| 4234 | if (invert) |
| 4235 | return(-1); |
| 4236 | else |
| 4237 | return(1); |
| 4238 | } |
| 4239 | } |
| 4240 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4241 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4242 | if (tmp < 0) { |
| 4243 | if (invert) |
| 4244 | return(1); |
| 4245 | else |
| 4246 | return(-1); |
| 4247 | } |
| 4248 | if (tmp > 0) { |
| 4249 | if (invert) |
| 4250 | return(-1); |
| 4251 | else |
| 4252 | return(1); |
| 4253 | } |
| 4254 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4255 | x++; |
| 4256 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4257 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4258 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4259 | if (invert) |
| 4260 | return(-1); |
| 4261 | else |
| 4262 | return(1); |
| 4263 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4264 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4265 | if (invert) |
| 4266 | return(1); |
| 4267 | else |
| 4268 | return(-1); |
| 4269 | } |
| 4270 | return(0); |
| 4271 | } |
| 4272 | |
| 4273 | /** |
| 4274 | * xmlSchemaComparePreserveCollapseStrings: |
| 4275 | * @x: a first string value |
| 4276 | * @y: a second string value |
| 4277 | * |
| 4278 | * Compare 2 string for their normalized values. |
| 4279 | * @x is a string with whitespace of "preserve", @y is |
| 4280 | * a string with a whitespace of "collapse". I.e. @x could |
| 4281 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4282 | * |
| 4283 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4284 | * case of error |
| 4285 | */ |
| 4286 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4287 | xmlSchemaComparePreserveCollapseStrings(const xmlChar *x, |
| 4288 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4289 | int invert) |
| 4290 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4291 | int tmp; |
| 4292 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4293 | /* |
| 4294 | * Skip leading 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++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4298 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4299 | while ((*x != 0) && (*y != 0)) { |
| 4300 | if IS_WSP_BLANK_CH(*y) { |
| 4301 | if (! IS_WSP_SPACE_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4302 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4303 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4304 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4305 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4306 | if (invert) |
| 4307 | return(1); |
| 4308 | else |
| 4309 | return(-1); |
| 4310 | } else { |
| 4311 | if (invert) |
| 4312 | return(-1); |
| 4313 | else |
| 4314 | return(1); |
| 4315 | } |
| 4316 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4317 | x++; |
| 4318 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4319 | /* |
| 4320 | * Skip contiguous blank chars of the collapsed string. |
| 4321 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4322 | while IS_WSP_BLANK_CH(*y) |
| 4323 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4324 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4325 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4326 | if (tmp < 0) { |
| 4327 | if (invert) |
| 4328 | return(1); |
| 4329 | else |
| 4330 | return(-1); |
| 4331 | } |
| 4332 | if (tmp > 0) { |
| 4333 | if (invert) |
| 4334 | return(-1); |
| 4335 | else |
| 4336 | return(1); |
| 4337 | } |
| 4338 | } |
| 4339 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4340 | if (*x != 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 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4346 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4347 | /* |
| 4348 | * Skip trailing blank chars of the collapsed string. |
| 4349 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4350 | while IS_WSP_BLANK_CH(*y) |
| 4351 | y++; |
| 4352 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4353 | if (invert) |
| 4354 | return(1); |
| 4355 | else |
| 4356 | return(-1); |
| 4357 | } |
| 4358 | } |
| 4359 | return(0); |
| 4360 | } |
| 4361 | |
| 4362 | /** |
| 4363 | * xmlSchemaComparePreserveCollapseStrings: |
| 4364 | * @x: a first string value |
| 4365 | * @y: a second string value |
| 4366 | * |
| 4367 | * Compare 2 string for their normalized values. |
| 4368 | * @x is a string with whitespace of "preserve", @y is |
| 4369 | * a string with a whitespace of "collapse". I.e. @x could |
| 4370 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 4371 | * |
| 4372 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4373 | * case of error |
| 4374 | */ |
| 4375 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4376 | xmlSchemaCompareReplaceCollapseStrings(const xmlChar *x, |
| 4377 | const xmlChar *y, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4378 | int invert) |
| 4379 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4380 | int tmp; |
| 4381 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4382 | /* |
| 4383 | * Skip leading blank chars of the collapsed string. |
| 4384 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4385 | while IS_WSP_BLANK_CH(*y) |
| 4386 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4387 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4388 | while ((*x != 0) && (*y != 0)) { |
| 4389 | if IS_WSP_BLANK_CH(*y) { |
| 4390 | if (! IS_WSP_BLANK_CH(*x)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4391 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4392 | * The yv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4393 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4394 | if ((*x - 0x20) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4395 | if (invert) |
| 4396 | return(1); |
| 4397 | else |
| 4398 | return(-1); |
| 4399 | } else { |
| 4400 | if (invert) |
| 4401 | return(-1); |
| 4402 | else |
| 4403 | return(1); |
| 4404 | } |
| 4405 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4406 | x++; |
| 4407 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4408 | /* |
| 4409 | * Skip contiguous blank chars of the collapsed string. |
| 4410 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4411 | while IS_WSP_BLANK_CH(*y) |
| 4412 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4413 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4414 | if IS_WSP_BLANK_CH(*x) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4415 | /* |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4416 | * The xv character would have been replaced to 0x20. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4417 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4418 | if ((0x20 - *y) < 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4419 | if (invert) |
| 4420 | return(1); |
| 4421 | else |
| 4422 | return(-1); |
| 4423 | } else { |
| 4424 | if (invert) |
| 4425 | return(-1); |
| 4426 | else |
| 4427 | return(1); |
| 4428 | } |
| 4429 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4430 | tmp = *x++ - *y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4431 | if (tmp < 0) |
| 4432 | return(-1); |
| 4433 | if (tmp > 0) |
| 4434 | return(1); |
| 4435 | } |
| 4436 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4437 | if (*x != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4438 | if (invert) |
| 4439 | return(-1); |
| 4440 | else |
| 4441 | return(1); |
| 4442 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4443 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4444 | /* |
| 4445 | * Skip trailing blank chars of the collapsed string. |
| 4446 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4447 | while IS_WSP_BLANK_CH(*y) |
| 4448 | y++; |
| 4449 | if (*y != 0) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4450 | if (invert) |
| 4451 | return(1); |
| 4452 | else |
| 4453 | return(-1); |
| 4454 | } |
| 4455 | } |
| 4456 | return(0); |
| 4457 | } |
| 4458 | |
| 4459 | |
| 4460 | /** |
| 4461 | * xmlSchemaCompareReplacedStrings: |
| 4462 | * @x: a first string value |
| 4463 | * @y: a second string value |
| 4464 | * |
| 4465 | * Compare 2 string for their normalized values. |
| 4466 | * |
| 4467 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4468 | * case of error |
| 4469 | */ |
| 4470 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4471 | xmlSchemaCompareReplacedStrings(const xmlChar *x, |
| 4472 | const xmlChar *y) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4473 | { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4474 | int tmp; |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4475 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4476 | while ((*x != 0) && (*y != 0)) { |
| 4477 | if IS_WSP_BLANK_CH(*y) { |
| 4478 | if (! IS_WSP_BLANK_CH(*x)) { |
| 4479 | if ((*x - 0x20) < 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4480 | return(-1); |
| 4481 | else |
| 4482 | return(1); |
| 4483 | } |
| 4484 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4485 | if IS_WSP_BLANK_CH(*x) { |
| 4486 | if ((0x20 - *y) < 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4487 | return(-1); |
| 4488 | else |
| 4489 | return(1); |
| 4490 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4491 | tmp = *x - *y; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4492 | if (tmp < 0) |
| 4493 | return(-1); |
| 4494 | if (tmp > 0) |
| 4495 | return(1); |
| 4496 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4497 | x++; |
| 4498 | y++; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4499 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4500 | if (*x != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4501 | return(1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4502 | if (*y != 0) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4503 | return(-1); |
| 4504 | return(0); |
| 4505 | } |
| 4506 | |
| 4507 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4508 | * xmlSchemaCompareNormStrings: |
| 4509 | * @x: a first string value |
| 4510 | * @y: a second string value |
| 4511 | * |
| 4512 | * Compare 2 string for their normalized values. |
| 4513 | * |
| 4514 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 4515 | * case of error |
| 4516 | */ |
| 4517 | static int |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4518 | xmlSchemaCompareNormStrings(const xmlChar *x, |
| 4519 | const xmlChar *y) { |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4520 | int tmp; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4521 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4522 | while (IS_BLANK_CH(*x)) x++; |
| 4523 | while (IS_BLANK_CH(*y)) y++; |
| 4524 | while ((*x != 0) && (*y != 0)) { |
| 4525 | if (IS_BLANK_CH(*x)) { |
| 4526 | if (!IS_BLANK_CH(*y)) { |
| 4527 | tmp = *x - *y; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4528 | return(tmp); |
| 4529 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4530 | while (IS_BLANK_CH(*x)) x++; |
| 4531 | while (IS_BLANK_CH(*y)) y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4532 | } else { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4533 | tmp = *x++ - *y++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4534 | if (tmp < 0) |
| 4535 | return(-1); |
| 4536 | if (tmp > 0) |
| 4537 | return(1); |
| 4538 | } |
| 4539 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4540 | if (*x != 0) { |
| 4541 | while (IS_BLANK_CH(*x)) x++; |
| 4542 | if (*x != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4543 | return(1); |
| 4544 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4545 | if (*y != 0) { |
| 4546 | while (IS_BLANK_CH(*y)) y++; |
| 4547 | if (*y != 0) |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4548 | return(-1); |
| 4549 | } |
| 4550 | return(0); |
| 4551 | } |
| 4552 | |
| 4553 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4554 | * xmlSchemaCompareFloats: |
| 4555 | * @x: a first float or double value |
| 4556 | * @y: a second float or double value |
| 4557 | * |
| 4558 | * Compare 2 values |
| 4559 | * |
| 4560 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4561 | * case of error |
| 4562 | */ |
| 4563 | static int |
| 4564 | xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4565 | double d1, d2; |
| 4566 | |
| 4567 | if ((x == NULL) || (y == NULL)) |
| 4568 | return(-2); |
| 4569 | |
| 4570 | /* |
| 4571 | * Cast everything to doubles. |
| 4572 | */ |
| 4573 | if (x->type == XML_SCHEMAS_DOUBLE) |
| 4574 | d1 = x->value.d; |
| 4575 | else if (x->type == XML_SCHEMAS_FLOAT) |
| 4576 | d1 = x->value.f; |
| 4577 | else |
| 4578 | return(-2); |
| 4579 | |
| 4580 | if (y->type == XML_SCHEMAS_DOUBLE) |
| 4581 | d2 = y->value.d; |
| 4582 | else if (y->type == XML_SCHEMAS_FLOAT) |
| 4583 | d2 = y->value.f; |
| 4584 | else |
| 4585 | return(-2); |
| 4586 | |
| 4587 | /* |
| 4588 | * Check for special cases. |
| 4589 | */ |
| 4590 | if (xmlXPathIsNaN(d1)) { |
| 4591 | if (xmlXPathIsNaN(d2)) |
| 4592 | return(0); |
| 4593 | return(1); |
| 4594 | } |
| 4595 | if (xmlXPathIsNaN(d2)) |
| 4596 | return(-1); |
| 4597 | if (d1 == xmlXPathPINF) { |
| 4598 | if (d2 == xmlXPathPINF) |
| 4599 | return(0); |
| 4600 | return(1); |
| 4601 | } |
| 4602 | if (d2 == xmlXPathPINF) |
| 4603 | return(-1); |
| 4604 | if (d1 == xmlXPathNINF) { |
| 4605 | if (d2 == xmlXPathNINF) |
| 4606 | return(0); |
| 4607 | return(-1); |
| 4608 | } |
| 4609 | if (d2 == xmlXPathNINF) |
| 4610 | return(1); |
| 4611 | |
| 4612 | /* |
| 4613 | * basic tests, the last one we should have equality, but |
| 4614 | * portability is more important than speed and handling |
| 4615 | * NaN or Inf in a portable way is always a challenge, so ... |
| 4616 | */ |
| 4617 | if (d1 < d2) |
| 4618 | return(-1); |
| 4619 | if (d1 > d2) |
| 4620 | return(1); |
| 4621 | if (d1 == d2) |
| 4622 | return(0); |
| 4623 | return(2); |
| 4624 | } |
| 4625 | |
| 4626 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4627 | * xmlSchemaCompareValues: |
| 4628 | * @x: a first value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4629 | * @xvalue: the first value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4630 | * @xwtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4631 | * @y: a second value |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4632 | * @xvalue: the second value as a string (optional) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4633 | * @ywtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4634 | * |
| 4635 | * Compare 2 values |
| 4636 | * |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 4637 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not |
| 4638 | * comparable and -2 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4639 | */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4640 | static int |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4641 | xmlSchemaCompareValuesInternal(xmlSchemaValType xtype, |
| 4642 | xmlSchemaValPtr x, |
| 4643 | const xmlChar *xvalue, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4644 | xmlSchemaWhitespaceValueType xws, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4645 | xmlSchemaValType ytype, |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4646 | xmlSchemaValPtr y, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4647 | const xmlChar *yvalue, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4648 | xmlSchemaWhitespaceValueType yws) |
| 4649 | { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4650 | switch (xtype) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4651 | case XML_SCHEMAS_UNKNOWN: |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 4652 | case XML_SCHEMAS_ANYTYPE: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4653 | return(-2); |
| 4654 | case XML_SCHEMAS_INTEGER: |
| 4655 | case XML_SCHEMAS_NPINTEGER: |
| 4656 | case XML_SCHEMAS_NINTEGER: |
| 4657 | case XML_SCHEMAS_NNINTEGER: |
| 4658 | case XML_SCHEMAS_PINTEGER: |
| 4659 | case XML_SCHEMAS_INT: |
| 4660 | case XML_SCHEMAS_UINT: |
| 4661 | case XML_SCHEMAS_LONG: |
| 4662 | case XML_SCHEMAS_ULONG: |
| 4663 | case XML_SCHEMAS_SHORT: |
| 4664 | case XML_SCHEMAS_USHORT: |
| 4665 | case XML_SCHEMAS_BYTE: |
| 4666 | case XML_SCHEMAS_UBYTE: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4667 | case XML_SCHEMAS_DECIMAL: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4668 | if ((x == NULL) || (y == NULL)) |
| 4669 | return(-2); |
| 4670 | if (ytype == xtype) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4671 | return(xmlSchemaCompareDecimals(x, y)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4672 | if ((ytype == XML_SCHEMAS_DECIMAL) || |
| 4673 | (ytype == XML_SCHEMAS_INTEGER) || |
| 4674 | (ytype == XML_SCHEMAS_NPINTEGER) || |
| 4675 | (ytype == XML_SCHEMAS_NINTEGER) || |
| 4676 | (ytype == XML_SCHEMAS_NNINTEGER) || |
| 4677 | (ytype == XML_SCHEMAS_PINTEGER) || |
| 4678 | (ytype == XML_SCHEMAS_INT) || |
| 4679 | (ytype == XML_SCHEMAS_UINT) || |
| 4680 | (ytype == XML_SCHEMAS_LONG) || |
| 4681 | (ytype == XML_SCHEMAS_ULONG) || |
| 4682 | (ytype == XML_SCHEMAS_SHORT) || |
| 4683 | (ytype == XML_SCHEMAS_USHORT) || |
| 4684 | (ytype == XML_SCHEMAS_BYTE) || |
| 4685 | (ytype == XML_SCHEMAS_UBYTE)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4686 | return(xmlSchemaCompareDecimals(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4687 | return(-2); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4688 | case XML_SCHEMAS_DURATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4689 | if ((x == NULL) || (y == NULL)) |
| 4690 | return(-2); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 4691 | if (ytype == XML_SCHEMAS_DURATION) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4692 | return(xmlSchemaCompareDurations(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4693 | return(-2); |
| 4694 | case XML_SCHEMAS_TIME: |
| 4695 | case XML_SCHEMAS_GDAY: |
| 4696 | case XML_SCHEMAS_GMONTH: |
| 4697 | case XML_SCHEMAS_GMONTHDAY: |
| 4698 | case XML_SCHEMAS_GYEAR: |
| 4699 | case XML_SCHEMAS_GYEARMONTH: |
| 4700 | case XML_SCHEMAS_DATE: |
| 4701 | case XML_SCHEMAS_DATETIME: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4702 | if ((x == NULL) || (y == NULL)) |
| 4703 | return(-2); |
| 4704 | if ((ytype == XML_SCHEMAS_DATETIME) || |
| 4705 | (ytype == XML_SCHEMAS_TIME) || |
| 4706 | (ytype == XML_SCHEMAS_GDAY) || |
| 4707 | (ytype == XML_SCHEMAS_GMONTH) || |
| 4708 | (ytype == XML_SCHEMAS_GMONTHDAY) || |
| 4709 | (ytype == XML_SCHEMAS_GYEAR) || |
| 4710 | (ytype == XML_SCHEMAS_DATE) || |
| 4711 | (ytype == XML_SCHEMAS_GYEARMONTH)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4712 | return (xmlSchemaCompareDates(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4713 | return (-2); |
Kasimier T. Buchcik | 2ee855d | 2005-03-07 11:14:14 +0000 | [diff] [blame] | 4714 | /* |
| 4715 | * Note that we will support comparison of string types against |
| 4716 | * anySimpleType as well. |
| 4717 | */ |
| 4718 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4719 | case XML_SCHEMAS_STRING: |
| 4720 | case XML_SCHEMAS_NORMSTRING: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4721 | case XML_SCHEMAS_TOKEN: |
| 4722 | case XML_SCHEMAS_LANGUAGE: |
| 4723 | case XML_SCHEMAS_NMTOKEN: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4724 | case XML_SCHEMAS_NAME: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4725 | case XML_SCHEMAS_NCNAME: |
| 4726 | case XML_SCHEMAS_ID: |
| 4727 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4728 | case XML_SCHEMAS_ENTITY: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4729 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4730 | { |
| 4731 | const xmlChar *xv, *yv; |
| 4732 | |
| 4733 | if (x == NULL) |
| 4734 | xv = xvalue; |
| 4735 | else |
| 4736 | xv = x->value.str; |
| 4737 | if (y == NULL) |
| 4738 | yv = yvalue; |
| 4739 | else |
| 4740 | yv = y->value.str; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4741 | /* |
| 4742 | * TODO: Compare those against QName. |
| 4743 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4744 | if (ytype == XML_SCHEMAS_QNAME) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4745 | TODO |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4746 | if (y == NULL) |
| 4747 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4748 | return (-2); |
| 4749 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4750 | if ((ytype == XML_SCHEMAS_ANYSIMPLETYPE) || |
| 4751 | (ytype == XML_SCHEMAS_STRING) || |
| 4752 | (ytype == XML_SCHEMAS_NORMSTRING) || |
| 4753 | (ytype == XML_SCHEMAS_TOKEN) || |
| 4754 | (ytype == XML_SCHEMAS_LANGUAGE) || |
| 4755 | (ytype == XML_SCHEMAS_NMTOKEN) || |
| 4756 | (ytype == XML_SCHEMAS_NAME) || |
| 4757 | (ytype == XML_SCHEMAS_NCNAME) || |
| 4758 | (ytype == XML_SCHEMAS_ID) || |
| 4759 | (ytype == XML_SCHEMAS_IDREF) || |
| 4760 | (ytype == XML_SCHEMAS_ENTITY) || |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4761 | (ytype == XML_SCHEMAS_ANYURI)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4762 | |
| 4763 | if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4764 | |
| 4765 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4766 | /* TODO: What about x < y or x > y. */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4767 | if (xmlStrEqual(xv, yv)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4768 | return (0); |
| 4769 | else |
| 4770 | return (2); |
| 4771 | } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4772 | return (xmlSchemaComparePreserveReplaceStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4773 | else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4774 | return (xmlSchemaComparePreserveCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4775 | |
| 4776 | } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 4777 | |
| 4778 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4779 | return (xmlSchemaComparePreserveReplaceStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4780 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4781 | return (xmlSchemaCompareReplacedStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4782 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4783 | return (xmlSchemaCompareReplaceCollapseStrings(xv, yv, 0)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4784 | |
| 4785 | } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 4786 | |
| 4787 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4788 | return (xmlSchemaComparePreserveCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4789 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4790 | return (xmlSchemaCompareReplaceCollapseStrings(yv, xv, 1)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4791 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4792 | return (xmlSchemaCompareNormStrings(xv, yv)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4793 | } else |
| 4794 | return (-2); |
| 4795 | |
| 4796 | } |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4797 | return (-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4798 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4799 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4800 | case XML_SCHEMAS_NOTATION: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4801 | if ((x == NULL) || (y == NULL)) |
| 4802 | return(-2); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 4803 | if ((ytype == XML_SCHEMAS_QNAME) || |
| 4804 | (ytype == XML_SCHEMAS_NOTATION)) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4805 | if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) && |
| 4806 | (xmlStrEqual(x->value.qname.uri, y->value.qname.uri))) |
| 4807 | return(0); |
| 4808 | return(2); |
| 4809 | } |
| 4810 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4811 | case XML_SCHEMAS_FLOAT: |
| 4812 | case XML_SCHEMAS_DOUBLE: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4813 | if ((x == NULL) || (y == NULL)) |
| 4814 | return(-2); |
| 4815 | if ((ytype == XML_SCHEMAS_FLOAT) || |
| 4816 | (ytype == XML_SCHEMAS_DOUBLE)) |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4817 | return (xmlSchemaCompareFloats(x, y)); |
| 4818 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4819 | case XML_SCHEMAS_BOOLEAN: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4820 | if ((x == NULL) || (y == NULL)) |
| 4821 | return(-2); |
| 4822 | if (ytype == XML_SCHEMAS_BOOLEAN) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4823 | if (x->value.b == y->value.b) |
| 4824 | return(0); |
| 4825 | if (x->value.b == 0) |
| 4826 | return(-1); |
| 4827 | return(1); |
| 4828 | } |
| 4829 | return (-2); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4830 | case XML_SCHEMAS_HEXBINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4831 | if ((x == NULL) || (y == NULL)) |
| 4832 | return(-2); |
| 4833 | if (ytype == XML_SCHEMAS_HEXBINARY) { |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4834 | if (x->value.hex.total == y->value.hex.total) { |
| 4835 | int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str); |
| 4836 | if (ret > 0) |
| 4837 | return(1); |
| 4838 | else if (ret == 0) |
| 4839 | return(0); |
| 4840 | } |
| 4841 | else if (x->value.hex.total > y->value.hex.total) |
| 4842 | return(1); |
| 4843 | |
| 4844 | return(-1); |
| 4845 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4846 | return (-2); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4847 | case XML_SCHEMAS_BASE64BINARY: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4848 | if ((x == NULL) || (y == NULL)) |
| 4849 | return(-2); |
| 4850 | if (ytype == XML_SCHEMAS_BASE64BINARY) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4851 | if (x->value.base64.total == y->value.base64.total) { |
| 4852 | int ret = xmlStrcmp(x->value.base64.str, |
| 4853 | y->value.base64.str); |
| 4854 | if (ret > 0) |
| 4855 | return(1); |
| 4856 | else if (ret == 0) |
| 4857 | return(0); |
Daniel Veillard | d369b13 | 2005-07-14 15:54:44 +0000 | [diff] [blame] | 4858 | else |
| 4859 | return(-1); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4860 | } |
| 4861 | else if (x->value.base64.total > y->value.base64.total) |
| 4862 | return(1); |
| 4863 | else |
| 4864 | return(-1); |
| 4865 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4866 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4867 | case XML_SCHEMAS_IDREFS: |
| 4868 | case XML_SCHEMAS_ENTITIES: |
| 4869 | case XML_SCHEMAS_NMTOKENS: |
| 4870 | TODO |
| 4871 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4872 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4873 | return -2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4877 | * xmlSchemaCompareValues: |
| 4878 | * @x: a first value |
| 4879 | * @y: a second value |
| 4880 | * |
| 4881 | * Compare 2 values |
| 4882 | * |
| 4883 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4884 | * case of error |
| 4885 | */ |
| 4886 | int |
| 4887 | xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4888 | xmlSchemaWhitespaceValueType xws, yws; |
| 4889 | |
Daniel Veillard | 5e09414 | 2005-02-18 19:36:12 +0000 | [diff] [blame] | 4890 | if ((x == NULL) || (y == NULL)) |
| 4891 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4892 | if (x->type == XML_SCHEMAS_STRING) |
| 4893 | xws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4894 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4895 | xws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4896 | else |
| 4897 | xws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4898 | |
| 4899 | if (y->type == XML_SCHEMAS_STRING) |
| 4900 | yws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4901 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4902 | yws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4903 | else |
| 4904 | yws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4905 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4906 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4907 | y, NULL, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | /** |
| 4911 | * xmlSchemaCompareValuesWhtsp: |
| 4912 | * @x: a first value |
| 4913 | * @xws: the whitespace value of x |
| 4914 | * @y: a second value |
| 4915 | * @yws: the whitespace value of y |
| 4916 | * |
| 4917 | * Compare 2 values |
| 4918 | * |
| 4919 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4920 | * case of error |
| 4921 | */ |
| 4922 | int |
| 4923 | xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4924 | xmlSchemaWhitespaceValueType xws, |
| 4925 | xmlSchemaValPtr y, |
| 4926 | xmlSchemaWhitespaceValueType yws) |
| 4927 | { |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 4928 | if ((x == NULL) || (y == NULL)) |
| 4929 | return(-2); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 4930 | return(xmlSchemaCompareValuesInternal(x->type, x, NULL, xws, y->type, |
| 4931 | y, NULL, yws)); |
| 4932 | } |
| 4933 | |
| 4934 | /** |
| 4935 | * xmlSchemaCompareValuesWhtspExt: |
| 4936 | * @x: a first value |
| 4937 | * @xws: the whitespace value of x |
| 4938 | * @y: a second value |
| 4939 | * @yws: the whitespace value of y |
| 4940 | * |
| 4941 | * Compare 2 values |
| 4942 | * |
| 4943 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4944 | * case of error |
| 4945 | */ |
| 4946 | static int |
| 4947 | xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype, |
| 4948 | xmlSchemaValPtr x, |
| 4949 | const xmlChar *xvalue, |
| 4950 | xmlSchemaWhitespaceValueType xws, |
| 4951 | xmlSchemaValType ytype, |
| 4952 | xmlSchemaValPtr y, |
| 4953 | const xmlChar *yvalue, |
| 4954 | xmlSchemaWhitespaceValueType yws) |
| 4955 | { |
| 4956 | return(xmlSchemaCompareValuesInternal(xtype, x, xvalue, xws, ytype, y, |
| 4957 | yvalue, yws)); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4961 | * xmlSchemaNormLen: |
| 4962 | * @value: a string |
| 4963 | * |
| 4964 | * Computes the UTF8 length of the normalized value of the string |
| 4965 | * |
| 4966 | * Returns the length or -1 in case of error. |
| 4967 | */ |
| 4968 | static int |
| 4969 | xmlSchemaNormLen(const xmlChar *value) { |
| 4970 | const xmlChar *utf; |
| 4971 | int ret = 0; |
| 4972 | |
| 4973 | if (value == NULL) |
| 4974 | return(-1); |
| 4975 | utf = value; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4976 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4977 | while (*utf != 0) { |
| 4978 | if (utf[0] & 0x80) { |
| 4979 | if ((utf[1] & 0xc0) != 0x80) |
| 4980 | return(-1); |
| 4981 | if ((utf[0] & 0xe0) == 0xe0) { |
| 4982 | if ((utf[2] & 0xc0) != 0x80) |
| 4983 | return(-1); |
| 4984 | if ((utf[0] & 0xf0) == 0xf0) { |
| 4985 | if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) |
| 4986 | return(-1); |
| 4987 | utf += 4; |
| 4988 | } else { |
| 4989 | utf += 3; |
| 4990 | } |
| 4991 | } else { |
| 4992 | utf += 2; |
| 4993 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4994 | } else if (IS_BLANK_CH(*utf)) { |
| 4995 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4996 | if (*utf == 0) |
| 4997 | break; |
| 4998 | } else { |
| 4999 | utf++; |
| 5000 | } |
| 5001 | ret++; |
| 5002 | } |
| 5003 | return(ret); |
| 5004 | } |
| 5005 | |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 5006 | /** |
| 5007 | * xmlSchemaGetFacetValueAsULong: |
| 5008 | * @facet: an schemas type facet |
| 5009 | * |
| 5010 | * Extract the value of a facet |
| 5011 | * |
| 5012 | * Returns the value as a long |
| 5013 | */ |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5014 | unsigned long |
| 5015 | xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet) |
| 5016 | { |
| 5017 | /* |
| 5018 | * TODO: Check if this is a decimal. |
| 5019 | */ |
William M. Brack | 094dd86 | 2004-11-14 14:28:34 +0000 | [diff] [blame] | 5020 | if (facet == NULL) |
| 5021 | return 0; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5022 | return ((unsigned long) facet->val->value.decimal.lo); |
| 5023 | } |
| 5024 | |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 5025 | /** |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5026 | * xmlSchemaValidateListSimpleTypeFacet: |
| 5027 | * @facet: the facet to check |
| 5028 | * @value: the lexical repr of the value to validate |
| 5029 | * @actualLen: the number of list items |
| 5030 | * @expectedLen: the resulting expected number of list items |
| 5031 | * |
| 5032 | * Checks the value of a list simple type against a facet. |
| 5033 | * |
| 5034 | * Returns 0 if the value is valid, a positive error code |
| 5035 | * number otherwise and -1 in case of an internal error. |
| 5036 | */ |
| 5037 | int |
| 5038 | xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet, |
| 5039 | const xmlChar *value, |
| 5040 | unsigned long actualLen, |
| 5041 | unsigned long *expectedLen) |
| 5042 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 5043 | if (facet == NULL) |
| 5044 | return(-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5045 | /* |
| 5046 | * TODO: Check if this will work with large numbers. |
| 5047 | * (compare value.decimal.mi and value.decimal.hi as well?). |
| 5048 | */ |
| 5049 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 5050 | if (actualLen != facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5051 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5052 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5053 | return (XML_SCHEMAV_CVC_LENGTH_VALID); |
| 5054 | } |
| 5055 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5056 | if (actualLen < facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5057 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5058 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5059 | return (XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5060 | } |
| 5061 | } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) { |
| 5062 | if (actualLen > facet->val->value.decimal.lo) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5063 | if (expectedLen != NULL) |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5064 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5065 | return (XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5066 | } |
| 5067 | } else |
| 5068 | /* |
| 5069 | * NOTE: That we can pass NULL as xmlSchemaValPtr to |
| 5070 | * xmlSchemaValidateFacet, since the remaining facet types |
| 5071 | * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION. |
| 5072 | */ |
| 5073 | return(xmlSchemaValidateFacet(NULL, facet, value, NULL)); |
| 5074 | return (0); |
| 5075 | } |
| 5076 | |
| 5077 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 5078 | * xmlSchemaValidateLengthFacet: |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5079 | * @type: the built-in type |
| 5080 | * @facet: the facet to check |
| 5081 | * @value: the lexical repr. of the value to be validated |
| 5082 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5083 | * @ws: the whitespace type of the value |
| 5084 | * @length: the actual length of the value |
| 5085 | * |
| 5086 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5087 | * facet; sets @length to the computed length of @value. |
| 5088 | * |
| 5089 | * Returns 0 if the value is valid, a positive error code |
| 5090 | * otherwise and -1 in case of an internal or API error. |
| 5091 | */ |
| 5092 | static int |
| 5093 | xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet, |
| 5094 | xmlSchemaTypeType valType, |
| 5095 | const xmlChar *value, |
| 5096 | xmlSchemaValPtr val, |
| 5097 | unsigned long *length, |
| 5098 | xmlSchemaWhitespaceValueType ws) |
| 5099 | { |
| 5100 | unsigned int len = 0; |
| 5101 | |
| 5102 | if ((length == NULL) || (facet == NULL)) |
| 5103 | return (-1); |
| 5104 | *length = 0; |
| 5105 | if ((facet->type != XML_SCHEMA_FACET_LENGTH) && |
| 5106 | (facet->type != XML_SCHEMA_FACET_MAXLENGTH) && |
| 5107 | (facet->type != XML_SCHEMA_FACET_MINLENGTH)) |
| 5108 | return (-1); |
| 5109 | |
| 5110 | /* |
| 5111 | * TODO: length, maxLength and minLength must be of type |
| 5112 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5113 | */ |
| 5114 | if ((facet->val == NULL) || |
| 5115 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5116 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5117 | (facet->val->value.decimal.frac != 0)) { |
| 5118 | return(-1); |
| 5119 | } |
| 5120 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
| 5121 | len = val->value.hex.total; |
| 5122 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5123 | len = val->value.base64.total; |
| 5124 | else { |
| 5125 | switch (valType) { |
| 5126 | case XML_SCHEMAS_STRING: |
| 5127 | case XML_SCHEMAS_NORMSTRING: |
| 5128 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5129 | /* |
| 5130 | * This is to ensure API compatibility with the old |
| 5131 | * xmlSchemaValidateLengthFacet(). Anyway, this was and |
| 5132 | * is not the correct handling. |
| 5133 | * TODO: Get rid of this case somehow. |
| 5134 | */ |
| 5135 | if (valType == XML_SCHEMAS_STRING) |
| 5136 | len = xmlUTF8Strlen(value); |
| 5137 | else |
| 5138 | len = xmlSchemaNormLen(value); |
| 5139 | } else if (value != NULL) { |
| 5140 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5141 | len = xmlSchemaNormLen(value); |
| 5142 | else |
| 5143 | /* |
| 5144 | * Should be OK for "preserve" as well. |
| 5145 | */ |
| 5146 | len = xmlUTF8Strlen(value); |
| 5147 | } |
| 5148 | break; |
| 5149 | case XML_SCHEMAS_IDREF: |
| 5150 | case XML_SCHEMAS_TOKEN: |
| 5151 | case XML_SCHEMAS_LANGUAGE: |
| 5152 | case XML_SCHEMAS_NMTOKEN: |
| 5153 | case XML_SCHEMAS_NAME: |
| 5154 | case XML_SCHEMAS_NCNAME: |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5155 | case XML_SCHEMAS_ID: |
| 5156 | /* |
| 5157 | * FIXME: What exactly to do with anyURI? |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5158 | */ |
| 5159 | case XML_SCHEMAS_ANYURI: |
| 5160 | if (value != NULL) |
| 5161 | len = xmlSchemaNormLen(value); |
| 5162 | break; |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5163 | case XML_SCHEMAS_QNAME: |
| 5164 | case XML_SCHEMAS_NOTATION: |
| 5165 | /* |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5166 | * For QName and NOTATION, those facets are |
| 5167 | * deprecated and should be ignored. |
Kasimier T. Buchcik | bd6c3f7 | 2005-05-25 17:29:36 +0000 | [diff] [blame] | 5168 | */ |
| 5169 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5170 | default: |
| 5171 | TODO |
| 5172 | } |
| 5173 | } |
| 5174 | *length = (unsigned long) len; |
| 5175 | /* |
| 5176 | * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi". |
| 5177 | */ |
| 5178 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 5179 | if (len != facet->val->value.decimal.lo) |
| 5180 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
| 5181 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 5182 | if (len < facet->val->value.decimal.lo) |
| 5183 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 5184 | } else { |
| 5185 | if (len > facet->val->value.decimal.lo) |
| 5186 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 5187 | } |
| 5188 | |
| 5189 | return (0); |
| 5190 | } |
| 5191 | |
| 5192 | /** |
| 5193 | * xmlSchemaValidateLengthFacet: |
| 5194 | * @type: the built-in type |
| 5195 | * @facet: the facet to check |
| 5196 | * @value: the lexical repr. of the value to be validated |
| 5197 | * @val: the precomputed value |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5198 | * @length: the actual length of the value |
| 5199 | * |
| 5200 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5201 | * facet; sets @length to the computed length of @value. |
| 5202 | * |
| 5203 | * Returns 0 if the value is valid, a positive error code |
| 5204 | * otherwise and -1 in case of an internal or API error. |
| 5205 | */ |
| 5206 | int |
| 5207 | xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type, |
| 5208 | xmlSchemaFacetPtr facet, |
| 5209 | const xmlChar *value, |
| 5210 | xmlSchemaValPtr val, |
| 5211 | unsigned long *length) |
| 5212 | { |
Daniel Veillard | cc5e233 | 2005-03-16 21:55:35 +0000 | [diff] [blame] | 5213 | if (type == NULL) |
| 5214 | return(-1); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5215 | return (xmlSchemaValidateLengthFacetInternal(facet, |
| 5216 | type->builtInType, value, val, length, |
| 5217 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
| 5218 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5219 | |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5220 | /** |
| 5221 | * xmlSchemaValidateLengthFacetWhtsp: |
| 5222 | * @facet: the facet to check |
| 5223 | * @valType: the built-in type |
| 5224 | * @value: the lexical repr. of the value to be validated |
| 5225 | * @val: the precomputed value |
| 5226 | * @ws: the whitespace type of the value |
| 5227 | * @length: the actual length of the value |
| 5228 | * |
| 5229 | * Checka a value against a "length", "minLength" and "maxLength" |
| 5230 | * facet; sets @length to the computed length of @value. |
| 5231 | * |
| 5232 | * Returns 0 if the value is valid, a positive error code |
| 5233 | * otherwise and -1 in case of an internal or API error. |
| 5234 | */ |
| 5235 | int |
| 5236 | xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5237 | xmlSchemaValType valType, |
| 5238 | const xmlChar *value, |
| 5239 | xmlSchemaValPtr val, |
| 5240 | unsigned long *length, |
| 5241 | xmlSchemaWhitespaceValueType ws) |
| 5242 | { |
| 5243 | return (xmlSchemaValidateLengthFacetInternal(facet, valType, value, val, |
| 5244 | length, ws)); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5248 | * xmlSchemaValidateFacetInternal: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5249 | * @facet: the facet to check |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5250 | * @fws: the whitespace type of the facet's value |
| 5251 | * @valType: the built-in type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5252 | * @value: the lexical repr of the value to validate |
| 5253 | * @val: the precomputed value |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5254 | * @ws: the whitespace type of the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5255 | * |
| 5256 | * Check a value against a facet condition |
| 5257 | * |
| 5258 | * Returns 0 if the element is schemas valid, a positive error code |
| 5259 | * number otherwise and -1 in case of internal or API error. |
| 5260 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5261 | static int |
| 5262 | xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet, |
| 5263 | xmlSchemaWhitespaceValueType fws, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5264 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5265 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5266 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5267 | xmlSchemaWhitespaceValueType ws) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5268 | { |
| 5269 | int ret; |
| 5270 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5271 | if (facet == NULL) |
| 5272 | return(-1); |
| 5273 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5274 | switch (facet->type) { |
| 5275 | case XML_SCHEMA_FACET_PATTERN: |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5276 | /* |
| 5277 | * NOTE that for patterns, the @value needs to be the normalized |
| 5278 | * value, *not* the lexical initial value or the canonical value. |
| 5279 | */ |
| 5280 | if (value == NULL) |
| 5281 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5282 | ret = xmlRegexpExec(facet->regexp, value); |
| 5283 | if (ret == 1) |
| 5284 | return(0); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5285 | if (ret == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5286 | return(XML_SCHEMAV_CVC_PATTERN_VALID); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5287 | return(ret); |
| 5288 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 5289 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5290 | if (ret == -2) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5291 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5292 | if (ret == -1) |
| 5293 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5294 | return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5295 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 5296 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5297 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5298 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5299 | if ((ret == -1) || (ret == 0)) |
| 5300 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5301 | return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5302 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 5303 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5304 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5305 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5306 | if (ret == 1) |
| 5307 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5308 | return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5309 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 5310 | ret = xmlSchemaCompareValues(val, facet->val); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5311 | if (ret == -2) |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5312 | return(-1); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 5313 | if ((ret == 1) || (ret == 0)) |
| 5314 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5315 | return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5316 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5317 | /* TODO whitespaces */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5318 | /* |
| 5319 | * NOTE: Whitespace should be handled to normalize |
| 5320 | * the value to be validated against a the facets; |
| 5321 | * not to normalize the value in-between. |
| 5322 | */ |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 5323 | return(0); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 5324 | case XML_SCHEMA_FACET_ENUMERATION: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5325 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5326 | /* |
| 5327 | * This is to ensure API compatibility with the old |
| 5328 | * xmlSchemaValidateFacet(). |
| 5329 | * TODO: Get rid of this case. |
| 5330 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5331 | if ((facet->value != NULL) && |
| 5332 | (xmlStrEqual(facet->value, value))) |
| 5333 | return(0); |
| 5334 | } else { |
| 5335 | ret = xmlSchemaCompareValuesWhtspExt(facet->val->type, |
| 5336 | facet->val, facet->value, fws, valType, val, |
| 5337 | value, ws); |
| 5338 | if (ret == -2) |
| 5339 | return(-1); |
| 5340 | if (ret == 0) |
| 5341 | return(0); |
| 5342 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5343 | return(XML_SCHEMAV_CVC_ENUMERATION_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5344 | case XML_SCHEMA_FACET_LENGTH: |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5345 | /* |
| 5346 | * SPEC (1.3) "if {primitive type definition} is QName or NOTATION, |
| 5347 | * then any {value} is facet-valid." |
| 5348 | */ |
| 5349 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5350 | (valType == XML_SCHEMAS_NOTATION)) |
| 5351 | return (0); |
| 5352 | /* No break on purpose. */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5353 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 5354 | case XML_SCHEMA_FACET_MINLENGTH: { |
| 5355 | unsigned int len = 0; |
| 5356 | |
Kasimier T. Buchcik | 4efd90d | 2005-06-09 10:32:53 +0000 | [diff] [blame] | 5357 | if ((valType == XML_SCHEMAS_QNAME) || |
| 5358 | (valType == XML_SCHEMAS_NOTATION)) |
| 5359 | return (0); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5360 | /* |
| 5361 | * TODO: length, maxLength and minLength must be of type |
| 5362 | * nonNegativeInteger only. Check if decimal is used somehow. |
| 5363 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5364 | if ((facet->val == NULL) || |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5365 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 5366 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5367 | (facet->val->value.decimal.frac != 0)) { |
| 5368 | return(-1); |
| 5369 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5370 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 5371 | len = val->value.hex.total; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 5372 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 5373 | len = val->value.base64.total; |
| 5374 | else { |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5375 | switch (valType) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5376 | case XML_SCHEMAS_STRING: |
| 5377 | case XML_SCHEMAS_NORMSTRING: |
| 5378 | if (ws == XML_SCHEMA_WHITESPACE_UNKNOWN) { |
| 5379 | /* |
| 5380 | * This is to ensure API compatibility with the old |
| 5381 | * xmlSchemaValidateFacet(). Anyway, this was and |
| 5382 | * is not the correct handling. |
| 5383 | * TODO: Get rid of this case somehow. |
| 5384 | */ |
| 5385 | if (valType == XML_SCHEMAS_STRING) |
| 5386 | len = xmlUTF8Strlen(value); |
| 5387 | else |
| 5388 | len = xmlSchemaNormLen(value); |
| 5389 | } else if (value != NULL) { |
| 5390 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 5391 | len = xmlSchemaNormLen(value); |
| 5392 | else |
| 5393 | /* |
| 5394 | * Should be OK for "preserve" as well. |
| 5395 | */ |
| 5396 | len = xmlUTF8Strlen(value); |
| 5397 | } |
| 5398 | break; |
| 5399 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5400 | case XML_SCHEMAS_TOKEN: |
| 5401 | case XML_SCHEMAS_LANGUAGE: |
| 5402 | case XML_SCHEMAS_NMTOKEN: |
| 5403 | case XML_SCHEMAS_NAME: |
| 5404 | case XML_SCHEMAS_NCNAME: |
| 5405 | case XML_SCHEMAS_ID: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5406 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5407 | if (value != NULL) |
| 5408 | len = xmlSchemaNormLen(value); |
| 5409 | break; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5410 | default: |
| 5411 | TODO |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 5412 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5413 | } |
| 5414 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5415 | if (len != facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5416 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5417 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5418 | if (len < facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5419 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5420 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 5421 | if (len > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5422 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 5423 | } |
| 5424 | break; |
| 5425 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5426 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 5427 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 5428 | |
| 5429 | if ((facet->val == NULL) || |
Kasimier T. Buchcik | 69dea3a | 2005-11-07 14:02:44 +0000 | [diff] [blame] | 5430 | ((facet->val->type != XML_SCHEMAS_PINTEGER) && |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5431 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 5432 | (facet->val->value.decimal.frac != 0)) { |
| 5433 | return(-1); |
| 5434 | } |
| 5435 | if ((val == NULL) || |
| 5436 | ((val->type != XML_SCHEMAS_DECIMAL) && |
| 5437 | (val->type != XML_SCHEMAS_INTEGER) && |
| 5438 | (val->type != XML_SCHEMAS_NPINTEGER) && |
| 5439 | (val->type != XML_SCHEMAS_NINTEGER) && |
| 5440 | (val->type != XML_SCHEMAS_NNINTEGER) && |
| 5441 | (val->type != XML_SCHEMAS_PINTEGER) && |
| 5442 | (val->type != XML_SCHEMAS_INT) && |
| 5443 | (val->type != XML_SCHEMAS_UINT) && |
| 5444 | (val->type != XML_SCHEMAS_LONG) && |
| 5445 | (val->type != XML_SCHEMAS_ULONG) && |
| 5446 | (val->type != XML_SCHEMAS_SHORT) && |
| 5447 | (val->type != XML_SCHEMAS_USHORT) && |
| 5448 | (val->type != XML_SCHEMAS_BYTE) && |
| 5449 | (val->type != XML_SCHEMAS_UBYTE))) { |
| 5450 | return(-1); |
| 5451 | } |
| 5452 | if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) { |
| 5453 | if (val->value.decimal.total > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5454 | return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5455 | |
| 5456 | } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) { |
| 5457 | if (val->value.decimal.frac > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 5458 | return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 5459 | } |
| 5460 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5461 | default: |
| 5462 | TODO |
| 5463 | } |
| 5464 | return(0); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 5465 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5466 | } |
| 5467 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5468 | /** |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5469 | * xmlSchemaValidateFacet: |
| 5470 | * @base: the base type |
| 5471 | * @facet: the facet to check |
| 5472 | * @value: the lexical repr of the value to validate |
| 5473 | * @val: the precomputed value |
| 5474 | * |
| 5475 | * Check a value against a facet condition |
| 5476 | * |
| 5477 | * Returns 0 if the element is schemas valid, a positive error code |
| 5478 | * number otherwise and -1 in case of internal or API error. |
| 5479 | */ |
| 5480 | int |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5481 | xmlSchemaValidateFacet(xmlSchemaTypePtr base, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5482 | xmlSchemaFacetPtr facet, |
| 5483 | const xmlChar *value, |
| 5484 | xmlSchemaValPtr val) |
| 5485 | { |
| 5486 | /* |
| 5487 | * This tries to ensure API compatibility regarding the old |
| 5488 | * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and |
| 5489 | * xmlSchemaValidateFacetWhtsp(). |
| 5490 | */ |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5491 | if (val != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5492 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5493 | XML_SCHEMA_WHITESPACE_UNKNOWN, val->type, value, val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5494 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5495 | else if (base != NULL) |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5496 | return(xmlSchemaValidateFacetInternal(facet, |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5497 | XML_SCHEMA_WHITESPACE_UNKNOWN, base->builtInType, value, val, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5498 | XML_SCHEMA_WHITESPACE_UNKNOWN)); |
Kasimier T. Buchcik | 9dbb0ce | 2005-03-16 16:39:23 +0000 | [diff] [blame] | 5499 | return(-1); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5500 | } |
| 5501 | |
| 5502 | /** |
| 5503 | * xmlSchemaValidateFacetWhtsp: |
| 5504 | * @facet: the facet to check |
| 5505 | * @fws: the whitespace type of the facet's value |
| 5506 | * @valType: the built-in type of the value |
| 5507 | * @value: the lexical (or normalized for pattern) repr of the value to validate |
| 5508 | * @val: the precomputed value |
| 5509 | * @ws: the whitespace type of the value |
| 5510 | * |
| 5511 | * Check a value against a facet condition. This takes value normalization |
| 5512 | * according to the specified whitespace types into account. |
| 5513 | * Note that @value needs to be the *normalized* value if the facet |
| 5514 | * is of type "pattern". |
| 5515 | * |
| 5516 | * Returns 0 if the element is schemas valid, a positive error code |
| 5517 | * number otherwise and -1 in case of internal or API error. |
| 5518 | */ |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5519 | int |
| 5520 | xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet, |
| 5521 | xmlSchemaWhitespaceValueType fws, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5522 | xmlSchemaValType valType, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5523 | const xmlChar *value, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5524 | xmlSchemaValPtr val, |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5525 | xmlSchemaWhitespaceValueType ws) |
| 5526 | { |
| 5527 | return(xmlSchemaValidateFacetInternal(facet, fws, valType, |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5528 | value, val, ws)); |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5529 | } |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5530 | |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5531 | #if 0 |
| 5532 | #ifndef DBL_DIG |
| 5533 | #define DBL_DIG 16 |
| 5534 | #endif |
| 5535 | #ifndef DBL_EPSILON |
| 5536 | #define DBL_EPSILON 1E-9 |
| 5537 | #endif |
| 5538 | |
| 5539 | #define INTEGER_DIGITS DBL_DIG |
| 5540 | #define FRACTION_DIGITS (DBL_DIG + 1) |
| 5541 | #define EXPONENT_DIGITS (3 + 2) |
| 5542 | |
| 5543 | /** |
| 5544 | * xmlXPathFormatNumber: |
| 5545 | * @number: number to format |
| 5546 | * @buffer: output buffer |
| 5547 | * @buffersize: size of output buffer |
| 5548 | * |
| 5549 | * Convert the number into a string representation. |
| 5550 | */ |
| 5551 | static void |
| 5552 | xmlSchemaFormatFloat(double number, char buffer[], int buffersize) |
| 5553 | { |
| 5554 | switch (xmlXPathIsInf(number)) { |
| 5555 | case 1: |
| 5556 | if (buffersize > (int)sizeof("INF")) |
| 5557 | snprintf(buffer, buffersize, "INF"); |
| 5558 | break; |
| 5559 | case -1: |
| 5560 | if (buffersize > (int)sizeof("-INF")) |
| 5561 | snprintf(buffer, buffersize, "-INF"); |
| 5562 | break; |
| 5563 | default: |
| 5564 | if (xmlXPathIsNaN(number)) { |
| 5565 | if (buffersize > (int)sizeof("NaN")) |
| 5566 | snprintf(buffer, buffersize, "NaN"); |
| 5567 | } else if (number == 0) { |
| 5568 | snprintf(buffer, buffersize, "0.0E0"); |
| 5569 | } else { |
| 5570 | /* 3 is sign, decimal point, and terminating zero */ |
| 5571 | char work[DBL_DIG + EXPONENT_DIGITS + 3]; |
| 5572 | int integer_place, fraction_place; |
| 5573 | char *ptr; |
| 5574 | char *after_fraction; |
| 5575 | double absolute_value; |
| 5576 | int size; |
| 5577 | |
| 5578 | absolute_value = fabs(number); |
| 5579 | |
| 5580 | /* |
| 5581 | * Result is in work, and after_fraction points |
| 5582 | * just past the fractional part. |
| 5583 | * Use scientific notation |
| 5584 | */ |
| 5585 | integer_place = DBL_DIG + EXPONENT_DIGITS + 1; |
| 5586 | fraction_place = DBL_DIG - 1; |
| 5587 | snprintf(work, sizeof(work),"%*.*e", |
| 5588 | integer_place, fraction_place, number); |
| 5589 | after_fraction = strchr(work + DBL_DIG, 'e'); |
| 5590 | /* Remove fractional trailing zeroes */ |
| 5591 | ptr = after_fraction; |
| 5592 | while (*(--ptr) == '0') |
| 5593 | ; |
| 5594 | if (*ptr != '.') |
| 5595 | ptr++; |
| 5596 | while ((*ptr++ = *after_fraction++) != 0); |
| 5597 | |
| 5598 | /* Finally copy result back to caller */ |
| 5599 | size = strlen(work) + 1; |
| 5600 | if (size > buffersize) { |
| 5601 | work[buffersize - 1] = 0; |
| 5602 | size = buffersize; |
| 5603 | } |
| 5604 | memmove(buffer, work, size); |
| 5605 | } |
| 5606 | break; |
| 5607 | } |
| 5608 | } |
| 5609 | #endif |
| 5610 | |
Kasimier T. Buchcik | 6c81d0e | 2005-03-15 14:58:11 +0000 | [diff] [blame] | 5611 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5612 | * xmlSchemaGetCanonValue: |
| 5613 | * @val: the precomputed value |
| 5614 | * @retValue: the returned value |
| 5615 | * |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5616 | * Get a the cononical lexical representation of the value. |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5617 | * The caller has to FREE the returned retValue. |
| 5618 | * |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5619 | * WARNING: Some value types are not supported yet, resulting |
| 5620 | * in a @retValue of "???". |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5621 | * |
| 5622 | * TODO: XML Schema 1.0 does not define canonical representations |
| 5623 | * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay, |
| 5624 | * 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] | 5625 | * |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5626 | * |
| 5627 | * Returns 0 if the value could be built, 1 if the value type is |
| 5628 | * not supported yet and -1 in case of API errors. |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5629 | */ |
| 5630 | int |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5631 | xmlSchemaGetCanonValue(xmlSchemaValPtr val, const xmlChar **retValue) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5632 | { |
Daniel Veillard | b5839c3 | 2005-02-19 18:27:14 +0000 | [diff] [blame] | 5633 | if ((retValue == NULL) || (val == NULL)) |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5634 | return (-1); |
| 5635 | *retValue = NULL; |
| 5636 | switch (val->type) { |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5637 | case XML_SCHEMAS_STRING: |
| 5638 | if (val->value.str == NULL) |
| 5639 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5640 | else |
| 5641 | *retValue = |
| 5642 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
| 5643 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5644 | case XML_SCHEMAS_NORMSTRING: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5645 | if (val->value.str == NULL) |
| 5646 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 5647 | else { |
| 5648 | *retValue = xmlSchemaWhiteSpaceReplace( |
| 5649 | (const xmlChar *) val->value.str); |
| 5650 | if ((*retValue) == NULL) |
| 5651 | *retValue = BAD_CAST xmlStrdup( |
| 5652 | (const xmlChar *) val->value.str); |
| 5653 | } |
| 5654 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5655 | case XML_SCHEMAS_TOKEN: |
| 5656 | case XML_SCHEMAS_LANGUAGE: |
| 5657 | case XML_SCHEMAS_NMTOKEN: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5658 | case XML_SCHEMAS_NAME: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5659 | case XML_SCHEMAS_NCNAME: |
| 5660 | case XML_SCHEMAS_ID: |
| 5661 | case XML_SCHEMAS_IDREF: |
| 5662 | case XML_SCHEMAS_ENTITY: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5663 | case XML_SCHEMAS_NOTATION: /* Unclear */ |
| 5664 | case XML_SCHEMAS_ANYURI: /* Unclear */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 5665 | if (val->value.str == NULL) |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5666 | return (-1); |
Kasimier T. Buchcik | aba15f7 | 2005-04-01 15:17:27 +0000 | [diff] [blame] | 5667 | *retValue = |
| 5668 | BAD_CAST xmlSchemaCollapseString(BAD_CAST val->value.str); |
| 5669 | if (*retValue == NULL) |
| 5670 | *retValue = |
| 5671 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5672 | break; |
| 5673 | case XML_SCHEMAS_QNAME: |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5674 | /* TODO: Unclear in XML Schema 1.0. */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 5675 | if (val->value.qname.uri == NULL) { |
| 5676 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.qname.name); |
| 5677 | return (0); |
| 5678 | } else { |
| 5679 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "{"); |
| 5680 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5681 | BAD_CAST val->value.qname.uri); |
| 5682 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5683 | BAD_CAST "}"); |
| 5684 | *retValue = BAD_CAST xmlStrcat((xmlChar *) (*retValue), |
| 5685 | BAD_CAST val->value.qname.uri); |
| 5686 | } |
| 5687 | break; |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 5688 | case XML_SCHEMAS_DECIMAL: |
| 5689 | /* |
| 5690 | * TODO: Lookout for a more simple implementation. |
| 5691 | */ |
| 5692 | if ((val->value.decimal.total == 1) && |
| 5693 | (val->value.decimal.lo == 0)) { |
| 5694 | *retValue = xmlStrdup(BAD_CAST "0.0"); |
| 5695 | } else { |
| 5696 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5697 | int bufsize; |
| 5698 | char *buf = NULL, *offs; |
| 5699 | |
| 5700 | /* Add room for the decimal point as well. */ |
| 5701 | bufsize = dec.total + 2; |
| 5702 | if (dec.sign) |
| 5703 | bufsize++; |
| 5704 | /* Add room for leading/trailing zero. */ |
| 5705 | if ((dec.frac == 0) || (dec.frac == dec.total)) |
| 5706 | bufsize++; |
| 5707 | buf = xmlMalloc(bufsize); |
| 5708 | offs = buf; |
| 5709 | if (dec.sign) |
| 5710 | *offs++ = '-'; |
| 5711 | if (dec.frac == dec.total) { |
| 5712 | *offs++ = '0'; |
| 5713 | *offs++ = '.'; |
| 5714 | } |
| 5715 | if (dec.hi != 0) |
| 5716 | snprintf(offs, bufsize - (offs - buf), |
| 5717 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5718 | else if (dec.mi != 0) |
| 5719 | snprintf(offs, bufsize - (offs - buf), |
| 5720 | "%lu%lu", dec.mi, dec.lo); |
| 5721 | else |
| 5722 | snprintf(offs, bufsize - (offs - buf), |
| 5723 | "%lu", dec.lo); |
| 5724 | |
| 5725 | if (dec.frac != 0) { |
| 5726 | if (dec.frac != dec.total) { |
| 5727 | int diff = dec.total - dec.frac; |
| 5728 | /* |
| 5729 | * Insert the decimal point. |
| 5730 | */ |
| 5731 | memmove(offs + diff + 1, offs + diff, dec.frac +1); |
| 5732 | offs[diff] = '.'; |
| 5733 | } else { |
| 5734 | unsigned int i = 0; |
| 5735 | /* |
| 5736 | * Insert missing zeroes behind the decimal point. |
| 5737 | */ |
| 5738 | while (*(offs + i) != 0) |
| 5739 | i++; |
| 5740 | if (i < dec.total) { |
| 5741 | memmove(offs + (dec.total - i), offs, i +1); |
| 5742 | memset(offs, '0', dec.total - i); |
| 5743 | } |
| 5744 | } |
| 5745 | } else { |
| 5746 | /* |
| 5747 | * Append decimal point and zero. |
| 5748 | */ |
| 5749 | offs = buf + bufsize - 1; |
| 5750 | *offs-- = 0; |
| 5751 | *offs-- = '0'; |
| 5752 | *offs-- = '.'; |
| 5753 | } |
| 5754 | *retValue = BAD_CAST buf; |
| 5755 | } |
| 5756 | break; |
| 5757 | case XML_SCHEMAS_INTEGER: |
| 5758 | case XML_SCHEMAS_PINTEGER: |
| 5759 | case XML_SCHEMAS_NPINTEGER: |
| 5760 | case XML_SCHEMAS_NINTEGER: |
| 5761 | case XML_SCHEMAS_NNINTEGER: |
| 5762 | case XML_SCHEMAS_LONG: |
| 5763 | case XML_SCHEMAS_BYTE: |
| 5764 | case XML_SCHEMAS_SHORT: |
| 5765 | case XML_SCHEMAS_INT: |
| 5766 | case XML_SCHEMAS_UINT: |
| 5767 | case XML_SCHEMAS_ULONG: |
| 5768 | case XML_SCHEMAS_USHORT: |
| 5769 | case XML_SCHEMAS_UBYTE: |
| 5770 | if ((val->value.decimal.total == 1) && |
| 5771 | (val->value.decimal.lo == 0)) |
| 5772 | *retValue = xmlStrdup(BAD_CAST "0"); |
| 5773 | else { |
| 5774 | xmlSchemaValDecimal dec = val->value.decimal; |
| 5775 | int bufsize = dec.total + 1; |
| 5776 | |
| 5777 | /* Add room for the decimal point as well. */ |
| 5778 | if (dec.sign) |
| 5779 | bufsize++; |
| 5780 | *retValue = xmlMalloc(bufsize); |
| 5781 | if (dec.hi != 0) { |
| 5782 | if (dec.sign) |
| 5783 | snprintf((char *) *retValue, bufsize, |
| 5784 | "-%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5785 | else |
| 5786 | snprintf((char *) *retValue, bufsize, |
| 5787 | "%lu%lu%lu", dec.hi, dec.mi, dec.lo); |
| 5788 | } else if (dec.mi != 0) { |
| 5789 | if (dec.sign) |
| 5790 | snprintf((char *) *retValue, bufsize, |
| 5791 | "-%lu%lu", dec.mi, dec.lo); |
| 5792 | else |
| 5793 | snprintf((char *) *retValue, bufsize, |
| 5794 | "%lu%lu", dec.mi, dec.lo); |
| 5795 | } else { |
| 5796 | if (dec.sign) |
| 5797 | snprintf((char *) *retValue, bufsize, "-%lu", dec.lo); |
| 5798 | else |
| 5799 | snprintf((char *) *retValue, bufsize, "%lu", dec.lo); |
| 5800 | } |
| 5801 | } |
| 5802 | break; |
| 5803 | case XML_SCHEMAS_BOOLEAN: |
| 5804 | if (val->value.b) |
| 5805 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "true"); |
| 5806 | else |
| 5807 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "false"); |
| 5808 | break; |
| 5809 | case XML_SCHEMAS_DURATION: { |
| 5810 | char buf[100]; |
| 5811 | unsigned long year; |
| 5812 | unsigned long mon, day, hour = 0, min = 0; |
| 5813 | double sec = 0, left; |
| 5814 | |
| 5815 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5816 | /* |
| 5817 | * TODO: This results in a normalized output of the value |
| 5818 | * - which is NOT conformant to the spec - |
| 5819 | * since the exact values of each property are not |
| 5820 | * recoverable. Think about extending the structure to |
| 5821 | * provide a field for every property. |
| 5822 | */ |
| 5823 | year = (unsigned long) FQUOTIENT(labs(val->value.dur.mon), 12); |
| 5824 | mon = labs(val->value.dur.mon) - 12 * year; |
| 5825 | |
| 5826 | day = (unsigned long) FQUOTIENT(fabs(val->value.dur.sec), 86400); |
| 5827 | left = fabs(val->value.dur.sec) - day * 86400; |
| 5828 | if (left > 0) { |
| 5829 | hour = (unsigned long) FQUOTIENT(left, 3600); |
| 5830 | left = left - (hour * 3600); |
| 5831 | if (left > 0) { |
| 5832 | min = (unsigned long) FQUOTIENT(left, 60); |
| 5833 | sec = left - (min * 60); |
| 5834 | } |
| 5835 | } |
| 5836 | if ((val->value.dur.mon < 0) || (val->value.dur.sec < 0)) |
| 5837 | snprintf(buf, 100, "P%luY%luM%luDT%luH%luM%.14gS", |
| 5838 | year, mon, day, hour, min, sec); |
| 5839 | else |
| 5840 | snprintf(buf, 100, "-P%luY%luM%luDT%luH%luM%.14gS", |
| 5841 | year, mon, day, hour, min, sec); |
| 5842 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5843 | } |
| 5844 | break; |
| 5845 | case XML_SCHEMAS_GYEAR: { |
| 5846 | char buf[30]; |
| 5847 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5848 | /* TODO: What to do with the timezone? */ |
| 5849 | snprintf(buf, 30, "%04ld", val->value.date.year); |
| 5850 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5851 | } |
| 5852 | break; |
| 5853 | case XML_SCHEMAS_GMONTH: { |
| 5854 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5855 | /* TODO: What to do with the timezone? */ |
| 5856 | *retValue = xmlMalloc(5); |
| 5857 | snprintf((char *) *retValue, 6, "--%02u", |
| 5858 | val->value.date.mon); |
| 5859 | } |
| 5860 | break; |
| 5861 | case XML_SCHEMAS_GDAY: { |
| 5862 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5863 | /* TODO: What to do with the timezone? */ |
| 5864 | *retValue = xmlMalloc(6); |
| 5865 | snprintf((char *) *retValue, 6, "---%02u", |
| 5866 | val->value.date.day); |
| 5867 | } |
| 5868 | break; |
| 5869 | case XML_SCHEMAS_GMONTHDAY: { |
| 5870 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5871 | /* TODO: What to do with the timezone? */ |
| 5872 | *retValue = xmlMalloc(8); |
| 5873 | snprintf((char *) *retValue, 8, "--%02u-%02u", |
| 5874 | val->value.date.mon, val->value.date.day); |
| 5875 | } |
| 5876 | break; |
| 5877 | case XML_SCHEMAS_GYEARMONTH: { |
| 5878 | char buf[35]; |
| 5879 | /* TODO: Unclear in XML Schema 1.0 */ |
| 5880 | /* TODO: What to do with the timezone? */ |
| 5881 | if (val->value.date.year < 0) |
| 5882 | snprintf(buf, 35, "-%04ld-%02u", |
| 5883 | labs(val->value.date.year), |
| 5884 | val->value.date.mon); |
| 5885 | else |
| 5886 | snprintf(buf, 35, "%04ld-%02u", |
| 5887 | val->value.date.year, val->value.date.mon); |
| 5888 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5889 | } |
| 5890 | break; |
| 5891 | case XML_SCHEMAS_TIME: |
| 5892 | { |
| 5893 | char buf[30]; |
| 5894 | |
| 5895 | if (val->value.date.tz_flag) { |
| 5896 | xmlSchemaValPtr norm; |
| 5897 | |
| 5898 | norm = xmlSchemaDateNormalize(val, 0); |
| 5899 | if (norm == NULL) |
| 5900 | return (-1); |
| 5901 | /* |
| 5902 | * TODO: Check if "%.14g" is portable. |
| 5903 | */ |
| 5904 | snprintf(buf, 30, |
| 5905 | "%02u:%02u:%02.14gZ", |
| 5906 | norm->value.date.hour, |
| 5907 | norm->value.date.min, |
| 5908 | norm->value.date.sec); |
| 5909 | xmlSchemaFreeValue(norm); |
| 5910 | } else { |
| 5911 | snprintf(buf, 30, |
| 5912 | "%02u:%02u:%02.14g", |
| 5913 | val->value.date.hour, |
| 5914 | val->value.date.min, |
| 5915 | val->value.date.sec); |
| 5916 | } |
| 5917 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5918 | } |
| 5919 | break; |
| 5920 | case XML_SCHEMAS_DATE: |
| 5921 | { |
| 5922 | char buf[30]; |
| 5923 | |
| 5924 | if (val->value.date.tz_flag) { |
| 5925 | xmlSchemaValPtr norm; |
| 5926 | |
| 5927 | norm = xmlSchemaDateNormalize(val, 0); |
| 5928 | if (norm == NULL) |
| 5929 | return (-1); |
| 5930 | /* |
| 5931 | * TODO: Append the canonical value of the |
| 5932 | * recoverable timezone and not "Z". |
| 5933 | */ |
| 5934 | snprintf(buf, 30, |
| 5935 | "%04ld:%02u:%02uZ", |
| 5936 | norm->value.date.year, norm->value.date.mon, |
| 5937 | norm->value.date.day); |
| 5938 | xmlSchemaFreeValue(norm); |
| 5939 | } else { |
| 5940 | snprintf(buf, 30, |
| 5941 | "%04ld:%02u:%02u", |
| 5942 | val->value.date.year, val->value.date.mon, |
| 5943 | val->value.date.day); |
| 5944 | } |
| 5945 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5946 | } |
| 5947 | break; |
| 5948 | case XML_SCHEMAS_DATETIME: |
| 5949 | { |
| 5950 | char buf[50]; |
| 5951 | |
| 5952 | if (val->value.date.tz_flag) { |
| 5953 | xmlSchemaValPtr norm; |
| 5954 | |
| 5955 | norm = xmlSchemaDateNormalize(val, 0); |
| 5956 | if (norm == NULL) |
| 5957 | return (-1); |
| 5958 | /* |
| 5959 | * TODO: Check if "%.14g" is portable. |
| 5960 | */ |
| 5961 | snprintf(buf, 50, |
| 5962 | "%04ld:%02u:%02uT%02u:%02u:%02.14gZ", |
| 5963 | norm->value.date.year, norm->value.date.mon, |
| 5964 | norm->value.date.day, norm->value.date.hour, |
| 5965 | norm->value.date.min, norm->value.date.sec); |
| 5966 | xmlSchemaFreeValue(norm); |
| 5967 | } else { |
| 5968 | snprintf(buf, 50, |
| 5969 | "%04ld:%02u:%02uT%02u:%02u:%02.14g", |
| 5970 | val->value.date.year, val->value.date.mon, |
| 5971 | val->value.date.day, val->value.date.hour, |
| 5972 | val->value.date.min, val->value.date.sec); |
| 5973 | } |
| 5974 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5975 | } |
| 5976 | break; |
| 5977 | case XML_SCHEMAS_HEXBINARY: |
| 5978 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.hex.str); |
| 5979 | break; |
| 5980 | case XML_SCHEMAS_BASE64BINARY: |
| 5981 | /* |
| 5982 | * TODO: Is the following spec piece implemented?: |
| 5983 | * SPEC: "Note: For some values the canonical form defined |
| 5984 | * above does not conform to [RFC 2045], which requires breaking |
| 5985 | * with linefeeds at appropriate intervals." |
| 5986 | */ |
| 5987 | *retValue = BAD_CAST xmlStrdup(BAD_CAST val->value.base64.str); |
| 5988 | break; |
| 5989 | case XML_SCHEMAS_FLOAT: { |
| 5990 | char buf[30]; |
| 5991 | /* |
| 5992 | * |m| < 16777216, -149 <= e <= 104. |
| 5993 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 5994 | * yet conformant. The c type float does not cover |
| 5995 | * the whole range. |
| 5996 | */ |
| 5997 | snprintf(buf, 30, "%01.14e", val->value.f); |
| 5998 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 5999 | } |
| 6000 | break; |
| 6001 | case XML_SCHEMAS_DOUBLE: { |
| 6002 | char buf[40]; |
| 6003 | /* |m| < 9007199254740992, -1075 <= e <= 970 */ |
| 6004 | /* |
| 6005 | * TODO: Handle, NaN, INF, -INF. The format is not |
| 6006 | * yet conformant. The c type float does not cover |
| 6007 | * the whole range. |
| 6008 | */ |
| 6009 | snprintf(buf, 40, "%01.14e", val->value.d); |
| 6010 | *retValue = BAD_CAST xmlStrdup(BAD_CAST buf); |
| 6011 | } |
| 6012 | break; |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6013 | default: |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6014 | *retValue = BAD_CAST xmlStrdup(BAD_CAST "???"); |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6015 | return (1); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6016 | } |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6017 | return (0); |
| 6018 | } |
| 6019 | |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6020 | /** |
Kasimier T. Buchcik | c872afb | 2005-04-18 10:57:04 +0000 | [diff] [blame] | 6021 | * xmlSchemaGetCanonValueWhtsp: |
| 6022 | * @val: the precomputed value |
| 6023 | * @retValue: the returned value |
| 6024 | * @ws: the whitespace type of the value |
| 6025 | * |
| 6026 | * Get a the cononical representation of the value. |
| 6027 | * The caller has to free the returned @retValue. |
| 6028 | * |
| 6029 | * Returns 0 if the value could be built, 1 if the value type is |
| 6030 | * not supported yet and -1 in case of API errors. |
| 6031 | */ |
| 6032 | int |
| 6033 | xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val, |
| 6034 | const xmlChar **retValue, |
| 6035 | xmlSchemaWhitespaceValueType ws) |
| 6036 | { |
| 6037 | if ((retValue == NULL) || (val == NULL)) |
| 6038 | return (-1); |
| 6039 | if ((ws == XML_SCHEMA_WHITESPACE_UNKNOWN) || |
| 6040 | (ws > XML_SCHEMA_WHITESPACE_COLLAPSE)) |
| 6041 | return (-1); |
| 6042 | |
| 6043 | *retValue = NULL; |
| 6044 | switch (val->type) { |
| 6045 | case XML_SCHEMAS_STRING: |
| 6046 | if (val->value.str == NULL) |
| 6047 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 6048 | else if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 6049 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 6050 | else if (ws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 6051 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 6052 | if ((*retValue) == NULL) |
| 6053 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6054 | break; |
| 6055 | case XML_SCHEMAS_NORMSTRING: |
| 6056 | if (val->value.str == NULL) |
| 6057 | *retValue = BAD_CAST xmlStrdup(BAD_CAST ""); |
| 6058 | else { |
| 6059 | if (ws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 6060 | *retValue = xmlSchemaCollapseString(val->value.str); |
| 6061 | else |
| 6062 | *retValue = xmlSchemaWhiteSpaceReplace(val->value.str); |
| 6063 | if ((*retValue) == NULL) |
| 6064 | *retValue = BAD_CAST xmlStrdup(val->value.str); |
| 6065 | } |
| 6066 | break; |
| 6067 | default: |
| 6068 | return (xmlSchemaGetCanonValue(val, retValue)); |
| 6069 | } |
| 6070 | return (0); |
| 6071 | } |
| 6072 | |
| 6073 | /** |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6074 | * xmlSchemaGetValType: |
| 6075 | * @val: a schemas value |
| 6076 | * |
| 6077 | * Accessor for the type of a value |
| 6078 | * |
| 6079 | * Returns the xmlSchemaValType of the value |
| 6080 | */ |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6081 | xmlSchemaValType |
| 6082 | xmlSchemaGetValType(xmlSchemaValPtr val) |
| 6083 | { |
Daniel Veillard | bda5957 | 2005-04-01 17:15:17 +0000 | [diff] [blame] | 6084 | if (val == NULL) |
| 6085 | return(XML_SCHEMAS_UNKNOWN); |
Kasimier T. Buchcik | 478d693 | 2005-03-16 16:29:18 +0000 | [diff] [blame] | 6086 | return (val->type); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 6087 | } |
| 6088 | |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 6089 | #define bottom_xmlschemastypes |
| 6090 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6091 | #endif /* LIBXML_SCHEMAS_ENABLED */ |