Fixed code after Plexippus XPath API changes.
[xuriella.git] / xslt.lisp
blob83f9159e2547ccaa04b6adbd55b0eba50b48b7bc
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-sys:environment-find-namespace env prefix)
132 prefix)))
134 (defmethod xpath-sys: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-sys:environment-find-function
170 ((env xslt-environment) lname uri)
171 (if (string= uri "")
172 (or (xpath-sys:find-xpath-function lname *xsl*)
173 (xpath-sys:find-xpath-function lname uri))
174 (xpath-sys:find-xpath-function lname uri)))
176 (defmethod xpath-sys:environment-find-variable
177 ((env xslt-environment) lname uri)
178 (let ((index
179 (find-variable-index lname uri *lexical-variable-declarations*)))
180 (when index
181 (lambda (ctx)
182 (declare (ignore ctx))
183 (svref *lexical-variable-values* index)))))
185 (defclass lexical-xslt-environment (xslt-environment) ())
187 (defmethod xpath-sys:environment-find-variable
188 ((env lexical-xslt-environment) lname uri)
189 (or (call-next-method)
190 (let ((index
191 (find-variable-index lname uri *global-variable-declarations*)))
192 (when index
193 (xslt-trace-thunk
194 (lambda (ctx)
195 (declare (ignore ctx))
196 (svref *global-variable-values* index))
197 "global ~s (uri ~s) = ~s" lname uri :result)))))
199 (defclass global-variable-environment (xslt-environment)
200 ((initial-global-variable-thunks
201 :initarg :initial-global-variable-thunks
202 :accessor initial-global-variable-thunks)))
204 (defmethod xpath-sys:environment-find-variable
205 ((env global-variable-environment) lname uri)
206 (or (call-next-method)
207 (gethash (cons lname uri) (initial-global-variable-thunks env))))
210 ;;;; TEXT-OUTPUT-SINK
211 ;;;;
212 ;;;; A sink that serializes only text and will error out on any other
213 ;;;; SAX event.
215 (defmacro with-text-output-sink ((var) &body body)
216 `(invoke-with-text-output-sink (lambda (,var) ,@body)))
218 (defclass text-output-sink (sax:default-handler)
219 ((target :initarg :target :accessor text-output-sink-target)))
221 (defmethod sax:characters ((sink text-output-sink) data)
222 (write-string data (text-output-sink-target sink)))
224 (defun invoke-with-text-output-sink (fn)
225 (with-output-to-string (s)
226 (funcall fn (make-instance 'text-output-sink :target s))))
229 ;;;; Names
231 (eval-when (:compile-toplevel :load-toplevel :execute)
232 (defvar *xsl* "http://www.w3.org/1999/XSL/Transform")
233 (defvar *xml* "http://www.w3.org/XML/1998/namespace")
234 (defvar *html* "http://www.w3.org/1999/xhtml"))
236 (defun of-name (local-name)
237 (stp:of-name local-name *xsl*))
239 (defun namep (node local-name)
240 (and (typep node '(or stp:element stp:attribute))
241 (equal (stp:namespace-uri node) *xsl*)
242 (equal (stp:local-name node) local-name)))
245 ;;;; PARSE-STYLESHEET
247 (defstruct stylesheet
248 (modes (make-hash-table :test 'equal))
249 (global-variables ())
250 (output-specification (make-output-specification))
251 (strip-tests nil)
252 (named-templates (make-hash-table :test 'equal))
253 (attribute-sets (make-hash-table :test 'equal))
254 (keys (make-hash-table :test 'equal)))
256 (defstruct mode (templates nil))
258 (defun find-mode (stylesheet local-name &optional uri)
259 (gethash (cons local-name uri) (stylesheet-modes stylesheet)))
261 (defun ensure-mode (stylesheet &optional local-name uri)
262 (or (find-mode stylesheet local-name uri)
263 (setf (gethash (cons local-name uri) (stylesheet-modes stylesheet))
264 (make-mode))))
266 (defun ensure-mode/qname (stylesheet qname env)
267 (if qname
268 (multiple-value-bind (local-name uri)
269 (decode-qname qname env nil)
270 (ensure-mode stylesheet local-name uri))
271 (find-mode stylesheet nil)))
273 (defun acons-namespaces (element &optional (bindings *namespaces*))
274 (map-namespace-declarations (lambda (prefix uri)
275 (push (cons prefix uri) bindings))
276 element)
277 bindings)
279 (defun find-key (name stylesheet)
280 (or (gethash name (stylesheet-keys stylesheet))
281 (xslt-error "unknown key: ~a" name)))
283 (defun make-key (match use) (cons match use))
285 (defun key-match (key) (car key))
287 (defun key-use (key) (cdr key))
289 (defun add-key (stylesheet name match use)
290 (if (gethash name (stylesheet-keys stylesheet))
291 (xslt-error "duplicate key: ~a" name)
292 (setf (gethash name (stylesheet-keys stylesheet))
293 (make-key match use))))
295 (defvar *excluded-namespaces* (list *xsl*))
296 (defvar *empty-mode*)
298 (defvar *xsl-include-stack* nil)
300 (defun uri-to-pathname (uri)
301 (cxml::uri-to-pathname (puri:parse-uri uri)))
303 (defun parse-stylesheet-to-stp (input uri-resolver)
304 (let* ((d (cxml:parse input (make-text-normalizer (cxml-stp:make-builder))))
305 (<transform> (stp:document-element d)))
306 (strip-stylesheet <transform>)
307 ;; FIXME: handle embedded stylesheets
308 (unless (and (equal (stp:namespace-uri <transform>) *xsl*)
309 (or (equal (stp:local-name <transform>) "transform")
310 (equal (stp:local-name <transform>) "stylesheet")))
311 (xslt-error "not a stylesheet"))
312 (dolist (include (stp:filter-children (of-name "include") <transform>))
313 (let* ((uri (puri:merge-uris (stp:attribute-value include "href")
314 (stp:base-uri include)))
315 (uri (if uri-resolver
316 (funcall uri-resolver (puri:render-uri uri nil))
317 uri))
318 (str (puri:render-uri uri nil))
319 (pathname
320 (handler-case
321 (uri-to-pathname uri)
322 (cxml:xml-parse-error (c)
323 (xslt-error "cannot find included stylesheet ~A: ~A"
324 uri c)))))
325 (with-open-file
326 (stream pathname
327 :element-type '(unsigned-byte 8)
328 :if-does-not-exist nil)
329 (unless stream
330 (xslt-error "cannot find included stylesheet ~A at ~A"
331 uri pathname))
332 (when (find str *xsl-include-stack* :test #'equal)
333 (xslt-error "recursive inclusion of ~A" uri))
334 (let* ((*xsl-include-stack* (cons str *xsl-include-stack*))
335 (<transform>2 (parse-stylesheet-to-stp stream uri-resolver)))
336 (stp:do-children (child <transform>2)
337 (stp:insert-child-after <transform>
338 (stp:copy child)
339 include))
340 (stp:detach include)))))
341 <transform>))
343 (defvar *instruction-base-uri*)
344 (defvar *apply-imports-limit*)
345 (defvar *import-priority*)
347 (defun parse-1-stylesheet (env stylesheet designator uri-resolver)
348 (let* ((<transform> (parse-stylesheet-to-stp designator uri-resolver))
349 (*instruction-base-uri* (stp:base-uri <transform>))
350 (*namespaces* (acons-namespaces <transform>))
351 (*apply-imports-limit* (1+ *import-priority*)))
352 (dolist (import (stp:filter-children (of-name "import") <transform>))
353 (let ((uri (puri:merge-uris (stp:attribute-value import "href")
354 (stp:base-uri import))))
355 (parse-imported-stylesheet env stylesheet uri uri-resolver)))
356 (incf *import-priority*)
357 (parse-exclude-result-prefixes! <transform> env)
358 (parse-global-variables! stylesheet <transform>)
359 (parse-keys! stylesheet <transform> env)
360 (parse-templates! stylesheet <transform> env)
361 (parse-output! stylesheet <transform>)
362 (parse-strip/preserve-space! stylesheet <transform> env)
363 (parse-attribute-sets! stylesheet <transform> env)))
365 (defvar *xsl-import-stack* nil)
367 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver)
368 (let* ((uri (if uri-resolver
369 (funcall uri-resolver (puri:render-uri uri nil))
370 uri))
371 (str (puri:render-uri uri nil))
372 (pathname
373 (handler-case
374 (uri-to-pathname uri)
375 (cxml:xml-parse-error (c)
376 (xslt-error "cannot find imported stylesheet ~A: ~A"
377 uri c)))))
378 (with-open-file
379 (stream pathname
380 :element-type '(unsigned-byte 8)
381 :if-does-not-exist nil)
382 (unless stream
383 (xslt-error "cannot find imported stylesheet ~A at ~A"
384 uri pathname))
385 (when (find str *xsl-import-stack* :test #'equal)
386 (xslt-error "recursive inclusion of ~A" uri))
387 (let ((*xsl-import-stack* (cons str *xsl-import-stack*)))
388 (parse-1-stylesheet env stylesheet stream uri-resolver)))))
390 (defun parse-stylesheet (designator &key uri-resolver)
391 (let* ((*import-priority* 0)
392 (puri:*strict-parse* nil)
393 (stylesheet (make-stylesheet))
394 (env (make-instance 'lexical-xslt-environment))
395 (*excluded-namespaces* *excluded-namespaces*)
396 (*global-variable-declarations* (make-empty-declaration-array)))
397 (ensure-mode stylesheet nil)
398 (parse-1-stylesheet env stylesheet designator uri-resolver)
399 ;; reverse attribute sets:
400 (let ((table (stylesheet-attribute-sets stylesheet)))
401 (maphash (lambda (k v)
402 (setf (gethash k table) (nreverse v)))
403 table))
404 stylesheet))
406 (defun parse-attribute-sets! (stylesheet <transform> env)
407 (dolist (elt (stp:filter-children (of-name "attribute-set") <transform>))
408 (push (let* ((sets
409 (mapcar (lambda (qname)
410 (multiple-value-list (decode-qname qname env nil)))
411 (words
412 (stp:attribute-value elt "use-attribute-sets"))))
413 (instructions
414 (stp:map-children 'list #'parse-instruction elt))
415 (*lexical-variable-declarations*
416 (make-empty-declaration-array))
417 (thunk
418 (compile-instruction `(progn ,@instructions) env))
419 (n-variables (length *lexical-variable-declarations*)))
420 (lambda (ctx)
421 (with-stack-limit ()
422 (loop for (local-name uri nil) in sets do
423 (dolist (thunk (find-attribute-set local-name uri))
424 (funcall thunk ctx)))
425 (let ((*lexical-variable-values*
426 (make-variable-value-array n-variables)))
427 (funcall thunk ctx)))))
428 (gethash (multiple-value-bind (local-name uri)
429 (decode-qname (stp:attribute-value elt "name") env nil)
430 (cons local-name uri))
431 (stylesheet-attribute-sets stylesheet)))))
433 (defun parse-exclude-result-prefixes! (<transform> env)
434 (stp:with-attributes (exclude-result-prefixes) <transform>
435 (dolist (prefix (words (or exclude-result-prefixes "")))
436 (when (equal prefix "#default")
437 (setf prefix nil))
438 (push (or (xpath-sys:environment-find-namespace env prefix)
439 (xslt-error "namespace not found: ~A" prefix))
440 *excluded-namespaces*))))
442 (defun parse-strip/preserve-space! (stylesheet <transform> env)
443 (xpath:with-namespaces ((nil #.*xsl*))
444 (dolist (elt (stp:filter-children (lambda (x)
445 (or (namep x "strip-space")
446 (namep x "preserve-space")))
447 <transform>))
448 (let ((*namespaces* (acons-namespaces elt))
449 (mode
450 (if (equal (stp:local-name elt) "strip-space")
451 :strip
452 :preserve)))
453 (dolist (name-test (words (stp:attribute-value elt "elements")))
454 (let* ((pos (search ":*" name-test))
455 (test-function
456 (cond
457 ((eql pos (- (length name-test) 2))
458 (let* ((prefix (subseq name-test 0 pos))
459 (name-test-uri
460 (xpath-sys:environment-find-namespace env prefix)))
461 (unless (xpath::nc-name-p prefix)
462 (xslt-error "not an NCName: ~A" prefix))
463 (lambda (local-name uri)
464 (declare (ignore local-name))
465 (if (equal uri name-test-uri)
466 mode
467 nil))))
468 ((equal name-test "*")
469 (lambda (local-name uri)
470 (declare (ignore local-name uri))
471 mode))
473 (multiple-value-bind (name-test-local-name name-test-uri)
474 (decode-qname name-test env nil)
475 (lambda (local-name uri)
476 (if (and (equal local-name name-test-local-name)
477 (equal uri name-test-uri))
478 mode
479 nil)))))))
480 (push test-function (stylesheet-strip-tests stylesheet))))))))
482 (defstruct (output-specification
483 (:conc-name "OUTPUT-"))
484 method
485 indent
486 omit-xml-declaration
487 encoding)
489 (defun parse-output! (stylesheet <transform>)
490 (let ((outputs (stp:filter-children (of-name "output") <transform>)))
491 (when outputs
492 (when (cdr outputs)
493 ;; FIXME:
494 ;; - concatenate cdata-section-elements
495 ;; - the others must not conflict
496 (error "oops, merging of output elements not supported yet"))
497 (let ((<output> (car outputs))
498 (spec (stylesheet-output-specification stylesheet)))
499 (stp:with-attributes (;; version
500 method
501 indent
502 encoding
503 ;;; media-type
504 ;;; doctype-system
505 ;;; doctype-public
506 omit-xml-declaration
507 ;;; standalone
508 ;;; cdata-section-elements
510 <output>
511 (setf (output-method spec) method)
512 (setf (output-indent spec) indent)
513 (setf (output-encoding spec) encoding)
514 (setf (output-omit-xml-declaration spec) omit-xml-declaration))))))
516 (defun make-empty-declaration-array ()
517 (make-array 1 :fill-pointer 0 :adjustable t))
519 (defun make-variable-value-array (n-lexical-variables)
520 (make-array n-lexical-variables :initial-element 'unbound))
522 (defun compile-global-variable (<variable> env) ;; also for <param>
523 (stp:with-attributes (name select) <variable>
524 (when (and select (stp:list-children <variable>))
525 (xslt-error "variable with select and body"))
526 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
527 (inner (cond
528 (select
529 (compile-xpath select env))
530 ((stp:list-children <variable>)
531 (let* ((inner-sexpr `(progn ,@(parse-body <variable>)))
532 (inner-thunk (compile-instruction inner-sexpr env)))
533 (lambda (ctx)
534 (apply-to-result-tree-fragment ctx inner-thunk))))
536 (lambda (ctx)
537 (declare (ignore ctx))
538 ""))))
539 (n-lexical-variables (length *lexical-variable-declarations*)))
540 (xslt-trace-thunk
541 (lambda (ctx)
542 (let* ((*lexical-variable-values*
543 (make-variable-value-array n-lexical-variables)))
544 (funcall inner ctx)))
545 "global ~s (~s) = ~s" name select :result))))
547 (defstruct (variable-information
548 (:constructor make-variable)
549 (:conc-name "VARIABLE-"))
550 index
551 thunk
552 local-name
554 param-p
555 thunk-setter)
557 (defun parse-global-variable! (<variable> global-env) ;; also for <param>
558 (let ((*namespaces* (acons-namespaces <variable>))
559 (qname (stp:attribute-value <variable> "name")))
560 (unless qname
561 (xslt-error "name missing in ~A" (stp:local-name <variable>)))
562 (multiple-value-bind (local-name uri)
563 (decode-qname qname global-env nil)
564 ;; For the normal compilation environment of templates, install it
565 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
566 (let ((index (intern-global-variable local-name uri)))
567 ;; For the evaluation of a global variable itself, build a thunk
568 ;; that lazily resolves other variables, stored into
569 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
570 (let* ((value-thunk :unknown)
571 (global-variable-thunk
572 (lambda (ctx)
573 (let ((v (global-variable-value index nil)))
574 (when (eq v 'seen)
575 (xslt-error "recursive variable definition"))
576 (cond
577 ((eq v 'unbound)
578 ;; (print (list :computing index))
579 (setf (global-variable-value index) 'seen)
580 (setf (global-variable-value index)
581 (funcall value-thunk ctx))
582 #+nil (print (list :done-computing index
583 (global-variable-value index)))
584 #+nil (global-variable-value index))
586 #+nil(print (list :have
587 index v))
588 v)))))
589 (thunk-setter
590 (lambda ()
591 (setf value-thunk
592 (compile-global-variable <variable> global-env)))))
593 (setf (gethash (cons local-name uri)
594 (initial-global-variable-thunks global-env))
595 global-variable-thunk)
596 (make-variable :index index
597 :local-name local-name
598 :uri uri
599 :thunk global-variable-thunk
600 :param-p (namep <variable> "param")
601 :thunk-setter thunk-setter))))))
603 (defun parse-keys! (stylesheet <transform> env)
604 (xpath:with-namespaces ((nil #.*xsl*))
605 (xpath:do-node-set
606 (<key> (xpath:evaluate "key" <transform>))
607 (stp:with-attributes (name match use) <key>
608 (unless name (xslt-error "key name attribute not specified"))
609 (unless match (xslt-error "key match attribute not specified"))
610 (unless use (xslt-error "key use attribute not specified"))
611 (add-key stylesheet name
612 (compile-xpath `(xpath:xpath ,(parse-key-pattern match)) env)
613 (compile-xpath use env))))))
615 (defun parse-global-variables! (stylesheet <transform>)
616 (xpath:with-namespaces ((nil #.*xsl*))
617 (let* ((table (make-hash-table :test 'equal))
618 (global-env (make-instance 'global-variable-environment
619 :initial-global-variable-thunks table))
620 (specs '()))
621 (xpath:do-node-set
622 (<variable> (xpath:evaluate "variable|param" <transform>))
623 (let ((var (parse-global-variable! <variable> global-env)))
624 (xslt-trace "parsing global variable ~s (uri ~s)"
625 (variable-local-name var)
626 (variable-uri var))
627 (when (find var
628 specs
629 :test (lambda (a b)
630 (and (equal (variable-local-name a)
631 (variable-local-name b))
632 (equal (variable-uri a)
633 (variable-uri b)))))
634 (xslt-error "duplicate definition for global variable ~A"
635 (variable-local-name var)))
636 (push var specs)))
637 ;; now that the global environment knows about all variables, run the
638 ;; thunk setters to perform their compilation
639 (setf specs (nreverse specs))
640 (mapc (lambda (spec) (funcall (variable-thunk-setter spec))) specs)
641 (setf (stylesheet-global-variables stylesheet) specs))))
643 (defun parse-templates! (stylesheet <transform> env)
644 (let ((i 0))
645 (dolist (<template> (stp:filter-children (of-name "template") <transform>))
646 (let ((*namespaces* (acons-namespaces <template>)))
647 (dolist (template (compile-template <template> env i))
648 (let ((name (template-name template)))
649 (if name
650 (let* ((table (stylesheet-named-templates stylesheet))
651 (head (car (gethash name table))))
652 (when (and head (eql (template-import-priority head)
653 (template-import-priority template)))
654 ;; fixme: is this supposed to be a run-time error?
655 (xslt-error "conflicting templates for ~A" name))
656 (push template (gethash name table)))
657 (let ((mode (ensure-mode/qname stylesheet
658 (template-mode-qname template)
659 env)))
660 (setf (template-mode template) mode)
661 (push template (mode-templates mode)))))))
662 (incf i))))
665 ;;;; APPLY-STYLESHEET
667 (defvar *stylesheet*)
668 (defvar *mode*)
670 (deftype xml-designator () '(or runes:xstream runes:rod array stream pathname))
672 (defstruct (parameter
673 (:constructor make-parameter (value local-name &optional uri)))
674 (uri "")
675 local-name
676 value)
678 (defun find-parameter-value (local-name uri parameters)
679 (dolist (p parameters)
680 (when (and (equal (parameter-local-name p) local-name)
681 (equal (parameter-uri p) uri))
682 (return (parameter-value p)))))
684 (defvar *uri-resolver*)
686 (defun parse-allowing-microsoft-bom (pathname handler)
687 (with-open-file (s pathname :element-type '(unsigned-byte 8))
688 (unless (and (eql (read-byte s nil) #xef)
689 (eql (read-byte s nil) #xbb)
690 (eql (read-byte s nil) #xbf))
691 (file-position s 0))
692 (cxml:parse s handler)))
694 (defun %document (uri-string base-uri)
695 (let* ((absolute-uri
696 (puri:merge-uris uri-string base-uri))
697 (resolved-uri
698 (if *uri-resolver*
699 (funcall *uri-resolver* (puri:render-uri absolute-uri nil))
700 absolute-uri))
701 (pathname
702 (handler-case
703 (uri-to-pathname resolved-uri)
704 (cxml:xml-parse-error (c)
705 (xslt-error "cannot find referenced document ~A: ~A"
706 resolved-uri c))))
707 (document
708 (handler-case
709 (parse-allowing-microsoft-bom pathname (stp:make-builder))
710 ((or file-error cxml:xml-parse-error) (c)
711 (xslt-error "cannot parse referenced document ~A: ~A"
712 pathname c))))
713 (xpath-root-node
714 (make-whitespace-stripper document
715 (stylesheet-strip-tests *stylesheet*))))
716 (when (puri:uri-fragment absolute-uri)
717 (xslt-error "use of fragment identifiers in document() not supported"))
718 xpath-root-node))
720 (xpath-sys:define-extension xslt *xsl*)
722 (xpath-sys:define-xpath-function/lazy
723 xslt :document
724 (object &optional node-set)
725 (let ((instruction-base-uri *instruction-base-uri*))
726 (lambda (ctx)
727 (let* ((object (funcall object ctx))
728 (node-set (and node-set (funcall node-set ctx)))
729 (uri
730 (when node-set
731 ;; FIXME: should use first node of the node set
732 ;; _in document order_
733 (xpath-protocol:base-uri (xpath:first-node node-set)))))
734 (xpath-sys:make-node-set
735 (if (xpath:node-set-p object)
736 (xpath:map-node-set->list
737 (lambda (node)
738 (%document (xpath:string-value node)
739 (or uri (xpath-protocol:base-uri node))))
740 object)
741 (list (%document (xpath:string-value object)
742 (or uri instruction-base-uri)))))))))
744 (xpath-sys:define-xpath-function/eager xslt :key (name object)
745 (let ((key (find-key (xpath:string-value name) *stylesheet*)))
746 (labels ((get-by-key (value)
747 (let ((value (xpath:string-value value)))
748 (xpath::filter-pipe
749 #'(lambda (node)
750 (equal value (xpath:string-value
751 (xpath:evaluate-compiled
752 (key-use key) node))))
753 (xpath-sys:pipe-of
754 (xpath:node-set-value
755 (xpath:evaluate-compiled
756 (key-match key) xpath:context)))))))
757 (xpath-sys:make-node-set
758 (xpath::sort-pipe
759 (if (xpath:node-set-p object)
760 (xpath::mappend-pipe #'get-by-key (xpath-sys:pipe-of object))
761 (get-by-key object)))))))
763 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
765 (xpath-sys:define-xpath-function/lazy xslt :current ()
766 #'(lambda (ctx)
767 (xpath-sys:make-node-set
768 (xpath-sys:make-pipe
769 (xpath:context-starting-node ctx)
770 nil))))
772 (xpath-sys:define-xpath-function/lazy xslt :generate-id (&optional node-set-thunk)
773 (if node-set-thunk
774 #'(lambda (ctx)
775 (xpath-sys:get-node-id (xpath:node-set-value (funcall node-set-thunk ctx))))
776 #'(lambda (ctx)
777 (xpath-sys:get-node-id (xpath:context-node ctx)))))
779 (defun apply-stylesheet
780 (stylesheet source-document
781 &key output parameters uri-resolver navigator)
782 (when (typep stylesheet 'xml-designator)
783 (setf stylesheet (parse-stylesheet stylesheet)))
784 (when (typep source-document 'xml-designator)
785 (setf source-document (cxml:parse source-document (stp:make-builder))))
786 (invoke-with-output-sink
787 (lambda ()
788 (handler-case*
789 (let* ((xpath:*navigator* (or navigator :default-navigator))
790 (puri:*strict-parse* nil)
791 (*stylesheet* stylesheet)
792 (*mode* (find-mode stylesheet nil))
793 (*empty-mode* (make-mode))
794 (global-variable-specs
795 (stylesheet-global-variables stylesheet))
796 (*global-variable-values*
797 (make-variable-value-array (length global-variable-specs)))
798 (*uri-resolver* uri-resolver)
799 (xpath-root-node
800 (make-whitespace-stripper
801 source-document
802 (stylesheet-strip-tests stylesheet)))
803 (ctx (xpath:make-context xpath-root-node)))
804 (mapc (lambda (spec)
805 (when (variable-param-p spec)
806 (let ((value
807 (find-parameter-value (variable-local-name spec)
808 (variable-uri spec)
809 parameters)))
810 (when value
811 (setf (global-variable-value (variable-index spec))
812 value)))))
813 global-variable-specs)
814 (mapc (lambda (spec)
815 (funcall (variable-thunk spec) ctx))
816 global-variable-specs)
817 #+nil (print global-variable-specs)
818 #+nil (print *global-variable-values*)
819 (apply-templates ctx))
820 (xpath:xpath-error (c)
821 (xslt-error "~A" c))))
822 stylesheet
823 output))
825 (defun find-attribute-set (local-name uri)
826 (or (gethash (cons local-name uri) (stylesheet-attribute-sets *stylesheet*))
827 (xslt-error "no such attribute set: ~A/~A" local-name uri)))
829 (defun apply-templates/list (list &optional param-bindings sort-predicate)
830 (when sort-predicate
831 (setf list (sort list sort-predicate)))
832 (let* ((n (length list))
833 (s/d (lambda () n)))
834 (loop
835 for i from 1
836 for child in list
838 (apply-templates (xpath:make-context child s/d i)
839 param-bindings))))
841 (defvar *stack-limit* 200)
843 (defun invoke-with-stack-limit (fn)
844 (let ((*stack-limit* (1- *stack-limit*)))
845 (unless (plusp *stack-limit*)
846 (xslt-error "*stack-limit* reached; stack overflow"))
847 (funcall fn)))
849 (defun invoke-template (ctx template param-bindings)
850 (let ((*lexical-variable-values*
851 (make-variable-value-array (template-n-variables template))))
852 (with-stack-limit ()
853 (loop
854 for (name-cons value) in param-bindings
855 for (nil index nil) = (find name-cons
856 (template-params template)
857 :test #'equal
858 :key #'car)
860 (unless index
861 (xslt-error "invalid template parameter ~A" name-cons))
862 (setf (lexical-variable-value index) value))
863 (funcall (template-body template) ctx))))
865 (defun apply-default-templates (ctx)
866 (let ((node (xpath:context-node ctx)))
867 (cond
868 ((or (xpath-protocol:node-type-p node :processing-instruction)
869 (xpath-protocol:node-type-p node :comment)))
870 ((or (xpath-protocol:node-type-p node :text)
871 (xpath-protocol:node-type-p node :attribute))
872 (write-text (xpath-protocol:node-text node)))
874 (apply-templates/list
875 (xpath::force
876 (xpath-protocol:child-pipe node)))))))
878 (defvar *apply-imports*)
880 (defun apply-applicable-templates (ctx templates param-bindings finally)
881 (labels ((apply-imports ()
882 (if templates
883 (let* ((this (pop templates))
884 (low (template-apply-imports-limit this))
885 (high (template-import-priority this)))
886 (setf templates
887 (remove-if-not
888 (lambda (x)
889 (<= low (template-import-priority x) high))
890 templates))
891 (invoke-template ctx this param-bindings))
892 (funcall finally))))
893 (let ((*apply-imports* #'apply-imports))
894 (apply-imports))))
896 (defun apply-templates (ctx &optional param-bindings)
897 (apply-applicable-templates ctx
898 (find-templates ctx)
899 param-bindings
900 (lambda ()
901 (apply-default-templates ctx))))
903 (defun call-template (ctx name &optional param-bindings)
904 (apply-applicable-templates ctx
905 (find-named-templates name)
906 param-bindings
907 (lambda ()
908 (error "cannot find named template: ~s"
909 name))))
911 (defun find-templates (ctx)
912 (let* ((matching-candidates
913 (remove-if-not (lambda (template)
914 (template-matches-p template ctx))
915 (mode-templates *mode*)))
916 (npriorities
917 (if matching-candidates
918 (1+ (reduce #'max
919 matching-candidates
920 :key #'template-import-priority))
922 (priority-groups (make-array npriorities :initial-element nil)))
923 (dolist (template matching-candidates)
924 (push template
925 (elt priority-groups (template-import-priority template))))
926 ;;; (print (map 'list #'length priority-groups))
927 ;;; (force-output)
928 (loop
929 for i from (1- npriorities) downto 0
930 for group = (elt priority-groups i)
931 for template = (maximize #'template< group)
932 when template
933 collect template)))
935 (defun find-named-templates (name)
936 (gethash name (stylesheet-named-templates *stylesheet*)))
938 (defun template< (a b) ;assuming same import priority
939 (let ((p (template-priority a))
940 (q (template-priority b)))
941 (cond
942 ((< p q) t)
943 ((> p q) nil)
945 (xslt-cerror "conflicting templates:~_~A,~_~A"
946 (template-match-expression a)
947 (template-match-expression b))
948 (< (template-position a) (template-position b))))))
950 (defun maximize (< things)
951 (when things
952 (let ((max (car things)))
953 (dolist (other (cdr things))
954 (when (funcall < max other)
955 (setf max other)))
956 max)))
958 (defun template-matches-p (template ctx)
959 (find (xpath:context-node ctx)
960 (xpath:all-nodes (funcall (template-match-thunk template) ctx))))
962 (defun invoke-with-output-sink (fn stylesheet output)
963 (etypecase output
964 (pathname
965 (with-open-file (s output
966 :direction :output
967 :element-type '(unsigned-byte 8)
968 :if-exists :rename-and-delete)
969 (invoke-with-output-sink fn stylesheet s)))
970 ((or stream null)
971 (invoke-with-output-sink fn
972 stylesheet
973 (make-output-sink stylesheet output)))
974 ((or hax:abstract-handler sax:abstract-handler)
975 (with-xml-output output
976 (funcall fn)))))
978 (defun make-output-sink (stylesheet stream)
979 (let* ((ystream
980 (if stream
981 (let ((et (stream-element-type stream)))
982 (cond
983 ((or (null et) (subtypep et '(unsigned-byte 8)))
984 (runes:make-octet-stream-ystream stream))
985 ((subtypep et 'character)
986 (runes:make-character-stream-ystream stream))))
987 (runes:make-rod-ystream)))
988 (output-spec (stylesheet-output-specification stylesheet))
989 (omit-xml-declaration-p
990 (equal (output-omit-xml-declaration output-spec) "yes"))
991 (sax-target
992 (make-instance 'cxml::sink
993 :ystream ystream
994 :omit-xml-declaration-p omit-xml-declaration-p)))
995 (if (equalp (output-method (stylesheet-output-specification stylesheet))
996 "HTML")
997 (make-instance 'combi-sink
998 :hax-target (make-instance 'chtml::sink
999 :ystream ystream)
1000 :sax-target sax-target
1001 :encoding (output-encoding output-spec))
1002 sax-target)))
1004 (defstruct template
1005 match-expression
1006 match-thunk
1007 name
1008 import-priority
1009 apply-imports-limit
1010 priority
1011 position
1012 mode
1013 mode-qname
1014 params
1015 body
1016 n-variables)
1018 (defun expression-priority (form)
1019 (let ((step (second form)))
1020 (if (and (null (cddr form))
1021 (listp step)
1022 (eq :child (car step))
1023 (null (cddr step)))
1024 (let ((name (second step)))
1025 (cond
1026 ((or (stringp name)
1027 (and (consp name)
1028 (or (eq (car name) :qname)
1029 (eq (car name) :processing-instruction))))
1030 0.0)
1031 ((and (consp name)
1032 (or (eq (car name) :namespace)
1033 (eq (car name) '*)))
1034 -0.25)
1036 -0.5)))
1037 0.5)))
1039 (defun valid-expression-p (expr)
1040 (cond
1041 ((atom expr) t)
1042 ((eq (first expr) :path)
1043 (every (lambda (x)
1044 (let ((filter (third x)))
1045 (or (null filter) (valid-expression-p filter))))
1046 (cdr expr)))
1047 ((eq (first expr) :variable) ;(!)
1048 nil)
1050 (every #'valid-expression-p (cdr expr)))))
1052 (defun parse-xpath (str)
1053 (handler-case
1054 (xpath:parse-xpath str)
1055 (xpath:xpath-error (c)
1056 (xslt-error "~A" c))))
1058 (defun parse-key-pattern (str)
1059 (let ((parsed
1060 (mapcar #'(lambda (item)
1061 `(:path (:root :node)
1062 (:descendant-or-self *)
1063 ,@(cdr item)))
1064 (parse-pattern str))))
1065 (if (null (rest parsed))
1066 (first parsed)
1067 `(:union ,@parsed))))
1069 (defun parse-pattern (str)
1070 ;; zzz check here for anything not allowed as an XSLT pattern
1071 ;; zzz can we hack id() and key() here?
1072 (let ((form (parse-xpath str)))
1073 (unless (consp form)
1074 (xslt-error "not a valid pattern: ~A" str))
1075 (labels ((process-form (form)
1076 (cond ((eq (car form) :union)
1077 (alexandria:mappend #'process-form (rest form)))
1078 ((not (eq (car form) :path)) ;zzz: filter statt path
1079 (xslt-error "not a valid pattern: ~A" str))
1080 ((not (valid-expression-p form))
1081 (xslt-error "invalid filter"))
1082 (t (list form)))))
1083 (process-form form))))
1085 (defun compile-value-thunk (value env)
1086 (if (and (listp value) (eq (car value) 'progn))
1087 (let ((inner-thunk (compile-instruction value env)))
1088 (lambda (ctx)
1089 (apply-to-result-tree-fragment ctx inner-thunk)))
1090 (compile-xpath value env)))
1092 (defun compile-var-bindings/nointern (forms env)
1093 (loop
1094 for (name value) in forms
1095 collect (multiple-value-bind (local-name uri)
1096 (decode-qname name env nil)
1097 (list (cons local-name uri)
1098 (xslt-trace-thunk
1099 (compile-value-thunk value env)
1100 "local variable ~s = ~s" name :result)))))
1102 (defun compile-var-bindings (forms env)
1103 (loop
1104 for (cons thunk) in (compile-var-bindings/nointern forms env)
1105 for (local-name . uri) = cons
1106 collect (list cons
1107 (push-variable local-name
1109 *lexical-variable-declarations*)
1110 thunk)))
1112 (defun compile-template (<template> env position)
1113 (stp:with-attributes (match name priority mode) <template>
1114 (unless (or name match)
1115 (xslt-error "missing match in template"))
1116 (multiple-value-bind (params body-pos)
1117 (loop
1118 for i from 0
1119 for child in (stp:list-children <template>)
1120 while (namep child "param")
1121 collect (parse-param child) into params
1122 finally (return (values params i)))
1123 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1124 (param-bindings (compile-var-bindings params env))
1125 (body (parse-body <template> body-pos (mapcar #'car params)))
1126 (body-thunk (compile-instruction `(progn ,@body) env))
1127 (outer-body-thunk
1128 (xslt-trace-thunk
1129 #'(lambda (ctx)
1130 (unwind-protect
1131 (progn
1132 ;; set params that weren't initialized by apply-templates
1133 (loop for (name index param-thunk) in param-bindings
1134 when (eq (lexical-variable-value index nil) 'unbound)
1135 do (setf (lexical-variable-value index)
1136 (funcall param-thunk ctx)))
1137 (funcall body-thunk ctx))))
1138 "template: match = ~s name = ~s" match name))
1139 (n-variables (length *lexical-variable-declarations*)))
1140 (append
1141 (when name
1142 (multiple-value-bind (local-name uri)
1143 (decode-qname name env nil)
1144 (list
1145 (make-template :name (cons local-name uri)
1146 :import-priority *import-priority*
1147 :apply-imports-limit *apply-imports-limit*
1148 :params param-bindings
1149 :body outer-body-thunk
1150 :n-variables n-variables))))
1151 (when match
1152 (mapcar (lambda (expression)
1153 (let ((match-thunk
1154 (xslt-trace-thunk
1155 (compile-xpath
1156 `(xpath:xpath
1157 (:path (:ancestor-or-self :node)
1158 ,@(cdr expression)))
1159 env)
1160 "match-thunk for template (match ~s): ~s --> ~s"
1161 match expression :result))
1162 (p (if priority
1163 (parse-number:parse-number priority)
1164 (expression-priority expression))))
1165 (make-template :match-expression expression
1166 :match-thunk match-thunk
1167 :import-priority *import-priority*
1168 :apply-imports-limit *apply-imports-limit*
1169 :priority p
1170 :position position
1171 :mode-qname mode
1172 :params param-bindings
1173 :body outer-body-thunk
1174 :n-variables n-variables)))
1175 (parse-pattern match))))))))
1176 #+(or)
1177 (xuriella::parse-stylesheet #p"/home/david/src/lisp/xuriella/test.xsl")