blob: 8103f7add27df7b8d305ab3aee19e3308d2864bc [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
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 Veillard96a4b252003-02-06 08:22:32 +000021#include <libxml/xpath.h>
22#include <libxml/uri.h>
Daniel Veillard4255d502002-04-16 15:50:10 +000023
24#include <libxml/xmlschemas.h>
25#include <libxml/schemasInternals.h>
26#include <libxml/xmlschemastypes.h>
27
Daniel Veillard070803b2002-05-03 07:29:38 +000028#ifdef HAVE_MATH_H
29#include <math.h>
30#endif
31
Daniel Veillard4255d502002-04-16 15:50:10 +000032#define DEBUG
33
34#define TODO \
35 xmlGenericError(xmlGenericErrorContext, \
36 "Unimplemented block at %s:%d\n", \
37 __FILE__, __LINE__);
38
39#define XML_SCHEMAS_NAMESPACE_NAME \
40 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
41
42typedef enum {
43 XML_SCHEMAS_UNKNOWN = 0,
44 XML_SCHEMAS_STRING,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000045 XML_SCHEMAS_NORMSTRING,
Daniel Veillard4255d502002-04-16 15:50:10 +000046 XML_SCHEMAS_DECIMAL,
Daniel Veillard070803b2002-05-03 07:29:38 +000047 XML_SCHEMAS_TIME,
48 XML_SCHEMAS_GDAY,
49 XML_SCHEMAS_GMONTH,
50 XML_SCHEMAS_GMONTHDAY,
51 XML_SCHEMAS_GYEAR,
52 XML_SCHEMAS_GYEARMONTH,
53 XML_SCHEMAS_DATE,
54 XML_SCHEMAS_DATETIME,
55 XML_SCHEMAS_DURATION,
Daniel Veillard84d70a42002-09-16 10:51:38 +000056 XML_SCHEMAS_FLOAT,
57 XML_SCHEMAS_DOUBLE,
Daniel Veillardc5a70f22003-02-06 23:41:59 +000058 XML_SCHEMAS_BOOLEAN,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000059 XML_SCHEMAS_TOKEN,
60 XML_SCHEMAS_LANGUAGE,
61 XML_SCHEMAS_NMTOKEN,
62 XML_SCHEMAS_NMTOKENS,
63 XML_SCHEMAS_NAME,
64 XML_SCHEMAS_QNAME,
65 XML_SCHEMAS_NCNAME,
66 XML_SCHEMAS_ID,
67 XML_SCHEMAS_IDREF,
68 XML_SCHEMAS_IDREFS,
69 XML_SCHEMAS_ENTITY,
70 XML_SCHEMAS_ENTITIES,
71 XML_SCHEMAS_NOTATION,
72 XML_SCHEMAS_ANYURI,
73 XML_SCHEMAS_INTEGER,
74 XML_SCHEMAS_NPINTEGER,
75 XML_SCHEMAS_NINTEGER,
76 XML_SCHEMAS_NNINTEGER,
77 XML_SCHEMAS_PINTEGER,
Daniel Veillard96a4b252003-02-06 08:22:32 +000078 XML_SCHEMAS_INT,
Daniel Veillard8bc6cf92003-02-27 17:42:22 +000079 XML_SCHEMAS_UINT,
80 XML_SCHEMAS_LONG,
81 XML_SCHEMAS_ULONG,
82 XML_SCHEMAS_SHORT,
83 XML_SCHEMAS_USHORT,
84 XML_SCHEMAS_BYTE,
Daniel Veillard560c2a42003-07-06 21:13:49 +000085 XML_SCHEMAS_UBYTE,
86 XML_SCHEMAS_HEXBINARY
Daniel Veillard4255d502002-04-16 15:50:10 +000087} xmlSchemaValType;
88
Daniel Veillard5f704af2003-03-05 10:01:43 +000089static unsigned long powten[10] = {
Daniel Veillard4255d502002-04-16 15:50:10 +000090 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000L,
91 100000000L, 1000000000L
92};
93
Daniel Veillard070803b2002-05-03 07:29:38 +000094/* Date value */
95typedef struct _xmlSchemaValDate xmlSchemaValDate;
96typedef xmlSchemaValDate *xmlSchemaValDatePtr;
97struct _xmlSchemaValDate {
98 long year;
99 unsigned int mon :4; /* 1 <= mon <= 12 */
100 unsigned int day :5; /* 1 <= day <= 31 */
101 unsigned int hour :5; /* 0 <= hour <= 23 */
102 unsigned int min :6; /* 0 <= min <= 59 */
103 double sec;
Daniel Veillarda77cf712003-05-09 23:09:55 +0000104 unsigned int tz_flag :1; /* is tzo explicitely set? */
Daniel Veillard070803b2002-05-03 07:29:38 +0000105 int tzo :11; /* -1440 <= tzo <= 1440 */
106};
107
108/* Duration value */
109typedef struct _xmlSchemaValDuration xmlSchemaValDuration;
110typedef xmlSchemaValDuration *xmlSchemaValDurationPtr;
111struct _xmlSchemaValDuration {
112 long mon; /* mon stores years also */
113 long day;
114 double sec; /* sec stores min and hour also */
115};
116
Daniel Veillard4255d502002-04-16 15:50:10 +0000117typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal;
118typedef xmlSchemaValDecimal *xmlSchemaValDecimalPtr;
119struct _xmlSchemaValDecimal {
120 /* would use long long but not portable */
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000121 unsigned long lo;
122 unsigned long mi;
123 unsigned long hi;
Daniel Veillard4255d502002-04-16 15:50:10 +0000124 unsigned int extra;
Daniel Veillard5a872412002-05-22 06:40:27 +0000125 unsigned int sign:1;
William M. Brackc1939562003-08-05 15:52:22 +0000126 unsigned int frac:7;
127 unsigned int total:8;
Daniel Veillard4255d502002-04-16 15:50:10 +0000128};
129
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000130typedef struct _xmlSchemaValQName xmlSchemaValQName;
131typedef xmlSchemaValQName *xmlSchemaValQNamePtr;
132struct _xmlSchemaValQName {
133 xmlChar *name;
134 xmlChar *uri;
135};
136
Daniel Veillard4255d502002-04-16 15:50:10 +0000137struct _xmlSchemaVal {
138 xmlSchemaValType type;
139 union {
Daniel Veillard5a872412002-05-22 06:40:27 +0000140 xmlSchemaValDecimal decimal;
Daniel Veillard070803b2002-05-03 07:29:38 +0000141 xmlSchemaValDate date;
142 xmlSchemaValDuration dur;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000143 xmlSchemaValQName qname;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000144 float f;
145 double d;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000146 int b;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000147 xmlChar *str;
Daniel Veillard4255d502002-04-16 15:50:10 +0000148 } value;
149};
150
151static int xmlSchemaTypesInitialized = 0;
152static xmlHashTablePtr xmlSchemaTypesBank = NULL;
153
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000154/*
155 * Basic types
156 */
Daniel Veillard4255d502002-04-16 15:50:10 +0000157static xmlSchemaTypePtr xmlSchemaTypeStringDef = NULL;
158static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef = NULL;
159static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef = NULL;
160static xmlSchemaTypePtr xmlSchemaTypeDecimalDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000161static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000162static xmlSchemaTypePtr xmlSchemaTypeDateDef = NULL;
Daniel Veillard070803b2002-05-03 07:29:38 +0000163static xmlSchemaTypePtr xmlSchemaTypeTimeDef = NULL;
164static xmlSchemaTypePtr xmlSchemaTypeGYearDef = NULL;
165static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef = NULL;
166static xmlSchemaTypePtr xmlSchemaTypeGDayDef = NULL;
167static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef = NULL;
168static xmlSchemaTypePtr xmlSchemaTypeGMonthDef = NULL;
169static xmlSchemaTypePtr xmlSchemaTypeDurationDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000170static xmlSchemaTypePtr xmlSchemaTypeFloatDef = NULL;
Daniel Veillardc5a70f22003-02-06 23:41:59 +0000171static xmlSchemaTypePtr xmlSchemaTypeBooleanDef = NULL;
Daniel Veillard84d70a42002-09-16 10:51:38 +0000172static xmlSchemaTypePtr xmlSchemaTypeDoubleDef = NULL;
Daniel Veillard560c2a42003-07-06 21:13:49 +0000173static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000174static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000175
176/*
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000177 * Derived types
178 */
179static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef = NULL;
180static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef = NULL;
181static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef = NULL;
182static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef = NULL;
183static xmlSchemaTypePtr xmlSchemaTypeIntegerDef = NULL;
184static xmlSchemaTypePtr xmlSchemaTypeLongDef = NULL;
185static xmlSchemaTypePtr xmlSchemaTypeIntDef = NULL;
186static xmlSchemaTypePtr xmlSchemaTypeShortDef = NULL;
187static xmlSchemaTypePtr xmlSchemaTypeByteDef = NULL;
188static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef = NULL;
189static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef = NULL;
190static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef = NULL;
191static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000192static xmlSchemaTypePtr xmlSchemaTypeNormStringDef = NULL;
193static xmlSchemaTypePtr xmlSchemaTypeTokenDef = NULL;
194static xmlSchemaTypePtr xmlSchemaTypeLanguageDef = NULL;
195static xmlSchemaTypePtr xmlSchemaTypeNameDef = NULL;
196static xmlSchemaTypePtr xmlSchemaTypeQNameDef = NULL;
Daniel Veillarde5b110b2003-02-04 14:43:39 +0000197static xmlSchemaTypePtr xmlSchemaTypeNCNameDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000198static xmlSchemaTypePtr xmlSchemaTypeIdDef = NULL;
199static xmlSchemaTypePtr xmlSchemaTypeIdrefDef = NULL;
200static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef = NULL;
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000201static xmlSchemaTypePtr xmlSchemaTypeEntityDef = NULL;
202static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef = NULL;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000203static xmlSchemaTypePtr xmlSchemaTypeNotationDef = NULL;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000204static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef = NULL;
205static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef = NULL;
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000206
207/*
Daniel Veillard4255d502002-04-16 15:50:10 +0000208 * xmlSchemaInitBasicType:
209 * @name: the type name
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000210 * @type: the value type associated
Daniel Veillard4255d502002-04-16 15:50:10 +0000211 *
212 * Initialize one default type
213 */
214static xmlSchemaTypePtr
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000215xmlSchemaInitBasicType(const char *name, xmlSchemaValType type) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000216 xmlSchemaTypePtr ret;
217
218 ret = (xmlSchemaTypePtr) xmlMalloc(sizeof(xmlSchemaType));
219 if (ret == NULL) {
220 xmlGenericError(xmlGenericErrorContext,
221 "Could not initilize type %s: out of memory\n", name);
222 return(NULL);
223 }
224 memset(ret, 0, sizeof(xmlSchemaType));
225 ret->name = xmlStrdup((const xmlChar *)name);
226 ret->type = XML_SCHEMA_TYPE_BASIC;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000227 ret->flags = type;
Daniel Veillard4255d502002-04-16 15:50:10 +0000228 ret->contentType = XML_SCHEMA_CONTENT_BASIC;
229 xmlHashAddEntry2(xmlSchemaTypesBank, ret->name,
230 XML_SCHEMAS_NAMESPACE_NAME, ret);
231 return(ret);
232}
233
234/*
235 * xmlSchemaInitTypes:
236 *
237 * Initialize the default XML Schemas type library
238 */
239void
Daniel Veillard6560a422003-03-27 21:25:38 +0000240xmlSchemaInitTypes(void)
241{
Daniel Veillard4255d502002-04-16 15:50:10 +0000242 if (xmlSchemaTypesInitialized != 0)
Daniel Veillard6560a422003-03-27 21:25:38 +0000243 return;
Daniel Veillard4255d502002-04-16 15:50:10 +0000244 xmlSchemaTypesBank = xmlHashCreate(40);
Daniel Veillard6560a422003-03-27 21:25:38 +0000245
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000246 /*
247 * primitive datatypes
248 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000249 xmlSchemaTypeStringDef = xmlSchemaInitBasicType("string",
Daniel Veillard6560a422003-03-27 21:25:38 +0000250 XML_SCHEMAS_STRING);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000251 xmlSchemaTypeAnyTypeDef = xmlSchemaInitBasicType("anyType",
Daniel Veillard6560a422003-03-27 21:25:38 +0000252 XML_SCHEMAS_UNKNOWN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000253 xmlSchemaTypeAnySimpleTypeDef = xmlSchemaInitBasicType("anySimpleType",
Daniel Veillard6560a422003-03-27 21:25:38 +0000254 XML_SCHEMAS_UNKNOWN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000255 xmlSchemaTypeDecimalDef = xmlSchemaInitBasicType("decimal",
Daniel Veillard6560a422003-03-27 21:25:38 +0000256 XML_SCHEMAS_DECIMAL);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000257 xmlSchemaTypeDateDef = xmlSchemaInitBasicType("date",
Daniel Veillard6560a422003-03-27 21:25:38 +0000258 XML_SCHEMAS_DATE);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000259 xmlSchemaTypeDatetimeDef = xmlSchemaInitBasicType("dateTime",
Daniel Veillard6560a422003-03-27 21:25:38 +0000260 XML_SCHEMAS_DATETIME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000261 xmlSchemaTypeTimeDef = xmlSchemaInitBasicType("time",
Daniel Veillard6560a422003-03-27 21:25:38 +0000262 XML_SCHEMAS_TIME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000263 xmlSchemaTypeGYearDef = xmlSchemaInitBasicType("gYear",
Daniel Veillard6560a422003-03-27 21:25:38 +0000264 XML_SCHEMAS_GYEAR);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000265 xmlSchemaTypeGYearMonthDef = xmlSchemaInitBasicType("gYearMonth",
Daniel Veillard6560a422003-03-27 21:25:38 +0000266 XML_SCHEMAS_GYEARMONTH);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000267 xmlSchemaTypeGMonthDef = xmlSchemaInitBasicType("gMonth",
Daniel Veillard6560a422003-03-27 21:25:38 +0000268 XML_SCHEMAS_GMONTH);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000269 xmlSchemaTypeGMonthDayDef = xmlSchemaInitBasicType("gMonthDay",
Daniel Veillard6560a422003-03-27 21:25:38 +0000270 XML_SCHEMAS_GMONTHDAY);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000271 xmlSchemaTypeGDayDef = xmlSchemaInitBasicType("gDay",
Daniel Veillard6560a422003-03-27 21:25:38 +0000272 XML_SCHEMAS_GDAY);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000273 xmlSchemaTypeDurationDef = xmlSchemaInitBasicType("duration",
Daniel Veillard6560a422003-03-27 21:25:38 +0000274 XML_SCHEMAS_DURATION);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000275 xmlSchemaTypeFloatDef = xmlSchemaInitBasicType("float",
Daniel Veillard6560a422003-03-27 21:25:38 +0000276 XML_SCHEMAS_FLOAT);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000277 xmlSchemaTypeDoubleDef = xmlSchemaInitBasicType("double",
Daniel Veillard6560a422003-03-27 21:25:38 +0000278 XML_SCHEMAS_DOUBLE);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000279 xmlSchemaTypeBooleanDef = xmlSchemaInitBasicType("boolean",
Daniel Veillard6560a422003-03-27 21:25:38 +0000280 XML_SCHEMAS_BOOLEAN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000281 xmlSchemaTypeAnyURIDef = xmlSchemaInitBasicType("anyURI",
Daniel Veillard6560a422003-03-27 21:25:38 +0000282 XML_SCHEMAS_ANYURI);
Daniel Veillard560c2a42003-07-06 21:13:49 +0000283 xmlSchemaTypeHexBinaryDef = xmlSchemaInitBasicType("hexBinary",
284 XML_SCHEMAS_HEXBINARY);
Daniel Veillard4255d502002-04-16 15:50:10 +0000285
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000286 /*
287 * derived datatypes
288 */
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000289 xmlSchemaTypeIntegerDef = xmlSchemaInitBasicType("integer",
Daniel Veillard6560a422003-03-27 21:25:38 +0000290 XML_SCHEMAS_INTEGER);;
291 xmlSchemaTypeNonPositiveIntegerDef =
292 xmlSchemaInitBasicType("nonPositiveInteger",
293 XML_SCHEMAS_NPINTEGER);;
294 xmlSchemaTypeNegativeIntegerDef =
295 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER);;
296 xmlSchemaTypeLongDef =
297 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG);;
298 xmlSchemaTypeIntDef = xmlSchemaInitBasicType("int", XML_SCHEMAS_INT);;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000299 xmlSchemaTypeShortDef = xmlSchemaInitBasicType("short",
Daniel Veillard6560a422003-03-27 21:25:38 +0000300 XML_SCHEMAS_SHORT);;
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000301 xmlSchemaTypeByteDef = xmlSchemaInitBasicType("byte",
Daniel Veillard6560a422003-03-27 21:25:38 +0000302 XML_SCHEMAS_BYTE);;
303 xmlSchemaTypeNonNegativeIntegerDef =
304 xmlSchemaInitBasicType("nonNegativeInteger",
305 XML_SCHEMAS_NNINTEGER);
306 xmlSchemaTypeUnsignedLongDef =
307 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG);;
308 xmlSchemaTypeUnsignedIntDef =
309 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT);;
310 xmlSchemaTypeUnsignedShortDef =
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000311 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT);;
Daniel Veillard6560a422003-03-27 21:25:38 +0000312 xmlSchemaTypeUnsignedByteDef =
313 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE);;
314 xmlSchemaTypePositiveIntegerDef =
315 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER);
Daniel Veillardc6e997c2003-01-27 12:35:42 +0000316
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000317 xmlSchemaTypeNormStringDef = xmlSchemaInitBasicType("normalizedString",
Daniel Veillard6560a422003-03-27 21:25:38 +0000318 XML_SCHEMAS_NORMSTRING);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000319 xmlSchemaTypeTokenDef = xmlSchemaInitBasicType("token",
Daniel Veillard6560a422003-03-27 21:25:38 +0000320 XML_SCHEMAS_TOKEN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000321 xmlSchemaTypeLanguageDef = xmlSchemaInitBasicType("language",
Daniel Veillard6560a422003-03-27 21:25:38 +0000322 XML_SCHEMAS_LANGUAGE);
323 xmlSchemaTypeIdDef = xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000324 xmlSchemaTypeIdrefDef = xmlSchemaInitBasicType("IDREF",
Daniel Veillard6560a422003-03-27 21:25:38 +0000325 XML_SCHEMAS_IDREF);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000326 xmlSchemaTypeIdrefsDef = xmlSchemaInitBasicType("IDREFS",
Daniel Veillard6560a422003-03-27 21:25:38 +0000327 XML_SCHEMAS_IDREFS);
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000328 xmlSchemaTypeEntityDef = xmlSchemaInitBasicType("ENTITY",
Daniel Veillard6560a422003-03-27 21:25:38 +0000329 XML_SCHEMAS_ENTITY);
Daniel Veillarda1a9d042003-03-18 16:53:17 +0000330 xmlSchemaTypeEntitiesDef = xmlSchemaInitBasicType("ENTITIES",
Daniel Veillard6560a422003-03-27 21:25:38 +0000331 XML_SCHEMAS_ENTITIES);
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000332 xmlSchemaTypeNotationDef = xmlSchemaInitBasicType("NOTATION",
333 XML_SCHEMAS_NOTATION);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000334 xmlSchemaTypeNameDef = xmlSchemaInitBasicType("Name",
Daniel Veillard6560a422003-03-27 21:25:38 +0000335 XML_SCHEMAS_NAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000336 xmlSchemaTypeQNameDef = xmlSchemaInitBasicType("QName",
Daniel Veillard6560a422003-03-27 21:25:38 +0000337 XML_SCHEMAS_QNAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000338 xmlSchemaTypeNCNameDef = xmlSchemaInitBasicType("NCName",
Daniel Veillard6560a422003-03-27 21:25:38 +0000339 XML_SCHEMAS_NCNAME);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000340 xmlSchemaTypeNmtokenDef = xmlSchemaInitBasicType("NMTOKEN",
Daniel Veillard6560a422003-03-27 21:25:38 +0000341 XML_SCHEMAS_NMTOKEN);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +0000342 xmlSchemaTypeNmtokensDef = xmlSchemaInitBasicType("NMTOKENS",
Daniel Veillard6560a422003-03-27 21:25:38 +0000343 XML_SCHEMAS_NMTOKENS);
Daniel Veillard4255d502002-04-16 15:50:10 +0000344 xmlSchemaTypesInitialized = 1;
345}
346
347/**
348 * xmlSchemaCleanupTypes:
349 *
350 * Cleanup the default XML Schemas type library
351 */
352void
353xmlSchemaCleanupTypes(void) {
354 if (xmlSchemaTypesInitialized == 0)
355 return;
356 xmlHashFree(xmlSchemaTypesBank, (xmlHashDeallocator) xmlSchemaFreeType);
357 xmlSchemaTypesInitialized = 0;
358}
359
360/**
361 * xmlSchemaNewValue:
362 * @type: the value type
363 *
364 * Allocate a new simple type value
365 *
366 * Returns a pointer to the new value or NULL in case of error
367 */
368static xmlSchemaValPtr
369xmlSchemaNewValue(xmlSchemaValType type) {
370 xmlSchemaValPtr value;
371
372 value = (xmlSchemaValPtr) xmlMalloc(sizeof(xmlSchemaVal));
373 if (value == NULL) {
374 return(NULL);
375 }
376 memset(value, 0, sizeof(xmlSchemaVal));
377 value->type = type;
378 return(value);
379}
380
381/**
382 * xmlSchemaFreeValue:
383 * @value: the value to free
384 *
385 * Cleanup the default XML Schemas type library
386 */
387void
388xmlSchemaFreeValue(xmlSchemaValPtr value) {
389 if (value == NULL)
390 return;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000391 switch (value->type) {
392 case XML_SCHEMAS_STRING:
393 case XML_SCHEMAS_NORMSTRING:
394 case XML_SCHEMAS_TOKEN:
395 case XML_SCHEMAS_LANGUAGE:
396 case XML_SCHEMAS_NMTOKEN:
397 case XML_SCHEMAS_NMTOKENS:
398 case XML_SCHEMAS_NAME:
Daniel Veillardc4c21552003-03-29 10:53:38 +0000399 case XML_SCHEMAS_NCNAME:
400 case XML_SCHEMAS_ID:
401 case XML_SCHEMAS_IDREF:
402 case XML_SCHEMAS_IDREFS:
403 case XML_SCHEMAS_ENTITY:
404 case XML_SCHEMAS_ENTITIES:
405 case XML_SCHEMAS_NOTATION:
406 case XML_SCHEMAS_ANYURI:
407 if (value->value.str != NULL)
408 xmlFree(value->value.str);
409 break;
Daniel Veillarde637c4a2003-03-30 21:10:09 +0000410 case XML_SCHEMAS_QNAME:
411 if (value->value.qname.uri != NULL)
412 xmlFree(value->value.qname.uri);
413 if (value->value.qname.name != NULL)
414 xmlFree(value->value.qname.name);
415 break;
Daniel Veillardc4c21552003-03-29 10:53:38 +0000416 default:
417 break;
418 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000419 xmlFree(value);
420}
421
422/**
423 * xmlSchemaGetPredefinedType:
424 * @name: the type name
425 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
426 *
427 * Lookup a type in the default XML Schemas type library
428 *
429 * Returns the type if found, NULL otherwise
430 */
431xmlSchemaTypePtr
432xmlSchemaGetPredefinedType(const xmlChar *name, const xmlChar *ns) {
433 if (xmlSchemaTypesInitialized == 0)
434 xmlSchemaInitTypes();
435 if (name == NULL)
436 return(NULL);
437 return((xmlSchemaTypePtr) xmlHashLookup2(xmlSchemaTypesBank, name, ns));
438}
Daniel Veillard070803b2002-05-03 07:29:38 +0000439
440/****************************************************************
441 * *
442 * Convenience macros and functions *
443 * *
444 ****************************************************************/
445
446#define IS_TZO_CHAR(c) \
447 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
448
449#define VALID_YEAR(yr) (yr != 0)
450#define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
451/* VALID_DAY should only be used when month is unknown */
452#define VALID_DAY(day) ((day >= 1) && (day <= 31))
453#define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
454#define VALID_MIN(min) ((min >= 0) && (min <= 59))
455#define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
456#define VALID_TZO(tzo) ((tzo > -1440) && (tzo < 1440))
457#define IS_LEAP(y) \
458 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
459
460static const long daysInMonth[12] =
461 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
462static const long daysInMonthLeap[12] =
463 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
464
Daniel Veillard5a872412002-05-22 06:40:27 +0000465#define MAX_DAYINMONTH(yr,mon) \
466 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
467
Daniel Veillard070803b2002-05-03 07:29:38 +0000468#define VALID_MDAY(dt) \
469 (IS_LEAP(dt->year) ? \
470 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
471 (dt->day <= daysInMonth[dt->mon - 1]))
472
473#define VALID_DATE(dt) \
474 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
475
476#define VALID_TIME(dt) \
477 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
478 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
479
480#define VALID_DATETIME(dt) \
481 (VALID_DATE(dt) && VALID_TIME(dt))
482
483#define SECS_PER_MIN (60)
484#define SECS_PER_HOUR (60 * SECS_PER_MIN)
485#define SECS_PER_DAY (24 * SECS_PER_HOUR)
486
Daniel Veillard5a872412002-05-22 06:40:27 +0000487static const long dayInYearByMonth[12] =
488 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
489static const long dayInLeapYearByMonth[12] =
490 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
491
492#define DAY_IN_YEAR(day, month, year) \
493 ((IS_LEAP(year) ? \
494 dayInLeapYearByMonth[month - 1] : \
495 dayInYearByMonth[month - 1]) + day)
496
497#ifdef DEBUG
498#define DEBUG_DATE(dt) \
499 xmlGenericError(xmlGenericErrorContext, \
500 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
501 dt->type,dt->value.date.year,dt->value.date.mon, \
502 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
503 dt->value.date.sec); \
504 if (dt->value.date.tz_flag) \
505 if (dt->value.date.tzo != 0) \
506 xmlGenericError(xmlGenericErrorContext, \
507 "%+05d\n",dt->value.date.tzo); \
508 else \
509 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
510 else \
511 xmlGenericError(xmlGenericErrorContext,"\n")
512#else
513#define DEBUG_DATE(dt)
514#endif
515
Daniel Veillard070803b2002-05-03 07:29:38 +0000516/**
517 * _xmlSchemaParseGYear:
518 * @dt: pointer to a date structure
519 * @str: pointer to the string to analyze
520 *
521 * Parses a xs:gYear without time zone and fills in the appropriate
522 * field of the @dt structure. @str is updated to point just after the
523 * xs:gYear. It is supposed that @dt->year is big enough to contain
524 * the year.
525 *
526 * Returns 0 or the error code
527 */
528static int
529_xmlSchemaParseGYear (xmlSchemaValDatePtr dt, const xmlChar **str) {
530 const xmlChar *cur = *str, *firstChar;
531 int isneg = 0, digcnt = 0;
532
533 if (((*cur < '0') || (*cur > '9')) &&
534 (*cur != '-') && (*cur != '+'))
535 return -1;
536
537 if (*cur == '-') {
538 isneg = 1;
539 cur++;
540 }
541
542 firstChar = cur;
543
544 while ((*cur >= '0') && (*cur <= '9')) {
545 dt->year = dt->year * 10 + (*cur - '0');
546 cur++;
547 digcnt++;
548 }
549
550 /* year must be at least 4 digits (CCYY); over 4
551 * digits cannot have a leading zero. */
552 if ((digcnt < 4) || ((digcnt > 4) && (*firstChar == '0')))
553 return 1;
554
555 if (isneg)
556 dt->year = - dt->year;
557
558 if (!VALID_YEAR(dt->year))
559 return 2;
560
561 *str = cur;
562 return 0;
563}
564
565/**
566 * PARSE_2_DIGITS:
567 * @num: the integer to fill in
568 * @cur: an #xmlChar *
569 * @invalid: an integer
570 *
571 * Parses a 2-digits integer and updates @num with the value. @cur is
572 * updated to point just after the integer.
573 * In case of error, @invalid is set to %TRUE, values of @num and
574 * @cur are undefined.
575 */
576#define PARSE_2_DIGITS(num, cur, invalid) \
577 if ((cur[0] < '0') || (cur[0] > '9') || \
578 (cur[1] < '0') || (cur[1] > '9')) \
579 invalid = 1; \
580 else \
581 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
582 cur += 2;
583
584/**
585 * PARSE_FLOAT:
586 * @num: the double to fill in
587 * @cur: an #xmlChar *
588 * @invalid: an integer
589 *
590 * Parses a float and updates @num with the value. @cur is
591 * updated to point just after the float. The float must have a
592 * 2-digits integer part and may or may not have a decimal part.
593 * In case of error, @invalid is set to %TRUE, values of @num and
594 * @cur are undefined.
595 */
596#define PARSE_FLOAT(num, cur, invalid) \
597 PARSE_2_DIGITS(num, cur, invalid); \
598 if (!invalid && (*cur == '.')) { \
599 double mult = 1; \
600 cur++; \
601 if ((*cur < '0') || (*cur > '9')) \
602 invalid = 1; \
603 while ((*cur >= '0') && (*cur <= '9')) { \
604 mult /= 10; \
605 num += (*cur - '0') * mult; \
606 cur++; \
607 } \
608 }
609
610/**
611 * _xmlSchemaParseGMonth:
612 * @dt: pointer to a date structure
613 * @str: pointer to the string to analyze
614 *
615 * Parses a xs:gMonth without time zone and fills in the appropriate
616 * field of the @dt structure. @str is updated to point just after the
617 * xs:gMonth.
618 *
619 * Returns 0 or the error code
620 */
621static int
622_xmlSchemaParseGMonth (xmlSchemaValDatePtr dt, const xmlChar **str) {
623 const xmlChar *cur = *str;
624 int ret = 0;
625
626 PARSE_2_DIGITS(dt->mon, cur, ret);
627 if (ret != 0)
628 return ret;
629
630 if (!VALID_MONTH(dt->mon))
631 return 2;
632
633 *str = cur;
634 return 0;
635}
636
637/**
638 * _xmlSchemaParseGDay:
639 * @dt: pointer to a date structure
640 * @str: pointer to the string to analyze
641 *
642 * Parses a xs:gDay without time zone and fills in the appropriate
643 * field of the @dt structure. @str is updated to point just after the
644 * xs:gDay.
645 *
646 * Returns 0 or the error code
647 */
648static int
649_xmlSchemaParseGDay (xmlSchemaValDatePtr dt, const xmlChar **str) {
650 const xmlChar *cur = *str;
651 int ret = 0;
652
653 PARSE_2_DIGITS(dt->day, cur, ret);
654 if (ret != 0)
655 return ret;
656
657 if (!VALID_DAY(dt->day))
658 return 2;
659
660 *str = cur;
661 return 0;
662}
663
664/**
665 * _xmlSchemaParseTime:
666 * @dt: pointer to a date structure
667 * @str: pointer to the string to analyze
668 *
669 * Parses a xs:time without time zone and fills in the appropriate
670 * fields of the @dt structure. @str is updated to point just after the
671 * xs:time.
672 * In case of error, values of @dt fields are undefined.
673 *
674 * Returns 0 or the error code
675 */
676static int
677_xmlSchemaParseTime (xmlSchemaValDatePtr dt, const xmlChar **str) {
678 const xmlChar *cur = *str;
679 unsigned int hour = 0; /* use temp var in case str is not xs:time */
680 int ret = 0;
681
682 PARSE_2_DIGITS(hour, cur, ret);
683 if (ret != 0)
684 return ret;
685
686 if (*cur != ':')
687 return 1;
688 cur++;
689
690 /* the ':' insures this string is xs:time */
691 dt->hour = hour;
692
693 PARSE_2_DIGITS(dt->min, cur, ret);
694 if (ret != 0)
695 return ret;
696
697 if (*cur != ':')
698 return 1;
699 cur++;
700
701 PARSE_FLOAT(dt->sec, cur, ret);
702 if (ret != 0)
703 return ret;
704
705 if (!VALID_TIME(dt))
706 return 2;
707
708 *str = cur;
709 return 0;
710}
711
712/**
713 * _xmlSchemaParseTimeZone:
714 * @dt: pointer to a date structure
715 * @str: pointer to the string to analyze
716 *
717 * Parses a time zone without time zone and fills in the appropriate
718 * field of the @dt structure. @str is updated to point just after the
719 * time zone.
720 *
721 * Returns 0 or the error code
722 */
723static int
724_xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt, const xmlChar **str) {
725 const xmlChar *cur = *str;
726 int ret = 0;
727
728 if (str == NULL)
729 return -1;
730
731 switch (*cur) {
732 case 0:
733 dt->tz_flag = 0;
734 dt->tzo = 0;
735 break;
736
737 case 'Z':
738 dt->tz_flag = 1;
739 dt->tzo = 0;
740 cur++;
741 break;
742
743 case '+':
744 case '-': {
745 int isneg = 0, tmp = 0;
746 isneg = (*cur == '-');
747
748 cur++;
749
750 PARSE_2_DIGITS(tmp, cur, ret);
751 if (ret != 0)
752 return ret;
753 if (!VALID_HOUR(tmp))
754 return 2;
755
756 if (*cur != ':')
757 return 1;
758 cur++;
759
760 dt->tzo = tmp * 60;
761
762 PARSE_2_DIGITS(tmp, cur, ret);
763 if (ret != 0)
764 return ret;
765 if (!VALID_MIN(tmp))
766 return 2;
767
768 dt->tzo += tmp;
769 if (isneg)
770 dt->tzo = - dt->tzo;
771
772 if (!VALID_TZO(dt->tzo))
773 return 2;
774
Daniel Veillard5a872412002-05-22 06:40:27 +0000775 dt->tz_flag = 1;
Daniel Veillard070803b2002-05-03 07:29:38 +0000776 break;
777 }
778 default:
779 return 1;
780 }
781
782 *str = cur;
783 return 0;
784}
785
786/****************************************************************
787 * *
788 * XML Schema Dates/Times Datatypes Handling *
789 * *
790 ****************************************************************/
791
792/**
793 * PARSE_DIGITS:
794 * @num: the integer to fill in
795 * @cur: an #xmlChar *
796 * @num_type: an integer flag
797 *
798 * Parses a digits integer and updates @num with the value. @cur is
799 * updated to point just after the integer.
800 * In case of error, @num_type is set to -1, values of @num and
801 * @cur are undefined.
802 */
803#define PARSE_DIGITS(num, cur, num_type) \
804 if ((*cur < '0') || (*cur > '9')) \
805 num_type = -1; \
806 else \
807 while ((*cur >= '0') && (*cur <= '9')) { \
808 num = num * 10 + (*cur - '0'); \
809 cur++; \
810 }
811
812/**
813 * PARSE_NUM:
814 * @num: the double to fill in
815 * @cur: an #xmlChar *
816 * @num_type: an integer flag
817 *
818 * Parses a float or integer and updates @num with the value. @cur is
819 * updated to point just after the number. If the number is a float,
820 * then it must have an integer part and a decimal part; @num_type will
821 * be set to 1. If there is no decimal part, @num_type is set to zero.
822 * In case of error, @num_type is set to -1, values of @num and
823 * @cur are undefined.
824 */
825#define PARSE_NUM(num, cur, num_type) \
826 num = 0; \
827 PARSE_DIGITS(num, cur, num_type); \
828 if (!num_type && (*cur == '.')) { \
829 double mult = 1; \
830 cur++; \
831 if ((*cur < '0') || (*cur > '9')) \
832 num_type = -1; \
833 else \
834 num_type = 1; \
835 while ((*cur >= '0') && (*cur <= '9')) { \
836 mult /= 10; \
837 num += (*cur - '0') * mult; \
838 cur++; \
839 } \
840 }
841
842/**
Daniel Veillard5a872412002-05-22 06:40:27 +0000843 * xmlSchemaValidateDates:
Daniel Veillard455cc072003-03-31 10:13:23 +0000844 * @type: the expected type or XML_SCHEMAS_UNKNOWN
Daniel Veillard070803b2002-05-03 07:29:38 +0000845 * @dateTime: string to analyze
846 * @val: the return computed value
847 *
848 * Check that @dateTime conforms to the lexical space of one of the date types.
849 * if true a value is computed and returned in @val.
850 *
851 * Returns 0 if this validates, a positive error code number otherwise
852 * and -1 in case of internal or API error.
853 */
854static int
Daniel Veillard455cc072003-03-31 10:13:23 +0000855xmlSchemaValidateDates (xmlSchemaValType type,
Daniel Veillard118aed72002-09-24 14:13:13 +0000856 const xmlChar *dateTime, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +0000857 xmlSchemaValPtr dt;
858 int ret;
859 const xmlChar *cur = dateTime;
860
861#define RETURN_TYPE_IF_VALID(t) \
862 if (IS_TZO_CHAR(*cur)) { \
863 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
864 if (ret == 0) { \
865 if (*cur != 0) \
866 goto error; \
867 dt->type = t; \
Daniel Veillard455cc072003-03-31 10:13:23 +0000868 goto done; \
Daniel Veillard070803b2002-05-03 07:29:38 +0000869 } \
870 }
871
872 if (dateTime == NULL)
873 return -1;
874
875 if ((*cur != '-') && (*cur < '0') && (*cur > '9'))
876 return 1;
877
878 dt = xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN);
879 if (dt == NULL)
880 return -1;
881
882 if ((cur[0] == '-') && (cur[1] == '-')) {
883 /*
884 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
885 * xs:gDay)
886 */
887 cur += 2;
888
889 /* is it an xs:gDay? */
890 if (*cur == '-') {
Daniel Veillard455cc072003-03-31 10:13:23 +0000891 if (type == XML_SCHEMAS_GMONTH)
892 goto error;
Daniel Veillard070803b2002-05-03 07:29:38 +0000893 ++cur;
894 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
895 if (ret != 0)
896 goto error;
897
898 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY);
899
900 goto error;
901 }
902
903 /*
904 * it should be an xs:gMonthDay or xs:gMonth
905 */
906 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
907 if (ret != 0)
908 goto error;
909
Daniel Veillardd3b9cd82003-04-09 11:24:17 +0000910 /*
911 * a '-' char could indicate this type is xs:gMonthDay or
912 * a negative time zone offset. Check for xs:gMonthDay first.
913 * Also the first three char's of a negative tzo (-MM:SS) can
914 * appear to be a valid day; so even if the day portion
915 * of the xs:gMonthDay verifies, we must insure it was not
916 * a tzo.
917 */
918 if (*cur == '-') {
919 const xmlChar *rewnd = cur;
920 cur++;
Daniel Veillard070803b2002-05-03 07:29:38 +0000921
Daniel Veillardd3b9cd82003-04-09 11:24:17 +0000922 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
923 if ((ret == 0) && ((*cur == 0) || (*cur != ':'))) {
924
925 /*
926 * we can use the VALID_MDAY macro to validate the month
927 * and day because the leap year test will flag year zero
928 * as a leap year (even though zero is an invalid year).
929 */
930 if (VALID_MDAY((&(dt->value.date)))) {
931
932 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY);
933
934 goto error;
935 }
936 }
937
938 /*
939 * not xs:gMonthDay so rewind and check if just xs:gMonth
940 * with an optional time zone.
941 */
942 cur = rewnd;
943 }
944
945 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH);
Daniel Veillard070803b2002-05-03 07:29:38 +0000946
947 goto error;
948 }
949
950 /*
951 * It's a right-truncated date or an xs:time.
952 * Try to parse an xs:time then fallback on right-truncated dates.
953 */
954 if ((*cur >= '0') && (*cur <= '9')) {
955 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
956 if (ret == 0) {
957 /* it's an xs:time */
958 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME);
959 }
960 }
961
962 /* fallback on date parsing */
963 cur = dateTime;
964
965 ret = _xmlSchemaParseGYear(&(dt->value.date), &cur);
966 if (ret != 0)
967 goto error;
968
969 /* is it an xs:gYear? */
970 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR);
971
972 if (*cur != '-')
973 goto error;
974 cur++;
975
976 ret = _xmlSchemaParseGMonth(&(dt->value.date), &cur);
977 if (ret != 0)
978 goto error;
979
980 /* is it an xs:gYearMonth? */
981 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH);
982
983 if (*cur != '-')
984 goto error;
985 cur++;
986
987 ret = _xmlSchemaParseGDay(&(dt->value.date), &cur);
988 if ((ret != 0) || !VALID_DATE((&(dt->value.date))))
989 goto error;
990
991 /* is it an xs:date? */
992 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE);
993
994 if (*cur != 'T')
995 goto error;
996 cur++;
997
998 /* it should be an xs:dateTime */
999 ret = _xmlSchemaParseTime(&(dt->value.date), &cur);
1000 if (ret != 0)
1001 goto error;
1002
1003 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur);
1004 if ((ret != 0) || (*cur != 0) || !VALID_DATETIME((&(dt->value.date))))
1005 goto error;
1006
Daniel Veillard455cc072003-03-31 10:13:23 +00001007
Daniel Veillard070803b2002-05-03 07:29:38 +00001008 dt->type = XML_SCHEMAS_DATETIME;
1009
Daniel Veillard455cc072003-03-31 10:13:23 +00001010done:
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001011#if 1
1012 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type))
1013 goto error;
1014#else
1015 /*
1016 * insure the parsed type is equal to or less significant (right
1017 * truncated) than the desired type.
1018 */
1019 if ((type != XML_SCHEMAS_UNKNOWN) && (type != dt->type)) {
1020
1021 /* time only matches time */
1022 if ((type == XML_SCHEMAS_TIME) && (dt->type == XML_SCHEMAS_TIME))
1023 goto error;
1024
1025 if ((type == XML_SCHEMAS_DATETIME) &&
1026 ((dt->type != XML_SCHEMAS_DATE) ||
1027 (dt->type != XML_SCHEMAS_GYEARMONTH) ||
1028 (dt->type != XML_SCHEMAS_GYEAR)))
1029 goto error;
1030
1031 if ((type == XML_SCHEMAS_DATE) &&
1032 ((dt->type != XML_SCHEMAS_GYEAR) ||
1033 (dt->type != XML_SCHEMAS_GYEARMONTH)))
1034 goto error;
1035
1036 if ((type == XML_SCHEMAS_GYEARMONTH) && (dt->type != XML_SCHEMAS_GYEAR))
1037 goto error;
1038
1039 if ((type == XML_SCHEMAS_GMONTHDAY) && (dt->type != XML_SCHEMAS_GMONTH))
1040 goto error;
1041 }
Daniel Veillard455cc072003-03-31 10:13:23 +00001042#endif
1043
Daniel Veillard070803b2002-05-03 07:29:38 +00001044 if (val != NULL)
1045 *val = dt;
Daniel Veillard80b19092003-03-28 13:29:53 +00001046 else
1047 xmlSchemaFreeValue(dt);
Daniel Veillard070803b2002-05-03 07:29:38 +00001048
1049 return 0;
1050
1051error:
1052 if (dt != NULL)
1053 xmlSchemaFreeValue(dt);
1054 return 1;
1055}
1056
1057/**
Daniel Veillard5a872412002-05-22 06:40:27 +00001058 * xmlSchemaValidateDuration:
Daniel Veillard070803b2002-05-03 07:29:38 +00001059 * @type: the predefined type
1060 * @duration: string to analyze
1061 * @val: the return computed value
1062 *
1063 * Check that @duration conforms to the lexical space of the duration type.
1064 * if true a value is computed and returned in @val.
1065 *
1066 * Returns 0 if this validates, a positive error code number otherwise
1067 * and -1 in case of internal or API error.
1068 */
1069static int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00001070xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00001071 const xmlChar *duration, xmlSchemaValPtr *val) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001072 const xmlChar *cur = duration;
1073 xmlSchemaValPtr dur;
1074 int isneg = 0;
1075 unsigned int seq = 0;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001076 double num;
1077 int num_type = 0; /* -1 = invalid, 0 = int, 1 = floating */
1078 const xmlChar desig[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1079 const double multi[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
Daniel Veillard070803b2002-05-03 07:29:38 +00001080
1081 if (duration == NULL)
1082 return -1;
1083
1084 if (*cur == '-') {
1085 isneg = 1;
1086 cur++;
1087 }
1088
1089 /* duration must start with 'P' (after sign) */
1090 if (*cur++ != 'P')
1091 return 1;
1092
Daniel Veillard80b19092003-03-28 13:29:53 +00001093 if (*cur == 0)
1094 return 1;
1095
Daniel Veillard070803b2002-05-03 07:29:38 +00001096 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
1097 if (dur == NULL)
1098 return -1;
1099
1100 while (*cur != 0) {
Daniel Veillard070803b2002-05-03 07:29:38 +00001101
1102 /* input string should be empty or invalid date/time item */
1103 if (seq >= sizeof(desig))
1104 goto error;
1105
1106 /* T designator must be present for time items */
1107 if (*cur == 'T') {
1108 if (seq <= 3) {
1109 seq = 3;
1110 cur++;
1111 } else
1112 return 1;
1113 } else if (seq == 3)
1114 goto error;
1115
1116 /* parse the number portion of the item */
1117 PARSE_NUM(num, cur, num_type);
1118
1119 if ((num_type == -1) || (*cur == 0))
1120 goto error;
1121
1122 /* update duration based on item type */
1123 while (seq < sizeof(desig)) {
1124 if (*cur == desig[seq]) {
1125
1126 /* verify numeric type; only seconds can be float */
1127 if ((num_type != 0) && (seq < (sizeof(desig)-1)))
1128 goto error;
1129
1130 switch (seq) {
1131 case 0:
1132 dur->value.dur.mon = (long)num * 12;
1133 break;
1134 case 1:
1135 dur->value.dur.mon += (long)num;
1136 break;
1137 default:
1138 /* convert to seconds using multiplier */
1139 dur->value.dur.sec += num * multi[seq];
1140 seq++;
1141 break;
1142 }
1143
1144 break; /* exit loop */
1145 }
1146 /* no date designators found? */
1147 if (++seq == 3)
1148 goto error;
1149 }
1150 cur++;
1151 }
1152
1153 if (isneg) {
1154 dur->value.dur.mon = -dur->value.dur.mon;
1155 dur->value.dur.day = -dur->value.dur.day;
1156 dur->value.dur.sec = -dur->value.dur.sec;
1157 }
1158
1159 if (val != NULL)
1160 *val = dur;
Daniel Veillard80b19092003-03-28 13:29:53 +00001161 else
1162 xmlSchemaFreeValue(dur);
Daniel Veillard070803b2002-05-03 07:29:38 +00001163
1164 return 0;
1165
1166error:
1167 if (dur != NULL)
1168 xmlSchemaFreeValue(dur);
1169 return 1;
1170}
1171
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001172/**
1173 * xmlSchemaStrip:
1174 * @value: a value
1175 *
1176 * Removes the leading and ending spaces of a string
1177 *
1178 * Returns the new string or NULL if no change was required.
1179 */
1180static xmlChar *
1181xmlSchemaStrip(const xmlChar *value) {
1182 const xmlChar *start = value, *end, *f;
1183
1184 if (value == NULL) return(NULL);
1185 while ((*start != 0) && (IS_BLANK(*start))) start++;
1186 end = start;
1187 while (*end != 0) end++;
1188 f = end;
1189 end--;
1190 while ((end > start) && (IS_BLANK(*end))) end--;
1191 end++;
1192 if ((start == value) && (f == end)) return(NULL);
1193 return(xmlStrndup(start, end - start));
1194}
Daniel Veillard96a4b252003-02-06 08:22:32 +00001195
1196/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001197 * xmlSchemaCollapseString:
1198 * @value: a value
1199 *
1200 * Removes and normalize white spaces in the string
1201 *
1202 * Returns the new string or NULL if no change was required.
1203 */
1204static xmlChar *
1205xmlSchemaCollapseString(const xmlChar *value) {
1206 const xmlChar *start = value, *end, *f;
1207 xmlChar *g;
1208 int col = 0;
1209
1210 if (value == NULL) return(NULL);
1211 while ((*start != 0) && (IS_BLANK(*start))) start++;
1212 end = start;
1213 while (*end != 0) {
1214 if ((*end == ' ') && (IS_BLANK(end[1]))) {
1215 col = end - start;
1216 break;
1217 } else if ((*end == 0xa) || (*end == 0x9) || (*end == 0xd)) {
1218 col = end - start;
1219 break;
1220 }
1221 end++;
1222 }
1223 if (col == 0) {
1224 f = end;
1225 end--;
1226 while ((end > start) && (IS_BLANK(*end))) end--;
1227 end++;
1228 if ((start == value) && (f == end)) return(NULL);
1229 return(xmlStrndup(start, end - start));
1230 }
1231 start = xmlStrdup(start);
1232 if (start == NULL) return(NULL);
1233 g = (xmlChar *) (start + col);
1234 end = g;
1235 while (*end != 0) {
1236 if (IS_BLANK(*end)) {
1237 end++;
1238 while (IS_BLANK(*end)) end++;
1239 if (*end != 0)
1240 *g++ = ' ';
1241 } else
1242 *g++ = *end++;
1243 }
1244 *g = 0;
1245 return((xmlChar *) start);
1246}
1247
1248/**
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001249 * xmlSchemaValAtomicListNode:
1250 * @type: the predefined atomic type for a token in the list
1251 * @value: the list value to check
1252 * @ret: the return computed value
1253 * @node: the node containing the value
1254 *
1255 * Check that a value conforms to the lexical space of the predefined
1256 * list type. if true a value is computed and returned in @ret.
1257 *
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001258 * Returns the number of items if this validates, a negative error code
1259 * number otherwise
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001260 */
1261static int
1262xmlSchemaValAtomicListNode(xmlSchemaTypePtr type, const xmlChar *value,
1263 xmlSchemaValPtr *ret, xmlNodePtr node) {
1264 xmlChar *val, *cur, *endval;
1265 int nb_values = 0;
Daniel Veillard580ced82003-03-21 21:22:48 +00001266 int tmp = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001267
1268 if (value == NULL) {
1269 return(-1);
1270 }
1271 val = xmlStrdup(value);
1272 if (val == NULL) {
1273 return(-1);
1274 }
1275 cur = val;
1276 /*
1277 * Split the list
1278 */
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001279 while (IS_BLANK(*cur)) *cur++ = 0;
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001280 while (*cur != 0) {
1281 if (IS_BLANK(*cur)) {
1282 *cur = 0;
1283 cur++;
1284 while (IS_BLANK(*cur)) *cur++ = 0;
1285 } else {
1286 nb_values++;
1287 cur++;
1288 while ((*cur != 0) && (!IS_BLANK(*cur))) cur++;
1289 }
1290 }
1291 if (nb_values == 0) {
1292 if (ret != NULL) {
1293 TODO
1294 }
1295 xmlFree(val);
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001296 return(nb_values);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001297 }
1298 endval = cur;
1299 cur = val;
1300 while ((*cur == 0) && (cur != endval)) cur++;
1301 while (cur != endval) {
1302 tmp = xmlSchemaValPredefTypeNode(type, cur, NULL, node);
1303 if (tmp != 0)
1304 break;
1305 while (*cur != 0) cur++;
1306 while ((*cur == 0) && (cur != endval)) cur++;
1307 }
1308 xmlFree(val);
1309 if (ret != NULL) {
1310 TODO
1311 }
Daniel Veillarda1a9d042003-03-18 16:53:17 +00001312 if (tmp == 0)
1313 return(nb_values);
1314 return(-1);
Daniel Veillard28c52ab2003-03-18 11:39:17 +00001315}
1316
1317/**
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001318 * xmlSchemaParseUInt:
1319 * @str: pointer to the string R/W
1320 * @llo: pointer to the low result
1321 * @lmi: pointer to the mid result
1322 * @lhi: pointer to the high result
1323 *
1324 * Parse an unsigned long into 3 fields.
1325 *
1326 * Returns the number of chars parsed or -1 if overflow of the capacity
1327 */
1328static int
1329xmlSchemaParseUInt(const xmlChar **str, unsigned long *llo,
1330 unsigned long *lmi, unsigned long *lhi) {
1331 unsigned long lo = 0, mi = 0, hi = 0;
1332 const xmlChar *tmp, *cur = *str;
1333 int ret = 0, i = 0;
1334
1335 while (*cur == '0') {
1336 ret++;
1337 cur++;
1338 }
1339 tmp = cur;
1340 while ((*tmp != 0) && (*tmp >= '0') && (*tmp <= '9')) {
1341 i++;tmp++;ret++;
1342 }
1343 if (i > 24) {
1344 *str = tmp;
1345 return(-1);
1346 }
1347 while (i > 16) {
1348 hi = hi * 10 + (*cur++ - '0');
1349 i--;
1350 }
1351 while (i > 8) {
1352 mi = mi * 10 + (*cur++ - '0');
1353 i--;
1354 }
1355 while (i > 0) {
1356 lo = lo * 10 + (*cur++ - '0');
1357 i--;
1358 }
1359
1360 *str = cur;
1361 *llo = lo;
1362 *lmi = mi;
1363 *lhi = hi;
1364 return(ret);
1365}
1366
1367/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001368 * xmlSchemaValAtomicType:
1369 * @type: the predefined type
1370 * @value: the value to check
1371 * @val: the return computed value
1372 * @node: the node containing the value
1373 * flags: flags to control the vlidation
1374 *
1375 * Check that a value conforms to the lexical space of the atomic type.
1376 * if true a value is computed and returned in @val.
1377 *
1378 * Returns 0 if this validates, a positive error code number otherwise
1379 * and -1 in case of internal or API error.
1380 */
1381static int
1382xmlSchemaValAtomicType(xmlSchemaTypePtr type, const xmlChar *value,
1383 xmlSchemaValPtr *val, xmlNodePtr node, int flags) {
1384 xmlSchemaValPtr v;
1385 xmlChar *norm = NULL;
Daniel Veillardd3b9cd82003-04-09 11:24:17 +00001386 int ret = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001387
1388 if (xmlSchemaTypesInitialized == 0)
1389 return(-1);
1390 if (type == NULL)
1391 return(-1);
1392
1393 if (val != NULL)
1394 *val = NULL;
1395 if ((flags == 0) && (value != NULL)) {
1396 if ((type->flags != XML_SCHEMAS_STRING) &&
1397 (type->flags != XML_SCHEMAS_NORMSTRING)) {
1398 norm = xmlSchemaCollapseString(value);
1399 if (norm != NULL)
1400 value = norm;
1401 }
1402 }
1403
1404 switch (type->flags) {
1405 case XML_SCHEMAS_UNKNOWN:
1406 if (type == xmlSchemaTypeAnyTypeDef)
1407 goto return0;
1408 goto error;
1409 case XML_SCHEMAS_STRING:
1410 goto return0;
1411 case XML_SCHEMAS_NORMSTRING:
1412 TODO
1413 goto return0;
1414 case XML_SCHEMAS_DECIMAL: {
1415 const xmlChar *cur = value, *tmp;
William M. Brackc1939562003-08-05 15:52:22 +00001416 unsigned int frac = 0, len, neg = 0;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001417 unsigned long base = 0;
1418 if (cur == NULL)
1419 goto return1;
1420 if (*cur == '+')
1421 cur++;
1422 else if (*cur == '-') {
1423 neg = 1;
1424 cur++;
1425 }
1426 tmp = cur;
1427 while ((*cur >= '0') && (*cur <= '9')) {
1428 base = base * 10 + (*cur - '0');
1429 cur++;
1430 }
1431 len = cur - tmp;
1432 if (*cur == '.') {
1433 cur++;
1434 tmp = cur;
1435 while ((*cur >= '0') && (*cur <= '9')) {
1436 base = base * 10 + (*cur - '0');
1437 cur++;
1438 }
1439 frac = cur - tmp;
1440 }
1441 if (*cur != 0)
1442 goto return1;
1443 if (val != NULL) {
1444 v = xmlSchemaNewValue(XML_SCHEMAS_DECIMAL);
1445 if (v != NULL) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001446 v->value.decimal.lo = base;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001447 v->value.decimal.sign = neg;
1448 v->value.decimal.frac = frac;
1449 v->value.decimal.total = frac + len;
1450 *val = v;
1451 }
1452 }
1453 goto return0;
1454 }
1455 case XML_SCHEMAS_TIME:
1456 case XML_SCHEMAS_GDAY:
1457 case XML_SCHEMAS_GMONTH:
1458 case XML_SCHEMAS_GMONTHDAY:
1459 case XML_SCHEMAS_GYEAR:
1460 case XML_SCHEMAS_GYEARMONTH:
1461 case XML_SCHEMAS_DATE:
1462 case XML_SCHEMAS_DATETIME:
Daniel Veillard455cc072003-03-31 10:13:23 +00001463 ret = xmlSchemaValidateDates(type->flags, value, val);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001464 break;
1465 case XML_SCHEMAS_DURATION:
1466 ret = xmlSchemaValidateDuration(type, value, val);
1467 break;
1468 case XML_SCHEMAS_FLOAT:
1469 case XML_SCHEMAS_DOUBLE: {
1470 const xmlChar *cur = value;
1471 int neg = 0;
1472 if (cur == NULL)
1473 goto return1;
1474 if ((cur[0] == 'N') && (cur[1] == 'a') && (cur[2] == 'N')) {
1475 cur += 3;
1476 if (*cur != 0)
1477 goto return1;
1478 if (val != NULL) {
1479 if (type == xmlSchemaTypeFloatDef) {
1480 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1481 if (v != NULL) {
1482 v->value.f = (float) xmlXPathNAN;
1483 } else {
1484 xmlSchemaFreeValue(v);
1485 goto error;
1486 }
1487 } else {
1488 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1489 if (v != NULL) {
1490 v->value.d = xmlXPathNAN;
1491 } else {
1492 xmlSchemaFreeValue(v);
1493 goto error;
1494 }
1495 }
1496 *val = v;
1497 }
1498 goto return0;
1499 }
1500 if (*cur == '-') {
1501 neg = 1;
1502 cur++;
1503 }
1504 if ((cur[0] == 'I') && (cur[1] == 'N') && (cur[2] == 'F')) {
1505 cur += 3;
1506 if (*cur != 0)
1507 goto return1;
1508 if (val != NULL) {
1509 if (type == xmlSchemaTypeFloatDef) {
1510 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1511 if (v != NULL) {
1512 if (neg)
1513 v->value.f = (float) xmlXPathNINF;
1514 else
1515 v->value.f = (float) xmlXPathPINF;
1516 } else {
1517 xmlSchemaFreeValue(v);
1518 goto error;
1519 }
1520 } else {
1521 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1522 if (v != NULL) {
1523 if (neg)
1524 v->value.d = xmlXPathNINF;
1525 else
1526 v->value.d = xmlXPathPINF;
1527 } else {
1528 xmlSchemaFreeValue(v);
1529 goto error;
1530 }
1531 }
1532 *val = v;
1533 }
1534 goto return0;
1535 }
1536 if ((neg == 0) && (*cur == '+'))
1537 cur++;
1538 if ((cur[0] == 0) || (cur[0] == '+') || (cur[0] == '-'))
1539 goto return1;
1540 while ((*cur >= '0') && (*cur <= '9')) {
1541 cur++;
1542 }
1543 if (*cur == '.') {
1544 cur++;
1545 while ((*cur >= '0') && (*cur <= '9'))
1546 cur++;
1547 }
1548 if ((*cur == 'e') || (*cur == 'E')) {
1549 cur++;
1550 if ((*cur == '-') || (*cur == '+'))
1551 cur++;
1552 while ((*cur >= '0') && (*cur <= '9'))
1553 cur++;
1554 }
1555 if (*cur != 0)
1556 goto return1;
1557 if (val != NULL) {
1558 if (type == xmlSchemaTypeFloatDef) {
1559 v = xmlSchemaNewValue(XML_SCHEMAS_FLOAT);
1560 if (v != NULL) {
1561 if (sscanf((const char *)value, "%f", &(v->value.f))==1) {
1562 *val = v;
1563 } else {
1564 xmlGenericError(xmlGenericErrorContext,
1565 "failed to scanf float %s\n", value);
1566 xmlSchemaFreeValue(v);
1567 goto return1;
1568 }
1569 } else {
1570 goto error;
1571 }
1572 } else {
1573 v = xmlSchemaNewValue(XML_SCHEMAS_DOUBLE);
1574 if (v != NULL) {
1575 if (sscanf((const char *)value, "%lf", &(v->value.d))==1) {
1576 *val = v;
1577 } else {
1578 xmlGenericError(xmlGenericErrorContext,
1579 "failed to scanf double %s\n", value);
1580 xmlSchemaFreeValue(v);
1581 goto return1;
1582 }
1583 } else {
1584 goto error;
1585 }
1586 }
1587 }
1588 goto return0;
1589 }
1590 case XML_SCHEMAS_BOOLEAN: {
1591 const xmlChar *cur = value;
1592
1593 if ((cur[0] == '0') && (cur[1] == 0))
1594 ret = 0;
1595 else if ((cur[0] == '1') && (cur[1] == 0))
1596 ret = 1;
1597 else if ((cur[0] == 't') && (cur[1] == 'r') && (cur[2] == 'u') &&
1598 (cur[3] == 'e') && (cur[4] == 0))
1599 ret = 1;
1600 else if ((cur[0] == 'f') && (cur[1] == 'a') && (cur[2] == 'l') &&
1601 (cur[3] == 's') && (cur[4] == 'e') && (cur[5] == 0))
1602 ret = 0;
1603 else
1604 goto return1;
1605 if (val != NULL) {
1606 v = xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN);
1607 if (v != NULL) {
1608 v->value.b = ret;
1609 *val = v;
1610 } else {
1611 goto error;
1612 }
1613 }
1614 goto return0;
1615 }
1616 case XML_SCHEMAS_TOKEN: {
1617 const xmlChar *cur = value;
1618
1619 if (IS_BLANK(*cur))
1620 goto return1;
1621
1622 while (*cur != 0) {
1623 if ((*cur == 0xd) || (*cur == 0xa) || (*cur == 0x9)) {
1624 goto return1;
1625 } else if (*cur == ' ') {
1626 cur++;
1627 if (*cur == 0)
1628 goto return1;
1629 if (*cur == ' ')
1630 goto return1;
1631 } else {
1632 cur++;
1633 }
1634 }
1635 if (val != NULL) {
1636 v = xmlSchemaNewValue(XML_SCHEMAS_TOKEN);
1637 if (v != NULL) {
1638 v->value.str = xmlStrdup(value);
1639 *val = v;
1640 } else {
1641 goto error;
1642 }
1643 }
1644 goto return0;
1645 }
1646 case XML_SCHEMAS_LANGUAGE:
1647 if (xmlCheckLanguageID(value) == 1) {
1648 if (val != NULL) {
1649 v = xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE);
1650 if (v != NULL) {
1651 v->value.str = xmlStrdup(value);
1652 *val = v;
1653 } else {
1654 goto error;
1655 }
1656 }
1657 goto return0;
1658 }
1659 goto return1;
1660 case XML_SCHEMAS_NMTOKEN:
1661 if (xmlValidateNMToken(value, 1) == 0) {
1662 if (val != NULL) {
1663 v = xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN);
1664 if (v != NULL) {
1665 v->value.str = xmlStrdup(value);
1666 *val = v;
1667 } else {
1668 goto error;
1669 }
1670 }
1671 goto return0;
1672 }
1673 goto return1;
1674 case XML_SCHEMAS_NMTOKENS:
1675 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef,
1676 value, val, node);
1677 if (ret > 0)
1678 ret = 0;
1679 else
1680 ret = 1;
1681 goto done;
1682 case XML_SCHEMAS_NAME:
1683 ret = xmlValidateName(value, 1);
1684 if ((ret == 0) && (val != NULL)) {
1685 TODO;
1686 }
1687 goto done;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001688 case XML_SCHEMAS_QNAME: {
1689 xmlChar *uri = NULL;
1690 xmlChar *local = NULL;
1691
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001692 ret = xmlValidateQName(value, 1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001693 if ((ret == 0) && (node != NULL)) {
1694 xmlChar *prefix;
1695 local = xmlSplitQName2(value, &prefix);
1696 if (prefix != NULL) {
1697 xmlNsPtr ns;
1698
1699 ns = xmlSearchNs(node->doc, node, prefix);
1700 if (ns == NULL)
1701 ret = 1;
1702 else if (val != NULL)
1703 uri = xmlStrdup(ns->href);
1704 }
1705 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1706 xmlFree(local);
1707 if (prefix != NULL)
1708 xmlFree(prefix);
1709 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001710 if ((ret == 0) && (val != NULL)) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001711 v = xmlSchemaNewValue(XML_SCHEMAS_QNAME);
1712 if (v != NULL) {
1713 if (local != NULL)
1714 v->value.qname.name = local;
1715 else
1716 v->value.qname.name = xmlStrdup(value);
1717 if (uri != NULL)
1718 v->value.qname.uri = uri;
1719
1720 *val = v;
1721 } else {
1722 if (local != NULL)
1723 xmlFree(local);
1724 if (uri != NULL)
1725 xmlFree(uri);
1726 goto error;
1727 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001728 }
1729 goto done;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001730 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001731 case XML_SCHEMAS_NCNAME:
1732 ret = xmlValidateNCName(value, 1);
1733 if ((ret == 0) && (val != NULL)) {
1734 v = xmlSchemaNewValue(XML_SCHEMAS_NCNAME);
1735 if (v != NULL) {
1736 v->value.str = xmlStrdup(value);
1737 *val = v;
1738 } else {
1739 goto error;
1740 }
1741 }
1742 goto done;
1743 case XML_SCHEMAS_ID:
1744 ret = xmlValidateNCName(value, 1);
1745 if ((ret == 0) && (val != NULL)) {
Daniel Veillard75bb3bb2003-05-12 15:25:56 +00001746 v = xmlSchemaNewValue(XML_SCHEMAS_ID);
1747 if (v != NULL) {
1748 v->value.str = xmlStrdup(value);
1749 *val = v;
1750 } else {
1751 goto error;
1752 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001753 }
1754 if ((ret == 0) && (node != NULL) &&
1755 (node->type == XML_ATTRIBUTE_NODE)) {
1756 xmlAttrPtr attr = (xmlAttrPtr) node;
1757 /*
1758 * NOTE: the IDness might have already be declared in the DTD
1759 */
1760 if (attr->atype != XML_ATTRIBUTE_ID) {
1761 xmlIDPtr res;
1762 xmlChar *strip;
1763
1764 strip = xmlSchemaStrip(value);
1765 if (strip != NULL) {
1766 res = xmlAddID(NULL, node->doc, strip, attr);
1767 xmlFree(strip);
1768 } else
1769 res = xmlAddID(NULL, node->doc, value, attr);
1770 if (res == NULL) {
1771 ret = 2;
1772 } else {
1773 attr->atype = XML_ATTRIBUTE_ID;
1774 }
1775 }
1776 }
1777 goto done;
1778 case XML_SCHEMAS_IDREF:
1779 ret = xmlValidateNCName(value, 1);
1780 if ((ret == 0) && (val != NULL)) {
1781 TODO;
1782 }
1783 if ((ret == 0) && (node != NULL) &&
1784 (node->type == XML_ATTRIBUTE_NODE)) {
1785 xmlAttrPtr attr = (xmlAttrPtr) node;
1786 xmlChar *strip;
1787
1788 strip = xmlSchemaStrip(value);
1789 if (strip != NULL) {
1790 xmlAddRef(NULL, node->doc, strip, attr);
1791 xmlFree(strip);
1792 } else
1793 xmlAddRef(NULL, node->doc, value, attr);
1794 attr->atype = XML_ATTRIBUTE_IDREF;
1795 }
1796 goto done;
1797 case XML_SCHEMAS_IDREFS:
1798 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef,
1799 value, val, node);
1800 if (ret < 0)
1801 ret = 2;
1802 else
1803 ret = 0;
1804 if ((ret == 0) && (node != NULL) &&
1805 (node->type == XML_ATTRIBUTE_NODE)) {
1806 xmlAttrPtr attr = (xmlAttrPtr) node;
1807
1808 attr->atype = XML_ATTRIBUTE_IDREFS;
1809 }
1810 goto done;
1811 case XML_SCHEMAS_ENTITY: {
1812 xmlChar *strip;
1813 ret = xmlValidateNCName(value, 1);
1814 if ((node == NULL) || (node->doc == NULL))
1815 ret = 3;
1816 if (ret == 0) {
1817 xmlEntityPtr ent;
1818
1819 strip = xmlSchemaStrip(value);
1820 if (strip != NULL) {
1821 ent = xmlGetDocEntity(node->doc, strip);
1822 xmlFree(strip);
1823 } else {
1824 ent = xmlGetDocEntity(node->doc, value);
1825 }
1826 if ((ent == NULL) ||
1827 (ent->etype != XML_EXTERNAL_GENERAL_UNPARSED_ENTITY))
1828 ret = 4;
1829 }
1830 if ((ret == 0) && (val != NULL)) {
1831 TODO;
1832 }
1833 if ((ret == 0) && (node != NULL) &&
1834 (node->type == XML_ATTRIBUTE_NODE)) {
1835 xmlAttrPtr attr = (xmlAttrPtr) node;
1836
1837 attr->atype = XML_ATTRIBUTE_ENTITY;
1838 }
1839 goto done;
1840 }
1841 case XML_SCHEMAS_ENTITIES:
1842 if ((node == NULL) || (node->doc == NULL))
1843 goto return3;
1844 ret = xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef,
1845 value, val, node);
1846 if (ret <= 0)
1847 ret = 1;
1848 else
1849 ret = 0;
1850 if ((ret == 0) && (node != NULL) &&
1851 (node->type == XML_ATTRIBUTE_NODE)) {
1852 xmlAttrPtr attr = (xmlAttrPtr) node;
1853
1854 attr->atype = XML_ATTRIBUTE_ENTITIES;
1855 }
1856 goto done;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001857 case XML_SCHEMAS_NOTATION: {
1858 xmlChar *uri = NULL;
1859 xmlChar *local = NULL;
1860
1861 ret = xmlValidateQName(value, 1);
1862 if ((ret == 0) && (node != NULL)) {
1863 xmlChar *prefix;
1864 local = xmlSplitQName2(value, &prefix);
1865 if (prefix != NULL) {
1866 xmlNsPtr ns;
1867
1868 ns = xmlSearchNs(node->doc, node, prefix);
1869 if (ns == NULL)
1870 ret = 1;
1871 else if (val != NULL)
1872 uri = xmlStrdup(ns->href);
1873 }
1874 if ((local != NULL) && ((val == NULL) || (ret != 0)))
1875 xmlFree(local);
1876 if (prefix != NULL)
1877 xmlFree(prefix);
1878 }
1879 if ((node == NULL) || (node->doc == NULL))
1880 ret = 3;
1881 if (ret == 0) {
1882 ret = xmlValidateNotationUse(NULL, node->doc, value);
1883 if (ret == 1)
1884 ret = 0;
1885 else
1886 ret = 1;
1887 }
1888 if ((ret == 0) && (val != NULL)) {
1889 v = xmlSchemaNewValue(XML_SCHEMAS_NOTATION);
1890 if (v != NULL) {
1891 if (local != NULL)
1892 v->value.qname.name = local;
1893 else
1894 v->value.qname.name = xmlStrdup(value);
1895 if (uri != NULL)
1896 v->value.qname.uri = uri;
1897
1898 *val = v;
1899 } else {
1900 if (local != NULL)
1901 xmlFree(local);
1902 if (uri != NULL)
1903 xmlFree(uri);
1904 goto error;
1905 }
1906 }
1907 goto done;
1908 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001909 case XML_SCHEMAS_ANYURI: {
1910 xmlURIPtr uri;
1911
1912 uri = xmlParseURI((const char *) value);
1913 if (uri == NULL)
1914 goto return1;
1915 if (val != NULL) {
1916 TODO;
1917 }
1918 xmlFreeURI(uri);
1919 goto return0;
1920 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00001921 case XML_SCHEMAS_HEXBINARY: {
1922 const xmlChar *tmp, *cur = value;
1923 int total, i = 0;
1924 unsigned long lo = 0, mi = 0, hi = 0;
1925 unsigned long *base;
1926
Daniel Veillard34ba3872003-07-15 13:34:05 +00001927 if (cur == NULL)
1928 goto return1;
Daniel Veillard560c2a42003-07-06 21:13:49 +00001929 tmp = cur;
1930 while (((*tmp >= '0') && (*tmp <= '9')) ||
1931 ((*tmp >= 'A') && (*tmp <= 'F')) ||
1932 ((*tmp >= 'a') && (*tmp <= 'f'))) {
1933 i++;tmp++;
1934 }
1935
1936 if (*tmp != 0)
1937 goto return1;
1938 if (i > 24)
1939 goto return1;
1940 if ((i % 2) != 0)
1941 goto return1;
1942
1943 total = i / 2; /* number of octets */
1944
1945 if (i >= 16)
1946 base = &hi;
1947 else if (i >= 8)
1948 base = &mi;
1949 else
1950 base = &lo;
1951
1952 while (i > 0) {
1953 if ((*cur >= '0') && (*cur <= '9')) {
1954 *base = *base * 16 + (*cur - '0');
1955 } else if ((*cur >= 'A') && (*cur <= 'F')) {
1956 *base = *base * 16 + (*cur - 'A') + 10;
1957 } else if ((*cur >= 'a') && (*cur <= 'f')) {
1958 *base = *base * 16 + (*cur - 'a') + 10;
1959 } else
1960 break;
1961
1962 cur++;
1963 i--;
1964 if (i == 16)
1965 base = &mi;
1966 else if (i == 8)
1967 base = &lo;
1968 }
1969
1970 if (val != NULL) {
1971 v = xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY);
1972 if (v != NULL) {
1973 v->value.decimal.lo = lo;
1974 v->value.decimal.mi = mi;
1975 v->value.decimal.hi = hi;
1976 v->value.decimal.total = total;
1977 *val = v;
1978 } else {
1979 goto error;
1980 }
1981 }
1982 goto return0;
1983 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001984 case XML_SCHEMAS_INTEGER:
1985 case XML_SCHEMAS_PINTEGER:
1986 case XML_SCHEMAS_NPINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001987 case XML_SCHEMAS_NINTEGER:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001988 case XML_SCHEMAS_NNINTEGER: {
1989 const xmlChar *cur = value;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001990 unsigned long lo, mi, hi;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001991 int sign = 0;
1992 if (cur == NULL)
1993 goto return1;
1994 if (*cur == '-') {
1995 sign = 1;
1996 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001997 } else if (*cur == '+')
Daniel Veillardb6c7f412003-03-29 16:41:55 +00001998 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00001999 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2000 if (ret == 0)
2001 goto return1;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002002 if (*cur != 0)
2003 goto return1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002004 if (type->flags == XML_SCHEMAS_NPINTEGER) {
2005 if ((sign == 0) &&
2006 ((hi != 0) || (mi != 0) || (lo != 0)))
2007 goto return1;
2008 } else if (type->flags == XML_SCHEMAS_PINTEGER) {
2009 if (sign == 1)
2010 goto return1;
2011 if ((hi == 0) && (mi == 0) && (lo == 0))
2012 goto return1;
2013 } else if (type->flags == XML_SCHEMAS_NINTEGER) {
2014 if (sign == 0)
2015 goto return1;
2016 if ((hi == 0) && (mi == 0) && (lo == 0))
2017 goto return1;
2018 } else if (type->flags == XML_SCHEMAS_NNINTEGER) {
2019 if ((sign == 1) &&
2020 ((hi != 0) || (mi != 0) || (lo != 0)))
2021 goto return1;
2022 }
2023 /*
2024 * We can store a value only if no overflow occured
2025 */
2026 if ((ret > 0) && (val != NULL)) {
2027 v = xmlSchemaNewValue(type->flags);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002028 if (v != NULL) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002029 v->value.decimal.lo = lo;
2030 v->value.decimal.mi = lo;
2031 v->value.decimal.hi = lo;
2032 v->value.decimal.sign = sign;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002033 v->value.decimal.frac = 0;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002034 v->value.decimal.total = cur - value;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002035 *val = v;
2036 }
2037 }
2038 goto return0;
2039 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002040 case XML_SCHEMAS_LONG:
2041 case XML_SCHEMAS_BYTE:
2042 case XML_SCHEMAS_SHORT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002043 case XML_SCHEMAS_INT: {
2044 const xmlChar *cur = value;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002045 unsigned long lo, mi, hi;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002046 int total = 0;
2047 int sign = 0;
2048 if (cur == NULL)
2049 goto return1;
2050 if (*cur == '-') {
2051 sign = 1;
2052 cur++;
2053 } else if (*cur == '+')
2054 cur++;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002055 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2056 if (ret <= 0)
2057 goto return1;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002058 if (*cur != 0)
2059 goto return1;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002060 if (type->flags == XML_SCHEMAS_LONG) {
2061 if (hi >= 922) {
2062 if (hi > 922)
2063 goto return1;
2064 if (mi >= 33720368) {
2065 if (mi > 33720368)
2066 goto return1;
2067 if ((sign == 0) && (lo > 54775807))
2068 goto return1;
2069 if ((sign == 1) && (lo > 54775808))
2070 goto return1;
2071 }
2072 }
2073 } else if (type->flags == XML_SCHEMAS_INT) {
2074 if (hi != 0)
2075 goto return1;
2076 if (mi >= 21) {
2077 if (mi > 21)
2078 goto return1;
2079 if ((sign == 0) && (lo > 47483647))
2080 goto return1;
2081 if ((sign == 1) && (lo > 47483648))
2082 goto return1;
2083 }
2084 } else if (type->flags == XML_SCHEMAS_SHORT) {
2085 if ((mi != 0) || (hi != 0))
2086 goto return1;
2087 if ((sign == 1) && (lo > 32768))
2088 goto return1;
2089 if ((sign == 0) && (lo > 32767))
2090 goto return1;
2091 } else if (type->flags == XML_SCHEMAS_BYTE) {
2092 if ((mi != 0) || (hi != 0))
2093 goto return1;
2094 if ((sign == 1) && (lo > 128))
2095 goto return1;
2096 if ((sign == 0) && (lo > 127))
2097 goto return1;
2098 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002099 if (val != NULL) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002100 v = xmlSchemaNewValue(type->flags);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002101 if (v != NULL) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002102 v->value.decimal.lo = lo;
2103 v->value.decimal.mi = lo;
2104 v->value.decimal.hi = lo;
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002105 v->value.decimal.sign = sign;
2106 v->value.decimal.frac = 0;
2107 v->value.decimal.total = total;
2108 *val = v;
2109 }
2110 }
2111 goto return0;
2112 }
2113 case XML_SCHEMAS_UINT:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002114 case XML_SCHEMAS_ULONG:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002115 case XML_SCHEMAS_USHORT:
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002116 case XML_SCHEMAS_UBYTE: {
2117 const xmlChar *cur = value;
2118 unsigned long lo, mi, hi;
2119 int total = 0;
2120 if (cur == NULL)
2121 goto return1;
2122 ret = xmlSchemaParseUInt(&cur, &lo, &mi, &hi);
2123 if (ret <= 0)
2124 goto return1;
2125 if (*cur != 0)
2126 goto return1;
2127 if (type->flags == XML_SCHEMAS_ULONG) {
2128 if (hi >= 1844) {
2129 if (hi > 1844)
2130 goto return1;
2131 if (mi >= 67440737) {
2132 if (mi > 67440737)
2133 goto return1;
2134 if (lo > 9551615)
2135 goto return1;
2136 }
2137 }
2138 } else if (type->flags == XML_SCHEMAS_UINT) {
2139 if (hi != 0)
2140 goto return1;
2141 if (mi >= 42) {
2142 if (mi > 42)
2143 goto return1;
2144 if (lo > 94967295)
2145 goto return1;
2146 }
2147 } else if (type->flags == XML_SCHEMAS_USHORT) {
2148 if ((mi != 0) || (hi != 0))
2149 goto return1;
2150 if (lo > 65535)
2151 goto return1;
2152 } else if (type->flags == XML_SCHEMAS_UBYTE) {
2153 if ((mi != 0) || (hi != 0))
2154 goto return1;
2155 if (lo > 255)
2156 goto return1;
2157 }
2158 if (val != NULL) {
2159 v = xmlSchemaNewValue(type->flags);
2160 if (v != NULL) {
2161 v->value.decimal.lo = lo;
2162 v->value.decimal.mi = mi;
2163 v->value.decimal.hi = hi;
2164 v->value.decimal.sign = 0;
2165 v->value.decimal.frac = 0;
2166 v->value.decimal.total = total;
2167 *val = v;
2168 }
2169 }
2170 goto return0;
2171 }
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002172 }
2173
2174done:
2175 if (norm != NULL) xmlFree(norm);
2176 return(ret);
2177return3:
2178 if (norm != NULL) xmlFree(norm);
2179 return(3);
2180return1:
2181 if (norm != NULL) xmlFree(norm);
2182 return(1);
2183return0:
2184 if (norm != NULL) xmlFree(norm);
2185 return(0);
2186error:
2187 if (norm != NULL) xmlFree(norm);
2188 return(-1);
2189}
2190
2191/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002192 * xmlSchemaValPredefTypeNode:
Daniel Veillard4255d502002-04-16 15:50:10 +00002193 * @type: the predefined type
2194 * @value: the value to check
2195 * @val: the return computed value
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002196 * @node: the node containing the value
Daniel Veillard4255d502002-04-16 15:50:10 +00002197 *
2198 * Check that a value conforms to the lexical space of the predefined type.
2199 * if true a value is computed and returned in @val.
2200 *
2201 * Returns 0 if this validates, a positive error code number otherwise
2202 * and -1 in case of internal or API error.
2203 */
2204int
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002205xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type, const xmlChar *value,
2206 xmlSchemaValPtr *val, xmlNodePtr node) {
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002207 return(xmlSchemaValAtomicType(type, value, val, node, 0));
Daniel Veillard4255d502002-04-16 15:50:10 +00002208}
2209
2210/**
Daniel Veillardc3da18a2003-03-18 00:31:04 +00002211 * xmlSchemaValidatePredefinedType:
2212 * @type: the predefined type
2213 * @value: the value to check
2214 * @val: the return computed value
2215 *
2216 * Check that a value conforms to the lexical space of the predefined type.
2217 * if true a value is computed and returned in @val.
2218 *
2219 * Returns 0 if this validates, a positive error code number otherwise
2220 * and -1 in case of internal or API error.
2221 */
2222int
2223xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type, const xmlChar *value,
2224 xmlSchemaValPtr *val) {
2225 return(xmlSchemaValPredefTypeNode(type, value, val, NULL));
2226}
2227
2228/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002229 * xmlSchemaCompareDecimals:
2230 * @x: a first decimal value
2231 * @y: a second decimal value
2232 *
2233 * Compare 2 decimals
2234 *
2235 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
2236 */
2237static int
2238xmlSchemaCompareDecimals(xmlSchemaValPtr x, xmlSchemaValPtr y)
2239{
2240 xmlSchemaValPtr swp;
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002241 int order = 1, p;
Daniel Veillard4255d502002-04-16 15:50:10 +00002242 unsigned long tmp;
2243
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002244 if ((x->value.decimal.sign) &&
2245 ((x->value.decimal.lo != 0) ||
2246 (x->value.decimal.mi != 0) ||
2247 (x->value.decimal.hi != 0))) {
2248 if ((y->value.decimal.sign) &&
2249 ((y->value.decimal.lo != 0) ||
2250 (y->value.decimal.mi != 0) ||
2251 (y->value.decimal.hi != 0)))
Daniel Veillard80b19092003-03-28 13:29:53 +00002252 order = -1;
2253 else
2254 return (-1);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002255 } else if ((y->value.decimal.sign) &&
2256 ((y->value.decimal.lo != 0) ||
2257 (y->value.decimal.mi != 0) ||
2258 (y->value.decimal.hi != 0))) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002259 return (1);
Daniel Veillard80b19092003-03-28 13:29:53 +00002260 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002261 if (x->value.decimal.frac == y->value.decimal.frac) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002262 if (x->value.decimal.hi < y->value.decimal.hi)
2263 return (-order);
2264 if (x->value.decimal.hi < y->value.decimal.hi)
2265 return (order);
2266 if (x->value.decimal.mi < y->value.decimal.mi)
2267 return (-order);
2268 if (x->value.decimal.mi < y->value.decimal.mi)
2269 return (order);
2270 if (x->value.decimal.lo < y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002271 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002272 if (x->value.decimal.lo > y->value.decimal.lo)
Daniel Veillard80b19092003-03-28 13:29:53 +00002273 return(order);
2274 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002275 }
2276 if (y->value.decimal.frac > x->value.decimal.frac) {
2277 swp = y;
2278 y = x;
2279 x = swp;
2280 order = -order;
2281 }
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002282 p = powten[x->value.decimal.frac - y->value.decimal.frac];
2283 tmp = x->value.decimal.lo / p;
2284 if (tmp > y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002285 return (order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002286 if (tmp < y->value.decimal.lo)
Daniel Veillard4255d502002-04-16 15:50:10 +00002287 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002288 tmp = y->value.decimal.lo * p;
2289 if (x->value.decimal.lo < tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002290 return (-order);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00002291 if (x->value.decimal.lo == tmp)
Daniel Veillard4255d502002-04-16 15:50:10 +00002292 return (0);
2293 return (order);
2294}
2295
2296/**
Daniel Veillard070803b2002-05-03 07:29:38 +00002297 * xmlSchemaCompareDurations:
2298 * @x: a first duration value
2299 * @y: a second duration value
2300 *
2301 * Compare 2 durations
2302 *
2303 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2304 * case of error
2305 */
2306static int
2307xmlSchemaCompareDurations(xmlSchemaValPtr x, xmlSchemaValPtr y)
2308{
2309 long carry, mon, day;
2310 double sec;
Daniel Veillard80b19092003-03-28 13:29:53 +00002311 int invert = 1;
2312 long xmon, xday, myear, minday, maxday;
Daniel Veillard070803b2002-05-03 07:29:38 +00002313 static const long dayRange [2][12] = {
2314 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
2315 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
2316
2317 if ((x == NULL) || (y == NULL))
Daniel Veillard5a872412002-05-22 06:40:27 +00002318 return -2;
Daniel Veillard070803b2002-05-03 07:29:38 +00002319
2320 /* months */
2321 mon = x->value.dur.mon - y->value.dur.mon;
2322
2323 /* seconds */
2324 sec = x->value.dur.sec - y->value.dur.sec;
2325 carry = (long)sec / SECS_PER_DAY;
2326 sec -= (double)(carry * SECS_PER_DAY);
2327
2328 /* days */
2329 day = x->value.dur.day - y->value.dur.day + carry;
2330
2331 /* easy test */
2332 if (mon == 0) {
2333 if (day == 0)
2334 if (sec == 0.0)
2335 return 0;
2336 else if (sec < 0.0)
2337 return -1;
2338 else
2339 return 1;
2340 else if (day < 0)
2341 return -1;
2342 else
2343 return 1;
2344 }
2345
2346 if (mon > 0) {
2347 if ((day >= 0) && (sec >= 0.0))
2348 return 1;
2349 else {
2350 xmon = mon;
2351 xday = -day;
2352 }
2353 } else if ((day <= 0) && (sec <= 0.0)) {
2354 return -1;
2355 } else {
Daniel Veillard80b19092003-03-28 13:29:53 +00002356 invert = -1;
Daniel Veillard070803b2002-05-03 07:29:38 +00002357 xmon = -mon;
2358 xday = day;
2359 }
2360
2361 myear = xmon / 12;
Daniel Veillard80b19092003-03-28 13:29:53 +00002362 if (myear == 0) {
2363 minday = 0;
2364 maxday = 0;
2365 } else {
2366 maxday = 366 * ((myear + 3) / 4) +
2367 365 * ((myear - 1) % 4);
2368 minday = maxday - 1;
2369 }
2370
Daniel Veillard070803b2002-05-03 07:29:38 +00002371 xmon = xmon % 12;
2372 minday += dayRange[0][xmon];
2373 maxday += dayRange[1][xmon];
2374
Daniel Veillard80b19092003-03-28 13:29:53 +00002375 if ((maxday == minday) && (maxday == xday))
2376 return(0); /* can this really happen ? */
Daniel Veillard070803b2002-05-03 07:29:38 +00002377 if (maxday < xday)
Daniel Veillard80b19092003-03-28 13:29:53 +00002378 return(-invert);
2379 if (minday > xday)
2380 return(invert);
Daniel Veillard070803b2002-05-03 07:29:38 +00002381
2382 /* indeterminate */
Daniel Veillard5a872412002-05-22 06:40:27 +00002383 return 2;
2384}
2385
2386/*
2387 * macros for adding date/times and durations
2388 */
2389#define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
2390#define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
2391#define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
2392#define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
2393
2394/**
2395 * _xmlSchemaDateAdd:
2396 * @dt: an #xmlSchemaValPtr
2397 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
2398 *
2399 * Compute a new date/time from @dt and @dur. This function assumes @dt
2400 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
2401 * or #XML_SCHEMAS_GYEAR.
2402 *
2403 * Returns date/time pointer or NULL.
2404 */
2405static xmlSchemaValPtr
2406_xmlSchemaDateAdd (xmlSchemaValPtr dt, xmlSchemaValPtr dur)
2407{
2408 xmlSchemaValPtr ret;
2409 long carry, tempdays, temp;
2410 xmlSchemaValDatePtr r, d;
2411 xmlSchemaValDurationPtr u;
2412
2413 if ((dt == NULL) || (dur == NULL))
2414 return NULL;
2415
2416 ret = xmlSchemaNewValue(dt->type);
2417 if (ret == NULL)
2418 return NULL;
2419
2420 r = &(ret->value.date);
2421 d = &(dt->value.date);
2422 u = &(dur->value.dur);
2423
2424 /* normalization */
2425 if (d->mon == 0)
2426 d->mon = 1;
2427
2428 /* normalize for time zone offset */
2429 u->sec -= (d->tzo * 60);
2430 d->tzo = 0;
2431
2432 /* normalization */
2433 if (d->day == 0)
2434 d->day = 1;
2435
2436 /* month */
2437 carry = d->mon + u->mon;
2438 r->mon = MODULO_RANGE(carry, 1, 13);
2439 carry = FQUOTIENT_RANGE(carry, 1, 13);
2440
2441 /* year (may be modified later) */
2442 r->year = d->year + carry;
2443 if (r->year == 0) {
2444 if (d->year > 0)
2445 r->year--;
2446 else
2447 r->year++;
2448 }
2449
2450 /* time zone */
2451 r->tzo = d->tzo;
2452 r->tz_flag = d->tz_flag;
2453
2454 /* seconds */
2455 r->sec = d->sec + u->sec;
2456 carry = FQUOTIENT((long)r->sec, 60);
2457 if (r->sec != 0.0) {
2458 r->sec = MODULO(r->sec, 60.0);
2459 }
2460
2461 /* minute */
2462 carry += d->min;
2463 r->min = MODULO(carry, 60);
2464 carry = FQUOTIENT(carry, 60);
2465
2466 /* hours */
2467 carry += d->hour;
2468 r->hour = MODULO(carry, 24);
2469 carry = FQUOTIENT(carry, 24);
2470
2471 /*
2472 * days
2473 * Note we use tempdays because the temporary values may need more
2474 * than 5 bits
2475 */
2476 if ((VALID_YEAR(r->year)) && (VALID_MONTH(r->mon)) &&
2477 (d->day > MAX_DAYINMONTH(r->year, r->mon)))
2478 tempdays = MAX_DAYINMONTH(r->year, r->mon);
2479 else if (d->day < 1)
2480 tempdays = 1;
2481 else
2482 tempdays = d->day;
2483
2484 tempdays += u->day + carry;
2485
2486 while (1) {
2487 if (tempdays < 1) {
2488 long tmon = MODULO_RANGE(r->mon-1, 1, 13);
2489 long tyr = r->year + FQUOTIENT_RANGE(r->mon-1, 1, 13);
2490 if (tyr == 0)
2491 tyr--;
2492 tempdays += MAX_DAYINMONTH(tyr, tmon);
2493 carry = -1;
2494 } else if (tempdays > MAX_DAYINMONTH(r->year, r->mon)) {
2495 tempdays = tempdays - MAX_DAYINMONTH(r->year, r->mon);
2496 carry = 1;
2497 } else
2498 break;
2499
2500 temp = r->mon + carry;
2501 r->mon = MODULO_RANGE(temp, 1, 13);
2502 r->year = r->year + FQUOTIENT_RANGE(temp, 1, 13);
2503 if (r->year == 0) {
2504 if (temp < 1)
2505 r->year--;
2506 else
2507 r->year++;
2508 }
2509 }
2510
2511 r->day = tempdays;
2512
2513 /*
2514 * adjust the date/time type to the date values
2515 */
2516 if (ret->type != XML_SCHEMAS_DATETIME) {
2517 if ((r->hour) || (r->min) || (r->sec))
2518 ret->type = XML_SCHEMAS_DATETIME;
2519 else if (ret->type != XML_SCHEMAS_DATE) {
2520 if ((r->mon != 1) && (r->day != 1))
2521 ret->type = XML_SCHEMAS_DATE;
2522 else if ((ret->type != XML_SCHEMAS_GYEARMONTH) && (r->mon != 1))
2523 ret->type = XML_SCHEMAS_GYEARMONTH;
2524 }
2525 }
2526
2527 return ret;
2528}
2529
2530/**
2531 * xmlSchemaDupVal:
2532 * @v: value to duplicate
2533 *
2534 * returns a duplicated value.
2535 */
2536static xmlSchemaValPtr
2537xmlSchemaDupVal (xmlSchemaValPtr v)
2538{
2539 xmlSchemaValPtr ret = xmlSchemaNewValue(v->type);
2540 if (ret == NULL)
2541 return ret;
2542
2543 memcpy(ret, v, sizeof(xmlSchemaVal));
2544 return ret;
2545}
2546
2547/**
2548 * xmlSchemaDateNormalize:
2549 * @dt: an #xmlSchemaValPtr
2550 *
2551 * Normalize @dt to GMT time.
2552 *
2553 */
2554static xmlSchemaValPtr
2555xmlSchemaDateNormalize (xmlSchemaValPtr dt, double offset)
2556{
2557 xmlSchemaValPtr dur, ret;
2558
2559 if (dt == NULL)
2560 return NULL;
2561
2562 if (((dt->type != XML_SCHEMAS_TIME) &&
2563 (dt->type != XML_SCHEMAS_DATETIME)) || (dt->value.date.tzo == 0))
2564 return xmlSchemaDupVal(dt);
2565
2566 dur = xmlSchemaNewValue(XML_SCHEMAS_DURATION);
2567 if (dur == NULL)
2568 return NULL;
2569
2570 dur->value.date.sec -= offset;
2571
2572 ret = _xmlSchemaDateAdd(dt, dur);
2573 if (ret == NULL)
2574 return NULL;
2575
2576 xmlSchemaFreeValue(dur);
2577
2578 /* ret->value.date.tzo = 0; */
2579 return ret;
2580}
2581
2582/**
2583 * _xmlSchemaDateCastYMToDays:
2584 * @dt: an #xmlSchemaValPtr
2585 *
2586 * Convert mon and year of @dt to total number of days. Take the
2587 * number of years since (or before) 1 AD and add the number of leap
2588 * years. This is a function because negative
2589 * years must be handled a little differently and there is no zero year.
2590 *
2591 * Returns number of days.
2592 */
2593static long
2594_xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt)
2595{
2596 long ret;
2597
2598 if (dt->value.date.year < 0)
2599 ret = (dt->value.date.year * 365) +
2600 (((dt->value.date.year+1)/4)-((dt->value.date.year+1)/100)+
2601 ((dt->value.date.year+1)/400)) +
2602 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2603 else
2604 ret = ((dt->value.date.year-1) * 365) +
2605 (((dt->value.date.year-1)/4)-((dt->value.date.year-1)/100)+
2606 ((dt->value.date.year-1)/400)) +
2607 DAY_IN_YEAR(0, dt->value.date.mon, dt->value.date.year);
2608
2609 return ret;
2610}
2611
2612/**
2613 * TIME_TO_NUMBER:
2614 * @dt: an #xmlSchemaValPtr
2615 *
2616 * Calculates the number of seconds in the time portion of @dt.
2617 *
2618 * Returns seconds.
2619 */
2620#define TIME_TO_NUMBER(dt) \
2621 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
Daniel Veillardb3721c22003-03-31 11:22:25 +00002622 (dt->value.date.min * SECS_PER_MIN) + \
2623 (dt->value.date.tzo * SECS_PER_MIN)) + \
2624 dt->value.date.sec)
Daniel Veillard5a872412002-05-22 06:40:27 +00002625
2626/**
2627 * xmlSchemaCompareDates:
2628 * @x: a first date/time value
2629 * @y: a second date/time value
2630 *
2631 * Compare 2 date/times
2632 *
2633 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2634 * case of error
2635 */
2636static int
2637xmlSchemaCompareDates (xmlSchemaValPtr x, xmlSchemaValPtr y)
2638{
2639 unsigned char xmask, ymask, xor_mask, and_mask;
2640 xmlSchemaValPtr p1, p2, q1, q2;
2641 long p1d, p2d, q1d, q2d;
2642
2643 if ((x == NULL) || (y == NULL))
2644 return -2;
2645
2646 if (x->value.date.tz_flag) {
2647
2648 if (!y->value.date.tz_flag) {
2649 p1 = xmlSchemaDateNormalize(x, 0);
2650 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2651 /* normalize y + 14:00 */
2652 q1 = xmlSchemaDateNormalize(y, (14 * SECS_PER_HOUR));
2653
2654 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002655 if (p1d < q1d) {
2656 xmlSchemaFreeValue(p1);
2657 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002658 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002659 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002660 double sec;
2661
2662 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002663 if (sec < 0.0) {
2664 xmlSchemaFreeValue(p1);
2665 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002666 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002667 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00002668 /* normalize y - 14:00 */
2669 q2 = xmlSchemaDateNormalize(y, -(14 * SECS_PER_HOUR));
2670 q2d = _xmlSchemaDateCastYMToDays(q2) + q2->value.date.day;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002671 xmlSchemaFreeValue(p1);
2672 xmlSchemaFreeValue(q1);
2673 xmlSchemaFreeValue(q2);
Daniel Veillard5a872412002-05-22 06:40:27 +00002674 if (p1d > q2d)
2675 return 1;
2676 else if (p1d == q2d) {
2677 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q2);
2678 if (sec > 0.0)
2679 return 1;
2680 else
2681 return 2; /* indeterminate */
2682 }
2683 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002684 } else {
2685 xmlSchemaFreeValue(p1);
2686 xmlSchemaFreeValue(q1);
2687 }
Daniel Veillard5a872412002-05-22 06:40:27 +00002688 }
2689 } else if (y->value.date.tz_flag) {
2690 q1 = xmlSchemaDateNormalize(y, 0);
2691 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2692
2693 /* normalize x - 14:00 */
2694 p1 = xmlSchemaDateNormalize(x, -(14 * SECS_PER_HOUR));
2695 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2696
Daniel Veillardfdc91562002-07-01 21:52:03 +00002697 if (p1d < q1d) {
2698 xmlSchemaFreeValue(p1);
2699 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002700 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002701 } else if (p1d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002702 double sec;
2703
2704 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002705 if (sec < 0.0) {
2706 xmlSchemaFreeValue(p1);
2707 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002708 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002709 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00002710 /* normalize x + 14:00 */
2711 p2 = xmlSchemaDateNormalize(x, (14 * SECS_PER_HOUR));
2712 p2d = _xmlSchemaDateCastYMToDays(p2) + p2->value.date.day;
2713
Daniel Veillard6560a422003-03-27 21:25:38 +00002714 if (p2d > q1d) {
2715 xmlSchemaFreeValue(p1);
2716 xmlSchemaFreeValue(q1);
2717 xmlSchemaFreeValue(p2);
Daniel Veillard5a872412002-05-22 06:40:27 +00002718 return 1;
Daniel Veillard6560a422003-03-27 21:25:38 +00002719 } else if (p2d == q1d) {
Daniel Veillard5a872412002-05-22 06:40:27 +00002720 sec = TIME_TO_NUMBER(p2) - TIME_TO_NUMBER(q1);
Daniel Veillard6560a422003-03-27 21:25:38 +00002721 xmlSchemaFreeValue(p1);
2722 xmlSchemaFreeValue(q1);
2723 xmlSchemaFreeValue(p2);
Daniel Veillard5a872412002-05-22 06:40:27 +00002724 if (sec > 0.0)
2725 return 1;
2726 else
2727 return 2; /* indeterminate */
2728 }
Daniel Veillard6560a422003-03-27 21:25:38 +00002729 xmlSchemaFreeValue(p1);
2730 xmlSchemaFreeValue(q1);
2731 xmlSchemaFreeValue(p2);
Daniel Veillard5a872412002-05-22 06:40:27 +00002732 }
Daniel Veillardfdc91562002-07-01 21:52:03 +00002733 } else {
2734 xmlSchemaFreeValue(p1);
2735 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002736 }
2737 }
2738
2739 /*
2740 * if the same type then calculate the difference
2741 */
2742 if (x->type == y->type) {
2743 q1 = xmlSchemaDateNormalize(y, 0);
2744 q1d = _xmlSchemaDateCastYMToDays(q1) + q1->value.date.day;
2745
2746 p1 = xmlSchemaDateNormalize(x, 0);
2747 p1d = _xmlSchemaDateCastYMToDays(p1) + p1->value.date.day;
2748
Daniel Veillardfdc91562002-07-01 21:52:03 +00002749 if (p1d < q1d) {
2750 xmlSchemaFreeValue(p1);
2751 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002752 return -1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002753 } else if (p1d > q1d) {
2754 xmlSchemaFreeValue(p1);
2755 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002756 return 1;
Daniel Veillardfdc91562002-07-01 21:52:03 +00002757 } else {
Daniel Veillard5a872412002-05-22 06:40:27 +00002758 double sec;
2759
2760 sec = TIME_TO_NUMBER(p1) - TIME_TO_NUMBER(q1);
Daniel Veillardfdc91562002-07-01 21:52:03 +00002761 xmlSchemaFreeValue(p1);
2762 xmlSchemaFreeValue(q1);
Daniel Veillard5a872412002-05-22 06:40:27 +00002763 if (sec < 0.0)
2764 return -1;
2765 else if (sec > 0.0)
2766 return 1;
2767
2768 }
2769 return 0;
2770 }
2771
2772 switch (x->type) {
2773 case XML_SCHEMAS_DATETIME:
2774 xmask = 0xf;
2775 break;
2776 case XML_SCHEMAS_DATE:
2777 xmask = 0x7;
2778 break;
2779 case XML_SCHEMAS_GYEAR:
2780 xmask = 0x1;
2781 break;
2782 case XML_SCHEMAS_GMONTH:
2783 xmask = 0x2;
2784 break;
2785 case XML_SCHEMAS_GDAY:
2786 xmask = 0x3;
2787 break;
2788 case XML_SCHEMAS_GYEARMONTH:
2789 xmask = 0x3;
2790 break;
2791 case XML_SCHEMAS_GMONTHDAY:
2792 xmask = 0x6;
2793 break;
2794 case XML_SCHEMAS_TIME:
2795 xmask = 0x8;
2796 break;
2797 default:
2798 xmask = 0;
2799 break;
2800 }
2801
2802 switch (y->type) {
2803 case XML_SCHEMAS_DATETIME:
2804 ymask = 0xf;
2805 break;
2806 case XML_SCHEMAS_DATE:
2807 ymask = 0x7;
2808 break;
2809 case XML_SCHEMAS_GYEAR:
2810 ymask = 0x1;
2811 break;
2812 case XML_SCHEMAS_GMONTH:
2813 ymask = 0x2;
2814 break;
2815 case XML_SCHEMAS_GDAY:
2816 ymask = 0x3;
2817 break;
2818 case XML_SCHEMAS_GYEARMONTH:
2819 ymask = 0x3;
2820 break;
2821 case XML_SCHEMAS_GMONTHDAY:
2822 ymask = 0x6;
2823 break;
2824 case XML_SCHEMAS_TIME:
2825 ymask = 0x8;
2826 break;
2827 default:
2828 ymask = 0;
2829 break;
2830 }
2831
2832 xor_mask = xmask ^ ymask; /* mark type differences */
2833 and_mask = xmask & ymask; /* mark field specification */
2834
2835 /* year */
2836 if (xor_mask & 1)
2837 return 2; /* indeterminate */
2838 else if (and_mask & 1) {
2839 if (x->value.date.year < y->value.date.year)
2840 return -1;
2841 else if (x->value.date.year > y->value.date.year)
2842 return 1;
2843 }
2844
2845 /* month */
2846 if (xor_mask & 2)
2847 return 2; /* indeterminate */
2848 else if (and_mask & 2) {
2849 if (x->value.date.mon < y->value.date.mon)
2850 return -1;
2851 else if (x->value.date.mon > y->value.date.mon)
2852 return 1;
2853 }
2854
2855 /* day */
2856 if (xor_mask & 4)
2857 return 2; /* indeterminate */
2858 else if (and_mask & 4) {
2859 if (x->value.date.day < y->value.date.day)
2860 return -1;
2861 else if (x->value.date.day > y->value.date.day)
2862 return 1;
2863 }
2864
2865 /* time */
2866 if (xor_mask & 8)
2867 return 2; /* indeterminate */
2868 else if (and_mask & 8) {
2869 if (x->value.date.hour < y->value.date.hour)
2870 return -1;
2871 else if (x->value.date.hour > y->value.date.hour)
2872 return 1;
2873 else if (x->value.date.min < y->value.date.min)
2874 return -1;
2875 else if (x->value.date.min > y->value.date.min)
2876 return 1;
2877 else if (x->value.date.sec < y->value.date.sec)
2878 return -1;
2879 else if (x->value.date.sec > y->value.date.sec)
2880 return 1;
2881 }
2882
Daniel Veillard070803b2002-05-03 07:29:38 +00002883 return 0;
2884}
2885
2886/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00002887 * xmlSchemaCompareNormStrings:
2888 * @x: a first string value
2889 * @y: a second string value
2890 *
2891 * Compare 2 string for their normalized values.
2892 *
2893 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
2894 * case of error
2895 */
2896static int
2897xmlSchemaCompareNormStrings(xmlSchemaValPtr x, xmlSchemaValPtr y) {
2898 const xmlChar *utf1;
2899 const xmlChar *utf2;
2900 int tmp;
2901
2902 if ((x == NULL) || (y == NULL))
2903 return(-2);
2904 utf1 = x->value.str;
2905 utf2 = y->value.str;
2906
2907 while (IS_BLANK(*utf1)) utf1++;
2908 while (IS_BLANK(*utf2)) utf2++;
2909 while ((*utf1 != 0) && (*utf2 != 0)) {
2910 if (IS_BLANK(*utf1)) {
2911 if (!IS_BLANK(*utf2)) {
2912 tmp = *utf1 - *utf2;
2913 return(tmp);
2914 }
2915 while (IS_BLANK(*utf1)) utf1++;
2916 while (IS_BLANK(*utf2)) utf2++;
2917 } else {
2918 tmp = *utf1++ - *utf2++;
2919 if (tmp < 0)
2920 return(-1);
2921 if (tmp > 0)
2922 return(1);
2923 }
2924 }
2925 if (*utf1 != 0) {
2926 while (IS_BLANK(*utf1)) utf1++;
2927 if (*utf1 != 0)
2928 return(1);
2929 }
2930 if (*utf2 != 0) {
2931 while (IS_BLANK(*utf2)) utf2++;
2932 if (*utf2 != 0)
2933 return(-1);
2934 }
2935 return(0);
2936}
2937
2938/**
Daniel Veillardb6c7f412003-03-29 16:41:55 +00002939 * xmlSchemaCompareFloats:
2940 * @x: a first float or double value
2941 * @y: a second float or double value
2942 *
2943 * Compare 2 values
2944 *
2945 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
2946 * case of error
2947 */
2948static int
2949xmlSchemaCompareFloats(xmlSchemaValPtr x, xmlSchemaValPtr y) {
2950 double d1, d2;
2951
2952 if ((x == NULL) || (y == NULL))
2953 return(-2);
2954
2955 /*
2956 * Cast everything to doubles.
2957 */
2958 if (x->type == XML_SCHEMAS_DOUBLE)
2959 d1 = x->value.d;
2960 else if (x->type == XML_SCHEMAS_FLOAT)
2961 d1 = x->value.f;
2962 else
2963 return(-2);
2964
2965 if (y->type == XML_SCHEMAS_DOUBLE)
2966 d2 = y->value.d;
2967 else if (y->type == XML_SCHEMAS_FLOAT)
2968 d2 = y->value.f;
2969 else
2970 return(-2);
2971
2972 /*
2973 * Check for special cases.
2974 */
2975 if (xmlXPathIsNaN(d1)) {
2976 if (xmlXPathIsNaN(d2))
2977 return(0);
2978 return(1);
2979 }
2980 if (xmlXPathIsNaN(d2))
2981 return(-1);
2982 if (d1 == xmlXPathPINF) {
2983 if (d2 == xmlXPathPINF)
2984 return(0);
2985 return(1);
2986 }
2987 if (d2 == xmlXPathPINF)
2988 return(-1);
2989 if (d1 == xmlXPathNINF) {
2990 if (d2 == xmlXPathNINF)
2991 return(0);
2992 return(-1);
2993 }
2994 if (d2 == xmlXPathNINF)
2995 return(1);
2996
2997 /*
2998 * basic tests, the last one we should have equality, but
2999 * portability is more important than speed and handling
3000 * NaN or Inf in a portable way is always a challenge, so ...
3001 */
3002 if (d1 < d2)
3003 return(-1);
3004 if (d1 > d2)
3005 return(1);
3006 if (d1 == d2)
3007 return(0);
3008 return(2);
3009}
3010
3011/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003012 * xmlSchemaCompareValues:
3013 * @x: a first value
3014 * @y: a second value
3015 *
3016 * Compare 2 values
3017 *
Daniel Veillard5a872412002-05-22 06:40:27 +00003018 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3019 * case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003020 */
Daniel Veillard80b19092003-03-28 13:29:53 +00003021int
Daniel Veillard4255d502002-04-16 15:50:10 +00003022xmlSchemaCompareValues(xmlSchemaValPtr x, xmlSchemaValPtr y) {
3023 if ((x == NULL) || (y == NULL))
3024 return(-2);
3025
3026 switch (x->type) {
Daniel Veillard80b19092003-03-28 13:29:53 +00003027 case XML_SCHEMAS_UNKNOWN:
3028 return(-2);
3029 case XML_SCHEMAS_INTEGER:
3030 case XML_SCHEMAS_NPINTEGER:
3031 case XML_SCHEMAS_NINTEGER:
3032 case XML_SCHEMAS_NNINTEGER:
3033 case XML_SCHEMAS_PINTEGER:
3034 case XML_SCHEMAS_INT:
3035 case XML_SCHEMAS_UINT:
3036 case XML_SCHEMAS_LONG:
3037 case XML_SCHEMAS_ULONG:
3038 case XML_SCHEMAS_SHORT:
3039 case XML_SCHEMAS_USHORT:
3040 case XML_SCHEMAS_BYTE:
3041 case XML_SCHEMAS_UBYTE:
Daniel Veillard4255d502002-04-16 15:50:10 +00003042 case XML_SCHEMAS_DECIMAL:
Daniel Veillard80b19092003-03-28 13:29:53 +00003043 if (y->type == x->type)
3044 return(xmlSchemaCompareDecimals(x, y));
3045 if ((y->type == XML_SCHEMAS_DECIMAL) ||
3046 (y->type == XML_SCHEMAS_INTEGER) ||
3047 (y->type == XML_SCHEMAS_NPINTEGER) ||
3048 (y->type == XML_SCHEMAS_NINTEGER) ||
3049 (y->type == XML_SCHEMAS_NNINTEGER) ||
3050 (y->type == XML_SCHEMAS_PINTEGER) ||
3051 (y->type == XML_SCHEMAS_INT) ||
3052 (y->type == XML_SCHEMAS_UINT) ||
3053 (y->type == XML_SCHEMAS_LONG) ||
3054 (y->type == XML_SCHEMAS_ULONG) ||
3055 (y->type == XML_SCHEMAS_SHORT) ||
3056 (y->type == XML_SCHEMAS_USHORT) ||
3057 (y->type == XML_SCHEMAS_BYTE) ||
3058 (y->type == XML_SCHEMAS_UBYTE))
Daniel Veillard4255d502002-04-16 15:50:10 +00003059 return(xmlSchemaCompareDecimals(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003060 return(-2);
Daniel Veillard070803b2002-05-03 07:29:38 +00003061 case XML_SCHEMAS_DURATION:
3062 if (y->type == XML_SCHEMAS_DURATION)
3063 return(xmlSchemaCompareDurations(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003064 return(-2);
3065 case XML_SCHEMAS_TIME:
3066 case XML_SCHEMAS_GDAY:
3067 case XML_SCHEMAS_GMONTH:
3068 case XML_SCHEMAS_GMONTHDAY:
3069 case XML_SCHEMAS_GYEAR:
3070 case XML_SCHEMAS_GYEARMONTH:
3071 case XML_SCHEMAS_DATE:
3072 case XML_SCHEMAS_DATETIME:
3073 if ((y->type == XML_SCHEMAS_DATETIME) ||
3074 (y->type == XML_SCHEMAS_TIME) ||
3075 (y->type == XML_SCHEMAS_GDAY) ||
3076 (y->type == XML_SCHEMAS_GMONTH) ||
3077 (y->type == XML_SCHEMAS_GMONTHDAY) ||
3078 (y->type == XML_SCHEMAS_GYEAR) ||
3079 (y->type == XML_SCHEMAS_DATE) ||
3080 (y->type == XML_SCHEMAS_GYEARMONTH))
3081 return (xmlSchemaCompareDates(x, y));
Daniel Veillard5a872412002-05-22 06:40:27 +00003082 return (-2);
Daniel Veillard80b19092003-03-28 13:29:53 +00003083 case XML_SCHEMAS_NORMSTRING:
Daniel Veillard80b19092003-03-28 13:29:53 +00003084 case XML_SCHEMAS_TOKEN:
3085 case XML_SCHEMAS_LANGUAGE:
3086 case XML_SCHEMAS_NMTOKEN:
Daniel Veillard80b19092003-03-28 13:29:53 +00003087 case XML_SCHEMAS_NAME:
Daniel Veillard80b19092003-03-28 13:29:53 +00003088 case XML_SCHEMAS_NCNAME:
3089 case XML_SCHEMAS_ID:
3090 case XML_SCHEMAS_IDREF:
Daniel Veillard80b19092003-03-28 13:29:53 +00003091 case XML_SCHEMAS_ENTITY:
Daniel Veillard80b19092003-03-28 13:29:53 +00003092 case XML_SCHEMAS_NOTATION:
3093 case XML_SCHEMAS_ANYURI:
Daniel Veillardc4c21552003-03-29 10:53:38 +00003094 if ((y->type == XML_SCHEMAS_NORMSTRING) ||
3095 (y->type == XML_SCHEMAS_TOKEN) ||
3096 (y->type == XML_SCHEMAS_LANGUAGE) ||
3097 (y->type == XML_SCHEMAS_NMTOKEN) ||
3098 (y->type == XML_SCHEMAS_NAME) ||
3099 (y->type == XML_SCHEMAS_QNAME) ||
3100 (y->type == XML_SCHEMAS_NCNAME) ||
3101 (y->type == XML_SCHEMAS_ID) ||
3102 (y->type == XML_SCHEMAS_IDREF) ||
3103 (y->type == XML_SCHEMAS_ENTITY) ||
3104 (y->type == XML_SCHEMAS_NOTATION) ||
3105 (y->type == XML_SCHEMAS_ANYURI))
3106 return (xmlSchemaCompareNormStrings(x, y));
3107 return (-2);
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003108 case XML_SCHEMAS_QNAME:
3109 if (y->type == XML_SCHEMAS_QNAME) {
3110 if ((xmlStrEqual(x->value.qname.name, y->value.qname.name)) &&
3111 (xmlStrEqual(x->value.qname.uri, y->value.qname.uri)))
3112 return(0);
3113 return(2);
3114 }
3115 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003116 case XML_SCHEMAS_FLOAT:
3117 case XML_SCHEMAS_DOUBLE:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003118 if ((y->type == XML_SCHEMAS_FLOAT) ||
3119 (y->type == XML_SCHEMAS_DOUBLE))
3120 return (xmlSchemaCompareFloats(x, y));
3121 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003122 case XML_SCHEMAS_BOOLEAN:
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003123 if (y->type == XML_SCHEMAS_BOOLEAN) {
3124 if (x->value.b == y->value.b)
3125 return(0);
3126 if (x->value.b == 0)
3127 return(-1);
3128 return(1);
3129 }
3130 return (-2);
Daniel Veillard560c2a42003-07-06 21:13:49 +00003131 case XML_SCHEMAS_HEXBINARY:
3132 if (y->type == XML_SCHEMAS_HEXBINARY)
3133 return (xmlSchemaCompareDecimals(x, y));
3134 return (-2);
Daniel Veillardc4c21552003-03-29 10:53:38 +00003135 case XML_SCHEMAS_STRING:
3136 case XML_SCHEMAS_IDREFS:
3137 case XML_SCHEMAS_ENTITIES:
3138 case XML_SCHEMAS_NMTOKENS:
3139 TODO
3140 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003141 }
Daniel Veillard5a872412002-05-22 06:40:27 +00003142 return -2;
Daniel Veillard4255d502002-04-16 15:50:10 +00003143}
3144
3145/**
Daniel Veillardc4c21552003-03-29 10:53:38 +00003146 * xmlSchemaNormLen:
3147 * @value: a string
3148 *
3149 * Computes the UTF8 length of the normalized value of the string
3150 *
3151 * Returns the length or -1 in case of error.
3152 */
3153static int
3154xmlSchemaNormLen(const xmlChar *value) {
3155 const xmlChar *utf;
3156 int ret = 0;
3157
3158 if (value == NULL)
3159 return(-1);
3160 utf = value;
3161 while (IS_BLANK(*utf)) utf++;
3162 while (*utf != 0) {
3163 if (utf[0] & 0x80) {
3164 if ((utf[1] & 0xc0) != 0x80)
3165 return(-1);
3166 if ((utf[0] & 0xe0) == 0xe0) {
3167 if ((utf[2] & 0xc0) != 0x80)
3168 return(-1);
3169 if ((utf[0] & 0xf0) == 0xf0) {
3170 if ((utf[0] & 0xf8) != 0xf0 || (utf[3] & 0xc0) != 0x80)
3171 return(-1);
3172 utf += 4;
3173 } else {
3174 utf += 3;
3175 }
3176 } else {
3177 utf += 2;
3178 }
3179 } else if (IS_BLANK(*utf)) {
3180 while (IS_BLANK(*utf)) utf++;
3181 if (*utf == 0)
3182 break;
3183 } else {
3184 utf++;
3185 }
3186 ret++;
3187 }
3188 return(ret);
3189}
3190
3191/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003192 * xmlSchemaValidateFacet:
Daniel Veillard01c13b52002-12-10 15:19:08 +00003193 * @base: the base type
Daniel Veillard4255d502002-04-16 15:50:10 +00003194 * @facet: the facet to check
3195 * @value: the lexical repr of the value to validate
3196 * @val: the precomputed value
3197 *
3198 * Check a value against a facet condition
3199 *
3200 * Returns 0 if the element is schemas valid, a positive error code
3201 * number otherwise and -1 in case of internal or API error.
3202 */
3203int
Daniel Veillarddda8f1b2002-09-26 09:47:36 +00003204xmlSchemaValidateFacet(xmlSchemaTypePtr base ATTRIBUTE_UNUSED,
Daniel Veillard118aed72002-09-24 14:13:13 +00003205 xmlSchemaFacetPtr facet,
Daniel Veillard4255d502002-04-16 15:50:10 +00003206 const xmlChar *value, xmlSchemaValPtr val)
3207{
3208 int ret;
3209
3210 switch (facet->type) {
3211 case XML_SCHEMA_FACET_PATTERN:
3212 ret = xmlRegexpExec(facet->regexp, value);
3213 if (ret == 1)
3214 return(0);
3215 if (ret == 0) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003216 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003217 return(1);
3218 }
3219 return(ret);
3220 case XML_SCHEMA_FACET_MAXEXCLUSIVE:
3221 ret = xmlSchemaCompareValues(val, facet->val);
3222 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003223 /* TODO error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003224 return(-1);
3225 }
3226 if (ret == -1)
3227 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003228 /* error code */
Daniel Veillard4255d502002-04-16 15:50:10 +00003229 return(1);
Daniel Veillard070803b2002-05-03 07:29:38 +00003230 case XML_SCHEMA_FACET_MAXINCLUSIVE:
3231 ret = xmlSchemaCompareValues(val, facet->val);
3232 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003233 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003234 return(-1);
3235 }
3236 if ((ret == -1) || (ret == 0))
3237 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003238 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003239 return(1);
3240 case XML_SCHEMA_FACET_MINEXCLUSIVE:
3241 ret = xmlSchemaCompareValues(val, facet->val);
3242 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003243 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003244 return(-1);
3245 }
3246 if (ret == 1)
3247 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003248 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003249 return(1);
3250 case XML_SCHEMA_FACET_MININCLUSIVE:
3251 ret = xmlSchemaCompareValues(val, facet->val);
3252 if (ret == -2) {
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003253 /* TODO error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003254 return(-1);
3255 }
3256 if ((ret == 1) || (ret == 0))
3257 return(0);
Daniel Veillard5a872412002-05-22 06:40:27 +00003258 /* error code */
Daniel Veillard070803b2002-05-03 07:29:38 +00003259 return(1);
Daniel Veillard8651f532002-04-17 09:06:27 +00003260 case XML_SCHEMA_FACET_WHITESPACE:
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003261 /* TODO whitespaces */
Daniel Veillard8651f532002-04-17 09:06:27 +00003262 return(0);
Daniel Veillard88c58912002-04-23 07:12:20 +00003263 case XML_SCHEMA_FACET_ENUMERATION:
3264 if ((facet->value != NULL) &&
3265 (xmlStrEqual(facet->value, value)))
3266 return(0);
3267 return(1);
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003268 case XML_SCHEMA_FACET_LENGTH:
3269 case XML_SCHEMA_FACET_MAXLENGTH:
3270 case XML_SCHEMA_FACET_MINLENGTH: {
3271 unsigned int len = 0;
3272
3273 if ((facet->val == NULL) ||
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003274 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3275 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003276 (facet->val->value.decimal.frac != 0)) {
3277 return(-1);
3278 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003279 if ((val != NULL) && (val->type == XML_SCHEMAS_HEXBINARY))
3280 len = val->value.decimal.total;
3281 else {
3282 switch (base->flags) {
3283 case XML_SCHEMAS_IDREF:
3284 case XML_SCHEMAS_NORMSTRING:
3285 case XML_SCHEMAS_TOKEN:
3286 case XML_SCHEMAS_LANGUAGE:
3287 case XML_SCHEMAS_NMTOKEN:
3288 case XML_SCHEMAS_NAME:
3289 case XML_SCHEMAS_NCNAME:
3290 case XML_SCHEMAS_ID:
3291 len = xmlSchemaNormLen(value);
3292 break;
3293 case XML_SCHEMAS_STRING:
3294 len = xmlUTF8Strlen(value);
3295 break;
3296 default:
3297 TODO
3298 }
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003299 }
3300 if (facet->type == XML_SCHEMA_FACET_LENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003301 if (len != facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003302 return(1);
3303 } else if (facet->type == XML_SCHEMA_FACET_MINLENGTH) {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003304 if (len < facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003305 return(1);
3306 } else {
Daniel Veillarde637c4a2003-03-30 21:10:09 +00003307 if (len > facet->val->value.decimal.lo)
Daniel Veillard8bc6cf92003-02-27 17:42:22 +00003308 return(1);
3309 }
3310 break;
3311 }
Daniel Veillard560c2a42003-07-06 21:13:49 +00003312 case XML_SCHEMA_FACET_TOTALDIGITS:
3313 case XML_SCHEMA_FACET_FRACTIONDIGITS:
3314
3315 if ((facet->val == NULL) ||
3316 ((facet->val->type != XML_SCHEMAS_DECIMAL) &&
3317 (facet->val->type != XML_SCHEMAS_NNINTEGER)) ||
3318 (facet->val->value.decimal.frac != 0)) {
3319 return(-1);
3320 }
3321 if ((val == NULL) ||
3322 ((val->type != XML_SCHEMAS_DECIMAL) &&
3323 (val->type != XML_SCHEMAS_INTEGER) &&
3324 (val->type != XML_SCHEMAS_NPINTEGER) &&
3325 (val->type != XML_SCHEMAS_NINTEGER) &&
3326 (val->type != XML_SCHEMAS_NNINTEGER) &&
3327 (val->type != XML_SCHEMAS_PINTEGER) &&
3328 (val->type != XML_SCHEMAS_INT) &&
3329 (val->type != XML_SCHEMAS_UINT) &&
3330 (val->type != XML_SCHEMAS_LONG) &&
3331 (val->type != XML_SCHEMAS_ULONG) &&
3332 (val->type != XML_SCHEMAS_SHORT) &&
3333 (val->type != XML_SCHEMAS_USHORT) &&
3334 (val->type != XML_SCHEMAS_BYTE) &&
3335 (val->type != XML_SCHEMAS_UBYTE))) {
3336 return(-1);
3337 }
3338 if (facet->type == XML_SCHEMA_FACET_TOTALDIGITS) {
3339 if (val->value.decimal.total > facet->val->value.decimal.lo)
3340 return(1);
3341
3342 } else if (facet->type == XML_SCHEMA_FACET_FRACTIONDIGITS) {
3343 if (val->value.decimal.frac > facet->val->value.decimal.lo)
3344 return(1);
3345 }
3346 break;
Daniel Veillard4255d502002-04-16 15:50:10 +00003347 default:
3348 TODO
3349 }
3350 return(0);
Daniel Veillardb6c7f412003-03-29 16:41:55 +00003351
Daniel Veillard4255d502002-04-16 15:50:10 +00003352}
3353
3354#endif /* LIBXML_SCHEMAS_ENABLED */