s/:union/union/
[xuriella.git] / xslt.lisp
blob619da43900a87352c7f29d44c3fc5d74f255db8a
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella)
32 #+sbcl
33 (declaim (optimize (debug 2)))
36 ;;;; XSLT-ERROR
38 (define-condition xslt-error (simple-error)
40 (:documentation "The class of all XSLT errors."))
42 (define-condition recoverable-xslt-error (xslt-error)
44 (:documentation "The class of recoverable XSLT errors."))
46 (defun xslt-error (fmt &rest args)
47 (error 'xslt-error :format-control fmt :format-arguments args))
49 (defun xslt-cerror (fmt &rest args)
50 (with-simple-restart (recover "recover")
51 (error 'recoverable-xslt-error
52 :format-control fmt
53 :format-arguments args)))
55 (defvar *debug* nil)
57 (defmacro handler-case* (form &rest clauses)
58 ;; like HANDLER-CASE if *DEBUG* is off. If it's on, don't establish
59 ;; a handler at all so that we see the real stack traces. (We could use
60 ;; HANDLER-BIND here and check at signalling time, but doesn't seem
61 ;; important.)
62 (let ((doit (gensym)))
63 `(flet ((,doit () ,form))
64 (if *debug*
65 (,doit)
66 (handler-case
67 (,doit)
68 ,@clauses)))))
70 (defun compile-xpath (xpath &optional env)
71 (handler-case*
72 (xpath:compile-xpath xpath env)
73 (xpath:xpath-error (c)
74 (xslt-error "~A" c))))
76 (defmacro with-stack-limit ((&optional) &body body)
77 `(invoke-with-stack-limit (lambda () ,@body)))
80 ;;;; Helper function and macro
82 (defun map-pipe-eagerly (fn pipe)
83 (xpath::enumerate pipe :key fn :result nil))
85 (defmacro do-pipe ((var pipe &optional result) &body body)
86 `(block nil
87 (map-pipe-eagerly #'(lambda (,var) ,@body) ,pipe)
88 ,result))
91 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
93 (defparameter *initial-namespaces*
94 '((nil . "")
95 ("xmlns" . #"http://www.w3.org/2000/xmlns/")
96 ("xml" . #"http://www.w3.org/XML/1998/namespace")))
98 (defparameter *namespaces* *initial-namespaces*)
100 (defvar *global-variable-declarations*)
101 (defvar *lexical-variable-declarations*)
103 (defvar *global-variable-values*)
104 (defvar *lexical-variable-values*)
106 (defclass xslt-environment () ())
108 (defun split-qname (str)
109 (handler-case
110 (multiple-value-bind (prefix local-name)
111 (cxml::split-qname str)
112 (unless
113 ;; FIXME: cxml should really offer a function that does
114 ;; checks for NCName and QName in a sensible way for user code.
115 ;; cxml::split-qname is tailored to the needs of the parser.
117 ;; For now, let's just check the syntax explicitly.
118 (and (or (null prefix) (xpath::nc-name-p prefix))
119 (xpath::nc-name-p local-name))
120 (xslt-error "not a qname: ~A" str))
121 (values prefix local-name))
122 (cxml:well-formedness-violation ()
123 (xslt-error "not a qname: ~A" str))))
125 (defun decode-qname (qname env attributep)
126 (multiple-value-bind (prefix local-name)
127 (split-qname qname)
128 (values local-name
129 (if (or prefix (not attributep))
130 (xpath:environment-find-namespace env prefix)
132 prefix)))
134 (defmethod xpath:environment-find-namespace ((env xslt-environment) prefix)
135 (cdr (assoc prefix *namespaces* :test 'equal)))
137 (defun find-variable-index (local-name uri table)
138 (position (cons local-name uri) table :test 'equal))
140 (defun intern-global-variable (local-name uri)
141 (or (find-variable-index local-name uri *global-variable-declarations*)
142 (push-variable local-name uri *global-variable-declarations*)))
144 (defun push-variable (local-name uri table)
145 (prog1
146 (length table)
147 (vector-push-extend (cons local-name uri) table)))
149 (defun lexical-variable-value (index &optional (errorp t))
150 (let ((result (svref *lexical-variable-values* index)))
151 (when errorp
152 (assert (not (eq result 'unbound))))
153 result))
155 (defun (setf lexical-variable-value) (newval index)
156 (assert (not (eq newval 'unbound)))
157 (setf (svref *lexical-variable-values* index) newval))
159 (defun global-variable-value (index &optional (errorp t))
160 (let ((result (svref *global-variable-values* index)))
161 (when errorp
162 (assert (not (eq result 'unbound))))
163 result))
165 (defun (setf global-variable-value) (newval index)
166 (assert (not (eq newval 'unbound)))
167 (setf (svref *global-variable-values* index) newval))
169 (defmethod xpath:environment-find-variable
170 ((env xslt-environment) lname uri)
171 (let ((index
172 (find-variable-index lname uri *lexical-variable-declarations*)))
173 (when index
174 (lambda (ctx)
175 (declare (ignore ctx))
176 (svref *lexical-variable-values* index)))))
178 (defclass lexical-xslt-environment (xslt-environment) ())
180 (defmethod xpath:environment-find-variable
181 ((env lexical-xslt-environment) lname uri)
182 (or (call-next-method)
183 (let ((index
184 (find-variable-index lname uri *global-variable-declarations*)))
185 (when index
186 (lambda (ctx)
187 (declare (ignore ctx))
188 (svref *global-variable-values* index))))))
190 (defclass global-variable-environment (xslt-environment)
191 ((initial-global-variable-thunks
192 :initarg :initial-global-variable-thunks
193 :accessor initial-global-variable-thunks)))
195 (defmethod xpath:environment-find-variable
196 ((env global-variable-environment) lname uri)
197 (or (call-next-method)
198 (gethash (cons lname uri) (initial-global-variable-thunks env))))
201 ;;;; TEXT-OUTPUT-SINK
202 ;;;;
203 ;;;; A sink that serializes only text and will error out on any other
204 ;;;; SAX event.
206 (defmacro with-text-output-sink ((var) &body body)
207 `(invoke-with-text-output-sink (lambda (,var) ,@body)))
209 (defclass text-output-sink (sax:default-handler)
210 ((target :initarg :target :accessor text-output-sink-target)))
212 (defmethod sax:characters ((sink text-output-sink) data)
213 (write-string data (text-output-sink-target sink)))
215 (defun invoke-with-text-output-sink (fn)
216 (with-output-to-string (s)
217 (funcall fn (make-instance 'text-output-sink :target s))))
220 ;;;; Names
222 (eval-when (:compile-toplevel :load-toplevel :execute)
223 (defvar *xsl* "http://www.w3.org/1999/XSL/Transform")
224 (defvar *xml* "http://www.w3.org/XML/1998/namespace")
225 (defvar *html* "http://www.w3.org/1999/xhtml"))
227 (defun of-name (local-name)
228 (stp:of-name local-name *xsl*))
230 (defun namep (node local-name)
231 (and (typep node '(or stp:element stp:attribute))
232 (equal (stp:namespace-uri node) *xsl*)
233 (equal (stp:local-name node) local-name)))
236 ;;;; PARSE-STYLESHEET
238 (defstruct stylesheet
239 (modes (make-hash-table :test 'equal))
240 (global-variables ())
241 (output-specification (make-output-specification))
242 (strip-tests nil)
243 (named-templates (make-hash-table :test 'equal))
244 (attribute-sets (make-hash-table :test 'equal)))
246 (defstruct mode (templates nil))
248 (defun find-mode (stylesheet local-name &optional uri)
249 (gethash (cons local-name uri) (stylesheet-modes stylesheet)))
251 (defun ensure-mode (stylesheet &optional local-name uri)
252 (or (find-mode stylesheet local-name uri)
253 (setf (gethash (cons local-name uri) (stylesheet-modes stylesheet))
254 (make-mode))))
256 (defun ensure-mode/qname (stylesheet qname env)
257 (if qname
258 (multiple-value-bind (local-name uri)
259 (decode-qname qname env nil)
260 (ensure-mode stylesheet local-name uri))
261 (find-mode stylesheet nil)))
263 (defun acons-namespaces (element &optional (bindings *namespaces*))
264 (map-namespace-declarations (lambda (prefix uri)
265 (push (cons prefix uri) bindings))
266 element)
267 bindings)
269 (defvar *excluded-namespaces* (list *xsl*))
270 (defvar *empty-mode*)
272 (defvar *xsl-include-stack* nil)
274 (defun parse-stylesheet-to-stp (input uri-resolver)
275 (let* ((d (cxml:parse input (make-text-normalizer (cxml-stp:make-builder))))
276 (<transform> (stp:document-element d)))
277 (strip-stylesheet <transform>)
278 ;; FIXME: handle embedded stylesheets
279 (unless (and (equal (stp:namespace-uri <transform>) *xsl*)
280 (or (equal (stp:local-name <transform>) "transform")
281 (equal (stp:local-name <transform>) "stylesheet")))
282 (xslt-error "not a stylesheet"))
283 (dolist (include (stp:filter-children (of-name "include") <transform>))
284 (let* ((uri (puri:merge-uris (stp:attribute-value include "href")
285 (stp:base-uri include)))
286 (uri (if uri-resolver
287 (funcall uri-resolver (puri:render-uri uri nil))
288 uri))
289 (str (puri:render-uri uri nil))
290 (pathname
291 (handler-case
292 (cxml::uri-to-pathname uri)
293 (cxml:xml-parse-error (c)
294 (xslt-error "cannot find included stylesheet ~A: ~A"
295 uri c)))))
296 (with-open-file
297 (stream pathname
298 :element-type '(unsigned-byte 8)
299 :if-does-not-exist nil)
300 (unless stream
301 (xslt-error "cannot find included stylesheet ~A at ~A"
302 uri pathname))
303 (when (find str *xsl-include-stack* :test #'equal)
304 (xslt-error "recursive inclusion of ~A" uri))
305 (let* ((*xsl-include-stack* (cons str *xsl-include-stack*))
306 (<transform>2 (parse-stylesheet-to-stp stream uri-resolver)))
307 (stp:do-children (child <transform>2)
308 (stp:insert-child-after <transform>
309 (stp:copy child)
310 include))
311 (stp:detach include)))))
312 <transform>))
314 (defvar *instruction-base-uri*)
315 (defvar *apply-imports-limit*)
316 (defvar *import-priority*)
318 (defun parse-1-stylesheet (env stylesheet designator uri-resolver)
319 (let* ((<transform> (parse-stylesheet-to-stp designator uri-resolver))
320 (*instruction-base-uri* (stp:base-uri <transform>))
321 (*namespaces* (acons-namespaces <transform>))
322 (*apply-imports-limit* (1+ *import-priority*)))
323 (dolist (import (stp:filter-children (of-name "import") <transform>))
324 (let ((uri (puri:merge-uris (stp:attribute-value import "href")
325 (stp:base-uri import))))
326 (parse-imported-stylesheet env stylesheet uri uri-resolver)))
327 (incf *import-priority*)
328 (parse-exclude-result-prefixes! <transform> env)
329 (parse-global-variables! stylesheet <transform>)
330 (parse-templates! stylesheet <transform> env)
331 (parse-output! stylesheet <transform>)
332 (parse-strip/preserve-space! stylesheet <transform> env)
333 (parse-attribute-sets! stylesheet <transform> env)))
335 (defvar *xsl-import-stack* nil)
337 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver)
338 (let* ((uri (if uri-resolver
339 (funcall uri-resolver (puri:render-uri uri nil))
340 uri))
341 (str (puri:render-uri uri nil))
342 (pathname
343 (handler-case
344 (cxml::uri-to-pathname uri)
345 (cxml:xml-parse-error (c)
346 (xslt-error "cannot find imported stylesheet ~A: ~A"
347 uri c)))))
348 (with-open-file
349 (stream pathname
350 :element-type '(unsigned-byte 8)
351 :if-does-not-exist nil)
352 (unless stream
353 (xslt-error "cannot find imported stylesheet ~A at ~A"
354 uri pathname))
355 (when (find str *xsl-import-stack* :test #'equal)
356 (xslt-error "recursive inclusion of ~A" uri))
357 (let ((*xsl-import-stack* (cons str *xsl-import-stack*)))
358 (parse-1-stylesheet env stylesheet stream uri-resolver)))))
360 (defun parse-stylesheet (designator &key uri-resolver)
361 (let* ((*import-priority* 0)
362 (puri:*strict-parse* nil)
363 (stylesheet (make-stylesheet))
364 (env (make-instance 'lexical-xslt-environment))
365 (*excluded-namespaces* *excluded-namespaces*)
366 (*global-variable-declarations* (make-empty-declaration-array)))
367 (ensure-mode stylesheet nil)
368 (parse-1-stylesheet env stylesheet designator uri-resolver)
369 ;; reverse attribute sets:
370 (let ((table (stylesheet-attribute-sets stylesheet)))
371 (maphash (lambda (k v)
372 (setf (gethash k table) (nreverse v)))
373 table))
374 stylesheet))
376 (defun parse-attribute-sets! (stylesheet <transform> env)
377 (dolist (elt (stp:filter-children (of-name "attribute-set") <transform>))
378 (push (let* ((sets
379 (mapcar (lambda (qname)
380 (multiple-value-list (decode-qname qname env nil)))
381 (words
382 (stp:attribute-value elt "use-attribute-sets"))))
383 (instructions
384 (stp:map-children 'list #'parse-instruction elt))
385 (*lexical-variable-declarations*
386 (make-empty-declaration-array))
387 (thunk
388 (compile-instruction `(progn ,@instructions) env))
389 (n-variables (length *lexical-variable-declarations*)))
390 (lambda (ctx)
391 (with-stack-limit ()
392 (loop for (local-name uri nil) in sets do
393 (dolist (thunk (find-attribute-set local-name uri))
394 (funcall thunk ctx)))
395 (let ((*lexical-variable-values*
396 (make-variable-value-array n-variables)))
397 (funcall thunk ctx)))))
398 (gethash (multiple-value-bind (local-name uri)
399 (decode-qname (stp:attribute-value elt "name") env nil)
400 (cons local-name uri))
401 (stylesheet-attribute-sets stylesheet)))))
403 (defun parse-exclude-result-prefixes! (<transform> env)
404 (stp:with-attributes (exclude-result-prefixes) <transform>
405 (dolist (prefix (words (or exclude-result-prefixes "")))
406 (when (equal prefix "#default")
407 (setf prefix nil))
408 (push (or (xpath:environment-find-namespace env prefix)
409 (xslt-error "namespace not found: ~A" prefix))
410 *excluded-namespaces*))))
412 (xpath:with-namespaces ((nil #.*xsl*))
413 (defun parse-strip/preserve-space! (stylesheet <transform> env)
414 (dolist (elt (stp:filter-children (lambda (x)
415 (or (namep x "strip-space")
416 (namep x "preserve-space")))
417 <transform>))
418 (let ((*namespaces* (acons-namespaces elt))
419 (mode
420 (if (equal (stp:local-name elt) "strip-space")
421 :strip
422 :preserve)))
423 (dolist (name-test (words (stp:attribute-value elt "elements")))
424 (let* ((pos (search ":*" name-test))
425 (test-function
426 (cond
427 ((eql pos (- (length name-test) 2))
428 (let* ((prefix (subseq name-test 0 pos))
429 (name-test-uri
430 (xpath:environment-find-namespace env prefix)))
431 (unless (xpath::nc-name-p prefix)
432 (xslt-error "not an NCName: ~A" prefix))
433 (lambda (local-name uri)
434 (declare (ignore local-name))
435 (if (equal uri name-test-uri)
436 mode
437 nil))))
438 ((equal name-test "*")
439 (lambda (local-name uri)
440 (declare (ignore local-name uri))
441 mode))
443 (multiple-value-bind (name-test-local-name name-test-uri)
444 (decode-qname name-test env nil)
445 (lambda (local-name uri)
446 (if (and (equal local-name name-test-local-name)
447 (equal uri name-test-uri))
448 mode
449 nil)))))))
450 (push test-function (stylesheet-strip-tests stylesheet))))))))
452 (defstruct (output-specification
453 (:conc-name "OUTPUT-"))
454 method
455 indent
456 omit-xml-declaration
457 encoding)
459 (defun parse-output! (stylesheet <transform>)
460 (let ((outputs (stp:filter-children (of-name "output") <transform>)))
461 (when outputs
462 (when (cdr outputs)
463 ;; FIXME:
464 ;; - concatenate cdata-section-elements
465 ;; - the others must not conflict
466 (error "oops, merging of output elements not supported yet"))
467 (let ((<output> (car outputs))
468 (spec (stylesheet-output-specification stylesheet)))
469 (stp:with-attributes (;; version
470 method
471 indent
472 encoding
473 ;;; media-type
474 ;;; doctype-system
475 ;;; doctype-public
476 omit-xml-declaration
477 ;;; standalone
478 ;;; cdata-section-elements
480 <output>
481 (setf (output-method spec) method)
482 (setf (output-indent spec) indent)
483 (setf (output-encoding spec) encoding)
484 (setf (output-omit-xml-declaration spec) omit-xml-declaration))))))
486 (defun make-empty-declaration-array ()
487 (make-array 1 :fill-pointer 0 :adjustable t))
489 (defun make-variable-value-array (n-lexical-variables)
490 (make-array n-lexical-variables :initial-element 'unbound))
492 (defun compile-global-variable (<variable> env) ;; also for <param>
493 (stp:with-attributes (name select) <variable>
494 (when (and select (stp:list-children <variable>))
495 (xslt-error "variable with select and body"))
496 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
497 (inner (cond
498 (select
499 (compile-xpath select env))
500 ((stp:list-children <variable>)
501 (let* ((inner-sexpr `(progn ,@(parse-body <variable>)))
502 (inner-thunk (compile-instruction inner-sexpr env)))
503 (lambda (ctx)
504 (apply-to-result-tree-fragment ctx inner-thunk))))
506 (lambda (ctx)
507 (declare (ignore ctx))
508 ""))))
509 (n-lexical-variables (length *lexical-variable-declarations*)))
510 (lambda (ctx)
511 (let ((*lexical-variable-values*
512 (make-variable-value-array n-lexical-variables)))
513 (funcall inner ctx))))))
515 (defstruct (variable-information
516 (:constructor make-variable)
517 (:conc-name "VARIABLE-"))
518 index
519 thunk
520 local-name
522 param-p
523 thunk-setter)
525 (defun parse-global-variable! (<variable> global-env) ;; also for <param>
526 (let ((*namespaces* (acons-namespaces <variable>))
527 (qname (stp:attribute-value <variable> "name")))
528 (unless qname
529 (xslt-error "name missing in ~A" (stp:local-name <variable>)))
530 (multiple-value-bind (local-name uri)
531 (decode-qname qname global-env nil)
532 ;; For the normal compilation environment of templates, install it
533 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
534 (let ((index (intern-global-variable local-name uri)))
535 ;; For the evaluation of a global variable itself, build a thunk
536 ;; that lazily resolves other variables, stored into
537 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
538 (let* ((value-thunk :unknown)
539 (global-variable-thunk
540 (lambda (ctx)
541 (let ((v (global-variable-value index nil)))
542 (when (eq v 'seen)
543 (xslt-error "recursive variable definition"))
544 (cond
545 ((eq v 'unbound)
546 ;; (print (list :computing index))
547 (setf (global-variable-value index) 'seen)
548 (setf (global-variable-value index)
549 (funcall value-thunk ctx))
550 #+nil (print (list :done-computing index
551 (global-variable-value index)))
552 #+nil (global-variable-value index))
554 #+nil(print (list :have
555 index v))
556 v)))))
557 (thunk-setter
558 (lambda ()
559 (setf value-thunk
560 (compile-global-variable <variable> global-env)))))
561 (setf (gethash (cons local-name uri)
562 (initial-global-variable-thunks global-env))
563 global-variable-thunk)
564 (make-variable :index index
565 :local-name local-name
566 :uri uri
567 :thunk global-variable-thunk
568 :param-p (namep <variable> "param")
569 :thunk-setter thunk-setter))))))
571 (xpath:with-namespaces ((nil #.*xsl*))
572 (defun parse-global-variables! (stylesheet <transform>)
573 (let* ((table (make-hash-table :test 'equal))
574 (global-env (make-instance 'global-variable-environment
575 :initial-global-variable-thunks table))
576 (specs '()))
577 (xpath:do-node-set
578 (<variable> (xpath:evaluate "variable|param" <transform>))
579 (let ((var (parse-global-variable! <variable> global-env)))
580 (when (find var
581 specs
582 :test (lambda (a b)
583 (and (equal (variable-local-name a)
584 (variable-local-name b))
585 (equal (variable-uri a)
586 (variable-uri b)))))
587 (xslt-error "duplicate definition for global variable ~A"
588 (variable-local-name var)))
589 (push var specs)))
590 ;; now that the global environment knows about all variables, run the
591 ;; thunk setters to perform their compilation
592 (mapc (lambda (spec) (funcall (variable-thunk-setter spec))) specs)
593 (setf (stylesheet-global-variables stylesheet) specs))))
595 (defun parse-templates! (stylesheet <transform> env)
596 (let ((i 0))
597 (dolist (<template> (stp:filter-children (of-name "template") <transform>))
598 (let ((*namespaces* (acons-namespaces <template>)))
599 (dolist (template (compile-template <template> env i))
600 (let ((name (template-name template)))
601 (if name
602 (let* ((table (stylesheet-named-templates stylesheet))
603 (head (car (gethash name table))))
604 (when (and head (eql (template-import-priority head)
605 (template-import-priority template)))
606 ;; fixme: is this supposed to be a run-time error?
607 (xslt-error "conflicting templates for ~A" name))
608 (push template (gethash name table)))
609 (let ((mode (ensure-mode/qname stylesheet
610 (template-mode-qname template)
611 env)))
612 (setf (template-mode template) mode)
613 (push template (mode-templates mode)))))))
614 (incf i))))
617 ;;;; APPLY-STYLESHEET
619 (defvar *stylesheet*)
620 (defvar *mode*)
622 (deftype xml-designator () '(or runes:xstream runes:rod array stream pathname))
624 (defstruct (parameter
625 (:constructor make-parameter (value local-name &optional uri)))
626 (uri "")
627 local-name
628 value)
630 (defun find-parameter-value (local-name uri parameters)
631 (dolist (p parameters)
632 (when (and (equal (parameter-local-name p) local-name)
633 (equal (parameter-uri p) uri))
634 (return (parameter-value p)))))
636 (defvar *uri-resolver*)
638 (defun parse-allowing-microsoft-bom (pathname handler)
639 (with-open-file (s pathname :element-type '(unsigned-byte 8))
640 (unless (and (eql (read-byte s nil) #xef)
641 (eql (read-byte s nil) #xbb)
642 (eql (read-byte s nil) #xbf))
643 (file-position s 0))
644 (cxml:parse s handler)))
646 (defun %document (uri-string base-uri)
647 (let* ((absolute-uri
648 (puri:merge-uris uri-string base-uri))
649 (resolved-uri
650 (if *uri-resolver*
651 (funcall *uri-resolver* (puri:render-uri absolute-uri nil))
652 absolute-uri))
653 (pathname
654 (handler-case
655 (cxml::uri-to-pathname resolved-uri)
656 (cxml:xml-parse-error (c)
657 (xslt-error "cannot find referenced document ~A: ~A"
658 resolved-uri c))))
659 (document
660 (handler-case
661 (parse-allowing-microsoft-bom pathname (stp:make-builder))
662 ((or file-error cxml:xml-parse-error) (c)
663 (xslt-error "cannot parse referenced document ~A: ~A"
664 pathname c))))
665 (xpath-root-node
666 (make-whitespace-stripper document
667 (stylesheet-strip-tests *stylesheet*))))
668 (when (puri:uri-fragment absolute-uri)
669 (xslt-error "use of fragment identifiers in document() not supported"))
670 xpath-root-node))
672 (xpath::define-xpath-function/lazy
673 :document
674 (object &optional node-set)
675 (let ((instruction-base-uri *instruction-base-uri*))
676 (lambda (ctx)
677 (declare (ignore ctx))
678 (let* ((object (funcall object))
679 (node-set (and node-set (funcall node-set)))
680 (uri
681 (when node-set
682 ;; FIXME: should use first node of the node set
683 ;; _in document order_
684 (xpath-protocol:base-uri (xpath:first-node node-set)))))
685 (xpath::make-node-set
686 (if (xpath:node-set-p object)
687 (xpath:map-node-set->list
688 (lambda (node)
689 (%document (xpath:string-value node)
690 (or uri (xpath-protocol:base-uri node))))
691 object)
692 (list (%document (xpath:string-value object)
693 (or uri instruction-base-uri)))))))))
695 (defun apply-stylesheet
696 (stylesheet source-document &key output parameters uri-resolver)
697 (when (typep stylesheet 'xml-designator)
698 (setf stylesheet (parse-stylesheet stylesheet)))
699 (when (typep source-document 'xml-designator)
700 (setf source-document (cxml:parse source-document (stp:make-builder))))
701 (invoke-with-output-sink
702 (lambda ()
703 (handler-case*
704 (let* ((puri:*strict-parse* nil)
705 (*stylesheet* stylesheet)
706 (*mode* (find-mode stylesheet nil))
707 (*empty-mode* (make-mode))
708 (global-variable-specs
709 (stylesheet-global-variables stylesheet))
710 (*global-variable-values*
711 (make-variable-value-array (length global-variable-specs)))
712 (*uri-resolver* uri-resolver)
713 (xpath-root-node
714 (make-whitespace-stripper
715 source-document
716 (stylesheet-strip-tests stylesheet)))
717 (ctx (xpath:make-context xpath-root-node)))
718 (mapc (lambda (spec)
719 (when (variable-param-p spec)
720 (let ((value
721 (find-parameter-value (variable-local-name spec)
722 (variable-uri spec)
723 parameters)))
724 (when value
725 (setf (global-variable-value (variable-index spec))
726 value)))))
727 global-variable-specs)
728 (mapc (lambda (spec)
729 (funcall (variable-thunk spec) ctx))
730 global-variable-specs)
731 #+nil (print global-variable-specs)
732 #+nil (print *global-variable-values*)
733 (apply-templates ctx))
734 (xpath:xpath-error (c)
735 (xslt-error "~A" c))))
736 stylesheet
737 output))
739 (defun find-attribute-set (local-name uri)
740 (or (gethash (cons local-name uri) (stylesheet-attribute-sets *stylesheet*))
741 (xslt-error "no such attribute set: ~A/~A" local-name uri)))
743 (defun apply-templates/list (list &optional param-bindings sort-predicate)
744 (when sort-predicate
745 (setf list (sort list sort-predicate)))
746 (let* ((n (length list))
747 (s/d (lambda () n)))
748 (loop
749 for i from 1
750 for child in list
752 (apply-templates (xpath:make-context child s/d i)
753 param-bindings))))
755 (defvar *stack-limit* 200)
757 (defun invoke-with-stack-limit (fn)
758 (let ((*stack-limit* (1- *stack-limit*)))
759 (unless (plusp *stack-limit*)
760 (xslt-error "*stack-limit* reached; stack overflow"))
761 (funcall fn)))
763 (defun invoke-template (ctx template param-bindings)
764 (let ((*lexical-variable-values*
765 (make-variable-value-array (template-n-variables template))))
766 (with-stack-limit ()
767 (loop
768 for (name-cons value) in param-bindings
769 for (nil index nil) = (find name-cons
770 (template-params template)
771 :test #'equal
772 :key #'car)
774 (unless index
775 (xslt-error "invalid template parameter ~A" name-cons))
776 (setf (lexical-variable-value index) value))
777 (funcall (template-body template) ctx))))
779 (defun apply-default-templates (ctx)
780 (let ((node (xpath:context-node ctx)))
781 (cond
782 ((or (xpath-protocol:node-type-p node :processing-instruction)
783 (xpath-protocol:node-type-p node :comment)))
784 ((or (xpath-protocol:node-type-p node :text)
785 (xpath-protocol:node-type-p node :attribute))
786 (write-text (xpath-protocol:string-value node)))
788 (apply-templates/list
789 (xpath::force
790 (xpath-protocol:child-pipe node)))))))
792 (defvar *apply-imports*)
794 (defun apply-applicable-templates (ctx templates param-bindings finally)
795 (labels ((apply-imports ()
796 (if templates
797 (let* ((this (pop templates))
798 (low (template-apply-imports-limit this))
799 (high (template-import-priority this)))
800 (setf templates
801 (remove-if-not
802 (lambda (x)
803 (<= low (template-import-priority x) high))
804 templates))
805 (invoke-template ctx this param-bindings))
806 (funcall finally))))
807 (let ((*apply-imports* #'apply-imports))
808 (apply-imports))))
810 (defun apply-templates (ctx &optional param-bindings)
811 (apply-applicable-templates ctx
812 (find-templates ctx)
813 param-bindings
814 (lambda ()
815 (apply-default-templates ctx))))
817 (defun call-template (ctx name &optional param-bindings)
818 (apply-applicable-templates ctx
819 (find-named-templates name)
820 param-bindings
821 (lambda ()
822 (error "cannot find named template: ~s"
823 name))))
825 (defun find-templates (ctx)
826 (let* ((matching-candidates
827 (remove-if-not (lambda (template)
828 (template-matches-p template ctx))
829 (mode-templates *mode*)))
830 (npriorities
831 (if matching-candidates
832 (1+ (reduce #'max
833 matching-candidates
834 :key #'template-import-priority))
836 (priority-groups (make-array npriorities :initial-element nil)))
837 (dolist (template matching-candidates)
838 (push template
839 (elt priority-groups (template-import-priority template))))
840 ;;; (print (map 'list #'length priority-groups))
841 ;;; (force-output)
842 (loop
843 for i from (1- npriorities) downto 0
844 for group = (elt priority-groups i)
845 for template = (maximize #'template< group)
846 when template
847 collect template)))
849 (defun find-named-templates (name)
850 (gethash name (stylesheet-named-templates *stylesheet*)))
852 (defun template< (a b) ;assuming same import priority
853 (let ((p (template-priority a))
854 (q (template-priority b)))
855 (cond
856 ((< p q) t)
857 ((> p q) nil)
859 (xslt-cerror "conflicting templates:~_~A,~_~A"
860 (template-match-expression a)
861 (template-match-expression b))
862 (< (template-position a) (template-position b))))))
864 (defun maximize (< things)
865 (when things
866 (let ((max (car things)))
867 (dolist (other (cdr things))
868 (when (funcall < max other)
869 (setf max other)))
870 max)))
872 (defun template-matches-p (template ctx)
873 (find (xpath:context-node ctx)
874 (xpath:all-nodes (funcall (template-match-thunk template) ctx))))
876 (defun invoke-with-output-sink (fn stylesheet output)
877 (etypecase output
878 (pathname
879 (with-open-file (s output
880 :direction :output
881 :element-type '(unsigned-byte 8)
882 :if-exists :rename-and-delete)
883 (invoke-with-output-sink fn stylesheet s)))
884 ((or stream null)
885 (invoke-with-output-sink fn
886 stylesheet
887 (make-output-sink stylesheet output)))
888 ((or hax:abstract-handler sax:abstract-handler)
889 (with-xml-output output
890 (funcall fn)))))
892 (defun make-output-sink (stylesheet stream)
893 (let* ((ystream
894 (if stream
895 (let ((et (stream-element-type stream)))
896 (cond
897 ((or (null et) (subtypep et '(unsigned-byte 8)))
898 (runes:make-octet-stream-ystream stream))
899 ((subtypep et 'character)
900 (runes:make-character-stream-ystream stream))))
901 (runes:make-rod-ystream)))
902 (output-spec (stylesheet-output-specification stylesheet))
903 (omit-xml-declaration-p
904 (equal (output-omit-xml-declaration output-spec) "yes"))
905 (sax-target
906 (make-instance 'cxml::sink
907 :ystream ystream
908 :omit-xml-declaration-p omit-xml-declaration-p)))
909 (if (equalp (output-method (stylesheet-output-specification stylesheet))
910 "HTML")
911 (make-instance 'combi-sink
912 :hax-target (make-instance 'chtml::sink
913 :ystream ystream)
914 :sax-target sax-target
915 :encoding (output-encoding output-spec))
916 sax-target)))
918 (defstruct template
919 match-expression
920 match-thunk
921 name
922 import-priority
923 apply-imports-limit
924 priority
925 position
926 mode
927 mode-qname
928 params
929 body
930 n-variables)
932 (defun expression-priority (form)
933 (let ((step (second form)))
934 (if (and (null (cddr form))
935 (listp step)
936 (eq :child (car step))
937 (null (cddr step)))
938 (let ((name (second step)))
939 (cond
940 ((or (stringp name)
941 (and (consp name)
942 (or (eq (car name) :qname)
943 (eq (car name) :processing-instruction))))
944 0.0)
945 ((and (consp name)
946 (or (eq (car name) :namespace)
947 (eq (car name) '*)))
948 -0.25)
950 -0.5)))
951 0.5)))
953 (defun valid-expression-p (expr)
954 (cond
955 ((atom expr) t)
956 ((eq (first expr) :path)
957 (every (lambda (x)
958 (let ((filter (third x)))
959 (or (null filter) (valid-expression-p filter))))
960 (cdr expr)))
961 ((eq (first expr) :variable) ;(!)
962 nil)
964 (every #'valid-expression-p (cdr expr)))))
966 (defun parse-pattern (str)
967 ;; zzz check here for anything not allowed as an XSLT pattern
968 ;; zzz can we hack id() and key() here?
969 (let ((form (xpath:parse-xpath str)))
970 (unless (consp form)
971 (xslt-error "not a valid pattern: ~A" str))
972 (mapcar (lambda (case)
973 (unless (eq (car case) :path) ;zzz: filter statt path
974 (xslt-error "not a valid pattern: ~A" str))
975 (unless (valid-expression-p case)
976 (xslt-error "invalid filter"))
977 case)
978 (if (eq (car form) 'union)
979 (cdr form)
980 (list form)))))
982 (defun compile-value-thunk (value env)
983 (if (and (listp value) (eq (car value) 'progn))
984 (let ((inner-thunk (compile-instruction value env)))
985 (lambda (ctx)
986 (apply-to-result-tree-fragment ctx inner-thunk)))
987 (compile-xpath value env)))
989 (defun compile-var-bindings/nointern (forms env)
990 (loop
991 for (name value) in forms
992 collect (multiple-value-bind (local-name uri)
993 (decode-qname name env nil)
994 (list (cons local-name uri)
995 (compile-value-thunk value env)))))
997 (defun compile-var-bindings (forms env)
998 (loop
999 for (cons thunk) in (compile-var-bindings/nointern forms env)
1000 for (local-name . uri) = cons
1001 collect (list cons
1002 (push-variable local-name
1004 *lexical-variable-declarations*)
1005 thunk)))
1007 (defun compile-template (<template> env position)
1008 (stp:with-attributes (match name priority mode) <template>
1009 (unless (or name match)
1010 (xslt-error "missing match in template"))
1011 (multiple-value-bind (params body-pos)
1012 (loop
1013 for i from 0
1014 for child in (stp:list-children <template>)
1015 while (namep child "param")
1016 collect (parse-param child) into params
1017 finally (return (values params i)))
1018 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1019 (param-bindings (compile-var-bindings params env))
1020 (body (parse-body <template> body-pos (mapcar #'car params)))
1021 (body-thunk (compile-instruction `(progn ,@body) env))
1022 (outer-body-thunk
1023 #'(lambda (ctx)
1024 ;; set params that weren't initialized by apply-templates
1025 (loop for (name index param-thunk) in param-bindings
1026 when (eq (lexical-variable-value index nil) 'unbound)
1027 do (setf (lexical-variable-value index)
1028 (funcall param-thunk ctx)))
1029 (funcall body-thunk ctx)))
1030 (n-variables (length *lexical-variable-declarations*)))
1031 (append
1032 (when name
1033 (multiple-value-bind (local-name uri)
1034 (decode-qname name env nil)
1035 (list
1036 (make-template :name (cons local-name uri)
1037 :import-priority *import-priority*
1038 :apply-imports-limit *apply-imports-limit*
1039 :params param-bindings
1040 :body outer-body-thunk
1041 :n-variables n-variables))))
1042 (when match
1043 (mapcar (lambda (expression)
1044 (let ((match-thunk
1045 (compile-xpath
1046 `(xpath:xpath
1047 (:path (:ancestor-or-self :node)
1048 ,@(cdr expression)))
1049 env))
1050 (p (if priority
1051 (parse-number:parse-number priority)
1052 (expression-priority expression))))
1053 (make-template :match-expression expression
1054 :match-thunk match-thunk
1055 :import-priority *import-priority*
1056 :apply-imports-limit *apply-imports-limit*
1057 :priority p
1058 :position position
1059 :mode-qname mode
1060 :params param-bindings
1061 :body outer-body-thunk
1062 :n-variables n-variables)))
1063 (parse-pattern match))))))))
1064 #+(or)
1065 (xuriella::parse-stylesheet #p"/home/david/src/lisp/xuriella/test.xsl")