updated TEST for recent merges
[xuriella.git] / xslt.lisp
blob1a531de8c42d78bbd996ed89aa686d6538e74805
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-function
170 ((env xslt-environment) lname uri)
171 (if (string= uri "")
172 (or (xpath:find-xpath-function lname *xsl*)
173 (xpath:find-xpath-function lname uri))
174 (xpath:find-xpath-function lname uri)))
176 (defmethod xpath: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: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: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)))
255 (defstruct mode (templates nil))
257 (defun find-mode (stylesheet local-name &optional uri)
258 (gethash (cons local-name uri) (stylesheet-modes stylesheet)))
260 (defun ensure-mode (stylesheet &optional local-name uri)
261 (or (find-mode stylesheet local-name uri)
262 (setf (gethash (cons local-name uri) (stylesheet-modes stylesheet))
263 (make-mode))))
265 (defun ensure-mode/qname (stylesheet qname env)
266 (if qname
267 (multiple-value-bind (local-name uri)
268 (decode-qname qname env nil)
269 (ensure-mode stylesheet local-name uri))
270 (find-mode stylesheet nil)))
272 (defun acons-namespaces (element &optional (bindings *namespaces*))
273 (map-namespace-declarations (lambda (prefix uri)
274 (push (cons prefix uri) bindings))
275 element)
276 bindings)
278 (defvar *excluded-namespaces* (list *xsl*))
279 (defvar *empty-mode*)
281 (defvar *xsl-include-stack* nil)
283 (defun uri-to-pathname (uri)
284 (cxml::uri-to-pathname (puri:parse-uri uri)))
286 (defun parse-stylesheet-to-stp (input uri-resolver)
287 (let* ((d (cxml:parse input (make-text-normalizer (cxml-stp:make-builder))))
288 (<transform> (stp:document-element d)))
289 (strip-stylesheet <transform>)
290 ;; FIXME: handle embedded stylesheets
291 (unless (and (equal (stp:namespace-uri <transform>) *xsl*)
292 (or (equal (stp:local-name <transform>) "transform")
293 (equal (stp:local-name <transform>) "stylesheet")))
294 (xslt-error "not a stylesheet"))
295 (dolist (include (stp:filter-children (of-name "include") <transform>))
296 (let* ((uri (puri:merge-uris (stp:attribute-value include "href")
297 (stp:base-uri include)))
298 (uri (if uri-resolver
299 (funcall uri-resolver (puri:render-uri uri nil))
300 uri))
301 (str (puri:render-uri uri nil))
302 (pathname
303 (handler-case
304 (uri-to-pathname uri)
305 (cxml:xml-parse-error (c)
306 (xslt-error "cannot find included stylesheet ~A: ~A"
307 uri c)))))
308 (with-open-file
309 (stream pathname
310 :element-type '(unsigned-byte 8)
311 :if-does-not-exist nil)
312 (unless stream
313 (xslt-error "cannot find included stylesheet ~A at ~A"
314 uri pathname))
315 (when (find str *xsl-include-stack* :test #'equal)
316 (xslt-error "recursive inclusion of ~A" uri))
317 (let* ((*xsl-include-stack* (cons str *xsl-include-stack*))
318 (<transform>2 (parse-stylesheet-to-stp stream uri-resolver)))
319 (stp:do-children (child <transform>2)
320 (stp:insert-child-after <transform>
321 (stp:copy child)
322 include))
323 (stp:detach include)))))
324 <transform>))
326 (defvar *instruction-base-uri*)
327 (defvar *apply-imports-limit*)
328 (defvar *import-priority*)
330 (defun parse-1-stylesheet (env stylesheet designator uri-resolver)
331 (let* ((<transform> (parse-stylesheet-to-stp designator uri-resolver))
332 (*instruction-base-uri* (stp:base-uri <transform>))
333 (*namespaces* (acons-namespaces <transform>))
334 (*apply-imports-limit* (1+ *import-priority*)))
335 (dolist (import (stp:filter-children (of-name "import") <transform>))
336 (let ((uri (puri:merge-uris (stp:attribute-value import "href")
337 (stp:base-uri import))))
338 (parse-imported-stylesheet env stylesheet uri uri-resolver)))
339 (incf *import-priority*)
340 (parse-exclude-result-prefixes! <transform> env)
341 (parse-global-variables! stylesheet <transform>)
342 (parse-templates! stylesheet <transform> env)
343 (parse-output! stylesheet <transform>)
344 (parse-strip/preserve-space! stylesheet <transform> env)
345 (parse-attribute-sets! stylesheet <transform> env)))
347 (defvar *xsl-import-stack* nil)
349 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver)
350 (let* ((uri (if uri-resolver
351 (funcall uri-resolver (puri:render-uri uri nil))
352 uri))
353 (str (puri:render-uri uri nil))
354 (pathname
355 (handler-case
356 (uri-to-pathname uri)
357 (cxml:xml-parse-error (c)
358 (xslt-error "cannot find imported stylesheet ~A: ~A"
359 uri c)))))
360 (with-open-file
361 (stream pathname
362 :element-type '(unsigned-byte 8)
363 :if-does-not-exist nil)
364 (unless stream
365 (xslt-error "cannot find imported stylesheet ~A at ~A"
366 uri pathname))
367 (when (find str *xsl-import-stack* :test #'equal)
368 (xslt-error "recursive inclusion of ~A" uri))
369 (let ((*xsl-import-stack* (cons str *xsl-import-stack*)))
370 (parse-1-stylesheet env stylesheet stream uri-resolver)))))
372 (defun parse-stylesheet (designator &key uri-resolver)
373 (let* ((*import-priority* 0)
374 (puri:*strict-parse* nil)
375 (stylesheet (make-stylesheet))
376 (env (make-instance 'lexical-xslt-environment))
377 (*excluded-namespaces* *excluded-namespaces*)
378 (*global-variable-declarations* (make-empty-declaration-array)))
379 (ensure-mode stylesheet nil)
380 (parse-1-stylesheet env stylesheet designator uri-resolver)
381 ;; reverse attribute sets:
382 (let ((table (stylesheet-attribute-sets stylesheet)))
383 (maphash (lambda (k v)
384 (setf (gethash k table) (nreverse v)))
385 table))
386 stylesheet))
388 (defun parse-attribute-sets! (stylesheet <transform> env)
389 (dolist (elt (stp:filter-children (of-name "attribute-set") <transform>))
390 (push (let* ((sets
391 (mapcar (lambda (qname)
392 (multiple-value-list (decode-qname qname env nil)))
393 (words
394 (stp:attribute-value elt "use-attribute-sets"))))
395 (instructions
396 (stp:map-children 'list #'parse-instruction elt))
397 (*lexical-variable-declarations*
398 (make-empty-declaration-array))
399 (thunk
400 (compile-instruction `(progn ,@instructions) env))
401 (n-variables (length *lexical-variable-declarations*)))
402 (lambda (ctx)
403 (with-stack-limit ()
404 (loop for (local-name uri nil) in sets do
405 (dolist (thunk (find-attribute-set local-name uri))
406 (funcall thunk ctx)))
407 (let ((*lexical-variable-values*
408 (make-variable-value-array n-variables)))
409 (funcall thunk ctx)))))
410 (gethash (multiple-value-bind (local-name uri)
411 (decode-qname (stp:attribute-value elt "name") env nil)
412 (cons local-name uri))
413 (stylesheet-attribute-sets stylesheet)))))
415 (defun parse-exclude-result-prefixes! (<transform> env)
416 (stp:with-attributes (exclude-result-prefixes) <transform>
417 (dolist (prefix (words (or exclude-result-prefixes "")))
418 (when (equal prefix "#default")
419 (setf prefix nil))
420 (push (or (xpath:environment-find-namespace env prefix)
421 (xslt-error "namespace not found: ~A" prefix))
422 *excluded-namespaces*))))
424 (defun parse-strip/preserve-space! (stylesheet <transform> env)
425 (xpath:with-namespaces ((nil #.*xsl*))
426 (dolist (elt (stp:filter-children (lambda (x)
427 (or (namep x "strip-space")
428 (namep x "preserve-space")))
429 <transform>))
430 (let ((*namespaces* (acons-namespaces elt))
431 (mode
432 (if (equal (stp:local-name elt) "strip-space")
433 :strip
434 :preserve)))
435 (dolist (name-test (words (stp:attribute-value elt "elements")))
436 (let* ((pos (search ":*" name-test))
437 (test-function
438 (cond
439 ((eql pos (- (length name-test) 2))
440 (let* ((prefix (subseq name-test 0 pos))
441 (name-test-uri
442 (xpath:environment-find-namespace env prefix)))
443 (unless (xpath::nc-name-p prefix)
444 (xslt-error "not an NCName: ~A" prefix))
445 (lambda (local-name uri)
446 (declare (ignore local-name))
447 (if (equal uri name-test-uri)
448 mode
449 nil))))
450 ((equal name-test "*")
451 (lambda (local-name uri)
452 (declare (ignore local-name uri))
453 mode))
455 (multiple-value-bind (name-test-local-name name-test-uri)
456 (decode-qname name-test env nil)
457 (lambda (local-name uri)
458 (if (and (equal local-name name-test-local-name)
459 (equal uri name-test-uri))
460 mode
461 nil)))))))
462 (push test-function (stylesheet-strip-tests stylesheet))))))))
464 (defstruct (output-specification
465 (:conc-name "OUTPUT-"))
466 method
467 indent
468 omit-xml-declaration
469 encoding)
471 (defun parse-output! (stylesheet <transform>)
472 (let ((outputs (stp:filter-children (of-name "output") <transform>)))
473 (when outputs
474 (when (cdr outputs)
475 ;; FIXME:
476 ;; - concatenate cdata-section-elements
477 ;; - the others must not conflict
478 (error "oops, merging of output elements not supported yet"))
479 (let ((<output> (car outputs))
480 (spec (stylesheet-output-specification stylesheet)))
481 (stp:with-attributes (;; version
482 method
483 indent
484 encoding
485 ;;; media-type
486 ;;; doctype-system
487 ;;; doctype-public
488 omit-xml-declaration
489 ;;; standalone
490 ;;; cdata-section-elements
492 <output>
493 (setf (output-method spec) method)
494 (setf (output-indent spec) indent)
495 (setf (output-encoding spec) encoding)
496 (setf (output-omit-xml-declaration spec) omit-xml-declaration))))))
498 (defun make-empty-declaration-array ()
499 (make-array 1 :fill-pointer 0 :adjustable t))
501 (defun make-variable-value-array (n-lexical-variables)
502 (make-array n-lexical-variables :initial-element 'unbound))
504 (defun compile-global-variable (<variable> env) ;; also for <param>
505 (stp:with-attributes (name select) <variable>
506 (when (and select (stp:list-children <variable>))
507 (xslt-error "variable with select and body"))
508 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
509 (inner (cond
510 (select
511 (compile-xpath select env))
512 ((stp:list-children <variable>)
513 (let* ((inner-sexpr `(progn ,@(parse-body <variable>)))
514 (inner-thunk (compile-instruction inner-sexpr env)))
515 (lambda (ctx)
516 (apply-to-result-tree-fragment ctx inner-thunk))))
518 (lambda (ctx)
519 (declare (ignore ctx))
520 ""))))
521 (n-lexical-variables (length *lexical-variable-declarations*)))
522 (xslt-trace-thunk
523 (lambda (ctx)
524 (let* ((*lexical-variable-values*
525 (make-variable-value-array n-lexical-variables)))
526 (funcall inner ctx)))
527 "global ~s (~s) = ~s" name select :result))))
529 (defstruct (variable-information
530 (:constructor make-variable)
531 (:conc-name "VARIABLE-"))
532 index
533 thunk
534 local-name
536 param-p
537 thunk-setter)
539 (defun parse-global-variable! (<variable> global-env) ;; also for <param>
540 (let ((*namespaces* (acons-namespaces <variable>))
541 (qname (stp:attribute-value <variable> "name")))
542 (unless qname
543 (xslt-error "name missing in ~A" (stp:local-name <variable>)))
544 (multiple-value-bind (local-name uri)
545 (decode-qname qname global-env nil)
546 ;; For the normal compilation environment of templates, install it
547 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
548 (let ((index (intern-global-variable local-name uri)))
549 ;; For the evaluation of a global variable itself, build a thunk
550 ;; that lazily resolves other variables, stored into
551 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
552 (let* ((value-thunk :unknown)
553 (global-variable-thunk
554 (lambda (ctx)
555 (let ((v (global-variable-value index nil)))
556 (when (eq v 'seen)
557 (xslt-error "recursive variable definition"))
558 (cond
559 ((eq v 'unbound)
560 ;; (print (list :computing index))
561 (setf (global-variable-value index) 'seen)
562 (setf (global-variable-value index)
563 (funcall value-thunk ctx))
564 #+nil (print (list :done-computing index
565 (global-variable-value index)))
566 #+nil (global-variable-value index))
568 #+nil(print (list :have
569 index v))
570 v)))))
571 (thunk-setter
572 (lambda ()
573 (setf value-thunk
574 (compile-global-variable <variable> global-env)))))
575 (setf (gethash (cons local-name uri)
576 (initial-global-variable-thunks global-env))
577 global-variable-thunk)
578 (make-variable :index index
579 :local-name local-name
580 :uri uri
581 :thunk global-variable-thunk
582 :param-p (namep <variable> "param")
583 :thunk-setter thunk-setter))))))
585 (defun parse-global-variables! (stylesheet <transform>)
586 (xpath:with-namespaces ((nil #.*xsl*))
587 (let* ((table (make-hash-table :test 'equal))
588 (global-env (make-instance 'global-variable-environment
589 :initial-global-variable-thunks table))
590 (specs '()))
591 (xpath:do-node-set
592 (<variable> (xpath:evaluate "variable|param" <transform>))
593 (let ((var (parse-global-variable! <variable> global-env)))
594 (xslt-trace "parsing global variable ~s (uri ~s)"
595 (variable-local-name var)
596 (variable-uri var))
597 (when (find var
598 specs
599 :test (lambda (a b)
600 (and (equal (variable-local-name a)
601 (variable-local-name b))
602 (equal (variable-uri a)
603 (variable-uri b)))))
604 (xslt-error "duplicate definition for global variable ~A"
605 (variable-local-name var)))
606 (push var specs)))
607 ;; now that the global environment knows about all variables, run the
608 ;; thunk setters to perform their compilation
609 (setf specs (nreverse specs))
610 (mapc (lambda (spec) (funcall (variable-thunk-setter spec))) specs)
611 (setf (stylesheet-global-variables stylesheet) specs))))
613 (defun parse-templates! (stylesheet <transform> env)
614 (let ((i 0))
615 (dolist (<template> (stp:filter-children (of-name "template") <transform>))
616 (let ((*namespaces* (acons-namespaces <template>)))
617 (dolist (template (compile-template <template> env i))
618 (let ((name (template-name template)))
619 (if name
620 (let* ((table (stylesheet-named-templates stylesheet))
621 (head (car (gethash name table))))
622 (when (and head (eql (template-import-priority head)
623 (template-import-priority template)))
624 ;; fixme: is this supposed to be a run-time error?
625 (xslt-error "conflicting templates for ~A" name))
626 (push template (gethash name table)))
627 (let ((mode (ensure-mode/qname stylesheet
628 (template-mode-qname template)
629 env)))
630 (setf (template-mode template) mode)
631 (push template (mode-templates mode)))))))
632 (incf i))))
635 ;;;; APPLY-STYLESHEET
637 (defvar *stylesheet*)
638 (defvar *mode*)
640 (deftype xml-designator () '(or runes:xstream runes:rod array stream pathname))
642 (defstruct (parameter
643 (:constructor make-parameter (value local-name &optional uri)))
644 (uri "")
645 local-name
646 value)
648 (defun find-parameter-value (local-name uri parameters)
649 (dolist (p parameters)
650 (when (and (equal (parameter-local-name p) local-name)
651 (equal (parameter-uri p) uri))
652 (return (parameter-value p)))))
654 (defvar *uri-resolver*)
656 (defun parse-allowing-microsoft-bom (pathname handler)
657 (with-open-file (s pathname :element-type '(unsigned-byte 8))
658 (unless (and (eql (read-byte s nil) #xef)
659 (eql (read-byte s nil) #xbb)
660 (eql (read-byte s nil) #xbf))
661 (file-position s 0))
662 (cxml:parse s handler)))
664 (defun %document (uri-string base-uri)
665 (let* ((absolute-uri
666 (puri:merge-uris uri-string base-uri))
667 (resolved-uri
668 (if *uri-resolver*
669 (funcall *uri-resolver* (puri:render-uri absolute-uri nil))
670 absolute-uri))
671 (pathname
672 (handler-case
673 (uri-to-pathname resolved-uri)
674 (cxml:xml-parse-error (c)
675 (xslt-error "cannot find referenced document ~A: ~A"
676 resolved-uri c))))
677 (document
678 (handler-case
679 (parse-allowing-microsoft-bom pathname (stp:make-builder))
680 ((or file-error cxml:xml-parse-error) (c)
681 (xslt-error "cannot parse referenced document ~A: ~A"
682 pathname c))))
683 (xpath-root-node
684 (make-whitespace-stripper document
685 (stylesheet-strip-tests *stylesheet*))))
686 (when (puri:uri-fragment absolute-uri)
687 (xslt-error "use of fragment identifiers in document() not supported"))
688 xpath-root-node))
690 (xpath:define-extension xslt *xsl*)
692 (xpath:define-xpath-function/lazy
693 xslt :document
694 (object &optional node-set)
695 (let ((instruction-base-uri *instruction-base-uri*))
696 (lambda (ctx)
697 (declare (ignore ctx))
698 (let* ((object (funcall object))
699 (node-set (and node-set (funcall node-set)))
700 (uri
701 (when node-set
702 ;; FIXME: should use first node of the node set
703 ;; _in document order_
704 (xpath-protocol:base-uri (xpath:first-node node-set)))))
705 (xpath::make-node-set
706 (if (xpath:node-set-p object)
707 (xpath:map-node-set->list
708 (lambda (node)
709 (%document (xpath:string-value node)
710 (or uri (xpath-protocol:base-uri node))))
711 object)
712 (list (%document (xpath:string-value object)
713 (or uri instruction-base-uri)))))))))
715 (defun apply-stylesheet
716 (stylesheet source-document &key output parameters uri-resolver)
717 (when (typep stylesheet 'xml-designator)
718 (setf stylesheet (parse-stylesheet stylesheet)))
719 (when (typep source-document 'xml-designator)
720 (setf source-document (cxml:parse source-document (stp:make-builder))))
721 (invoke-with-output-sink
722 (lambda ()
723 (handler-case*
724 (let* ((puri:*strict-parse* nil)
725 (*stylesheet* stylesheet)
726 (*mode* (find-mode stylesheet nil))
727 (*empty-mode* (make-mode))
728 (global-variable-specs
729 (stylesheet-global-variables stylesheet))
730 (*global-variable-values*
731 (make-variable-value-array (length global-variable-specs)))
732 (*uri-resolver* uri-resolver)
733 (xpath-root-node
734 (make-whitespace-stripper
735 source-document
736 (stylesheet-strip-tests stylesheet)))
737 (ctx (xpath:make-context xpath-root-node)))
738 (mapc (lambda (spec)
739 (when (variable-param-p spec)
740 (let ((value
741 (find-parameter-value (variable-local-name spec)
742 (variable-uri spec)
743 parameters)))
744 (when value
745 (setf (global-variable-value (variable-index spec))
746 value)))))
747 global-variable-specs)
748 (mapc (lambda (spec)
749 (funcall (variable-thunk spec) ctx))
750 global-variable-specs)
751 #+nil (print global-variable-specs)
752 #+nil (print *global-variable-values*)
753 (apply-templates ctx))
754 (xpath:xpath-error (c)
755 (xslt-error "~A" c))))
756 stylesheet
757 output))
759 (defun find-attribute-set (local-name uri)
760 (or (gethash (cons local-name uri) (stylesheet-attribute-sets *stylesheet*))
761 (xslt-error "no such attribute set: ~A/~A" local-name uri)))
763 (defun apply-templates/list (list &optional param-bindings sort-predicate)
764 (when sort-predicate
765 (setf list (sort list sort-predicate)))
766 (let* ((n (length list))
767 (s/d (lambda () n)))
768 (loop
769 for i from 1
770 for child in list
772 (apply-templates (xpath:make-context child s/d i)
773 param-bindings))))
775 (defvar *stack-limit* 200)
777 (defun invoke-with-stack-limit (fn)
778 (let ((*stack-limit* (1- *stack-limit*)))
779 (unless (plusp *stack-limit*)
780 (xslt-error "*stack-limit* reached; stack overflow"))
781 (funcall fn)))
783 (defun invoke-template (ctx template param-bindings)
784 (let ((*lexical-variable-values*
785 (make-variable-value-array (template-n-variables template))))
786 (with-stack-limit ()
787 (loop
788 for (name-cons value) in param-bindings
789 for (nil index nil) = (find name-cons
790 (template-params template)
791 :test #'equal
792 :key #'car)
794 (unless index
795 (xslt-error "invalid template parameter ~A" name-cons))
796 (setf (lexical-variable-value index) value))
797 (funcall (template-body template) ctx))))
799 (defun apply-default-templates (ctx)
800 (let ((node (xpath:context-node ctx)))
801 (cond
802 ((or (xpath-protocol:node-type-p node :processing-instruction)
803 (xpath-protocol:node-type-p node :comment)))
804 ((or (xpath-protocol:node-type-p node :text)
805 (xpath-protocol:node-type-p node :attribute))
806 (write-text (xpath-protocol:string-value node)))
808 (apply-templates/list
809 (xpath::force
810 (xpath-protocol:child-pipe node)))))))
812 (defvar *apply-imports*)
814 (defun apply-applicable-templates (ctx templates param-bindings finally)
815 (labels ((apply-imports ()
816 (if templates
817 (let* ((this (pop templates))
818 (low (template-apply-imports-limit this))
819 (high (template-import-priority this)))
820 (setf templates
821 (remove-if-not
822 (lambda (x)
823 (<= low (template-import-priority x) high))
824 templates))
825 (invoke-template ctx this param-bindings))
826 (funcall finally))))
827 (let ((*apply-imports* #'apply-imports))
828 (apply-imports))))
830 (defun apply-templates (ctx &optional param-bindings)
831 (apply-applicable-templates ctx
832 (find-templates ctx)
833 param-bindings
834 (lambda ()
835 (apply-default-templates ctx))))
837 (defun call-template (ctx name &optional param-bindings)
838 (apply-applicable-templates ctx
839 (find-named-templates name)
840 param-bindings
841 (lambda ()
842 (error "cannot find named template: ~s"
843 name))))
845 (defun find-templates (ctx)
846 (let* ((matching-candidates
847 (remove-if-not (lambda (template)
848 (template-matches-p template ctx))
849 (mode-templates *mode*)))
850 (npriorities
851 (if matching-candidates
852 (1+ (reduce #'max
853 matching-candidates
854 :key #'template-import-priority))
856 (priority-groups (make-array npriorities :initial-element nil)))
857 (dolist (template matching-candidates)
858 (push template
859 (elt priority-groups (template-import-priority template))))
860 ;;; (print (map 'list #'length priority-groups))
861 ;;; (force-output)
862 (loop
863 for i from (1- npriorities) downto 0
864 for group = (elt priority-groups i)
865 for template = (maximize #'template< group)
866 when template
867 collect template)))
869 (defun find-named-templates (name)
870 (gethash name (stylesheet-named-templates *stylesheet*)))
872 (defun template< (a b) ;assuming same import priority
873 (let ((p (template-priority a))
874 (q (template-priority b)))
875 (cond
876 ((< p q) t)
877 ((> p q) nil)
879 (xslt-cerror "conflicting templates:~_~A,~_~A"
880 (template-match-expression a)
881 (template-match-expression b))
882 (< (template-position a) (template-position b))))))
884 (defun maximize (< things)
885 (when things
886 (let ((max (car things)))
887 (dolist (other (cdr things))
888 (when (funcall < max other)
889 (setf max other)))
890 max)))
892 (defun template-matches-p (template ctx)
893 (find (xpath:context-node ctx)
894 (xpath:all-nodes (funcall (template-match-thunk template) ctx))))
896 (defun invoke-with-output-sink (fn stylesheet output)
897 (etypecase output
898 (pathname
899 (with-open-file (s output
900 :direction :output
901 :element-type '(unsigned-byte 8)
902 :if-exists :rename-and-delete)
903 (invoke-with-output-sink fn stylesheet s)))
904 ((or stream null)
905 (invoke-with-output-sink fn
906 stylesheet
907 (make-output-sink stylesheet output)))
908 ((or hax:abstract-handler sax:abstract-handler)
909 (with-xml-output output
910 (funcall fn)))))
912 (defun make-output-sink (stylesheet stream)
913 (let* ((ystream
914 (if stream
915 (let ((et (stream-element-type stream)))
916 (cond
917 ((or (null et) (subtypep et '(unsigned-byte 8)))
918 (runes:make-octet-stream-ystream stream))
919 ((subtypep et 'character)
920 (runes:make-character-stream-ystream stream))))
921 (runes:make-rod-ystream)))
922 (output-spec (stylesheet-output-specification stylesheet))
923 (omit-xml-declaration-p
924 (equal (output-omit-xml-declaration output-spec) "yes"))
925 (sax-target
926 (make-instance 'cxml::sink
927 :ystream ystream
928 :omit-xml-declaration-p omit-xml-declaration-p)))
929 (if (equalp (output-method (stylesheet-output-specification stylesheet))
930 "HTML")
931 (make-instance 'combi-sink
932 :hax-target (make-instance 'chtml::sink
933 :ystream ystream)
934 :sax-target sax-target
935 :encoding (output-encoding output-spec))
936 sax-target)))
938 (defstruct template
939 match-expression
940 match-thunk
941 name
942 import-priority
943 apply-imports-limit
944 priority
945 position
946 mode
947 mode-qname
948 params
949 body
950 n-variables)
952 (defun expression-priority (form)
953 (let ((step (second form)))
954 (if (and (null (cddr form))
955 (listp step)
956 (eq :child (car step))
957 (null (cddr step)))
958 (let ((name (second step)))
959 (cond
960 ((or (stringp name)
961 (and (consp name)
962 (or (eq (car name) :qname)
963 (eq (car name) :processing-instruction))))
964 0.0)
965 ((and (consp name)
966 (or (eq (car name) :namespace)
967 (eq (car name) '*)))
968 -0.25)
970 -0.5)))
971 0.5)))
973 (defun valid-expression-p (expr)
974 (cond
975 ((atom expr) t)
976 ((eq (first expr) :path)
977 (every (lambda (x)
978 (let ((filter (third x)))
979 (or (null filter) (valid-expression-p filter))))
980 (cdr expr)))
981 ((eq (first expr) :variable) ;(!)
982 nil)
984 (every #'valid-expression-p (cdr expr)))))
986 (defun parse-pattern (str)
987 ;; zzz check here for anything not allowed as an XSLT pattern
988 ;; zzz can we hack id() and key() here?
989 (let ((form (xpath:parse-xpath str)))
990 (unless (consp form)
991 (xslt-error "not a valid pattern: ~A" str))
992 (labels ((process-form (form)
993 (cond ((eq (car form) :union)
994 (alexandria:mappend #'process-form (rest form)))
995 ((not (eq (car form) :path)) ;zzz: filter statt path
996 (xslt-error "not a valid pattern: ~A" str))
997 ((not (valid-expression-p form))
998 (xslt-error "invalid filter"))
999 (t (list form)))))
1000 (process-form form))))
1002 (defun compile-value-thunk (value env)
1003 (if (and (listp value) (eq (car value) 'progn))
1004 (let ((inner-thunk (compile-instruction value env)))
1005 (lambda (ctx)
1006 (apply-to-result-tree-fragment ctx inner-thunk)))
1007 (compile-xpath value env)))
1009 (defun compile-var-bindings/nointern (forms env)
1010 (loop
1011 for (name value) in forms
1012 collect (multiple-value-bind (local-name uri)
1013 (decode-qname name env nil)
1014 (list (cons local-name uri)
1015 (xslt-trace-thunk
1016 (compile-value-thunk value env)
1017 "local variable ~s = ~s" name :result)))))
1019 (defun compile-var-bindings (forms env)
1020 (loop
1021 for (cons thunk) in (compile-var-bindings/nointern forms env)
1022 for (local-name . uri) = cons
1023 collect (list cons
1024 (push-variable local-name
1026 *lexical-variable-declarations*)
1027 thunk)))
1029 (defun compile-template (<template> env position)
1030 (stp:with-attributes (match name priority mode) <template>
1031 (unless (or name match)
1032 (xslt-error "missing match in template"))
1033 (multiple-value-bind (params body-pos)
1034 (loop
1035 for i from 0
1036 for child in (stp:list-children <template>)
1037 while (namep child "param")
1038 collect (parse-param child) into params
1039 finally (return (values params i)))
1040 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1041 (param-bindings (compile-var-bindings params env))
1042 (body (parse-body <template> body-pos (mapcar #'car params)))
1043 (body-thunk (compile-instruction `(progn ,@body) env))
1044 (outer-body-thunk
1045 (xslt-trace-thunk
1046 #'(lambda (ctx)
1047 (unwind-protect
1048 (progn
1049 ;; set params that weren't initialized by apply-templates
1050 (loop for (name index param-thunk) in param-bindings
1051 when (eq (lexical-variable-value index nil) 'unbound)
1052 do (setf (lexical-variable-value index)
1053 (funcall param-thunk ctx)))
1054 (funcall body-thunk ctx))))
1055 "template: match = ~s name = ~s" match name))
1056 (n-variables (length *lexical-variable-declarations*)))
1057 (append
1058 (when name
1059 (multiple-value-bind (local-name uri)
1060 (decode-qname name env nil)
1061 (list
1062 (make-template :name (cons local-name uri)
1063 :import-priority *import-priority*
1064 :apply-imports-limit *apply-imports-limit*
1065 :params param-bindings
1066 :body outer-body-thunk
1067 :n-variables n-variables))))
1068 (when match
1069 (mapcar (lambda (expression)
1070 (let ((match-thunk
1071 (xslt-trace-thunk
1072 (compile-xpath
1073 `(xpath:xpath
1074 (:path (:ancestor-or-self :node)
1075 ,@(cdr expression)))
1076 env)
1077 "match-thunk for template (match ~s): ~s --> ~s"
1078 match expression :result))
1079 (p (if priority
1080 (parse-number:parse-number priority)
1081 (expression-priority expression))))
1082 (make-template :match-expression expression
1083 :match-thunk match-thunk
1084 :import-priority *import-priority*
1085 :apply-imports-limit *apply-imports-limit*
1086 :priority p
1087 :position position
1088 :mode-qname mode
1089 :params param-bindings
1090 :body outer-body-thunk
1091 :n-variables n-variables)))
1092 (parse-pattern match))))))))
1093 #+(or)
1094 (xuriella::parse-stylesheet #p"/home/david/src/lisp/xuriella/test.xsl")