summaryrefslogtreecommitdiff
path: root/lisp/nxml/rng-xsd.el
blob: 81314b85ca94d99d974902c63a3671c2051d6a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
;;; rng-xsd.el --- W3C XML Schema datatypes library for RELAX NG  -*- lexical-binding:t -*-

;; Copyright (C) 2003, 2007-2021 Free Software Foundation, Inc.

;; Author: James Clark
;; Keywords: wp, hypermedia, languages, XML, RelaxNG

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; The main entry point is `rng-xsd-compile'. The validator
;; knows to use this for the datatype library with URI
;; http://www.w3.org/2001/XMLSchema-datatypes because it
;; is the value of the rng-dt-compile property on that URI
;; as a symbol.
;;
;; W3C XML Schema Datatypes are specified by
;;   http://www.w3.org/TR/xmlschema-2/
;; Guidelines for using them with RELAX NG are described in
;;   http://relaxng.org/xsd.html

;;; Code:

(require 'rng-dt)
(require 'rng-util)
(require 'xsd-regexp)

;;;###autoload
(put 'http://www.w3.org/2001/XMLSchema-datatypes
     'rng-dt-compile
     #'rng-xsd-compile)

;;;###autoload
(defun rng-xsd-compile (name params)
  "Provide W3C XML Schema as a RELAX NG datatypes library.
NAME is a symbol giving the local name of the datatype.  PARAMS is a
list of pairs (PARAM-NAME . PARAM-VALUE) where PARAM-NAME is a symbol
giving the name of the parameter and PARAM-VALUE is a string giving
its value.  If NAME or PARAMS are invalid, it calls `rng-dt-error'
passing it arguments in the same style as format; the value from
`rng-dt-error' will be returned.  Otherwise, it returns a list.  The
first member of the list is t if any string is a legal value for the
datatype and nil otherwise.  The second argument is a symbol; this
symbol will be called as a function passing it a string followed by
the remaining members of the list.  The function must return an object
representing the value of the datatype that was represented by the
string, or nil if the string is not a representation of any value.
The object returned can be any convenient non-nil value, provided
that, if two strings represent the same value, the returned objects
must be equal."
  (let ((convert (get name 'rng-xsd-convert)))
    (if (not convert)
	(rng-dt-error "There is no XSD datatype named %s" name)
      (rng-xsd-compile1 name params convert))))

;;; Parameters

(defun rng-xsd-compile1 (name params convert)
  (if (null params)
      (cons (equal convert '(identity))
	    (cond ((eq name 'string) convert)
		  ((eq name 'normalizedString)
		   (cons 'rng-xsd-replace-space convert))
		  ((and (not (eq name 'string))
			(or (memq 'identity convert)
			    (memq 'rng-xsd-convert-any-uri convert)
			    (memq 'rng-xsd-check-pattern convert)))
		   (cons 'rng-xsd-collapse-space convert))
		  (t convert)))
    (let* ((param (car params))
	   (param-name (car param))
	   (param-value (cdr param)))
      (cond ((memq param-name
		   '(minExclusive maxExclusive minInclusive maxInclusive))
	     (let ((limit (apply (car convert)
				 (cons param-value
				       (cdr convert))))
		   (less-than-fun (get name 'rng-xsd-less-than)))
	       (cond ((not limit)
		      (rng-dt-error "Minimum value %s is not valid"
				    param-value))
		     ((not less-than-fun)
		      (rng-dt-error "Values of type %s are not ordered"
				    param-name))
		     (t
		      (rng-xsd-compile1 name
					(cdr params)
					(cons (get param-name
						   'rng-xsd-check)
					      (cons less-than-fun
						    (cons limit convert))))))))
	    ((memq param-name '(length minLength maxLength))
	     (let ((limit (rng-xsd-string-to-non-negative-integer param-value))
		   (length-fun (get name 'rng-xsd-length)))
	       (cond ((not limit)
		      (rng-dt-error "Length %s is not valid" param-value))
		     ((not length-fun)
		      (rng-dt-error "Values of type %s do not have a length"
				    param-name))
		     (t
		      (rng-xsd-compile1 name
					(cdr params)
					(cons (get param-name
						   'rng-xsd-check)
					      (cons length-fun
						    (cons limit convert))))))))
	    ((memq param-name '(fractionDigits totalDigits))
	     (let ((n (rng-xsd-string-to-non-negative-integer param-value)))
	       (cond ((not n)
		      (rng-dt-error "Number of digits %s is not valid"
				    param-value))
		     (t
		      (rng-xsd-compile1 name
					(cdr params)
					(cons (get param-name
						   'rng-xsd-check)
					      (cons n convert)))))))
	    ((eq param-name 'pattern)
	     (condition-case err
		 (rng-xsd-compile1 name
				   (cdr params)
				   (cons 'rng-xsd-check-pattern
					 (cons (concat
						"\\`"
						(xsdre-translate param-value)
						"\\'")
					       convert)))
	       (xsdre-invalid-regexp
		(rng-dt-error "Invalid regular expression (%s)"
			      (nth 1 err)))))
	    ((memq param-name '(enumeration whiteSpace))
	     (rng-dt-error "Facet %s cannot be used in RELAX NG" param-name))
	    (t (rng-dt-error "Unknown facet %s" param-name))))))

