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