typos
[cxml-rng.git] / types.lisp
blob029c1ecc41a619e68be17b3843994e2852c2e83d
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
2 ;;;
3 ;;; Copyright (c) 2007 David Lichteblau. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package :cxml-types)
31 (defstruct (param (:constructor make-param (name value)))
32 "@short{A named data type parameter.}
34 (With the XSD type library, parameters are known as restricting facets.)
35 @see-constructor{make-param}
36 @see{find-type}
37 @see{cxml-rng:pattern-params}
38 @see{cxml-rng:data}
39 @see-slot{param-name}
40 @see-slot{param-value}"
41 name
42 value)
44 (setf (documentation 'make-param 'function)
45 "@arg[name]{parameter name, a string}
46 @arg[value]{parameter value, a string}
47 @return{a @class{param}}
48 Create a data type parameter.
49 @see{param-name}
50 @see{param-value}")
52 (setf (documentation 'param-name 'function)
53 "@arg[instance]{an instance of @class{param}}
54 @return{a string}
55 The data type parameter's name.
56 @see{param-value}")
58 (setf (documentation 'param-value 'function)
59 "@arg[instance]{an instance of @class{param}}
60 @return{a string}
61 The data type parameter's value.
62 @see{param-name}")
64 (defclass data-type () ()
65 (:documentation
66 "@short{The abstract superclass of all types.}
68 Each type belongs to a datatype library, named by a keyword. In each
69 library, the types are named by strings.
71 @see-constructor{find-type}
72 @see-slot{type-name}
73 @see-slot{type-library}
74 @see-slot{type-context-dependent-p}
75 @see{parse}
76 @see{equal-using-type}
77 @see{lessp-using-type}
78 @see{validp}"))
80 (defgeneric find-type (library name params)
81 (:documentation
82 "@arg[library]{datatype library, a keyword symbol}
83 @arg[name]{the type's name, a string}
84 @arg[params]{type parameters, a list of @class{param} instances}
85 @return{an instance of @class{data-type}, or @code{nil}}
86 @short{Look up the type named @em{name} in datatype library @em{library}.}
88 Additional parameters (knows as restricting facets in XSD) can be passed
89 to specify or restrict the type for the purposes of @fun{validp}.
91 Return a type instance for this type and the additional parameters,
92 @code{nil} if the type does not exist, or
93 @code{:error} if the type exists, but the specified parameters are not
94 valid for that type.
96 @see{data-type}"))
98 (defgeneric type-library (type)
99 (:documentation
100 "@arg[type]{an instance of @class{data-type}}
101 @return{library name, a keyword}
102 @short{Return the name of the library this type belongs to.}
104 @see{type-name}
105 @see{type-context-dependent-p}"))
107 (defgeneric type-name (type)
108 (:documentation
109 "@arg[type]{an instance of @class{data-type}}
110 @return{type name, a string}
111 @short{Return the name this type has within its library.}
113 @see{type-library}
114 @see{type-context-dependent-p}"))
116 (defmethod find-type ((library t) name params)
117 (declare (ignore name params))
118 nil)
120 (defgeneric type-context-dependent-p (type)
121 (:documentation
122 "@arg[type]{an instance of @class{data-type}}
123 @return{a boolean}
124 @short{Return true if parsing and validation of values by this type
125 depends on the validation context.}
127 In this case, the optional @code{context} argument to @fun{parse} and
128 @fun{validp} is required, and an error will be signalled if it is missing.
130 @see{validation-context}
131 @see{type-name}
132 @see{type-library}
133 @see{type-context-dependent-p}"))
135 (defmethod type-context-dependent-p ((type data-type))
136 nil)
138 (defgeneric equal-using-type (type u v)
139 (:documentation
140 "@arg[type]{an instance of @class{data-type}}
141 @arg[u]{a parsed value as returned by @fun{parse}}
142 @arg[v]{a parsed value as returned by @fun{parse}}
143 @return{a boolean}
144 @short{Compare the @emph{values} @code{u} and @code{v} using a
145 data-type-dependent equality function.}
147 @see{validp}"))
149 (defgeneric parse (type e &optional context)
150 (:documentation
151 "@arg[type]{an instance of @class{data-type}}
152 @arg[e]{a string}
153 @arg[context]{an instance of @class{validation-context}}
154 @return{an object}
155 @short{Parse string @code{e} and return a representation of its value
156 as defined by the data type.}
158 The @code{context} argument is required if @fun{type-context-dependent-p}
159 is true for @code{type}, and will be ignored otherwise.
161 @see{equal-using-type}
162 @see{validp}"))
164 (defgeneric validp (type e &optional context)
165 (:documentation
166 "@arg[type]{an instance of @class{data-type}}
167 @arg[e]{a string}
168 @arg[context]{an instance of @class{validation-context}}
169 @return{a boolean}
170 @short{Determine whether a string is a valid lexical representation
171 for a type.}
173 The @code{context} argument is required if @fun{type-context-dependent-p}
174 is true for @code{type}, and will be ignored otherwise.
176 @see{parse}
177 @see{equal-using-type}"))
180 ;;; Validation context
182 (defclass validation-context () ()
183 (:documentation
184 "@short{This abstract class defines a protocol allowing data types
185 to query the XML parser about its current state.}
187 Some types are context dependent, as indicated by
188 @fun{type-context-dependent-p}. Those types need access to state
189 computed by the XML parser implicitly, like namespace bindings or
190 the Base URI.
192 User-defined subclasses must implement methods
193 for the functions @fun{context-find-namespace-binding} and
194 @fun{context-find-unparsed-entity}.
196 Two pre-defined validation context implementations are
197 provided, one for use with SAX, the other based on Klacks."))
199 (defgeneric context-find-namespace-binding (context prefix)
200 (:documentation
201 "@arg[context]{an instance of @class{validation-context}}
202 @arg[prefix]{name prefix, a string}
203 @return{the namespace URI as a string, or NIL}
204 @short{This function resolves a namespace prefix to a namespace URI in the
205 current context.}
206 All currently declared namespaces
207 are taken into account, including those declared directly on the
208 current element."))
210 (defgeneric context-find-unparsed-entity (context name)
211 (:documentation
212 "@arg[context]{an instance of @class{validation-context}}
213 @arg[name]{entity name, a string}
214 @return{@code{nil}, or a list of public id, system id, and notation name}
215 This function looks for an unparsed entity in the current context."))
217 (defclass klacks-validation-context (validation-context)
218 ((source :initarg :source :accessor context-source))
219 (:documentation
220 "A validation-context implementation that queries
221 a klacks source for information about the parser's current state.
222 @see-constructor{make-klacks-validation-context}"))
224 (defun make-klacks-validation-context (source)
225 "@arg[source]{a @a[http://common-lisp.net/project/cxml/klacks.html]{
226 klacks source}}
227 @return{a @class{klacks-validation-context}}
228 Create a validation-context that will query the given klacks source for
229 the current parser context."
230 (make-instance 'klacks-validation-context :source source))
232 (defmethod context-find-namespace-binding
233 ((context klacks-validation-context) prefix)
234 (klacks:find-namespace-binding prefix (context-source context)))
236 ;; zzz nicht schoen.
237 (defmethod context-find-unparsed-entity
238 ((context klacks-validation-context) name)
239 (or (dolist (x (slot-value (context-source context)
240 'cxml::external-declarations))
241 (when (and (eq (car x) 'sax:unparsed-entity-declaration)
242 (equal (cadr x) name))
243 (return t)))
244 (dolist (x (slot-value (context-source context)
245 'cxml::internal-declarations))
246 (when (and (eq (car x) 'sax:unparsed-entity-declaration)
247 (equal (cadr x) name))
248 (return t)))))
250 (defclass sax-validation-context-mixin (validation-context)
251 ((stack :initform nil :accessor context-stack)
252 (unparsed-entities :initform (make-hash-table :test 'equal)
253 :accessor unparsed-entities))
254 (:documentation
255 "@short{A class that implements validation-context as a mixin for
256 user-defined SAX handler classes.}
258 The mixin will record namespace information
259 automatically, and the user's SAX handler can simply be passed as a
260 validation context to data type functions."))
262 (defmethod sax:start-prefix-mapping
263 ((handler sax-validation-context-mixin) prefix uri)
264 (push (cons prefix uri) (context-stack handler)))
266 (defmethod sax:end-prefix-mapping
267 ((handler sax-validation-context-mixin) prefix)
268 (setf (context-stack handler)
269 (remove prefix
270 (context-stack handler)
271 :count 1
272 :key #'car
273 :test #'equal)))
275 (defmethod sax:unparsed-entity-declaration
276 ((context sax-validation-context-mixin)
277 name public-id system-id notation-name)
278 (setf (gethash name (unparsed-entities context))
279 (list public-id system-id notation-name)))
281 (defmethod context-find-namespace-binding
282 ((context sax-validation-context-mixin) prefix)
283 (cdr (assoc prefix (context-stack context) :test #'equal)))
285 (defmethod context-find-unparsed-entity
286 ((context sax-validation-context-mixin) name)
287 (gethash name (unparsed-entities context)))
290 ;;; Relax NG built-in type library
292 (defclass rng-type (data-type) ()
293 (:documentation
294 "@short{The class of Relax NG built-in types.}
295 Relax NG defines two built-in data type: string and token.
297 The Relax NG type library is named @code{:||}."))
299 (defmethod print-object ((object rng-type) stream)
300 (print-unreadable-object (object stream :type t :identity nil)))
302 (defclass string-type (rng-type) ()
303 (:documentation
304 "@short{The Relax NG 'string' type.}
305 This data type allows arbitrary strings and interprets them as-is.
307 For this type, @fun{parse} will return any string unchanged, and
308 @fun{equal-using-type} compares strings using @code{equal}."))
310 (defclass token-type (rng-type) ()
311 (:documentation
312 "@short{The Relax NG 'token' type.}
313 This data type allows arbitrary strings and normalizes all whitespaces.
315 For this type, @fun{parse} will return the string with leading and
316 trailing whitespace removed, and remaining sequences of spaces
317 compressed down to one space character each.
319 A method for @fun{equal-using-type} compares strings using @code{equal}."))
321 (defmethod type-library ((type rng-type))
322 :||)
324 (defvar *string-data-type* (make-instance 'string-type))
325 (defvar *token-data-type* (make-instance 'token-type))
327 (defmethod find-type ((library (eql :||)) name params)
328 (cond
329 ((eq name :probe) t)
330 (params :error)
331 ((equal name "string") *string-data-type*)
332 ((equal name "token") *token-data-type*)
333 (t nil)))
335 (defmethod equal-using-type ((type rng-type) u v)
336 (equal u v))
338 (defmethod validp ((type rng-type) e &optional context)
339 (declare (ignore e context))
342 (defmethod type-name ((type string-type)) "string")
343 (defmethod type-name ((type token-type)) "token")
345 (defmethod parse ((type string-type) e &optional context)
346 (declare (ignore context))
349 (defmethod parse ((type token-type) e &optional context)
350 (declare (ignore context))
351 (normalize-whitespace e))
353 (eval-when (:compile-toplevel :load-toplevel :execute)
354 (defparameter *whitespace*
355 (format nil "~C~C~C~C"
356 (code-char 9)
357 (code-char 32)
358 (code-char 13)
359 (code-char 10))))
361 (defun normalize-whitespace (str)
362 (cl-ppcre:regex-replace-all #.(format nil "[~A]+" *whitespace*)
363 (string-trim *whitespace* str)
364 " "))
366 (defun replace-whitespace (str)
367 (cl-ppcre:regex-replace-all #.(format nil "[~A]" *whitespace*)
369 " "))
372 ;;; XML Schema Part 2: Datatypes Second Edition
374 (defparameter *xsd-types* (make-hash-table :test 'equal))
376 (defmacro defxsd
377 ((class-name type-name) (&rest supers) (&rest slots) &rest args)
378 `(progn
379 (setf (gethash ,type-name *xsd-types*) ',class-name)
380 (defclass ,class-name ,supers
381 ((type-name :initform ,type-name
382 :reader type-name
383 :allocation :class)
384 ,@slots)
385 ,@args)))
387 (defclass xsd-type (data-type)
388 ((patterns :initform nil :initarg :patterns :reader patterns))
389 (:documentation
390 "@short{The class of XML Schema built-in types.}
392 Subclasses of xsd-type provide the built-in types of
393 @a[http://www.w3.org/TR/xmlschema-2/]{
394 XML Schema Part 2: Datatypes Second Edition}
395 as specified in @a[http://relaxng.org/xsd-20010907.html]{Guidelines for
396 using W3C XML Schema Datatypes with RELAX NG}.
398 The XSD type library
399 is named @code{:|http://www.w3.org/2001/XMLSchema-datatypes|}.
401 @b{Parameters.} All XSD types accept regular expressions restricting
402 the set of strings accepted by the type. The pattern parameter is
403 called @code{\"pattern\"}. This parameter can be repeated to specify
404 multiple regular expressions that must all match the data.
405 As an initarg, specify @code{:pattern} with a list of regular expressions
406 as an argument.
408 @see-slot{patterns}"))
410 (defmethod print-object ((object xsd-type) stream)
411 (print-unreadable-object (object stream :type t :identity nil)
412 (describe-facets object stream)))
414 (defgeneric describe-facets (object stream)
415 (:method-combination progn))
417 (defmethod describe-facets progn ((object xsd-type) stream)
418 (format stream "~{ :pattern ~A~}" (patterns object)))
420 (defmethod type-library ((type xsd-type))
421 :|http://www.w3.org/2001/XMLSchema-datatypes|)
423 (defun zip (keys values)
424 (loop for key in keys for value in values collect key collect value))
426 (defgeneric parse-parameter (class-name type-name param-name value))
428 (defun parse-parameters (type-class params)
429 (let ((patterns '())
430 (args '()))
431 (dolist (param params (values t patterns args))
432 (let ((name (param-name param))
433 (value (param-value param)))
434 (if (equal name "pattern")
435 (push value patterns)
436 (multiple-value-bind (key required-class)
437 (case (find-symbol (param-name param) :keyword)
438 (:|length| (values :exact-length 'length-mixin))
439 (:|maxLength| (values :max-length 'length-mixin))
440 (:|minLength| (values :min-length 'length-mixin))
441 (:|minInclusive| (values :min-inclusive 'ordering-mixin))
442 (:|maxInclusive| (values :max-inclusive 'ordering-mixin))
443 (:|minExclusive| (values :min-exclusive 'ordering-mixin))
444 (:|maxExclusive| (values :max-exclusive 'ordering-mixin))
445 (:|totalDigits| (values :total-digits 'decimal-type))
446 (:|fractionDigits| (values :fraction-digits 'decimal-type))
447 (t (return nil)))
448 (unless (subtypep type-class required-class)
449 (return nil))
450 (when (loop
451 for (k nil) on args by #'cddr
452 thereis (eq key k))
453 (return nil))
454 (push (parse-parameter required-class
455 type-class
457 (normalize-whitespace value))
458 args)
459 (push key args)))))))
461 (defmethod find-type
462 ((library (eql :|http://www.w3.org/2001/XMLSchema-datatypes|)) name params)
463 (if (eq name :probe)
465 (let ((class (gethash name *xsd-types*)))
466 (if class
467 (multiple-value-bind (ok patterns other-args)
468 (parse-parameters class params)
469 (if ok
470 (apply #'make-instance
471 class
472 :patterns patterns
473 other-args)
474 :error))
475 nil))))
477 (defgeneric parse/xsd (type e context))
479 (defgeneric validp/xsd (type v context)
480 (:method-combination and))
482 (defmethod validp/xsd and ((type xsd-type) v context)
483 (declare (ignore context))
484 ;; zzz
485 #+(or)
486 (every (lambda (pattern)
487 (cl-ppcre:all-matches pattern v))
488 (patterns type))
491 (defmethod validp ((type xsd-type) e &optional context)
492 (not (eq :error (parse/xsd type e context))))
494 (defmethod parse ((type xsd-type) e &optional context)
495 (let ((result (parse/xsd type e context)))
496 (when (eq result :error)
497 (error "not valid for data type ~A: ~S" type e))
498 result))
500 ;; Handle the whiteSpace "facet" before the subclass sees it.
501 ;; If parsing succeded, check other facets by asking validp/xsd.
502 (defmethod parse/xsd :around ((type xsd-type) e context)
503 (let ((result (call-next-method type
504 (munge-whitespace type e)
505 context)))
506 (if (or (eq result :error) (validp/xsd type result context))
507 result
508 :error)))
510 (defgeneric munge-whitespace (type e))
512 (defmethod munge-whitespace ((type xsd-type) e)
513 (normalize-whitespace e))
516 ;;; ordering-mixin
518 (defclass ordering-mixin ()
519 ((min-exclusive :initform nil
520 :initarg :min-exclusive
521 :accessor min-exclusive)
522 (max-exclusive :initform nil
523 :initarg :max-exclusive
524 :accessor max-exclusive)
525 (min-inclusive :initform nil
526 :initarg :min-inclusive
527 :accessor min-inclusive)
528 (max-inclusive :initform nil
529 :initarg :max-inclusive
530 :accessor max-inclusive)))
532 (defmethod describe-facets progn ((object ordering-mixin) stream)
533 (dolist (slot '(min-exclusive max-exclusive min-inclusive max-inclusive))
534 (let ((value (slot-value object slot)))
535 (when value
536 (format stream " ~A ~A"
537 (intern (symbol-name slot) :keyword)
538 value)))))
540 (defmethod parse-parameter
541 ((class-name (eql 'ordering-mixin)) type-name (param t) value)
542 (parse (make-instance type-name) value nil))
544 (defgeneric lessp-using-type (type u v)
545 (:documentation
546 "@arg[type]{an ordered @class{data-type}}
547 @arg[u]{a parsed value as returned by @fun{parse}}
548 @arg[v]{a parsed value as returned by @fun{parse}}
549 @return{a boolean}
550 @short{Compare the @emph{values} @code{u} and @code{v} using a
551 data-type-dependent partial ordering.}
553 A method for this function is provided only by types that have a
554 natural partial ordering.
556 @see{equal-using-type}"))
558 (defun <-using-type (type u v)
559 (lessp-using-type type u v))
561 (defun <=-using-type (type u v)
562 (or (lessp-using-type type u v) (equal-using-type type u v)))
564 ;; it's only a partial ordering, so in general this is not the opposite of <=
565 (defun >-using-type (type u v)
566 (lessp-using-type type v u))
568 ;; it's only a partial ordering, so in general this is not the opposite of <
569 (defun >=-using-type (type u v)
570 (or (lessp-using-type type v u) (equal-using-type type v u)))
572 (defmethod validp/xsd and ((type ordering-mixin) v context)
573 (declare (ignore context))
574 (with-slots (min-exclusive max-exclusive min-inclusive max-inclusive) type
575 (and (or (null min-exclusive) (>-using-type type v min-exclusive))
576 (or (null max-exclusive) (<-using-type type v max-exclusive))
577 (or (null min-inclusive) (>=-using-type type v min-inclusive))
578 (or (null max-inclusive) (<=-using-type type v max-inclusive)))))
581 ;;; length-mixin
583 (defclass length-mixin ()
584 ((exact-length :initform nil :initarg :exact-length :accessor exact-length)
585 (min-length :initform nil :initarg :min-length :accessor min-length)
586 (max-length :initform nil :initarg :max-length :accessor max-length)))
588 (defmethod describe-facets progn ((object length-mixin) stream)
589 (dolist (slot '(exact-length min-length max-length))
590 (let ((value (slot-value object slot)))
591 (when value
592 (format stream " ~A ~A"
593 (intern (symbol-name slot) :keyword)
594 value)))))
596 (defmethod parse-parameter
597 ((class-name (eql 'length-mixin)) (type-name t) (param t) value)
598 (parse (make-instance 'non-negative-integer-type) value nil))
600 ;; extra-hack fuer die "Laenge" eines QName...
601 (defgeneric length-using-type (type u))
602 (defmethod length-using-type ((type length-mixin) e) (length e))
604 (defmethod validp/xsd and ((type length-mixin) v context)
605 (declare (ignore context))
606 (with-slots (exact-length min-length max-length) type
607 (or (not (or exact-length min-length max-length))
608 (let ((l (length-using-type type v)))
609 (and (or (null exact-length) (eql l exact-length))
610 (or (null min-length) (>= l min-length))
611 (or (null max-length) (<= l max-length)))))))
614 ;;; enumeration-type
616 (defclass enumeration-type (xsd-type length-mixin)
617 ((word-type :reader word-type)))
619 (defmethod initialize-instance :after ((type enumeration-type) &key)
620 (setf (min-length type) (max* 1 (min-length type))))
622 (defmethod parse/xsd ((type enumeration-type) e context)
623 (let ((wt (word-type type)))
624 (loop
625 for word in (cl-ppcre:split " " e)
626 for v = (parse wt word context)
627 collect v
628 when (eq v :error) do (return :error))))
632 ;;;; Primitive types
634 ;;; duration
636 (defxsd (duration-type "duration") (xsd-type ordering-mixin)
638 (:documentation
639 "@short{The duration data type, representing a duration of time.}
641 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
642 the @a[http://www.w3.org/TR/xmlschema-2/#duration]{specification}.
644 @b{Implementation.} This type returns lists of the form
645 @code{(years months days hours minutes seconds)}. Each
646 value can be @code{nil} or a number. All values are integers
647 except for @code{seconds}, which is a real.
649 @b{Example.} @code{P1Y2M3DT10H30M}
650 maps to @code{(1 2 3 10 30 nil)}
652 @b{Parameters.} This type is ordered and allows the parameters
653 @fun{max-inclusive}, @fun{min-inclusive},
654 @fun{max-exclusive}, and @fun{min-exclusive}."))
656 (defmethod equal-using-type ((type duration-type) u v)
657 (equal u v))
659 ;; zzz das ist vielleicht ein bisschen zu woertlich implementiert
660 (defmethod lessp-using-type ((type duration-type) u v)
661 (let ((dt (make-instance 'date-time-type)))
662 (every (lambda (str)
663 (let ((s (parse dt str nil)))
664 (lessp-using-type dt
665 (datetime+duration s u)
666 (datetime+duration s v))))
667 '("1696-09-01T00:00:00Z"
668 "1697-02-01T00:00:00Z"
669 "1903-03-01T00:00:00Z"
670 "1903-07-01T00:00:00Z"))))
672 (defun datetime+duration (s d)
673 (destructuring-bind (syear smonth sday shour sminute ssecond szone) s
674 (destructuring-bind (dyear dmonth dday dhour dminute dsecond) d
675 (setf dhour (or dhour 0))
676 (setf dminute (or dminute 0))
677 (setf dsecond (or dsecond 0))
678 (labels ((floor3 (a low high)
679 (multiple-value-bind (u v)
680 (floor (- a low) (- high low))
681 (values u (+ low v))))
682 (maximum-day-in-month-for (yearvalue monthvalue)
683 (multiple-value-bind (m y)
684 (floor3 monthvalue 1 13)
685 (day-limit m (+ yearvalue y)))))
686 (multiple-value-bind (carry emonth) (floor3 (+ smonth dmonth) 1 13)
687 (let ((eyear (+ syear dyear carry))
688 (ezone szone))
689 (multiple-value-bind (carry esecond) (floor (+ ssecond dsecond) 60)
690 (multiple-value-bind (carry eminute)
691 (floor (+ sminute dminute carry) 60)
692 (multiple-value-bind (carry ehour)
693 (floor (+ shour dhour carry) 24)
694 (let* ((mdimf (maximum-day-in-month-for eyear emonth))
695 (tmpdays (max 1 (min sday mdimf)))
696 (eday (+ tmpdays dday carry)))
697 (loop
698 (let* ((mdimf (maximum-day-in-month-for eyear emonth))
699 (carry
700 (cond
701 ((< eday 1)
702 (setf eday (+ eday mdimf))
704 ((> eday mdimf)
705 (setf eday (- eday mdimf))
708 (return))))
709 (tmp (+ emonth carry)))
710 (multiple-value-bind (y m)
711 (floor3 tmp 1 13)
712 (setf emonth m)
713 (incf eyear y))))
714 (list eyear emonth eday ehour eminute esecond
715 ezone)))))))))))
717 (defun scan-to-strings (&rest args)
718 (coerce (nth-value 1 (apply #'cl-ppcre:scan-to-strings args)) 'list))
720 (defmethod parse/xsd ((type duration-type) e context)
721 (declare (ignore context))
722 (destructuring-bind (&optional minusp y m d tp h min s)
723 (scan-to-strings "(?x)
724 ^(-)? # minus
725 P(?:(\\d+)Y)? # years
726 (?:(\\d+)M)? # months
727 (?:(\\d+)D)? # days
728 (T # (time)
729 (?:(\\d+)H)? # hours
730 (?:(\\d+)M)? # minutes
731 (?:(\\d+(?:[.]\\d+)?)S)? # seconds
732 )?$"
734 (if (and (or y m d h min s)
735 (or (null tp) (or h min s)))
736 (let ((f (if minusp -1 1)))
737 (flet ((int (str)
738 (and str (* f (parse-integer str)))))
739 (list (int y) (int m) (int d) (int h) (int min)
740 (and s (* f (parse-number:parse-number s))))))
741 :error)))
744 ;;; dateTime
746 (defclass time-ordering-mixin (ordering-mixin) ())
748 (defxsd (date-time-type "dateTime") (xsd-type time-ordering-mixin)
750 (:documentation
751 "@short{The dateTime data type, representing a moment in time.}
753 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
754 the @a[http://www.w3.org/TR/xmlschema-2/#dateTime]{specification}.
756 @b{Implementation.} This type returns lists of the form
757 @code{(year month day hour minute second timezone)}. Each
758 value is an integer, except except for @code{second}, which is a real,
759 and @code{timezone} which is a real or @code{nil}.
760 A @code{timezone} of @code{nil} indicates UTC.
762 @b{Example.} @code{2002-10-10T12:00:00-05:00}
763 maps to @code{(2002 10 10 12 0 0 -5)}
765 @b{Parameters.} This type is ordered and allows the parameters
766 @fun{max-inclusive}, @fun{min-inclusive},
767 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
768 except within a timezone, see the spec for details."))
770 (defmethod equal-using-type ((type time-ordering-mixin) u v)
771 (equal u v))
773 ;; add zone-offset as a duration (if any), but keep a boolean in the
774 ;; zone-offset field indicating whether there was a time-zone
775 (defun normalize-date-time (u)
776 (destructuring-bind (year month day hour minute second zone-offset) u
777 (let ((v (list year month day hour minute second (and zone-offset t))))
778 (if zone-offset
779 (multiple-value-bind (h m)
780 (truncate zone-offset)
781 (datetime+timezone v h (* m 100)))
782 v))))
784 (defun datetime+timezone (d h m)
785 (datetime+duration d (list 0 0 0 h m 0)))
787 (defmethod lessp-using-type ((type time-ordering-mixin) p q)
788 (destructuring-bind (pyear pmonth pday phour pminute psecond pzone)
789 (normalize-date-time p)
790 (destructuring-bind (qyear qmonth qday qhour qminute qsecond qzone)
791 (normalize-date-time q)
792 (cond
793 ((and pzone (not qzone))
794 (lessp-using-type type p (datetime+timezone q 14 0)))
795 ((and (not pzone) qzone)
796 (lessp-using-type type (datetime+timezone p -14 0) q))
798 ;; zzz hier sollen wir <> liefern bei Feldern, die in genau einer
799 ;; der Zeiten fehlen. Wir stellen aber fehlende Felder derzeit
800 ;; defaulted dar, koennen diese Situation also nicht feststellen.
801 ;; Einen Unterschied sollte das nur machen, wenn Werte verschiedener
802 ;; Datentypen miteinander verglichen werden. Das bieten wir einfach
803 ;; nicht an.
804 (loop
805 for a in (list pyear pmonth pday phour pminute psecond)
806 for b in (list qyear qmonth qday qhour qminute qsecond)
808 (when (< a b)
809 (return t))
810 (when (> a b)
811 (return nil))))))))
813 (defun day-limit (m y)
814 (cond
815 ((and (eql m 2)
816 (or (zerop (mod y 400))
817 (and (zerop (mod y 4))
818 (not (zerop (mod y 100))))))
820 ((eql m 2) 28)
821 ((if (<= m 7) (oddp m) (evenp m)) 31)
822 (t 30)))
824 (defun parse-time (minusp y m d h min s tz tz-sign tz-h tz-m
825 &key (start 0) end)
826 (declare (ignore tz start end)) ;zzz
827 ;; parse into numbers
828 (flet ((int (str)
829 (and str (parse-integer str)))
830 (num (str)
831 (and str (parse-number:parse-number str))))
832 (setf (values y m d h min s tz-h tz-m)
833 (values (* (int y) (if minusp -1 1))
834 (int m) (int d) (int h) (int min)
835 (num s)
836 (int tz-h) (int tz-m))))
837 (let ((day-limit (day-limit m y)))
838 ;; check ranges
839 (cond
840 ((and y m d h min s
841 (plusp y)
842 (<= 1 m 12)
843 (<= 1 d day-limit)
844 (<= 0 h 24)
845 (<= 0 min 59)
846 ;; zzz sind leap seconds immer erlaubt?
847 (<= 0 s 60))
848 ;; 24:00:00 must be canonicalized
849 (when (and (eql h 24) (zerop min) (zerop s))
850 (incf h)
851 (incf d)
852 (when (> d day-limit)
853 (setf d 1)
854 (incf m)
855 (when (> m 12)
856 (incf y))))
857 (let ((tz-offset
858 (when tz-h
859 (* (if (equal tz-sign "-") -1 1)
860 (+ tz-h (/ tz-m 100))))))
861 (list (* y (if minusp -1 1)) m d h min s tz-offset)
862 ;; (subseq ... start end)
865 :error))))
867 (defmethod parse/xsd ((type date-time-type) e context)
868 (declare (ignore context))
869 (destructuring-bind (&optional minusp y m d h min s tz tz-sign tz-h tz-m)
870 (scan-to-strings "(?x)
871 ^(-)? # opt. minus
872 ((?:[1-9]\\d*)?\\d{4}) # year
873 -(\\d\\d) # month
874 -(\\d\\d) # day
875 T # (time)
876 (\\d\\d) # hour
877 :(\\d\\d) # minute
878 :(\\d+(?:[.]\\d+)?) # second
879 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
882 (parse-time minusp y m d h min s tz tz-sign tz-h tz-m)))
885 ;;; time
887 (defxsd (time-type "time") (xsd-type time-ordering-mixin)
889 (:documentation
890 "@short{The time data type, representing a time of day.}
892 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
893 the @a[http://www.w3.org/TR/xmlschema-2/#dateTime]{specification}.
895 @b{Implementation.} This type returns the same kind of lists as
896 @class{date-time-type}, except that the fields @code{year},
897 @code{month} and @code{day} are filled with dummy values from the
898 Gregorian year AD 1.
900 @b{Parameters.} This type is ordered and allows the parameters
901 @fun{max-inclusive}, @fun{min-inclusive},
902 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
903 except within a timezone, see the spec for details."))
905 (defmethod parse/xsd ((type time-type) e context)
906 (declare (ignore context))
907 (destructuring-bind (&optional h min s tz tz-sign tz-h tz-m)
908 (scan-to-strings "(?x)
909 ^(\\d\\d) # hour
910 :(\\d\\d) # minute
911 :(\\d+(?:[.]\\d+)?) # second
912 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
915 (parse-time nil "1" "1" "1" h min s tz tz-sign tz-h tz-m
916 :start 3)))
919 ;;; date
921 (defxsd (date-type "date") (xsd-type time-ordering-mixin)
923 (:documentation
924 "@short{The date data type, representing a day of the year.}
926 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
927 the @a[http://www.w3.org/TR/xmlschema-2/#date]{specification}.
929 @b{Implementation.} This type returns the same kind of lists as
930 @class{date-time-type}, except that the fields @code{hour},
931 @code{minute} and @code{second} are filled with dummy values from the
932 Gregorian year AD 1.
934 @b{Parameters.} This type is ordered and allows the parameters
935 @fun{max-inclusive}, @fun{min-inclusive},
936 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
937 except within a timezone, see the spec for details."))
939 (defmethod parse/xsd ((type date-type) e context)
940 (declare (ignore context))
941 (destructuring-bind (&optional minusp y m d tz tz-sign tz-h tz-m)
942 (scan-to-strings "(?x)
943 ^(-)? # opt. minus
944 ((?:[1-9]\\d*)?\\d{4}) # year
945 -(\\d\\d) # month
946 -(\\d\\d) # day
947 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
950 (parse-time minusp y m d "0" "0" "0" tz tz-sign tz-h tz-m
951 :end 3)))
954 ;;; gYearMonth
956 (defxsd (year-month-type "gYearMonth") (xsd-type time-ordering-mixin)
958 (:documentation
959 "@short{The gYearMonth data type, representing the calendar month of a
960 specific year.}
962 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
963 the @a[http://www.w3.org/TR/xmlschema-2/#gYearMonth]{specification}.
965 @b{Implementation.} This type returns the same kind of lists as
966 @class{date-time-type}, except that the fields @code{day}, @code{hour},
967 @code{minute} and @code{second} are filled with dummy values from the
968 Gregorian year AD 1.
970 @b{Parameters.} This type is ordered and allows the parameters
971 @fun{max-inclusive}, @fun{min-inclusive},
972 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
973 except within a timezone, see the spec for details."))
975 (defmethod parse/xsd ((type year-month-type) e context)
976 (declare (ignore context))
977 (destructuring-bind (&optional minusp y m)
978 (scan-to-strings "(?x)
979 ^(-)? # opt. minus
980 ((?:[1-9]\\d*)?\\d{4}) # year
981 -(\\d\\d) # month
984 (parse-time minusp y m "1" "0" "0" "0" nil nil nil nil
985 :end 2)))
988 ;;; gYear
990 (defxsd (year-type "gYear") (xsd-type time-ordering-mixin)
992 (:documentation
993 "@short{The gYear data type, representing a calendar year.}
995 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
996 the @a[http://www.w3.org/TR/xmlschema-2/#gYear]{specification}.
998 @b{Implementation.} This type returns the same kind of lists as
999 @class{date-time-type}, except that the fields @code{month}, @code{day},
1000 @code{hour}, @code{minute} and @code{second} are filled with dummy values
1001 from the Gregorian year AD 1.
1003 @b{Parameters.} This type is ordered and allows the parameters
1004 @fun{max-inclusive}, @fun{min-inclusive},
1005 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
1006 except within a timezone, see the spec for details."))
1008 (defmethod parse/xsd ((type year-type) e context)
1009 (declare (ignore context))
1010 (destructuring-bind (&optional minusp y tz tz-sign tz-h tz-m)
1011 (scan-to-strings "(?x)
1012 ^(-)? # opt. minus
1013 ((?:[1-9]\\d*)?\\d{4}) # year
1014 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
1017 (parse-time minusp y "1" "1" "0" "0" "0" tz tz-sign tz-h tz-m
1018 :end 1)))
1021 ;;; gMonthDay
1023 (defxsd (month-day-type "gMonthDay") (xsd-type time-ordering-mixin)
1025 (:documentation
1026 "@short{The gMonthDay data type, representing a calendar month and day.}
1028 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
1029 the @a[http://www.w3.org/TR/xmlschema-2/#monthDay]{specification}.
1031 @b{Implementation.} This type returns the same kind of lists as
1032 @class{date-time-type}, except that the fields @code{year},
1033 @code{hour}, @code{minute} and @code{second} are filled with dummy values
1034 from the Gregorian year AD 1.
1036 @b{Parameters.} This type is ordered and allows the parameters
1037 @fun{max-inclusive}, @fun{min-inclusive},
1038 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
1039 except within a timezone, see the spec for details."))
1041 (defmethod parse/xsd ((type month-day-type) e context)
1042 (declare (ignore context))
1043 (destructuring-bind (&optional m d tz tz-sign tz-h tz-m)
1044 (scan-to-strings "(?x)
1045 ^--(\\d\\d) # month
1046 -(\\d\\d) # day
1047 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
1050 (parse-time nil "1" m d "0" "0" "0" tz tz-sign tz-h tz-m
1051 :start 1 :end 3)))
1054 ;;; gDay
1056 (defxsd (day-type "gDay") (xsd-type time-ordering-mixin)
1058 (:documentation
1059 "@short{The gDay data type, representing a calendar day.}
1061 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
1062 the @a[http://www.w3.org/TR/xmlschema-2/#gDay]{specification}.
1064 @b{Implementation.} This type returns the same kind of lists as
1065 @class{date-time-type}, except that the fields @code{year}, @code{month},
1066 @code{hour}, @code{minute} and @code{second} are filled with dummy values
1067 from the Gregorian year AD 1.
1069 @b{Parameters.} This type is ordered and allows the parameters
1070 @fun{max-inclusive}, @fun{min-inclusive},
1071 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
1072 except within a timezone, see the spec for details."))
1074 (defmethod parse/xsd ((type day-type) e context)
1075 (declare (ignore context))
1076 (destructuring-bind (&optional d tz tz-sign tz-h tz-m)
1077 (scan-to-strings "(?x)
1078 ---(\\d\\d) # day
1079 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
1082 (parse-time nil "1" "1" d "0" "0" "0" tz tz-sign tz-h tz-m
1083 :start 3 :end 4)))
1086 ;;; gMonth
1088 (defxsd (month-type "gMonth") (xsd-type time-ordering-mixin)
1090 (:documentation
1091 "@short{The gMonth data type, representing a calendar month.}
1093 @b{Syntax.} This type accepts an ISO-like syntax. For details refer to
1094 the @a[http://www.w3.org/TR/xmlschema-2/#gMonth]{specification}.
1096 @b{Implementation.} This type returns the same kind of lists as
1097 @class{date-time-type}, except that the fields @code{year}, @code{day},
1098 @code{hour}, @code{minute} and @code{second} are filled with dummy values
1099 from the Gregorian year AD 1.
1101 @b{Parameters.} This type is ordered and allows the parameters
1102 @fun{max-inclusive}, @fun{min-inclusive},
1103 @fun{max-exclusive}, and @fun{min-exclusive}. The ordering is partial
1104 except within a timezone, see the spec for details."))
1106 (defmethod parse/xsd ((type month-type) e context)
1107 (declare (ignore context))
1108 (destructuring-bind (&optional m tz tz-sign tz-h tz-m)
1109 (scan-to-strings "(?x)
1110 ^--(\\d\\d) # month
1111 (([+-])(\\d\\d):(\\d\\d)|Z)? # opt timezone
1114 (parse-time nil "1" m "1" "0" "0" "0" tz tz-sign tz-h tz-m
1115 :start 2 :end 3)))
1118 ;;; boolean
1120 (defxsd (boolean-type "boolean") (xsd-type)
1122 (:documentation
1123 "@short{The boolean data type.}
1125 @b{Syntax.} \"1\", \"0\", \"true\", or \"false\".
1126 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#boolean]{specification}.
1128 @b{Implementation.} This type returns @code{t} or @code{nil}.
1130 @b{Parameters.} No parameters except for @fun{pattern} are available for
1131 this type."))
1133 (defmethod parse/xsd ((type boolean-type) e context)
1134 (declare (ignore context))
1135 (case (find-symbol e :keyword)
1136 ((:|true| :|1|) t)
1137 ((:|false| :|0|) nil)))
1140 ;;; base64Binary
1142 (defxsd (base64-binary-type "base64Binary") (xsd-type length-mixin)
1144 (:documentation
1145 "@short{The base64Binary data type.}
1147 @b{Syntax.} Normal Base64 syntax.
1148 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#base64Binary]{specification}.
1150 @b{Implementation.} This type returns an @code{(unsigned-byte 8)}
1151 vector.
1153 @b{Parameters.} This type allows restrictions on the length of the octet
1154 vector through the parameters @fun{exact-length}, @fun{min-length}, and
1155 @fun{max-length}."))
1157 (defmethod equal-using-type ((type base64-binary-type) u v)
1158 (equalp u v))
1160 (defmethod parse/xsd ((type base64-binary-type) e context)
1161 (declare (ignore context))
1162 (if (cl-ppcre:all-matches
1163 "(?x)
1164 ^(([A-Za-z0-9+/][ ]?[A-Za-z0-9+/][ ]?[A-Za-z0-9+/]
1165 [ ]?[A-Za-z0-9+/][ ]?)*
1166 (([A-Za-z0-9+/][ ]?[A-Za-z0-9+/][ ]?[A-Za-z0-9+/][ ]?[A-Za-z0-9+/])
1167 | ([A-Za-z0-9+/][ ]?[A-Za-z0-9+/][ ]?[AEIMQUYcgkosw048][ ]?=)
1168 | ([A-Za-z0-9+/][ ]?[AQgw][ ]?=[ ]?=)))?$"
1170 (handler-case
1171 (cl-base64:base64-string-to-usb8-array e)
1172 (warning (c)
1173 (error "unexpected failure in Base64 decoding: ~A" c)))
1174 :error))
1177 ;;; hexBinary
1179 (defxsd (hex-binary-type "hexBinary") (xsd-type length-mixin)
1181 (:documentation
1182 "@short{The hexBinary data type.}
1184 @b{Syntax.} A sequence of two-digit hexadecimal numbers representing
1185 one octet each.
1186 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#hexBinary]{specification}.
1188 @b{Implementation.} This type returns an @code{(unsigned-byte 8)}
1189 vector.
1191 @b{Parameters.} This type allows restrictions on the length of the octet
1192 vector through the parameters @fun{exact-length}, @fun{min-length}, and
1193 @fun{max-length}."))
1195 (defmethod equal-using-type ((type hex-binary-type) u v)
1196 (equalp u v))
1198 (defmethod parse/xsd ((type hex-binary-type) e context)
1199 (declare (ignore context))
1200 (if (evenp (length e))
1201 (let ((result
1202 (make-array (/ (length e) 2) :element-type '(unsigned-byte 8))))
1203 (loop
1204 for i from 0 below (length e) by 2
1205 for j from 0
1207 (setf (elt result j)
1208 (handler-case
1209 (parse-integer e :start i :end (+ i 2) :radix 16)
1210 (error ()
1211 (return :error))))
1212 finally (return result)))
1213 :error))
1216 ;;; float
1218 (defxsd (float-type "float") (xsd-type ordering-mixin)
1220 (:documentation
1221 "@short{The float data type.}
1223 @b{Syntax.} A floating-point number in a \"scientific notation\".
1224 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#float]{specification}.
1226 @b{Implementation.} This type returns a @code{single-float} or, on
1227 implementations where Infinity and Nan cannot be represented as such,
1228 a special symbol that is treated as if it was Infinity or NaN by the
1229 built-in ordering.
1231 @b{Parameters.} This type is ordered and allows the parameters
1232 @fun{max-inclusive}, @fun{min-inclusive},
1233 @fun{max-exclusive}, and @fun{min-exclusive}."))
1235 (defmethod equal-using-type ((type float-type) u v)
1236 #+(or sbcl allegro) (= u v)
1237 #-(or sbcl allegro) (float= u v))
1239 (defmethod lessp-using-type ((type float-type) u v)
1240 #+(or sbcl allegro) (< u v)
1241 #-(or sbcl allegro) (float< u v))
1243 ;; this one is more complex than would seem necessary, because too-large
1244 ;; and too-small values must be rounded to infinity rather than erroring out
1245 (defun parse-float (e min max +inf -inf nan)
1246 (cond
1247 ((equal e "INF") +inf)
1248 ((equal e "-INF") -inf)
1249 ((equal e "Nan") nan)
1251 (destructuring-bind (&optional a b)
1252 (scan-to-strings "^([^eE]+)(?:[eE]([^eE]+))?$" e)
1253 (if a
1254 (let* ((mantissa (parse/xsd (make-instance 'decimal-type) a nil))
1255 (exponent
1256 (when b
1257 (parse/xsd (make-instance 'integer-type) b nil))))
1258 (if (or (eq mantissa :error) (eq exponent :error))
1259 :error
1260 (let ((ratio (* mantissa (expt 10 (or exponent 1)))))
1261 (cond
1262 ((< ratio min) -inf)
1263 ((> ratio max) +inf)
1264 (t (float ratio min))))))
1265 :error)))))
1267 ;; zzz nehme hier an, dass single-float in IEEE single float ist.
1268 ;; Das stimmt unter LispWorks bestimmt wieder nicht.
1269 (defmethod parse/xsd ((type float-type) e context)
1270 (declare (ignore context))
1271 (parse-float e
1272 most-negative-single-float
1273 most-positive-single-float
1274 single-float-positive-infinity
1275 single-float-negative-infinity
1276 single-float-nan))
1279 ;;; decimal
1281 (defxsd (decimal-type "decimal") (xsd-type ordering-mixin)
1282 ((fraction-digits :initform nil
1283 :initarg :fraction-digits
1284 :accessor fraction-digits)
1285 (total-digits :initform nil
1286 :initarg :total-digits
1287 :accessor total-digits))
1288 (:documentation
1289 "@short{The decimal data type.}
1291 @b{Syntax.} A rational number, written using an optional decimal point
1292 and decimal places.
1293 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#decimal]{specification}.
1295 @b{Implementation.} This type returns a @code{rational}.
1297 @b{Parameters.} This type is ordered and allows the parameters
1298 @fun{max-inclusive}, @fun{min-inclusive},
1299 @fun{max-exclusive}, and @fun{min-exclusive}."))
1301 (defmethod describe-facets progn ((object decimal-type) stream)
1302 (dolist (slot '(fraction-digits total-digits))
1303 (let ((value (slot-value object slot)))
1304 (when value
1305 (format stream " ~A ~A"
1306 (intern (symbol-name slot) :keyword)
1307 value)))))
1309 (defmethod parse-parameter
1310 ((class-name (eql 'decimal-type))
1311 (type-name t)
1312 (param (eql :fraction-digits))
1313 value)
1314 (parse (make-instance 'non-negative-integer-type) value nil))
1316 (defmethod parse-parameter
1317 ((class-name (eql 'decimal-type))
1318 (type-name t)
1319 (param (eql :total-digits))
1320 value)
1321 (parse (make-instance 'positive-integer-type) value nil))
1323 (defmethod lessp-using-type ((type decimal-type) u v)
1324 (< u v))
1326 (defmethod equal-using-type ((type decimal-type) u v)
1327 (= u v))
1329 (defmethod validp/xsd and ((type decimal-type) v context)
1330 (declare (ignore context))
1331 (with-slots (fraction-digits total-digits) type
1332 (and (or (null fraction-digits)
1333 (let* ((betrag (abs v))
1334 (fraction (- betrag (truncate betrag)))
1335 (scaled (* fraction (expt 10 fraction-digits))))
1336 (zerop (mod scaled 1))))
1337 (or (null total-digits)
1338 (let ((scaled (abs v)))
1339 (loop
1340 until (zerop (mod scaled 1))
1341 do (setf scaled (* scaled 10)))
1342 (< scaled (expt 10 total-digits)))))))
1344 (defmethod parse/xsd ((type decimal-type) e context)
1345 (declare (ignore context))
1346 (destructuring-bind (&optional a b)
1347 (scan-to-strings "^([+-]?\\d*)(?:[.](\\d+))?$" e)
1348 (if (plusp (+ (length a) (length b)))
1349 (+ (if (plusp (length a))
1350 (parse-integer a)
1352 (if (plusp (length b))
1353 (/ (parse-integer b) (expt 10 (length b)))
1355 :error)))
1358 ;;; double
1360 (defxsd (double-type "double") (xsd-type ordering-mixin)
1362 (:documentation
1363 "@short{The double data type.}
1365 @b{Syntax.} A floating-point number in a \"scientific notation\".
1366 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#double]{specification}.
1368 @b{Implementation.} This type returns a @code{double-float} or, on
1369 implementations where Infinity and Nan cannot be represented as such,
1370 a special symbol that is treated as if it was Infinity or NaN by the
1371 built-in ordering.
1373 @b{Parameters.} This type is ordered and allows the parameters
1374 @fun{max-inclusive}, @fun{min-inclusive},
1375 @fun{max-exclusive}, and @fun{min-exclusive}."))
1377 (defmethod equal-using-type ((type double-type) u v)
1378 #+(or sbcl allegro) (= u v)
1379 #-(or sbcl allegro) (float= u v))
1381 (defmethod lessp-using-type ((type double-type) u v)
1382 #+(or sbcl allegro) (< u v)
1383 #-(or sbcl allegro) (float< u v))
1385 ;; zzz nehme hier an, dass double-float in IEEE double float ist.
1386 ;; Auch das ist nicht garantiert.
1387 (defmethod parse/xsd ((type double-type) e context)
1388 (declare (ignore context))
1389 (parse-float e
1390 most-negative-double-float
1391 most-positive-double-float
1392 double-float-positive-infinity
1393 double-float-negative-infinity
1394 double-float-nan))
1397 ;;; AnyURi
1399 (defxsd (any-uri-type "anyURI") (xsd-type length-mixin)
1401 (:documentation
1402 "@short{The anyURI data type.}
1404 @b{Syntax.} An arbitrary string (!).
1405 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#anyURI]{specification}.
1407 @b{Implementation.} This type returns a normalized string in which
1408 special characters have been escaped.
1410 @b{Parameters.} This type allows restrictions on the length of the
1411 normalized string through the parameters @fun{exact-length},
1412 @fun{min-length}, and @fun{max-length}."))
1414 (defmethod equal-using-type ((type any-uri-type) u v)
1415 (equal u v))
1417 (defmethod parse/xsd ((type any-uri-type) e context)
1418 (cxml-rng::escape-uri e))
1421 ;;; QName
1422 ;;; NOTATION
1424 (defclass qname-like (xsd-type length-mixin) ())
1426 (defxsd (qname-type "QName") (qname-like)
1428 (:documentation
1429 "@short{The QName data type.}
1431 @b{Syntax.} A Qualified Name, as per the \"Namespaces in XML\"
1432 specification. The namespace prefix must be bound to a namespace URI
1433 in the context.
1434 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#QName]{specification}.
1436 @b{Context dependent.} This type is context dependent and requires
1437 the @code{context} argument to @fun{parse} and @fun{validp}.
1439 @b{Implementation.} This type returns a structure with two components,
1440 the namespace URI and the local name. fixme: and the original length.
1441 fixme: export this structure.
1443 @b{Parameters.} This type allows restrictions on the length of the
1444 original QName through the parameters @fun{exact-length},
1445 @fun{min-length}, and @fun{max-length}."))
1447 (defxsd (notation-type "NOTATION") (qname-like)
1449 (:documentation
1450 "@short{The NOTATION data type.}
1452 @b{Syntax.} A qualified name.
1453 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#NOTATION]{specification}.
1455 @b{Implementation.} This type is treated exactly like
1456 @class{qname-type}, as specified in
1457 @a[http://relaxng.org/xsd-20010907.html]{Guidelines for using W3C XML
1458 Schema Datatypes with RELAX NG}.
1460 @b{Parameters.} This type allows restrictions on the length of the
1461 original QName through the parameters @fun{exact-length},
1462 @fun{min-length}, and @fun{max-length}."))
1464 (defstruct (qname (:constructor make-qname (uri lname length)))
1466 lname
1467 length)
1469 (defmethod length-using-type ((type qname-like) e)
1470 (qname-length e))
1472 (defmethod equal-using-type ((type qname-like) u v)
1473 (and (equal (qname-uri u) (qname-uri v))
1474 (equal (qname-lname u) (qname-lname v))))
1476 (defun namep (str)
1477 (and (not (zerop (length str)))
1478 (cxml::name-start-rune-p (elt str 0))
1479 (every #'cxml::name-rune-p str)))
1481 (defmethod parse/xsd ((type qname-like) e context)
1482 (handler-case
1483 (if (namep e)
1484 (multiple-value-bind (prefix local-name) (cxml::split-qname e)
1485 (let ((uri (when prefix
1486 (context-find-namespace-binding context prefix))))
1487 (if (and prefix (not uri))
1488 :error
1489 (make-qname uri local-name (length e)))))
1490 :error)
1491 (cxml:well-formedness-violation ()
1492 :error)))
1495 ;;; string
1497 (defxsd (xsd-string-type "string") (xsd-type length-mixin)
1499 (:documentation
1500 "@short{The string data type.}
1502 @b{Syntax.} An arbitrary string.
1503 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#string]{specification}.
1505 @b{Implementation.} Returns the string unchanged. This is the only
1506 XSD type that does not normalize or replace whitespace.
1508 @b{Parameters.} This type allows restrictions on the length of the
1509 string through the parameters @fun{exact-length},
1510 @fun{min-length}, and @fun{max-length}."))
1512 (defmethod equal-using-type ((type xsd-string-type) u v)
1513 (equal u v))
1515 (defmethod munge-whitespace ((type xsd-string-type) e)
1518 (defmethod parse/xsd ((type xsd-string-type) e context)
1522 ;;;;
1523 ;;;; Derived types
1524 ;;;;
1526 ;;; normalizedString
1528 (defxsd (normalized-string-type "normalizedString") (xsd-string-type)
1530 (:documentation
1531 "@short{The normalizedString data type, derived from string.}
1533 @b{Syntax.} An arbitrary string.
1534 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#normalizedString]{specification}.
1536 @b{Implementation.} Returns the string with whitespace replaced.
1538 I.e., each whitespace character is replaced by a space
1539 (character code 32), but multiple spaces, as well as
1540 leading and trailing spaces will still be returned.
1542 (This is the only XSD type that replaces whitespace in this way.)
1544 @b{Parameters.} This type allows restrictions on the length of the
1545 normalized string through the parameters @fun{exact-length},
1546 @fun{min-length}, and @fun{max-length}."))
1548 (defmethod munge-whitespace ((type normalized-string-type) e)
1549 (replace-whitespace e))
1552 ;;; token
1554 (defxsd (xsd-token-type "token") (normalized-string-type)
1556 (:documentation
1557 "@short{The token data type, derived from normalizedString.}
1559 @b{Syntax.} An arbitrary string.
1560 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#token]{specification}.
1562 @b{Implementation.} Returns the string with normalized whitespace.
1564 I.e., each whitespace character is replaced by a space
1565 (character code 32), multiple spaces are collapsed into one character,
1566 and leading and trailing spaces will be removed.
1568 (This is the standard behaviour of all XSD types with the exception of
1569 token's supertypes @class{string-type} and @class{normalized-string-type}.)
1571 @b{Parameters.} This type allows restrictions on the length of the
1572 normalized string through the parameters @fun{exact-length},
1573 @fun{min-length}, and @fun{max-length}."))
1575 (defmethod munge-whitespace ((type xsd-token-type) e)
1576 (normalize-whitespace e))
1579 ;;; language
1581 (defxsd (language-type "language") (xsd-token-type)
1582 ((patterns :initform '("[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*")))
1583 (:documentation
1584 "@short{The language data type, derived from token.}
1586 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#language]{specification}.
1588 @b{Restrictions.} This type restricts its supertype @class{token-type}
1589 to strings of the pattern \"[a-zA-Z]{1,8@}(-[a-zA-Z0-9]{1,8@})*\".
1591 @b{Parameters and implementation.} Unchanged from the supertype."))
1594 ;;; Name
1596 (defxsd (name-type "Name") (xsd-token-type)
1597 ((patterns :initform '("\\i\\c*")))
1598 (:documentation
1599 "@short{The Name data type, derived from token.}
1601 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#Name]{specification}.
1603 @b{Restrictions.} This type restricts its supertype @class{token-type}
1604 to strings of the pattern \"\\i\\c*\".
1606 @b{Parameters and implementation.} Unchanged from the supertype."))
1609 ;;; NCName
1611 (defxsd (ncname-type "NCName") (name-type)
1612 ((patterns :initform '("[\\i-[:]][\\c-[:]]*")))
1613 (:documentation
1614 "@short{The NCName data type, derived from Name.}
1616 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#NCName]{specification}.
1618 @b{Restrictions.} This type restricts its supertype @class{name-type}
1619 to strings of the pattern \"[\\i-[:]][\\c-[:]]*\".
1621 @b{Parameters and implementation.} Unchanged from the supertype."))
1623 (defmethod equal-using-type ((type ncname-type) u v)
1624 (equal u v))
1626 (defun nc-name-p (str)
1627 (and (namep str) (cxml::nc-name-p str)))
1629 (defmethod parse/xsd ((type ncname-type) e context)
1630 ;; zzz mit pattern machen
1631 (if (nc-name-p e)
1633 :error))
1635 ;;; ID
1637 (defxsd (id-type "ID") (ncname-type)
1639 (:documentation
1640 "@short{The ID data type, derived from NCName.}
1642 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#ID]{specification}.
1644 @b{Restrictions.} None, except when used with DTD compatibility.
1645 See @a[http://relaxng.org/xsd-20010907.html]{Guidelines for using W3C XML
1646 Schema Datatypes with RELAX NG}.
1647 (fixme: not implemented yet -- dfl, 2007-06-06)
1649 @b{Parameters and implementation.} Unchanged from the supertype."))
1652 ;;; IDREF
1654 (defxsd (idref-type "IDREF") (id-type)
1656 (:documentation
1657 "@short{The IDREF data type, derived from ID.}
1659 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#IDREF]{specification}.
1661 @b{Restrictions.} None, except when used with DTD compatibility.
1662 See @a[http://relaxng.org/xsd-20010907.html]{Guidelines for using W3C XML
1663 Schema Datatypes with RELAX NG}.
1664 (fixme: not implemented yet -- dfl, 2007-06-06)
1666 @b{Parameters and implementation.} Unchanged from the supertype."))
1669 ;;; IDREFS
1671 (defxsd (idrefs-type "IDREFS") (enumeration-type)
1672 ((word-type :initform (make-instance 'idref-type)))
1673 (:documentation
1674 "@short{The IDREFS data type, an enumeration.}
1676 @b{Syntax.} A whitespace-separated sequence of @class{idref-type}
1677 values, with at least one element.
1679 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#IDREFS]{specification}.
1681 @b{Implementation.} This type returns a list of the values as returned by
1682 @class{idref-type}.
1684 @b{Parameters.} This type allows restrictions on the number of values
1685 through the parameters @fun{exact-length},@fun{min-length}, and
1686 @fun{max-length}."))
1689 ;;; ENTITY
1691 (defxsd (entity-type "ENTITY") (ncname-type)
1693 (:documentation
1694 "@short{The ENTITY data type, derived from NCName.}
1696 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#ENTITY]{specification}.
1698 @b{Restrictions.} This type restricts its supertype @class{ncname-type}
1699 to names that have been declared as unparsed entities in the context.
1701 @b{Context dependent.} This type is context dependent and requires
1702 the @code{context} argument to @fun{parse} and @fun{validp}.
1704 @b{Parameters and implementation.} Unchanged from the supertype."))
1706 (defmethod parse/xsd ((type entity-type) e context)
1707 (if (context-find-unparsed-entity context e)
1709 :error))
1712 ;;; ENTITIES
1714 (defxsd (entities-type "ENTITIES") (enumeration-type)
1715 ((word-type :initform (make-instance 'entity-type)))
1716 (:documentation
1717 "@short{The ENTITIES data type, an enumeration.}
1719 @b{Syntax.} A whitespace-separated sequence of @class{entity-type}
1720 values, with at least one element.
1722 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#ENTITIES]{specification}.
1724 @b{Implementation.} This type returns a list of the values as returned by
1725 @class{entity-type}.
1727 @b{Context dependent.} This type is context dependent and requires
1728 the @code{context} argument to @fun{parse} and @fun{validp}.
1730 @b{Parameters.} This type allows restrictions on the number of values
1731 through the parameters @fun{exact-length},@fun{min-length}, and
1732 @fun{max-length}."))
1735 ;;; NMTOKEN
1737 (defxsd (nmtoken-type "NMTOKEN") (xsd-token-type)
1738 ((patterns :initform '("\\c+")))
1739 (:documentation
1740 "@short{The NMTOKEN data type, derived from token.}
1742 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#NMTOKEN]{specification}.
1744 @b{Restrictions.} This type restricts its supertype @class{token-type}
1745 to strings of the pattern \"\\c+\".
1747 @b{Parameters and implementation.} Unchanged from the supertype."))
1750 ;;; NMTOKENS
1752 (defxsd (nmtokens-type "NMTOKENS") (enumeration-type)
1753 ((word-type :initform (make-instance 'nmtoken-type)))
1754 (:documentation
1755 "@short{The NMTOKENS data type, an enumeration.}
1757 @b{Syntax.} A whitespace-separated sequence of @class{nmtoken-type}
1758 values, with at least one element.
1760 C.f. the @a[http://www.w3.org/TR/xmlschema-2/#NMTOKENS]{specification}.
1762 @b{Implementation.} This type returns a list of the values as returned by
1763 @class{nmtoken-type}.
1765 @b{Parameters.} This type allows restrictions on the number of values
1766 through the parameters @fun{exact-length},@fun{min-length}, and
1767 @fun{max-length}."))
1770 ;;; integer
1772 (defxsd (integer-type "integer") (decimal-type) ())
1774 ;; period is forbidden, so there's no point in letting decimal handle parsing
1775 ;; fixme: sind fuehrende nullen nun erlaubt oder nicht? die spec sagt ja,
1776 ;; das pattern im schema nicht.
1777 (defmethod parse/xsd ((type integer-type) e context)
1778 (declare (ignore context))
1779 (if (cl-ppcre:all-matches "^[+-]?(?:[1-9]\\d*|0)$" e)
1780 (parse-number:parse-number e)
1781 :error))
1784 ;;; nonPositiveInteger
1786 (defxsd (non-positive-integer-type "nonPositiveInteger") (integer-type) ())
1788 (defun min* (a b)
1789 (cond
1790 ((null a) b)
1791 ((null b) a)
1792 (t (min a b))))
1794 (defun max* (a b)
1795 (cond
1796 ((null a) b)
1797 ((null b) a)
1798 (t (max a b))))
1800 (defmethod initialize-instance :after ((type non-positive-integer-type) &key)
1801 (setf (max-inclusive type)
1802 (min* 0 (max-inclusive type))))
1805 ;;; nonPositiveInteger
1807 (defxsd (negative-integer-type "negativeInteger") (non-positive-integer-type)
1810 (defmethod initialize-instance :after ((type negative-integer-type) &key)
1811 (setf (max-inclusive type)
1812 (min* -1 (max-inclusive type))))
1815 ;;; long
1817 (defxsd (long-type "long") (integer-type) ())
1819 (defmethod initialize-instance :after ((type long-type) &key)
1820 (setf (max-inclusive type) (min* 9223372036854775807 (max-inclusive type)))
1821 (setf (min-inclusive type) (max* -9223372036854775808 (min-inclusive type))))
1824 ;;; int
1826 (defxsd (int-type "int") (long-type) ())
1828 (defmethod initialize-instance :after ((type int-type) &key)
1829 (setf (max-inclusive type) (min* 2147483647 (max-inclusive type)))
1830 (setf (min-inclusive type) (max* -2147483648 (min-inclusive type))))
1833 ;;; short
1835 (defxsd (short-type "short") (int-type) ())
1837 (defmethod initialize-instance :after ((type short-type) &key)
1838 (setf (max-inclusive type) (min* 32767 (max-inclusive type)))
1839 (setf (min-inclusive type) (max* -32768 (min-inclusive type))))
1842 ;;; byte
1844 (defxsd (byte-type "byte") (short-type) ())
1846 (defmethod initialize-instance :after ((type byte-type) &key)
1847 (setf (max-inclusive type) (min* 127 (max-inclusive type)))
1848 (setf (min-inclusive type) (max* -128 (min-inclusive type))))
1851 ;;; nonNegativeInteger
1853 (defxsd (non-negative-integer-type "nonNegativeInteger") (integer-type) ())
1855 (defmethod initialize-instance :after ((type non-negative-integer-type) &key)
1856 (setf (min-inclusive type) (max* 0 (min-inclusive type))))
1859 ;;; unsignedLong
1861 (defxsd (unsigned-long-type "unsignedLong") (non-negative-integer-type) ())
1863 (defmethod initialize-instance :after ((type unsigned-long-type) &key)
1864 (setf (max-inclusive type) (min* 18446744073709551615 (max-inclusive type))))
1867 ;;; unsignedInt
1869 (defxsd (unsigned-int-type "unsignedInt") (unsigned-long-type) ())
1871 (defmethod initialize-instance :after ((type unsigned-int-type) &key)
1872 (setf (max-inclusive type) (min* 4294967295 (max-inclusive type))))
1875 ;;; unsignedShort
1877 (defxsd (unsigned-short-type "unsignedShort") (unsigned-int-type) ())
1879 (defmethod initialize-instance :after ((type unsigned-short-type) &key)
1880 (setf (max-inclusive type) (min* 65535 (max-inclusive type))))
1883 ;;; unsignedByte
1885 (defxsd (unsigned-byte-type "unsignedByte") (unsigned-short-type) ())
1887 (defmethod initialize-instance :after ((type unsigned-byte-type) &key)
1888 (setf (max-inclusive type) (min* 255 (max-inclusive type))))
1891 ;;; positiveInteger
1893 (defxsd (positive-integer-type "positiveInteger") (non-negative-integer-type)
1896 (defmethod initialize-instance :after ((type positive-integer-type) &key)
1897 (setf (min-inclusive type) (max* 1 (min-inclusive type))))