Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * schemastypes.c : implementation of the XML Schema Datatypes |
| 3 | * definition and validity checking |
| 4 | * |
| 5 | * See Copyright for the status of this software. |
| 6 | * |
| 7 | * Daniel Veillard <veillard@redhat.com> |
| 8 | */ |
| 9 | |
| 10 | #define IN_LIBXML |
| 11 | #include "libxml.h" |
| 12 | |
| 13 | #ifdef LIBXML_SCHEMAS_ENABLED |
| 14 | |
| 15 | #include <string.h> |
| 16 | #include <libxml/xmlmemory.h> |
| 17 | #include <libxml/parser.h> |
| 18 | #include <libxml/parserInternals.h> |
| 19 | #include <libxml/hash.h> |
| 20 | #include <libxml/valid.h> |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 21 | #include <libxml/xpath.h> |
| 22 | #include <libxml/uri.h> |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 23 | |
| 24 | #include <libxml/xmlschemas.h> |
| 25 | #include <libxml/schemasInternals.h> |
| 26 | #include <libxml/xmlschemastypes.h> |
| 27 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 28 | #ifdef HAVE_MATH_H |
| 29 | #include <math.h> |
| 30 | #endif |
| 31 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 32 | #define DEBUG |
| 33 | |
Daniel Veillard | d0cf7f6 | 2004-11-09 16:17:02 +0000 | [diff] [blame] | 34 | #ifndef LIBXML_XPATH_ENABLED |
| 35 | extern double xmlXPathNAN; |
| 36 | extern double xmlXPathPINF; |
| 37 | extern double xmlXPathNINF; |
| 38 | #endif |
| 39 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 40 | #define TODO \ |
| 41 | xmlGenericError(xmlGenericErrorContext, \ |
| 42 | "Unimplemented block at %s:%d\n", \ |
| 43 | __FILE__, __LINE__); |
| 44 | |
| 45 | #define XML_SCHEMAS_NAMESPACE_NAME \ |
| 46 | (const xmlChar *)"http://www.w3.org/2001/XMLSchema" |
| 47 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 48 | #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \ |
| 49 | ((c) == 0xd)) |
| 50 | |
| 51 | #define IS_WSP_SPACE_CH(c) ((c) == 0x20) |
| 52 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 53 | |
Daniel Veillard | 5f704af | 2003-03-05 10:01:43 +0000 | [diff] [blame] | 54 | static unsigned long powten[10] = { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 55 | 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L, |
| 56 | 100000000L, 1000000000L |
| 57 | }; |
| 58 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 59 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 60 | /* Date value */ |
| 61 | typedef struct _xmlSchemaValDate xmlSchemaValDate; |
| 62 | typedef xmlSchemaValDate *xmlSchemaValDatePtr; |
| 63 | struct _xmlSchemaValDate { |
| 64 | long year; |
| 65 | unsigned int mon :4; /* 1 <= mon <= 12 */ |
| 66 | unsigned int day :5; /* 1 <= day <= 31 */ |
| 67 | unsigned int hour :5; /* 0 <= hour <= 23 */ |
| 68 | unsigned int min :6; /* 0 <= min <= 59 */ |
| 69 | double sec; |
Daniel Veillard | a77cf71 | 2003-05-09 23:09:55 +0000 | [diff] [blame] | 70 | unsigned int tz_flag :1; /* is tzo explicitely set? */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 71 | int tzo :11; /* -1440 <= tzo <= 1440 */ |
| 72 | }; |
| 73 | |
| 74 | /* Duration value */ |
| 75 | typedef struct _xmlSchemaValDuration xmlSchemaValDuration; |
| 76 | typedef xmlSchemaValDuration *xmlSchemaValDurationPtr; |
| 77 | struct _xmlSchemaValDuration { |
| 78 | long mon; /* mon stores years also */ |
| 79 | long day; |
| 80 | double sec; /* sec stores min and hour also */ |
| 81 | }; |
| 82 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 83 | typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal; |
| 84 | typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr; |
| 85 | struct _xmlSchemaValDecimal { |
| 86 | /* would use long long but not portable */ |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 87 | unsigned long lo; |
| 88 | unsigned long mi; |
| 89 | unsigned long hi; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 90 | unsigned int extra; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 91 | unsigned int sign:1; |
William M. Brack | c193956 | 2003-08-05 15:52:22 +0000 | [diff] [blame] | 92 | unsigned int frac:7; |
| 93 | unsigned int total:8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 96 | typedef struct _xmlSchemaValQName xmlSchemaValQName; |
| 97 | typedef xmlSchemaValQName *xmlSchemaValQNamePtr; |
| 98 | struct _xmlSchemaValQName { |
| 99 | xmlChar *name; |
| 100 | xmlChar *uri; |
| 101 | }; |
| 102 | |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 103 | typedef struct _xmlSchemaValHex xmlSchemaValHex; |
| 104 | typedef xmlSchemaValHex *xmlSchemaValHexPtr; |
| 105 | struct _xmlSchemaValHex { |
| 106 | xmlChar *str; |
| 107 | unsigned int total; |
| 108 | }; |
| 109 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 110 | typedef struct _xmlSchemaValBase64 xmlSchemaValBase64; |
| 111 | typedef xmlSchemaValBase64 *xmlSchemaValBase64Ptr; |
| 112 | struct _xmlSchemaValBase64 { |
| 113 | xmlChar *str; |
| 114 | unsigned int total; |
| 115 | }; |
| 116 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 117 | struct _xmlSchemaVal { |
| 118 | xmlSchemaValType type; |
| 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 | ************************************************************************/ |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 212 | /* |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 213 | * xmlSchemaInitBasicType: |
| 214 | * @name: the type name |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 215 | * @type: the value type associated |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 216 | * |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 217 | * Initialize one primitive built-in type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 218 | */ |
| 219 | static xmlSchemaTypePtr |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 220 | xmlSchemaInitBasicType(const char *name, xmlSchemaValType type, |
| 221 | xmlSchemaTypePtr baseType) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 222 | xmlSchemaTypePtr ret; |
| 223 | |
| 224 | ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType)); |
| 225 | if (ret == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 226 | xmlSchemaTypeErrMemory(NULL, "could not initialize basic types"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 227 | return(NULL); |
| 228 | } |
| 229 | memset(ret, 0, sizeof(xmlSchemaType)); |
Daniel Veillard | be9c632 | 2003-11-22 20:37:51 +0000 | [diff] [blame] | 230 | ret->name = (const xmlChar *)name; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 231 | ret->type = XML_SCHEMA_TYPE_BASIC; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 232 | ret->baseType = baseType; |
| 233 | /* |
| 234 | * Hack to reflect the variety. |
| 235 | */ |
| 236 | if ((type == XML_SCHEMAS_IDREFS) || |
| 237 | (type == XML_SCHEMAS_NMTOKENS) || |
| 238 | (type == XML_SCHEMAS_ENTITIES)) |
| 239 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_LIST; |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 240 | else if ((type != XML_SCHEMAS_ANYTYPE) && |
| 241 | (type != XML_SCHEMAS_ANYSIMPLETYPE)) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 242 | ret->flags |= XML_SCHEMAS_TYPE_VARIETY_ATOMIC; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 243 | ret->contentType = XML_SCHEMA_CONTENT_BASIC; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 244 | switch (type) { |
| 245 | case XML_SCHEMAS_STRING: |
| 246 | case XML_SCHEMAS_DECIMAL: |
| 247 | case XML_SCHEMAS_DATE: |
| 248 | case XML_SCHEMAS_DATETIME: |
| 249 | case XML_SCHEMAS_TIME: |
| 250 | case XML_SCHEMAS_GYEAR: |
| 251 | case XML_SCHEMAS_GYEARMONTH: |
| 252 | case XML_SCHEMAS_GMONTH: |
| 253 | case XML_SCHEMAS_GMONTHDAY: |
| 254 | case XML_SCHEMAS_GDAY: |
| 255 | case XML_SCHEMAS_DURATION: |
| 256 | case XML_SCHEMAS_FLOAT: |
| 257 | case XML_SCHEMAS_DOUBLE: |
| 258 | case XML_SCHEMAS_BOOLEAN: |
| 259 | case XML_SCHEMAS_ANYURI: |
| 260 | case XML_SCHEMAS_HEXBINARY: |
| 261 | case XML_SCHEMAS_BASE64BINARY: |
| 262 | case XML_SCHEMAS_QNAME: |
| 263 | case XML_SCHEMAS_NOTATION: |
| 264 | ret->flags |= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE; |
William M. Brack | 96d2eff | 2004-06-30 11:48:47 +0000 | [diff] [blame] | 265 | default: |
| 266 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 269 | xmlHashAddEntry2(xmlSchemaTypesBank, ret->name, |
| 270 | XML_SCHEMAS_NAMESPACE_NAME, ret); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 271 | ret->builtInType = type; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 272 | return(ret); |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * xmlSchemaInitTypes: |
| 277 | * |
| 278 | * Initialize the default XML Schemas type library |
| 279 | */ |
| 280 | void |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 281 | xmlSchemaInitTypes(void) |
| 282 | { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 283 | if (xmlSchemaTypesInitialized != 0) |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 284 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 285 | xmlSchemaTypesBank = xmlHashCreate(40); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 286 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 287 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 288 | /* |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 289 | * 3.4.7 Built-in Complex Type Definition |
| 290 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 291 | xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType", |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 292 | XML_SCHEMAS_ANYTYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 293 | NULL); |
| 294 | xmlSchemaTypeAnyTypeDef->baseType = xmlSchemaTypeAnyTypeDef; |
| 295 | xmlSchemaTypeAnyTypeDef->contentType = XML_SCHEMA_CONTENT_MIXED; |
| 296 | { |
| 297 | xmlSchemaWildcardPtr wild; |
| 298 | |
| 299 | wild = (xmlSchemaWildcardPtr) xmlMalloc(sizeof(xmlSchemaWildcard)); |
| 300 | if (wild == NULL) { |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 301 | xmlSchemaTypeErrMemory(NULL, "could not create an attribute wildcard on anyType"); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 302 | return; |
| 303 | } |
| 304 | memset(wild, 0, sizeof(xmlSchemaWildcard)); |
| 305 | wild->any = 1; |
| 306 | wild->processContents = XML_SCHEMAS_ANY_LAX; |
| 307 | wild->minOccurs = 1; |
| 308 | wild->maxOccurs = 1; |
| 309 | xmlSchemaTypeAnyTypeDef->attributeWildcard = wild; |
| 310 | } |
| 311 | xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType", |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 312 | XML_SCHEMAS_ANYSIMPLETYPE, |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 313 | xmlSchemaTypeAnyTypeDef); |
| 314 | /* |
| 315 | * primitive datatypes |
| 316 | */ |
| 317 | xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string", |
| 318 | XML_SCHEMAS_STRING, |
| 319 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 320 | xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 321 | XML_SCHEMAS_DECIMAL, |
| 322 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 323 | xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 324 | XML_SCHEMAS_DATE, |
| 325 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 326 | xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 327 | XML_SCHEMAS_DATETIME, |
| 328 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 329 | xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 330 | XML_SCHEMAS_TIME, |
| 331 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 332 | xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 333 | XML_SCHEMAS_GYEAR, |
| 334 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 335 | xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 336 | XML_SCHEMAS_GYEARMONTH, |
| 337 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 338 | xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 339 | XML_SCHEMAS_GMONTH, |
| 340 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 341 | xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 342 | XML_SCHEMAS_GMONTHDAY, |
| 343 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 344 | xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 345 | XML_SCHEMAS_GDAY, |
| 346 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 347 | xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 348 | XML_SCHEMAS_DURATION, |
| 349 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 350 | xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 351 | XML_SCHEMAS_FLOAT, |
| 352 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 353 | xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 354 | XML_SCHEMAS_DOUBLE, |
| 355 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 356 | xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 357 | XML_SCHEMAS_BOOLEAN, |
| 358 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 359 | xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 360 | XML_SCHEMAS_ANYURI, |
| 361 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 362 | xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 363 | XML_SCHEMAS_HEXBINARY, |
| 364 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 365 | xmlSchemaTypeBase64BinaryDef |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 366 | = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY, |
| 367 | xmlSchemaTypeAnySimpleTypeDef); |
| 368 | xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION", |
| 369 | XML_SCHEMAS_NOTATION, |
| 370 | xmlSchemaTypeAnySimpleTypeDef); |
| 371 | xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName", |
| 372 | XML_SCHEMAS_QNAME, |
| 373 | xmlSchemaTypeAnySimpleTypeDef); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 374 | |
Daniel Veillard | c6e997c | 2003-01-27 12:35:42 +0000 | [diff] [blame] | 375 | /* |
| 376 | * derived datatypes |
| 377 | */ |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 378 | xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 379 | XML_SCHEMAS_INTEGER, |
| 380 | xmlSchemaTypeDecimalDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 381 | xmlSchemaTypeNonPositiveIntegerDef = |
| 382 | xmlSchemaInitBasicType("nonPositiveInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 383 | XML_SCHEMAS_NPINTEGER, |
| 384 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 385 | xmlSchemaTypeNegativeIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 386 | xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER, |
| 387 | xmlSchemaTypeNonPositiveIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 388 | xmlSchemaTypeLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 389 | xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG, |
| 390 | xmlSchemaTypeIntegerDef); |
| 391 | xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT, |
| 392 | xmlSchemaTypeLongDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 393 | xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 394 | XML_SCHEMAS_SHORT, |
| 395 | xmlSchemaTypeIntDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 396 | xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 397 | XML_SCHEMAS_BYTE, |
| 398 | xmlSchemaTypeShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 399 | xmlSchemaTypeNonNegativeIntegerDef = |
| 400 | xmlSchemaInitBasicType("nonNegativeInteger", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 401 | XML_SCHEMAS_NNINTEGER, |
| 402 | xmlSchemaTypeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 403 | xmlSchemaTypeUnsignedLongDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 404 | xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG, |
| 405 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 406 | xmlSchemaTypeUnsignedIntDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 407 | xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT, |
| 408 | xmlSchemaTypeUnsignedLongDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 409 | xmlSchemaTypeUnsignedShortDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 410 | xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT, |
| 411 | xmlSchemaTypeUnsignedIntDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 412 | xmlSchemaTypeUnsignedByteDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 413 | xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE, |
| 414 | xmlSchemaTypeUnsignedShortDef); |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 415 | xmlSchemaTypePositiveIntegerDef = |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 416 | xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER, |
| 417 | xmlSchemaTypeNonNegativeIntegerDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 418 | xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 419 | XML_SCHEMAS_NORMSTRING, |
| 420 | xmlSchemaTypeStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 421 | xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 422 | XML_SCHEMAS_TOKEN, |
| 423 | xmlSchemaTypeNormStringDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 424 | xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 425 | XML_SCHEMAS_LANGUAGE, |
| 426 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 427 | xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 428 | XML_SCHEMAS_NAME, |
| 429 | xmlSchemaTypeTokenDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 430 | xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 431 | XML_SCHEMAS_NMTOKEN, |
| 432 | xmlSchemaTypeTokenDef); |
| 433 | xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName", |
| 434 | XML_SCHEMAS_NCNAME, |
| 435 | xmlSchemaTypeNameDef); |
| 436 | xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID, |
| 437 | xmlSchemaTypeNCNameDef); |
| 438 | xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF", |
| 439 | XML_SCHEMAS_IDREF, |
| 440 | xmlSchemaTypeNCNameDef); |
| 441 | xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS", |
| 442 | XML_SCHEMAS_IDREFS, |
| 443 | xmlSchemaTypeIdrefDef); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 444 | xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS", |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 445 | XML_SCHEMAS_NMTOKENS, |
| 446 | xmlSchemaTypeNmtokenDef); |
| 447 | xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY", |
| 448 | XML_SCHEMAS_ENTITY, |
| 449 | xmlSchemaTypeNCNameDef); |
| 450 | xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES", |
| 451 | XML_SCHEMAS_ENTITIES, |
| 452 | xmlSchemaTypeNCNameDef); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 453 | xmlSchemaTypesInitialized = 1; |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * xmlSchemaCleanupTypes: |
| 458 | * |
| 459 | * Cleanup the default XML Schemas type library |
| 460 | */ |
| 461 | void |
| 462 | xmlSchemaCleanupTypes(void) { |
| 463 | if (xmlSchemaTypesInitialized == 0) |
| 464 | return; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 465 | xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef->attributeWildcard); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 466 | xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType); |
| 467 | xmlSchemaTypesInitialized = 0; |
| 468 | } |
| 469 | |
| 470 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 471 | * xmlSchemaIsBuiltInTypeFacet: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 472 | * @type: the built-in type |
| 473 | * @facetType: the facet type |
| 474 | * |
| 475 | * Evaluates if a specific facet can be |
| 476 | * used in conjunction with a type. |
| 477 | * |
| 478 | * Returns 1 if the facet can be used with the given built-in type, |
| 479 | * 0 otherwise and -1 in case the type is not a built-in type. |
| 480 | */ |
| 481 | int |
| 482 | xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type, int facetType) |
| 483 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 484 | if (type == NULL) |
| 485 | return (-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 486 | if (type->type != XML_SCHEMA_TYPE_BASIC) |
| 487 | return (-1); |
| 488 | switch (type->builtInType) { |
| 489 | case XML_SCHEMAS_BOOLEAN: |
| 490 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 491 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 492 | return (1); |
| 493 | else |
| 494 | return (0); |
| 495 | case XML_SCHEMAS_STRING: |
| 496 | case XML_SCHEMAS_NOTATION: |
| 497 | case XML_SCHEMAS_QNAME: |
| 498 | case XML_SCHEMAS_ANYURI: |
| 499 | case XML_SCHEMAS_BASE64BINARY: |
| 500 | case XML_SCHEMAS_HEXBINARY: |
| 501 | if ((facetType == XML_SCHEMA_FACET_LENGTH) || |
| 502 | (facetType == XML_SCHEMA_FACET_MINLENGTH) || |
| 503 | (facetType == XML_SCHEMA_FACET_MAXLENGTH) || |
| 504 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 505 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 506 | (facetType == XML_SCHEMA_FACET_WHITESPACE)) |
| 507 | return (1); |
| 508 | else |
| 509 | return (0); |
| 510 | case XML_SCHEMAS_DECIMAL: |
| 511 | if ((facetType == XML_SCHEMA_FACET_TOTALDIGITS) || |
| 512 | (facetType == XML_SCHEMA_FACET_FRACTIONDIGITS) || |
| 513 | (facetType == XML_SCHEMA_FACET_PATTERN) || |
| 514 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 515 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 516 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 517 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 518 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 519 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 520 | return (1); |
| 521 | else |
| 522 | return (0); |
| 523 | case XML_SCHEMAS_TIME: |
| 524 | case XML_SCHEMAS_GDAY: |
| 525 | case XML_SCHEMAS_GMONTH: |
| 526 | case XML_SCHEMAS_GMONTHDAY: |
| 527 | case XML_SCHEMAS_GYEAR: |
| 528 | case XML_SCHEMAS_GYEARMONTH: |
| 529 | case XML_SCHEMAS_DATE: |
| 530 | case XML_SCHEMAS_DATETIME: |
| 531 | case XML_SCHEMAS_DURATION: |
| 532 | case XML_SCHEMAS_FLOAT: |
| 533 | case XML_SCHEMAS_DOUBLE: |
| 534 | if ((facetType == XML_SCHEMA_FACET_PATTERN) || |
| 535 | (facetType == XML_SCHEMA_FACET_ENUMERATION) || |
| 536 | (facetType == XML_SCHEMA_FACET_WHITESPACE) || |
| 537 | (facetType == XML_SCHEMA_FACET_MAXINCLUSIVE) || |
| 538 | (facetType == XML_SCHEMA_FACET_MAXEXCLUSIVE) || |
| 539 | (facetType == XML_SCHEMA_FACET_MININCLUSIVE) || |
| 540 | (facetType == XML_SCHEMA_FACET_MINEXCLUSIVE)) |
| 541 | return (1); |
| 542 | else |
| 543 | return (0); |
| 544 | default: |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 545 | break; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 546 | } |
| 547 | return (0); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * xmlSchemaGetBuiltInType: |
| 552 | * @type: the type of the built in type |
| 553 | * |
| 554 | * Gives you the type struct for a built-in |
| 555 | * type by its type id. |
| 556 | * |
| 557 | * Returns the type if found, NULL otherwise. |
| 558 | */ |
| 559 | xmlSchemaTypePtr |
| 560 | xmlSchemaGetBuiltInType(xmlSchemaValType type) |
| 561 | { |
| 562 | if (xmlSchemaTypesInitialized == 0) |
| 563 | xmlSchemaInitTypes(); |
| 564 | switch (type) { |
| 565 | |
| 566 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 567 | return (xmlSchemaTypeAnySimpleTypeDef); |
| 568 | case XML_SCHEMAS_STRING: |
| 569 | return (xmlSchemaTypeStringDef); |
| 570 | case XML_SCHEMAS_NORMSTRING: |
| 571 | return (xmlSchemaTypeNormStringDef); |
| 572 | case XML_SCHEMAS_DECIMAL: |
| 573 | return (xmlSchemaTypeDecimalDef); |
| 574 | case XML_SCHEMAS_TIME: |
| 575 | return (xmlSchemaTypeTimeDef); |
| 576 | case XML_SCHEMAS_GDAY: |
| 577 | return (xmlSchemaTypeGDayDef); |
| 578 | case XML_SCHEMAS_GMONTH: |
| 579 | return (xmlSchemaTypeGMonthDef); |
| 580 | case XML_SCHEMAS_GMONTHDAY: |
| 581 | return (xmlSchemaTypeGMonthDayDef); |
| 582 | case XML_SCHEMAS_GYEAR: |
| 583 | return (xmlSchemaTypeGYearDef); |
| 584 | case XML_SCHEMAS_GYEARMONTH: |
| 585 | return (xmlSchemaTypeGYearMonthDef); |
| 586 | case XML_SCHEMAS_DATE: |
| 587 | return (xmlSchemaTypeDateDef); |
| 588 | case XML_SCHEMAS_DATETIME: |
| 589 | return (xmlSchemaTypeDatetimeDef); |
| 590 | case XML_SCHEMAS_DURATION: |
| 591 | return (xmlSchemaTypeDurationDef); |
| 592 | case XML_SCHEMAS_FLOAT: |
| 593 | return (xmlSchemaTypeFloatDef); |
| 594 | case XML_SCHEMAS_DOUBLE: |
| 595 | return (xmlSchemaTypeDoubleDef); |
| 596 | case XML_SCHEMAS_BOOLEAN: |
| 597 | return (xmlSchemaTypeBooleanDef); |
| 598 | case XML_SCHEMAS_TOKEN: |
| 599 | return (xmlSchemaTypeTokenDef); |
| 600 | case XML_SCHEMAS_LANGUAGE: |
| 601 | return (xmlSchemaTypeLanguageDef); |
| 602 | case XML_SCHEMAS_NMTOKEN: |
| 603 | return (xmlSchemaTypeNmtokenDef); |
| 604 | case XML_SCHEMAS_NMTOKENS: |
| 605 | return (xmlSchemaTypeNmtokensDef); |
| 606 | case XML_SCHEMAS_NAME: |
| 607 | return (xmlSchemaTypeNameDef); |
| 608 | case XML_SCHEMAS_QNAME: |
| 609 | return (xmlSchemaTypeQNameDef); |
| 610 | case XML_SCHEMAS_NCNAME: |
| 611 | return (xmlSchemaTypeNCNameDef); |
| 612 | case XML_SCHEMAS_ID: |
| 613 | return (xmlSchemaTypeIdDef); |
| 614 | case XML_SCHEMAS_IDREF: |
| 615 | return (xmlSchemaTypeIdrefDef); |
| 616 | case XML_SCHEMAS_IDREFS: |
| 617 | return (xmlSchemaTypeIdrefsDef); |
| 618 | case XML_SCHEMAS_ENTITY: |
| 619 | return (xmlSchemaTypeEntityDef); |
| 620 | case XML_SCHEMAS_ENTITIES: |
| 621 | return (xmlSchemaTypeEntitiesDef); |
| 622 | case XML_SCHEMAS_NOTATION: |
| 623 | return (xmlSchemaTypeNotationDef); |
| 624 | case XML_SCHEMAS_ANYURI: |
| 625 | return (xmlSchemaTypeAnyURIDef); |
| 626 | case XML_SCHEMAS_INTEGER: |
| 627 | return (xmlSchemaTypeIntegerDef); |
| 628 | case XML_SCHEMAS_NPINTEGER: |
| 629 | return (xmlSchemaTypeNonPositiveIntegerDef); |
| 630 | case XML_SCHEMAS_NINTEGER: |
| 631 | return (xmlSchemaTypeNegativeIntegerDef); |
| 632 | case XML_SCHEMAS_NNINTEGER: |
| 633 | return (xmlSchemaTypeNonNegativeIntegerDef); |
| 634 | case XML_SCHEMAS_PINTEGER: |
| 635 | return (xmlSchemaTypePositiveIntegerDef); |
| 636 | case XML_SCHEMAS_INT: |
| 637 | return (xmlSchemaTypeIntDef); |
| 638 | case XML_SCHEMAS_UINT: |
| 639 | return (xmlSchemaTypeUnsignedIntDef); |
| 640 | case XML_SCHEMAS_LONG: |
| 641 | return (xmlSchemaTypeLongDef); |
| 642 | case XML_SCHEMAS_ULONG: |
| 643 | return (xmlSchemaTypeUnsignedLongDef); |
| 644 | case XML_SCHEMAS_SHORT: |
| 645 | return (xmlSchemaTypeShortDef); |
| 646 | case XML_SCHEMAS_USHORT: |
| 647 | return (xmlSchemaTypeUnsignedShortDef); |
| 648 | case XML_SCHEMAS_BYTE: |
| 649 | return (xmlSchemaTypeByteDef); |
| 650 | case XML_SCHEMAS_UBYTE: |
| 651 | return (xmlSchemaTypeUnsignedByteDef); |
| 652 | case XML_SCHEMAS_HEXBINARY: |
| 653 | return (xmlSchemaTypeHexBinaryDef); |
| 654 | case XML_SCHEMAS_BASE64BINARY: |
| 655 | return (xmlSchemaTypeBase64BinaryDef); |
| 656 | case XML_SCHEMAS_ANYTYPE: |
| 657 | return (xmlSchemaTypeAnyTypeDef); |
| 658 | default: |
| 659 | return (NULL); |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 664 | * xmlSchemaNewValue: |
| 665 | * @type: the value type |
| 666 | * |
| 667 | * Allocate a new simple type value |
| 668 | * |
| 669 | * Returns a pointer to the new value or NULL in case of error |
| 670 | */ |
| 671 | static xmlSchemaValPtr |
| 672 | xmlSchemaNewValue(xmlSchemaValType type) { |
| 673 | xmlSchemaValPtr value; |
| 674 | |
| 675 | value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 676 | if (value == NULL) { |
| 677 | return(NULL); |
| 678 | } |
| 679 | memset(value, 0, sizeof(xmlSchemaVal)); |
| 680 | value->type = type; |
| 681 | return(value); |
| 682 | } |
| 683 | |
| 684 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 685 | * xmlSchemaNewStringValue: |
| 686 | * @type: the value type |
| 687 | * |
| 688 | * Allocate a new simple type value. The type can be |
| 689 | * of XML_SCHEMAS_STRING. |
| 690 | * |
| 691 | * Returns a pointer to the new value or NULL in case of error |
| 692 | */ |
| 693 | xmlSchemaValPtr |
| 694 | xmlSchemaNewStringValue(xmlSchemaValType type, |
| 695 | const xmlChar *value) |
| 696 | { |
| 697 | xmlSchemaValPtr val; |
| 698 | |
| 699 | if (type != XML_SCHEMAS_STRING) |
| 700 | return(NULL); |
| 701 | val = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal)); |
| 702 | if (val == NULL) { |
| 703 | return(NULL); |
| 704 | } |
| 705 | memset(val, 0, sizeof(xmlSchemaVal)); |
| 706 | val->type = type; |
| 707 | val->value.str = (xmlChar *) value; |
| 708 | return(val); |
| 709 | } |
| 710 | |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 711 | /** |
| 712 | * xmlSchemaNewNOTATIONValue: |
| 713 | * @type: the value type |
| 714 | * |
| 715 | * Allocate a new NOTATION value. |
| 716 | * |
| 717 | * Returns a pointer to the new value or NULL in case of error |
| 718 | */ |
| 719 | xmlSchemaValPtr |
| 720 | xmlSchemaNewNOTATIONValue(const xmlChar *name, |
| 721 | const xmlChar *ns) |
| 722 | { |
| 723 | xmlSchemaValPtr val; |
| 724 | |
| 725 | val = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 726 | if (val == NULL) |
| 727 | return (NULL); |
| 728 | |
| 729 | val->value.qname.name = name; |
| 730 | if (ns != NULL) |
| 731 | val->value.qname.uri = ns; |
| 732 | return(val); |
| 733 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 734 | |
| 735 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 736 | * xmlSchemaFreeValue: |
| 737 | * @value: the value to free |
| 738 | * |
| 739 | * Cleanup the default XML Schemas type library |
| 740 | */ |
| 741 | void |
| 742 | xmlSchemaFreeValue(xmlSchemaValPtr value) { |
| 743 | if (value == NULL) |
| 744 | return; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 745 | switch (value->type) { |
| 746 | case XML_SCHEMAS_STRING: |
| 747 | case XML_SCHEMAS_NORMSTRING: |
| 748 | case XML_SCHEMAS_TOKEN: |
| 749 | case XML_SCHEMAS_LANGUAGE: |
| 750 | case XML_SCHEMAS_NMTOKEN: |
| 751 | case XML_SCHEMAS_NMTOKENS: |
| 752 | case XML_SCHEMAS_NAME: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 753 | case XML_SCHEMAS_NCNAME: |
| 754 | case XML_SCHEMAS_ID: |
| 755 | case XML_SCHEMAS_IDREF: |
| 756 | case XML_SCHEMAS_IDREFS: |
| 757 | case XML_SCHEMAS_ENTITY: |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 758 | case XML_SCHEMAS_ENTITIES: |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 759 | case XML_SCHEMAS_ANYURI: |
| 760 | if (value->value.str != NULL) |
| 761 | xmlFree(value->value.str); |
| 762 | break; |
Kasimier T. Buchcik | b06b4de | 2005-02-17 19:00:23 +0000 | [diff] [blame] | 763 | case XML_SCHEMAS_NOTATION: |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 764 | case XML_SCHEMAS_QNAME: |
| 765 | if (value->value.qname.uri != NULL) |
| 766 | xmlFree(value->value.qname.uri); |
| 767 | if (value->value.qname.name != NULL) |
| 768 | xmlFree(value->value.qname.name); |
| 769 | break; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 770 | case XML_SCHEMAS_HEXBINARY: |
| 771 | if (value->value.hex.str != NULL) |
| 772 | xmlFree(value->value.hex.str); |
| 773 | break; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 774 | case XML_SCHEMAS_BASE64BINARY: |
| 775 | if (value->value.base64.str != NULL) |
| 776 | xmlFree(value->value.base64.str); |
| 777 | break; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 778 | default: |
| 779 | break; |
| 780 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 781 | xmlFree(value); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * xmlSchemaGetPredefinedType: |
| 786 | * @name: the type name |
| 787 | * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema" |
| 788 | * |
| 789 | * Lookup a type in the default XML Schemas type library |
| 790 | * |
| 791 | * Returns the type if found, NULL otherwise |
| 792 | */ |
| 793 | xmlSchemaTypePtr |
| 794 | xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) { |
| 795 | if (xmlSchemaTypesInitialized == 0) |
| 796 | xmlSchemaInitTypes(); |
| 797 | if (name == NULL) |
| 798 | return(NULL); |
| 799 | return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns)); |
| 800 | } |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 801 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 802 | /** |
| 803 | * xmlSchemaGetBuiltInListSimpleTypeItemType: |
| 804 | * @type: the built-in simple type. |
| 805 | * |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 806 | * Lookup function |
| 807 | * |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 808 | * Returns the item type of @type as defined by the built-in datatype |
| 809 | * 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] | 810 | */ |
| 811 | xmlSchemaTypePtr |
| 812 | xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type) |
| 813 | { |
Daniel Veillard | 4259532 | 2004-11-08 10:52:06 +0000 | [diff] [blame] | 814 | if ((type == NULL) || (type->type != XML_SCHEMA_TYPE_BASIC)) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 815 | return (NULL); |
| 816 | switch (type->builtInType) { |
| 817 | case XML_SCHEMAS_NMTOKENS: |
| 818 | return (xmlSchemaTypeNmtokenDef ); |
| 819 | case XML_SCHEMAS_IDREFS: |
| 820 | return (xmlSchemaTypeIdrefDef); |
| 821 | case XML_SCHEMAS_ENTITIES: |
| 822 | return (xmlSchemaTypeEntityDef); |
| 823 | default: |
| 824 | return (NULL); |
| 825 | } |
| 826 | } |
| 827 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 828 | /**************************************************************** |
| 829 | * * |
| 830 | * Convenience macros and functions * |
| 831 | * * |
| 832 | ****************************************************************/ |
| 833 | |
| 834 | #define IS_TZO_CHAR(c) \ |
| 835 | ((c == 0) || (c == 'Z') || (c == '+') || (c == '-')) |
| 836 | |
| 837 | #define VALID_YEAR(yr) (yr != 0) |
| 838 | #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12)) |
| 839 | /* VALID_DAY should only be used when month is unknown */ |
| 840 | #define VALID_DAY(day) ((day >= 1) && (day <= 31)) |
| 841 | #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23)) |
| 842 | #define VALID_MIN(min) ((min >= 0) && (min <= 59)) |
| 843 | #define VALID_SEC(sec) ((sec >= 0) && (sec < 60)) |
| 844 | #define VALID_TZO(tzo) ((tzo > -1440) && (tzo < 1440)) |
| 845 | #define IS_LEAP(y) \ |
| 846 | (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) |
| 847 | |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 848 | static const unsigned int daysInMonth[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 849 | { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 850 | static const unsigned int daysInMonthLeap[12] = |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 851 | { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
| 852 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 853 | #define MAX_DAYINMONTH(yr,mon) \ |
| 854 | (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1]) |
| 855 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 856 | #define VALID_MDAY(dt) \ |
| 857 | (IS_LEAP(dt->year) ? \ |
| 858 | (dt->day <= daysInMonthLeap[dt->mon - 1]) : \ |
| 859 | (dt->day <= daysInMonth[dt->mon - 1])) |
| 860 | |
| 861 | #define VALID_DATE(dt) \ |
| 862 | (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt)) |
| 863 | |
| 864 | #define VALID_TIME(dt) \ |
| 865 | (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \ |
| 866 | VALID_SEC(dt->sec) && VALID_TZO(dt->tzo)) |
| 867 | |
| 868 | #define VALID_DATETIME(dt) \ |
| 869 | (VALID_DATE(dt) && VALID_TIME(dt)) |
| 870 | |
| 871 | #define SECS_PER_MIN (60) |
| 872 | #define SECS_PER_HOUR (60 * SECS_PER_MIN) |
| 873 | #define SECS_PER_DAY (24 * SECS_PER_HOUR) |
| 874 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 875 | static const long dayInYearByMonth[12] = |
| 876 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; |
| 877 | static const long dayInLeapYearByMonth[12] = |
| 878 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }; |
| 879 | |
| 880 | #define DAY_IN_YEAR(day, month, year) \ |
| 881 | ((IS_LEAP(year) ? \ |
| 882 | dayInLeapYearByMonth[month - 1] : \ |
| 883 | dayInYearByMonth[month - 1]) + day) |
| 884 | |
| 885 | #ifdef DEBUG |
| 886 | #define DEBUG_DATE(dt) \ |
| 887 | xmlGenericError(xmlGenericErrorContext, \ |
| 888 | "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \ |
| 889 | dt->type,dt->value.date.year,dt->value.date.mon, \ |
| 890 | dt->value.date.day,dt->value.date.hour,dt->value.date.min, \ |
| 891 | dt->value.date.sec); \ |
| 892 | if (dt->value.date.tz_flag) \ |
| 893 | if (dt->value.date.tzo != 0) \ |
| 894 | xmlGenericError(xmlGenericErrorContext, \ |
| 895 | "%+05d\n",dt->value.date.tzo); \ |
| 896 | else \ |
| 897 | xmlGenericError(xmlGenericErrorContext, "Z\n"); \ |
| 898 | else \ |
| 899 | xmlGenericError(xmlGenericErrorContext,"\n") |
| 900 | #else |
| 901 | #define DEBUG_DATE(dt) |
| 902 | #endif |
| 903 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 904 | /** |
| 905 | * _xmlSchemaParseGYear: |
| 906 | * @dt: pointer to a date structure |
| 907 | * @str: pointer to the string to analyze |
| 908 | * |
| 909 | * Parses a xs:gYear without time zone and fills in the appropriate |
| 910 | * field of the @dt structure. @str is updated to point just after the |
| 911 | * xs:gYear. It is supposed that @dt->year is big enough to contain |
| 912 | * the year. |
| 913 | * |
| 914 | * Returns 0 or the error code |
| 915 | */ |
| 916 | static int |
| 917 | _xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 918 | const xmlChar *cur = *str, *firstChar; |
| 919 | int isneg = 0, digcnt = 0; |
| 920 | |
| 921 | if (((*cur < '0') || (*cur > '9')) && |
| 922 | (*cur != '-') && (*cur != '+')) |
| 923 | return -1; |
| 924 | |
| 925 | if (*cur == '-') { |
| 926 | isneg = 1; |
| 927 | cur++; |
| 928 | } |
| 929 | |
| 930 | firstChar = cur; |
| 931 | |
| 932 | while ((*cur >= '0') && (*cur <= '9')) { |
| 933 | dt->year = dt->year * 10 + (*cur - '0'); |
| 934 | cur++; |
| 935 | digcnt++; |
| 936 | } |
| 937 | |
| 938 | /* year must be at least 4 digits (CCYY); over 4 |
| 939 | * digits cannot have a leading zero. */ |
| 940 | if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0'))) |
| 941 | return 1; |
| 942 | |
| 943 | if (isneg) |
| 944 | dt->year = - dt->year; |
| 945 | |
| 946 | if (!VALID_YEAR(dt->year)) |
| 947 | return 2; |
| 948 | |
| 949 | *str = cur; |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | /** |
| 954 | * PARSE_2_DIGITS: |
| 955 | * @num: the integer to fill in |
| 956 | * @cur: an #xmlChar * |
| 957 | * @invalid: an integer |
| 958 | * |
| 959 | * Parses a 2-digits integer and updates @num with the value. @cur is |
| 960 | * updated to point just after the integer. |
| 961 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 962 | * @cur are undefined. |
| 963 | */ |
| 964 | #define PARSE_2_DIGITS(num, cur, invalid) \ |
| 965 | if ((cur[0] < '0') || (cur[0] > '9') || \ |
| 966 | (cur[1] < '0') || (cur[1] > '9')) \ |
| 967 | invalid = 1; \ |
| 968 | else \ |
| 969 | num = (cur[0] - '0') * 10 + (cur[1] - '0'); \ |
| 970 | cur += 2; |
| 971 | |
| 972 | /** |
| 973 | * PARSE_FLOAT: |
| 974 | * @num: the double to fill in |
| 975 | * @cur: an #xmlChar * |
| 976 | * @invalid: an integer |
| 977 | * |
| 978 | * Parses a float and updates @num with the value. @cur is |
| 979 | * updated to point just after the float. The float must have a |
| 980 | * 2-digits integer part and may or may not have a decimal part. |
| 981 | * In case of error, @invalid is set to %TRUE, values of @num and |
| 982 | * @cur are undefined. |
| 983 | */ |
| 984 | #define PARSE_FLOAT(num, cur, invalid) \ |
| 985 | PARSE_2_DIGITS(num, cur, invalid); \ |
| 986 | if (!invalid && (*cur == '.')) { \ |
| 987 | double mult = 1; \ |
| 988 | cur++; \ |
| 989 | if ((*cur < '0') || (*cur > '9')) \ |
| 990 | invalid = 1; \ |
| 991 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 992 | mult /= 10; \ |
| 993 | num += (*cur - '0') * mult; \ |
| 994 | cur++; \ |
| 995 | } \ |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * _xmlSchemaParseGMonth: |
| 1000 | * @dt: pointer to a date structure |
| 1001 | * @str: pointer to the string to analyze |
| 1002 | * |
| 1003 | * Parses a xs:gMonth without time zone and fills in the appropriate |
| 1004 | * field of the @dt structure. @str is updated to point just after the |
| 1005 | * xs:gMonth. |
| 1006 | * |
| 1007 | * Returns 0 or the error code |
| 1008 | */ |
| 1009 | static int |
| 1010 | _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1011 | const xmlChar *cur = *str; |
| 1012 | int ret = 0; |
| 1013 | |
| 1014 | PARSE_2_DIGITS(dt->mon, cur, ret); |
| 1015 | if (ret != 0) |
| 1016 | return ret; |
| 1017 | |
| 1018 | if (!VALID_MONTH(dt->mon)) |
| 1019 | return 2; |
| 1020 | |
| 1021 | *str = cur; |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * _xmlSchemaParseGDay: |
| 1027 | * @dt: pointer to a date structure |
| 1028 | * @str: pointer to the string to analyze |
| 1029 | * |
| 1030 | * Parses a xs:gDay without time zone and fills in the appropriate |
| 1031 | * field of the @dt structure. @str is updated to point just after the |
| 1032 | * xs:gDay. |
| 1033 | * |
| 1034 | * Returns 0 or the error code |
| 1035 | */ |
| 1036 | static int |
| 1037 | _xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1038 | const xmlChar *cur = *str; |
| 1039 | int ret = 0; |
| 1040 | |
| 1041 | PARSE_2_DIGITS(dt->day, cur, ret); |
| 1042 | if (ret != 0) |
| 1043 | return ret; |
| 1044 | |
| 1045 | if (!VALID_DAY(dt->day)) |
| 1046 | return 2; |
| 1047 | |
| 1048 | *str = cur; |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * _xmlSchemaParseTime: |
| 1054 | * @dt: pointer to a date structure |
| 1055 | * @str: pointer to the string to analyze |
| 1056 | * |
| 1057 | * Parses a xs:time without time zone and fills in the appropriate |
| 1058 | * fields of the @dt structure. @str is updated to point just after the |
| 1059 | * xs:time. |
| 1060 | * In case of error, values of @dt fields are undefined. |
| 1061 | * |
| 1062 | * Returns 0 or the error code |
| 1063 | */ |
| 1064 | static int |
| 1065 | _xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1066 | const xmlChar *cur = *str; |
| 1067 | unsigned int hour = 0; /* use temp var in case str is not xs:time */ |
| 1068 | int ret = 0; |
| 1069 | |
| 1070 | PARSE_2_DIGITS(hour, cur, ret); |
| 1071 | if (ret != 0) |
| 1072 | return ret; |
| 1073 | |
| 1074 | if (*cur != ':') |
| 1075 | return 1; |
| 1076 | cur++; |
| 1077 | |
| 1078 | /* the ':' insures this string is xs:time */ |
| 1079 | dt->hour = hour; |
| 1080 | |
| 1081 | PARSE_2_DIGITS(dt->min, cur, ret); |
| 1082 | if (ret != 0) |
| 1083 | return ret; |
| 1084 | |
| 1085 | if (*cur != ':') |
| 1086 | return 1; |
| 1087 | cur++; |
| 1088 | |
| 1089 | PARSE_FLOAT(dt->sec, cur, ret); |
| 1090 | if (ret != 0) |
| 1091 | return ret; |
| 1092 | |
| 1093 | if (!VALID_TIME(dt)) |
| 1094 | return 2; |
| 1095 | |
| 1096 | *str = cur; |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | /** |
| 1101 | * _xmlSchemaParseTimeZone: |
| 1102 | * @dt: pointer to a date structure |
| 1103 | * @str: pointer to the string to analyze |
| 1104 | * |
| 1105 | * Parses a time zone without time zone and fills in the appropriate |
| 1106 | * field of the @dt structure. @str is updated to point just after the |
| 1107 | * time zone. |
| 1108 | * |
| 1109 | * Returns 0 or the error code |
| 1110 | */ |
| 1111 | static int |
| 1112 | _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) { |
| 1113 | const xmlChar *cur = *str; |
| 1114 | int ret = 0; |
| 1115 | |
| 1116 | if (str == NULL) |
| 1117 | return -1; |
| 1118 | |
| 1119 | switch (*cur) { |
| 1120 | case 0: |
| 1121 | dt->tz_flag = 0; |
| 1122 | dt->tzo = 0; |
| 1123 | break; |
| 1124 | |
| 1125 | case 'Z': |
| 1126 | dt->tz_flag = 1; |
| 1127 | dt->tzo = 0; |
| 1128 | cur++; |
| 1129 | break; |
| 1130 | |
| 1131 | case '+': |
| 1132 | case '-': { |
| 1133 | int isneg = 0, tmp = 0; |
| 1134 | isneg = (*cur == '-'); |
| 1135 | |
| 1136 | cur++; |
| 1137 | |
| 1138 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1139 | if (ret != 0) |
| 1140 | return ret; |
| 1141 | if (!VALID_HOUR(tmp)) |
| 1142 | return 2; |
| 1143 | |
| 1144 | if (*cur != ':') |
| 1145 | return 1; |
| 1146 | cur++; |
| 1147 | |
| 1148 | dt->tzo = tmp * 60; |
| 1149 | |
| 1150 | PARSE_2_DIGITS(tmp, cur, ret); |
| 1151 | if (ret != 0) |
| 1152 | return ret; |
| 1153 | if (!VALID_MIN(tmp)) |
| 1154 | return 2; |
| 1155 | |
| 1156 | dt->tzo += tmp; |
| 1157 | if (isneg) |
| 1158 | dt->tzo = - dt->tzo; |
| 1159 | |
| 1160 | if (!VALID_TZO(dt->tzo)) |
| 1161 | return 2; |
| 1162 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1163 | dt->tz_flag = 1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1164 | break; |
| 1165 | } |
| 1166 | default: |
| 1167 | return 1; |
| 1168 | } |
| 1169 | |
| 1170 | *str = cur; |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1174 | /** |
| 1175 | * _xmlSchemaBase64Decode: |
| 1176 | * @ch: a character |
| 1177 | * |
| 1178 | * Converts a base64 encoded character to its base 64 value. |
| 1179 | * |
| 1180 | * Returns 0-63 (value), 64 (pad), or -1 (not recognized) |
| 1181 | */ |
| 1182 | static int |
| 1183 | _xmlSchemaBase64Decode (const xmlChar ch) { |
| 1184 | if (('A' <= ch) && (ch <= 'Z')) return ch - 'A'; |
| 1185 | if (('a' <= ch) && (ch <= 'z')) return ch - 'a' + 26; |
| 1186 | if (('0' <= ch) && (ch <= '9')) return ch - '0' + 52; |
| 1187 | if ('+' == ch) return 62; |
| 1188 | if ('/' == ch) return 63; |
| 1189 | if ('=' == ch) return 64; |
| 1190 | return -1; |
| 1191 | } |
| 1192 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1193 | /**************************************************************** |
| 1194 | * * |
| 1195 | * XML Schema Dates/Times Datatypes Handling * |
| 1196 | * * |
| 1197 | ****************************************************************/ |
| 1198 | |
| 1199 | /** |
| 1200 | * PARSE_DIGITS: |
| 1201 | * @num: the integer to fill in |
| 1202 | * @cur: an #xmlChar * |
| 1203 | * @num_type: an integer flag |
| 1204 | * |
| 1205 | * Parses a digits integer and updates @num with the value. @cur is |
| 1206 | * updated to point just after the integer. |
| 1207 | * In case of error, @num_type is set to -1, values of @num and |
| 1208 | * @cur are undefined. |
| 1209 | */ |
| 1210 | #define PARSE_DIGITS(num, cur, num_type) \ |
| 1211 | if ((*cur < '0') || (*cur > '9')) \ |
| 1212 | num_type = -1; \ |
| 1213 | else \ |
| 1214 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1215 | num = num * 10 + (*cur - '0'); \ |
| 1216 | cur++; \ |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * PARSE_NUM: |
| 1221 | * @num: the double to fill in |
| 1222 | * @cur: an #xmlChar * |
| 1223 | * @num_type: an integer flag |
| 1224 | * |
| 1225 | * Parses a float or integer and updates @num with the value. @cur is |
| 1226 | * updated to point just after the number. If the number is a float, |
| 1227 | * then it must have an integer part and a decimal part; @num_type will |
| 1228 | * be set to 1. If there is no decimal part, @num_type is set to zero. |
| 1229 | * In case of error, @num_type is set to -1, values of @num and |
| 1230 | * @cur are undefined. |
| 1231 | */ |
| 1232 | #define PARSE_NUM(num, cur, num_type) \ |
| 1233 | num = 0; \ |
| 1234 | PARSE_DIGITS(num, cur, num_type); \ |
| 1235 | if (!num_type && (*cur == '.')) { \ |
| 1236 | double mult = 1; \ |
| 1237 | cur++; \ |
| 1238 | if ((*cur < '0') || (*cur > '9')) \ |
| 1239 | num_type = -1; \ |
| 1240 | else \ |
| 1241 | num_type = 1; \ |
| 1242 | while ((*cur >= '0') && (*cur <= '9')) { \ |
| 1243 | mult /= 10; \ |
| 1244 | num += (*cur - '0') * mult; \ |
| 1245 | cur++; \ |
| 1246 | } \ |
| 1247 | } |
| 1248 | |
| 1249 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1250 | * xmlSchemaValidateDates: |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1251 | * @type: the expected type or XML_SCHEMAS_UNKNOWN |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1252 | * @dateTime: string to analyze |
| 1253 | * @val: the return computed value |
| 1254 | * |
| 1255 | * Check that @dateTime conforms to the lexical space of one of the date types. |
| 1256 | * if true a value is computed and returned in @val. |
| 1257 | * |
| 1258 | * Returns 0 if this validates, a positive error code number otherwise |
| 1259 | * and -1 in case of internal or API error. |
| 1260 | */ |
| 1261 | static int |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1262 | xmlSchemaValidateDates (xmlSchemaValType type, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1263 | const xmlChar *dateTime, xmlSchemaValPtr *val) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1264 | xmlSchemaValPtr dt; |
| 1265 | int ret; |
| 1266 | const xmlChar *cur = dateTime; |
| 1267 | |
| 1268 | #define RETURN_TYPE_IF_VALID(t) \ |
| 1269 | if (IS_TZO_CHAR(*cur)) { \ |
| 1270 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \ |
| 1271 | if (ret == 0) { \ |
| 1272 | if (*cur != 0) \ |
| 1273 | goto error; \ |
| 1274 | dt->type = t; \ |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1275 | goto done; \ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1276 | } \ |
| 1277 | } |
| 1278 | |
| 1279 | if (dateTime == NULL) |
| 1280 | return -1; |
| 1281 | |
| 1282 | if ((*cur != '-') && (*cur < '0') && (*cur > '9')) |
| 1283 | return 1; |
| 1284 | |
| 1285 | dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN); |
| 1286 | if (dt == NULL) |
| 1287 | return -1; |
| 1288 | |
| 1289 | if ((cur[0] == '-') && (cur[1] == '-')) { |
| 1290 | /* |
| 1291 | * It's an incomplete date (xs:gMonthDay, xs:gMonth or |
| 1292 | * xs:gDay) |
| 1293 | */ |
| 1294 | cur += 2; |
| 1295 | |
| 1296 | /* is it an xs:gDay? */ |
| 1297 | if (*cur == '-') { |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1298 | if (type == XML_SCHEMAS_GMONTH) |
| 1299 | goto error; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1300 | ++cur; |
| 1301 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1302 | if (ret != 0) |
| 1303 | goto error; |
| 1304 | |
| 1305 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY); |
| 1306 | |
| 1307 | goto error; |
| 1308 | } |
| 1309 | |
| 1310 | /* |
| 1311 | * it should be an xs:gMonthDay or xs:gMonth |
| 1312 | */ |
| 1313 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1314 | if (ret != 0) |
| 1315 | goto error; |
| 1316 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1317 | /* |
| 1318 | * a '-' char could indicate this type is xs:gMonthDay or |
| 1319 | * a negative time zone offset. Check for xs:gMonthDay first. |
| 1320 | * Also the first three char's of a negative tzo (-MM:SS) can |
| 1321 | * appear to be a valid day; so even if the day portion |
| 1322 | * of the xs:gMonthDay verifies, we must insure it was not |
| 1323 | * a tzo. |
| 1324 | */ |
| 1325 | if (*cur == '-') { |
| 1326 | const xmlChar *rewnd = cur; |
| 1327 | cur++; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1328 | |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1329 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1330 | if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) { |
| 1331 | |
| 1332 | /* |
| 1333 | * we can use the VALID_MDAY macro to validate the month |
| 1334 | * and day because the leap year test will flag year zero |
| 1335 | * as a leap year (even though zero is an invalid year). |
| 1336 | */ |
| 1337 | if (VALID_MDAY((&(dt->value.date)))) { |
| 1338 | |
| 1339 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY); |
| 1340 | |
| 1341 | goto error; |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * not xs:gMonthDay so rewind and check if just xs:gMonth |
| 1347 | * with an optional time zone. |
| 1348 | */ |
| 1349 | cur = rewnd; |
| 1350 | } |
| 1351 | |
| 1352 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1353 | |
| 1354 | goto error; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * It's a right-truncated date or an xs:time. |
| 1359 | * Try to parse an xs:time then fallback on right-truncated dates. |
| 1360 | */ |
| 1361 | if ((*cur >= '0') && (*cur <= '9')) { |
| 1362 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1363 | if (ret == 0) { |
| 1364 | /* it's an xs:time */ |
| 1365 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | /* fallback on date parsing */ |
| 1370 | cur = dateTime; |
| 1371 | |
| 1372 | ret = _xmlSchemaParseGYear(&(dt->value.date), &cur); |
| 1373 | if (ret != 0) |
| 1374 | goto error; |
| 1375 | |
| 1376 | /* is it an xs:gYear? */ |
| 1377 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR); |
| 1378 | |
| 1379 | if (*cur != '-') |
| 1380 | goto error; |
| 1381 | cur++; |
| 1382 | |
| 1383 | ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur); |
| 1384 | if (ret != 0) |
| 1385 | goto error; |
| 1386 | |
| 1387 | /* is it an xs:gYearMonth? */ |
| 1388 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH); |
| 1389 | |
| 1390 | if (*cur != '-') |
| 1391 | goto error; |
| 1392 | cur++; |
| 1393 | |
| 1394 | ret = _xmlSchemaParseGDay(&(dt->value.date), &cur); |
| 1395 | if ((ret != 0) || !VALID_DATE((&(dt->value.date)))) |
| 1396 | goto error; |
| 1397 | |
| 1398 | /* is it an xs:date? */ |
| 1399 | RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE); |
| 1400 | |
| 1401 | if (*cur != 'T') |
| 1402 | goto error; |
| 1403 | cur++; |
| 1404 | |
| 1405 | /* it should be an xs:dateTime */ |
| 1406 | ret = _xmlSchemaParseTime(&(dt->value.date), &cur); |
| 1407 | if (ret != 0) |
| 1408 | goto error; |
| 1409 | |
| 1410 | ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); |
| 1411 | if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date)))) |
| 1412 | goto error; |
| 1413 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1414 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1415 | dt->type = XML_SCHEMAS_DATETIME; |
| 1416 | |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1417 | done: |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1418 | #if 1 |
| 1419 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) |
| 1420 | goto error; |
| 1421 | #else |
| 1422 | /* |
| 1423 | * insure the parsed type is equal to or less significant (right |
| 1424 | * truncated) than the desired type. |
| 1425 | */ |
| 1426 | if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) { |
| 1427 | |
| 1428 | /* time only matches time */ |
| 1429 | if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME)) |
| 1430 | goto error; |
| 1431 | |
| 1432 | if ((type == XML_SCHEMAS_DATETIME) && |
| 1433 | ((dt->type != XML_SCHEMAS_DATE) || |
| 1434 | (dt->type != XML_SCHEMAS_GYEARMONTH) || |
| 1435 | (dt->type != XML_SCHEMAS_GYEAR))) |
| 1436 | goto error; |
| 1437 | |
| 1438 | if ((type == XML_SCHEMAS_DATE) && |
| 1439 | ((dt->type != XML_SCHEMAS_GYEAR) || |
| 1440 | (dt->type != XML_SCHEMAS_GYEARMONTH))) |
| 1441 | goto error; |
| 1442 | |
| 1443 | if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR)) |
| 1444 | goto error; |
| 1445 | |
| 1446 | if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH)) |
| 1447 | goto error; |
| 1448 | } |
Daniel Veillard | 455cc07 | 2003-03-31 10:13:23 +0000 | [diff] [blame] | 1449 | #endif |
| 1450 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1451 | if (val != NULL) |
| 1452 | *val = dt; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1453 | else |
| 1454 | xmlSchemaFreeValue(dt); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1455 | |
| 1456 | return 0; |
| 1457 | |
| 1458 | error: |
| 1459 | if (dt != NULL) |
| 1460 | xmlSchemaFreeValue(dt); |
| 1461 | return 1; |
| 1462 | } |
| 1463 | |
| 1464 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 1465 | * xmlSchemaValidateDuration: |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1466 | * @type: the predefined type |
| 1467 | * @duration: string to analyze |
| 1468 | * @val: the return computed value |
| 1469 | * |
| 1470 | * Check that @duration conforms to the lexical space of the duration type. |
| 1471 | * if true a value is computed and returned in @val. |
| 1472 | * |
| 1473 | * Returns 0 if this validates, a positive error code number otherwise |
| 1474 | * and -1 in case of internal or API error. |
| 1475 | */ |
| 1476 | static int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 1477 | xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 1478 | const xmlChar *duration, xmlSchemaValPtr *val) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1479 | const xmlChar *cur = duration; |
| 1480 | xmlSchemaValPtr dur; |
| 1481 | int isneg = 0; |
| 1482 | unsigned int seq = 0; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1483 | double num; |
| 1484 | int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */ |
| 1485 | const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'}; |
| 1486 | 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] | 1487 | |
| 1488 | if (duration == NULL) |
| 1489 | return -1; |
| 1490 | |
| 1491 | if (*cur == '-') { |
| 1492 | isneg = 1; |
| 1493 | cur++; |
| 1494 | } |
| 1495 | |
| 1496 | /* duration must start with 'P' (after sign) */ |
| 1497 | if (*cur++ != 'P') |
| 1498 | return 1; |
| 1499 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1500 | if (*cur == 0) |
| 1501 | return 1; |
| 1502 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1503 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 1504 | if (dur == NULL) |
| 1505 | return -1; |
| 1506 | |
| 1507 | while (*cur != 0) { |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1508 | |
| 1509 | /* input string should be empty or invalid date/time item */ |
| 1510 | if (seq >= sizeof(desig)) |
| 1511 | goto error; |
| 1512 | |
| 1513 | /* T designator must be present for time items */ |
| 1514 | if (*cur == 'T') { |
| 1515 | if (seq <= 3) { |
| 1516 | seq = 3; |
| 1517 | cur++; |
| 1518 | } else |
| 1519 | return 1; |
| 1520 | } else if (seq == 3) |
| 1521 | goto error; |
| 1522 | |
| 1523 | /* parse the number portion of the item */ |
| 1524 | PARSE_NUM(num, cur, num_type); |
| 1525 | |
| 1526 | if ((num_type == -1) || (*cur == 0)) |
| 1527 | goto error; |
| 1528 | |
| 1529 | /* update duration based on item type */ |
| 1530 | while (seq < sizeof(desig)) { |
| 1531 | if (*cur == desig[seq]) { |
| 1532 | |
| 1533 | /* verify numeric type; only seconds can be float */ |
| 1534 | if ((num_type != 0) && (seq < (sizeof(desig)-1))) |
| 1535 | goto error; |
| 1536 | |
| 1537 | switch (seq) { |
| 1538 | case 0: |
| 1539 | dur->value.dur.mon = (long)num * 12; |
| 1540 | break; |
| 1541 | case 1: |
| 1542 | dur->value.dur.mon += (long)num; |
| 1543 | break; |
| 1544 | default: |
| 1545 | /* convert to seconds using multiplier */ |
| 1546 | dur->value.dur.sec += num * multi[seq]; |
| 1547 | seq++; |
| 1548 | break; |
| 1549 | } |
| 1550 | |
| 1551 | break; /* exit loop */ |
| 1552 | } |
| 1553 | /* no date designators found? */ |
| 1554 | if (++seq == 3) |
| 1555 | goto error; |
| 1556 | } |
| 1557 | cur++; |
| 1558 | } |
| 1559 | |
| 1560 | if (isneg) { |
| 1561 | dur->value.dur.mon = -dur->value.dur.mon; |
| 1562 | dur->value.dur.day = -dur->value.dur.day; |
| 1563 | dur->value.dur.sec = -dur->value.dur.sec; |
| 1564 | } |
| 1565 | |
| 1566 | if (val != NULL) |
| 1567 | *val = dur; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 1568 | else |
| 1569 | xmlSchemaFreeValue(dur); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 1570 | |
| 1571 | return 0; |
| 1572 | |
| 1573 | error: |
| 1574 | if (dur != NULL) |
| 1575 | xmlSchemaFreeValue(dur); |
| 1576 | return 1; |
| 1577 | } |
| 1578 | |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1579 | /** |
| 1580 | * xmlSchemaStrip: |
| 1581 | * @value: a value |
| 1582 | * |
| 1583 | * Removes the leading and ending spaces of a string |
| 1584 | * |
| 1585 | * Returns the new string or NULL if no change was required. |
| 1586 | */ |
| 1587 | static xmlChar * |
| 1588 | xmlSchemaStrip(const xmlChar *value) { |
| 1589 | const xmlChar *start = value, *end, *f; |
| 1590 | |
| 1591 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1592 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1593 | end = start; |
| 1594 | while (*end != 0) end++; |
| 1595 | f = end; |
| 1596 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1597 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1598 | end++; |
| 1599 | if ((start == value) && (f == end)) return(NULL); |
| 1600 | return(xmlStrndup(start, end - start)); |
| 1601 | } |
Daniel Veillard | 96a4b25 | 2003-02-06 08:22:32 +0000 | [diff] [blame] | 1602 | |
| 1603 | /** |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1604 | * xmlSchemaWhiteSpaceReplace: |
| 1605 | * @value: a value |
| 1606 | * |
| 1607 | * Replaces 0xd, 0x9 and 0xa with a space. |
| 1608 | * |
| 1609 | * Returns the new string or NULL if no change was required. |
| 1610 | */ |
| 1611 | xmlChar * |
| 1612 | xmlSchemaWhiteSpaceReplace(const xmlChar *value) { |
| 1613 | const xmlChar *cur = value; |
| 1614 | xmlChar *ret = NULL, *mcur; |
| 1615 | |
| 1616 | if (value == NULL) |
| 1617 | return(NULL); |
| 1618 | |
| 1619 | while ((*cur != 0) && |
| 1620 | (((*cur) != 0xd) && ((*cur) != 0x9) && ((*cur) != 0xa))) { |
| 1621 | cur++; |
| 1622 | } |
| 1623 | if (*cur == 0) |
| 1624 | return (NULL); |
| 1625 | ret = xmlStrdup(value); |
| 1626 | /* TODO FIXME: I guess gcc will bark at this. */ |
| 1627 | mcur = (xmlChar *) (ret + (cur - value)); |
| 1628 | do { |
| 1629 | if ( ((*mcur) == 0xd) || ((*mcur) == 0x9) || ((*mcur) == 0xa) ) |
| 1630 | *mcur = ' '; |
| 1631 | mcur++; |
| 1632 | } while (*mcur != 0); |
| 1633 | return(ret); |
| 1634 | } |
| 1635 | |
| 1636 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1637 | * xmlSchemaCollapseString: |
| 1638 | * @value: a value |
| 1639 | * |
| 1640 | * Removes and normalize white spaces in the string |
| 1641 | * |
| 1642 | * Returns the new string or NULL if no change was required. |
| 1643 | */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1644 | xmlChar * |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1645 | xmlSchemaCollapseString(const xmlChar *value) { |
| 1646 | const xmlChar *start = value, *end, *f; |
| 1647 | xmlChar *g; |
| 1648 | int col = 0; |
| 1649 | |
| 1650 | if (value == NULL) return(NULL); |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1651 | while ((*start != 0) && (IS_BLANK_CH(*start))) start++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1652 | end = start; |
| 1653 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1654 | if ((*end == ' ') && (IS_BLANK_CH(end[1]))) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1655 | col = end - start; |
| 1656 | break; |
| 1657 | } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) { |
| 1658 | col = end - start; |
| 1659 | break; |
| 1660 | } |
| 1661 | end++; |
| 1662 | } |
| 1663 | if (col == 0) { |
| 1664 | f = end; |
| 1665 | end--; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1666 | while ((end > start) && (IS_BLANK_CH(*end))) end--; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1667 | end++; |
| 1668 | if ((start == value) && (f == end)) return(NULL); |
| 1669 | return(xmlStrndup(start, end - start)); |
| 1670 | } |
| 1671 | start = xmlStrdup(start); |
| 1672 | if (start == NULL) return(NULL); |
| 1673 | g = (xmlChar *) (start + col); |
| 1674 | end = g; |
| 1675 | while (*end != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1676 | if (IS_BLANK_CH(*end)) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1677 | end++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1678 | while (IS_BLANK_CH(*end)) end++; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1679 | if (*end != 0) |
| 1680 | *g++ = ' '; |
| 1681 | } else |
| 1682 | *g++ = *end++; |
| 1683 | } |
| 1684 | *g = 0; |
| 1685 | return((xmlChar *) start); |
| 1686 | } |
| 1687 | |
| 1688 | /** |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1689 | * xmlSchemaValAtomicListNode: |
| 1690 | * @type: the predefined atomic type for a token in the list |
| 1691 | * @value: the list value to check |
| 1692 | * @ret: the return computed value |
| 1693 | * @node: the node containing the value |
| 1694 | * |
| 1695 | * Check that a value conforms to the lexical space of the predefined |
| 1696 | * list type. if true a value is computed and returned in @ret. |
| 1697 | * |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1698 | * Returns the number of items if this validates, a negative error code |
| 1699 | * number otherwise |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1700 | */ |
| 1701 | static int |
| 1702 | xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 1703 | xmlSchemaValPtr *ret, xmlNodePtr node) { |
| 1704 | xmlChar *val, *cur, *endval; |
| 1705 | int nb_values = 0; |
Daniel Veillard | 580ced8 | 2003-03-21 21:22:48 +0000 | [diff] [blame] | 1706 | int tmp = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1707 | |
| 1708 | if (value == NULL) { |
| 1709 | return(-1); |
| 1710 | } |
| 1711 | val = xmlStrdup(value); |
| 1712 | if (val == NULL) { |
| 1713 | return(-1); |
| 1714 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 1715 | if (ret != NULL) { |
| 1716 | *ret = NULL; |
| 1717 | } |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1718 | cur = val; |
| 1719 | /* |
| 1720 | * Split the list |
| 1721 | */ |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1722 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1723 | while (*cur != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1724 | if (IS_BLANK_CH(*cur)) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1725 | *cur = 0; |
| 1726 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1727 | while (IS_BLANK_CH(*cur)) *cur++ = 0; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1728 | } else { |
| 1729 | nb_values++; |
| 1730 | cur++; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 1731 | while ((*cur != 0) && (!IS_BLANK_CH(*cur))) cur++; |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1732 | } |
| 1733 | } |
| 1734 | if (nb_values == 0) { |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1735 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1736 | return(nb_values); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1737 | } |
| 1738 | endval = cur; |
| 1739 | cur = val; |
| 1740 | while ((*cur == 0) && (cur != endval)) cur++; |
| 1741 | while (cur != endval) { |
| 1742 | tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node); |
| 1743 | if (tmp != 0) |
| 1744 | break; |
| 1745 | while (*cur != 0) cur++; |
| 1746 | while ((*cur == 0) && (cur != endval)) cur++; |
| 1747 | } |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 1748 | /* TODO what return value ? c.f. bug #158628 |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1749 | if (ret != NULL) { |
| 1750 | TODO |
Daniel Veillard | 6fc5db0 | 2005-01-16 00:05:58 +0000 | [diff] [blame] | 1751 | } */ |
| 1752 | xmlFree(val); |
Daniel Veillard | a1a9d04 | 2003-03-18 16:53:17 +0000 | [diff] [blame] | 1753 | if (tmp == 0) |
| 1754 | return(nb_values); |
| 1755 | return(-1); |
Daniel Veillard | 28c52ab | 2003-03-18 11:39:17 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | /** |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 1759 | * xmlSchemaParseUInt: |
| 1760 | * @str: pointer to the string R/W |
| 1761 | * @llo: pointer to the low result |
| 1762 | * @lmi: pointer to the mid result |
| 1763 | * @lhi: pointer to the high result |
| 1764 | * |
| 1765 | * Parse an unsigned long into 3 fields. |
| 1766 | * |
| 1767 | * Returns the number of chars parsed or -1 if overflow of the capacity |
| 1768 | */ |
| 1769 | static int |
| 1770 | xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo, |
| 1771 | unsigned long *lmi, unsigned long *lhi) { |
| 1772 | unsigned long lo = 0, mi = 0, hi = 0; |
| 1773 | const xmlChar *tmp, *cur = *str; |
| 1774 | int ret = 0, i = 0; |
| 1775 | |
| 1776 | while (*cur == '0') { |
| 1777 | ret++; |
| 1778 | cur++; |
| 1779 | } |
| 1780 | tmp = cur; |
| 1781 | while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) { |
| 1782 | i++;tmp++;ret++; |
| 1783 | } |
| 1784 | if (i > 24) { |
| 1785 | *str = tmp; |
| 1786 | return(-1); |
| 1787 | } |
| 1788 | while (i > 16) { |
| 1789 | hi = hi * 10 + (*cur++ - '0'); |
| 1790 | i--; |
| 1791 | } |
| 1792 | while (i > 8) { |
| 1793 | mi = mi * 10 + (*cur++ - '0'); |
| 1794 | i--; |
| 1795 | } |
| 1796 | while (i > 0) { |
| 1797 | lo = lo * 10 + (*cur++ - '0'); |
| 1798 | i--; |
| 1799 | } |
| 1800 | |
| 1801 | *str = cur; |
| 1802 | *llo = lo; |
| 1803 | *lmi = mi; |
| 1804 | *lhi = hi; |
| 1805 | return(ret); |
| 1806 | } |
| 1807 | |
| 1808 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1809 | * xmlSchemaValAtomicType: |
| 1810 | * @type: the predefined type |
| 1811 | * @value: the value to check |
| 1812 | * @val: the return computed value |
| 1813 | * @node: the node containing the value |
| 1814 | * flags: flags to control the vlidation |
| 1815 | * |
| 1816 | * Check that a value conforms to the lexical space of the atomic type. |
| 1817 | * if true a value is computed and returned in @val. |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1818 | * This checks the value space for list types as well (IDREFS, NMTOKENS). |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1819 | * |
| 1820 | * Returns 0 if this validates, a positive error code number otherwise |
| 1821 | * and -1 in case of internal or API error. |
| 1822 | */ |
| 1823 | static int |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1824 | xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar * value, |
| 1825 | xmlSchemaValPtr * val, xmlNodePtr node, int flags) |
| 1826 | { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1827 | xmlSchemaValPtr v; |
| 1828 | xmlChar *norm = NULL; |
Daniel Veillard | d3b9cd8 | 2003-04-09 11:24:17 +0000 | [diff] [blame] | 1829 | int ret = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1830 | |
| 1831 | if (xmlSchemaTypesInitialized == 0) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1832 | xmlSchemaInitTypes(); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1833 | if (type == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1834 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1835 | |
Daniel Veillard | eebd633 | 2004-08-26 10:30:44 +0000 | [diff] [blame] | 1836 | /* |
| 1837 | * validating a non existant text node is similar to validating |
| 1838 | * an empty one. |
| 1839 | */ |
| 1840 | if (value == NULL) |
| 1841 | value = BAD_CAST ""; |
| 1842 | |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1843 | if (val != NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1844 | *val = NULL; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1845 | if ((flags == 0) && (value != NULL)) { |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1846 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1847 | if ((type->builtInType != XML_SCHEMAS_STRING) && |
Kasimier T. Buchcik | 91feaf8 | 2004-11-12 14:04:58 +0000 | [diff] [blame] | 1848 | (type->builtInType != XML_SCHEMAS_ANYTYPE) && |
| 1849 | (type->builtInType != XML_SCHEMAS_ANYSIMPLETYPE)) { |
| 1850 | if (type->builtInType == XML_SCHEMAS_NORMSTRING) |
| 1851 | norm = xmlSchemaWhiteSpaceReplace(value); |
| 1852 | else |
| 1853 | norm = xmlSchemaCollapseString(value); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1854 | if (norm != NULL) |
| 1855 | value = norm; |
| 1856 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1857 | } |
| 1858 | |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1859 | switch (type->builtInType) { |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 1860 | case XML_SCHEMAS_UNKNOWN: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1861 | goto error; |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 1862 | case XML_SCHEMAS_ANYTYPE: |
| 1863 | case XML_SCHEMAS_ANYSIMPLETYPE: |
| 1864 | goto return0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1865 | case XML_SCHEMAS_STRING: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1866 | goto return0; |
Daniel Veillard | 1516d5b | 2004-01-22 07:27:45 +0000 | [diff] [blame] | 1867 | case XML_SCHEMAS_NORMSTRING:{ |
| 1868 | const xmlChar *cur = value; |
| 1869 | |
| 1870 | while (*cur != 0) { |
| 1871 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 1872 | goto return1; |
| 1873 | } else { |
| 1874 | cur++; |
| 1875 | } |
| 1876 | } |
| 1877 | if (val != NULL) { |
| 1878 | v = xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING); |
| 1879 | if (v != NULL) { |
| 1880 | v->value.str = xmlStrdup(value); |
| 1881 | *val = v; |
| 1882 | } else { |
| 1883 | goto error; |
| 1884 | } |
| 1885 | } |
| 1886 | goto return0; |
| 1887 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1888 | case XML_SCHEMAS_DECIMAL:{ |
| 1889 | const xmlChar *cur = value, *tmp; |
| 1890 | unsigned int frac = 0, len, neg = 0; |
| 1891 | unsigned long base = 0; |
| 1892 | |
| 1893 | if (cur == NULL) |
| 1894 | goto return1; |
| 1895 | if (*cur == '+') |
| 1896 | cur++; |
| 1897 | else if (*cur == '-') { |
| 1898 | neg = 1; |
| 1899 | cur++; |
| 1900 | } |
| 1901 | tmp = cur; |
| 1902 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1903 | base = base * 10 + (*cur - '0'); |
| 1904 | cur++; |
| 1905 | } |
| 1906 | len = cur - tmp; |
| 1907 | if (*cur == '.') { |
| 1908 | cur++; |
| 1909 | tmp = cur; |
| 1910 | while ((*cur >= '0') && (*cur <= '9')) { |
| 1911 | base = base * 10 + (*cur - '0'); |
| 1912 | cur++; |
| 1913 | } |
| 1914 | frac = cur - tmp; |
| 1915 | } |
| 1916 | if (*cur != 0) |
| 1917 | goto return1; |
| 1918 | if (val != NULL) { |
| 1919 | v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL); |
| 1920 | if (v != NULL) { |
| 1921 | v->value.decimal.lo = base; |
| 1922 | v->value.decimal.sign = neg; |
| 1923 | v->value.decimal.frac = frac; |
| 1924 | v->value.decimal.total = frac + len; |
| 1925 | *val = v; |
| 1926 | } |
| 1927 | } |
| 1928 | goto return0; |
| 1929 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1930 | case XML_SCHEMAS_TIME: |
| 1931 | case XML_SCHEMAS_GDAY: |
| 1932 | case XML_SCHEMAS_GMONTH: |
| 1933 | case XML_SCHEMAS_GMONTHDAY: |
| 1934 | case XML_SCHEMAS_GYEAR: |
| 1935 | case XML_SCHEMAS_GYEARMONTH: |
| 1936 | case XML_SCHEMAS_DATE: |
| 1937 | case XML_SCHEMAS_DATETIME: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 1938 | ret = xmlSchemaValidateDates(type->builtInType, value, val); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1939 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1940 | case XML_SCHEMAS_DURATION: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1941 | ret = xmlSchemaValidateDuration(type, value, val); |
| 1942 | break; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1943 | case XML_SCHEMAS_FLOAT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1944 | case XML_SCHEMAS_DOUBLE:{ |
| 1945 | const xmlChar *cur = value; |
| 1946 | int neg = 0; |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 1947 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1948 | if (cur == NULL) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 1949 | goto return1; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 1950 | if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) { |
| 1951 | cur += 3; |
| 1952 | if (*cur != 0) |
| 1953 | goto return1; |
| 1954 | if (val != NULL) { |
| 1955 | if (type == xmlSchemaTypeFloatDef) { |
| 1956 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 1957 | if (v != NULL) { |
| 1958 | v->value.f = (float) xmlXPathNAN; |
| 1959 | } else { |
| 1960 | xmlSchemaFreeValue(v); |
| 1961 | goto error; |
| 1962 | } |
| 1963 | } else { |
| 1964 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 1965 | if (v != NULL) { |
| 1966 | v->value.d = xmlXPathNAN; |
| 1967 | } else { |
| 1968 | xmlSchemaFreeValue(v); |
| 1969 | goto error; |
| 1970 | } |
| 1971 | } |
| 1972 | *val = v; |
| 1973 | } |
| 1974 | goto return0; |
| 1975 | } |
| 1976 | if (*cur == '-') { |
| 1977 | neg = 1; |
| 1978 | cur++; |
| 1979 | } |
| 1980 | if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) { |
| 1981 | cur += 3; |
| 1982 | if (*cur != 0) |
| 1983 | goto return1; |
| 1984 | if (val != NULL) { |
| 1985 | if (type == xmlSchemaTypeFloatDef) { |
| 1986 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 1987 | if (v != NULL) { |
| 1988 | if (neg) |
| 1989 | v->value.f = (float) xmlXPathNINF; |
| 1990 | else |
| 1991 | v->value.f = (float) xmlXPathPINF; |
| 1992 | } else { |
| 1993 | xmlSchemaFreeValue(v); |
| 1994 | goto error; |
| 1995 | } |
| 1996 | } else { |
| 1997 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 1998 | if (v != NULL) { |
| 1999 | if (neg) |
| 2000 | v->value.d = xmlXPathNINF; |
| 2001 | else |
| 2002 | v->value.d = xmlXPathPINF; |
| 2003 | } else { |
| 2004 | xmlSchemaFreeValue(v); |
| 2005 | goto error; |
| 2006 | } |
| 2007 | } |
| 2008 | *val = v; |
| 2009 | } |
| 2010 | goto return0; |
| 2011 | } |
| 2012 | if ((neg == 0) && (*cur == '+')) |
| 2013 | cur++; |
| 2014 | if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-')) |
| 2015 | goto return1; |
| 2016 | while ((*cur >= '0') && (*cur <= '9')) { |
| 2017 | cur++; |
| 2018 | } |
| 2019 | if (*cur == '.') { |
| 2020 | cur++; |
| 2021 | while ((*cur >= '0') && (*cur <= '9')) |
| 2022 | cur++; |
| 2023 | } |
| 2024 | if ((*cur == 'e') || (*cur == 'E')) { |
| 2025 | cur++; |
| 2026 | if ((*cur == '-') || (*cur == '+')) |
| 2027 | cur++; |
| 2028 | while ((*cur >= '0') && (*cur <= '9')) |
| 2029 | cur++; |
| 2030 | } |
| 2031 | if (*cur != 0) |
| 2032 | goto return1; |
| 2033 | if (val != NULL) { |
| 2034 | if (type == xmlSchemaTypeFloatDef) { |
| 2035 | v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT); |
| 2036 | if (v != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2037 | if (sscanf((const char *) value, "%f", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2038 | &(v->value.f)) == 1) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2039 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2040 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2041 | xmlSchemaFreeValue(v); |
| 2042 | goto return1; |
| 2043 | } |
| 2044 | } else { |
| 2045 | goto error; |
| 2046 | } |
| 2047 | } else { |
| 2048 | v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE); |
| 2049 | if (v != NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2050 | if (sscanf((const char *) value, "%lf", |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2051 | &(v->value.d)) == 1) { |
| 2052 | *val = v; |
| 2053 | } else { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2054 | xmlSchemaFreeValue(v); |
| 2055 | goto return1; |
| 2056 | } |
| 2057 | } else { |
| 2058 | goto error; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | goto return0; |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 2063 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2064 | case XML_SCHEMAS_BOOLEAN:{ |
| 2065 | const xmlChar *cur = value; |
| 2066 | |
| 2067 | if ((cur[0] == '0') && (cur[1] == 0)) |
| 2068 | ret = 0; |
| 2069 | else if ((cur[0] == '1') && (cur[1] == 0)) |
| 2070 | ret = 1; |
| 2071 | else if ((cur[0] == 't') && (cur[1] == 'r') |
| 2072 | && (cur[2] == 'u') && (cur[3] == 'e') |
| 2073 | && (cur[4] == 0)) |
| 2074 | ret = 1; |
| 2075 | else if ((cur[0] == 'f') && (cur[1] == 'a') |
| 2076 | && (cur[2] == 'l') && (cur[3] == 's') |
| 2077 | && (cur[4] == 'e') && (cur[5] == 0)) |
| 2078 | ret = 0; |
| 2079 | else |
| 2080 | goto return1; |
| 2081 | if (val != NULL) { |
| 2082 | v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN); |
| 2083 | if (v != NULL) { |
| 2084 | v->value.b = ret; |
| 2085 | *val = v; |
| 2086 | } else { |
| 2087 | goto error; |
| 2088 | } |
| 2089 | } |
| 2090 | goto return0; |
| 2091 | } |
| 2092 | case XML_SCHEMAS_TOKEN:{ |
| 2093 | const xmlChar *cur = value; |
| 2094 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 2095 | if (IS_BLANK_CH(*cur)) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2096 | goto return1; |
| 2097 | |
| 2098 | while (*cur != 0) { |
| 2099 | if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) { |
| 2100 | goto return1; |
| 2101 | } else if (*cur == ' ') { |
| 2102 | cur++; |
| 2103 | if (*cur == 0) |
| 2104 | goto return1; |
| 2105 | if (*cur == ' ') |
| 2106 | goto return1; |
| 2107 | } else { |
| 2108 | cur++; |
| 2109 | } |
| 2110 | } |
| 2111 | if (val != NULL) { |
| 2112 | v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN); |
| 2113 | if (v != NULL) { |
| 2114 | v->value.str = xmlStrdup(value); |
| 2115 | *val = v; |
| 2116 | } else { |
| 2117 | goto error; |
| 2118 | } |
| 2119 | } |
| 2120 | goto return0; |
| 2121 | } |
| 2122 | case XML_SCHEMAS_LANGUAGE: |
| 2123 | if (xmlCheckLanguageID(value) == 1) { |
| 2124 | if (val != NULL) { |
| 2125 | v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE); |
| 2126 | if (v != NULL) { |
| 2127 | v->value.str = xmlStrdup(value); |
| 2128 | *val = v; |
| 2129 | } else { |
| 2130 | goto error; |
| 2131 | } |
| 2132 | } |
| 2133 | goto return0; |
| 2134 | } |
| 2135 | goto return1; |
| 2136 | case XML_SCHEMAS_NMTOKEN: |
| 2137 | if (xmlValidateNMToken(value, 1) == 0) { |
| 2138 | if (val != NULL) { |
| 2139 | v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN); |
| 2140 | if (v != NULL) { |
| 2141 | v->value.str = xmlStrdup(value); |
| 2142 | *val = v; |
| 2143 | } else { |
| 2144 | goto error; |
| 2145 | } |
| 2146 | } |
| 2147 | goto return0; |
| 2148 | } |
| 2149 | goto return1; |
| 2150 | case XML_SCHEMAS_NMTOKENS: |
| 2151 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef, |
| 2152 | value, val, node); |
| 2153 | if (ret > 0) |
| 2154 | ret = 0; |
| 2155 | else |
| 2156 | ret = 1; |
| 2157 | goto done; |
| 2158 | case XML_SCHEMAS_NAME: |
| 2159 | ret = xmlValidateName(value, 1); |
Daniel Veillard | df292f7 | 2005-01-16 19:00:15 +0000 | [diff] [blame] | 2160 | if ((ret == 0) && (val != NULL) && (value != NULL)) { |
| 2161 | v = xmlSchemaNewValue(XML_SCHEMAS_NAME); |
| 2162 | if (v != NULL) { |
| 2163 | const xmlChar *start = value, *end; |
| 2164 | while (IS_BLANK_CH(*start)) start++; |
| 2165 | end = start; |
| 2166 | while ((*end != 0) && (!IS_BLANK_CH(*end))) end++; |
| 2167 | v->value.str = xmlStrndup(start, end - start); |
| 2168 | *val = v; |
| 2169 | } else { |
| 2170 | goto error; |
| 2171 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2172 | } |
| 2173 | goto done; |
| 2174 | case XML_SCHEMAS_QNAME:{ |
| 2175 | xmlChar *uri = NULL; |
| 2176 | xmlChar *local = NULL; |
| 2177 | |
| 2178 | ret = xmlValidateQName(value, 1); |
| 2179 | if ((ret == 0) && (node != NULL)) { |
| 2180 | xmlChar *prefix; |
| 2181 | |
| 2182 | local = xmlSplitQName2(value, &prefix); |
| 2183 | if (prefix != NULL) { |
| 2184 | xmlNsPtr ns; |
| 2185 | |
| 2186 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2187 | if (ns == NULL) |
| 2188 | ret = 1; |
| 2189 | else if (val != NULL) |
| 2190 | uri = xmlStrdup(ns->href); |
| 2191 | } |
| 2192 | if ((local != NULL) && ((val == NULL) || (ret != 0))) |
| 2193 | xmlFree(local); |
| 2194 | if (prefix != NULL) |
| 2195 | xmlFree(prefix); |
| 2196 | } |
| 2197 | if ((ret == 0) && (val != NULL)) { |
| 2198 | v = xmlSchemaNewValue(XML_SCHEMAS_QNAME); |
| 2199 | if (v != NULL) { |
| 2200 | if (local != NULL) |
| 2201 | v->value.qname.name = local; |
| 2202 | else |
| 2203 | v->value.qname.name = xmlStrdup(value); |
| 2204 | if (uri != NULL) |
| 2205 | v->value.qname.uri = uri; |
| 2206 | |
| 2207 | *val = v; |
| 2208 | } else { |
| 2209 | if (local != NULL) |
| 2210 | xmlFree(local); |
| 2211 | if (uri != NULL) |
| 2212 | xmlFree(uri); |
| 2213 | goto error; |
| 2214 | } |
| 2215 | } |
| 2216 | goto done; |
| 2217 | } |
| 2218 | case XML_SCHEMAS_NCNAME: |
| 2219 | ret = xmlValidateNCName(value, 1); |
| 2220 | if ((ret == 0) && (val != NULL)) { |
| 2221 | v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME); |
| 2222 | if (v != NULL) { |
| 2223 | v->value.str = xmlStrdup(value); |
| 2224 | *val = v; |
| 2225 | } else { |
| 2226 | goto error; |
| 2227 | } |
| 2228 | } |
| 2229 | goto done; |
| 2230 | case XML_SCHEMAS_ID: |
| 2231 | ret = xmlValidateNCName(value, 1); |
| 2232 | if ((ret == 0) && (val != NULL)) { |
| 2233 | v = xmlSchemaNewValue(XML_SCHEMAS_ID); |
| 2234 | if (v != NULL) { |
| 2235 | v->value.str = xmlStrdup(value); |
| 2236 | *val = v; |
| 2237 | } else { |
| 2238 | goto error; |
| 2239 | } |
| 2240 | } |
| 2241 | if ((ret == 0) && (node != NULL) && |
| 2242 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2243 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2244 | |
| 2245 | /* |
| 2246 | * NOTE: the IDness might have already be declared in the DTD |
| 2247 | */ |
| 2248 | if (attr->atype != XML_ATTRIBUTE_ID) { |
| 2249 | xmlIDPtr res; |
| 2250 | xmlChar *strip; |
| 2251 | |
| 2252 | strip = xmlSchemaStrip(value); |
| 2253 | if (strip != NULL) { |
| 2254 | res = xmlAddID(NULL, node->doc, strip, attr); |
| 2255 | xmlFree(strip); |
| 2256 | } else |
| 2257 | res = xmlAddID(NULL, node->doc, value, attr); |
| 2258 | if (res == NULL) { |
| 2259 | ret = 2; |
| 2260 | } else { |
| 2261 | attr->atype = XML_ATTRIBUTE_ID; |
| 2262 | } |
| 2263 | } |
| 2264 | } |
| 2265 | goto done; |
| 2266 | case XML_SCHEMAS_IDREF: |
| 2267 | ret = xmlValidateNCName(value, 1); |
| 2268 | if ((ret == 0) && (val != NULL)) { |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 2269 | v = xmlSchemaNewValue(XML_SCHEMAS_IDREF); |
| 2270 | if (v == NULL) |
| 2271 | goto error; |
| 2272 | v->value.str = xmlStrdup(value); |
| 2273 | *val = v; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2274 | } |
| 2275 | if ((ret == 0) && (node != NULL) && |
| 2276 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2277 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2278 | xmlChar *strip; |
| 2279 | |
| 2280 | strip = xmlSchemaStrip(value); |
| 2281 | if (strip != NULL) { |
| 2282 | xmlAddRef(NULL, node->doc, strip, attr); |
| 2283 | xmlFree(strip); |
| 2284 | } else |
| 2285 | xmlAddRef(NULL, node->doc, value, attr); |
| 2286 | attr->atype = XML_ATTRIBUTE_IDREF; |
| 2287 | } |
| 2288 | goto done; |
| 2289 | case XML_SCHEMAS_IDREFS: |
| 2290 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef, |
| 2291 | value, val, node); |
| 2292 | if (ret < 0) |
| 2293 | ret = 2; |
| 2294 | else |
| 2295 | ret = 0; |
| 2296 | if ((ret == 0) && (node != NULL) && |
| 2297 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2298 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2299 | |
| 2300 | attr->atype = XML_ATTRIBUTE_IDREFS; |
| 2301 | } |
| 2302 | goto done; |
| 2303 | case XML_SCHEMAS_ENTITY:{ |
| 2304 | xmlChar *strip; |
| 2305 | |
| 2306 | ret = xmlValidateNCName(value, 1); |
| 2307 | if ((node == NULL) || (node->doc == NULL)) |
| 2308 | ret = 3; |
| 2309 | if (ret == 0) { |
| 2310 | xmlEntityPtr ent; |
| 2311 | |
| 2312 | strip = xmlSchemaStrip(value); |
| 2313 | if (strip != NULL) { |
| 2314 | ent = xmlGetDocEntity(node->doc, strip); |
| 2315 | xmlFree(strip); |
| 2316 | } else { |
| 2317 | ent = xmlGetDocEntity(node->doc, value); |
| 2318 | } |
| 2319 | if ((ent == NULL) || |
| 2320 | (ent->etype != |
| 2321 | XML_EXTERNAL_GENERAL_UNPARSED_ENTITY)) |
| 2322 | ret = 4; |
| 2323 | } |
| 2324 | if ((ret == 0) && (val != NULL)) { |
| 2325 | TODO; |
| 2326 | } |
| 2327 | if ((ret == 0) && (node != NULL) && |
| 2328 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2329 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2330 | |
| 2331 | attr->atype = XML_ATTRIBUTE_ENTITY; |
| 2332 | } |
| 2333 | goto done; |
| 2334 | } |
| 2335 | case XML_SCHEMAS_ENTITIES: |
| 2336 | if ((node == NULL) || (node->doc == NULL)) |
| 2337 | goto return3; |
| 2338 | ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef, |
| 2339 | value, val, node); |
| 2340 | if (ret <= 0) |
| 2341 | ret = 1; |
| 2342 | else |
| 2343 | ret = 0; |
| 2344 | if ((ret == 0) && (node != NULL) && |
| 2345 | (node->type == XML_ATTRIBUTE_NODE)) { |
| 2346 | xmlAttrPtr attr = (xmlAttrPtr) node; |
| 2347 | |
| 2348 | attr->atype = XML_ATTRIBUTE_ENTITIES; |
| 2349 | } |
| 2350 | goto done; |
| 2351 | case XML_SCHEMAS_NOTATION:{ |
| 2352 | xmlChar *uri = NULL; |
| 2353 | xmlChar *local = NULL; |
| 2354 | |
| 2355 | ret = xmlValidateQName(value, 1); |
| 2356 | if ((ret == 0) && (node != NULL)) { |
| 2357 | xmlChar *prefix; |
| 2358 | |
| 2359 | local = xmlSplitQName2(value, &prefix); |
| 2360 | if (prefix != NULL) { |
| 2361 | xmlNsPtr ns; |
| 2362 | |
| 2363 | ns = xmlSearchNs(node->doc, node, prefix); |
| 2364 | if (ns == NULL) |
| 2365 | ret = 1; |
| 2366 | else if (val != NULL) |
| 2367 | uri = xmlStrdup(ns->href); |
| 2368 | } |
| 2369 | if ((local != NULL) && ((val == NULL) || (ret != 0))) |
| 2370 | xmlFree(local); |
| 2371 | if (prefix != NULL) |
| 2372 | xmlFree(prefix); |
| 2373 | } |
| 2374 | if ((node == NULL) || (node->doc == NULL)) |
| 2375 | ret = 3; |
| 2376 | if (ret == 0) { |
| 2377 | ret = xmlValidateNotationUse(NULL, node->doc, value); |
| 2378 | if (ret == 1) |
| 2379 | ret = 0; |
| 2380 | else |
| 2381 | ret = 1; |
| 2382 | } |
| 2383 | if ((ret == 0) && (val != NULL)) { |
| 2384 | v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION); |
| 2385 | if (v != NULL) { |
| 2386 | if (local != NULL) |
| 2387 | v->value.qname.name = local; |
| 2388 | else |
| 2389 | v->value.qname.name = xmlStrdup(value); |
| 2390 | if (uri != NULL) |
| 2391 | v->value.qname.uri = uri; |
| 2392 | |
| 2393 | *val = v; |
| 2394 | } else { |
| 2395 | if (local != NULL) |
| 2396 | xmlFree(local); |
| 2397 | if (uri != NULL) |
| 2398 | xmlFree(uri); |
| 2399 | goto error; |
| 2400 | } |
| 2401 | } |
| 2402 | goto done; |
| 2403 | } |
| 2404 | case XML_SCHEMAS_ANYURI:{ |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2405 | if (*value != 0) { |
| 2406 | xmlURIPtr uri = xmlParseURI((const char *) value); |
| 2407 | if (uri == NULL) |
| 2408 | goto return1; |
| 2409 | xmlFreeURI(uri); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2410 | } |
Daniel Veillard | 11c466a | 2004-03-14 12:20:15 +0000 | [diff] [blame] | 2411 | |
| 2412 | if (val != NULL) { |
| 2413 | v = xmlSchemaNewValue(XML_SCHEMAS_ANYURI); |
| 2414 | if (v == NULL) |
| 2415 | goto error; |
| 2416 | v->value.str = xmlStrdup(value); |
| 2417 | *val = v; |
| 2418 | } |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2419 | goto return0; |
| 2420 | } |
| 2421 | case XML_SCHEMAS_HEXBINARY:{ |
| 2422 | const xmlChar *cur = value; |
| 2423 | xmlChar *base; |
| 2424 | int total, i = 0; |
| 2425 | |
Daniel Veillard | f34a20e | 2004-08-31 08:42:17 +0000 | [diff] [blame] | 2426 | if (cur == NULL) |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2427 | goto return1; |
| 2428 | |
| 2429 | while (((*cur >= '0') && (*cur <= '9')) || |
| 2430 | ((*cur >= 'A') && (*cur <= 'F')) || |
| 2431 | ((*cur >= 'a') && (*cur <= 'f'))) { |
| 2432 | i++; |
| 2433 | cur++; |
| 2434 | } |
| 2435 | |
| 2436 | if (*cur != 0) |
| 2437 | goto return1; |
| 2438 | if ((i % 2) != 0) |
| 2439 | goto return1; |
| 2440 | |
| 2441 | if (val != NULL) { |
| 2442 | |
| 2443 | v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY); |
| 2444 | if (v == NULL) |
| 2445 | goto error; |
| 2446 | |
| 2447 | cur = xmlStrdup(value); |
| 2448 | if (cur == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2449 | xmlSchemaTypeErrMemory(node, "allocating hexbin data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2450 | xmlFree(v); |
| 2451 | goto return1; |
| 2452 | } |
| 2453 | |
| 2454 | total = i / 2; /* number of octets */ |
| 2455 | |
| 2456 | base = (xmlChar *) cur; |
| 2457 | while (i-- > 0) { |
| 2458 | if (*base >= 'a') |
| 2459 | *base = *base - ('a' - 'A'); |
| 2460 | base++; |
| 2461 | } |
| 2462 | |
| 2463 | v->value.hex.str = (xmlChar *) cur; |
| 2464 | v->value.hex.total = total; |
| 2465 | *val = v; |
| 2466 | } |
| 2467 | goto return0; |
| 2468 | } |
| 2469 | case XML_SCHEMAS_BASE64BINARY:{ |
| 2470 | /* ISSUE: |
| 2471 | * |
| 2472 | * Ignore all stray characters? (yes, currently) |
| 2473 | * Worry about long lines? (no, currently) |
| 2474 | * |
| 2475 | * rfc2045.txt: |
| 2476 | * |
| 2477 | * "The encoded output stream must be represented in lines of |
| 2478 | * no more than 76 characters each. All line breaks or other |
| 2479 | * characters not found in Table 1 must be ignored by decoding |
| 2480 | * software. In base64 data, characters other than those in |
| 2481 | * Table 1, line breaks, and other white space probably |
| 2482 | * indicate a transmission error, about which a warning |
| 2483 | * message or even a message rejection might be appropriate |
| 2484 | * under some circumstances." */ |
| 2485 | const xmlChar *cur = value; |
| 2486 | xmlChar *base; |
| 2487 | int total, i = 0, pad = 0; |
| 2488 | |
| 2489 | if (cur == NULL) |
| 2490 | goto return1; |
| 2491 | |
| 2492 | for (; *cur; ++cur) { |
| 2493 | int decc; |
| 2494 | |
| 2495 | decc = _xmlSchemaBase64Decode(*cur); |
| 2496 | if (decc < 0) ; |
| 2497 | else if (decc < 64) |
| 2498 | i++; |
| 2499 | else |
| 2500 | break; |
| 2501 | } |
| 2502 | for (; *cur; ++cur) { |
| 2503 | int decc; |
| 2504 | |
| 2505 | decc = _xmlSchemaBase64Decode(*cur); |
| 2506 | if (decc < 0) ; |
| 2507 | else if (decc < 64) |
| 2508 | goto return1; |
| 2509 | if (decc == 64) |
| 2510 | pad++; |
| 2511 | } |
| 2512 | |
| 2513 | /* rfc2045.txt: "Special processing is performed if fewer than |
| 2514 | * 24 bits are available at the end of the data being encoded. |
| 2515 | * A full encoding quantum is always completed at the end of a |
| 2516 | * body. When fewer than 24 input bits are available in an |
| 2517 | * input group, zero bits are added (on the right) to form an |
| 2518 | * integral number of 6-bit groups. Padding at the end of the |
| 2519 | * data is performed using the "=" character. Since all |
| 2520 | * base64 input is an integral number of octets, only the |
| 2521 | * following cases can arise: (1) the final quantum of |
| 2522 | * encoding input is an integral multiple of 24 bits; here, |
| 2523 | * the final unit of encoded output will be an integral |
| 2524 | * multiple ofindent: Standard input:701: Warning:old style |
| 2525 | * assignment ambiguity in "=*". Assuming "= *" 4 characters |
| 2526 | * with no "=" padding, (2) the final |
| 2527 | * quantum of encoding input is exactly 8 bits; here, the |
| 2528 | * final unit of encoded output will be two characters |
| 2529 | * followed by two "=" padding characters, or (3) the final |
| 2530 | * quantum of encoding input is exactly 16 bits; here, the |
| 2531 | * final unit of encoded output will be three characters |
| 2532 | * followed by one "=" padding character." */ |
| 2533 | |
| 2534 | total = 3 * (i / 4); |
| 2535 | if (pad == 0) { |
| 2536 | if (i % 4 != 0) |
| 2537 | goto return1; |
| 2538 | } else if (pad == 1) { |
| 2539 | int decc; |
| 2540 | |
| 2541 | if (i % 4 != 3) |
| 2542 | goto return1; |
| 2543 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 2544 | (decc < 0) || (decc > 63); |
| 2545 | decc = _xmlSchemaBase64Decode(*cur)) |
| 2546 | --cur; |
| 2547 | /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/ |
| 2548 | /* 00111100 -> 0x3c */ |
| 2549 | if (decc & ~0x3c) |
| 2550 | goto return1; |
| 2551 | total += 2; |
| 2552 | } else if (pad == 2) { |
| 2553 | int decc; |
| 2554 | |
| 2555 | if (i % 4 != 2) |
| 2556 | goto return1; |
| 2557 | for (decc = _xmlSchemaBase64Decode(*cur); |
| 2558 | (decc < 0) || (decc > 63); |
| 2559 | decc = _xmlSchemaBase64Decode(*cur)) |
| 2560 | --cur; |
| 2561 | /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */ |
| 2562 | /* 00110000 -> 0x30 */ |
| 2563 | if (decc & ~0x30) |
| 2564 | goto return1; |
| 2565 | total += 1; |
| 2566 | } else |
| 2567 | goto return1; |
| 2568 | |
| 2569 | if (val != NULL) { |
| 2570 | v = xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY); |
| 2571 | if (v == NULL) |
| 2572 | goto error; |
| 2573 | base = |
| 2574 | (xmlChar *) xmlMallocAtomic((i + pad + 1) * |
| 2575 | sizeof(xmlChar)); |
| 2576 | if (base == NULL) { |
Daniel Veillard | d0c9c32 | 2003-10-10 00:49:42 +0000 | [diff] [blame] | 2577 | xmlSchemaTypeErrMemory(node, "allocating base64 data"); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2578 | xmlFree(v); |
| 2579 | goto return1; |
| 2580 | } |
| 2581 | v->value.base64.str = base; |
| 2582 | for (cur = value; *cur; ++cur) |
| 2583 | if (_xmlSchemaBase64Decode(*cur) >= 0) { |
| 2584 | *base = *cur; |
| 2585 | ++base; |
| 2586 | } |
| 2587 | *base = 0; |
| 2588 | v->value.base64.total = total; |
| 2589 | *val = v; |
| 2590 | } |
| 2591 | goto return0; |
| 2592 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2593 | case XML_SCHEMAS_INTEGER: |
| 2594 | case XML_SCHEMAS_PINTEGER: |
| 2595 | case XML_SCHEMAS_NPINTEGER: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2596 | case XML_SCHEMAS_NINTEGER: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2597 | case XML_SCHEMAS_NNINTEGER:{ |
| 2598 | const xmlChar *cur = value; |
| 2599 | unsigned long lo, mi, hi; |
| 2600 | int sign = 0; |
| 2601 | |
| 2602 | if (cur == NULL) |
| 2603 | goto return1; |
| 2604 | if (*cur == '-') { |
| 2605 | sign = 1; |
| 2606 | cur++; |
| 2607 | } else if (*cur == '+') |
| 2608 | cur++; |
| 2609 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 2610 | if (ret == 0) |
| 2611 | goto return1; |
| 2612 | if (*cur != 0) |
| 2613 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2614 | if (type->builtInType == XML_SCHEMAS_NPINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2615 | if ((sign == 0) && |
| 2616 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 2617 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2618 | } else if (type->builtInType == XML_SCHEMAS_PINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2619 | if (sign == 1) |
| 2620 | goto return1; |
| 2621 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 2622 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2623 | } else if (type->builtInType == XML_SCHEMAS_NINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2624 | if (sign == 0) |
| 2625 | goto return1; |
| 2626 | if ((hi == 0) && (mi == 0) && (lo == 0)) |
| 2627 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2628 | } else if (type->builtInType == XML_SCHEMAS_NNINTEGER) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2629 | if ((sign == 1) && |
| 2630 | ((hi != 0) || (mi != 0) || (lo != 0))) |
| 2631 | goto return1; |
| 2632 | } |
| 2633 | /* |
| 2634 | * We can store a value only if no overflow occured |
| 2635 | */ |
| 2636 | if ((ret > 0) && (val != NULL)) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2637 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2638 | if (v != NULL) { |
| 2639 | v->value.decimal.lo = lo; |
| 2640 | v->value.decimal.mi = lo; |
| 2641 | v->value.decimal.hi = lo; |
| 2642 | v->value.decimal.sign = sign; |
| 2643 | v->value.decimal.frac = 0; |
| 2644 | v->value.decimal.total = cur - value; |
| 2645 | *val = v; |
| 2646 | } |
| 2647 | } |
| 2648 | goto return0; |
| 2649 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2650 | case XML_SCHEMAS_LONG: |
| 2651 | case XML_SCHEMAS_BYTE: |
| 2652 | case XML_SCHEMAS_SHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2653 | case XML_SCHEMAS_INT:{ |
| 2654 | const xmlChar *cur = value; |
| 2655 | unsigned long lo, mi, hi; |
| 2656 | int total = 0; |
| 2657 | int sign = 0; |
| 2658 | |
| 2659 | if (cur == NULL) |
| 2660 | goto return1; |
| 2661 | if (*cur == '-') { |
| 2662 | sign = 1; |
| 2663 | cur++; |
| 2664 | } else if (*cur == '+') |
| 2665 | cur++; |
| 2666 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 2667 | if (ret <= 0) |
| 2668 | goto return1; |
| 2669 | if (*cur != 0) |
| 2670 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2671 | if (type->builtInType == XML_SCHEMAS_LONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2672 | if (hi >= 922) { |
| 2673 | if (hi > 922) |
| 2674 | goto return1; |
| 2675 | if (mi >= 33720368) { |
| 2676 | if (mi > 33720368) |
| 2677 | goto return1; |
| 2678 | if ((sign == 0) && (lo > 54775807)) |
| 2679 | goto return1; |
| 2680 | if ((sign == 1) && (lo > 54775808)) |
| 2681 | goto return1; |
| 2682 | } |
| 2683 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2684 | } else if (type->builtInType == XML_SCHEMAS_INT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2685 | if (hi != 0) |
| 2686 | goto return1; |
| 2687 | if (mi >= 21) { |
| 2688 | if (mi > 21) |
| 2689 | goto return1; |
| 2690 | if ((sign == 0) && (lo > 47483647)) |
| 2691 | goto return1; |
| 2692 | if ((sign == 1) && (lo > 47483648)) |
| 2693 | goto return1; |
| 2694 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2695 | } else if (type->builtInType == XML_SCHEMAS_SHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2696 | if ((mi != 0) || (hi != 0)) |
| 2697 | goto return1; |
| 2698 | if ((sign == 1) && (lo > 32768)) |
| 2699 | goto return1; |
| 2700 | if ((sign == 0) && (lo > 32767)) |
| 2701 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2702 | } else if (type->builtInType == XML_SCHEMAS_BYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2703 | if ((mi != 0) || (hi != 0)) |
| 2704 | goto return1; |
| 2705 | if ((sign == 1) && (lo > 128)) |
| 2706 | goto return1; |
| 2707 | if ((sign == 0) && (lo > 127)) |
| 2708 | goto return1; |
| 2709 | } |
| 2710 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2711 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2712 | if (v != NULL) { |
| 2713 | v->value.decimal.lo = lo; |
| 2714 | v->value.decimal.mi = lo; |
| 2715 | v->value.decimal.hi = lo; |
| 2716 | v->value.decimal.sign = sign; |
| 2717 | v->value.decimal.frac = 0; |
| 2718 | v->value.decimal.total = total; |
| 2719 | *val = v; |
| 2720 | } |
| 2721 | } |
| 2722 | goto return0; |
| 2723 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2724 | case XML_SCHEMAS_UINT: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2725 | case XML_SCHEMAS_ULONG: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2726 | case XML_SCHEMAS_USHORT: |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2727 | case XML_SCHEMAS_UBYTE:{ |
| 2728 | const xmlChar *cur = value; |
| 2729 | unsigned long lo, mi, hi; |
| 2730 | int total = 0; |
| 2731 | |
| 2732 | if (cur == NULL) |
| 2733 | goto return1; |
| 2734 | ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi); |
| 2735 | if (ret <= 0) |
| 2736 | goto return1; |
| 2737 | if (*cur != 0) |
| 2738 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2739 | if (type->builtInType == XML_SCHEMAS_ULONG) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2740 | if (hi >= 1844) { |
| 2741 | if (hi > 1844) |
| 2742 | goto return1; |
| 2743 | if (mi >= 67440737) { |
| 2744 | if (mi > 67440737) |
| 2745 | goto return1; |
| 2746 | if (lo > 9551615) |
| 2747 | goto return1; |
| 2748 | } |
| 2749 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2750 | } else if (type->builtInType == XML_SCHEMAS_UINT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2751 | if (hi != 0) |
| 2752 | goto return1; |
| 2753 | if (mi >= 42) { |
| 2754 | if (mi > 42) |
| 2755 | goto return1; |
| 2756 | if (lo > 94967295) |
| 2757 | goto return1; |
| 2758 | } |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2759 | } else if (type->builtInType == XML_SCHEMAS_USHORT) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2760 | if ((mi != 0) || (hi != 0)) |
| 2761 | goto return1; |
| 2762 | if (lo > 65535) |
| 2763 | goto return1; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2764 | } else if (type->builtInType == XML_SCHEMAS_UBYTE) { |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2765 | if ((mi != 0) || (hi != 0)) |
| 2766 | goto return1; |
| 2767 | if (lo > 255) |
| 2768 | goto return1; |
| 2769 | } |
| 2770 | if (val != NULL) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2771 | v = xmlSchemaNewValue(type->builtInType); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2772 | if (v != NULL) { |
| 2773 | v->value.decimal.lo = lo; |
| 2774 | v->value.decimal.mi = mi; |
| 2775 | v->value.decimal.hi = hi; |
| 2776 | v->value.decimal.sign = 0; |
| 2777 | v->value.decimal.frac = 0; |
| 2778 | v->value.decimal.total = total; |
| 2779 | *val = v; |
| 2780 | } |
| 2781 | } |
| 2782 | goto return0; |
| 2783 | } |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 2786 | done: |
| 2787 | if (norm != NULL) |
| 2788 | xmlFree(norm); |
| 2789 | return (ret); |
| 2790 | return3: |
| 2791 | if (norm != NULL) |
| 2792 | xmlFree(norm); |
| 2793 | return (3); |
| 2794 | return1: |
| 2795 | if (norm != NULL) |
| 2796 | xmlFree(norm); |
| 2797 | return (1); |
| 2798 | return0: |
| 2799 | if (norm != NULL) |
| 2800 | xmlFree(norm); |
| 2801 | return (0); |
| 2802 | error: |
| 2803 | if (norm != NULL) |
| 2804 | xmlFree(norm); |
| 2805 | return (-1); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2809 | * xmlSchemaValPredefTypeNode: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2810 | * @type: the predefined type |
| 2811 | * @value: the value to check |
| 2812 | * @val: the return computed value |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2813 | * @node: the node containing the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2814 | * |
| 2815 | * Check that a value conforms to the lexical space of the predefined type. |
| 2816 | * if true a value is computed and returned in @val. |
| 2817 | * |
| 2818 | * Returns 0 if this validates, a positive error code number otherwise |
| 2819 | * and -1 in case of internal or API error. |
| 2820 | */ |
| 2821 | int |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2822 | xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value, |
| 2823 | xmlSchemaValPtr *val, xmlNodePtr node) { |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 2824 | return(xmlSchemaValAtomicType(type, value, val, node, 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
| 2827 | /** |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2828 | * xmlSchemaValPredefTypeNodeNoNorm: |
| 2829 | * @type: the predefined type |
| 2830 | * @value: the value to check |
| 2831 | * @val: the return computed value |
| 2832 | * @node: the node containing the value |
| 2833 | * |
| 2834 | * Check that a value conforms to the lexical space of the predefined type. |
| 2835 | * if true a value is computed and returned in @val. |
| 2836 | * This one does apply any normalization to the value. |
| 2837 | * |
| 2838 | * Returns 0 if this validates, a positive error code number otherwise |
| 2839 | * and -1 in case of internal or API error. |
| 2840 | */ |
| 2841 | int |
| 2842 | xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type, const xmlChar *value, |
| 2843 | xmlSchemaValPtr *val, xmlNodePtr node) { |
| 2844 | return(xmlSchemaValAtomicType(type, value, val, node, 1)); |
| 2845 | } |
| 2846 | |
| 2847 | /** |
Daniel Veillard | c3da18a | 2003-03-18 00:31:04 +0000 | [diff] [blame] | 2848 | * xmlSchemaValidatePredefinedType: |
| 2849 | * @type: the predefined type |
| 2850 | * @value: the value to check |
| 2851 | * @val: the return computed value |
| 2852 | * |
| 2853 | * Check that a value conforms to the lexical space of the predefined type. |
| 2854 | * if true a value is computed and returned in @val. |
| 2855 | * |
| 2856 | * Returns 0 if this validates, a positive error code number otherwise |
| 2857 | * and -1 in case of internal or API error. |
| 2858 | */ |
| 2859 | int |
| 2860 | xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value, |
| 2861 | xmlSchemaValPtr *val) { |
| 2862 | return(xmlSchemaValPredefTypeNode(type, value, val, NULL)); |
| 2863 | } |
| 2864 | |
| 2865 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2866 | * xmlSchemaCompareDecimals: |
| 2867 | * @x: a first decimal value |
| 2868 | * @y: a second decimal value |
| 2869 | * |
| 2870 | * Compare 2 decimals |
| 2871 | * |
| 2872 | * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error |
| 2873 | */ |
| 2874 | static int |
| 2875 | xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 2876 | { |
| 2877 | xmlSchemaValPtr swp; |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2878 | int order = 1, p; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2879 | unsigned long tmp; |
| 2880 | |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2881 | if ((x->value.decimal.sign) && |
| 2882 | ((x->value.decimal.lo != 0) || |
| 2883 | (x->value.decimal.mi != 0) || |
| 2884 | (x->value.decimal.hi != 0))) { |
| 2885 | if ((y->value.decimal.sign) && |
| 2886 | ((y->value.decimal.lo != 0) || |
| 2887 | (y->value.decimal.mi != 0) || |
| 2888 | (y->value.decimal.hi != 0))) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2889 | order = -1; |
| 2890 | else |
| 2891 | return (-1); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2892 | } else if ((y->value.decimal.sign) && |
| 2893 | ((y->value.decimal.lo != 0) || |
| 2894 | (y->value.decimal.mi != 0) || |
| 2895 | (y->value.decimal.hi != 0))) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2896 | return (1); |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2897 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2898 | if (x->value.decimal.frac == y->value.decimal.frac) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2899 | if (x->value.decimal.hi < y->value.decimal.hi) |
| 2900 | return (-order); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2901 | if (x->value.decimal.hi > y->value.decimal.hi) |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2902 | return (order); |
| 2903 | if (x->value.decimal.mi < y->value.decimal.mi) |
| 2904 | return (-order); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 2905 | if (x->value.decimal.mi > y->value.decimal.mi) |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2906 | return (order); |
| 2907 | if (x->value.decimal.lo < y->value.decimal.lo) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2908 | return (-order); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2909 | if (x->value.decimal.lo > y->value.decimal.lo) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2910 | return(order); |
| 2911 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2912 | } |
| 2913 | if (y->value.decimal.frac > x->value.decimal.frac) { |
| 2914 | swp = y; |
| 2915 | y = x; |
| 2916 | x = swp; |
| 2917 | order = -order; |
| 2918 | } |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2919 | p = powten[x->value.decimal.frac - y->value.decimal.frac]; |
| 2920 | tmp = x->value.decimal.lo / p; |
| 2921 | if (tmp > y->value.decimal.lo) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2922 | return (order); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2923 | if (tmp < y->value.decimal.lo) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2924 | return (-order); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2925 | tmp = y->value.decimal.lo * p; |
| 2926 | if (x->value.decimal.lo < tmp) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2927 | return (-order); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 2928 | if (x->value.decimal.lo == tmp) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2929 | return (0); |
| 2930 | return (order); |
| 2931 | } |
| 2932 | |
| 2933 | /** |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2934 | * xmlSchemaCompareDurations: |
| 2935 | * @x: a first duration value |
| 2936 | * @y: a second duration value |
| 2937 | * |
| 2938 | * Compare 2 durations |
| 2939 | * |
| 2940 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 2941 | * case of error |
| 2942 | */ |
| 2943 | static int |
| 2944 | xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 2945 | { |
| 2946 | long carry, mon, day; |
| 2947 | double sec; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2948 | int invert = 1; |
| 2949 | long xmon, xday, myear, minday, maxday; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2950 | static const long dayRange [2][12] = { |
| 2951 | { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, }, |
| 2952 | { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} }; |
| 2953 | |
| 2954 | if ((x == NULL) || (y == NULL)) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 2955 | return -2; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2956 | |
| 2957 | /* months */ |
| 2958 | mon = x->value.dur.mon - y->value.dur.mon; |
| 2959 | |
| 2960 | /* seconds */ |
| 2961 | sec = x->value.dur.sec - y->value.dur.sec; |
| 2962 | carry = (long)sec / SECS_PER_DAY; |
| 2963 | sec -= (double)(carry * SECS_PER_DAY); |
| 2964 | |
| 2965 | /* days */ |
| 2966 | day = x->value.dur.day - y->value.dur.day + carry; |
| 2967 | |
| 2968 | /* easy test */ |
| 2969 | if (mon == 0) { |
| 2970 | if (day == 0) |
| 2971 | if (sec == 0.0) |
| 2972 | return 0; |
| 2973 | else if (sec < 0.0) |
| 2974 | return -1; |
| 2975 | else |
| 2976 | return 1; |
| 2977 | else if (day < 0) |
| 2978 | return -1; |
| 2979 | else |
| 2980 | return 1; |
| 2981 | } |
| 2982 | |
| 2983 | if (mon > 0) { |
| 2984 | if ((day >= 0) && (sec >= 0.0)) |
| 2985 | return 1; |
| 2986 | else { |
| 2987 | xmon = mon; |
| 2988 | xday = -day; |
| 2989 | } |
| 2990 | } else if ((day <= 0) && (sec <= 0.0)) { |
| 2991 | return -1; |
| 2992 | } else { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2993 | invert = -1; |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 2994 | xmon = -mon; |
| 2995 | xday = day; |
| 2996 | } |
| 2997 | |
| 2998 | myear = xmon / 12; |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 2999 | if (myear == 0) { |
| 3000 | minday = 0; |
| 3001 | maxday = 0; |
| 3002 | } else { |
| 3003 | maxday = 366 * ((myear + 3) / 4) + |
| 3004 | 365 * ((myear - 1) % 4); |
| 3005 | minday = maxday - 1; |
| 3006 | } |
| 3007 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3008 | xmon = xmon % 12; |
| 3009 | minday += dayRange[0][xmon]; |
| 3010 | maxday += dayRange[1][xmon]; |
| 3011 | |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3012 | if ((maxday == minday) && (maxday == xday)) |
| 3013 | return(0); /* can this really happen ? */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3014 | if (maxday < xday) |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 3015 | return(-invert); |
| 3016 | if (minday > xday) |
| 3017 | return(invert); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3018 | |
| 3019 | /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3020 | return 2; |
| 3021 | } |
| 3022 | |
| 3023 | /* |
| 3024 | * macros for adding date/times and durations |
| 3025 | */ |
| 3026 | #define FQUOTIENT(a,b) (floor(((double)a/(double)b))) |
| 3027 | #define MODULO(a,b) (a - FQUOTIENT(a,b) * b) |
| 3028 | #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low))) |
| 3029 | #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low) |
| 3030 | |
| 3031 | /** |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3032 | * xmlSchemaDupVal: |
| 3033 | * @v: the #xmlSchemaValPtr value to duplicate |
| 3034 | * |
| 3035 | * Makes a copy of @v. The calling program is responsible for freeing |
| 3036 | * the returned value. |
| 3037 | * |
| 3038 | * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error. |
| 3039 | */ |
| 3040 | static xmlSchemaValPtr |
| 3041 | xmlSchemaDupVal (xmlSchemaValPtr v) |
| 3042 | { |
| 3043 | xmlSchemaValPtr ret = xmlSchemaNewValue(v->type); |
| 3044 | if (ret == NULL) |
| 3045 | return NULL; |
| 3046 | |
| 3047 | memcpy(ret, v, sizeof(xmlSchemaVal)); |
| 3048 | return ret; |
| 3049 | } |
| 3050 | |
| 3051 | /** |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3052 | * _xmlSchemaDateAdd: |
| 3053 | * @dt: an #xmlSchemaValPtr |
| 3054 | * @dur: an #xmlSchemaValPtr of type #XS_DURATION |
| 3055 | * |
| 3056 | * Compute a new date/time from @dt and @dur. This function assumes @dt |
| 3057 | * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH, |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3058 | * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as |
| 3059 | * @dt. The calling program is responsible for freeing the returned value. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3060 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3061 | * Returns a pointer to a new #xmlSchemaVal or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3062 | */ |
| 3063 | static xmlSchemaValPtr |
| 3064 | _xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur) |
| 3065 | { |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3066 | xmlSchemaValPtr ret, tmp; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3067 | long carry, tempdays, temp; |
| 3068 | xmlSchemaValDatePtr r, d; |
| 3069 | xmlSchemaValDurationPtr u; |
| 3070 | |
| 3071 | if ((dt == NULL) || (dur == NULL)) |
| 3072 | return NULL; |
| 3073 | |
| 3074 | ret = xmlSchemaNewValue(dt->type); |
| 3075 | if (ret == NULL) |
| 3076 | return NULL; |
| 3077 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3078 | /* make a copy so we don't alter the original value */ |
| 3079 | tmp = xmlSchemaDupVal(dt); |
| 3080 | if (tmp == NULL) { |
| 3081 | xmlSchemaFreeValue(ret); |
| 3082 | return NULL; |
| 3083 | } |
| 3084 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3085 | r = &(ret->value.date); |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3086 | d = &(tmp->value.date); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3087 | u = &(dur->value.dur); |
| 3088 | |
| 3089 | /* normalization */ |
| 3090 | if (d->mon == 0) |
| 3091 | d->mon = 1; |
| 3092 | |
| 3093 | /* normalize for time zone offset */ |
| 3094 | u->sec -= (d->tzo * 60); |
| 3095 | d->tzo = 0; |
| 3096 | |
| 3097 | /* normalization */ |
| 3098 | if (d->day == 0) |
| 3099 | d->day = 1; |
| 3100 | |
| 3101 | /* month */ |
| 3102 | carry = d->mon + u->mon; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3103 | r->mon = (unsigned int) MODULO_RANGE(carry, 1, 13); |
| 3104 | carry = (long) FQUOTIENT_RANGE(carry, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3105 | |
| 3106 | /* year (may be modified later) */ |
| 3107 | r->year = d->year + carry; |
| 3108 | if (r->year == 0) { |
| 3109 | if (d->year > 0) |
| 3110 | r->year--; |
| 3111 | else |
| 3112 | r->year++; |
| 3113 | } |
| 3114 | |
| 3115 | /* time zone */ |
| 3116 | r->tzo = d->tzo; |
| 3117 | r->tz_flag = d->tz_flag; |
| 3118 | |
| 3119 | /* seconds */ |
| 3120 | r->sec = d->sec + u->sec; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3121 | carry = (long) FQUOTIENT((long)r->sec, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3122 | if (r->sec != 0.0) { |
| 3123 | r->sec = MODULO(r->sec, 60.0); |
| 3124 | } |
| 3125 | |
| 3126 | /* minute */ |
| 3127 | carry += d->min; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3128 | r->min = (unsigned int) MODULO(carry, 60); |
| 3129 | carry = (long) FQUOTIENT(carry, 60); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3130 | |
| 3131 | /* hours */ |
| 3132 | carry += d->hour; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3133 | r->hour = (unsigned int) MODULO(carry, 24); |
| 3134 | carry = (long)FQUOTIENT(carry, 24); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3135 | |
| 3136 | /* |
| 3137 | * days |
| 3138 | * Note we use tempdays because the temporary values may need more |
| 3139 | * than 5 bits |
| 3140 | */ |
| 3141 | if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) && |
| 3142 | (d->day > MAX_DAYINMONTH(r->year, r->mon))) |
| 3143 | tempdays = MAX_DAYINMONTH(r->year, r->mon); |
| 3144 | else if (d->day < 1) |
| 3145 | tempdays = 1; |
| 3146 | else |
| 3147 | tempdays = d->day; |
| 3148 | |
| 3149 | tempdays += u->day + carry; |
| 3150 | |
| 3151 | while (1) { |
| 3152 | if (tempdays < 1) { |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3153 | long tmon = (long) MODULO_RANGE(r->mon-1, 1, 13); |
| 3154 | long tyr = r->year + (long)FQUOTIENT_RANGE(r->mon-1, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3155 | if (tyr == 0) |
| 3156 | tyr--; |
| 3157 | tempdays += MAX_DAYINMONTH(tyr, tmon); |
| 3158 | carry = -1; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3159 | } else if (tempdays > (long) MAX_DAYINMONTH(r->year, r->mon)) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3160 | tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon); |
| 3161 | carry = 1; |
| 3162 | } else |
| 3163 | break; |
| 3164 | |
| 3165 | temp = r->mon + carry; |
Daniel Veillard | ebe25d4 | 2004-03-25 09:35:49 +0000 | [diff] [blame] | 3166 | r->mon = (unsigned int) MODULO_RANGE(temp, 1, 13); |
| 3167 | r->year = r->year + (unsigned int) FQUOTIENT_RANGE(temp, 1, 13); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3168 | if (r->year == 0) { |
| 3169 | if (temp < 1) |
| 3170 | r->year--; |
| 3171 | else |
| 3172 | r->year++; |
| 3173 | } |
| 3174 | } |
| 3175 | |
| 3176 | r->day = tempdays; |
| 3177 | |
| 3178 | /* |
| 3179 | * adjust the date/time type to the date values |
| 3180 | */ |
| 3181 | if (ret->type != XML_SCHEMAS_DATETIME) { |
| 3182 | if ((r->hour) || (r->min) || (r->sec)) |
| 3183 | ret->type = XML_SCHEMAS_DATETIME; |
| 3184 | else if (ret->type != XML_SCHEMAS_DATE) { |
| 3185 | if ((r->mon != 1) && (r->day != 1)) |
| 3186 | ret->type = XML_SCHEMAS_DATE; |
| 3187 | else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1)) |
| 3188 | ret->type = XML_SCHEMAS_GYEARMONTH; |
| 3189 | } |
| 3190 | } |
| 3191 | |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3192 | xmlSchemaFreeValue(tmp); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3193 | |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3194 | return ret; |
| 3195 | } |
| 3196 | |
| 3197 | /** |
| 3198 | * xmlSchemaDateNormalize: |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3199 | * @dt: an #xmlSchemaValPtr of a date/time type value. |
| 3200 | * @offset: number of seconds to adjust @dt by. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3201 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3202 | * Normalize @dt to GMT time. The @offset parameter is subtracted from |
| 3203 | * the return value is a time-zone offset is present on @dt. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3204 | * |
Daniel Veillard | 669adfc | 2004-05-29 20:12:46 +0000 | [diff] [blame] | 3205 | * Returns a normalized copy of @dt or NULL if error. |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3206 | */ |
| 3207 | static xmlSchemaValPtr |
| 3208 | xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset) |
| 3209 | { |
| 3210 | xmlSchemaValPtr dur, ret; |
| 3211 | |
| 3212 | if (dt == NULL) |
| 3213 | return NULL; |
| 3214 | |
| 3215 | if (((dt->type != XML_SCHEMAS_TIME) && |
| 3216 | (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0)) |
| 3217 | return xmlSchemaDupVal(dt); |
| 3218 | |
| 3219 | dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION); |
| 3220 | if (dur == NULL) |
| 3221 | return NULL; |
| 3222 | |
| 3223 | dur->value.date.sec -= offset; |
| 3224 | |
| 3225 | ret = _xmlSchemaDateAdd(dt, dur); |
| 3226 | if (ret == NULL) |
| 3227 | return NULL; |
| 3228 | |
| 3229 | xmlSchemaFreeValue(dur); |
| 3230 | |
| 3231 | /* ret->value.date.tzo = 0; */ |
| 3232 | return ret; |
| 3233 | } |
| 3234 | |
| 3235 | /** |
| 3236 | * _xmlSchemaDateCastYMToDays: |
| 3237 | * @dt: an #xmlSchemaValPtr |
| 3238 | * |
| 3239 | * Convert mon and year of @dt to total number of days. Take the |
| 3240 | * number of years since (or before) 1 AD and add the number of leap |
| 3241 | * years. This is a function because negative |
| 3242 | * years must be handled a little differently and there is no zero year. |
| 3243 | * |
| 3244 | * Returns number of days. |
| 3245 | */ |
| 3246 | static long |
| 3247 | _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt) |
| 3248 | { |
| 3249 | long ret; |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3250 | int mon; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3251 | |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3252 | mon = dt->value.date.mon; |
| 3253 | if (mon <= 0) mon = 1; /* normalization */ |
| 3254 | |
| 3255 | if (dt->value.date.year <= 0) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3256 | ret = (dt->value.date.year * 365) + |
| 3257 | (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+ |
| 3258 | ((dt->value.date.year+1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3259 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3260 | else |
| 3261 | ret = ((dt->value.date.year-1) * 365) + |
| 3262 | (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+ |
| 3263 | ((dt->value.date.year-1)/400)) + |
Daniel Veillard | 49e8963 | 2004-09-23 16:24:36 +0000 | [diff] [blame] | 3264 | DAY_IN_YEAR(0, mon, dt->value.date.year); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3265 | |
| 3266 | return ret; |
| 3267 | } |
| 3268 | |
| 3269 | /** |
| 3270 | * TIME_TO_NUMBER: |
| 3271 | * @dt: an #xmlSchemaValPtr |
| 3272 | * |
| 3273 | * Calculates the number of seconds in the time portion of @dt. |
| 3274 | * |
| 3275 | * Returns seconds. |
| 3276 | */ |
| 3277 | #define TIME_TO_NUMBER(dt) \ |
| 3278 | ((double)((dt->value.date.hour * SECS_PER_HOUR) + \ |
Daniel Veillard | b3721c2 | 2003-03-31 11:22:25 +0000 | [diff] [blame] | 3279 | (dt->value.date.min * SECS_PER_MIN) + \ |
| 3280 | (dt->value.date.tzo * SECS_PER_MIN)) + \ |
| 3281 | dt->value.date.sec) |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3282 | |
| 3283 | /** |
| 3284 | * xmlSchemaCompareDates: |
| 3285 | * @x: a first date/time value |
| 3286 | * @y: a second date/time value |
| 3287 | * |
| 3288 | * Compare 2 date/times |
| 3289 | * |
| 3290 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3291 | * case of error |
| 3292 | */ |
| 3293 | static int |
| 3294 | xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3295 | { |
| 3296 | unsigned char xmask, ymask, xor_mask, and_mask; |
| 3297 | xmlSchemaValPtr p1, p2, q1, q2; |
| 3298 | long p1d, p2d, q1d, q2d; |
| 3299 | |
| 3300 | if ((x == NULL) || (y == NULL)) |
| 3301 | return -2; |
| 3302 | |
| 3303 | if (x->value.date.tz_flag) { |
| 3304 | |
| 3305 | if (!y->value.date.tz_flag) { |
| 3306 | p1 = xmlSchemaDateNormalize(x, 0); |
| 3307 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3308 | /* normalize y + 14:00 */ |
| 3309 | q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR)); |
| 3310 | |
| 3311 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3312 | if (p1d < q1d) { |
| 3313 | xmlSchemaFreeValue(p1); |
| 3314 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3315 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3316 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3317 | double sec; |
| 3318 | |
| 3319 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3320 | if (sec < 0.0) { |
| 3321 | xmlSchemaFreeValue(p1); |
| 3322 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3323 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3324 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3325 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3326 | /* normalize y - 14:00 */ |
| 3327 | q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR)); |
| 3328 | q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day; |
| 3329 | if (p1d > q2d) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3330 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3331 | else if (p1d == q2d) { |
| 3332 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2); |
| 3333 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3334 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3335 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3336 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3337 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3338 | xmlSchemaFreeValue(p1); |
| 3339 | xmlSchemaFreeValue(q1); |
| 3340 | xmlSchemaFreeValue(q2); |
| 3341 | if (ret != 0) |
| 3342 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3343 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3344 | } else { |
| 3345 | xmlSchemaFreeValue(p1); |
| 3346 | xmlSchemaFreeValue(q1); |
| 3347 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3348 | } |
| 3349 | } else if (y->value.date.tz_flag) { |
| 3350 | q1 = xmlSchemaDateNormalize(y, 0); |
| 3351 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 3352 | |
| 3353 | /* normalize x - 14:00 */ |
| 3354 | p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR)); |
| 3355 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3356 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3357 | if (p1d < q1d) { |
| 3358 | xmlSchemaFreeValue(p1); |
| 3359 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3360 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3361 | } else if (p1d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3362 | double sec; |
| 3363 | |
| 3364 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3365 | if (sec < 0.0) { |
| 3366 | xmlSchemaFreeValue(p1); |
| 3367 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3368 | return -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3369 | } else { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3370 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3371 | /* normalize x + 14:00 */ |
| 3372 | p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR)); |
| 3373 | p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day; |
| 3374 | |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3375 | if (p2d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3376 | ret = 1; |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3377 | } else if (p2d == q1d) { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3378 | sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1); |
| 3379 | if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3380 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3381 | else |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3382 | ret = 2; /* indeterminate */ |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3383 | } |
Daniel Veillard | 6560a42 | 2003-03-27 21:25:38 +0000 | [diff] [blame] | 3384 | xmlSchemaFreeValue(p1); |
| 3385 | xmlSchemaFreeValue(q1); |
| 3386 | xmlSchemaFreeValue(p2); |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3387 | if (ret != 0) |
| 3388 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3389 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3390 | } else { |
| 3391 | xmlSchemaFreeValue(p1); |
| 3392 | xmlSchemaFreeValue(q1); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | /* |
| 3397 | * if the same type then calculate the difference |
| 3398 | */ |
| 3399 | if (x->type == y->type) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3400 | int ret = 0; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3401 | q1 = xmlSchemaDateNormalize(y, 0); |
| 3402 | q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day; |
| 3403 | |
| 3404 | p1 = xmlSchemaDateNormalize(x, 0); |
| 3405 | p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day; |
| 3406 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3407 | if (p1d < q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3408 | ret = -1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3409 | } else if (p1d > q1d) { |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3410 | ret = 1; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 3411 | } else { |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3412 | double sec; |
| 3413 | |
| 3414 | sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1); |
| 3415 | if (sec < 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3416 | ret = -1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3417 | else if (sec > 0.0) |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3418 | ret = 1; |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3419 | |
| 3420 | } |
Daniel Veillard | 4aede2e | 2003-10-17 12:43:59 +0000 | [diff] [blame] | 3421 | xmlSchemaFreeValue(p1); |
| 3422 | xmlSchemaFreeValue(q1); |
| 3423 | return(ret); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | switch (x->type) { |
| 3427 | case XML_SCHEMAS_DATETIME: |
| 3428 | xmask = 0xf; |
| 3429 | break; |
| 3430 | case XML_SCHEMAS_DATE: |
| 3431 | xmask = 0x7; |
| 3432 | break; |
| 3433 | case XML_SCHEMAS_GYEAR: |
| 3434 | xmask = 0x1; |
| 3435 | break; |
| 3436 | case XML_SCHEMAS_GMONTH: |
| 3437 | xmask = 0x2; |
| 3438 | break; |
| 3439 | case XML_SCHEMAS_GDAY: |
| 3440 | xmask = 0x3; |
| 3441 | break; |
| 3442 | case XML_SCHEMAS_GYEARMONTH: |
| 3443 | xmask = 0x3; |
| 3444 | break; |
| 3445 | case XML_SCHEMAS_GMONTHDAY: |
| 3446 | xmask = 0x6; |
| 3447 | break; |
| 3448 | case XML_SCHEMAS_TIME: |
| 3449 | xmask = 0x8; |
| 3450 | break; |
| 3451 | default: |
| 3452 | xmask = 0; |
| 3453 | break; |
| 3454 | } |
| 3455 | |
| 3456 | switch (y->type) { |
| 3457 | case XML_SCHEMAS_DATETIME: |
| 3458 | ymask = 0xf; |
| 3459 | break; |
| 3460 | case XML_SCHEMAS_DATE: |
| 3461 | ymask = 0x7; |
| 3462 | break; |
| 3463 | case XML_SCHEMAS_GYEAR: |
| 3464 | ymask = 0x1; |
| 3465 | break; |
| 3466 | case XML_SCHEMAS_GMONTH: |
| 3467 | ymask = 0x2; |
| 3468 | break; |
| 3469 | case XML_SCHEMAS_GDAY: |
| 3470 | ymask = 0x3; |
| 3471 | break; |
| 3472 | case XML_SCHEMAS_GYEARMONTH: |
| 3473 | ymask = 0x3; |
| 3474 | break; |
| 3475 | case XML_SCHEMAS_GMONTHDAY: |
| 3476 | ymask = 0x6; |
| 3477 | break; |
| 3478 | case XML_SCHEMAS_TIME: |
| 3479 | ymask = 0x8; |
| 3480 | break; |
| 3481 | default: |
| 3482 | ymask = 0; |
| 3483 | break; |
| 3484 | } |
| 3485 | |
| 3486 | xor_mask = xmask ^ ymask; /* mark type differences */ |
| 3487 | and_mask = xmask & ymask; /* mark field specification */ |
| 3488 | |
| 3489 | /* year */ |
| 3490 | if (xor_mask & 1) |
| 3491 | return 2; /* indeterminate */ |
| 3492 | else if (and_mask & 1) { |
| 3493 | if (x->value.date.year < y->value.date.year) |
| 3494 | return -1; |
| 3495 | else if (x->value.date.year > y->value.date.year) |
| 3496 | return 1; |
| 3497 | } |
| 3498 | |
| 3499 | /* month */ |
| 3500 | if (xor_mask & 2) |
| 3501 | return 2; /* indeterminate */ |
| 3502 | else if (and_mask & 2) { |
| 3503 | if (x->value.date.mon < y->value.date.mon) |
| 3504 | return -1; |
| 3505 | else if (x->value.date.mon > y->value.date.mon) |
| 3506 | return 1; |
| 3507 | } |
| 3508 | |
| 3509 | /* day */ |
| 3510 | if (xor_mask & 4) |
| 3511 | return 2; /* indeterminate */ |
| 3512 | else if (and_mask & 4) { |
| 3513 | if (x->value.date.day < y->value.date.day) |
| 3514 | return -1; |
| 3515 | else if (x->value.date.day > y->value.date.day) |
| 3516 | return 1; |
| 3517 | } |
| 3518 | |
| 3519 | /* time */ |
| 3520 | if (xor_mask & 8) |
| 3521 | return 2; /* indeterminate */ |
| 3522 | else if (and_mask & 8) { |
| 3523 | if (x->value.date.hour < y->value.date.hour) |
| 3524 | return -1; |
| 3525 | else if (x->value.date.hour > y->value.date.hour) |
| 3526 | return 1; |
| 3527 | else if (x->value.date.min < y->value.date.min) |
| 3528 | return -1; |
| 3529 | else if (x->value.date.min > y->value.date.min) |
| 3530 | return 1; |
| 3531 | else if (x->value.date.sec < y->value.date.sec) |
| 3532 | return -1; |
| 3533 | else if (x->value.date.sec > y->value.date.sec) |
| 3534 | return 1; |
| 3535 | } |
| 3536 | |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 3537 | return 0; |
| 3538 | } |
| 3539 | |
| 3540 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 3541 | * xmlSchemaComparePreserveReplaceStrings: |
| 3542 | * @x: a first string value |
| 3543 | * @y: a second string value |
| 3544 | * @invert: inverts the result if x < y or x > y. |
| 3545 | * |
| 3546 | * Compare 2 string for their normalized values. |
| 3547 | * @x is a string with whitespace of "preserve", @y is |
| 3548 | * a string with a whitespace of "replace". I.e. @x could |
| 3549 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 3550 | * |
| 3551 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 3552 | * case of error |
| 3553 | */ |
| 3554 | static int |
| 3555 | xmlSchemaComparePreserveReplaceStrings(xmlSchemaValPtr x, xmlSchemaValPtr y, |
| 3556 | int invert) |
| 3557 | { |
| 3558 | const xmlChar *utf1; |
| 3559 | const xmlChar *utf2; |
| 3560 | int tmp; |
| 3561 | |
| 3562 | if ((x == NULL) || (y == NULL)) |
| 3563 | return(-2); |
| 3564 | utf1 = x->value.str; |
| 3565 | utf2 = y->value.str; |
| 3566 | |
| 3567 | while ((*utf1 != 0) && (*utf2 != 0)) { |
| 3568 | if (IS_WSP_REPLACE_CH(*utf2)) { |
| 3569 | if (! IS_WSP_SPACE_CH(*utf1)) { |
| 3570 | if ((*utf1 - 0x20) < 0) { |
| 3571 | if (invert) |
| 3572 | return(1); |
| 3573 | else |
| 3574 | return(-1); |
| 3575 | } else { |
| 3576 | if (invert) |
| 3577 | return(-1); |
| 3578 | else |
| 3579 | return(1); |
| 3580 | } |
| 3581 | } |
| 3582 | } else { |
| 3583 | tmp = *utf1 - *utf2; |
| 3584 | if (tmp < 0) { |
| 3585 | if (invert) |
| 3586 | return(1); |
| 3587 | else |
| 3588 | return(-1); |
| 3589 | } |
| 3590 | if (tmp > 0) { |
| 3591 | if (invert) |
| 3592 | return(-1); |
| 3593 | else |
| 3594 | return(1); |
| 3595 | } |
| 3596 | } |
| 3597 | utf1++; |
| 3598 | utf2++; |
| 3599 | } |
| 3600 | if (*utf1 != 0) { |
| 3601 | if (invert) |
| 3602 | return(-1); |
| 3603 | else |
| 3604 | return(1); |
| 3605 | } |
| 3606 | if (*utf2 != 0) { |
| 3607 | if (invert) |
| 3608 | return(1); |
| 3609 | else |
| 3610 | return(-1); |
| 3611 | } |
| 3612 | return(0); |
| 3613 | } |
| 3614 | |
| 3615 | /** |
| 3616 | * xmlSchemaComparePreserveCollapseStrings: |
| 3617 | * @x: a first string value |
| 3618 | * @y: a second string value |
| 3619 | * |
| 3620 | * Compare 2 string for their normalized values. |
| 3621 | * @x is a string with whitespace of "preserve", @y is |
| 3622 | * a string with a whitespace of "collapse". I.e. @x could |
| 3623 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 3624 | * |
| 3625 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 3626 | * case of error |
| 3627 | */ |
| 3628 | static int |
| 3629 | xmlSchemaComparePreserveCollapseStrings(xmlSchemaValPtr x, xmlSchemaValPtr y, |
| 3630 | int invert) |
| 3631 | { |
| 3632 | const xmlChar *utf1; |
| 3633 | const xmlChar *utf2; |
| 3634 | int tmp; |
| 3635 | |
| 3636 | if ((x == NULL) || (y == NULL)) |
| 3637 | return(-2); |
| 3638 | utf1 = x->value.str; |
| 3639 | utf2 = y->value.str; |
| 3640 | |
| 3641 | /* |
| 3642 | * Skip leading blank chars of the collapsed string. |
| 3643 | */ |
| 3644 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3645 | utf2++; |
| 3646 | |
| 3647 | while ((*utf1 != 0) && (*utf2 != 0)) { |
| 3648 | if (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) { |
| 3649 | if (! IS_WSP_SPACE_CH(*utf1)) { |
| 3650 | /* |
| 3651 | * The utf2 character would have been replaced to 0x20. |
| 3652 | */ |
| 3653 | if ((*utf1 - 0x20) < 0) { |
| 3654 | if (invert) |
| 3655 | return(1); |
| 3656 | else |
| 3657 | return(-1); |
| 3658 | } else { |
| 3659 | if (invert) |
| 3660 | return(-1); |
| 3661 | else |
| 3662 | return(1); |
| 3663 | } |
| 3664 | } |
| 3665 | utf1++; |
| 3666 | utf2++; |
| 3667 | /* |
| 3668 | * Skip contiguous blank chars of the collapsed string. |
| 3669 | */ |
| 3670 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3671 | utf2++; |
| 3672 | } else { |
| 3673 | tmp = *utf1++ - *utf2++; |
| 3674 | if (tmp < 0) { |
| 3675 | if (invert) |
| 3676 | return(1); |
| 3677 | else |
| 3678 | return(-1); |
| 3679 | } |
| 3680 | if (tmp > 0) { |
| 3681 | if (invert) |
| 3682 | return(-1); |
| 3683 | else |
| 3684 | return(1); |
| 3685 | } |
| 3686 | } |
| 3687 | } |
| 3688 | if (*utf1 != 0) { |
| 3689 | if (invert) |
| 3690 | return(-1); |
| 3691 | else |
| 3692 | return(1); |
| 3693 | } |
| 3694 | if (*utf2 != 0) { |
| 3695 | /* |
| 3696 | * Skip trailing blank chars of the collapsed string. |
| 3697 | */ |
| 3698 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3699 | utf2++; |
| 3700 | if (*utf2 != 0) { |
| 3701 | if (invert) |
| 3702 | return(1); |
| 3703 | else |
| 3704 | return(-1); |
| 3705 | } |
| 3706 | } |
| 3707 | return(0); |
| 3708 | } |
| 3709 | |
| 3710 | /** |
| 3711 | * xmlSchemaComparePreserveCollapseStrings: |
| 3712 | * @x: a first string value |
| 3713 | * @y: a second string value |
| 3714 | * |
| 3715 | * Compare 2 string for their normalized values. |
| 3716 | * @x is a string with whitespace of "preserve", @y is |
| 3717 | * a string with a whitespace of "collapse". I.e. @x could |
| 3718 | * be an "xsd:string" and @y an "xsd:normalizedString". |
| 3719 | * |
| 3720 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 3721 | * case of error |
| 3722 | */ |
| 3723 | static int |
| 3724 | xmlSchemaCompareReplaceCollapseStrings(xmlSchemaValPtr x, xmlSchemaValPtr y, |
| 3725 | int invert) |
| 3726 | { |
| 3727 | const xmlChar *utf1; |
| 3728 | const xmlChar *utf2; |
| 3729 | int tmp; |
| 3730 | |
| 3731 | if ((x == NULL) || (y == NULL)) |
| 3732 | return(-2); |
| 3733 | utf1 = x->value.str; |
| 3734 | utf2 = y->value.str; |
| 3735 | |
| 3736 | /* |
| 3737 | * Skip leading blank chars of the collapsed string. |
| 3738 | */ |
| 3739 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3740 | utf2++; |
| 3741 | |
| 3742 | while ((*utf1 != 0) && (*utf2 != 0)) { |
| 3743 | if (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) { |
| 3744 | if (! (IS_WSP_SPACE_CH(*utf1) || IS_WSP_REPLACE_CH(*utf1))) { |
| 3745 | /* |
| 3746 | * The utf2 character would have been replaced to 0x20. |
| 3747 | */ |
| 3748 | if ((*utf1 - 0x20) < 0) { |
| 3749 | if (invert) |
| 3750 | return(1); |
| 3751 | else |
| 3752 | return(-1); |
| 3753 | } else { |
| 3754 | if (invert) |
| 3755 | return(-1); |
| 3756 | else |
| 3757 | return(1); |
| 3758 | } |
| 3759 | } |
| 3760 | utf1++; |
| 3761 | utf2++; |
| 3762 | /* |
| 3763 | * Skip contiguous blank chars of the collapsed string. |
| 3764 | */ |
| 3765 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3766 | utf2++; |
| 3767 | } else { |
| 3768 | if (IS_WSP_SPACE_CH(*utf1) || IS_WSP_REPLACE_CH(*utf1)) { |
| 3769 | /* |
| 3770 | * The utf1 character would have been replaced to 0x20. |
| 3771 | */ |
| 3772 | if ((0x20 - *utf2) < 0) { |
| 3773 | if (invert) |
| 3774 | return(1); |
| 3775 | else |
| 3776 | return(-1); |
| 3777 | } else { |
| 3778 | if (invert) |
| 3779 | return(-1); |
| 3780 | else |
| 3781 | return(1); |
| 3782 | } |
| 3783 | } |
| 3784 | tmp = *utf1++ - *utf2++; |
| 3785 | if (tmp < 0) |
| 3786 | return(-1); |
| 3787 | if (tmp > 0) |
| 3788 | return(1); |
| 3789 | } |
| 3790 | } |
| 3791 | if (*utf1 != 0) { |
| 3792 | if (invert) |
| 3793 | return(-1); |
| 3794 | else |
| 3795 | return(1); |
| 3796 | } |
| 3797 | if (*utf2 != 0) { |
| 3798 | /* |
| 3799 | * Skip trailing blank chars of the collapsed string. |
| 3800 | */ |
| 3801 | while (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) |
| 3802 | utf2++; |
| 3803 | if (*utf2 != 0) { |
| 3804 | if (invert) |
| 3805 | return(1); |
| 3806 | else |
| 3807 | return(-1); |
| 3808 | } |
| 3809 | } |
| 3810 | return(0); |
| 3811 | } |
| 3812 | |
| 3813 | |
| 3814 | /** |
| 3815 | * xmlSchemaCompareReplacedStrings: |
| 3816 | * @x: a first string value |
| 3817 | * @y: a second string value |
| 3818 | * |
| 3819 | * Compare 2 string for their normalized values. |
| 3820 | * |
| 3821 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 3822 | * case of error |
| 3823 | */ |
| 3824 | static int |
| 3825 | xmlSchemaCompareReplacedStrings(xmlSchemaValPtr x, xmlSchemaValPtr y) |
| 3826 | { |
| 3827 | const xmlChar *utf1; |
| 3828 | const xmlChar *utf2; |
| 3829 | int tmp; |
| 3830 | |
| 3831 | if ((x == NULL) || (y == NULL)) |
| 3832 | return(-2); |
| 3833 | utf1 = x->value.str; |
| 3834 | utf2 = y->value.str; |
| 3835 | |
| 3836 | while ((*utf1 != 0) && (*utf2 != 0)) { |
| 3837 | if (IS_WSP_SPACE_CH(*utf2) || IS_WSP_REPLACE_CH(*utf2)) { |
| 3838 | if (! (IS_WSP_SPACE_CH(*utf1) || IS_WSP_REPLACE_CH(*utf1))) { |
| 3839 | if ((*utf1 - 0x20) < 0) |
| 3840 | return(-1); |
| 3841 | else |
| 3842 | return(1); |
| 3843 | } |
| 3844 | } else { |
| 3845 | if (IS_WSP_SPACE_CH(*utf1) || IS_WSP_REPLACE_CH(*utf1)) { |
| 3846 | if ((0x20 - *utf2) < 0) |
| 3847 | return(-1); |
| 3848 | else |
| 3849 | return(1); |
| 3850 | } |
| 3851 | tmp = *utf1 - *utf2; |
| 3852 | if (tmp < 0) |
| 3853 | return(-1); |
| 3854 | if (tmp > 0) |
| 3855 | return(1); |
| 3856 | } |
| 3857 | utf1++; |
| 3858 | utf2++; |
| 3859 | } |
| 3860 | if (*utf1 != 0) |
| 3861 | return(1); |
| 3862 | if (*utf2 != 0) |
| 3863 | return(-1); |
| 3864 | return(0); |
| 3865 | } |
| 3866 | |
| 3867 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3868 | * xmlSchemaCompareNormStrings: |
| 3869 | * @x: a first string value |
| 3870 | * @y: a second string value |
| 3871 | * |
| 3872 | * Compare 2 string for their normalized values. |
| 3873 | * |
| 3874 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in |
| 3875 | * case of error |
| 3876 | */ |
| 3877 | static int |
| 3878 | xmlSchemaCompareNormStrings(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 3879 | const xmlChar *utf1; |
| 3880 | const xmlChar *utf2; |
| 3881 | int tmp; |
| 3882 | |
| 3883 | if ((x == NULL) || (y == NULL)) |
| 3884 | return(-2); |
| 3885 | utf1 = x->value.str; |
| 3886 | utf2 = y->value.str; |
| 3887 | |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3888 | while (IS_BLANK_CH(*utf1)) utf1++; |
| 3889 | while (IS_BLANK_CH(*utf2)) utf2++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3890 | while ((*utf1 != 0) && (*utf2 != 0)) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3891 | if (IS_BLANK_CH(*utf1)) { |
| 3892 | if (!IS_BLANK_CH(*utf2)) { |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3893 | tmp = *utf1 - *utf2; |
| 3894 | return(tmp); |
| 3895 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3896 | while (IS_BLANK_CH(*utf1)) utf1++; |
| 3897 | while (IS_BLANK_CH(*utf2)) utf2++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3898 | } else { |
| 3899 | tmp = *utf1++ - *utf2++; |
| 3900 | if (tmp < 0) |
| 3901 | return(-1); |
| 3902 | if (tmp > 0) |
| 3903 | return(1); |
| 3904 | } |
| 3905 | } |
| 3906 | if (*utf1 != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3907 | while (IS_BLANK_CH(*utf1)) utf1++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3908 | if (*utf1 != 0) |
| 3909 | return(1); |
| 3910 | } |
| 3911 | if (*utf2 != 0) { |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 3912 | while (IS_BLANK_CH(*utf2)) utf2++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 3913 | if (*utf2 != 0) |
| 3914 | return(-1); |
| 3915 | } |
| 3916 | return(0); |
| 3917 | } |
| 3918 | |
| 3919 | /** |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 3920 | * xmlSchemaCompareFloats: |
| 3921 | * @x: a first float or double value |
| 3922 | * @y: a second float or double value |
| 3923 | * |
| 3924 | * Compare 2 values |
| 3925 | * |
| 3926 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 3927 | * case of error |
| 3928 | */ |
| 3929 | static int |
| 3930 | xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 3931 | double d1, d2; |
| 3932 | |
| 3933 | if ((x == NULL) || (y == NULL)) |
| 3934 | return(-2); |
| 3935 | |
| 3936 | /* |
| 3937 | * Cast everything to doubles. |
| 3938 | */ |
| 3939 | if (x->type == XML_SCHEMAS_DOUBLE) |
| 3940 | d1 = x->value.d; |
| 3941 | else if (x->type == XML_SCHEMAS_FLOAT) |
| 3942 | d1 = x->value.f; |
| 3943 | else |
| 3944 | return(-2); |
| 3945 | |
| 3946 | if (y->type == XML_SCHEMAS_DOUBLE) |
| 3947 | d2 = y->value.d; |
| 3948 | else if (y->type == XML_SCHEMAS_FLOAT) |
| 3949 | d2 = y->value.f; |
| 3950 | else |
| 3951 | return(-2); |
| 3952 | |
| 3953 | /* |
| 3954 | * Check for special cases. |
| 3955 | */ |
| 3956 | if (xmlXPathIsNaN(d1)) { |
| 3957 | if (xmlXPathIsNaN(d2)) |
| 3958 | return(0); |
| 3959 | return(1); |
| 3960 | } |
| 3961 | if (xmlXPathIsNaN(d2)) |
| 3962 | return(-1); |
| 3963 | if (d1 == xmlXPathPINF) { |
| 3964 | if (d2 == xmlXPathPINF) |
| 3965 | return(0); |
| 3966 | return(1); |
| 3967 | } |
| 3968 | if (d2 == xmlXPathPINF) |
| 3969 | return(-1); |
| 3970 | if (d1 == xmlXPathNINF) { |
| 3971 | if (d2 == xmlXPathNINF) |
| 3972 | return(0); |
| 3973 | return(-1); |
| 3974 | } |
| 3975 | if (d2 == xmlXPathNINF) |
| 3976 | return(1); |
| 3977 | |
| 3978 | /* |
| 3979 | * basic tests, the last one we should have equality, but |
| 3980 | * portability is more important than speed and handling |
| 3981 | * NaN or Inf in a portable way is always a challenge, so ... |
| 3982 | */ |
| 3983 | if (d1 < d2) |
| 3984 | return(-1); |
| 3985 | if (d1 > d2) |
| 3986 | return(1); |
| 3987 | if (d1 == d2) |
| 3988 | return(0); |
| 3989 | return(2); |
| 3990 | } |
| 3991 | |
| 3992 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3993 | * xmlSchemaCompareValues: |
| 3994 | * @x: a first value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 3995 | * @xwtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3996 | * @y: a second value |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 3997 | * @ywtsp: the whitespace type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3998 | * |
| 3999 | * Compare 2 values |
| 4000 | * |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4001 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4002 | * case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4003 | */ |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4004 | static int |
| 4005 | xmlSchemaCompareValuesInternal(xmlSchemaValPtr x, |
| 4006 | xmlSchemaWhitespaceValueType xws, |
| 4007 | xmlSchemaValPtr y, |
| 4008 | xmlSchemaWhitespaceValueType yws) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4009 | if ((x == NULL) || (y == NULL)) |
| 4010 | return(-2); |
| 4011 | |
| 4012 | switch (x->type) { |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4013 | case XML_SCHEMAS_UNKNOWN: |
William M. Brack | 2f2a663 | 2004-08-20 23:09:47 +0000 | [diff] [blame] | 4014 | case XML_SCHEMAS_ANYTYPE: |
| 4015 | case XML_SCHEMAS_ANYSIMPLETYPE: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4016 | return(-2); |
| 4017 | case XML_SCHEMAS_INTEGER: |
| 4018 | case XML_SCHEMAS_NPINTEGER: |
| 4019 | case XML_SCHEMAS_NINTEGER: |
| 4020 | case XML_SCHEMAS_NNINTEGER: |
| 4021 | case XML_SCHEMAS_PINTEGER: |
| 4022 | case XML_SCHEMAS_INT: |
| 4023 | case XML_SCHEMAS_UINT: |
| 4024 | case XML_SCHEMAS_LONG: |
| 4025 | case XML_SCHEMAS_ULONG: |
| 4026 | case XML_SCHEMAS_SHORT: |
| 4027 | case XML_SCHEMAS_USHORT: |
| 4028 | case XML_SCHEMAS_BYTE: |
| 4029 | case XML_SCHEMAS_UBYTE: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4030 | case XML_SCHEMAS_DECIMAL: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4031 | if (y->type == x->type) |
| 4032 | return(xmlSchemaCompareDecimals(x, y)); |
| 4033 | if ((y->type == XML_SCHEMAS_DECIMAL) || |
| 4034 | (y->type == XML_SCHEMAS_INTEGER) || |
| 4035 | (y->type == XML_SCHEMAS_NPINTEGER) || |
| 4036 | (y->type == XML_SCHEMAS_NINTEGER) || |
| 4037 | (y->type == XML_SCHEMAS_NNINTEGER) || |
| 4038 | (y->type == XML_SCHEMAS_PINTEGER) || |
| 4039 | (y->type == XML_SCHEMAS_INT) || |
| 4040 | (y->type == XML_SCHEMAS_UINT) || |
| 4041 | (y->type == XML_SCHEMAS_LONG) || |
| 4042 | (y->type == XML_SCHEMAS_ULONG) || |
| 4043 | (y->type == XML_SCHEMAS_SHORT) || |
| 4044 | (y->type == XML_SCHEMAS_USHORT) || |
| 4045 | (y->type == XML_SCHEMAS_BYTE) || |
| 4046 | (y->type == XML_SCHEMAS_UBYTE)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4047 | return(xmlSchemaCompareDecimals(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4048 | return(-2); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4049 | case XML_SCHEMAS_DURATION: |
| 4050 | if (y->type == XML_SCHEMAS_DURATION) |
| 4051 | return(xmlSchemaCompareDurations(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4052 | return(-2); |
| 4053 | case XML_SCHEMAS_TIME: |
| 4054 | case XML_SCHEMAS_GDAY: |
| 4055 | case XML_SCHEMAS_GMONTH: |
| 4056 | case XML_SCHEMAS_GMONTHDAY: |
| 4057 | case XML_SCHEMAS_GYEAR: |
| 4058 | case XML_SCHEMAS_GYEARMONTH: |
| 4059 | case XML_SCHEMAS_DATE: |
| 4060 | case XML_SCHEMAS_DATETIME: |
| 4061 | if ((y->type == XML_SCHEMAS_DATETIME) || |
| 4062 | (y->type == XML_SCHEMAS_TIME) || |
| 4063 | (y->type == XML_SCHEMAS_GDAY) || |
| 4064 | (y->type == XML_SCHEMAS_GMONTH) || |
| 4065 | (y->type == XML_SCHEMAS_GMONTHDAY) || |
| 4066 | (y->type == XML_SCHEMAS_GYEAR) || |
| 4067 | (y->type == XML_SCHEMAS_DATE) || |
| 4068 | (y->type == XML_SCHEMAS_GYEARMONTH)) |
| 4069 | return (xmlSchemaCompareDates(x, y)); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4070 | return (-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4071 | case XML_SCHEMAS_STRING: |
| 4072 | case XML_SCHEMAS_NORMSTRING: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4073 | case XML_SCHEMAS_TOKEN: |
| 4074 | case XML_SCHEMAS_LANGUAGE: |
| 4075 | case XML_SCHEMAS_NMTOKEN: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4076 | case XML_SCHEMAS_NAME: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4077 | case XML_SCHEMAS_NCNAME: |
| 4078 | case XML_SCHEMAS_ID: |
| 4079 | case XML_SCHEMAS_IDREF: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4080 | case XML_SCHEMAS_ENTITY: |
Daniel Veillard | 80b1909 | 2003-03-28 13:29:53 +0000 | [diff] [blame] | 4081 | case XML_SCHEMAS_NOTATION: |
| 4082 | case XML_SCHEMAS_ANYURI: |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4083 | /* |
| 4084 | * TODO: Compare those against QName. |
| 4085 | */ |
| 4086 | if (y->type == XML_SCHEMAS_QNAME) { |
| 4087 | TODO |
| 4088 | return (-2); |
| 4089 | } |
| 4090 | if ((y->type == XML_SCHEMAS_STRING) || |
| 4091 | (y->type == XML_SCHEMAS_NORMSTRING) || |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4092 | (y->type == XML_SCHEMAS_TOKEN) || |
| 4093 | (y->type == XML_SCHEMAS_LANGUAGE) || |
| 4094 | (y->type == XML_SCHEMAS_NMTOKEN) || |
| 4095 | (y->type == XML_SCHEMAS_NAME) || |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4096 | (y->type == XML_SCHEMAS_NCNAME) || |
| 4097 | (y->type == XML_SCHEMAS_ID) || |
| 4098 | (y->type == XML_SCHEMAS_IDREF) || |
| 4099 | (y->type == XML_SCHEMAS_ENTITY) || |
| 4100 | (y->type == XML_SCHEMAS_NOTATION) || |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4101 | (y->type == XML_SCHEMAS_ANYURI)) { |
| 4102 | |
| 4103 | if (xws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4104 | |
| 4105 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) { |
| 4106 | /* TODO: What about x < y or x > y. */ |
| 4107 | if (xmlStrEqual(x->value.str, y->value.str)) |
| 4108 | return (0); |
| 4109 | else |
| 4110 | return (2); |
| 4111 | } else if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 4112 | return (xmlSchemaComparePreserveReplaceStrings(x, y, 0)); |
| 4113 | else if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 4114 | return (xmlSchemaComparePreserveCollapseStrings(x, y, 0)); |
| 4115 | |
| 4116 | } else if (xws == XML_SCHEMA_WHITESPACE_REPLACE) { |
| 4117 | |
| 4118 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
| 4119 | return (xmlSchemaComparePreserveReplaceStrings(y, x, 1)); |
| 4120 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 4121 | return (xmlSchemaCompareReplacedStrings(x, y)); |
| 4122 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 4123 | return (xmlSchemaCompareReplaceCollapseStrings(x, y, 0)); |
| 4124 | |
| 4125 | } else if (xws == XML_SCHEMA_WHITESPACE_COLLAPSE) { |
| 4126 | |
| 4127 | if (yws == XML_SCHEMA_WHITESPACE_PRESERVE) |
| 4128 | return (xmlSchemaComparePreserveCollapseStrings(y, x, 1)); |
| 4129 | if (yws == XML_SCHEMA_WHITESPACE_REPLACE) |
| 4130 | return (xmlSchemaCompareReplaceCollapseStrings(y, x, 1)); |
| 4131 | if (yws == XML_SCHEMA_WHITESPACE_COLLAPSE) |
| 4132 | return (xmlSchemaCompareNormStrings(x, y)); |
| 4133 | } else |
| 4134 | return (-2); |
| 4135 | |
| 4136 | } |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4137 | return (-2); |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4138 | case XML_SCHEMAS_QNAME: |
| 4139 | if (y->type == XML_SCHEMAS_QNAME) { |
| 4140 | if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) && |
| 4141 | (xmlStrEqual(x->value.qname.uri, y->value.qname.uri))) |
| 4142 | return(0); |
| 4143 | return(2); |
| 4144 | } |
| 4145 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4146 | case XML_SCHEMAS_FLOAT: |
| 4147 | case XML_SCHEMAS_DOUBLE: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4148 | if ((y->type == XML_SCHEMAS_FLOAT) || |
| 4149 | (y->type == XML_SCHEMAS_DOUBLE)) |
| 4150 | return (xmlSchemaCompareFloats(x, y)); |
| 4151 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4152 | case XML_SCHEMAS_BOOLEAN: |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4153 | if (y->type == XML_SCHEMAS_BOOLEAN) { |
| 4154 | if (x->value.b == y->value.b) |
| 4155 | return(0); |
| 4156 | if (x->value.b == 0) |
| 4157 | return(-1); |
| 4158 | return(1); |
| 4159 | } |
| 4160 | return (-2); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4161 | case XML_SCHEMAS_HEXBINARY: |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4162 | if (y->type == XML_SCHEMAS_HEXBINARY) { |
| 4163 | if (x->value.hex.total == y->value.hex.total) { |
| 4164 | int ret = xmlStrcmp(x->value.hex.str, y->value.hex.str); |
| 4165 | if (ret > 0) |
| 4166 | return(1); |
| 4167 | else if (ret == 0) |
| 4168 | return(0); |
| 4169 | } |
| 4170 | else if (x->value.hex.total > y->value.hex.total) |
| 4171 | return(1); |
| 4172 | |
| 4173 | return(-1); |
| 4174 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4175 | return (-2); |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4176 | case XML_SCHEMAS_BASE64BINARY: |
| 4177 | if (y->type == XML_SCHEMAS_BASE64BINARY) { |
| 4178 | if (x->value.base64.total == y->value.base64.total) { |
| 4179 | int ret = xmlStrcmp(x->value.base64.str, |
| 4180 | y->value.base64.str); |
| 4181 | if (ret > 0) |
| 4182 | return(1); |
| 4183 | else if (ret == 0) |
| 4184 | return(0); |
| 4185 | } |
| 4186 | else if (x->value.base64.total > y->value.base64.total) |
| 4187 | return(1); |
| 4188 | else |
| 4189 | return(-1); |
| 4190 | } |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4191 | return (-2); |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4192 | case XML_SCHEMAS_IDREFS: |
| 4193 | case XML_SCHEMAS_ENTITIES: |
| 4194 | case XML_SCHEMAS_NMTOKENS: |
| 4195 | TODO |
| 4196 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4197 | } |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4198 | return -2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4199 | } |
| 4200 | |
| 4201 | /** |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4202 | * xmlSchemaCompareValues: |
| 4203 | * @x: a first value |
| 4204 | * @y: a second value |
| 4205 | * |
| 4206 | * Compare 2 values |
| 4207 | * |
| 4208 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4209 | * case of error |
| 4210 | */ |
| 4211 | int |
| 4212 | xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) { |
| 4213 | xmlSchemaWhitespaceValueType xws, yws; |
| 4214 | |
Daniel Veillard | 5e09414 | 2005-02-18 19:36:12 +0000 | [diff] [blame^] | 4215 | if ((x == NULL) || (y == NULL)) |
| 4216 | return(-2); |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4217 | if (x->type == XML_SCHEMAS_STRING) |
| 4218 | xws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4219 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4220 | xws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4221 | else |
| 4222 | xws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4223 | |
| 4224 | if (y->type == XML_SCHEMAS_STRING) |
| 4225 | yws = XML_SCHEMA_WHITESPACE_PRESERVE; |
| 4226 | else if (x->type == XML_SCHEMAS_NORMSTRING) |
| 4227 | yws = XML_SCHEMA_WHITESPACE_REPLACE; |
| 4228 | else |
| 4229 | yws = XML_SCHEMA_WHITESPACE_COLLAPSE; |
| 4230 | |
| 4231 | return(xmlSchemaCompareValuesInternal(x, xws, y, yws)); |
| 4232 | } |
| 4233 | |
| 4234 | /** |
| 4235 | * xmlSchemaCompareValuesWhtsp: |
| 4236 | * @x: a first value |
| 4237 | * @xws: the whitespace value of x |
| 4238 | * @y: a second value |
| 4239 | * @yws: the whitespace value of y |
| 4240 | * |
| 4241 | * Compare 2 values |
| 4242 | * |
| 4243 | * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in |
| 4244 | * case of error |
| 4245 | */ |
| 4246 | int |
| 4247 | xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x, |
| 4248 | xmlSchemaWhitespaceValueType xws, |
| 4249 | xmlSchemaValPtr y, |
| 4250 | xmlSchemaWhitespaceValueType yws) { |
| 4251 | return(xmlSchemaCompareValuesInternal(x, xws, y, yws)); |
| 4252 | } |
| 4253 | |
| 4254 | /** |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4255 | * xmlSchemaNormLen: |
| 4256 | * @value: a string |
| 4257 | * |
| 4258 | * Computes the UTF8 length of the normalized value of the string |
| 4259 | * |
| 4260 | * Returns the length or -1 in case of error. |
| 4261 | */ |
| 4262 | static int |
| 4263 | xmlSchemaNormLen(const xmlChar *value) { |
| 4264 | const xmlChar *utf; |
| 4265 | int ret = 0; |
| 4266 | |
| 4267 | if (value == NULL) |
| 4268 | return(-1); |
| 4269 | utf = value; |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4270 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4271 | while (*utf != 0) { |
| 4272 | if (utf[0] & 0x80) { |
| 4273 | if ((utf[1] & 0xc0) != 0x80) |
| 4274 | return(-1); |
| 4275 | if ((utf[0] & 0xe0) == 0xe0) { |
| 4276 | if ((utf[2] & 0xc0) != 0x80) |
| 4277 | return(-1); |
| 4278 | if ((utf[0] & 0xf0) == 0xf0) { |
| 4279 | if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80) |
| 4280 | return(-1); |
| 4281 | utf += 4; |
| 4282 | } else { |
| 4283 | utf += 3; |
| 4284 | } |
| 4285 | } else { |
| 4286 | utf += 2; |
| 4287 | } |
William M. Brack | 76e95df | 2003-10-18 16:20:14 +0000 | [diff] [blame] | 4288 | } else if (IS_BLANK_CH(*utf)) { |
| 4289 | while (IS_BLANK_CH(*utf)) utf++; |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4290 | if (*utf == 0) |
| 4291 | break; |
| 4292 | } else { |
| 4293 | utf++; |
| 4294 | } |
| 4295 | ret++; |
| 4296 | } |
| 4297 | return(ret); |
| 4298 | } |
| 4299 | |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 4300 | /** |
| 4301 | * xmlSchemaGetFacetValueAsULong: |
| 4302 | * @facet: an schemas type facet |
| 4303 | * |
| 4304 | * Extract the value of a facet |
| 4305 | * |
| 4306 | * Returns the value as a long |
| 4307 | */ |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4308 | unsigned long |
| 4309 | xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet) |
| 4310 | { |
| 4311 | /* |
| 4312 | * TODO: Check if this is a decimal. |
| 4313 | */ |
William M. Brack | 094dd86 | 2004-11-14 14:28:34 +0000 | [diff] [blame] | 4314 | if (facet == NULL) |
| 4315 | return 0; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4316 | return ((unsigned long) facet->val->value.decimal.lo); |
| 4317 | } |
| 4318 | |
Daniel Veillard | c4c2155 | 2003-03-29 10:53:38 +0000 | [diff] [blame] | 4319 | /** |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4320 | * xmlSchemaValidateListSimpleTypeFacet: |
| 4321 | * @facet: the facet to check |
| 4322 | * @value: the lexical repr of the value to validate |
| 4323 | * @actualLen: the number of list items |
| 4324 | * @expectedLen: the resulting expected number of list items |
| 4325 | * |
| 4326 | * Checks the value of a list simple type against a facet. |
| 4327 | * |
| 4328 | * Returns 0 if the value is valid, a positive error code |
| 4329 | * number otherwise and -1 in case of an internal error. |
| 4330 | */ |
| 4331 | int |
| 4332 | xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet, |
| 4333 | const xmlChar *value, |
| 4334 | unsigned long actualLen, |
| 4335 | unsigned long *expectedLen) |
| 4336 | { |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4337 | if (facet == NULL) |
| 4338 | return(-1); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4339 | /* |
| 4340 | * TODO: Check if this will work with large numbers. |
| 4341 | * (compare value.decimal.mi and value.decimal.hi as well?). |
| 4342 | */ |
| 4343 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 4344 | if (actualLen != facet->val->value.decimal.lo) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4345 | if (expectedLen != 0) |
| 4346 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4347 | return (XML_SCHEMAV_CVC_LENGTH_VALID); |
| 4348 | } |
| 4349 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 4350 | if (actualLen < facet->val->value.decimal.lo) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4351 | if (expectedLen != 0) |
| 4352 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4353 | return (XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 4354 | } |
| 4355 | } else if (facet->type == XML_SCHEMA_FACET_MAXLENGTH) { |
| 4356 | if (actualLen > facet->val->value.decimal.lo) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4357 | if (expectedLen != 0) |
| 4358 | *expectedLen = facet->val->value.decimal.lo; |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4359 | return (XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 4360 | } |
| 4361 | } else |
| 4362 | /* |
| 4363 | * NOTE: That we can pass NULL as xmlSchemaValPtr to |
| 4364 | * xmlSchemaValidateFacet, since the remaining facet types |
| 4365 | * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION. |
| 4366 | */ |
| 4367 | return(xmlSchemaValidateFacet(NULL, facet, value, NULL)); |
| 4368 | return (0); |
| 4369 | } |
| 4370 | |
| 4371 | /** |
Daniel Veillard | 6927b10 | 2004-10-27 17:29:04 +0000 | [diff] [blame] | 4372 | * xmlSchemaValidateLengthFacet: |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4373 | * @type: the built-in type |
| 4374 | * @facet: the facet to check |
| 4375 | * @value: the lexical repr. of the value to be validated |
| 4376 | * @val: the precomputed value |
| 4377 | * @length: the actual length of the value |
| 4378 | * |
| 4379 | * Checka a value against a "length", "minLength" and "maxLength" |
| 4380 | * facet; sets @length to the computed length of @value. |
| 4381 | * |
| 4382 | * Returns 0 if the value is valid, a positive error code |
| 4383 | * otherwise and -1 in case of an internal or API error. |
| 4384 | */ |
| 4385 | int |
| 4386 | xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type, |
| 4387 | xmlSchemaFacetPtr facet, |
| 4388 | const xmlChar *value, |
| 4389 | xmlSchemaValPtr val, |
| 4390 | unsigned long *length) |
| 4391 | { |
| 4392 | unsigned int len = 0; |
| 4393 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4394 | if ((length == NULL) || (facet == NULL) || (type == NULL)) |
| 4395 | return (-1); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 4396 | *length = 0; |
| 4397 | if ((facet->type != XML_SCHEMA_FACET_LENGTH) && |
| 4398 | (facet->type != XML_SCHEMA_FACET_MAXLENGTH) && |
| 4399 | (facet->type != XML_SCHEMA_FACET_MINLENGTH)) |
| 4400 | return (-1); |
| 4401 | |
| 4402 | if ((facet->val == NULL) || |
| 4403 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 4404 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 4405 | (facet->val->value.decimal.frac != 0)) { |
| 4406 | return(-1); |
| 4407 | } |
| 4408 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
| 4409 | len = val->value.hex.total; |
| 4410 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 4411 | len = val->value.base64.total; |
| 4412 | else { |
| 4413 | switch (type->builtInType) { |
| 4414 | case XML_SCHEMAS_IDREF: |
| 4415 | case XML_SCHEMAS_NORMSTRING: |
| 4416 | case XML_SCHEMAS_TOKEN: |
| 4417 | case XML_SCHEMAS_LANGUAGE: |
| 4418 | case XML_SCHEMAS_NMTOKEN: |
| 4419 | case XML_SCHEMAS_NAME: |
| 4420 | case XML_SCHEMAS_NCNAME: |
| 4421 | case XML_SCHEMAS_ID: |
| 4422 | len = xmlSchemaNormLen(value); |
| 4423 | break; |
| 4424 | case XML_SCHEMAS_STRING: |
| 4425 | /* |
| 4426 | * FIXME: What exactly to do with anyURI? |
| 4427 | */ |
| 4428 | case XML_SCHEMAS_ANYURI: |
| 4429 | if (value != NULL) |
| 4430 | len = xmlUTF8Strlen(value); |
| 4431 | break; |
| 4432 | default: |
| 4433 | TODO |
| 4434 | } |
| 4435 | } |
| 4436 | *length = (unsigned long) len; |
| 4437 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
| 4438 | if (len != facet->val->value.decimal.lo) |
| 4439 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
| 4440 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
| 4441 | if (len < facet->val->value.decimal.lo) |
| 4442 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
| 4443 | } else { |
| 4444 | if (len > facet->val->value.decimal.lo) |
| 4445 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
| 4446 | } |
| 4447 | |
| 4448 | return (0); |
| 4449 | } |
| 4450 | |
| 4451 | /** |
| 4452 | * xmlSchemaValidateFacet: |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 4453 | * @base: the base type |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4454 | * @facet: the facet to check |
| 4455 | * @value: the lexical repr of the value to validate |
| 4456 | * @val: the precomputed value |
| 4457 | * |
| 4458 | * Check a value against a facet condition |
| 4459 | * |
| 4460 | * Returns 0 if the element is schemas valid, a positive error code |
| 4461 | * number otherwise and -1 in case of internal or API error. |
| 4462 | */ |
| 4463 | int |
Daniel Veillard | dda8f1b | 2002-09-26 09:47:36 +0000 | [diff] [blame] | 4464 | xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED, |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4465 | xmlSchemaFacetPtr facet, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4466 | const xmlChar *value, xmlSchemaValPtr val) |
| 4467 | { |
| 4468 | int ret; |
| 4469 | |
Daniel Veillard | ce682bc | 2004-11-05 17:22:25 +0000 | [diff] [blame] | 4470 | if ((facet == NULL) || (value == NULL)) |
| 4471 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4472 | switch (facet->type) { |
| 4473 | case XML_SCHEMA_FACET_PATTERN: |
| 4474 | ret = xmlRegexpExec(facet->regexp, value); |
| 4475 | if (ret == 1) |
| 4476 | return(0); |
| 4477 | if (ret == 0) { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4478 | return(XML_SCHEMAV_CVC_PATTERN_VALID); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4479 | } |
| 4480 | return(ret); |
| 4481 | case XML_SCHEMA_FACET_MAXEXCLUSIVE: |
| 4482 | ret = xmlSchemaCompareValues(val, facet->val); |
| 4483 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4484 | /* TODO error code */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4485 | return(-1); |
| 4486 | } |
| 4487 | if (ret == -1) |
| 4488 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4489 | /* error code */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4490 | return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4491 | case XML_SCHEMA_FACET_MAXINCLUSIVE: |
| 4492 | ret = xmlSchemaCompareValues(val, facet->val); |
| 4493 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4494 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4495 | return(-1); |
| 4496 | } |
| 4497 | if ((ret == -1) || (ret == 0)) |
| 4498 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4499 | /* error code */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4500 | return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4501 | case XML_SCHEMA_FACET_MINEXCLUSIVE: |
| 4502 | ret = xmlSchemaCompareValues(val, facet->val); |
| 4503 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4504 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4505 | return(-1); |
| 4506 | } |
| 4507 | if (ret == 1) |
| 4508 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4509 | /* error code */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4510 | return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID); |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4511 | case XML_SCHEMA_FACET_MININCLUSIVE: |
| 4512 | ret = xmlSchemaCompareValues(val, facet->val); |
| 4513 | if (ret == -2) { |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4514 | /* TODO error code */ |
Daniel Veillard | 070803b | 2002-05-03 07:29:38 +0000 | [diff] [blame] | 4515 | return(-1); |
| 4516 | } |
| 4517 | if ((ret == 1) || (ret == 0)) |
| 4518 | return(0); |
Daniel Veillard | 5a87241 | 2002-05-22 06:40:27 +0000 | [diff] [blame] | 4519 | /* error code */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4520 | return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID); |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4521 | case XML_SCHEMA_FACET_WHITESPACE: |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4522 | /* TODO whitespaces */ |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4523 | /* |
| 4524 | * NOTE: Whitespace should be handled to normalize |
| 4525 | * the value to be validated against a the facets; |
| 4526 | * not to normalize the value in-between. |
| 4527 | */ |
Daniel Veillard | 8651f53 | 2002-04-17 09:06:27 +0000 | [diff] [blame] | 4528 | return(0); |
Daniel Veillard | 88c5891 | 2002-04-23 07:12:20 +0000 | [diff] [blame] | 4529 | case XML_SCHEMA_FACET_ENUMERATION: |
| 4530 | if ((facet->value != NULL) && |
| 4531 | (xmlStrEqual(facet->value, value))) |
| 4532 | return(0); |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4533 | return(XML_SCHEMAV_CVC_ENUMERATION_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4534 | case XML_SCHEMA_FACET_LENGTH: |
| 4535 | case XML_SCHEMA_FACET_MAXLENGTH: |
| 4536 | case XML_SCHEMA_FACET_MINLENGTH: { |
| 4537 | unsigned int len = 0; |
| 4538 | |
| 4539 | if ((facet->val == NULL) || |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4540 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 4541 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4542 | (facet->val->value.decimal.frac != 0)) { |
| 4543 | return(-1); |
| 4544 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4545 | if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY)) |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4546 | len = val->value.hex.total; |
Daniel Veillard | 1ac24d3 | 2003-08-27 14:15:15 +0000 | [diff] [blame] | 4547 | else if ((val != NULL) && (val->type == XML_SCHEMAS_BASE64BINARY)) |
| 4548 | len = val->value.base64.total; |
| 4549 | else { |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4550 | switch (base->builtInType) { |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4551 | case XML_SCHEMAS_IDREF: |
| 4552 | case XML_SCHEMAS_NORMSTRING: |
| 4553 | case XML_SCHEMAS_TOKEN: |
| 4554 | case XML_SCHEMAS_LANGUAGE: |
| 4555 | case XML_SCHEMAS_NMTOKEN: |
| 4556 | case XML_SCHEMAS_NAME: |
| 4557 | case XML_SCHEMAS_NCNAME: |
| 4558 | case XML_SCHEMAS_ID: |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4559 | len = xmlSchemaNormLen(value); |
| 4560 | break; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4561 | case XML_SCHEMAS_STRING: |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4562 | /* |
| 4563 | * FIXME: What exactly to do with anyURI? |
| 4564 | */ |
| 4565 | case XML_SCHEMAS_ANYURI: |
William M. Brack | fbf2c5e | 2004-02-03 17:55:56 +0000 | [diff] [blame] | 4566 | if (value != NULL) |
| 4567 | len = xmlUTF8Strlen(value); |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4568 | break; |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4569 | default: |
| 4570 | TODO |
Daniel Veillard | 70bcb0e | 2003-08-08 14:00:28 +0000 | [diff] [blame] | 4571 | } |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4572 | } |
| 4573 | if (facet->type == XML_SCHEMA_FACET_LENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4574 | if (len != facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4575 | return(XML_SCHEMAV_CVC_LENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4576 | } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4577 | if (len < facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4578 | return(XML_SCHEMAV_CVC_MINLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4579 | } else { |
Daniel Veillard | e637c4a | 2003-03-30 21:10:09 +0000 | [diff] [blame] | 4580 | if (len > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4581 | return(XML_SCHEMAV_CVC_MAXLENGTH_VALID); |
Daniel Veillard | 8bc6cf9 | 2003-02-27 17:42:22 +0000 | [diff] [blame] | 4582 | } |
| 4583 | break; |
| 4584 | } |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4585 | case XML_SCHEMA_FACET_TOTALDIGITS: |
| 4586 | case XML_SCHEMA_FACET_FRACTIONDIGITS: |
| 4587 | |
| 4588 | if ((facet->val == NULL) || |
| 4589 | ((facet->val->type != XML_SCHEMAS_DECIMAL) && |
| 4590 | (facet->val->type != XML_SCHEMAS_NNINTEGER)) || |
| 4591 | (facet->val->value.decimal.frac != 0)) { |
| 4592 | return(-1); |
| 4593 | } |
| 4594 | if ((val == NULL) || |
| 4595 | ((val->type != XML_SCHEMAS_DECIMAL) && |
| 4596 | (val->type != XML_SCHEMAS_INTEGER) && |
| 4597 | (val->type != XML_SCHEMAS_NPINTEGER) && |
| 4598 | (val->type != XML_SCHEMAS_NINTEGER) && |
| 4599 | (val->type != XML_SCHEMAS_NNINTEGER) && |
| 4600 | (val->type != XML_SCHEMAS_PINTEGER) && |
| 4601 | (val->type != XML_SCHEMAS_INT) && |
| 4602 | (val->type != XML_SCHEMAS_UINT) && |
| 4603 | (val->type != XML_SCHEMAS_LONG) && |
| 4604 | (val->type != XML_SCHEMAS_ULONG) && |
| 4605 | (val->type != XML_SCHEMAS_SHORT) && |
| 4606 | (val->type != XML_SCHEMAS_USHORT) && |
| 4607 | (val->type != XML_SCHEMAS_BYTE) && |
| 4608 | (val->type != XML_SCHEMAS_UBYTE))) { |
| 4609 | return(-1); |
| 4610 | } |
| 4611 | if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) { |
| 4612 | if (val->value.decimal.total > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4613 | return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4614 | |
| 4615 | } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) { |
| 4616 | if (val->value.decimal.frac > facet->val->value.decimal.lo) |
Daniel Veillard | 01fa615 | 2004-06-29 17:04:39 +0000 | [diff] [blame] | 4617 | return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID); |
Daniel Veillard | 560c2a4 | 2003-07-06 21:13:49 +0000 | [diff] [blame] | 4618 | } |
| 4619 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4620 | default: |
| 4621 | TODO |
| 4622 | } |
| 4623 | return(0); |
Daniel Veillard | b6c7f41 | 2003-03-29 16:41:55 +0000 | [diff] [blame] | 4624 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4625 | } |
| 4626 | |
Kasimier T. Buchcik | 6e224f1 | 2005-02-17 11:10:44 +0000 | [diff] [blame] | 4627 | /** |
| 4628 | * xmlSchemaGetCanonValue: |
| 4629 | * @val: the precomputed value |
| 4630 | * @retValue: the returned value |
| 4631 | * |
| 4632 | * Returns a the cononical representation of the value. |
| 4633 | * The called has to free the returned retValue. |
| 4634 | * |
| 4635 | * Returns 0 if the value could be built and -1 in case of |
| 4636 | * API errors or if the value type is not supported yet. |
| 4637 | */ |
| 4638 | int |
| 4639 | xmlSchemaGetCanonValue(xmlSchemaValPtr val, |
| 4640 | const xmlChar **retValue) |
| 4641 | { |
| 4642 | if (retValue == NULL) |
| 4643 | return (-1); |
| 4644 | *retValue = NULL; |
| 4645 | switch (val->type) { |
| 4646 | case XML_SCHEMAS_STRING: |
| 4647 | case XML_SCHEMAS_NORMSTRING: |
| 4648 | /* |
| 4649 | case XML_SCHEMAS_TOKEN: |
| 4650 | case XML_SCHEMAS_LANGUAGE: |
| 4651 | case XML_SCHEMAS_NMTOKEN: |
| 4652 | case XML_SCHEMAS_NAME: |
| 4653 | case XML_SCHEMAS_QNAME: |
| 4654 | case XML_SCHEMAS_NCNAME: |
| 4655 | case XML_SCHEMAS_ID: |
| 4656 | case XML_SCHEMAS_IDREF: |
| 4657 | case XML_SCHEMAS_ENTITY: |
| 4658 | case XML_SCHEMAS_NOTATION: |
| 4659 | case XML_SCHEMAS_ANYURI: |
| 4660 | */ |
| 4661 | if (val->value.str == NULL) |
| 4662 | *retValue = NULL; |
| 4663 | else |
| 4664 | /* TODO: This is not yet correct for non-normalized values. */ |
| 4665 | *retValue = |
| 4666 | BAD_CAST xmlStrdup((const xmlChar *) val->value.str); |
| 4667 | return (0); |
| 4668 | default: |
| 4669 | return (-1); |
| 4670 | } |
| 4671 | return (-1); |
| 4672 | } |
| 4673 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4674 | #endif /* LIBXML_SCHEMAS_ENABLED */ |