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