2 * schemastypes.c : implementation of the XML Schema Datatypes
3 * definition and validity checking
5 * See Copyright for the status of this software.
7 * Daniel Veillard <veillard@redhat.com>
13 #ifdef LIBXML_SCHEMAS_ENABLED
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>
21 #include <libxml/xpath.h>
22 #include <libxml/uri.h>
24 #include <libxml/xmlschemas.h>
25 #include <libxml/schemasInternals.h>
26 #include <libxml/xmlschemastypes.h>
37 #ifndef LIBXML_XPATH_ENABLED
38 extern double xmlXPathNAN
;
39 extern double xmlXPathPINF
;
40 extern double xmlXPathNINF
;
44 xmlGenericError(xmlGenericErrorContext, \
45 "Unimplemented block at %s:%d\n", \
48 #define XML_SCHEMAS_NAMESPACE_NAME \
49 (const xmlChar *)"http://www.w3.org/2001/XMLSchema"
51 #define IS_WSP_REPLACE_CH(c) ((((c) == 0x9) || ((c) == 0xa)) || \
54 #define IS_WSP_SPACE_CH(c) ((c) == 0x20)
56 #define IS_WSP_BLANK_CH(c) IS_BLANK_CH(c)
59 typedef struct _xmlSchemaValDate xmlSchemaValDate
;
60 typedef xmlSchemaValDate
*xmlSchemaValDatePtr
;
61 struct _xmlSchemaValDate
{
63 unsigned int mon
:4; /* 1 <= mon <= 12 */
64 unsigned int day
:5; /* 1 <= day <= 31 */
65 unsigned int hour
:5; /* 0 <= hour <= 23 */
66 unsigned int min
:6; /* 0 <= min <= 59 */
68 unsigned int tz_flag
:1; /* is tzo explicitely set? */
69 signed int tzo
:12; /* -1440 <= tzo <= 1440;
70 currently only -840 to +840 are needed */
74 typedef struct _xmlSchemaValDuration xmlSchemaValDuration
;
75 typedef xmlSchemaValDuration
*xmlSchemaValDurationPtr
;
76 struct _xmlSchemaValDuration
{
77 long mon
; /* mon stores years also */
79 double sec
; /* sec stores min and hour also */
82 typedef struct _xmlSchemaValDecimal xmlSchemaValDecimal
;
83 typedef xmlSchemaValDecimal
*xmlSchemaValDecimalPtr
;
84 struct _xmlSchemaValDecimal
{
85 /* would use long long but not portable */
95 typedef struct _xmlSchemaValQName xmlSchemaValQName
;
96 typedef xmlSchemaValQName
*xmlSchemaValQNamePtr
;
97 struct _xmlSchemaValQName
{
102 typedef struct _xmlSchemaValHex xmlSchemaValHex
;
103 typedef xmlSchemaValHex
*xmlSchemaValHexPtr
;
104 struct _xmlSchemaValHex
{
109 typedef struct _xmlSchemaValBase64 xmlSchemaValBase64
;
110 typedef xmlSchemaValBase64
*xmlSchemaValBase64Ptr
;
111 struct _xmlSchemaValBase64
{
116 struct _xmlSchemaVal
{
117 xmlSchemaValType type
;
118 struct _xmlSchemaVal
*next
;
120 xmlSchemaValDecimal decimal
;
121 xmlSchemaValDate date
;
122 xmlSchemaValDuration dur
;
123 xmlSchemaValQName qname
;
125 xmlSchemaValBase64 base64
;
133 static int xmlSchemaTypesInitialized
= 0;
134 static xmlHashTablePtr xmlSchemaTypesBank
= NULL
;
139 static xmlSchemaTypePtr xmlSchemaTypeStringDef
= NULL
;
140 static xmlSchemaTypePtr xmlSchemaTypeAnyTypeDef
= NULL
;
141 static xmlSchemaTypePtr xmlSchemaTypeAnySimpleTypeDef
= NULL
;
142 static xmlSchemaTypePtr xmlSchemaTypeDecimalDef
= NULL
;
143 static xmlSchemaTypePtr xmlSchemaTypeDatetimeDef
= NULL
;
144 static xmlSchemaTypePtr xmlSchemaTypeDateDef
= NULL
;
145 static xmlSchemaTypePtr xmlSchemaTypeTimeDef
= NULL
;
146 static xmlSchemaTypePtr xmlSchemaTypeGYearDef
= NULL
;
147 static xmlSchemaTypePtr xmlSchemaTypeGYearMonthDef
= NULL
;
148 static xmlSchemaTypePtr xmlSchemaTypeGDayDef
= NULL
;
149 static xmlSchemaTypePtr xmlSchemaTypeGMonthDayDef
= NULL
;
150 static xmlSchemaTypePtr xmlSchemaTypeGMonthDef
= NULL
;
151 static xmlSchemaTypePtr xmlSchemaTypeDurationDef
= NULL
;
152 static xmlSchemaTypePtr xmlSchemaTypeFloatDef
= NULL
;
153 static xmlSchemaTypePtr xmlSchemaTypeBooleanDef
= NULL
;
154 static xmlSchemaTypePtr xmlSchemaTypeDoubleDef
= NULL
;
155 static xmlSchemaTypePtr xmlSchemaTypeHexBinaryDef
= NULL
;
156 static xmlSchemaTypePtr xmlSchemaTypeBase64BinaryDef
= NULL
;
157 static xmlSchemaTypePtr xmlSchemaTypeAnyURIDef
= NULL
;
162 static xmlSchemaTypePtr xmlSchemaTypePositiveIntegerDef
= NULL
;
163 static xmlSchemaTypePtr xmlSchemaTypeNonPositiveIntegerDef
= NULL
;
164 static xmlSchemaTypePtr xmlSchemaTypeNegativeIntegerDef
= NULL
;
165 static xmlSchemaTypePtr xmlSchemaTypeNonNegativeIntegerDef
= NULL
;
166 static xmlSchemaTypePtr xmlSchemaTypeIntegerDef
= NULL
;
167 static xmlSchemaTypePtr xmlSchemaTypeLongDef
= NULL
;
168 static xmlSchemaTypePtr xmlSchemaTypeIntDef
= NULL
;
169 static xmlSchemaTypePtr xmlSchemaTypeShortDef
= NULL
;
170 static xmlSchemaTypePtr xmlSchemaTypeByteDef
= NULL
;
171 static xmlSchemaTypePtr xmlSchemaTypeUnsignedLongDef
= NULL
;
172 static xmlSchemaTypePtr xmlSchemaTypeUnsignedIntDef
= NULL
;
173 static xmlSchemaTypePtr xmlSchemaTypeUnsignedShortDef
= NULL
;
174 static xmlSchemaTypePtr xmlSchemaTypeUnsignedByteDef
= NULL
;
175 static xmlSchemaTypePtr xmlSchemaTypeNormStringDef
= NULL
;
176 static xmlSchemaTypePtr xmlSchemaTypeTokenDef
= NULL
;
177 static xmlSchemaTypePtr xmlSchemaTypeLanguageDef
= NULL
;
178 static xmlSchemaTypePtr xmlSchemaTypeNameDef
= NULL
;
179 static xmlSchemaTypePtr xmlSchemaTypeQNameDef
= NULL
;
180 static xmlSchemaTypePtr xmlSchemaTypeNCNameDef
= NULL
;
181 static xmlSchemaTypePtr xmlSchemaTypeIdDef
= NULL
;
182 static xmlSchemaTypePtr xmlSchemaTypeIdrefDef
= NULL
;
183 static xmlSchemaTypePtr xmlSchemaTypeIdrefsDef
= NULL
;
184 static xmlSchemaTypePtr xmlSchemaTypeEntityDef
= NULL
;
185 static xmlSchemaTypePtr xmlSchemaTypeEntitiesDef
= NULL
;
186 static xmlSchemaTypePtr xmlSchemaTypeNotationDef
= NULL
;
187 static xmlSchemaTypePtr xmlSchemaTypeNmtokenDef
= NULL
;
188 static xmlSchemaTypePtr xmlSchemaTypeNmtokensDef
= NULL
;
190 /************************************************************************
192 * Datatype error handlers *
194 ************************************************************************/
196 * xmlSchemaTypeErrMemory:
197 * @extra: extra informations
199 * Handle an out of memory condition
202 xmlSchemaTypeErrMemory(xmlNodePtr node
, const char *extra
)
204 __xmlSimpleError(XML_FROM_DATATYPE
, XML_ERR_NO_MEMORY
, node
, NULL
, extra
);
207 /************************************************************************
209 * Base types support *
211 ************************************************************************/
215 * @type: the value type
217 * Allocate a new simple type value
219 * Returns a pointer to the new value or NULL in case of error
221 static xmlSchemaValPtr
222 xmlSchemaNewValue(xmlSchemaValType type
) {
223 xmlSchemaValPtr value
;
225 value
= (xmlSchemaValPtr
) xmlMalloc(sizeof(xmlSchemaVal
));
229 memset(value
, 0, sizeof(xmlSchemaVal
));
234 static xmlSchemaFacetPtr
235 xmlSchemaNewMinLengthFacet(int value
)
237 xmlSchemaFacetPtr ret
;
239 ret
= xmlSchemaNewFacet();
243 ret
->type
= XML_SCHEMA_FACET_MINLENGTH
;
244 ret
->val
= xmlSchemaNewValue(XML_SCHEMAS_NNINTEGER
);
245 ret
->val
->value
.decimal
.lo
= value
;
250 * xmlSchemaInitBasicType:
251 * @name: the type name
252 * @type: the value type associated
254 * Initialize one primitive built-in type
256 static xmlSchemaTypePtr
257 xmlSchemaInitBasicType(const char *name
, xmlSchemaValType type
,
258 xmlSchemaTypePtr baseType
) {
259 xmlSchemaTypePtr ret
;
261 ret
= (xmlSchemaTypePtr
) xmlMalloc(sizeof(xmlSchemaType
));
263 xmlSchemaTypeErrMemory(NULL
, "could not initialize basic types");
266 memset(ret
, 0, sizeof(xmlSchemaType
));
267 ret
->name
= (const xmlChar
*)name
;
268 ret
->targetNamespace
= XML_SCHEMAS_NAMESPACE_NAME
;
269 ret
->type
= XML_SCHEMA_TYPE_BASIC
;
270 ret
->baseType
= baseType
;
271 ret
->contentType
= XML_SCHEMA_CONTENT_BASIC
;
276 case XML_SCHEMAS_STRING
:
277 case XML_SCHEMAS_DECIMAL
:
278 case XML_SCHEMAS_DATE
:
279 case XML_SCHEMAS_DATETIME
:
280 case XML_SCHEMAS_TIME
:
281 case XML_SCHEMAS_GYEAR
:
282 case XML_SCHEMAS_GYEARMONTH
:
283 case XML_SCHEMAS_GMONTH
:
284 case XML_SCHEMAS_GMONTHDAY
:
285 case XML_SCHEMAS_GDAY
:
286 case XML_SCHEMAS_DURATION
:
287 case XML_SCHEMAS_FLOAT
:
288 case XML_SCHEMAS_DOUBLE
:
289 case XML_SCHEMAS_BOOLEAN
:
290 case XML_SCHEMAS_ANYURI
:
291 case XML_SCHEMAS_HEXBINARY
:
292 case XML_SCHEMAS_BASE64BINARY
:
293 case XML_SCHEMAS_QNAME
:
294 case XML_SCHEMAS_NOTATION
:
295 ret
->flags
|= XML_SCHEMAS_TYPE_BUILTIN_PRIMITIVE
;
304 case XML_SCHEMAS_ANYTYPE
:
305 case XML_SCHEMAS_ANYSIMPLETYPE
:
307 case XML_SCHEMAS_IDREFS
:
308 case XML_SCHEMAS_NMTOKENS
:
309 case XML_SCHEMAS_ENTITIES
:
310 ret
->flags
|= XML_SCHEMAS_TYPE_VARIETY_LIST
;
311 ret
->facets
= xmlSchemaNewMinLengthFacet(1);
312 ret
->flags
|= XML_SCHEMAS_TYPE_HAS_FACETS
;
315 ret
->flags
|= XML_SCHEMAS_TYPE_VARIETY_ATOMIC
;
318 xmlHashAddEntry2(xmlSchemaTypesBank
, ret
->name
,
319 XML_SCHEMAS_NAMESPACE_NAME
, ret
);
320 ret
->builtInType
= type
;
325 * WARNING: Those type reside normally in xmlschemas.c but are
326 * redefined here locally in oder of being able to use them for xs:anyType-
327 * TODO: Remove those definition if we move the types to a header file.
328 * TODO: Always keep those structs up-to-date with the originals.
330 #define UNBOUNDED (1 << 30)
332 typedef struct _xmlSchemaTreeItem xmlSchemaTreeItem
;
333 typedef xmlSchemaTreeItem
*xmlSchemaTreeItemPtr
;
334 struct _xmlSchemaTreeItem
{
335 xmlSchemaTypeType type
;
336 xmlSchemaAnnotPtr annot
;
337 xmlSchemaTreeItemPtr next
;
338 xmlSchemaTreeItemPtr children
;
341 typedef struct _xmlSchemaParticle xmlSchemaParticle
;
342 typedef xmlSchemaParticle
*xmlSchemaParticlePtr
;
343 struct _xmlSchemaParticle
{
344 xmlSchemaTypeType type
;
345 xmlSchemaAnnotPtr annot
;
346 xmlSchemaTreeItemPtr next
;
347 xmlSchemaTreeItemPtr children
;
353 typedef struct _xmlSchemaModelGroup xmlSchemaModelGroup
;
354 typedef xmlSchemaModelGroup
*xmlSchemaModelGroupPtr
;
355 struct _xmlSchemaModelGroup
{
356 xmlSchemaTypeType type
;
357 xmlSchemaAnnotPtr annot
;
358 xmlSchemaTreeItemPtr next
;
359 xmlSchemaTreeItemPtr children
;
363 static xmlSchemaParticlePtr
364 xmlSchemaAddParticle(void)
366 xmlSchemaParticlePtr ret
= NULL
;
368 ret
= (xmlSchemaParticlePtr
)
369 xmlMalloc(sizeof(xmlSchemaParticle
));
371 xmlSchemaTypeErrMemory(NULL
, "allocating particle component");
374 memset(ret
, 0, sizeof(xmlSchemaParticle
));
375 ret
->type
= XML_SCHEMA_TYPE_PARTICLE
;
382 * xmlSchemaInitTypes:
384 * Initialize the default XML Schemas type library
387 xmlSchemaInitTypes(void)
389 if (xmlSchemaTypesInitialized
!= 0)
391 xmlSchemaTypesBank
= xmlHashCreate(40);
395 * 3.4.7 Built-in Complex Type Definition
397 xmlSchemaTypeAnyTypeDef
= xmlSchemaInitBasicType("anyType",
400 xmlSchemaTypeAnyTypeDef
->baseType
= xmlSchemaTypeAnyTypeDef
;
401 xmlSchemaTypeAnyTypeDef
->contentType
= XML_SCHEMA_CONTENT_MIXED
;
403 * Init the content type.
405 xmlSchemaTypeAnyTypeDef
->contentType
= XML_SCHEMA_CONTENT_MIXED
;
407 xmlSchemaParticlePtr particle
;
408 xmlSchemaModelGroupPtr sequence
;
409 xmlSchemaWildcardPtr wild
;
410 /* First particle. */
411 particle
= xmlSchemaAddParticle();
412 if (particle
== NULL
)
414 xmlSchemaTypeAnyTypeDef
->subtypes
= (xmlSchemaTypePtr
) particle
;
415 /* Sequence model group. */
416 sequence
= (xmlSchemaModelGroupPtr
)
417 xmlMalloc(sizeof(xmlSchemaModelGroup
));
418 if (sequence
== NULL
) {
419 xmlSchemaTypeErrMemory(NULL
, "allocating model group component");
422 memset(sequence
, 0, sizeof(xmlSchemaModelGroup
));
423 sequence
->type
= XML_SCHEMA_TYPE_SEQUENCE
;
424 particle
->children
= (xmlSchemaTreeItemPtr
) sequence
;
425 /* Second particle. */
426 particle
= xmlSchemaAddParticle();
427 if (particle
== NULL
)
429 particle
->minOccurs
= 0;
430 particle
->maxOccurs
= UNBOUNDED
;
431 sequence
->children
= (xmlSchemaTreeItemPtr
) particle
;
433 wild
= (xmlSchemaWildcardPtr
) xmlMalloc(sizeof(xmlSchemaWildcard
));
435 xmlSchemaTypeErrMemory(NULL
, "allocating wildcard component");
438 memset(wild
, 0, sizeof(xmlSchemaWildcard
));
439 wild
->type
= XML_SCHEMA_TYPE_ANY
;
441 wild
->processContents
= XML_SCHEMAS_ANY_LAX
;
442 particle
->children
= (xmlSchemaTreeItemPtr
) wild
;
444 * Create the attribute wildcard.
446 wild
= (xmlSchemaWildcardPtr
) xmlMalloc(sizeof(xmlSchemaWildcard
));
448 xmlSchemaTypeErrMemory(NULL
, "could not create an attribute "
449 "wildcard on anyType");
452 memset(wild
, 0, sizeof(xmlSchemaWildcard
));
454 wild
->processContents
= XML_SCHEMAS_ANY_LAX
;
455 xmlSchemaTypeAnyTypeDef
->attributeWildcard
= wild
;
457 xmlSchemaTypeAnySimpleTypeDef
= xmlSchemaInitBasicType("anySimpleType",
458 XML_SCHEMAS_ANYSIMPLETYPE
,
459 xmlSchemaTypeAnyTypeDef
);
461 * primitive datatypes
463 xmlSchemaTypeStringDef
= xmlSchemaInitBasicType("string",
465 xmlSchemaTypeAnySimpleTypeDef
);
466 xmlSchemaTypeDecimalDef
= xmlSchemaInitBasicType("decimal",
468 xmlSchemaTypeAnySimpleTypeDef
);
469 xmlSchemaTypeDateDef
= xmlSchemaInitBasicType("date",
471 xmlSchemaTypeAnySimpleTypeDef
);
472 xmlSchemaTypeDatetimeDef
= xmlSchemaInitBasicType("dateTime",
473 XML_SCHEMAS_DATETIME
,
474 xmlSchemaTypeAnySimpleTypeDef
);
475 xmlSchemaTypeTimeDef
= xmlSchemaInitBasicType("time",
477 xmlSchemaTypeAnySimpleTypeDef
);
478 xmlSchemaTypeGYearDef
= xmlSchemaInitBasicType("gYear",
480 xmlSchemaTypeAnySimpleTypeDef
);
481 xmlSchemaTypeGYearMonthDef
= xmlSchemaInitBasicType("gYearMonth",
482 XML_SCHEMAS_GYEARMONTH
,
483 xmlSchemaTypeAnySimpleTypeDef
);
484 xmlSchemaTypeGMonthDef
= xmlSchemaInitBasicType("gMonth",
486 xmlSchemaTypeAnySimpleTypeDef
);
487 xmlSchemaTypeGMonthDayDef
= xmlSchemaInitBasicType("gMonthDay",
488 XML_SCHEMAS_GMONTHDAY
,
489 xmlSchemaTypeAnySimpleTypeDef
);
490 xmlSchemaTypeGDayDef
= xmlSchemaInitBasicType("gDay",
492 xmlSchemaTypeAnySimpleTypeDef
);
493 xmlSchemaTypeDurationDef
= xmlSchemaInitBasicType("duration",
494 XML_SCHEMAS_DURATION
,
495 xmlSchemaTypeAnySimpleTypeDef
);
496 xmlSchemaTypeFloatDef
= xmlSchemaInitBasicType("float",
498 xmlSchemaTypeAnySimpleTypeDef
);
499 xmlSchemaTypeDoubleDef
= xmlSchemaInitBasicType("double",
501 xmlSchemaTypeAnySimpleTypeDef
);
502 xmlSchemaTypeBooleanDef
= xmlSchemaInitBasicType("boolean",
504 xmlSchemaTypeAnySimpleTypeDef
);
505 xmlSchemaTypeAnyURIDef
= xmlSchemaInitBasicType("anyURI",
507 xmlSchemaTypeAnySimpleTypeDef
);
508 xmlSchemaTypeHexBinaryDef
= xmlSchemaInitBasicType("hexBinary",
509 XML_SCHEMAS_HEXBINARY
,
510 xmlSchemaTypeAnySimpleTypeDef
);
511 xmlSchemaTypeBase64BinaryDef
512 = xmlSchemaInitBasicType("base64Binary", XML_SCHEMAS_BASE64BINARY
,
513 xmlSchemaTypeAnySimpleTypeDef
);
514 xmlSchemaTypeNotationDef
= xmlSchemaInitBasicType("NOTATION",
515 XML_SCHEMAS_NOTATION
,
516 xmlSchemaTypeAnySimpleTypeDef
);
517 xmlSchemaTypeQNameDef
= xmlSchemaInitBasicType("QName",
519 xmlSchemaTypeAnySimpleTypeDef
);
524 xmlSchemaTypeIntegerDef
= xmlSchemaInitBasicType("integer",
526 xmlSchemaTypeDecimalDef
);
527 xmlSchemaTypeNonPositiveIntegerDef
=
528 xmlSchemaInitBasicType("nonPositiveInteger",
529 XML_SCHEMAS_NPINTEGER
,
530 xmlSchemaTypeIntegerDef
);
531 xmlSchemaTypeNegativeIntegerDef
=
532 xmlSchemaInitBasicType("negativeInteger", XML_SCHEMAS_NINTEGER
,
533 xmlSchemaTypeNonPositiveIntegerDef
);
534 xmlSchemaTypeLongDef
=
535 xmlSchemaInitBasicType("long", XML_SCHEMAS_LONG
,
536 xmlSchemaTypeIntegerDef
);
537 xmlSchemaTypeIntDef
= xmlSchemaInitBasicType("int", XML_SCHEMAS_INT
,
538 xmlSchemaTypeLongDef
);
539 xmlSchemaTypeShortDef
= xmlSchemaInitBasicType("short",
541 xmlSchemaTypeIntDef
);
542 xmlSchemaTypeByteDef
= xmlSchemaInitBasicType("byte",
544 xmlSchemaTypeShortDef
);
545 xmlSchemaTypeNonNegativeIntegerDef
=
546 xmlSchemaInitBasicType("nonNegativeInteger",
547 XML_SCHEMAS_NNINTEGER
,
548 xmlSchemaTypeIntegerDef
);
549 xmlSchemaTypeUnsignedLongDef
=
550 xmlSchemaInitBasicType("unsignedLong", XML_SCHEMAS_ULONG
,
551 xmlSchemaTypeNonNegativeIntegerDef
);
552 xmlSchemaTypeUnsignedIntDef
=
553 xmlSchemaInitBasicType("unsignedInt", XML_SCHEMAS_UINT
,
554 xmlSchemaTypeUnsignedLongDef
);
555 xmlSchemaTypeUnsignedShortDef
=
556 xmlSchemaInitBasicType("unsignedShort", XML_SCHEMAS_USHORT
,
557 xmlSchemaTypeUnsignedIntDef
);
558 xmlSchemaTypeUnsignedByteDef
=
559 xmlSchemaInitBasicType("unsignedByte", XML_SCHEMAS_UBYTE
,
560 xmlSchemaTypeUnsignedShortDef
);
561 xmlSchemaTypePositiveIntegerDef
=
562 xmlSchemaInitBasicType("positiveInteger", XML_SCHEMAS_PINTEGER
,
563 xmlSchemaTypeNonNegativeIntegerDef
);
564 xmlSchemaTypeNormStringDef
= xmlSchemaInitBasicType("normalizedString",
565 XML_SCHEMAS_NORMSTRING
,
566 xmlSchemaTypeStringDef
);
567 xmlSchemaTypeTokenDef
= xmlSchemaInitBasicType("token",
569 xmlSchemaTypeNormStringDef
);
570 xmlSchemaTypeLanguageDef
= xmlSchemaInitBasicType("language",
571 XML_SCHEMAS_LANGUAGE
,
572 xmlSchemaTypeTokenDef
);
573 xmlSchemaTypeNameDef
= xmlSchemaInitBasicType("Name",
575 xmlSchemaTypeTokenDef
);
576 xmlSchemaTypeNmtokenDef
= xmlSchemaInitBasicType("NMTOKEN",
578 xmlSchemaTypeTokenDef
);
579 xmlSchemaTypeNCNameDef
= xmlSchemaInitBasicType("NCName",
581 xmlSchemaTypeNameDef
);
582 xmlSchemaTypeIdDef
= xmlSchemaInitBasicType("ID", XML_SCHEMAS_ID
,
583 xmlSchemaTypeNCNameDef
);
584 xmlSchemaTypeIdrefDef
= xmlSchemaInitBasicType("IDREF",
586 xmlSchemaTypeNCNameDef
);
587 xmlSchemaTypeEntityDef
= xmlSchemaInitBasicType("ENTITY",
589 xmlSchemaTypeNCNameDef
);
591 * Derived list types.
594 xmlSchemaTypeEntitiesDef
= xmlSchemaInitBasicType("ENTITIES",
595 XML_SCHEMAS_ENTITIES
,
596 xmlSchemaTypeAnySimpleTypeDef
);
597 xmlSchemaTypeEntitiesDef
->subtypes
= xmlSchemaTypeEntityDef
;
599 xmlSchemaTypeIdrefsDef
= xmlSchemaInitBasicType("IDREFS",
601 xmlSchemaTypeAnySimpleTypeDef
);
602 xmlSchemaTypeIdrefsDef
->subtypes
= xmlSchemaTypeIdrefDef
;
605 xmlSchemaTypeNmtokensDef
= xmlSchemaInitBasicType("NMTOKENS",
606 XML_SCHEMAS_NMTOKENS
,
607 xmlSchemaTypeAnySimpleTypeDef
);
608 xmlSchemaTypeNmtokensDef
->subtypes
= xmlSchemaTypeNmtokenDef
;
610 xmlSchemaTypesInitialized
= 1;
614 * xmlSchemaCleanupTypes:
616 * Cleanup the default XML Schemas type library
619 xmlSchemaCleanupTypes(void) {
620 if (xmlSchemaTypesInitialized
== 0)
626 xmlSchemaParticlePtr particle
;
627 /* Attribute wildcard. */
628 xmlSchemaFreeWildcard(xmlSchemaTypeAnyTypeDef
->attributeWildcard
);
630 particle
= (xmlSchemaParticlePtr
) xmlSchemaTypeAnyTypeDef
->subtypes
;
632 xmlSchemaFreeWildcard((xmlSchemaWildcardPtr
)
633 particle
->children
->children
->children
);
634 xmlFree((xmlSchemaParticlePtr
) particle
->children
->children
);
635 /* Sequence model group. */
636 xmlFree((xmlSchemaModelGroupPtr
) particle
->children
);
637 xmlFree((xmlSchemaParticlePtr
) particle
);
638 xmlSchemaTypeAnyTypeDef
->subtypes
= NULL
;
640 xmlHashFree(xmlSchemaTypesBank
, (xmlHashDeallocator
) xmlSchemaFreeType
);
641 xmlSchemaTypesInitialized
= 0;
645 * xmlSchemaIsBuiltInTypeFacet:
646 * @type: the built-in type
647 * @facetType: the facet type
649 * Evaluates if a specific facet can be
650 * used in conjunction with a type.
652 * Returns 1 if the facet can be used with the given built-in type,
653 * 0 otherwise and -1 in case the type is not a built-in type.
656 xmlSchemaIsBuiltInTypeFacet(xmlSchemaTypePtr type
, int facetType
)
660 if (type
->type
!= XML_SCHEMA_TYPE_BASIC
)
662 switch (type
->builtInType
) {
663 case XML_SCHEMAS_BOOLEAN
:
664 if ((facetType
== XML_SCHEMA_FACET_PATTERN
) ||
665 (facetType
== XML_SCHEMA_FACET_WHITESPACE
))
669 case XML_SCHEMAS_STRING
:
670 case XML_SCHEMAS_NOTATION
:
671 case XML_SCHEMAS_QNAME
:
672 case XML_SCHEMAS_ANYURI
:
673 case XML_SCHEMAS_BASE64BINARY
:
674 case XML_SCHEMAS_HEXBINARY
:
675 if ((facetType
== XML_SCHEMA_FACET_LENGTH
) ||
676 (facetType
== XML_SCHEMA_FACET_MINLENGTH
) ||
677 (facetType
== XML_SCHEMA_FACET_MAXLENGTH
) ||
678 (facetType
== XML_SCHEMA_FACET_PATTERN
) ||
679 (facetType
== XML_SCHEMA_FACET_ENUMERATION
) ||
680 (facetType
== XML_SCHEMA_FACET_WHITESPACE
))
684 case XML_SCHEMAS_DECIMAL
:
685 if ((facetType
== XML_SCHEMA_FACET_TOTALDIGITS
) ||
686 (facetType
== XML_SCHEMA_FACET_FRACTIONDIGITS
) ||
687 (facetType
== XML_SCHEMA_FACET_PATTERN
) ||
688 (facetType
== XML_SCHEMA_FACET_WHITESPACE
) ||
689 (facetType
== XML_SCHEMA_FACET_ENUMERATION
) ||
690 (facetType
== XML_SCHEMA_FACET_MAXINCLUSIVE
) ||
691 (facetType
== XML_SCHEMA_FACET_MAXEXCLUSIVE
) ||
692 (facetType
== XML_SCHEMA_FACET_MININCLUSIVE
) ||
693 (facetType
== XML_SCHEMA_FACET_MINEXCLUSIVE
))
697 case XML_SCHEMAS_TIME
:
698 case XML_SCHEMAS_GDAY
:
699 case XML_SCHEMAS_GMONTH
:
700 case XML_SCHEMAS_GMONTHDAY
:
701 case XML_SCHEMAS_GYEAR
:
702 case XML_SCHEMAS_GYEARMONTH
:
703 case XML_SCHEMAS_DATE
:
704 case XML_SCHEMAS_DATETIME
:
705 case XML_SCHEMAS_DURATION
:
706 case XML_SCHEMAS_FLOAT
:
707 case XML_SCHEMAS_DOUBLE
:
708 if ((facetType
== XML_SCHEMA_FACET_PATTERN
) ||
709 (facetType
== XML_SCHEMA_FACET_ENUMERATION
) ||
710 (facetType
== XML_SCHEMA_FACET_WHITESPACE
) ||
711 (facetType
== XML_SCHEMA_FACET_MAXINCLUSIVE
) ||
712 (facetType
== XML_SCHEMA_FACET_MAXEXCLUSIVE
) ||
713 (facetType
== XML_SCHEMA_FACET_MININCLUSIVE
) ||
714 (facetType
== XML_SCHEMA_FACET_MINEXCLUSIVE
))
725 * xmlSchemaGetBuiltInType:
726 * @type: the type of the built in type
728 * Gives you the type struct for a built-in
729 * type by its type id.
731 * Returns the type if found, NULL otherwise.
734 xmlSchemaGetBuiltInType(xmlSchemaValType type
)
736 if (xmlSchemaTypesInitialized
== 0)
737 xmlSchemaInitTypes();
740 case XML_SCHEMAS_ANYSIMPLETYPE
:
741 return (xmlSchemaTypeAnySimpleTypeDef
);
742 case XML_SCHEMAS_STRING
:
743 return (xmlSchemaTypeStringDef
);
744 case XML_SCHEMAS_NORMSTRING
:
745 return (xmlSchemaTypeNormStringDef
);
746 case XML_SCHEMAS_DECIMAL
:
747 return (xmlSchemaTypeDecimalDef
);
748 case XML_SCHEMAS_TIME
:
749 return (xmlSchemaTypeTimeDef
);
750 case XML_SCHEMAS_GDAY
:
751 return (xmlSchemaTypeGDayDef
);
752 case XML_SCHEMAS_GMONTH
:
753 return (xmlSchemaTypeGMonthDef
);
754 case XML_SCHEMAS_GMONTHDAY
:
755 return (xmlSchemaTypeGMonthDayDef
);
756 case XML_SCHEMAS_GYEAR
:
757 return (xmlSchemaTypeGYearDef
);
758 case XML_SCHEMAS_GYEARMONTH
:
759 return (xmlSchemaTypeGYearMonthDef
);
760 case XML_SCHEMAS_DATE
:
761 return (xmlSchemaTypeDateDef
);
762 case XML_SCHEMAS_DATETIME
:
763 return (xmlSchemaTypeDatetimeDef
);
764 case XML_SCHEMAS_DURATION
:
765 return (xmlSchemaTypeDurationDef
);
766 case XML_SCHEMAS_FLOAT
:
767 return (xmlSchemaTypeFloatDef
);
768 case XML_SCHEMAS_DOUBLE
:
769 return (xmlSchemaTypeDoubleDef
);
770 case XML_SCHEMAS_BOOLEAN
:
771 return (xmlSchemaTypeBooleanDef
);
772 case XML_SCHEMAS_TOKEN
:
773 return (xmlSchemaTypeTokenDef
);
774 case XML_SCHEMAS_LANGUAGE
:
775 return (xmlSchemaTypeLanguageDef
);
776 case XML_SCHEMAS_NMTOKEN
:
777 return (xmlSchemaTypeNmtokenDef
);
778 case XML_SCHEMAS_NMTOKENS
:
779 return (xmlSchemaTypeNmtokensDef
);
780 case XML_SCHEMAS_NAME
:
781 return (xmlSchemaTypeNameDef
);
782 case XML_SCHEMAS_QNAME
:
783 return (xmlSchemaTypeQNameDef
);
784 case XML_SCHEMAS_NCNAME
:
785 return (xmlSchemaTypeNCNameDef
);
787 return (xmlSchemaTypeIdDef
);
788 case XML_SCHEMAS_IDREF
:
789 return (xmlSchemaTypeIdrefDef
);
790 case XML_SCHEMAS_IDREFS
:
791 return (xmlSchemaTypeIdrefsDef
);
792 case XML_SCHEMAS_ENTITY
:
793 return (xmlSchemaTypeEntityDef
);
794 case XML_SCHEMAS_ENTITIES
:
795 return (xmlSchemaTypeEntitiesDef
);
796 case XML_SCHEMAS_NOTATION
:
797 return (xmlSchemaTypeNotationDef
);
798 case XML_SCHEMAS_ANYURI
:
799 return (xmlSchemaTypeAnyURIDef
);
800 case XML_SCHEMAS_INTEGER
:
801 return (xmlSchemaTypeIntegerDef
);
802 case XML_SCHEMAS_NPINTEGER
:
803 return (xmlSchemaTypeNonPositiveIntegerDef
);
804 case XML_SCHEMAS_NINTEGER
:
805 return (xmlSchemaTypeNegativeIntegerDef
);
806 case XML_SCHEMAS_NNINTEGER
:
807 return (xmlSchemaTypeNonNegativeIntegerDef
);
808 case XML_SCHEMAS_PINTEGER
:
809 return (xmlSchemaTypePositiveIntegerDef
);
810 case XML_SCHEMAS_INT
:
811 return (xmlSchemaTypeIntDef
);
812 case XML_SCHEMAS_UINT
:
813 return (xmlSchemaTypeUnsignedIntDef
);
814 case XML_SCHEMAS_LONG
:
815 return (xmlSchemaTypeLongDef
);
816 case XML_SCHEMAS_ULONG
:
817 return (xmlSchemaTypeUnsignedLongDef
);
818 case XML_SCHEMAS_SHORT
:
819 return (xmlSchemaTypeShortDef
);
820 case XML_SCHEMAS_USHORT
:
821 return (xmlSchemaTypeUnsignedShortDef
);
822 case XML_SCHEMAS_BYTE
:
823 return (xmlSchemaTypeByteDef
);
824 case XML_SCHEMAS_UBYTE
:
825 return (xmlSchemaTypeUnsignedByteDef
);
826 case XML_SCHEMAS_HEXBINARY
:
827 return (xmlSchemaTypeHexBinaryDef
);
828 case XML_SCHEMAS_BASE64BINARY
:
829 return (xmlSchemaTypeBase64BinaryDef
);
830 case XML_SCHEMAS_ANYTYPE
:
831 return (xmlSchemaTypeAnyTypeDef
);
838 * xmlSchemaValueAppend:
840 * @cur: the value to be appended
842 * Appends a next sibling to a list of computed values.
844 * Returns 0 if succeeded and -1 on API errors.
847 xmlSchemaValueAppend(xmlSchemaValPtr prev
, xmlSchemaValPtr cur
) {
849 if ((prev
== NULL
) || (cur
== NULL
))
856 * xmlSchemaValueGetNext:
859 * Accessor for the next sibling of a list of computed values.
861 * Returns the next value or NULL if there was none, or on
865 xmlSchemaValueGetNext(xmlSchemaValPtr cur
) {
873 * xmlSchemaValueGetAsString:
876 * Accessor for the string value of a computed value.
878 * Returns the string value or NULL if there was none, or on
882 xmlSchemaValueGetAsString(xmlSchemaValPtr val
)
887 case XML_SCHEMAS_STRING
:
888 case XML_SCHEMAS_NORMSTRING
:
889 case XML_SCHEMAS_ANYSIMPLETYPE
:
890 case XML_SCHEMAS_TOKEN
:
891 case XML_SCHEMAS_LANGUAGE
:
892 case XML_SCHEMAS_NMTOKEN
:
893 case XML_SCHEMAS_NAME
:
894 case XML_SCHEMAS_NCNAME
:
896 case XML_SCHEMAS_IDREF
:
897 case XML_SCHEMAS_ENTITY
:
898 case XML_SCHEMAS_ANYURI
:
899 return (BAD_CAST val
->value
.str
);
907 * xmlSchemaValueGetAsBoolean:
910 * Accessor for the boolean value of a computed value.
912 * Returns 1 if true and 0 if false, or in case of an error. Hmm.
915 xmlSchemaValueGetAsBoolean(xmlSchemaValPtr val
)
917 if ((val
== NULL
) || (val
->type
!= XML_SCHEMAS_BOOLEAN
))
919 return (val
->value
.b
);
923 * xmlSchemaNewStringValue:
924 * @type: the value type
927 * Allocate a new simple type value. The type can be
928 * of XML_SCHEMAS_STRING.
929 * WARNING: This one is intended to be expanded for other
930 * string based types. We need this for anySimpleType as well.
931 * The given value is consumed and freed with the struct.
933 * Returns a pointer to the new value or NULL in case of error
936 xmlSchemaNewStringValue(xmlSchemaValType type
,
937 const xmlChar
*value
)
941 if (type
!= XML_SCHEMAS_STRING
)
943 val
= (xmlSchemaValPtr
) xmlMalloc(sizeof(xmlSchemaVal
));
947 memset(val
, 0, sizeof(xmlSchemaVal
));
949 val
->value
.str
= (xmlChar
*) value
;
954 * xmlSchemaNewNOTATIONValue:
955 * @name: the notation name
956 * @ns: the notation namespace name or NULL
958 * Allocate a new NOTATION value.
959 * The given values are consumed and freed with the struct.
961 * Returns a pointer to the new value or NULL in case of error
964 xmlSchemaNewNOTATIONValue(const xmlChar
*name
,
969 val
= xmlSchemaNewValue(XML_SCHEMAS_NOTATION
);
973 val
->value
.qname
.name
= (xmlChar
*)name
;
975 val
->value
.qname
.uri
= (xmlChar
*)ns
;
980 * xmlSchemaNewQNameValue:
981 * @namespaceName: the namespace name
982 * @localName: the local name
984 * Allocate a new QName value.
985 * The given values are consumed and freed with the struct.
987 * Returns a pointer to the new value or NULL in case of an error.
990 xmlSchemaNewQNameValue(const xmlChar
*namespaceName
,
991 const xmlChar
*localName
)
995 val
= xmlSchemaNewValue(XML_SCHEMAS_QNAME
);
999 val
->value
.qname
.name
= (xmlChar
*) localName
;
1000 val
->value
.qname
.uri
= (xmlChar
*) namespaceName
;
1005 * xmlSchemaFreeValue:
1006 * @value: the value to free
1008 * Cleanup the default XML Schemas type library
1011 xmlSchemaFreeValue(xmlSchemaValPtr value
) {
1012 xmlSchemaValPtr prev
;
1014 while (value
!= NULL
) {
1015 switch (value
->type
) {
1016 case XML_SCHEMAS_STRING
:
1017 case XML_SCHEMAS_NORMSTRING
:
1018 case XML_SCHEMAS_TOKEN
:
1019 case XML_SCHEMAS_LANGUAGE
:
1020 case XML_SCHEMAS_NMTOKEN
:
1021 case XML_SCHEMAS_NMTOKENS
:
1022 case XML_SCHEMAS_NAME
:
1023 case XML_SCHEMAS_NCNAME
:
1024 case XML_SCHEMAS_ID
:
1025 case XML_SCHEMAS_IDREF
:
1026 case XML_SCHEMAS_IDREFS
:
1027 case XML_SCHEMAS_ENTITY
:
1028 case XML_SCHEMAS_ENTITIES
:
1029 case XML_SCHEMAS_ANYURI
:
1030 case XML_SCHEMAS_ANYSIMPLETYPE
:
1031 if (value
->value
.str
!= NULL
)
1032 xmlFree(value
->value
.str
);
1034 case XML_SCHEMAS_NOTATION
:
1035 case XML_SCHEMAS_QNAME
:
1036 if (value
->value
.qname
.uri
!= NULL
)
1037 xmlFree(value
->value
.qname
.uri
);
1038 if (value
->value
.qname
.name
!= NULL
)
1039 xmlFree(value
->value
.qname
.name
);
1041 case XML_SCHEMAS_HEXBINARY
:
1042 if (value
->value
.hex
.str
!= NULL
)
1043 xmlFree(value
->value
.hex
.str
);
1045 case XML_SCHEMAS_BASE64BINARY
:
1046 if (value
->value
.base64
.str
!= NULL
)
1047 xmlFree(value
->value
.base64
.str
);
1053 value
= value
->next
;
1059 * xmlSchemaGetPredefinedType:
1060 * @name: the type name
1061 * @ns: the URI of the namespace usually "http://www.w3.org/2001/XMLSchema"
1063 * Lookup a type in the default XML Schemas type library
1065 * Returns the type if found, NULL otherwise
1068 xmlSchemaGetPredefinedType(const xmlChar
*name
, const xmlChar
*ns
) {
1069 if (xmlSchemaTypesInitialized
== 0)
1070 xmlSchemaInitTypes();
1073 return((xmlSchemaTypePtr
) xmlHashLookup2(xmlSchemaTypesBank
, name
, ns
));
1077 * xmlSchemaGetBuiltInListSimpleTypeItemType:
1078 * @type: the built-in simple type.
1082 * Returns the item type of @type as defined by the built-in datatype
1083 * hierarchy of XML Schema Part 2: Datatypes, or NULL in case of an error.
1086 xmlSchemaGetBuiltInListSimpleTypeItemType(xmlSchemaTypePtr type
)
1088 if ((type
== NULL
) || (type
->type
!= XML_SCHEMA_TYPE_BASIC
))
1090 switch (type
->builtInType
) {
1091 case XML_SCHEMAS_NMTOKENS
:
1092 return (xmlSchemaTypeNmtokenDef
);
1093 case XML_SCHEMAS_IDREFS
:
1094 return (xmlSchemaTypeIdrefDef
);
1095 case XML_SCHEMAS_ENTITIES
:
1096 return (xmlSchemaTypeEntityDef
);
1102 /****************************************************************
1104 * Convenience macros and functions *
1106 ****************************************************************/
1108 #define IS_TZO_CHAR(c) \
1109 ((c == 0) || (c == 'Z') || (c == '+') || (c == '-'))
1111 #define VALID_YEAR(yr) (yr != 0)
1112 #define VALID_MONTH(mon) ((mon >= 1) && (mon <= 12))
1113 /* VALID_DAY should only be used when month is unknown */
1114 #define VALID_DAY(day) ((day >= 1) && (day <= 31))
1115 #define VALID_HOUR(hr) ((hr >= 0) && (hr <= 23))
1116 #define VALID_MIN(min) ((min >= 0) && (min <= 59))
1117 #define VALID_SEC(sec) ((sec >= 0) && (sec < 60))
1118 #define VALID_TZO(tzo) ((tzo > -840) && (tzo < 840))
1119 #define IS_LEAP(y) \
1120 (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0))
1122 static const unsigned int daysInMonth
[12] =
1123 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1124 static const unsigned int daysInMonthLeap
[12] =
1125 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
1127 #define MAX_DAYINMONTH(yr,mon) \
1128 (IS_LEAP(yr) ? daysInMonthLeap[mon - 1] : daysInMonth[mon - 1])
1130 #define VALID_MDAY(dt) \
1131 (IS_LEAP(dt->year) ? \
1132 (dt->day <= daysInMonthLeap[dt->mon - 1]) : \
1133 (dt->day <= daysInMonth[dt->mon - 1]))
1135 #define VALID_DATE(dt) \
1136 (VALID_YEAR(dt->year) && VALID_MONTH(dt->mon) && VALID_MDAY(dt))
1138 #define VALID_TIME(dt) \
1139 (VALID_HOUR(dt->hour) && VALID_MIN(dt->min) && \
1140 VALID_SEC(dt->sec) && VALID_TZO(dt->tzo))
1142 #define VALID_DATETIME(dt) \
1143 (VALID_DATE(dt) && VALID_TIME(dt))
1145 #define SECS_PER_MIN (60)
1146 #define SECS_PER_HOUR (60 * SECS_PER_MIN)
1147 #define SECS_PER_DAY (24 * SECS_PER_HOUR)
1149 static const long dayInYearByMonth
[12] =
1150 { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1151 static const long dayInLeapYearByMonth
[12] =
1152 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 };
1154 #define DAY_IN_YEAR(day, month, year) \
1156 dayInLeapYearByMonth[month - 1] : \
1157 dayInYearByMonth[month - 1]) + day)
1160 #define DEBUG_DATE(dt) \
1161 xmlGenericError(xmlGenericErrorContext, \
1162 "type=%o %04ld-%02u-%02uT%02u:%02u:%03f", \
1163 dt->type,dt->value.date.year,dt->value.date.mon, \
1164 dt->value.date.day,dt->value.date.hour,dt->value.date.min, \
1165 dt->value.date.sec); \
1166 if (dt->value.date.tz_flag) \
1167 if (dt->value.date.tzo != 0) \
1168 xmlGenericError(xmlGenericErrorContext, \
1169 "%+05d\n",dt->value.date.tzo); \
1171 xmlGenericError(xmlGenericErrorContext, "Z\n"); \
1173 xmlGenericError(xmlGenericErrorContext,"\n")
1175 #define DEBUG_DATE(dt)
1179 * _xmlSchemaParseGYear:
1180 * @dt: pointer to a date structure
1181 * @str: pointer to the string to analyze
1183 * Parses a xs:gYear without time zone and fills in the appropriate
1184 * field of the @dt structure. @str is updated to point just after the
1185 * xs:gYear. It is supposed that @dt->year is big enough to contain
1188 * Returns 0 or the error code
1191 _xmlSchemaParseGYear (xmlSchemaValDatePtr dt
, const xmlChar
**str
) {
1192 const xmlChar
*cur
= *str
, *firstChar
;
1193 int isneg
= 0, digcnt
= 0;
1195 if (((*cur
< '0') || (*cur
> '9')) &&
1196 (*cur
!= '-') && (*cur
!= '+'))
1206 while ((*cur
>= '0') && (*cur
<= '9')) {
1207 dt
->year
= dt
->year
* 10 + (*cur
- '0');
1212 /* year must be at least 4 digits (CCYY); over 4
1213 * digits cannot have a leading zero. */
1214 if ((digcnt
< 4) || ((digcnt
> 4) && (*firstChar
== '0')))
1218 dt
->year
= - dt
->year
;
1220 if (!VALID_YEAR(dt
->year
))
1229 * @num: the integer to fill in
1230 * @cur: an #xmlChar *
1231 * @invalid: an integer
1233 * Parses a 2-digits integer and updates @num with the value. @cur is
1234 * updated to point just after the integer.
1235 * In case of error, @invalid is set to %TRUE, values of @num and
1236 * @cur are undefined.
1238 #define PARSE_2_DIGITS(num, cur, invalid) \
1239 if ((cur[0] < '0') || (cur[0] > '9') || \
1240 (cur[1] < '0') || (cur[1] > '9')) \
1243 num = (cur[0] - '0') * 10 + (cur[1] - '0'); \
1248 * @num: the double to fill in
1249 * @cur: an #xmlChar *
1250 * @invalid: an integer
1252 * Parses a float and updates @num with the value. @cur is
1253 * updated to point just after the float. The float must have a
1254 * 2-digits integer part and may or may not have a decimal part.
1255 * In case of error, @invalid is set to %TRUE, values of @num and
1256 * @cur are undefined.
1258 #define PARSE_FLOAT(num, cur, invalid) \
1259 PARSE_2_DIGITS(num, cur, invalid); \
1260 if (!invalid && (*cur == '.')) { \
1263 if ((*cur < '0') || (*cur > '9')) \
1265 while ((*cur >= '0') && (*cur <= '9')) { \
1267 num += (*cur - '0') * mult; \
1273 * _xmlSchemaParseGMonth:
1274 * @dt: pointer to a date structure
1275 * @str: pointer to the string to analyze
1277 * Parses a xs:gMonth without time zone and fills in the appropriate
1278 * field of the @dt structure. @str is updated to point just after the
1281 * Returns 0 or the error code
1284 _xmlSchemaParseGMonth (xmlSchemaValDatePtr dt
, const xmlChar
**str
) {
1285 const xmlChar
*cur
= *str
;
1287 unsigned int value
= 0;
1289 PARSE_2_DIGITS(value
, cur
, ret
);
1293 if (!VALID_MONTH(value
))
1303 * _xmlSchemaParseGDay:
1304 * @dt: pointer to a date structure
1305 * @str: pointer to the string to analyze
1307 * Parses a xs:gDay without time zone and fills in the appropriate
1308 * field of the @dt structure. @str is updated to point just after the
1311 * Returns 0 or the error code
1314 _xmlSchemaParseGDay (xmlSchemaValDatePtr dt
, const xmlChar
**str
) {
1315 const xmlChar
*cur
= *str
;
1317 unsigned int value
= 0;
1319 PARSE_2_DIGITS(value
, cur
, ret
);
1323 if (!VALID_DAY(value
))
1332 * _xmlSchemaParseTime:
1333 * @dt: pointer to a date structure
1334 * @str: pointer to the string to analyze
1336 * Parses a xs:time without time zone and fills in the appropriate
1337 * fields of the @dt structure. @str is updated to point just after the
1339 * In case of error, values of @dt fields are undefined.
1341 * Returns 0 or the error code
1344 _xmlSchemaParseTime (xmlSchemaValDatePtr dt
, const xmlChar
**str
) {
1345 const xmlChar
*cur
= *str
;
1349 PARSE_2_DIGITS(value
, cur
, ret
);
1354 if (!VALID_HOUR(value
))
1358 /* the ':' insures this string is xs:time */
1361 PARSE_2_DIGITS(value
, cur
, ret
);
1364 if (!VALID_MIN(value
))
1372 PARSE_FLOAT(dt
->sec
, cur
, ret
);
1376 if ((!VALID_SEC(dt
->sec
)) || (!VALID_TZO(dt
->tzo
)))
1384 * _xmlSchemaParseTimeZone:
1385 * @dt: pointer to a date structure
1386 * @str: pointer to the string to analyze
1388 * Parses a time zone without time zone and fills in the appropriate
1389 * field of the @dt structure. @str is updated to point just after the
1392 * Returns 0 or the error code
1395 _xmlSchemaParseTimeZone (xmlSchemaValDatePtr dt
, const xmlChar
**str
) {
1417 int isneg
= 0, tmp
= 0;
1418 isneg
= (*cur
== '-');
1422 PARSE_2_DIGITS(tmp
, cur
, ret
);
1425 if (!VALID_HOUR(tmp
))
1434 PARSE_2_DIGITS(tmp
, cur
, ret
);
1437 if (!VALID_MIN(tmp
))
1442 dt
->tzo
= - dt
->tzo
;
1444 if (!VALID_TZO(dt
->tzo
))
1459 * _xmlSchemaBase64Decode:
1462 * Converts a base64 encoded character to its base 64 value.
1464 * Returns 0-63 (value), 64 (pad), or -1 (not recognized)
1467 _xmlSchemaBase64Decode (const xmlChar ch
) {
1468 if (('A' <= ch
) && (ch
<= 'Z')) return ch
- 'A';
1469 if (('a' <= ch
) && (ch
<= 'z')) return ch
- 'a' + 26;
1470 if (('0' <= ch
) && (ch
<= '9')) return ch
- '0' + 52;
1471 if ('+' == ch
) return 62;
1472 if ('/' == ch
) return 63;
1473 if ('=' == ch
) return 64;
1477 /****************************************************************
1479 * XML Schema Dates/Times Datatypes Handling *
1481 ****************************************************************/
1485 * @num: the integer to fill in
1486 * @cur: an #xmlChar *
1487 * @num_type: an integer flag
1489 * Parses a digits integer and updates @num with the value. @cur is
1490 * updated to point just after the integer.
1491 * In case of error, @num_type is set to -1, values of @num and
1492 * @cur are undefined.
1494 #define PARSE_DIGITS(num, cur, num_type) \
1495 if ((*cur < '0') || (*cur > '9')) \
1498 while ((*cur >= '0') && (*cur <= '9')) { \
1499 num = num * 10 + (*cur - '0'); \
1505 * @num: the double to fill in
1506 * @cur: an #xmlChar *
1507 * @num_type: an integer flag
1509 * Parses a float or integer and updates @num with the value. @cur is
1510 * updated to point just after the number. If the number is a float,
1511 * then it must have an integer part and a decimal part; @num_type will
1512 * be set to 1. If there is no decimal part, @num_type is set to zero.
1513 * In case of error, @num_type is set to -1, values of @num and
1514 * @cur are undefined.
1516 #define PARSE_NUM(num, cur, num_type) \
1518 PARSE_DIGITS(num, cur, num_type); \
1519 if (!num_type && (*cur == '.')) { \
1522 if ((*cur < '0') || (*cur > '9')) \
1526 while ((*cur >= '0') && (*cur <= '9')) { \
1528 num += (*cur - '0') * mult; \
1534 * xmlSchemaValidateDates:
1535 * @type: the expected type or XML_SCHEMAS_UNKNOWN
1536 * @dateTime: string to analyze
1537 * @val: the return computed value
1539 * Check that @dateTime conforms to the lexical space of one of the date types.
1540 * if true a value is computed and returned in @val.
1542 * Returns 0 if this validates, a positive error code number otherwise
1543 * and -1 in case of internal or API error.
1546 xmlSchemaValidateDates (xmlSchemaValType type
,
1547 const xmlChar
*dateTime
, xmlSchemaValPtr
*val
,
1551 const xmlChar
*cur
= dateTime
;
1553 #define RETURN_TYPE_IF_VALID(t) \
1554 if (IS_TZO_CHAR(*cur)) { \
1555 ret = _xmlSchemaParseTimeZone(&(dt->value.date), &cur); \
1564 if (dateTime
== NULL
)
1568 while IS_WSP_BLANK_CH(*cur
) cur
++;
1570 if ((*cur
!= '-') && (*cur
< '0') && (*cur
> '9'))
1573 dt
= xmlSchemaNewValue(XML_SCHEMAS_UNKNOWN
);
1577 if ((cur
[0] == '-') && (cur
[1] == '-')) {
1579 * It's an incomplete date (xs:gMonthDay, xs:gMonth or
1584 /* is it an xs:gDay? */
1586 if (type
== XML_SCHEMAS_GMONTH
)
1589 ret
= _xmlSchemaParseGDay(&(dt
->value
.date
), &cur
);
1593 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GDAY
);
1599 * it should be an xs:gMonthDay or xs:gMonth
1601 ret
= _xmlSchemaParseGMonth(&(dt
->value
.date
), &cur
);
1606 * a '-' char could indicate this type is xs:gMonthDay or
1607 * a negative time zone offset. Check for xs:gMonthDay first.
1608 * Also the first three char's of a negative tzo (-MM:SS) can
1609 * appear to be a valid day; so even if the day portion
1610 * of the xs:gMonthDay verifies, we must insure it was not
1614 const xmlChar
*rewnd
= cur
;
1617 ret
= _xmlSchemaParseGDay(&(dt
->value
.date
), &cur
);
1618 if ((ret
== 0) && ((*cur
== 0) || (*cur
!= ':'))) {
1621 * we can use the VALID_MDAY macro to validate the month
1622 * and day because the leap year test will flag year zero
1623 * as a leap year (even though zero is an invalid year).
1624 * FUTURE TODO: Zero will become valid in XML Schema 1.1
1627 if (VALID_MDAY((&(dt
->value
.date
)))) {
1629 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTHDAY
);
1636 * not xs:gMonthDay so rewind and check if just xs:gMonth
1637 * with an optional time zone.
1642 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GMONTH
);
1648 * It's a right-truncated date or an xs:time.
1649 * Try to parse an xs:time then fallback on right-truncated dates.
1651 if ((*cur
>= '0') && (*cur
<= '9')) {
1652 ret
= _xmlSchemaParseTime(&(dt
->value
.date
), &cur
);
1654 /* it's an xs:time */
1655 RETURN_TYPE_IF_VALID(XML_SCHEMAS_TIME
);
1659 /* fallback on date parsing */
1662 ret
= _xmlSchemaParseGYear(&(dt
->value
.date
), &cur
);
1666 /* is it an xs:gYear? */
1667 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEAR
);
1673 ret
= _xmlSchemaParseGMonth(&(dt
->value
.date
), &cur
);
1677 /* is it an xs:gYearMonth? */
1678 RETURN_TYPE_IF_VALID(XML_SCHEMAS_GYEARMONTH
);
1684 ret
= _xmlSchemaParseGDay(&(dt
->value
.date
), &cur
);
1685 if ((ret
!= 0) || !VALID_DATE((&(dt
->value
.date
))))
1688 /* is it an xs:date? */
1689 RETURN_TYPE_IF_VALID(XML_SCHEMAS_DATE
);
1695 /* it should be an xs:dateTime */
1696 ret
= _xmlSchemaParseTime(&(dt
->value
.date
), &cur
);
1700 ret
= _xmlSchemaParseTimeZone(&(dt
->value
.date
), &cur
);
1702 while IS_WSP_BLANK_CH(*cur
) cur
++;
1703 if ((ret
!= 0) || (*cur
!= 0) || (!(VALID_DATETIME((&(dt
->value
.date
))))))
1707 dt
->type
= XML_SCHEMAS_DATETIME
;
1711 if ((type
!= XML_SCHEMAS_UNKNOWN
) && (type
!= dt
->type
))
1715 * insure the parsed type is equal to or less significant (right
1716 * truncated) than the desired type.
1718 if ((type
!= XML_SCHEMAS_UNKNOWN
) && (type
!= dt
->type
)) {
1720 /* time only matches time */
1721 if ((type
== XML_SCHEMAS_TIME
) && (dt
->type
== XML_SCHEMAS_TIME
))
1724 if ((type
== XML_SCHEMAS_DATETIME
) &&
1725 ((dt
->type
!= XML_SCHEMAS_DATE
) ||
1726 (dt
->type
!= XML_SCHEMAS_GYEARMONTH
) ||
1727 (dt
->type
!= XML_SCHEMAS_GYEAR
)))
1730 if ((type
== XML_SCHEMAS_DATE
) &&
1731 ((dt
->type
!= XML_SCHEMAS_GYEAR
) ||
1732 (dt
->type
!= XML_SCHEMAS_GYEARMONTH
)))
1735 if ((type
== XML_SCHEMAS_GYEARMONTH
) && (dt
->type
!= XML_SCHEMAS_GYEAR
))
1738 if ((type
== XML_SCHEMAS_GMONTHDAY
) && (dt
->type
!= XML_SCHEMAS_GMONTH
))
1746 xmlSchemaFreeValue(dt
);
1752 xmlSchemaFreeValue(dt
);
1757 * xmlSchemaValidateDuration:
1758 * @type: the predefined type
1759 * @duration: string to analyze
1760 * @val: the return computed value
1762 * Check that @duration conforms to the lexical space of the duration type.
1763 * if true a value is computed and returned in @val.
1765 * Returns 0 if this validates, a positive error code number otherwise
1766 * and -1 in case of internal or API error.
1769 xmlSchemaValidateDuration (xmlSchemaTypePtr type ATTRIBUTE_UNUSED
,
1770 const xmlChar
*duration
, xmlSchemaValPtr
*val
,
1772 const xmlChar
*cur
= duration
;
1773 xmlSchemaValPtr dur
;
1775 unsigned int seq
= 0;
1777 int num_type
= 0; /* -1 = invalid, 0 = int, 1 = floating */
1778 const xmlChar desig
[] = {'Y', 'M', 'D', 'H', 'M', 'S'};
1779 const double multi
[] = { 0.0, 0.0, 86400.0, 3600.0, 60.0, 1.0, 0.0};
1781 if (duration
== NULL
)
1785 while IS_WSP_BLANK_CH(*cur
) cur
++;
1792 /* duration must start with 'P' (after sign) */
1799 dur
= xmlSchemaNewValue(XML_SCHEMAS_DURATION
);
1805 /* input string should be empty or invalid date/time item */
1806 if (seq
>= sizeof(desig
))
1809 /* T designator must be present for time items */
1816 } else if (seq
== 3)
1819 /* parse the number portion of the item */
1820 PARSE_NUM(num
, cur
, num_type
);
1822 if ((num_type
== -1) || (*cur
== 0))
1825 /* update duration based on item type */
1826 while (seq
< sizeof(desig
)) {
1827 if (*cur
== desig
[seq
]) {
1829 /* verify numeric type; only seconds can be float */
1830 if ((num_type
!= 0) && (seq
< (sizeof(desig
)-1)))
1835 dur
->value
.dur
.mon
= (long)num
* 12;
1838 dur
->value
.dur
.mon
+= (long)num
;
1841 /* convert to seconds using multiplier */
1842 dur
->value
.dur
.sec
+= num
* multi
[seq
];
1847 break; /* exit loop */
1849 /* no date designators found? */
1850 if ((++seq
== 3) || (seq
== 6))
1855 while IS_WSP_BLANK_CH(*cur
) cur
++;
1859 dur
->value
.dur
.mon
= -dur
->value
.dur
.mon
;
1860 dur
->value
.dur
.day
= -dur
->value
.dur
.day
;
1861 dur
->value
.dur
.sec
= -dur
->value
.dur
.sec
;
1867 xmlSchemaFreeValue(dur
);
1873 xmlSchemaFreeValue(dur
);
1881 * Removes the leading and ending spaces of a string
1883 * Returns the new string or NULL if no change was required.
1886 xmlSchemaStrip(const xmlChar
*value
) {
1887 const xmlChar
*start
= value
, *end
, *f
;
1889 if (value
== NULL
) return(NULL
);
1890 while ((*start
!= 0) && (IS_BLANK_CH(*start
))) start
++;
1892 while (*end
!= 0) end
++;
1895 while ((end
> start
) && (IS_BLANK_CH(*end
))) end
--;
1897 if ((start
== value
) && (f
== end
)) return(NULL
);
1898 return(xmlStrndup(start
, end
- start
));
1902 * xmlSchemaWhiteSpaceReplace:
1905 * Replaces 0xd, 0x9 and 0xa with a space.
1907 * Returns the new string or NULL if no change was required.
1910 xmlSchemaWhiteSpaceReplace(const xmlChar
*value
) {
1911 const xmlChar
*cur
= value
;
1912 xmlChar
*ret
= NULL
, *mcur
;
1917 while ((*cur
!= 0) &&
1918 (((*cur
) != 0xd) && ((*cur
) != 0x9) && ((*cur
) != 0xa))) {
1923 ret
= xmlStrdup(value
);
1924 /* TODO FIXME: I guess gcc will bark at this. */
1925 mcur
= (xmlChar
*) (ret
+ (cur
- value
));
1927 if ( ((*mcur
) == 0xd) || ((*mcur
) == 0x9) || ((*mcur
) == 0xa) )
1930 } while (*mcur
!= 0);
1935 * xmlSchemaCollapseString:
1938 * Removes and normalize white spaces in the string
1940 * Returns the new string or NULL if no change was required.
1943 xmlSchemaCollapseString(const xmlChar
*value
) {
1944 const xmlChar
*start
= value
, *end
, *f
;
1948 if (value
== NULL
) return(NULL
);
1949 while ((*start
!= 0) && (IS_BLANK_CH(*start
))) start
++;
1952 if ((*end
== ' ') && (IS_BLANK_CH(end
[1]))) {
1955 } else if ((*end
== 0xa) || (*end
== 0x9) || (*end
== 0xd)) {
1964 while ((end
> start
) && (IS_BLANK_CH(*end
))) end
--;
1966 if ((start
== value
) && (f
== end
)) return(NULL
);
1967 return(xmlStrndup(start
, end
- start
));
1969 start
= xmlStrdup(start
);
1970 if (start
== NULL
) return(NULL
);
1971 g
= (xmlChar
*) (start
+ col
);
1974 if (IS_BLANK_CH(*end
)) {
1976 while (IS_BLANK_CH(*end
)) end
++;
1983 return((xmlChar
*) start
);
1987 * xmlSchemaValAtomicListNode:
1988 * @type: the predefined atomic type for a token in the list
1989 * @value: the list value to check
1990 * @ret: the return computed value
1991 * @node: the node containing the value
1993 * Check that a value conforms to the lexical space of the predefined
1994 * list type. if true a value is computed and returned in @ret.
1996 * Returns the number of items if this validates, a negative error code
2000 xmlSchemaValAtomicListNode(xmlSchemaTypePtr type
, const xmlChar
*value
,
2001 xmlSchemaValPtr
*ret
, xmlNodePtr node
) {
2002 xmlChar
*val
, *cur
, *endval
;
2006 if (value
== NULL
) {
2009 val
= xmlStrdup(value
);
2020 while (IS_BLANK_CH(*cur
)) *cur
++ = 0;
2022 if (IS_BLANK_CH(*cur
)) {
2025 while (IS_BLANK_CH(*cur
)) *cur
++ = 0;
2029 while ((*cur
!= 0) && (!IS_BLANK_CH(*cur
))) cur
++;
2032 if (nb_values
== 0) {
2038 while ((*cur
== 0) && (cur
!= endval
)) cur
++;
2039 while (cur
!= endval
) {
2040 tmp
= xmlSchemaValPredefTypeNode(type
, cur
, NULL
, node
);
2043 while (*cur
!= 0) cur
++;
2044 while ((*cur
== 0) && (cur
!= endval
)) cur
++;
2046 /* TODO what return value ? c.f. bug #158628
2057 * xmlSchemaParseUInt:
2058 * @str: pointer to the string R/W
2059 * @llo: pointer to the low result
2060 * @lmi: pointer to the mid result
2061 * @lhi: pointer to the high result
2063 * Parse an unsigned long into 3 fields.
2065 * Returns the number of significant digits in the number or
2066 * -1 if overflow of the capacity and -2 if it's not a number.
2069 xmlSchemaParseUInt(const xmlChar
**str
, unsigned long *llo
,
2070 unsigned long *lmi
, unsigned long *lhi
) {
2071 unsigned long lo
= 0, mi
= 0, hi
= 0;
2072 const xmlChar
*tmp
, *cur
= *str
;
2075 if (!((*cur
>= '0') && (*cur
<= '9')))
2078 while (*cur
== '0') { /* ignore leading zeroes */
2082 while ((*tmp
!= 0) && (*tmp
>= '0') && (*tmp
<= '9')) {
2090 hi
= hi
* 10 + (*cur
++ - '0');
2094 mi
= mi
* 10 + (*cur
++ - '0');
2098 lo
= lo
* 10 + (*cur
++ - '0');
2110 * xmlSchemaValAtomicType:
2111 * @type: the predefined type
2112 * @value: the value to check
2113 * @val: the return computed value
2114 * @node: the node containing the value
2115 * flags: flags to control the vlidation
2117 * Check that a value conforms to the lexical space of the atomic type.
2118 * if true a value is computed and returned in @val.
2119 * This checks the value space for list types as well (IDREFS, NMTOKENS).
2121 * Returns 0 if this validates, a positive error code number otherwise
2122 * and -1 in case of internal or API error.
2125 xmlSchemaValAtomicType(xmlSchemaTypePtr type
, const xmlChar
* value
,
2126 xmlSchemaValPtr
* val
, xmlNodePtr node
, int flags
,
2127 xmlSchemaWhitespaceValueType ws
,
2128 int normOnTheFly
, int applyNorm
, int createStringValue
)
2131 xmlChar
*norm
= NULL
;
2134 if (xmlSchemaTypesInitialized
== 0)
2135 xmlSchemaInitTypes();
2140 * validating a non existant text node is similar to validating
2144 value
= BAD_CAST
"";
2148 if ((flags
== 0) && (value
!= NULL
)) {
2150 if ((type
->builtInType
!= XML_SCHEMAS_STRING
) &&
2151 (type
->builtInType
!= XML_SCHEMAS_ANYTYPE
) &&
2152 (type
->builtInType
!= XML_SCHEMAS_ANYSIMPLETYPE
)) {
2153 if (type
->builtInType
== XML_SCHEMAS_NORMSTRING
)
2154 norm
= xmlSchemaWhiteSpaceReplace(value
);
2156 norm
= xmlSchemaCollapseString(value
);
2162 switch (type
->builtInType
) {
2163 case XML_SCHEMAS_UNKNOWN
:
2165 case XML_SCHEMAS_ANYTYPE
:
2166 case XML_SCHEMAS_ANYSIMPLETYPE
:
2167 if ((createStringValue
) && (val
!= NULL
)) {
2168 v
= xmlSchemaNewValue(XML_SCHEMAS_ANYSIMPLETYPE
);
2170 v
->value
.str
= xmlStrdup(value
);
2177 case XML_SCHEMAS_STRING
:
2178 if (! normOnTheFly
) {
2179 const xmlChar
*cur
= value
;
2181 if (ws
== XML_SCHEMA_WHITESPACE_REPLACE
) {
2183 if ((*cur
== 0xd) || (*cur
== 0xa) || (*cur
== 0x9)) {
2189 } else if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
) {
2191 if ((*cur
== 0xd) || (*cur
== 0xa) || (*cur
== 0x9)) {
2193 } else if IS_WSP_SPACE_CH(*cur
) {
2195 if IS_WSP_SPACE_CH(*cur
)
2203 if (createStringValue
&& (val
!= NULL
)) {
2205 if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
2206 norm
= xmlSchemaCollapseString(value
);
2207 else if (ws
== XML_SCHEMA_WHITESPACE_REPLACE
)
2208 norm
= xmlSchemaWhiteSpaceReplace(value
);
2212 v
= xmlSchemaNewValue(XML_SCHEMAS_STRING
);
2214 v
->value
.str
= xmlStrdup(value
);
2221 case XML_SCHEMAS_NORMSTRING
:{
2224 if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
2225 norm
= xmlSchemaCollapseString(value
);
2227 norm
= xmlSchemaWhiteSpaceReplace(value
);
2232 const xmlChar
*cur
= value
;
2234 if ((*cur
== 0xd) || (*cur
== 0xa) || (*cur
== 0x9)) {
2242 v
= xmlSchemaNewValue(XML_SCHEMAS_NORMSTRING
);
2244 v
->value
.str
= xmlStrdup(value
);
2252 case XML_SCHEMAS_DECIMAL
:{
2253 const xmlChar
*cur
= value
;
2254 unsigned int len
, neg
, integ
, hasLeadingZeroes
;
2256 xmlChar
*cptr
= cval
;
2258 if ((cur
== NULL
) || (*cur
== 0))
2262 * xs:decimal has a whitespace-facet value of 'collapse'.
2265 while IS_WSP_BLANK_CH(*cur
) cur
++;
2268 * First we handle an optional sign.
2274 } else if (*cur
== '+')
2277 * Disallow: "", "-", "- "
2282 * Next we "pre-parse" the number, in preparation for calling
2283 * the common routine xmlSchemaParseUInt. We get rid of any
2284 * leading zeroes (because we have reserved only 25 chars),
2285 * and note the position of a decimal point.
2289 hasLeadingZeroes
= 0;
2291 * Skip leading zeroes.
2293 while (*cur
== '0') {
2295 hasLeadingZeroes
= 1;
2299 if ((*cur
>= '0') && (*cur
<= '9')) {
2302 } else if (*cur
== '.') {
2306 if ((*cur
>= '0') && (*cur
<= '9')) {
2313 * Disallow "." but allow "00."
2315 if ((len
== 0) && (!hasLeadingZeroes
))
2323 while IS_WSP_BLANK_CH(*cur
) cur
++;
2325 goto return1
; /* error if any extraneous chars */
2327 v
= xmlSchemaNewValue(XML_SCHEMAS_DECIMAL
);
2330 * Now evaluate the significant digits of the number
2336 * Get rid of trailing zeroes in the
2339 while ((len
!= integ
) && (*(cptr
-1) == '0')) {
2345 * Terminate the (preparsed) string.
2351 xmlSchemaParseUInt((const xmlChar
**)&cptr
,
2352 &v
->value
.decimal
.lo
,
2353 &v
->value
.decimal
.mi
,
2354 &v
->value
.decimal
.hi
);
2358 * Set the total digits to 1 if a zero value.
2360 v
->value
.decimal
.sign
= neg
;
2362 /* Speedup for zero values. */
2363 v
->value
.decimal
.total
= 1;
2365 v
->value
.decimal
.total
= len
;
2367 v
->value
.decimal
.frac
= 0;
2369 v
->value
.decimal
.frac
= len
- integ
;
2376 case XML_SCHEMAS_TIME
:
2377 case XML_SCHEMAS_GDAY
:
2378 case XML_SCHEMAS_GMONTH
:
2379 case XML_SCHEMAS_GMONTHDAY
:
2380 case XML_SCHEMAS_GYEAR
:
2381 case XML_SCHEMAS_GYEARMONTH
:
2382 case XML_SCHEMAS_DATE
:
2383 case XML_SCHEMAS_DATETIME
:
2384 ret
= xmlSchemaValidateDates(type
->builtInType
, value
, val
,
2387 case XML_SCHEMAS_DURATION
:
2388 ret
= xmlSchemaValidateDuration(type
, value
, val
,
2391 case XML_SCHEMAS_FLOAT
:
2392 case XML_SCHEMAS_DOUBLE
:{
2393 const xmlChar
*cur
= value
;
2397 while IS_WSP_BLANK_CH(*cur
) cur
++;
2399 if ((cur
[0] == 'N') && (cur
[1] == 'a') && (cur
[2] == 'N')) {
2404 if (type
== xmlSchemaTypeFloatDef
) {
2405 v
= xmlSchemaNewValue(XML_SCHEMAS_FLOAT
);
2407 v
->value
.f
= (float) xmlXPathNAN
;
2409 xmlSchemaFreeValue(v
);
2413 v
= xmlSchemaNewValue(XML_SCHEMAS_DOUBLE
);
2415 v
->value
.d
= xmlXPathNAN
;
2417 xmlSchemaFreeValue(v
);
2429 if ((cur
[0] == 'I') && (cur
[1] == 'N') && (cur
[2] == 'F')) {
2434 if (type
== xmlSchemaTypeFloatDef
) {
2435 v
= xmlSchemaNewValue(XML_SCHEMAS_FLOAT
);
2438 v
->value
.f
= (float) xmlXPathNINF
;
2440 v
->value
.f
= (float) xmlXPathPINF
;
2442 xmlSchemaFreeValue(v
);
2446 v
= xmlSchemaNewValue(XML_SCHEMAS_DOUBLE
);
2449 v
->value
.d
= xmlXPathNINF
;
2451 v
->value
.d
= xmlXPathPINF
;
2453 xmlSchemaFreeValue(v
);
2461 if ((neg
== 0) && (*cur
== '+'))
2463 if ((cur
[0] == 0) || (cur
[0] == '+') || (cur
[0] == '-'))
2465 while ((*cur
>= '0') && (*cur
<= '9')) {
2470 while ((*cur
>= '0') && (*cur
<= '9'))
2473 if ((*cur
== 'e') || (*cur
== 'E')) {
2475 if ((*cur
== '-') || (*cur
== '+'))
2477 while ((*cur
>= '0') && (*cur
<= '9'))
2481 while IS_WSP_BLANK_CH(*cur
) cur
++;
2486 if (type
== xmlSchemaTypeFloatDef
) {
2487 v
= xmlSchemaNewValue(XML_SCHEMAS_FLOAT
);
2490 * TODO: sscanf seems not to give the correct
2491 * value for extremely high/low values.
2492 * E.g. "1E-149" results in zero.
2494 if (sscanf((const char *) value
, "%f",
2495 &(v
->value
.f
)) == 1) {
2498 xmlSchemaFreeValue(v
);
2505 v
= xmlSchemaNewValue(XML_SCHEMAS_DOUBLE
);
2508 * TODO: sscanf seems not to give the correct
2509 * value for extremely high/low values.
2511 if (sscanf((const char *) value
, "%lf",
2512 &(v
->value
.d
)) == 1) {
2515 xmlSchemaFreeValue(v
);
2525 case XML_SCHEMAS_BOOLEAN
:{
2526 const xmlChar
*cur
= value
;
2529 while IS_WSP_BLANK_CH(*cur
) cur
++;
2533 } else if (*cur
== '1') {
2536 } else if (*cur
== 't') {
2538 if ((*cur
++ == 'r') && (*cur
++ == 'u') &&
2543 } else if (*cur
== 'f') {
2545 if ((*cur
++ == 'a') && (*cur
++ == 'l') &&
2546 (*cur
++ == 's') && (*cur
++ == 'e')) {
2553 while IS_WSP_BLANK_CH(*cur
) cur
++;
2558 if ((cur
[0] == '0') && (cur
[1] == 0))
2560 else if ((cur
[0] == '1') && (cur
[1] == 0))
2562 else if ((cur
[0] == 't') && (cur
[1] == 'r')
2563 && (cur
[2] == 'u') && (cur
[3] == 'e')
2566 else if ((cur
[0] == 'f') && (cur
[1] == 'a')
2567 && (cur
[2] == 'l') && (cur
[3] == 's')
2568 && (cur
[4] == 'e') && (cur
[5] == 0))
2574 v
= xmlSchemaNewValue(XML_SCHEMAS_BOOLEAN
);
2584 case XML_SCHEMAS_TOKEN
:{
2585 const xmlChar
*cur
= value
;
2587 if (! normOnTheFly
) {
2589 if ((*cur
== 0xd) || (*cur
== 0xa) || (*cur
== 0x9)) {
2591 } else if (*cur
== ' ') {
2603 v
= xmlSchemaNewValue(XML_SCHEMAS_TOKEN
);
2605 v
->value
.str
= xmlStrdup(value
);
2613 case XML_SCHEMAS_LANGUAGE
:
2615 norm
= xmlSchemaCollapseString(value
);
2619 if (xmlCheckLanguageID(value
) == 1) {
2621 v
= xmlSchemaNewValue(XML_SCHEMAS_LANGUAGE
);
2623 v
->value
.str
= xmlStrdup(value
);
2632 case XML_SCHEMAS_NMTOKEN
:
2633 if (xmlValidateNMToken(value
, 1) == 0) {
2635 v
= xmlSchemaNewValue(XML_SCHEMAS_NMTOKEN
);
2637 v
->value
.str
= xmlStrdup(value
);
2646 case XML_SCHEMAS_NMTOKENS
:
2647 ret
= xmlSchemaValAtomicListNode(xmlSchemaTypeNmtokenDef
,
2654 case XML_SCHEMAS_NAME
:
2655 ret
= xmlValidateName(value
, 1);
2656 if ((ret
== 0) && (val
!= NULL
) && (value
!= NULL
)) {
2657 v
= xmlSchemaNewValue(XML_SCHEMAS_NAME
);
2659 const xmlChar
*start
= value
, *end
;
2660 while (IS_BLANK_CH(*start
)) start
++;
2662 while ((*end
!= 0) && (!IS_BLANK_CH(*end
))) end
++;
2663 v
->value
.str
= xmlStrndup(start
, end
- start
);
2670 case XML_SCHEMAS_QNAME
:{
2671 const xmlChar
*uri
= NULL
;
2672 xmlChar
*local
= NULL
;
2674 ret
= xmlValidateQName(value
, 1);
2681 local
= xmlSplitQName2(value
, &prefix
);
2682 ns
= xmlSearchNs(node
->doc
, node
, prefix
);
2683 if ((ns
== NULL
) && (prefix
!= NULL
)) {
2695 v
= xmlSchemaNewValue(XML_SCHEMAS_QNAME
);
2702 v
->value
.qname
.name
= local
;
2704 v
->value
.qname
.name
= xmlStrdup(value
);
2706 v
->value
.qname
.uri
= xmlStrdup(uri
);
2713 case XML_SCHEMAS_NCNAME
:
2714 ret
= xmlValidateNCName(value
, 1);
2715 if ((ret
== 0) && (val
!= NULL
)) {
2716 v
= xmlSchemaNewValue(XML_SCHEMAS_NCNAME
);
2718 v
->value
.str
= xmlStrdup(value
);
2725 case XML_SCHEMAS_ID
:
2726 ret
= xmlValidateNCName(value
, 1);
2727 if ((ret
== 0) && (val
!= NULL
)) {
2728 v
= xmlSchemaNewValue(XML_SCHEMAS_ID
);
2730 v
->value
.str
= xmlStrdup(value
);
2736 if ((ret
== 0) && (node
!= NULL
) &&
2737 (node
->type
== XML_ATTRIBUTE_NODE
)) {
2738 xmlAttrPtr attr
= (xmlAttrPtr
) node
;
2741 * NOTE: the IDness might have already be declared in the DTD
2743 if (attr
->atype
!= XML_ATTRIBUTE_ID
) {
2747 strip
= xmlSchemaStrip(value
);
2748 if (strip
!= NULL
) {
2749 res
= xmlAddID(NULL
, node
->doc
, strip
, attr
);
2752 res
= xmlAddID(NULL
, node
->doc
, value
, attr
);
2756 attr
->atype
= XML_ATTRIBUTE_ID
;
2761 case XML_SCHEMAS_IDREF
:
2762 ret
= xmlValidateNCName(value
, 1);
2763 if ((ret
== 0) && (val
!= NULL
)) {
2764 v
= xmlSchemaNewValue(XML_SCHEMAS_IDREF
);
2767 v
->value
.str
= xmlStrdup(value
);
2770 if ((ret
== 0) && (node
!= NULL
) &&
2771 (node
->type
== XML_ATTRIBUTE_NODE
)) {
2772 xmlAttrPtr attr
= (xmlAttrPtr
) node
;
2775 strip
= xmlSchemaStrip(value
);
2776 if (strip
!= NULL
) {
2777 xmlAddRef(NULL
, node
->doc
, strip
, attr
);
2780 xmlAddRef(NULL
, node
->doc
, value
, attr
);
2781 attr
->atype
= XML_ATTRIBUTE_IDREF
;
2784 case XML_SCHEMAS_IDREFS
:
2785 ret
= xmlSchemaValAtomicListNode(xmlSchemaTypeIdrefDef
,
2791 if ((ret
== 0) && (node
!= NULL
) &&
2792 (node
->type
== XML_ATTRIBUTE_NODE
)) {
2793 xmlAttrPtr attr
= (xmlAttrPtr
) node
;
2795 attr
->atype
= XML_ATTRIBUTE_IDREFS
;
2798 case XML_SCHEMAS_ENTITY
:{
2801 ret
= xmlValidateNCName(value
, 1);
2802 if ((node
== NULL
) || (node
->doc
== NULL
))
2807 strip
= xmlSchemaStrip(value
);
2808 if (strip
!= NULL
) {
2809 ent
= xmlGetDocEntity(node
->doc
, strip
);
2812 ent
= xmlGetDocEntity(node
->doc
, value
);
2814 if ((ent
== NULL
) ||
2816 XML_EXTERNAL_GENERAL_UNPARSED_ENTITY
))
2819 if ((ret
== 0) && (val
!= NULL
)) {
2822 if ((ret
== 0) && (node
!= NULL
) &&
2823 (node
->type
== XML_ATTRIBUTE_NODE
)) {
2824 xmlAttrPtr attr
= (xmlAttrPtr
) node
;
2826 attr
->atype
= XML_ATTRIBUTE_ENTITY
;
2830 case XML_SCHEMAS_ENTITIES
:
2831 if ((node
== NULL
) || (node
->doc
== NULL
))
2833 ret
= xmlSchemaValAtomicListNode(xmlSchemaTypeEntityDef
,
2839 if ((ret
== 0) && (node
!= NULL
) &&
2840 (node
->type
== XML_ATTRIBUTE_NODE
)) {
2841 xmlAttrPtr attr
= (xmlAttrPtr
) node
;
2843 attr
->atype
= XML_ATTRIBUTE_ENTITIES
;
2846 case XML_SCHEMAS_NOTATION
:{
2847 xmlChar
*uri
= NULL
;
2848 xmlChar
*local
= NULL
;
2850 ret
= xmlValidateQName(value
, 1);
2851 if ((ret
== 0) && (node
!= NULL
)) {
2854 local
= xmlSplitQName2(value
, &prefix
);
2855 if (prefix
!= NULL
) {
2858 ns
= xmlSearchNs(node
->doc
, node
, prefix
);
2861 else if (val
!= NULL
)
2862 uri
= xmlStrdup(ns
->href
);
2864 if ((local
!= NULL
) && ((val
== NULL
) || (ret
!= 0)))
2869 if ((node
== NULL
) || (node
->doc
== NULL
))
2872 ret
= xmlValidateNotationUse(NULL
, node
->doc
, value
);
2878 if ((ret
== 0) && (val
!= NULL
)) {
2879 v
= xmlSchemaNewValue(XML_SCHEMAS_NOTATION
);
2882 v
->value
.qname
.name
= local
;
2884 v
->value
.qname
.name
= xmlStrdup(value
);
2886 v
->value
.qname
.uri
= uri
;
2899 case XML_SCHEMAS_ANYURI
:{
2902 xmlChar
*tmpval
, *cur
;
2904 norm
= xmlSchemaCollapseString(value
);
2908 tmpval
= xmlStrdup(value
);
2909 for (cur
= tmpval
; *cur
; ++cur
) {
2910 if (*cur
< 32 || *cur
>= 127 || *cur
== ' ' ||
2911 *cur
== '<' || *cur
== '>' || *cur
== '"' ||
2912 *cur
== '{' || *cur
== '}' || *cur
== '|' ||
2913 *cur
== '\\' || *cur
== '^' || *cur
== '`' ||
2917 uri
= xmlParseURI((const char *) tmpval
);
2925 v
= xmlSchemaNewValue(XML_SCHEMAS_ANYURI
);
2928 v
->value
.str
= xmlStrdup(value
);
2933 case XML_SCHEMAS_HEXBINARY
:{
2934 const xmlChar
*cur
= value
, *start
;
2942 while IS_WSP_BLANK_CH(*cur
) cur
++;
2945 while (((*cur
>= '0') && (*cur
<= '9')) ||
2946 ((*cur
>= 'A') && (*cur
<= 'F')) ||
2947 ((*cur
>= 'a') && (*cur
<= 'f'))) {
2952 while IS_WSP_BLANK_CH(*cur
) cur
++;
2961 v
= xmlSchemaNewValue(XML_SCHEMAS_HEXBINARY
);
2965 * Copy only the normalized piece.
2966 * CRITICAL TODO: Check this.
2968 cur
= xmlStrndup(start
, i
);
2970 xmlSchemaTypeErrMemory(node
, "allocating hexbin data");
2975 total
= i
/ 2; /* number of octets */
2977 base
= (xmlChar
*) cur
;
2980 *base
= *base
- ('a' - 'A');
2984 v
->value
.hex
.str
= (xmlChar
*) cur
;
2985 v
->value
.hex
.total
= total
;
2990 case XML_SCHEMAS_BASE64BINARY
:{
2993 * Ignore all stray characters? (yes, currently)
2994 * Worry about long lines? (no, currently)
2998 * "The encoded output stream must be represented in lines of
2999 * no more than 76 characters each. All line breaks or other
3000 * characters not found in Table 1 must be ignored by decoding
3001 * software. In base64 data, characters other than those in
3002 * Table 1, line breaks, and other white space probably
3003 * indicate a transmission error, about which a warning
3004 * message or even a message rejection might be appropriate
3005 * under some circumstances." */
3006 const xmlChar
*cur
= value
;
3008 int total
, i
= 0, pad
= 0;
3013 for (; *cur
; ++cur
) {
3016 decc
= _xmlSchemaBase64Decode(*cur
);
3023 for (; *cur
; ++cur
) {
3026 decc
= _xmlSchemaBase64Decode(*cur
);
3034 /* rfc2045.txt: "Special processing is performed if fewer than
3035 * 24 bits are available at the end of the data being encoded.
3036 * A full encoding quantum is always completed at the end of a
3037 * body. When fewer than 24 input bits are available in an
3038 * input group, zero bits are added (on the right) to form an
3039 * integral number of 6-bit groups. Padding at the end of the
3040 * data is performed using the "=" character. Since all
3041 * base64 input is an integral number of octets, only the
3042 * following cases can arise: (1) the final quantum of
3043 * encoding input is an integral multiple of 24 bits; here,
3044 * the final unit of encoded output will be an integral
3045 * multiple ofindent: Standard input:701: Warning:old style
3046 * assignment ambiguity in "=*". Assuming "= *" 4 characters
3047 * with no "=" padding, (2) the final
3048 * quantum of encoding input is exactly 8 bits; here, the
3049 * final unit of encoded output will be two characters
3050 * followed by two "=" padding characters, or (3) the final
3051 * quantum of encoding input is exactly 16 bits; here, the
3052 * final unit of encoded output will be three characters
3053 * followed by one "=" padding character." */
3055 total
= 3 * (i
/ 4);
3059 } else if (pad
== 1) {
3064 for (decc
= _xmlSchemaBase64Decode(*cur
);
3065 (decc
< 0) || (decc
> 63);
3066 decc
= _xmlSchemaBase64Decode(*cur
))
3068 /* 16bits in 24bits means 2 pad bits: nnnnnn nnmmmm mmmm00*/
3069 /* 00111100 -> 0x3c */
3073 } else if (pad
== 2) {
3078 for (decc
= _xmlSchemaBase64Decode(*cur
);
3079 (decc
< 0) || (decc
> 63);
3080 decc
= _xmlSchemaBase64Decode(*cur
))
3082 /* 8bits in 12bits means 4 pad bits: nnnnnn nn0000 */
3083 /* 00110000 -> 0x30 */
3091 v
= xmlSchemaNewValue(XML_SCHEMAS_BASE64BINARY
);
3095 (xmlChar
*) xmlMallocAtomic((i
+ pad
+ 1) *
3098 xmlSchemaTypeErrMemory(node
, "allocating base64 data");
3102 v
->value
.base64
.str
= base
;
3103 for (cur
= value
; *cur
; ++cur
)
3104 if (_xmlSchemaBase64Decode(*cur
) >= 0) {
3109 v
->value
.base64
.total
= total
;
3114 case XML_SCHEMAS_INTEGER
:
3115 case XML_SCHEMAS_PINTEGER
:
3116 case XML_SCHEMAS_NPINTEGER
:
3117 case XML_SCHEMAS_NINTEGER
:
3118 case XML_SCHEMAS_NNINTEGER
:{
3119 const xmlChar
*cur
= value
;
3120 unsigned long lo
, mi
, hi
;
3126 while IS_WSP_BLANK_CH(*cur
) cur
++;
3130 } else if (*cur
== '+')
3132 ret
= xmlSchemaParseUInt(&cur
, &lo
, &mi
, &hi
);
3136 while IS_WSP_BLANK_CH(*cur
) cur
++;
3139 if (type
->builtInType
== XML_SCHEMAS_NPINTEGER
) {
3141 ((hi
!= 0) || (mi
!= 0) || (lo
!= 0)))
3143 } else if (type
->builtInType
== XML_SCHEMAS_PINTEGER
) {
3146 if ((hi
== 0) && (mi
== 0) && (lo
== 0))
3148 } else if (type
->builtInType
== XML_SCHEMAS_NINTEGER
) {
3151 if ((hi
== 0) && (mi
== 0) && (lo
== 0))
3153 } else if (type
->builtInType
== XML_SCHEMAS_NNINTEGER
) {
3155 ((hi
!= 0) || (mi
!= 0) || (lo
!= 0)))
3159 v
= xmlSchemaNewValue(type
->builtInType
);
3163 v
->value
.decimal
.lo
= lo
;
3164 v
->value
.decimal
.mi
= mi
;
3165 v
->value
.decimal
.hi
= hi
;
3166 v
->value
.decimal
.sign
= sign
;
3167 v
->value
.decimal
.frac
= 0;
3168 v
->value
.decimal
.total
= ret
;
3174 case XML_SCHEMAS_LONG
:
3175 case XML_SCHEMAS_BYTE
:
3176 case XML_SCHEMAS_SHORT
:
3177 case XML_SCHEMAS_INT
:{
3178 const xmlChar
*cur
= value
;
3179 unsigned long lo
, mi
, hi
;
3187 } else if (*cur
== '+')
3189 ret
= xmlSchemaParseUInt(&cur
, &lo
, &mi
, &hi
);
3194 if (type
->builtInType
== XML_SCHEMAS_LONG
) {
3198 if (mi
>= 33720368) {
3201 if ((sign
== 0) && (lo
> 54775807))
3203 if ((sign
== 1) && (lo
> 54775808))
3207 } else if (type
->builtInType
== XML_SCHEMAS_INT
) {
3213 if ((sign
== 0) && (lo
> 47483647))
3215 if ((sign
== 1) && (lo
> 47483648))
3218 } else if (type
->builtInType
== XML_SCHEMAS_SHORT
) {
3219 if ((mi
!= 0) || (hi
!= 0))
3221 if ((sign
== 1) && (lo
> 32768))
3223 if ((sign
== 0) && (lo
> 32767))
3225 } else if (type
->builtInType
== XML_SCHEMAS_BYTE
) {
3226 if ((mi
!= 0) || (hi
!= 0))
3228 if ((sign
== 1) && (lo
> 128))
3230 if ((sign
== 0) && (lo
> 127))
3234 v
= xmlSchemaNewValue(type
->builtInType
);
3236 v
->value
.decimal
.lo
= lo
;
3237 v
->value
.decimal
.mi
= mi
;
3238 v
->value
.decimal
.hi
= hi
;
3239 v
->value
.decimal
.sign
= sign
;
3240 v
->value
.decimal
.frac
= 0;
3241 v
->value
.decimal
.total
= ret
;
3247 case XML_SCHEMAS_UINT
:
3248 case XML_SCHEMAS_ULONG
:
3249 case XML_SCHEMAS_USHORT
:
3250 case XML_SCHEMAS_UBYTE
:{
3251 const xmlChar
*cur
= value
;
3252 unsigned long lo
, mi
, hi
;
3256 ret
= xmlSchemaParseUInt(&cur
, &lo
, &mi
, &hi
);
3261 if (type
->builtInType
== XML_SCHEMAS_ULONG
) {
3265 if (mi
>= 67440737) {
3272 } else if (type
->builtInType
== XML_SCHEMAS_UINT
) {
3281 } else if (type
->builtInType
== XML_SCHEMAS_USHORT
) {
3282 if ((mi
!= 0) || (hi
!= 0))
3286 } else if (type
->builtInType
== XML_SCHEMAS_UBYTE
) {
3287 if ((mi
!= 0) || (hi
!= 0))
3293 v
= xmlSchemaNewValue(type
->builtInType
);
3295 v
->value
.decimal
.lo
= lo
;
3296 v
->value
.decimal
.mi
= mi
;
3297 v
->value
.decimal
.hi
= hi
;
3298 v
->value
.decimal
.sign
= 0;
3299 v
->value
.decimal
.frac
= 0;
3300 v
->value
.decimal
.total
= ret
;
3331 * xmlSchemaValPredefTypeNode:
3332 * @type: the predefined type
3333 * @value: the value to check
3334 * @val: the return computed value
3335 * @node: the node containing the value
3337 * Check that a value conforms to the lexical space of the predefined type.
3338 * if true a value is computed and returned in @val.
3340 * Returns 0 if this validates, a positive error code number otherwise
3341 * and -1 in case of internal or API error.
3344 xmlSchemaValPredefTypeNode(xmlSchemaTypePtr type
, const xmlChar
*value
,
3345 xmlSchemaValPtr
*val
, xmlNodePtr node
) {
3346 return(xmlSchemaValAtomicType(type
, value
, val
, node
, 0,
3347 XML_SCHEMA_WHITESPACE_UNKNOWN
, 1, 1, 0));
3351 * xmlSchemaValPredefTypeNodeNoNorm:
3352 * @type: the predefined type
3353 * @value: the value to check
3354 * @val: the return computed value
3355 * @node: the node containing the value
3357 * Check that a value conforms to the lexical space of the predefined type.
3358 * if true a value is computed and returned in @val.
3359 * This one does apply any normalization to the value.
3361 * Returns 0 if this validates, a positive error code number otherwise
3362 * and -1 in case of internal or API error.
3365 xmlSchemaValPredefTypeNodeNoNorm(xmlSchemaTypePtr type
, const xmlChar
*value
,
3366 xmlSchemaValPtr
*val
, xmlNodePtr node
) {
3367 return(xmlSchemaValAtomicType(type
, value
, val
, node
, 1,
3368 XML_SCHEMA_WHITESPACE_UNKNOWN
, 1, 0, 1));
3372 * xmlSchemaValidatePredefinedType:
3373 * @type: the predefined type
3374 * @value: the value to check
3375 * @val: the return computed value
3377 * Check that a value conforms to the lexical space of the predefined type.
3378 * if true a value is computed and returned in @val.
3380 * Returns 0 if this validates, a positive error code number otherwise
3381 * and -1 in case of internal or API error.
3384 xmlSchemaValidatePredefinedType(xmlSchemaTypePtr type
, const xmlChar
*value
,
3385 xmlSchemaValPtr
*val
) {
3386 return(xmlSchemaValPredefTypeNode(type
, value
, val
, NULL
));
3390 * xmlSchemaCompareDecimals:
3391 * @x: a first decimal value
3392 * @y: a second decimal value
3394 * Compare 2 decimals
3396 * Returns -1 if x < y, 0 if x == y, 1 if x > y and -2 in case of error
3399 xmlSchemaCompareDecimals(xmlSchemaValPtr x
, xmlSchemaValPtr y
)
3401 xmlSchemaValPtr swp
;
3402 int order
= 1, integx
, integy
, dlen
;
3403 unsigned long hi
, mi
, lo
;
3406 * First test: If x is -ve and not zero
3408 if ((x
->value
.decimal
.sign
) &&
3409 ((x
->value
.decimal
.lo
!= 0) ||
3410 (x
->value
.decimal
.mi
!= 0) ||
3411 (x
->value
.decimal
.hi
!= 0))) {
3413 * Then if y is -ve and not zero reverse the compare
3415 if ((y
->value
.decimal
.sign
) &&
3416 ((y
->value
.decimal
.lo
!= 0) ||
3417 (y
->value
.decimal
.mi
!= 0) ||
3418 (y
->value
.decimal
.hi
!= 0)))
3421 * Otherwise (y >= 0) we have the answer
3426 * If x is not -ve and y is -ve we have the answer
3428 } else if ((y
->value
.decimal
.sign
) &&
3429 ((y
->value
.decimal
.lo
!= 0) ||
3430 (y
->value
.decimal
.mi
!= 0) ||
3431 (y
->value
.decimal
.hi
!= 0))) {
3435 * If it's not simply determined by a difference in sign,
3436 * then we need to compare the actual values of the two nums.
3437 * To do this, we start by looking at the integral parts.
3438 * If the number of integral digits differ, then we have our
3441 integx
= x
->value
.decimal
.total
- x
->value
.decimal
.frac
;
3442 integy
= y
->value
.decimal
.total
- y
->value
.decimal
.frac
;
3444 * NOTE: We changed the "total" for values like "0.1"
3445 * (or "-0.1" or ".1") to be 1, which was 2 previously.
3446 * Therefore the special case, when such values are
3447 * compared with 0, needs to be handled separately;
3448 * otherwise a zero would be recognized incorrectly as
3449 * greater than those values. This has the nice side effect
3450 * that we gain an overall optimized comparison with zeroes.
3451 * Note that a "0" has a "total" of 1 already.
3454 if (x
->value
.decimal
.lo
== 0) {
3457 else if (y
->value
.decimal
.lo
!= 0)
3464 if (y
->value
.decimal
.lo
== 0) {
3467 else if (x
->value
.decimal
.lo
!= 0)
3474 if (integx
> integy
)
3476 else if (integy
> integx
)
3480 * If the number of integral digits is the same for both numbers,
3481 * then things get a little more complicated. We need to "normalize"
3482 * the numbers in order to properly compare them. To do this, we
3483 * look at the total length of each number (length => number of
3484 * significant digits), and divide the "shorter" by 10 (decreasing
3485 * the length) until they are of equal length.
3487 dlen
= x
->value
.decimal
.total
- y
->value
.decimal
.total
;
3488 if (dlen
< 0) { /* y has more digits than x */
3490 hi
= y
->value
.decimal
.hi
;
3491 mi
= y
->value
.decimal
.mi
;
3492 lo
= y
->value
.decimal
.lo
;
3495 } else { /* x has more digits than y */
3497 hi
= x
->value
.decimal
.hi
;
3498 mi
= x
->value
.decimal
.mi
;
3499 lo
= x
->value
.decimal
.lo
;
3501 while (dlen
> 8) { /* in effect, right shift by 10**8 */
3508 unsigned long rem1
, rem2
;
3509 rem1
= (hi
% 10) * 100000000L;
3511 rem2
= (mi
% 10) * 100000000L;
3512 mi
= (mi
+ rem1
) / 10;
3513 lo
= (lo
+ rem2
) / 10;
3516 if (hi
> swp
->value
.decimal
.hi
) {
3518 } else if (hi
== swp
->value
.decimal
.hi
) {
3519 if (mi
> swp
->value
.decimal
.mi
) {
3521 } else if (mi
== swp
->value
.decimal
.mi
) {
3522 if (lo
> swp
->value
.decimal
.lo
) {
3524 } else if (lo
== swp
->value
.decimal
.lo
) {
3525 if (x
->value
.decimal
.total
== y
->value
.decimal
.total
) {
3537 * xmlSchemaCompareDurations:
3538 * @x: a first duration value
3539 * @y: a second duration value
3541 * Compare 2 durations
3543 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3547 xmlSchemaCompareDurations(xmlSchemaValPtr x
, xmlSchemaValPtr y
)
3549 long carry
, mon
, day
;
3552 long xmon
, xday
, myear
, minday
, maxday
;
3553 static const long dayRange
[2][12] = {
3554 { 0, 28, 59, 89, 120, 150, 181, 212, 242, 273, 303, 334, },
3555 { 0, 31, 62, 92, 123, 153, 184, 215, 245, 276, 306, 337} };
3557 if ((x
== NULL
) || (y
== NULL
))
3561 mon
= x
->value
.dur
.mon
- y
->value
.dur
.mon
;
3564 sec
= x
->value
.dur
.sec
- y
->value
.dur
.sec
;
3565 carry
= (long)sec
/ SECS_PER_DAY
;
3566 sec
-= (double)(carry
* SECS_PER_DAY
);
3569 day
= x
->value
.dur
.day
- y
->value
.dur
.day
+ carry
;
3587 if ((day
>= 0) && (sec
>= 0.0))
3593 } else if ((day
<= 0) && (sec
<= 0.0)) {
3606 maxday
= 366 * ((myear
+ 3) / 4) +
3607 365 * ((myear
- 1) % 4);
3608 minday
= maxday
- 1;
3612 minday
+= dayRange
[0][xmon
];
3613 maxday
+= dayRange
[1][xmon
];
3615 if ((maxday
== minday
) && (maxday
== xday
))
3616 return(0); /* can this really happen ? */
3627 * macros for adding date/times and durations
3629 #define FQUOTIENT(a,b) (floor(((double)a/(double)b)))
3630 #define MODULO(a,b) (a - FQUOTIENT(a,b) * b)
3631 #define FQUOTIENT_RANGE(a,low,high) (FQUOTIENT((a-low),(high-low)))
3632 #define MODULO_RANGE(a,low,high) ((MODULO((a-low),(high-low)))+low)
3636 * @v: the #xmlSchemaValPtr value to duplicate
3638 * Makes a copy of @v. The calling program is responsible for freeing
3639 * the returned value.
3641 * returns a pointer to a duplicated #xmlSchemaValPtr or NULL if error.
3643 static xmlSchemaValPtr
3644 xmlSchemaDupVal (xmlSchemaValPtr v
)
3646 xmlSchemaValPtr ret
= xmlSchemaNewValue(v
->type
);
3650 memcpy(ret
, v
, sizeof(xmlSchemaVal
));
3656 * xmlSchemaCopyValue:
3657 * @val: the precomputed value to be copied
3659 * Copies the precomputed value. This duplicates any string within.
3661 * Returns the copy or NULL if a copy for a data-type is not implemented.
3664 xmlSchemaCopyValue(xmlSchemaValPtr val
)
3666 xmlSchemaValPtr ret
= NULL
, prev
= NULL
, cur
;
3669 * Copy the string values.
3671 while (val
!= NULL
) {
3672 switch (val
->type
) {
3673 case XML_SCHEMAS_ANYTYPE
:
3674 case XML_SCHEMAS_IDREFS
:
3675 case XML_SCHEMAS_ENTITIES
:
3676 case XML_SCHEMAS_NMTOKENS
:
3677 xmlSchemaFreeValue(ret
);
3679 case XML_SCHEMAS_ANYSIMPLETYPE
:
3680 case XML_SCHEMAS_STRING
:
3681 case XML_SCHEMAS_NORMSTRING
:
3682 case XML_SCHEMAS_TOKEN
:
3683 case XML_SCHEMAS_LANGUAGE
:
3684 case XML_SCHEMAS_NAME
:
3685 case XML_SCHEMAS_NCNAME
:
3686 case XML_SCHEMAS_ID
:
3687 case XML_SCHEMAS_IDREF
:
3688 case XML_SCHEMAS_ENTITY
:
3689 case XML_SCHEMAS_NMTOKEN
:
3690 case XML_SCHEMAS_ANYURI
:
3691 cur
= xmlSchemaDupVal(val
);
3692 if (val
->value
.str
!= NULL
)
3693 cur
->value
.str
= xmlStrdup(BAD_CAST val
->value
.str
);
3695 case XML_SCHEMAS_QNAME
:
3696 case XML_SCHEMAS_NOTATION
:
3697 cur
= xmlSchemaDupVal(val
);
3698 if (val
->value
.qname
.name
!= NULL
)
3699 cur
->value
.qname
.name
=
3700 xmlStrdup(BAD_CAST val
->value
.qname
.name
);
3701 if (val
->value
.qname
.uri
!= NULL
)
3702 cur
->value
.qname
.uri
=
3703 xmlStrdup(BAD_CAST val
->value
.qname
.uri
);
3705 case XML_SCHEMAS_HEXBINARY
:
3706 cur
= xmlSchemaDupVal(val
);
3707 if (val
->value
.hex
.str
!= NULL
)
3708 cur
->value
.hex
.str
= xmlStrdup(BAD_CAST val
->value
.hex
.str
);
3710 case XML_SCHEMAS_BASE64BINARY
:
3711 cur
= xmlSchemaDupVal(val
);
3712 if (val
->value
.base64
.str
!= NULL
)
3713 cur
->value
.base64
.str
=
3714 xmlStrdup(BAD_CAST val
->value
.base64
.str
);
3717 cur
= xmlSchemaDupVal(val
);
3731 * _xmlSchemaDateAdd:
3732 * @dt: an #xmlSchemaValPtr
3733 * @dur: an #xmlSchemaValPtr of type #XS_DURATION
3735 * Compute a new date/time from @dt and @dur. This function assumes @dt
3736 * is either #XML_SCHEMAS_DATETIME, #XML_SCHEMAS_DATE, #XML_SCHEMAS_GYEARMONTH,
3737 * or #XML_SCHEMAS_GYEAR. The returned #xmlSchemaVal is the same type as
3738 * @dt. The calling program is responsible for freeing the returned value.
3740 * Returns a pointer to a new #xmlSchemaVal or NULL if error.
3742 static xmlSchemaValPtr
3743 _xmlSchemaDateAdd (xmlSchemaValPtr dt
, xmlSchemaValPtr dur
)
3745 xmlSchemaValPtr ret
, tmp
;
3746 long carry
, tempdays
, temp
;
3747 xmlSchemaValDatePtr r
, d
;
3748 xmlSchemaValDurationPtr u
;
3750 if ((dt
== NULL
) || (dur
== NULL
))
3753 ret
= xmlSchemaNewValue(dt
->type
);
3757 /* make a copy so we don't alter the original value */
3758 tmp
= xmlSchemaDupVal(dt
);
3760 xmlSchemaFreeValue(ret
);
3764 r
= &(ret
->value
.date
);
3765 d
= &(tmp
->value
.date
);
3766 u
= &(dur
->value
.dur
);
3772 /* normalize for time zone offset */
3773 u
->sec
-= (d
->tzo
* 60);
3781 carry
= d
->mon
+ u
->mon
;
3782 r
->mon
= (unsigned int) MODULO_RANGE(carry
, 1, 13);
3783 carry
= (long) FQUOTIENT_RANGE(carry
, 1, 13);
3785 /* year (may be modified later) */
3786 r
->year
= d
->year
+ carry
;
3796 r
->tz_flag
= d
->tz_flag
;
3799 r
->sec
= d
->sec
+ u
->sec
;
3800 carry
= (long) FQUOTIENT((long)r
->sec
, 60);
3801 if (r
->sec
!= 0.0) {
3802 r
->sec
= MODULO(r
->sec
, 60.0);
3807 r
->min
= (unsigned int) MODULO(carry
, 60);
3808 carry
= (long) FQUOTIENT(carry
, 60);
3812 r
->hour
= (unsigned int) MODULO(carry
, 24);
3813 carry
= (long)FQUOTIENT(carry
, 24);
3817 * Note we use tempdays because the temporary values may need more
3820 if ((VALID_YEAR(r
->year
)) && (VALID_MONTH(r
->mon
)) &&
3821 (d
->day
> MAX_DAYINMONTH(r
->year
, r
->mon
)))
3822 tempdays
= MAX_DAYINMONTH(r
->year
, r
->mon
);
3823 else if (d
->day
< 1)
3828 tempdays
+= u
->day
+ carry
;
3832 long tmon
= (long) MODULO_RANGE((int)r
->mon
-1, 1, 13);
3833 long tyr
= r
->year
+ (long)FQUOTIENT_RANGE((int)r
->mon
-1, 1, 13);
3837 * Coverity detected an overrun in daysInMonth
3838 * of size 12 at position 12 with index variable "((r)->mon - 1)"
3844 tempdays
+= MAX_DAYINMONTH(tyr
, tmon
);
3846 } else if (tempdays
> (long) MAX_DAYINMONTH(r
->year
, r
->mon
)) {
3847 tempdays
= tempdays
- MAX_DAYINMONTH(r
->year
, r
->mon
);
3852 temp
= r
->mon
+ carry
;
3853 r
->mon
= (unsigned int) MODULO_RANGE(temp
, 1, 13);
3854 r
->year
= r
->year
+ (unsigned int) FQUOTIENT_RANGE(temp
, 1, 13);
3866 * adjust the date/time type to the date values
3868 if (ret
->type
!= XML_SCHEMAS_DATETIME
) {
3869 if ((r
->hour
) || (r
->min
) || (r
->sec
))
3870 ret
->type
= XML_SCHEMAS_DATETIME
;
3871 else if (ret
->type
!= XML_SCHEMAS_DATE
) {
3872 if ((r
->mon
!= 1) && (r
->day
!= 1))
3873 ret
->type
= XML_SCHEMAS_DATE
;
3874 else if ((ret
->type
!= XML_SCHEMAS_GYEARMONTH
) && (r
->mon
!= 1))
3875 ret
->type
= XML_SCHEMAS_GYEARMONTH
;
3879 xmlSchemaFreeValue(tmp
);
3885 * xmlSchemaDateNormalize:
3886 * @dt: an #xmlSchemaValPtr of a date/time type value.
3887 * @offset: number of seconds to adjust @dt by.
3889 * Normalize @dt to GMT time. The @offset parameter is subtracted from
3890 * the return value is a time-zone offset is present on @dt.
3892 * Returns a normalized copy of @dt or NULL if error.
3894 static xmlSchemaValPtr
3895 xmlSchemaDateNormalize (xmlSchemaValPtr dt
, double offset
)
3897 xmlSchemaValPtr dur
, ret
;
3902 if (((dt
->type
!= XML_SCHEMAS_TIME
) &&
3903 (dt
->type
!= XML_SCHEMAS_DATETIME
) &&
3904 (dt
->type
!= XML_SCHEMAS_DATE
)) || (dt
->value
.date
.tzo
== 0))
3905 return xmlSchemaDupVal(dt
);
3907 dur
= xmlSchemaNewValue(XML_SCHEMAS_DURATION
);
3911 dur
->value
.date
.sec
-= offset
;
3913 ret
= _xmlSchemaDateAdd(dt
, dur
);
3917 xmlSchemaFreeValue(dur
);
3919 /* ret->value.date.tzo = 0; */
3924 * _xmlSchemaDateCastYMToDays:
3925 * @dt: an #xmlSchemaValPtr
3927 * Convert mon and year of @dt to total number of days. Take the
3928 * number of years since (or before) 1 AD and add the number of leap
3929 * years. This is a function because negative
3930 * years must be handled a little differently and there is no zero year.
3932 * Returns number of days.
3935 _xmlSchemaDateCastYMToDays (const xmlSchemaValPtr dt
)
3940 mon
= dt
->value
.date
.mon
;
3941 if (mon
<= 0) mon
= 1; /* normalization */
3943 if (dt
->value
.date
.year
<= 0)
3944 ret
= (dt
->value
.date
.year
* 365) +
3945 (((dt
->value
.date
.year
+1)/4)-((dt
->value
.date
.year
+1)/100)+
3946 ((dt
->value
.date
.year
+1)/400)) +
3947 DAY_IN_YEAR(0, mon
, dt
->value
.date
.year
);
3949 ret
= ((dt
->value
.date
.year
-1) * 365) +
3950 (((dt
->value
.date
.year
-1)/4)-((dt
->value
.date
.year
-1)/100)+
3951 ((dt
->value
.date
.year
-1)/400)) +
3952 DAY_IN_YEAR(0, mon
, dt
->value
.date
.year
);
3959 * @dt: an #xmlSchemaValPtr
3961 * Calculates the number of seconds in the time portion of @dt.
3965 #define TIME_TO_NUMBER(dt) \
3966 ((double)((dt->value.date.hour * SECS_PER_HOUR) + \
3967 (dt->value.date.min * SECS_PER_MIN) + \
3968 (dt->value.date.tzo * SECS_PER_MIN)) + \
3972 * xmlSchemaCompareDates:
3973 * @x: a first date/time value
3974 * @y: a second date/time value
3976 * Compare 2 date/times
3978 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
3982 xmlSchemaCompareDates (xmlSchemaValPtr x
, xmlSchemaValPtr y
)
3984 unsigned char xmask
, ymask
, xor_mask
, and_mask
;
3985 xmlSchemaValPtr p1
, p2
, q1
, q2
;
3986 long p1d
, p2d
, q1d
, q2d
;
3988 if ((x
== NULL
) || (y
== NULL
))
3991 if (x
->value
.date
.tz_flag
) {
3993 if (!y
->value
.date
.tz_flag
) {
3994 p1
= xmlSchemaDateNormalize(x
, 0);
3995 p1d
= _xmlSchemaDateCastYMToDays(p1
) + p1
->value
.date
.day
;
3996 /* normalize y + 14:00 */
3997 q1
= xmlSchemaDateNormalize(y
, (14 * SECS_PER_HOUR
));
3999 q1d
= _xmlSchemaDateCastYMToDays(q1
) + q1
->value
.date
.day
;
4001 xmlSchemaFreeValue(p1
);
4002 xmlSchemaFreeValue(q1
);
4004 } else if (p1d
== q1d
) {
4007 sec
= TIME_TO_NUMBER(p1
) - TIME_TO_NUMBER(q1
);
4009 xmlSchemaFreeValue(p1
);
4010 xmlSchemaFreeValue(q1
);
4014 /* normalize y - 14:00 */
4015 q2
= xmlSchemaDateNormalize(y
, -(14 * SECS_PER_HOUR
));
4016 q2d
= _xmlSchemaDateCastYMToDays(q2
) + q2
->value
.date
.day
;
4019 else if (p1d
== q2d
) {
4020 sec
= TIME_TO_NUMBER(p1
) - TIME_TO_NUMBER(q2
);
4024 ret
= 2; /* indeterminate */
4026 xmlSchemaFreeValue(p1
);
4027 xmlSchemaFreeValue(q1
);
4028 xmlSchemaFreeValue(q2
);
4033 xmlSchemaFreeValue(p1
);
4034 xmlSchemaFreeValue(q1
);
4037 } else if (y
->value
.date
.tz_flag
) {
4038 q1
= xmlSchemaDateNormalize(y
, 0);
4039 q1d
= _xmlSchemaDateCastYMToDays(q1
) + q1
->value
.date
.day
;
4041 /* normalize x - 14:00 */
4042 p1
= xmlSchemaDateNormalize(x
, -(14 * SECS_PER_HOUR
));
4043 p1d
= _xmlSchemaDateCastYMToDays(p1
) + p1
->value
.date
.day
;
4046 xmlSchemaFreeValue(p1
);
4047 xmlSchemaFreeValue(q1
);
4049 } else if (p1d
== q1d
) {
4052 sec
= TIME_TO_NUMBER(p1
) - TIME_TO_NUMBER(q1
);
4054 xmlSchemaFreeValue(p1
);
4055 xmlSchemaFreeValue(q1
);
4059 /* normalize x + 14:00 */
4060 p2
= xmlSchemaDateNormalize(x
, (14 * SECS_PER_HOUR
));
4061 p2d
= _xmlSchemaDateCastYMToDays(p2
) + p2
->value
.date
.day
;
4065 } else if (p2d
== q1d
) {
4066 sec
= TIME_TO_NUMBER(p2
) - TIME_TO_NUMBER(q1
);
4070 ret
= 2; /* indeterminate */
4072 xmlSchemaFreeValue(p1
);
4073 xmlSchemaFreeValue(q1
);
4074 xmlSchemaFreeValue(p2
);
4079 xmlSchemaFreeValue(p1
);
4080 xmlSchemaFreeValue(q1
);
4085 * if the same type then calculate the difference
4087 if (x
->type
== y
->type
) {
4089 q1
= xmlSchemaDateNormalize(y
, 0);
4090 q1d
= _xmlSchemaDateCastYMToDays(q1
) + q1
->value
.date
.day
;
4092 p1
= xmlSchemaDateNormalize(x
, 0);
4093 p1d
= _xmlSchemaDateCastYMToDays(p1
) + p1
->value
.date
.day
;
4097 } else if (p1d
> q1d
) {
4102 sec
= TIME_TO_NUMBER(p1
) - TIME_TO_NUMBER(q1
);
4109 xmlSchemaFreeValue(p1
);
4110 xmlSchemaFreeValue(q1
);
4115 case XML_SCHEMAS_DATETIME
:
4118 case XML_SCHEMAS_DATE
:
4121 case XML_SCHEMAS_GYEAR
:
4124 case XML_SCHEMAS_GMONTH
:
4127 case XML_SCHEMAS_GDAY
:
4130 case XML_SCHEMAS_GYEARMONTH
:
4133 case XML_SCHEMAS_GMONTHDAY
:
4136 case XML_SCHEMAS_TIME
:
4145 case XML_SCHEMAS_DATETIME
:
4148 case XML_SCHEMAS_DATE
:
4151 case XML_SCHEMAS_GYEAR
:
4154 case XML_SCHEMAS_GMONTH
:
4157 case XML_SCHEMAS_GDAY
:
4160 case XML_SCHEMAS_GYEARMONTH
:
4163 case XML_SCHEMAS_GMONTHDAY
:
4166 case XML_SCHEMAS_TIME
:
4174 xor_mask
= xmask
^ ymask
; /* mark type differences */
4175 and_mask
= xmask
& ymask
; /* mark field specification */
4179 return 2; /* indeterminate */
4180 else if (and_mask
& 1) {
4181 if (x
->value
.date
.year
< y
->value
.date
.year
)
4183 else if (x
->value
.date
.year
> y
->value
.date
.year
)
4189 return 2; /* indeterminate */
4190 else if (and_mask
& 2) {
4191 if (x
->value
.date
.mon
< y
->value
.date
.mon
)
4193 else if (x
->value
.date
.mon
> y
->value
.date
.mon
)
4199 return 2; /* indeterminate */
4200 else if (and_mask
& 4) {
4201 if (x
->value
.date
.day
< y
->value
.date
.day
)
4203 else if (x
->value
.date
.day
> y
->value
.date
.day
)
4209 return 2; /* indeterminate */
4210 else if (and_mask
& 8) {
4211 if (x
->value
.date
.hour
< y
->value
.date
.hour
)
4213 else if (x
->value
.date
.hour
> y
->value
.date
.hour
)
4215 else if (x
->value
.date
.min
< y
->value
.date
.min
)
4217 else if (x
->value
.date
.min
> y
->value
.date
.min
)
4219 else if (x
->value
.date
.sec
< y
->value
.date
.sec
)
4221 else if (x
->value
.date
.sec
> y
->value
.date
.sec
)
4229 * xmlSchemaComparePreserveReplaceStrings:
4230 * @x: a first string value
4231 * @y: a second string value
4232 * @invert: inverts the result if x < y or x > y.
4234 * Compare 2 string for their normalized values.
4235 * @x is a string with whitespace of "preserve", @y is
4236 * a string with a whitespace of "replace". I.e. @x could
4237 * be an "xsd:string" and @y an "xsd:normalizedString".
4239 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4243 xmlSchemaComparePreserveReplaceStrings(const xmlChar
*x
,
4249 while ((*x
!= 0) && (*y
!= 0)) {
4250 if (IS_WSP_REPLACE_CH(*y
)) {
4251 if (! IS_WSP_SPACE_CH(*x
)) {
4252 if ((*x
- 0x20) < 0) {
4298 * xmlSchemaComparePreserveCollapseStrings:
4299 * @x: a first string value
4300 * @y: a second string value
4302 * Compare 2 string for their normalized values.
4303 * @x is a string with whitespace of "preserve", @y is
4304 * a string with a whitespace of "collapse". I.e. @x could
4305 * be an "xsd:string" and @y an "xsd:normalizedString".
4307 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4311 xmlSchemaComparePreserveCollapseStrings(const xmlChar
*x
,
4318 * Skip leading blank chars of the collapsed string.
4320 while IS_WSP_BLANK_CH(*y
)
4323 while ((*x
!= 0) && (*y
!= 0)) {
4324 if IS_WSP_BLANK_CH(*y
) {
4325 if (! IS_WSP_SPACE_CH(*x
)) {
4327 * The yv character would have been replaced to 0x20.
4329 if ((*x
- 0x20) < 0) {
4344 * Skip contiguous blank chars of the collapsed string.
4346 while IS_WSP_BLANK_CH(*y
)
4372 * Skip trailing blank chars of the collapsed string.
4374 while IS_WSP_BLANK_CH(*y
)
4387 * xmlSchemaComparePreserveCollapseStrings:
4388 * @x: a first string value
4389 * @y: a second string value
4391 * Compare 2 string for their normalized values.
4392 * @x is a string with whitespace of "preserve", @y is
4393 * a string with a whitespace of "collapse". I.e. @x could
4394 * be an "xsd:string" and @y an "xsd:normalizedString".
4396 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4400 xmlSchemaCompareReplaceCollapseStrings(const xmlChar
*x
,
4407 * Skip leading blank chars of the collapsed string.
4409 while IS_WSP_BLANK_CH(*y
)
4412 while ((*x
!= 0) && (*y
!= 0)) {
4413 if IS_WSP_BLANK_CH(*y
) {
4414 if (! IS_WSP_BLANK_CH(*x
)) {
4416 * The yv character would have been replaced to 0x20.
4418 if ((*x
- 0x20) < 0) {
4433 * Skip contiguous blank chars of the collapsed string.
4435 while IS_WSP_BLANK_CH(*y
)
4438 if IS_WSP_BLANK_CH(*x
) {
4440 * The xv character would have been replaced to 0x20.
4442 if ((0x20 - *y
) < 0) {
4469 * Skip trailing blank chars of the collapsed string.
4471 while IS_WSP_BLANK_CH(*y
)
4485 * xmlSchemaCompareReplacedStrings:
4486 * @x: a first string value
4487 * @y: a second string value
4489 * Compare 2 string for their normalized values.
4491 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4495 xmlSchemaCompareReplacedStrings(const xmlChar
*x
,
4500 while ((*x
!= 0) && (*y
!= 0)) {
4501 if IS_WSP_BLANK_CH(*y
) {
4502 if (! IS_WSP_BLANK_CH(*x
)) {
4503 if ((*x
- 0x20) < 0)
4509 if IS_WSP_BLANK_CH(*x
) {
4510 if ((0x20 - *y
) < 0)
4532 * xmlSchemaCompareNormStrings:
4533 * @x: a first string value
4534 * @y: a second string value
4536 * Compare 2 string for their normalized values.
4538 * Returns -1 if x < y, 0 if x == y, 1 if x > y, and -2 in
4542 xmlSchemaCompareNormStrings(const xmlChar
*x
,
4546 while (IS_BLANK_CH(*x
)) x
++;
4547 while (IS_BLANK_CH(*y
)) y
++;
4548 while ((*x
!= 0) && (*y
!= 0)) {
4549 if (IS_BLANK_CH(*x
)) {
4550 if (!IS_BLANK_CH(*y
)) {
4554 while (IS_BLANK_CH(*x
)) x
++;
4555 while (IS_BLANK_CH(*y
)) y
++;
4565 while (IS_BLANK_CH(*x
)) x
++;
4570 while (IS_BLANK_CH(*y
)) y
++;
4578 * xmlSchemaCompareFloats:
4579 * @x: a first float or double value
4580 * @y: a second float or double value
4584 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4588 xmlSchemaCompareFloats(xmlSchemaValPtr x
, xmlSchemaValPtr y
) {
4591 if ((x
== NULL
) || (y
== NULL
))
4595 * Cast everything to doubles.
4597 if (x
->type
== XML_SCHEMAS_DOUBLE
)
4599 else if (x
->type
== XML_SCHEMAS_FLOAT
)
4604 if (y
->type
== XML_SCHEMAS_DOUBLE
)
4606 else if (y
->type
== XML_SCHEMAS_FLOAT
)
4612 * Check for special cases.
4614 if (xmlXPathIsNaN(d1
)) {
4615 if (xmlXPathIsNaN(d2
))
4619 if (xmlXPathIsNaN(d2
))
4621 if (d1
== xmlXPathPINF
) {
4622 if (d2
== xmlXPathPINF
)
4626 if (d2
== xmlXPathPINF
)
4628 if (d1
== xmlXPathNINF
) {
4629 if (d2
== xmlXPathNINF
)
4633 if (d2
== xmlXPathNINF
)
4637 * basic tests, the last one we should have equality, but
4638 * portability is more important than speed and handling
4639 * NaN or Inf in a portable way is always a challenge, so ...
4651 * xmlSchemaCompareValues:
4653 * @xvalue: the first value as a string (optional)
4654 * @xwtsp: the whitespace type
4655 * @y: a second value
4656 * @xvalue: the second value as a string (optional)
4657 * @ywtsp: the whitespace type
4661 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, 3 if not
4662 * comparable and -2 in case of error
4665 xmlSchemaCompareValuesInternal(xmlSchemaValType xtype
,
4667 const xmlChar
*xvalue
,
4668 xmlSchemaWhitespaceValueType xws
,
4669 xmlSchemaValType ytype
,
4671 const xmlChar
*yvalue
,
4672 xmlSchemaWhitespaceValueType yws
)
4675 case XML_SCHEMAS_UNKNOWN
:
4676 case XML_SCHEMAS_ANYTYPE
:
4678 case XML_SCHEMAS_INTEGER
:
4679 case XML_SCHEMAS_NPINTEGER
:
4680 case XML_SCHEMAS_NINTEGER
:
4681 case XML_SCHEMAS_NNINTEGER
:
4682 case XML_SCHEMAS_PINTEGER
:
4683 case XML_SCHEMAS_INT
:
4684 case XML_SCHEMAS_UINT
:
4685 case XML_SCHEMAS_LONG
:
4686 case XML_SCHEMAS_ULONG
:
4687 case XML_SCHEMAS_SHORT
:
4688 case XML_SCHEMAS_USHORT
:
4689 case XML_SCHEMAS_BYTE
:
4690 case XML_SCHEMAS_UBYTE
:
4691 case XML_SCHEMAS_DECIMAL
:
4692 if ((x
== NULL
) || (y
== NULL
))
4695 return(xmlSchemaCompareDecimals(x
, y
));
4696 if ((ytype
== XML_SCHEMAS_DECIMAL
) ||
4697 (ytype
== XML_SCHEMAS_INTEGER
) ||
4698 (ytype
== XML_SCHEMAS_NPINTEGER
) ||
4699 (ytype
== XML_SCHEMAS_NINTEGER
) ||
4700 (ytype
== XML_SCHEMAS_NNINTEGER
) ||
4701 (ytype
== XML_SCHEMAS_PINTEGER
) ||
4702 (ytype
== XML_SCHEMAS_INT
) ||
4703 (ytype
== XML_SCHEMAS_UINT
) ||
4704 (ytype
== XML_SCHEMAS_LONG
) ||
4705 (ytype
== XML_SCHEMAS_ULONG
) ||
4706 (ytype
== XML_SCHEMAS_SHORT
) ||
4707 (ytype
== XML_SCHEMAS_USHORT
) ||
4708 (ytype
== XML_SCHEMAS_BYTE
) ||
4709 (ytype
== XML_SCHEMAS_UBYTE
))
4710 return(xmlSchemaCompareDecimals(x
, y
));
4712 case XML_SCHEMAS_DURATION
:
4713 if ((x
== NULL
) || (y
== NULL
))
4715 if (ytype
== XML_SCHEMAS_DURATION
)
4716 return(xmlSchemaCompareDurations(x
, y
));
4718 case XML_SCHEMAS_TIME
:
4719 case XML_SCHEMAS_GDAY
:
4720 case XML_SCHEMAS_GMONTH
:
4721 case XML_SCHEMAS_GMONTHDAY
:
4722 case XML_SCHEMAS_GYEAR
:
4723 case XML_SCHEMAS_GYEARMONTH
:
4724 case XML_SCHEMAS_DATE
:
4725 case XML_SCHEMAS_DATETIME
:
4726 if ((x
== NULL
) || (y
== NULL
))
4728 if ((ytype
== XML_SCHEMAS_DATETIME
) ||
4729 (ytype
== XML_SCHEMAS_TIME
) ||
4730 (ytype
== XML_SCHEMAS_GDAY
) ||
4731 (ytype
== XML_SCHEMAS_GMONTH
) ||
4732 (ytype
== XML_SCHEMAS_GMONTHDAY
) ||
4733 (ytype
== XML_SCHEMAS_GYEAR
) ||
4734 (ytype
== XML_SCHEMAS_DATE
) ||
4735 (ytype
== XML_SCHEMAS_GYEARMONTH
))
4736 return (xmlSchemaCompareDates(x
, y
));
4739 * Note that we will support comparison of string types against
4740 * anySimpleType as well.
4742 case XML_SCHEMAS_ANYSIMPLETYPE
:
4743 case XML_SCHEMAS_STRING
:
4744 case XML_SCHEMAS_NORMSTRING
:
4745 case XML_SCHEMAS_TOKEN
:
4746 case XML_SCHEMAS_LANGUAGE
:
4747 case XML_SCHEMAS_NMTOKEN
:
4748 case XML_SCHEMAS_NAME
:
4749 case XML_SCHEMAS_NCNAME
:
4750 case XML_SCHEMAS_ID
:
4751 case XML_SCHEMAS_IDREF
:
4752 case XML_SCHEMAS_ENTITY
:
4753 case XML_SCHEMAS_ANYURI
:
4755 const xmlChar
*xv
, *yv
;
4766 * TODO: Compare those against QName.
4768 if (ytype
== XML_SCHEMAS_QNAME
) {
4774 if ((ytype
== XML_SCHEMAS_ANYSIMPLETYPE
) ||
4775 (ytype
== XML_SCHEMAS_STRING
) ||
4776 (ytype
== XML_SCHEMAS_NORMSTRING
) ||
4777 (ytype
== XML_SCHEMAS_TOKEN
) ||
4778 (ytype
== XML_SCHEMAS_LANGUAGE
) ||
4779 (ytype
== XML_SCHEMAS_NMTOKEN
) ||
4780 (ytype
== XML_SCHEMAS_NAME
) ||
4781 (ytype
== XML_SCHEMAS_NCNAME
) ||
4782 (ytype
== XML_SCHEMAS_ID
) ||
4783 (ytype
== XML_SCHEMAS_IDREF
) ||
4784 (ytype
== XML_SCHEMAS_ENTITY
) ||
4785 (ytype
== XML_SCHEMAS_ANYURI
)) {
4787 if (xws
== XML_SCHEMA_WHITESPACE_PRESERVE
) {
4789 if (yws
== XML_SCHEMA_WHITESPACE_PRESERVE
) {
4790 /* TODO: What about x < y or x > y. */
4791 if (xmlStrEqual(xv
, yv
))
4795 } else if (yws
== XML_SCHEMA_WHITESPACE_REPLACE
)
4796 return (xmlSchemaComparePreserveReplaceStrings(xv
, yv
, 0));
4797 else if (yws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
4798 return (xmlSchemaComparePreserveCollapseStrings(xv
, yv
, 0));
4800 } else if (xws
== XML_SCHEMA_WHITESPACE_REPLACE
) {
4802 if (yws
== XML_SCHEMA_WHITESPACE_PRESERVE
)
4803 return (xmlSchemaComparePreserveReplaceStrings(yv
, xv
, 1));
4804 if (yws
== XML_SCHEMA_WHITESPACE_REPLACE
)
4805 return (xmlSchemaCompareReplacedStrings(xv
, yv
));
4806 if (yws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
4807 return (xmlSchemaCompareReplaceCollapseStrings(xv
, yv
, 0));
4809 } else if (xws
== XML_SCHEMA_WHITESPACE_COLLAPSE
) {
4811 if (yws
== XML_SCHEMA_WHITESPACE_PRESERVE
)
4812 return (xmlSchemaComparePreserveCollapseStrings(yv
, xv
, 1));
4813 if (yws
== XML_SCHEMA_WHITESPACE_REPLACE
)
4814 return (xmlSchemaCompareReplaceCollapseStrings(yv
, xv
, 1));
4815 if (yws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
4816 return (xmlSchemaCompareNormStrings(xv
, yv
));
4823 case XML_SCHEMAS_QNAME
:
4824 case XML_SCHEMAS_NOTATION
:
4825 if ((x
== NULL
) || (y
== NULL
))
4827 if ((ytype
== XML_SCHEMAS_QNAME
) ||
4828 (ytype
== XML_SCHEMAS_NOTATION
)) {
4829 if ((xmlStrEqual(x
->value
.qname
.name
, y
->value
.qname
.name
)) &&
4830 (xmlStrEqual(x
->value
.qname
.uri
, y
->value
.qname
.uri
)))
4835 case XML_SCHEMAS_FLOAT
:
4836 case XML_SCHEMAS_DOUBLE
:
4837 if ((x
== NULL
) || (y
== NULL
))
4839 if ((ytype
== XML_SCHEMAS_FLOAT
) ||
4840 (ytype
== XML_SCHEMAS_DOUBLE
))
4841 return (xmlSchemaCompareFloats(x
, y
));
4843 case XML_SCHEMAS_BOOLEAN
:
4844 if ((x
== NULL
) || (y
== NULL
))
4846 if (ytype
== XML_SCHEMAS_BOOLEAN
) {
4847 if (x
->value
.b
== y
->value
.b
)
4849 if (x
->value
.b
== 0)
4854 case XML_SCHEMAS_HEXBINARY
:
4855 if ((x
== NULL
) || (y
== NULL
))
4857 if (ytype
== XML_SCHEMAS_HEXBINARY
) {
4858 if (x
->value
.hex
.total
== y
->value
.hex
.total
) {
4859 int ret
= xmlStrcmp(x
->value
.hex
.str
, y
->value
.hex
.str
);
4865 else if (x
->value
.hex
.total
> y
->value
.hex
.total
)
4871 case XML_SCHEMAS_BASE64BINARY
:
4872 if ((x
== NULL
) || (y
== NULL
))
4874 if (ytype
== XML_SCHEMAS_BASE64BINARY
) {
4875 if (x
->value
.base64
.total
== y
->value
.base64
.total
) {
4876 int ret
= xmlStrcmp(x
->value
.base64
.str
,
4877 y
->value
.base64
.str
);
4885 else if (x
->value
.base64
.total
> y
->value
.base64
.total
)
4891 case XML_SCHEMAS_IDREFS
:
4892 case XML_SCHEMAS_ENTITIES
:
4893 case XML_SCHEMAS_NMTOKENS
:
4901 * xmlSchemaCompareValues:
4903 * @y: a second value
4907 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4911 xmlSchemaCompareValues(xmlSchemaValPtr x
, xmlSchemaValPtr y
) {
4912 xmlSchemaWhitespaceValueType xws
, yws
;
4914 if ((x
== NULL
) || (y
== NULL
))
4916 if (x
->type
== XML_SCHEMAS_STRING
)
4917 xws
= XML_SCHEMA_WHITESPACE_PRESERVE
;
4918 else if (x
->type
== XML_SCHEMAS_NORMSTRING
)
4919 xws
= XML_SCHEMA_WHITESPACE_REPLACE
;
4921 xws
= XML_SCHEMA_WHITESPACE_COLLAPSE
;
4923 if (y
->type
== XML_SCHEMAS_STRING
)
4924 yws
= XML_SCHEMA_WHITESPACE_PRESERVE
;
4925 else if (x
->type
== XML_SCHEMAS_NORMSTRING
)
4926 yws
= XML_SCHEMA_WHITESPACE_REPLACE
;
4928 yws
= XML_SCHEMA_WHITESPACE_COLLAPSE
;
4930 return(xmlSchemaCompareValuesInternal(x
->type
, x
, NULL
, xws
, y
->type
,
4935 * xmlSchemaCompareValuesWhtsp:
4937 * @xws: the whitespace value of x
4938 * @y: a second value
4939 * @yws: the whitespace value of y
4943 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4947 xmlSchemaCompareValuesWhtsp(xmlSchemaValPtr x
,
4948 xmlSchemaWhitespaceValueType xws
,
4950 xmlSchemaWhitespaceValueType yws
)
4952 if ((x
== NULL
) || (y
== NULL
))
4954 return(xmlSchemaCompareValuesInternal(x
->type
, x
, NULL
, xws
, y
->type
,
4959 * xmlSchemaCompareValuesWhtspExt:
4961 * @xws: the whitespace value of x
4962 * @y: a second value
4963 * @yws: the whitespace value of y
4967 * Returns -1 if x < y, 0 if x == y, 1 if x > y, 2 if x <> y, and -2 in
4971 xmlSchemaCompareValuesWhtspExt(xmlSchemaValType xtype
,
4973 const xmlChar
*xvalue
,
4974 xmlSchemaWhitespaceValueType xws
,
4975 xmlSchemaValType ytype
,
4977 const xmlChar
*yvalue
,
4978 xmlSchemaWhitespaceValueType yws
)
4980 return(xmlSchemaCompareValuesInternal(xtype
, x
, xvalue
, xws
, ytype
, y
,
4988 * Computes the UTF8 length of the normalized value of the string
4990 * Returns the length or -1 in case of error.
4993 xmlSchemaNormLen(const xmlChar
*value
) {
5000 while (IS_BLANK_CH(*utf
)) utf
++;
5002 if (utf
[0] & 0x80) {
5003 if ((utf
[1] & 0xc0) != 0x80)
5005 if ((utf
[0] & 0xe0) == 0xe0) {
5006 if ((utf
[2] & 0xc0) != 0x80)
5008 if ((utf
[0] & 0xf0) == 0xf0) {
5009 if ((utf
[0] & 0xf8) != 0xf0 || (utf
[3] & 0xc0) != 0x80)
5018 } else if (IS_BLANK_CH(*utf
)) {
5019 while (IS_BLANK_CH(*utf
)) utf
++;
5031 * xmlSchemaGetFacetValueAsULong:
5032 * @facet: an schemas type facet
5034 * Extract the value of a facet
5036 * Returns the value as a long
5039 xmlSchemaGetFacetValueAsULong(xmlSchemaFacetPtr facet
)
5042 * TODO: Check if this is a decimal.
5046 return ((unsigned long) facet
->val
->value
.decimal
.lo
);
5050 * xmlSchemaValidateListSimpleTypeFacet:
5051 * @facet: the facet to check
5052 * @value: the lexical repr of the value to validate
5053 * @actualLen: the number of list items
5054 * @expectedLen: the resulting expected number of list items
5056 * Checks the value of a list simple type against a facet.
5058 * Returns 0 if the value is valid, a positive error code
5059 * number otherwise and -1 in case of an internal error.
5062 xmlSchemaValidateListSimpleTypeFacet(xmlSchemaFacetPtr facet
,
5063 const xmlChar
*value
,
5064 unsigned long actualLen
,
5065 unsigned long *expectedLen
)
5070 * TODO: Check if this will work with large numbers.
5071 * (compare value.decimal.mi and value.decimal.hi as well?).
5073 if (facet
->type
== XML_SCHEMA_FACET_LENGTH
) {
5074 if (actualLen
!= facet
->val
->value
.decimal
.lo
) {
5075 if (expectedLen
!= NULL
)
5076 *expectedLen
= facet
->val
->value
.decimal
.lo
;
5077 return (XML_SCHEMAV_CVC_LENGTH_VALID
);
5079 } else if (facet
->type
== XML_SCHEMA_FACET_MINLENGTH
) {
5080 if (actualLen
< facet
->val
->value
.decimal
.lo
) {
5081 if (expectedLen
!= NULL
)
5082 *expectedLen
= facet
->val
->value
.decimal
.lo
;
5083 return (XML_SCHEMAV_CVC_MINLENGTH_VALID
);
5085 } else if (facet
->type
== XML_SCHEMA_FACET_MAXLENGTH
) {
5086 if (actualLen
> facet
->val
->value
.decimal
.lo
) {
5087 if (expectedLen
!= NULL
)
5088 *expectedLen
= facet
->val
->value
.decimal
.lo
;
5089 return (XML_SCHEMAV_CVC_MAXLENGTH_VALID
);
5093 * NOTE: That we can pass NULL as xmlSchemaValPtr to
5094 * xmlSchemaValidateFacet, since the remaining facet types
5095 * are: XML_SCHEMA_FACET_PATTERN, XML_SCHEMA_FACET_ENUMERATION.
5097 return(xmlSchemaValidateFacet(NULL
, facet
, value
, NULL
));
5102 * xmlSchemaValidateLengthFacet:
5103 * @type: the built-in type
5104 * @facet: the facet to check
5105 * @value: the lexical repr. of the value to be validated
5106 * @val: the precomputed value
5107 * @ws: the whitespace type of the value
5108 * @length: the actual length of the value
5110 * Checka a value against a "length", "minLength" and "maxLength"
5111 * facet; sets @length to the computed length of @value.
5113 * Returns 0 if the value is valid, a positive error code
5114 * otherwise and -1 in case of an internal or API error.
5117 xmlSchemaValidateLengthFacetInternal(xmlSchemaFacetPtr facet
,
5118 xmlSchemaValType valType
,
5119 const xmlChar
*value
,
5120 xmlSchemaValPtr val
,
5121 unsigned long *length
,
5122 xmlSchemaWhitespaceValueType ws
)
5124 unsigned int len
= 0;
5126 if ((length
== NULL
) || (facet
== NULL
))
5129 if ((facet
->type
!= XML_SCHEMA_FACET_LENGTH
) &&
5130 (facet
->type
!= XML_SCHEMA_FACET_MAXLENGTH
) &&
5131 (facet
->type
!= XML_SCHEMA_FACET_MINLENGTH
))
5135 * TODO: length, maxLength and minLength must be of type
5136 * nonNegativeInteger only. Check if decimal is used somehow.
5138 if ((facet
->val
== NULL
) ||
5139 ((facet
->val
->type
!= XML_SCHEMAS_DECIMAL
) &&
5140 (facet
->val
->type
!= XML_SCHEMAS_NNINTEGER
)) ||
5141 (facet
->val
->value
.decimal
.frac
!= 0)) {
5144 if ((val
!= NULL
) && (val
->type
== XML_SCHEMAS_HEXBINARY
))
5145 len
= val
->value
.hex
.total
;
5146 else if ((val
!= NULL
) && (val
->type
== XML_SCHEMAS_BASE64BINARY
))
5147 len
= val
->value
.base64
.total
;
5150 case XML_SCHEMAS_STRING
:
5151 case XML_SCHEMAS_NORMSTRING
:
5152 if (ws
== XML_SCHEMA_WHITESPACE_UNKNOWN
) {
5154 * This is to ensure API compatibility with the old
5155 * xmlSchemaValidateLengthFacet(). Anyway, this was and
5156 * is not the correct handling.
5157 * TODO: Get rid of this case somehow.
5159 if (valType
== XML_SCHEMAS_STRING
)
5160 len
= xmlUTF8Strlen(value
);
5162 len
= xmlSchemaNormLen(value
);
5163 } else if (value
!= NULL
) {
5164 if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
5165 len
= xmlSchemaNormLen(value
);
5168 * Should be OK for "preserve" as well.
5170 len
= xmlUTF8Strlen(value
);
5173 case XML_SCHEMAS_IDREF
:
5174 case XML_SCHEMAS_TOKEN
:
5175 case XML_SCHEMAS_LANGUAGE
:
5176 case XML_SCHEMAS_NMTOKEN
:
5177 case XML_SCHEMAS_NAME
:
5178 case XML_SCHEMAS_NCNAME
:
5179 case XML_SCHEMAS_ID
:
5181 * FIXME: What exactly to do with anyURI?
5183 case XML_SCHEMAS_ANYURI
:
5185 len
= xmlSchemaNormLen(value
);
5187 case XML_SCHEMAS_QNAME
:
5188 case XML_SCHEMAS_NOTATION
:
5190 * For QName and NOTATION, those facets are
5191 * deprecated and should be ignored.
5198 *length
= (unsigned long) len
;
5200 * TODO: Return the whole expected value, i.e. "lo", "mi" and "hi".
5202 if (facet
->type
== XML_SCHEMA_FACET_LENGTH
) {
5203 if (len
!= facet
->val
->value
.decimal
.lo
)
5204 return(XML_SCHEMAV_CVC_LENGTH_VALID
);
5205 } else if (facet
->type
== XML_SCHEMA_FACET_MINLENGTH
) {
5206 if (len
< facet
->val
->value
.decimal
.lo
)
5207 return(XML_SCHEMAV_CVC_MINLENGTH_VALID
);
5209 if (len
> facet
->val
->value
.decimal
.lo
)
5210 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID
);
5217 * xmlSchemaValidateLengthFacet:
5218 * @type: the built-in type
5219 * @facet: the facet to check
5220 * @value: the lexical repr. of the value to be validated
5221 * @val: the precomputed value
5222 * @length: the actual length of the value
5224 * Checka a value against a "length", "minLength" and "maxLength"
5225 * facet; sets @length to the computed length of @value.
5227 * Returns 0 if the value is valid, a positive error code
5228 * otherwise and -1 in case of an internal or API error.
5231 xmlSchemaValidateLengthFacet(xmlSchemaTypePtr type
,
5232 xmlSchemaFacetPtr facet
,
5233 const xmlChar
*value
,
5234 xmlSchemaValPtr val
,
5235 unsigned long *length
)
5239 return (xmlSchemaValidateLengthFacetInternal(facet
,
5240 type
->builtInType
, value
, val
, length
,
5241 XML_SCHEMA_WHITESPACE_UNKNOWN
));
5245 * xmlSchemaValidateLengthFacetWhtsp:
5246 * @facet: the facet to check
5247 * @valType: the built-in type
5248 * @value: the lexical repr. of the value to be validated
5249 * @val: the precomputed value
5250 * @ws: the whitespace type of the value
5251 * @length: the actual length of the value
5253 * Checka a value against a "length", "minLength" and "maxLength"
5254 * facet; sets @length to the computed length of @value.
5256 * Returns 0 if the value is valid, a positive error code
5257 * otherwise and -1 in case of an internal or API error.
5260 xmlSchemaValidateLengthFacetWhtsp(xmlSchemaFacetPtr facet
,
5261 xmlSchemaValType valType
,
5262 const xmlChar
*value
,
5263 xmlSchemaValPtr val
,
5264 unsigned long *length
,
5265 xmlSchemaWhitespaceValueType ws
)
5267 return (xmlSchemaValidateLengthFacetInternal(facet
, valType
, value
, val
,
5272 * xmlSchemaValidateFacetInternal:
5273 * @facet: the facet to check
5274 * @fws: the whitespace type of the facet's value
5275 * @valType: the built-in type of the value
5276 * @value: the lexical repr of the value to validate
5277 * @val: the precomputed value
5278 * @ws: the whitespace type of the value
5280 * Check a value against a facet condition
5282 * Returns 0 if the element is schemas valid, a positive error code
5283 * number otherwise and -1 in case of internal or API error.
5286 xmlSchemaValidateFacetInternal(xmlSchemaFacetPtr facet
,
5287 xmlSchemaWhitespaceValueType fws
,
5288 xmlSchemaValType valType
,
5289 const xmlChar
*value
,
5290 xmlSchemaValPtr val
,
5291 xmlSchemaWhitespaceValueType ws
)
5298 switch (facet
->type
) {
5299 case XML_SCHEMA_FACET_PATTERN
:
5301 * NOTE that for patterns, the @value needs to be the normalized
5302 * value, *not* the lexical initial value or the canonical value.
5306 ret
= xmlRegexpExec(facet
->regexp
, value
);
5310 return(XML_SCHEMAV_CVC_PATTERN_VALID
);
5312 case XML_SCHEMA_FACET_MAXEXCLUSIVE
:
5313 ret
= xmlSchemaCompareValues(val
, facet
->val
);
5318 return(XML_SCHEMAV_CVC_MAXEXCLUSIVE_VALID
);
5319 case XML_SCHEMA_FACET_MAXINCLUSIVE
:
5320 ret
= xmlSchemaCompareValues(val
, facet
->val
);
5323 if ((ret
== -1) || (ret
== 0))
5325 return(XML_SCHEMAV_CVC_MAXINCLUSIVE_VALID
);
5326 case XML_SCHEMA_FACET_MINEXCLUSIVE
:
5327 ret
= xmlSchemaCompareValues(val
, facet
->val
);
5332 return(XML_SCHEMAV_CVC_MINEXCLUSIVE_VALID
);
5333 case XML_SCHEMA_FACET_MININCLUSIVE
:
5334 ret
= xmlSchemaCompareValues(val
, facet
->val
);
5337 if ((ret
== 1) || (ret
== 0))
5339 return(XML_SCHEMAV_CVC_MININCLUSIVE_VALID
);
5340 case XML_SCHEMA_FACET_WHITESPACE
:
5341 /* TODO whitespaces */
5343 * NOTE: Whitespace should be handled to normalize
5344 * the value to be validated against a the facets;
5345 * not to normalize the value in-between.
5348 case XML_SCHEMA_FACET_ENUMERATION
:
5349 if (ws
== XML_SCHEMA_WHITESPACE_UNKNOWN
) {
5351 * This is to ensure API compatibility with the old
5352 * xmlSchemaValidateFacet().
5353 * TODO: Get rid of this case.
5355 if ((facet
->value
!= NULL
) &&
5356 (xmlStrEqual(facet
->value
, value
)))
5359 ret
= xmlSchemaCompareValuesWhtspExt(facet
->val
->type
,
5360 facet
->val
, facet
->value
, fws
, valType
, val
,
5367 return(XML_SCHEMAV_CVC_ENUMERATION_VALID
);
5368 case XML_SCHEMA_FACET_LENGTH
:
5370 * SPEC (1.3) "if {primitive type definition} is QName or NOTATION,
5371 * then any {value} is facet-valid."
5373 if ((valType
== XML_SCHEMAS_QNAME
) ||
5374 (valType
== XML_SCHEMAS_NOTATION
))
5376 /* No break on purpose. */
5377 case XML_SCHEMA_FACET_MAXLENGTH
:
5378 case XML_SCHEMA_FACET_MINLENGTH
: {
5379 unsigned int len
= 0;
5381 if ((valType
== XML_SCHEMAS_QNAME
) ||
5382 (valType
== XML_SCHEMAS_NOTATION
))
5385 * TODO: length, maxLength and minLength must be of type
5386 * nonNegativeInteger only. Check if decimal is used somehow.
5388 if ((facet
->val
== NULL
) ||
5389 ((facet
->val
->type
!= XML_SCHEMAS_DECIMAL
) &&
5390 (facet
->val
->type
!= XML_SCHEMAS_NNINTEGER
)) ||
5391 (facet
->val
->value
.decimal
.frac
!= 0)) {
5394 if ((val
!= NULL
) && (val
->type
== XML_SCHEMAS_HEXBINARY
))
5395 len
= val
->value
.hex
.total
;
5396 else if ((val
!= NULL
) && (val
->type
== XML_SCHEMAS_BASE64BINARY
))
5397 len
= val
->value
.base64
.total
;
5400 case XML_SCHEMAS_STRING
:
5401 case XML_SCHEMAS_NORMSTRING
:
5402 if (ws
== XML_SCHEMA_WHITESPACE_UNKNOWN
) {
5404 * This is to ensure API compatibility with the old
5405 * xmlSchemaValidateFacet(). Anyway, this was and
5406 * is not the correct handling.
5407 * TODO: Get rid of this case somehow.
5409 if (valType
== XML_SCHEMAS_STRING
)
5410 len
= xmlUTF8Strlen(value
);
5412 len
= xmlSchemaNormLen(value
);
5413 } else if (value
!= NULL
) {
5414 if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
5415 len
= xmlSchemaNormLen(value
);
5418 * Should be OK for "preserve" as well.
5420 len
= xmlUTF8Strlen(value
);
5423 case XML_SCHEMAS_IDREF
:
5424 case XML_SCHEMAS_TOKEN
:
5425 case XML_SCHEMAS_LANGUAGE
:
5426 case XML_SCHEMAS_NMTOKEN
:
5427 case XML_SCHEMAS_NAME
:
5428 case XML_SCHEMAS_NCNAME
:
5429 case XML_SCHEMAS_ID
:
5430 case XML_SCHEMAS_ANYURI
:
5432 len
= xmlSchemaNormLen(value
);
5438 if (facet
->type
== XML_SCHEMA_FACET_LENGTH
) {
5439 if (len
!= facet
->val
->value
.decimal
.lo
)
5440 return(XML_SCHEMAV_CVC_LENGTH_VALID
);
5441 } else if (facet
->type
== XML_SCHEMA_FACET_MINLENGTH
) {
5442 if (len
< facet
->val
->value
.decimal
.lo
)
5443 return(XML_SCHEMAV_CVC_MINLENGTH_VALID
);
5445 if (len
> facet
->val
->value
.decimal
.lo
)
5446 return(XML_SCHEMAV_CVC_MAXLENGTH_VALID
);
5450 case XML_SCHEMA_FACET_TOTALDIGITS
:
5451 case XML_SCHEMA_FACET_FRACTIONDIGITS
:
5453 if ((facet
->val
== NULL
) ||
5454 ((facet
->val
->type
!= XML_SCHEMAS_PINTEGER
) &&
5455 (facet
->val
->type
!= XML_SCHEMAS_NNINTEGER
)) ||
5456 (facet
->val
->value
.decimal
.frac
!= 0)) {
5459 if ((val
== NULL
) ||
5460 ((val
->type
!= XML_SCHEMAS_DECIMAL
) &&
5461 (val
->type
!= XML_SCHEMAS_INTEGER
) &&
5462 (val
->type
!= XML_SCHEMAS_NPINTEGER
) &&
5463 (val
->type
!= XML_SCHEMAS_NINTEGER
) &&
5464 (val
->type
!= XML_SCHEMAS_NNINTEGER
) &&
5465 (val
->type
!= XML_SCHEMAS_PINTEGER
) &&
5466 (val
->type
!= XML_SCHEMAS_INT
) &&
5467 (val
->type
!= XML_SCHEMAS_UINT
) &&
5468 (val
->type
!= XML_SCHEMAS_LONG
) &&
5469 (val
->type
!= XML_SCHEMAS_ULONG
) &&
5470 (val
->type
!= XML_SCHEMAS_SHORT
) &&
5471 (val
->type
!= XML_SCHEMAS_USHORT
) &&
5472 (val
->type
!= XML_SCHEMAS_BYTE
) &&
5473 (val
->type
!= XML_SCHEMAS_UBYTE
))) {
5476 if (facet
->type
== XML_SCHEMA_FACET_TOTALDIGITS
) {
5477 if (val
->value
.decimal
.total
> facet
->val
->value
.decimal
.lo
)
5478 return(XML_SCHEMAV_CVC_TOTALDIGITS_VALID
);
5480 } else if (facet
->type
== XML_SCHEMA_FACET_FRACTIONDIGITS
) {
5481 if (val
->value
.decimal
.frac
> facet
->val
->value
.decimal
.lo
)
5482 return(XML_SCHEMAV_CVC_FRACTIONDIGITS_VALID
);
5493 * xmlSchemaValidateFacet:
5494 * @base: the base type
5495 * @facet: the facet to check
5496 * @value: the lexical repr of the value to validate
5497 * @val: the precomputed value
5499 * Check a value against a facet condition
5501 * Returns 0 if the element is schemas valid, a positive error code
5502 * number otherwise and -1 in case of internal or API error.
5505 xmlSchemaValidateFacet(xmlSchemaTypePtr base
,
5506 xmlSchemaFacetPtr facet
,
5507 const xmlChar
*value
,
5508 xmlSchemaValPtr val
)
5511 * This tries to ensure API compatibility regarding the old
5512 * xmlSchemaValidateFacet() and the new xmlSchemaValidateFacetInternal() and
5513 * xmlSchemaValidateFacetWhtsp().
5516 return(xmlSchemaValidateFacetInternal(facet
,
5517 XML_SCHEMA_WHITESPACE_UNKNOWN
, val
->type
, value
, val
,
5518 XML_SCHEMA_WHITESPACE_UNKNOWN
));
5519 else if (base
!= NULL
)
5520 return(xmlSchemaValidateFacetInternal(facet
,
5521 XML_SCHEMA_WHITESPACE_UNKNOWN
, base
->builtInType
, value
, val
,
5522 XML_SCHEMA_WHITESPACE_UNKNOWN
));
5527 * xmlSchemaValidateFacetWhtsp:
5528 * @facet: the facet to check
5529 * @fws: the whitespace type of the facet's value
5530 * @valType: the built-in type of the value
5531 * @value: the lexical (or normalized for pattern) repr of the value to validate
5532 * @val: the precomputed value
5533 * @ws: the whitespace type of the value
5535 * Check a value against a facet condition. This takes value normalization
5536 * according to the specified whitespace types into account.
5537 * Note that @value needs to be the *normalized* value if the facet
5538 * is of type "pattern".
5540 * Returns 0 if the element is schemas valid, a positive error code
5541 * number otherwise and -1 in case of internal or API error.
5544 xmlSchemaValidateFacetWhtsp(xmlSchemaFacetPtr facet
,
5545 xmlSchemaWhitespaceValueType fws
,
5546 xmlSchemaValType valType
,
5547 const xmlChar
*value
,
5548 xmlSchemaValPtr val
,
5549 xmlSchemaWhitespaceValueType ws
)
5551 return(xmlSchemaValidateFacetInternal(facet
, fws
, valType
,
5560 #define DBL_EPSILON 1E-9
5563 #define INTEGER_DIGITS DBL_DIG
5564 #define FRACTION_DIGITS (DBL_DIG + 1)
5565 #define EXPONENT_DIGITS (3 + 2)
5568 * xmlXPathFormatNumber:
5569 * @number: number to format
5570 * @buffer: output buffer
5571 * @buffersize: size of output buffer
5573 * Convert the number into a string representation.
5576 xmlSchemaFormatFloat(double number
, char buffer
[], int buffersize
)
5578 switch (xmlXPathIsInf(number
)) {
5580 if (buffersize
> (int)sizeof("INF"))
5581 snprintf(buffer
, buffersize
, "INF");
5584 if (buffersize
> (int)sizeof("-INF"))
5585 snprintf(buffer
, buffersize
, "-INF");
5588 if (xmlXPathIsNaN(number
)) {
5589 if (buffersize
> (int)sizeof("NaN"))
5590 snprintf(buffer
, buffersize
, "NaN");
5591 } else if (number
== 0) {
5592 snprintf(buffer
, buffersize
, "0.0E0");
5594 /* 3 is sign, decimal point, and terminating zero */
5595 char work
[DBL_DIG
+ EXPONENT_DIGITS
+ 3];
5596 int integer_place
, fraction_place
;
5598 char *after_fraction
;
5599 double absolute_value
;
5602 absolute_value
= fabs(number
);
5605 * Result is in work, and after_fraction points
5606 * just past the fractional part.
5607 * Use scientific notation
5609 integer_place
= DBL_DIG
+ EXPONENT_DIGITS
+ 1;
5610 fraction_place
= DBL_DIG
- 1;
5611 snprintf(work
, sizeof(work
),"%*.*e",
5612 integer_place
, fraction_place
, number
);
5613 after_fraction
= strchr(work
+ DBL_DIG
, 'e');
5614 /* Remove fractional trailing zeroes */
5615 ptr
= after_fraction
;
5616 while (*(--ptr
) == '0')
5620 while ((*ptr
++ = *after_fraction
++) != 0);
5622 /* Finally copy result back to caller */
5623 size
= strlen(work
) + 1;
5624 if (size
> buffersize
) {
5625 work
[buffersize
- 1] = 0;
5628 memmove(buffer
, work
, size
);
5636 * xmlSchemaGetCanonValue:
5637 * @val: the precomputed value
5638 * @retValue: the returned value
5640 * Get a the cononical lexical representation of the value.
5641 * The caller has to FREE the returned retValue.
5643 * WARNING: Some value types are not supported yet, resulting
5644 * in a @retValue of "???".
5646 * TODO: XML Schema 1.0 does not define canonical representations
5647 * for: duration, gYearMonth, gYear, gMonthDay, gMonth, gDay,
5648 * anyURI, QName, NOTATION. This will be fixed in XML Schema 1.1.
5651 * Returns 0 if the value could be built, 1 if the value type is
5652 * not supported yet and -1 in case of API errors.
5655 xmlSchemaGetCanonValue(xmlSchemaValPtr val
, const xmlChar
**retValue
)
5657 if ((retValue
== NULL
) || (val
== NULL
))
5660 switch (val
->type
) {
5661 case XML_SCHEMAS_STRING
:
5662 if (val
->value
.str
== NULL
)
5663 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"");
5666 BAD_CAST
xmlStrdup((const xmlChar
*) val
->value
.str
);
5668 case XML_SCHEMAS_NORMSTRING
:
5669 if (val
->value
.str
== NULL
)
5670 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"");
5672 *retValue
= xmlSchemaWhiteSpaceReplace(
5673 (const xmlChar
*) val
->value
.str
);
5674 if ((*retValue
) == NULL
)
5675 *retValue
= BAD_CAST
xmlStrdup(
5676 (const xmlChar
*) val
->value
.str
);
5679 case XML_SCHEMAS_TOKEN
:
5680 case XML_SCHEMAS_LANGUAGE
:
5681 case XML_SCHEMAS_NMTOKEN
:
5682 case XML_SCHEMAS_NAME
:
5683 case XML_SCHEMAS_NCNAME
:
5684 case XML_SCHEMAS_ID
:
5685 case XML_SCHEMAS_IDREF
:
5686 case XML_SCHEMAS_ENTITY
:
5687 case XML_SCHEMAS_NOTATION
: /* Unclear */
5688 case XML_SCHEMAS_ANYURI
: /* Unclear */
5689 if (val
->value
.str
== NULL
)
5692 BAD_CAST
xmlSchemaCollapseString(BAD_CAST val
->value
.str
);
5693 if (*retValue
== NULL
)
5695 BAD_CAST
xmlStrdup((const xmlChar
*) val
->value
.str
);
5697 case XML_SCHEMAS_QNAME
:
5698 /* TODO: Unclear in XML Schema 1.0. */
5699 if (val
->value
.qname
.uri
== NULL
) {
5700 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST val
->value
.qname
.name
);
5703 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"{");
5704 *retValue
= BAD_CAST
xmlStrcat((xmlChar
*) (*retValue
),
5705 BAD_CAST val
->value
.qname
.uri
);
5706 *retValue
= BAD_CAST
xmlStrcat((xmlChar
*) (*retValue
),
5708 *retValue
= BAD_CAST
xmlStrcat((xmlChar
*) (*retValue
),
5709 BAD_CAST val
->value
.qname
.uri
);
5712 case XML_SCHEMAS_DECIMAL
:
5714 * TODO: Lookout for a more simple implementation.
5716 if ((val
->value
.decimal
.total
== 1) &&
5717 (val
->value
.decimal
.lo
== 0)) {
5718 *retValue
= xmlStrdup(BAD_CAST
"0.0");
5720 xmlSchemaValDecimal dec
= val
->value
.decimal
;
5722 char *buf
= NULL
, *offs
;
5724 /* Add room for the decimal point as well. */
5725 bufsize
= dec
.total
+ 2;
5728 /* Add room for leading/trailing zero. */
5729 if ((dec
.frac
== 0) || (dec
.frac
== dec
.total
))
5731 buf
= xmlMalloc(bufsize
);
5737 if (dec
.frac
== dec
.total
) {
5742 snprintf(offs
, bufsize
- (offs
- buf
),
5743 "%lu%lu%lu", dec
.hi
, dec
.mi
, dec
.lo
);
5744 else if (dec
.mi
!= 0)
5745 snprintf(offs
, bufsize
- (offs
- buf
),
5746 "%lu%lu", dec
.mi
, dec
.lo
);
5748 snprintf(offs
, bufsize
- (offs
- buf
),
5751 if (dec
.frac
!= 0) {
5752 if (dec
.frac
!= dec
.total
) {
5753 int diff
= dec
.total
- dec
.frac
;
5755 * Insert the decimal point.
5757 memmove(offs
+ diff
+ 1, offs
+ diff
, dec
.frac
+1);
5762 * Insert missing zeroes behind the decimal point.
5764 while (*(offs
+ i
) != 0)
5766 if (i
< dec
.total
) {
5767 memmove(offs
+ (dec
.total
- i
), offs
, i
+1);
5768 memset(offs
, '0', dec
.total
- i
);
5773 * Append decimal point and zero.
5775 offs
= buf
+ bufsize
- 1;
5780 *retValue
= BAD_CAST buf
;
5783 case XML_SCHEMAS_INTEGER
:
5784 case XML_SCHEMAS_PINTEGER
:
5785 case XML_SCHEMAS_NPINTEGER
:
5786 case XML_SCHEMAS_NINTEGER
:
5787 case XML_SCHEMAS_NNINTEGER
:
5788 case XML_SCHEMAS_LONG
:
5789 case XML_SCHEMAS_BYTE
:
5790 case XML_SCHEMAS_SHORT
:
5791 case XML_SCHEMAS_INT
:
5792 case XML_SCHEMAS_UINT
:
5793 case XML_SCHEMAS_ULONG
:
5794 case XML_SCHEMAS_USHORT
:
5795 case XML_SCHEMAS_UBYTE
:
5796 if ((val
->value
.decimal
.total
== 1) &&
5797 (val
->value
.decimal
.lo
== 0))
5798 *retValue
= xmlStrdup(BAD_CAST
"0");
5800 xmlSchemaValDecimal dec
= val
->value
.decimal
;
5801 int bufsize
= dec
.total
+ 1;
5803 /* Add room for the decimal point as well. */
5806 *retValue
= xmlMalloc(bufsize
);
5807 if (*retValue
== NULL
)
5811 snprintf((char *) *retValue
, bufsize
,
5812 "-%lu%lu%lu", dec
.hi
, dec
.mi
, dec
.lo
);
5814 snprintf((char *) *retValue
, bufsize
,
5815 "%lu%lu%lu", dec
.hi
, dec
.mi
, dec
.lo
);
5816 } else if (dec
.mi
!= 0) {
5818 snprintf((char *) *retValue
, bufsize
,
5819 "-%lu%lu", dec
.mi
, dec
.lo
);
5821 snprintf((char *) *retValue
, bufsize
,
5822 "%lu%lu", dec
.mi
, dec
.lo
);
5825 snprintf((char *) *retValue
, bufsize
, "-%lu", dec
.lo
);
5827 snprintf((char *) *retValue
, bufsize
, "%lu", dec
.lo
);
5831 case XML_SCHEMAS_BOOLEAN
:
5833 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"true");
5835 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"false");
5837 case XML_SCHEMAS_DURATION
: {
5840 unsigned long mon
, day
, hour
= 0, min
= 0;
5841 double sec
= 0, left
;
5843 /* TODO: Unclear in XML Schema 1.0 */
5845 * TODO: This results in a normalized output of the value
5846 * - which is NOT conformant to the spec -
5847 * since the exact values of each property are not
5848 * recoverable. Think about extending the structure to
5849 * provide a field for every property.
5851 year
= (unsigned long) FQUOTIENT(labs(val
->value
.dur
.mon
), 12);
5852 mon
= labs(val
->value
.dur
.mon
) - 12 * year
;
5854 day
= (unsigned long) FQUOTIENT(fabs(val
->value
.dur
.sec
), 86400);
5855 left
= fabs(val
->value
.dur
.sec
) - day
* 86400;
5857 hour
= (unsigned long) FQUOTIENT(left
, 3600);
5858 left
= left
- (hour
* 3600);
5860 min
= (unsigned long) FQUOTIENT(left
, 60);
5861 sec
= left
- (min
* 60);
5864 if ((val
->value
.dur
.mon
< 0) || (val
->value
.dur
.sec
< 0))
5865 snprintf(buf
, 100, "P%luY%luM%luDT%luH%luM%.14gS",
5866 year
, mon
, day
, hour
, min
, sec
);
5868 snprintf(buf
, 100, "-P%luY%luM%luDT%luH%luM%.14gS",
5869 year
, mon
, day
, hour
, min
, sec
);
5870 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
5873 case XML_SCHEMAS_GYEAR
: {
5875 /* TODO: Unclear in XML Schema 1.0 */
5876 /* TODO: What to do with the timezone? */
5877 snprintf(buf
, 30, "%04ld", val
->value
.date
.year
);
5878 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
5881 case XML_SCHEMAS_GMONTH
: {
5882 /* TODO: Unclear in XML Schema 1.0 */
5883 /* TODO: What to do with the timezone? */
5884 *retValue
= xmlMalloc(6);
5885 if (*retValue
== NULL
)
5887 snprintf((char *) *retValue
, 6, "--%02u",
5888 val
->value
.date
.mon
);
5891 case XML_SCHEMAS_GDAY
: {
5892 /* TODO: Unclear in XML Schema 1.0 */
5893 /* TODO: What to do with the timezone? */
5894 *retValue
= xmlMalloc(6);
5895 if (*retValue
== NULL
)
5897 snprintf((char *) *retValue
, 6, "---%02u",
5898 val
->value
.date
.day
);
5901 case XML_SCHEMAS_GMONTHDAY
: {
5902 /* TODO: Unclear in XML Schema 1.0 */
5903 /* TODO: What to do with the timezone? */
5904 *retValue
= xmlMalloc(8);
5905 if (*retValue
== NULL
)
5907 snprintf((char *) *retValue
, 8, "--%02u-%02u",
5908 val
->value
.date
.mon
, val
->value
.date
.day
);
5911 case XML_SCHEMAS_GYEARMONTH
: {
5913 /* TODO: Unclear in XML Schema 1.0 */
5914 /* TODO: What to do with the timezone? */
5915 if (val
->value
.date
.year
< 0)
5916 snprintf(buf
, 35, "-%04ld-%02u",
5917 labs(val
->value
.date
.year
),
5918 val
->value
.date
.mon
);
5920 snprintf(buf
, 35, "%04ld-%02u",
5921 val
->value
.date
.year
, val
->value
.date
.mon
);
5922 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
5925 case XML_SCHEMAS_TIME
:
5929 if (val
->value
.date
.tz_flag
) {
5930 xmlSchemaValPtr norm
;
5932 norm
= xmlSchemaDateNormalize(val
, 0);
5936 * TODO: Check if "%.14g" is portable.
5939 "%02u:%02u:%02.14gZ",
5940 norm
->value
.date
.hour
,
5941 norm
->value
.date
.min
,
5942 norm
->value
.date
.sec
);
5943 xmlSchemaFreeValue(norm
);
5946 "%02u:%02u:%02.14g",
5947 val
->value
.date
.hour
,
5948 val
->value
.date
.min
,
5949 val
->value
.date
.sec
);
5951 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
5954 case XML_SCHEMAS_DATE
:
5958 if (val
->value
.date
.tz_flag
) {
5959 xmlSchemaValPtr norm
;
5961 norm
= xmlSchemaDateNormalize(val
, 0);
5965 * TODO: Append the canonical value of the
5966 * recoverable timezone and not "Z".
5970 norm
->value
.date
.year
, norm
->value
.date
.mon
,
5971 norm
->value
.date
.day
);
5972 xmlSchemaFreeValue(norm
);
5976 val
->value
.date
.year
, val
->value
.date
.mon
,
5977 val
->value
.date
.day
);
5979 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
5982 case XML_SCHEMAS_DATETIME
:
5986 if (val
->value
.date
.tz_flag
) {
5987 xmlSchemaValPtr norm
;
5989 norm
= xmlSchemaDateNormalize(val
, 0);
5993 * TODO: Check if "%.14g" is portable.
5996 "%04ld:%02u:%02uT%02u:%02u:%02.14gZ",
5997 norm
->value
.date
.year
, norm
->value
.date
.mon
,
5998 norm
->value
.date
.day
, norm
->value
.date
.hour
,
5999 norm
->value
.date
.min
, norm
->value
.date
.sec
);
6000 xmlSchemaFreeValue(norm
);
6003 "%04ld:%02u:%02uT%02u:%02u:%02.14g",
6004 val
->value
.date
.year
, val
->value
.date
.mon
,
6005 val
->value
.date
.day
, val
->value
.date
.hour
,
6006 val
->value
.date
.min
, val
->value
.date
.sec
);
6008 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
6011 case XML_SCHEMAS_HEXBINARY
:
6012 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST val
->value
.hex
.str
);
6014 case XML_SCHEMAS_BASE64BINARY
:
6016 * TODO: Is the following spec piece implemented?:
6017 * SPEC: "Note: For some values the canonical form defined
6018 * above does not conform to [RFC 2045], which requires breaking
6019 * with linefeeds at appropriate intervals."
6021 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST val
->value
.base64
.str
);
6023 case XML_SCHEMAS_FLOAT
: {
6026 * |m| < 16777216, -149 <= e <= 104.
6027 * TODO: Handle, NaN, INF, -INF. The format is not
6028 * yet conformant. The c type float does not cover
6031 snprintf(buf
, 30, "%01.14e", val
->value
.f
);
6032 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
6035 case XML_SCHEMAS_DOUBLE
: {
6037 /* |m| < 9007199254740992, -1075 <= e <= 970 */
6039 * TODO: Handle, NaN, INF, -INF. The format is not
6040 * yet conformant. The c type float does not cover
6043 snprintf(buf
, 40, "%01.14e", val
->value
.d
);
6044 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST buf
);
6048 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"???");
6051 if (*retValue
== NULL
)
6057 * xmlSchemaGetCanonValueWhtsp:
6058 * @val: the precomputed value
6059 * @retValue: the returned value
6060 * @ws: the whitespace type of the value
6062 * Get a the cononical representation of the value.
6063 * The caller has to free the returned @retValue.
6065 * Returns 0 if the value could be built, 1 if the value type is
6066 * not supported yet and -1 in case of API errors.
6069 xmlSchemaGetCanonValueWhtsp(xmlSchemaValPtr val
,
6070 const xmlChar
**retValue
,
6071 xmlSchemaWhitespaceValueType ws
)
6073 if ((retValue
== NULL
) || (val
== NULL
))
6075 if ((ws
== XML_SCHEMA_WHITESPACE_UNKNOWN
) ||
6076 (ws
> XML_SCHEMA_WHITESPACE_COLLAPSE
))
6080 switch (val
->type
) {
6081 case XML_SCHEMAS_STRING
:
6082 if (val
->value
.str
== NULL
)
6083 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"");
6084 else if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
6085 *retValue
= xmlSchemaCollapseString(val
->value
.str
);
6086 else if (ws
== XML_SCHEMA_WHITESPACE_REPLACE
)
6087 *retValue
= xmlSchemaWhiteSpaceReplace(val
->value
.str
);
6088 if ((*retValue
) == NULL
)
6089 *retValue
= BAD_CAST
xmlStrdup(val
->value
.str
);
6091 case XML_SCHEMAS_NORMSTRING
:
6092 if (val
->value
.str
== NULL
)
6093 *retValue
= BAD_CAST
xmlStrdup(BAD_CAST
"");
6095 if (ws
== XML_SCHEMA_WHITESPACE_COLLAPSE
)
6096 *retValue
= xmlSchemaCollapseString(val
->value
.str
);
6098 *retValue
= xmlSchemaWhiteSpaceReplace(val
->value
.str
);
6099 if ((*retValue
) == NULL
)
6100 *retValue
= BAD_CAST
xmlStrdup(val
->value
.str
);
6104 return (xmlSchemaGetCanonValue(val
, retValue
));
6110 * xmlSchemaGetValType:
6111 * @val: a schemas value
6113 * Accessor for the type of a value
6115 * Returns the xmlSchemaValType of the value
6118 xmlSchemaGetValType(xmlSchemaValPtr val
)
6121 return(XML_SCHEMAS_UNKNOWN
);
6125 #define bottom_xmlschemastypes
6126 #include "elfgcchack.h"
6127 #endif /* LIBXML_SCHEMAS_ENABLED */