(defun rng-xsd-string-to-non-negative-integer (str)
  (and (rng-xsd-convert-integer str)
       (let ((n (string-to-number str)))
	 (and (integerp n)
	      (>= n 0)
	      n))))

(defun rng-xsd-collapse-space (str convert &rest args)
  (apply convert (cons (mapconcat 'identity (split-string str "[ \t\n\r]+")
				  " ")
		       args)))

(defun rng-xsd-replace-space (str convert &rest args)
  (apply convert
	 (cons (let ((i 0)
		     copied)
		 (while (and (setq i (string-match "[\r\n\t]" str i))
			     (or copied (setq copied (copy-sequence str)))
			     (aset copied i 32)
			     (setq i (1+ i))))
		 (or copied str))
	       args)))

(put 'minExclusive 'rng-xsd-check 'rng-xsd-check-min-exclusive)
(put 'minInclusive 'rng-xsd-check 'rng-xsd-check-min-inclusive)
(put 'maxExclusive 'rng-xsd-check 'rng-xsd-check-max-exclusive)
(put 'maxInclusive 'rng-xsd-check 'rng-xsd-check-max-inclusive)
(put 'length 'rng-xsd-check 'rng-xsd-check-length)
(put 'minLength 'rng-xsd-check 'rng-xsd-check-min-length)
(put 'maxLength 'rng-xsd-check 'rng-xsd-check-max-length)
(put 'fractionDigits 'rng-xsd-check 'rng-xsd-check-fraction-digits)
(put 'totalDigits 'rng-xsd-check 'rng-xsd-check-total-digits)

(defun rng-xsd-check-min-exclusive (str less-than-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (funcall less-than-fun limit obj)
	 obj)))

(defun rng-xsd-check-min-inclusive (str less-than-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (or (funcall less-than-fun limit obj)
	     (equal limit obj))
	 obj)))

(defun rng-xsd-check-max-exclusive (str less-than-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (funcall less-than-fun obj limit)
	 obj)))

(defun rng-xsd-check-max-inclusive (str less-than-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (or (funcall less-than-fun obj limit)
	     (equal obj limit))
	 obj)))

(defun rng-xsd-check-min-length (str length-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (>= (funcall length-fun obj) limit)
	 obj)))

(defun rng-xsd-check-max-length (str length-fun limit convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (<= (funcall length-fun obj) limit)
	 obj)))

(defun rng-xsd-check-length (str length-fun len convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (= (funcall length-fun obj) len)
	 obj)))

(defun rng-xsd-check-fraction-digits (str n convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (<= (length (aref obj 2)) n)
	 obj)))

(defun rng-xsd-check-total-digits (str n convert &rest args)
  (let ((obj (apply convert (cons str args))))
    (and obj
	 (<= (+ (length (aref obj 1))
		(length (aref obj 2)))
	     n)
	 obj)))

(defun rng-xsd-check-pattern (str regexp convert &rest args)
  (and (let ((case-fold-search nil)) (string-match regexp str))
       (apply convert (cons str args))))


(defun rng-xsd-convert-boolean (string)
  (and (string-match "\\`[ \t\n\r]*\\(?:\\(true\\|1\\)\\|false\\|0\\)[ \t\n\r]*\\'" string)
       (if (match-beginning 1) 'true 'false)))

(defun rng-xsd-convert-decimal (string)
  "Convert a string representing a decimal to an object representing it values.
A decimal value is represented by a vector [SIGN INTEGER-DIGITS
FRACTION-DIGITS] where SIGN is 1 or -1, INTEGER-DIGITS is a string
containing zero or more digits, with no leading zero, and
FRACTION-DIGITS is a string containing zero or more digits with no
trailing digits.  For example, -0021.0430 would be represented by [-1
\"21\" \"043\"]."
  (and (string-match "\\`[ \t\n\r]*\\([-+]\\)?\\(0*\\([1-9][0-9]*\\)?\\(\\.\\([0-9]*[1-9]\\)?0*\\)?\\)[ \t\n\r]*\\'" string)
       (let ((digits (match-string 2 string)))
	 (and (not (string= digits "."))
	      (not (string= digits ""))))
       (let ((integer-digits (match-string 3 string)))
	 (vector (if (and (equal (match-string 1 string) "-")
			  ;; Normalize -0 to 0
			  integer-digits)
		     -1
		   1)
		 (or integer-digits "")
		 (or (match-string 5 string) "")))))

(defun rng-xsd-convert-integer (string)
  (and (string-match "\\`[ \t\n\r]*\\([-+]\\)?\\(?:0*\\([1-9][0-9]*\\)\\|0+\\)[ \t\n\r]*\\'" string)
       (let ((integer-digits (match-string 2 string)))
	 (vector (if (and (equal (match-string 1 string) "-")
			  ;; Normalize -0 to 0
			  integer-digits)
		     -1
		   1)
		 (or integer-digits "")
		 ""))))

(defun rng-xsd-decimal< (n1 n2)
  (< (rng-xsd-compare-decimal n1 n2) 0))

(defun rng-xsd-compare-decimal (n1 n2)
  "Return a < 0, 0, > 0 according as n1 < n2, n1 = n2 or n1 > n2."
  (let* ((sign1 (aref n1 0))
	 (sign2 (aref n2 0))
	 (sign (- sign1 sign2)))
    (if (= sign 0)
	(* sign1
	   (let* ((int1 (aref n1 1))
		  (int2 (aref n2 1))
		  (len1 (length int1))
		  (len2 (length int2))
		  (lencmp (- len1 len2)))
	     (if (eq lencmp 0)
		 (if (string= int1 int2)
		     (rng-xsd-strcmp (aref n1 2) (aref n2 2))
		   (rng-xsd-strcmp int1 int2))
	       lencmp)))
      sign)))

(defconst rng-xsd-float-regexp
  (concat "\\`[ \r\n\t]*\\(?:"
	  "\\("
	  "[-+]?\\(?:[0-9]+\\(?:\\.[0-9]*\\)?\\|\\.[0-9]+\\)"
	  "\\(?:[eE][-+]?[0-9]+\\)?"
	  "\\)"
	  "\\|\\(INF\\)"
	  "\\|\\(-INF\\)"
	  "\\|\\(NaN\\)"
	  "\\)[ \r\n\t]*\\'"))

(defun rng-xsd-convert-float (string)
  (cond ((not (string-match rng-xsd-float-regexp string)) nil)
	((match-beginning 1)
	 (float (string-to-number (match-string 1 string))))
	((match-beginning 2) 1.0e+INF)
	((match-beginning 3) -1.0e+INF)
	;; Don't use a NaN float because we want NaN to be equal to NaN
	((match-beginning 4) 'NaN)))

(defun rng-xsd-float< (f1 f2)
  (and (not (eq f1 'NaN))
       (not (eq f2 'NaN))
       (< f1 f2)))

(defun rng-xsd-convert-token (string regexp)
  (and (string-match regexp string)
       (match-string 1 string)))

(defun rng-xsd-convert-hex-binary (string)
  (and (string-match "\\`[ \r\n\t]*\\(\\(?:[[:xdigit:]][[:xdigit:]]\\)*\\)[ \r\n\t]*\\'"
		     string)
       (downcase (match-string 1 string))))

(defun rng-xsd-hex-binary-length (obj)
  (/ (length obj) 2))

(defconst rng-xsd-base64-binary-regexp
  (let ((S "[ \t\r\n]*")
	(B04 "[AQgw]")
	(B16 "[AEIMQUYcgkosw048]")
	(B64 "[A-Za-z0-9+/]"))
    (concat "\\`" S "\\(?:\\(?:" B64 S "\\)\\{4\\}\\)*"
	    "\\(?:" B64 S B64 S B16 S "=" S
	    "\\|" B64 S B04 S "=" S "=" S "\\)?\\'")))

(defun rng-xsd-convert-base64-binary (string)
  (and (string-match rng-xsd-base64-binary-regexp string)
       (replace-regexp-in-string "[ \t\r\n]+" "" string t t)))

(defun rng-xsd-base64-binary-length (obj)
  (let ((n (* (/ (length obj) 4) 3)))
    (if (and (> n 0)
	     (string= (substring obj -1) "="))
	(- n (if (string= (substring obj -2) "==")
		 2
	       1))
      n)))

(defun rng-xsd-convert-any-uri (string)
  (and (string-match "\\`\\(?:[^%]\\|%[[:xdigit:]][[:xdigit:]]\\)*\\'" string)
       (string-match "\\`[^#]*\\(?:#[^#]*\\)?\\'" string)
       (string-match "\\`\\(?:[a-zA-Z][-+.A-Za-z0-9]*:.+\\|[^:]*\\(?:[#/?].*\\)?\\)\\'" string)
       string))

(defun rng-xsd-make-date-time-regexp (template)
  "Return a regular expression matching a ISO 8601 date/time.
The template is a string with Y standing for years field, M standing
for months, D standing for day of month, T standing for a literal T, t
standing for time and - standing for a literal hyphen.  A time zone is
always allowed at the end.  Regardless of the fields appearing in the
template, the regular expression will have twelve groups matching the
year sign, year, month, day of month, hours, minutes, integer seconds,
fractional seconds (including leading period), time zone, time zone
sign, time zone hours, time zone minutes."
  (let ((i 0)
	(len (length template))
	(parts nil)
	first last c)
    (while (< i len)
      (setq c (aref template i))
      (setq parts
	    (cons (cond ((eq c ?Y)
			 (setq first 0)
			 (setq last 1)
			 "\\(-\\)?\\(\\(?:[1-9][0-9]*\\)?[0-9]\\{4\\}\\)")
			((eq c ?M)
			 (or first
			     (setq first 2))
			 (setq last 2)
			 "\\([0-9][0-9]\\)")
			((eq c ?D)
			 (or first
			     (setq first 3))
			 (setq last 3)
			 "\\([0-9][0-9]\\)")
			((eq c ?t)
			 (or first
			     (setq first 4))
			 (setq last 7)
			 "\\([0-9][0-9]\\):\\([0-9][0-9]\\):\\([0-9][0-9]\\)\\(\\.[0-9]*\\)?")
			(t (string c)))
		  parts))
      (setq i (1+ i)))
    (while (< last 7)
      (setq last (1+ last))
      ;; Add dummy fields that can never much but keep the group
      ;; numbers uniform.
      (setq parts (cons "\\(\\'X\\)?" parts)))
    (setq parts (cons "\\(Z\\|\\([-+]\\)\\([0-9][0-9]\\):\\([0-5][0-9]\\)\\)?[ \t\n\r]*\\'"
		      parts))
    (setq parts (cons "\\`[ \t\n\r]*" (nreverse parts)))
    (while (> first 0)
      (setq first (1- first))
      (setq parts (cons "\\(X\\)?" parts)))
    (apply 'concat parts)))

(defconst rng-xsd-seconds-per-day (* 24 60 60))
(defconst rng-xsd-days-in-month [31 28 31 30 31 30 31 31 30 31 30 31])

(defun rng-xsd-days-in-month (year month)
  (if (and (= month 2) (rng-xsd-leap-year-p year))
      29
    (aref rng-xsd-days-in-month (1- month))))

(defconst rng-xsd-months-to-days
  (let ((v (make-vector 12 nil))
	(total 0)
	(i 0))
    (while (< i 12)
      (setq total (+ total (aref rng-xsd-days-in-month i)))
      (aset v i total)
      (setq i (1+ i)))
    v))

(defun rng-xsd-convert-date-time (string regexp)
  "Convert an XML Schema date/time to a list.
Returns nil if invalid.  REGEXP is a regexp for parsing the date time
as returned by `rng-xsd-make-date-time-regexp'.  The list has 4 members
\(HAS-TIME-ZONE DAY SECOND SECOND-FRACTION), where HAS-TIME-ZONE is t
or nil depending on whether a time zone was specified, DAY is an
integer giving a day number (with Jan 1 1AD being day 1), SECOND is the
second within that day, and SECOND-FRACTION is a float giving the
fractional part of the second."
  (and (string-match regexp string)
       (let ((year-sign (match-string 1 string))
	     (year (match-string 2 string))
	     (month (match-string 3 string))
	     (day (match-string 4 string))
	     (hour (match-string 5 string))
	     (minute (match-string 6 string))
	     (second (match-string 7 string))
	     (second-fraction (match-string 8 string))
	     (has-time-zone (match-string 9 string))
	     (time-zone-sign (match-string 10 string))
	     (time-zone-hour (match-string 11 string))
	     (time-zone-minute (match-string 12 string)))
	 (setq year-sign (if year-sign -1 1))
	 (setq year
	       (if year
		   (* year-sign
		      (string-to-number year))
		 2000))
	 (setq month
	       (if month (string-to-number month) 1))
	 (setq day
	       (if day (string-to-number day) 1))
	 (setq hour
	       (if hour (string-to-number hour) 0))
	 (setq minute
	       (if minute (string-to-number minute) 0))
	 (setq second
	       (if second (string-to-number second) 0))
	 (setq second-fraction
	       (if second-fraction
		   (float (string-to-number second-fraction))
		 0.0))
	 (setq has-time-zone (and has-time-zone t))
	 (setq time-zone-sign
	       (if (equal time-zone-sign "-") -1 1))
	 (setq time-zone-hour
	       (if time-zone-hour (string-to-number time-zone-hour) 0))
	 (setq time-zone-minute
	       (if time-zone-minute (string-to-number time-zone-minute) 0))
	 (and (>= month 1)
	      (<= month 12)
	      (>= day 1)
	      (<= day (rng-xsd-days-in-month year month))
	      (<= hour 23)
	      (<= minute 59)
	      (<= second 60)		; leap second
	      (<= time-zone-hour 23)
	      (<= time-zone-minute 59)
	      (cons has-time-zone
		    (rng-xsd-add-seconds
		     (list (rng-xsd-date-to-days year month day)
			   (rng-xsd-time-to-seconds hour minute second)
			   second-fraction)
		     (* (rng-xsd-time-to-seconds time-zone-hour
						 time-zone-minute
						 0)
			(- time-zone-sign))))))))

(defun rng-xsd-leap-year-p (year)
  (and (= (% year 4) 0)
       (or (/= (% year 100) 0)
	   (= (% year 400) 0))))

(defun rng-xsd-time-to-seconds (hour minute second)
  (+ (* (+ (* hour 60)
	   minute)
	60)
     second))

(defconst rng-xsd-max-tz (rng-xsd-time-to-seconds 14 0 0))

(defun rng-xsd-date-time< (dt1 dt2)
  (cond ((eq (car dt1) (car dt2))
	 (rng-xsd-number-list< (cdr dt1) (cdr dt2)))
	((car dt1)
	 (rng-xsd-number-list< (cdr dt1)
			       (rng-xsd-add-seconds (cdr dt2)
						    (- rng-xsd-max-tz))))
	(t
	 (rng-xsd-number-list< (rng-xsd-add-seconds (cdr dt1)
						    rng-xsd-max-tz)
			       (cdr dt2)))))

(defun rng-xsd-add-seconds (date offset)
  (let ((day (nth 0 date))
	(second (+ (nth 1 date) offset))
	(fraction (nth 2 date)))
    (cond ((< second 0)
	   (list (1- day)
		 (+ second rng-xsd-seconds-per-day)
		 fraction))
	  ((>= second rng-xsd-seconds-per-day)
	   (list (1+ day)
		 (- second rng-xsd-seconds-per-day)
		 fraction))
	  (t (list day second fraction)))))

(defun rng-xsd-number-list< (numbers1 numbers2)
  (while (and numbers1 (= (car numbers1) (car numbers2)))
    (setq numbers1 (cdr numbers1))
    (setq numbers2 (cdr numbers2)))
  (and numbers1
       (< (car numbers1) (car numbers2))))

(defun rng-xsd-date-to-days (year month day)
  "Return a unique day number where Jan 1 1 AD is day 1."
  (if (> year 0)			; AD
      (+ (rng-xsd-days-in-years (- year 1))
	 (rng-xsd-day-number-in-year year month day))
    (- (+ (- (rng-xsd-days-in-years (- 3 year))
	     (rng-xsd-days-in-years 3))
	  (- (if (rng-xsd-leap-year-p year) 366 365)
	     (rng-xsd-day-number-in-year year month day))))))

(defun rng-xsd-days-in-years (years)
  "The number of days in YEARS years where the first year is 1AD."
  (+ (* 365 years)
     (/ years 4)
     (- (/ years 100))
     (/ years 400)))

(defun rng-xsd-day-number-in-year (year month day)
  (+ (if (= month 1)
	 0
	 (aref rng-xsd-months-to-days (- month 2)))
     day
     (if (and (> month 2)
	      (rng-xsd-leap-year-p year))
	 1
       0)))

(defconst rng-xsd-duration-regexp
    "\\`[ \t\r\n]*\\(-\\)?P\
\\([0-9]+Y\\)?\\([0-9]+M\\)?\\([0-9]+D\\)?\
\\(?:T\\([0-9]+H\\)?\\([0-9]+M\\)?\
\\(\\([0-9]+\\(?:\\.[0-9]*\\)?\\|\\.[0-9]+\\)S\\)?\\)?\
[ \t\r\n]*\\'")


(defun rng-xsd-convert-duration (string)
  (and (string-match rng-xsd-duration-regexp string)
       (let ((last (substring string -1)))
	 (not (or (string= last "P")
		  (string= last "T"))))
       ;; years months days hours minutes seconds
       (let ((v (make-vector 6 0))
	     (sign (if (match-beginning 1) -1 1))
	     (i 0))
	 (while (< i 6)
	   (let ((start (match-beginning (+ i 2))))
	     (when start
	       (aset v i (* sign
			    (string-to-number
			     (substring string
					start
					(1- (match-end (+ i 2)))))))))
	   (setq i (1+ i)))
	 ;; Force seconds to be float so that equal works properly.
	 (aset v 5 (float (aref v 5)))
	 v)))

(defconst rng-xsd-min-seconds-per-month (* 28 rng-xsd-seconds-per-day))

(defun rng-xsd-duration< (d1 d2)
  (let* ((months1 (rng-xsd-duration-months d1))
	 (months2 (rng-xsd-duration-months d2))
	 (seconds1 (rng-xsd-duration-seconds d1))
	 (seconds2 (rng-xsd-duration-seconds d2)))
    (cond ((< months1 months2)
	   (if (< (- seconds1 seconds2) rng-xsd-min-seconds-per-month)
	       t
	     (rng-xsd-months-seconds< months1 seconds1 months2 seconds2)))
	  ((> months1 months2)
	   (if (< (- seconds2 seconds1) rng-xsd-min-seconds-per-month)
	       nil
	     (rng-xsd-months-seconds< months1 seconds1 months2 seconds2)))
	  (t (< seconds1 seconds2)))))

(defconst xsd-duration-reference-dates
  '((1696 . 9) (1697 . 2) (1903 . 3) (1903 . 7)))

(defun rng-xsd-months-seconds< (months1 seconds1 months2 seconds2)
  (let ((ret t)
	(ref-dates xsd-duration-reference-dates))
    (while (let* ((ref-date (car ref-dates))
		  (ref-year (car ref-date))
		  (ref-month (cdr ref-date)))
	     (unless (< (+ (rng-xsd-month-seconds months1
						  ref-year
						  ref-month)
			   seconds1)
			(+ (rng-xsd-month-seconds months2
						  ref-year
						  ref-month)
			   seconds2))
		 (setq ret nil))
	     (and ret
		  (setq ref-dates (cdr ref-dates)))))
    ret))


(defun rng-xsd-month-seconds (months ref-year ref-month)
  "Return the seconds in a number of months starting on a reference date.
Returns a floating point number."
  (* (rng-xsd-month-days (abs months) ref-year ref-month)
     (float rng-xsd-seconds-per-day)
     (if (< months 0) -1.0 1.0)))

(defconst rng-xsd-years-per-gregorian-cycle 400)
(defconst rng-xsd-months-per-gregorian-cycle
  (* rng-xsd-years-per-gregorian-cycle 12))
(defconst rng-xsd-leap-years-per-gregorian-cycle (- 100 (- 4 1)))
(defconst rng-xsd-days-per-gregorian-cycle
  (+ (* 365 rng-xsd-years-per-gregorian-cycle)
     rng-xsd-leap-years-per-gregorian-cycle))

(defun rng-xsd-month-days (months ref-year ref-month)
  "Return the days in a number of months starting on a reference date.
MONTHS must be an integer >= 0."
  (let ((days 0))
    (setq months (mod months rng-xsd-months-per-gregorian-cycle))
    ;; This may be rather slow, but it is highly unlikely
    ;; ever to be used in real life.
    (while (> months 0)
      (setq days
	    (+ (rng-xsd-days-in-month ref-year ref-month)
	       days))
      (setq ref-month
	    (if (eq ref-month 12)
		(progn
		  (setq ref-year (1+ ref-year))
		  1)
	      (1+ ref-month)))
      (setq months (1- months)))
    (+ (* (/ months rng-xsd-months-per-gregorian-cycle)
	  rng-xsd-days-per-gregorian-cycle)
       days)))

(defun rng-xsd-duration-months (d)
  (+ (* (aref d 0) 12)
     (aref d 1)))

(defun rng-xsd-duration-seconds (d)
  (+ (* (+ (* (+ (* (aref d 2)
		    24.0)
		 (aref d 3))
	      60.0)
	   (aref d 4))
	60.0)
     (aref d 5)))

(defun rng-xsd-convert-qname (string)
  (and (string-match "\\`[ \r\n\t]*\\([_[:alpha:]][-._[:alnum:]]*\\(:[_[:alpha:]][-._[:alnum:]]*\\)?\\)[ \r\n\t]*\\'" string)
       (let ((colon (match-beginning 2))
	     (context (apply (car rng-dt-namespace-context-getter)
			     (cdr rng-dt-namespace-context-getter))))
	 (if colon
	     (let* ((prefix (substring string
				       (match-beginning 1)
				       colon))
		    (binding (assoc prefix (cdr context))))
	       (and binding
		    (cons (cdr binding)
			  (substring string
				     (1+ colon)
				     (match-end 1)))))
	   (cons (car context)
		 (match-string 1 string))))))

(defun rng-xsd-convert-list (string convert &rest args)
  (let* ((tokens (split-string string "[ \t\n\r]+"))
	 (tem tokens))
    (while tem
      (let ((obj (apply convert
			(cons (car tem) args))))
	(cond (obj
	       (setcar tem obj)
	       (setq tem (cdr tem)))
	      (t
	       (setq tokens nil)
	       (setq tem nil)))))
    ;; Fortuitously this returns nil if the list is empty
    ;; which is what we want since the list types
    ;; have to have one or more members.
    tokens))

(defun rng-xsd-strcmp (s1 s2)
  (cond ((string= s1 s2) 0)
	((string< s1 s2) -1)
	(t 1)))

(put 'string 'rng-xsd-convert '(identity))
(put 'string 'rng-xsd-length 'length)
(put 'string 'rng-xsd-matches-anything t)

(put 'normalizedString 'rng-xsd-convert '(identity))
(put 'normalizedString 'rng-xsd-length 'length)
(put 'normalizedString 'rng-xsd-matches-anything t)

(put 'token 'rng-xsd-convert '(identity))
(put 'token 'rng-xsd-length 'length)
(put 'token 'rng-xsd-matches-anything t)

(put 'hexBinary 'rng-xsd-convert '(rng-xsd-convert-hex-binary))
(put 'hexBinary 'rng-xsd-length 'rng-xsd-hex-binary-length)

(put 'base64Binary 'rng-xsd-convert '(rng-xsd-convert-base64-binary))
(put 'base64Binary 'rng-xsd-length 'rng-xsd-base64-binary-length)

(put 'boolean 'rng-xsd-convert '(rng-xsd-convert-boolean))

(put 'float 'rng-xsd-convert '(rng-xsd-convert-float))
(put 'float 'rng-xsd-less-than 'rng-xsd-float<)

(put 'double 'rng-xsd-convert '(rng-xsd-convert-float))
(put 'double 'rng-xsd-less-than 'rng-xsd-float<)

(put 'decimal 'rng-xsd-convert '(rng-xsd-convert-decimal))
(put 'decimal 'rng-xsd-less-than 'rng-xsd-decimal<)

(put 'integer 'rng-xsd-convert '(rng-xsd-convert-integer))
(put 'integer 'rng-xsd-less-than 'rng-xsd-decimal<)

(defun rng-xsd-def-integer-type (name min max)
  (put name 'rng-xsd-less-than 'rng-xsd-decimal<)
  (put name
       'rng-xsd-convert
       (cdr (rng-xsd-compile 'integer
			     (append (and min `((minInclusive . ,min)))
				     (and max `((maxInclusive . ,max))))))))

(defun rng-xsd-def-token-type (name regexp)
  (put name 'rng-xsd-convert (list 'rng-xsd-convert-token
				   (concat "\\`[\r\n\t ]*\\("
					   regexp
					   "\\)[\r\n\t ]*\\'")))
  (put name 'rng-xsd-length 'length))

(rng-xsd-def-token-type 'NMTOKEN "[-.:_[:alnum:]]+")
(rng-xsd-def-token-type 'Name "[:_[:alpha:]][-.:_[:alnum:]]*")
(rng-xsd-def-token-type 'NCName "[_[:alpha:]][-._[:alnum:]]*")
(rng-xsd-def-token-type 'language
			"[a-zA-Z]\\{1,8\\}\\(?:-[a-zA-Z0-9]\\{1,8\\}\\)*")

(put 'ENTITY 'rng-xsd-convert (get 'NCName 'rng-xsd-convert))
(put 'ENTITY 'rng-xsd-length 'length)
(put 'ID 'rng-xsd-convert (get 'NCName 'rng-xsd-convert))
(put 'ID 'rng-xsd-length 'length)
(put 'IDREF 'rng-xsd-convert (get 'NCName 'rng-xsd-convert))
(put 'IDREF 'rng-xsd-length 'length)

(defun rng-xsd-def-list-type (name member-name)
  (put name 'rng-xsd-convert (cons 'rng-xsd-convert-list
				   (get member-name 'rng-xsd-convert)))
  (put name 'rng-xsd-length 'length))

(rng-xsd-def-list-type 'NMTOKENS 'NMTOKEN)
(rng-xsd-def-list-type 'IDREFS 'IDREF)
(rng-xsd-def-list-type 'ENTITIES 'ENTITY)

(put 'anyURI 'rng-xsd-convert '(rng-xsd-convert-any-uri))
(put 'anyURI 'rng-xsd-length 'length)

(put 'QName 'rng-xsd-convert '(rng-xsd-convert-qname))
(put 'NOTATION 'rng-xsd-convert '(rng-xsd-convert-qname))

(defconst rng-xsd-long-max "9223372036854775807")
(defconst rng-xsd-long-min "-9223372036854775808")
(defconst rng-xsd-int-max "2147483647")
(defconst rng-xsd-int-min "-2147483648")
(defconst rng-xsd-short-max "32767")
(defconst rng-xsd-short-min "-32768")
(defconst rng-xsd-byte-max "127")
(defconst rng-xsd-byte-min "-128")
(defconst rng-xsd-unsigned-long-max "18446744073709551615")
(defconst rng-xsd-unsigned-int-max "4294967295")
(defconst rng-xsd-unsigned-short-max "65535")
(defconst rng-xsd-unsigned-byte-max "255")

(rng-xsd-def-integer-type 'nonNegativeInteger "0" nil)
(rng-xsd-def-integer-type 'positiveInteger "1" nil)
(rng-xsd-def-integer-type 'nonPositiveInteger nil "0")
(rng-xsd-def-integer-type 'negativeInteger nil "-1")
(rng-xsd-def-integer-type 'long rng-xsd-long-min rng-xsd-long-max)
(rng-xsd-def-integer-type 'int rng-xsd-int-min rng-xsd-int-max)
(rng-xsd-def-integer-type 'short rng-xsd-short-min rng-xsd-short-max)
(rng-xsd-def-integer-type 'byte rng-xsd-byte-min rng-xsd-byte-max)
(rng-xsd-def-integer-type 'unsignedLong "0" rng-xsd-unsigned-long-max)
(rng-xsd-def-integer-type 'unsignedInt "0" rng-xsd-unsigned-int-max)
(rng-xsd-def-integer-type 'unsignedShort "0" rng-xsd-unsigned-short-max)
(rng-xsd-def-integer-type 'unsignedByte "0" rng-xsd-unsigned-byte-max)

(defun rng-xsd-def-date-time-type (name template)
  (put name 'rng-xsd-convert (list 'rng-xsd-convert-date-time
				   (rng-xsd-make-date-time-regexp template)))
  (put name 'rng-xsd-less-than 'rng-xsd-date-time<))

(rng-xsd-def-date-time-type 'dateTime "Y-M-DTt")
(rng-xsd-def-date-time-type 'time "t")
(rng-xsd-def-date-time-type 'date "Y-M-D")
(rng-xsd-def-date-time-type 'gYearMonth "Y-M")
(rng-xsd-def-date-time-type 'gYear "Y")
(rng-xsd-def-date-time-type 'gMonthDay "--M-D")
(rng-xsd-def-date-time-type 'gDay "---D")
(rng-xsd-def-date-time-type 'gMonth "--M")

(put 'duration 'rng-xsd-convert '(rng-xsd-convert-duration))
(put 'duration 'rng-xsd-less-than 'rng-xsd-duration<)

(provide 'rng-xsd)

;;; rng-xsd.el ends here