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
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
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.
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 (declaim (optimize (debug 3) (safety 3) (space 0) (speed 0)))
35 (defmacro define-instruction
(name (args-var env-var
) &body body
)
36 `(setf (get ',name
'xslt-instruction
)
37 (lambda (,args-var
,env-var
)
38 (declare (ignorable ,env-var
))
41 (define-instruction if
(args env
)
42 (destructuring-bind (test then
&optional else
) args
43 (let ((test-thunk (compile-xpath test env
))
44 (then-thunk (compile-instruction then env
))
45 (else-thunk (when else
(compile-instruction else env
))))
48 ((xpath:boolean-value
(funcall test-thunk ctx
))
49 (funcall then-thunk ctx
))
51 (funcall else-thunk ctx
)))))))
53 (define-instruction when
(args env
)
54 (destructuring-bind (test &rest body
) args
55 (compile-instruction `(if ,test
(progn ,@body
)) env
)))
57 (define-instruction unless
(args env
)
58 (destructuring-bind (test &rest body
) args
59 (compile-instruction `(if (:not
,test
) (progn ,@body
)) env
)))
61 (define-instruction cond
(args env
)
63 (destructuring-bind ((test &body body
) &rest clauses
) args
64 (compile-instruction (if (eq test t
)
72 (define-instruction progn
(args env
)
74 (let ((first-thunk (compile-instruction (first args
) env
))
75 (rest-thunk (compile-instruction `(progn ,@(rest args
)) env
)))
77 (funcall first-thunk ctx
)
78 (funcall rest-thunk ctx
)))
81 (defun decode-qname/runtime
(qname namespaces attributep
)
83 (multiple-value-bind (prefix local-name
)
86 (if (or prefix
(not attributep
))
87 (cdr (assoc prefix namespaces
:test
'equal
))
90 (cxml:well-formedness-violation
()
91 (xslt-error "not a qname: ~A" qname
))))
93 (define-instruction xsl
:element
(args env
)
94 (destructuring-bind ((name &key namespace use-attribute-sets
)
97 (declare (ignore use-attribute-sets
)) ;fixme
98 (multiple-value-bind (name-thunk constant-name-p
)
99 (compile-attribute-value-template name env
)
100 (multiple-value-bind (ns-thunk constant-ns-p
)
102 (compile-attribute-value-template namespace env
)
104 (let ((body-thunk (compile-instruction `(progn ,@body
) env
)))
105 (if (and constant-name-p constant-ns-p
)
106 (compile-element/constant-name name namespace env body-thunk
)
107 (compile-element/runtime name-thunk ns-thunk body-thunk
)))))))
109 (defun compile-element/constant-name
(qname namespace env body-thunk
)
110 ;; the simple case: compile-time decoding of the QName
111 (multiple-value-bind (local-name uri prefix
)
112 (decode-qname qname env nil
)
114 (setf uri namespace
))
116 (with-element (local-name uri
:suggested-prefix prefix
)
117 (funcall body-thunk ctx
)))))
119 (defun compile-element/runtime
(name-thunk ns-thunk body-thunk
)
120 ;; run-time decoding of the QName, but using the same namespaces
121 ;; that would have been known at compilation time.
122 (let ((namespaces *namespaces
*))
124 (let ((qname (funcall name-thunk ctx
)))
125 (multiple-value-bind (local-name uri prefix
)
126 (decode-qname/runtime qname namespaces nil
)
128 (setf uri
(funcall ns-thunk ctx
)))
131 (with-element (local-name uri
:suggested-prefix prefix
)
132 (funcall body-thunk ctx
)))))))
134 (define-instruction xsl
:use-attribute-sets
(args env
)
135 (destructuring-bind (str) args
136 (let ((sets (mapcar (lambda (qname)
137 (multiple-value-list (decode-qname qname env nil
)))
140 (loop for
(local-name uri nil
) in sets do
141 (dolist (thunk (find-attribute-set local-name uri
))
142 (funcall thunk ctx
)))))))
144 (define-instruction xsl
:attribute
(args env
)
145 (destructuring-bind ((name &key namespace
) &body body
) args
146 (multiple-value-bind (name-thunk constant-name-p
)
147 (compile-attribute-value-template name env
)
148 (multiple-value-bind (ns-thunk constant-ns-p
)
150 (compile-attribute-value-template namespace env
)
152 (let ((value-thunk (compile-instruction `(progn ,@body
) env
)))
153 (if (and constant-name-p constant-ns-p
)
154 (compile-attribute/constant-name name namespace env value-thunk
)
155 (compile-attribute/runtime name-thunk ns-thunk value-thunk
)))))))
157 (defun compile-attribute/constant-name
(qname namespace env value-thunk
)
158 ;; the simple case: compile-time decoding of the QName
159 (multiple-value-bind (local-name uri prefix
)
160 (decode-qname qname env nil
)
162 (setf uri namespace
))
164 (write-attribute local-name
166 (with-text-output-sink (s)
168 (funcall value-thunk ctx
)))
169 :suggested-prefix prefix
))))
171 (defun compile-attribute/runtime
(name-thunk ns-thunk value-thunk
)
172 ;; run-time decoding of the QName, but using the same namespaces
173 ;; that would have been known at compilation time.
174 (let ((namespaces *namespaces
*))
176 (let ((qname (funcall name-thunk ctx
)))
177 (multiple-value-bind (local-name uri prefix
)
178 (decode-qname/runtime qname namespaces nil
)
180 (setf uri
(funcall ns-thunk ctx
)))
181 (write-attribute local-name
183 (with-text-output-sink (s)
185 (funcall value-thunk ctx
)))
186 :suggested-prefix prefix
))))))
188 (defun remove-excluded-namespaces
189 (namespaces &optional
(excluded-uris *excluded-namespaces
*))
190 (let ((koerbchen '())
193 for cons in namespaces
194 for
(prefix . uri
) = cons
197 ((find prefix kroepfchen
:test
#'equal
))
198 ((find uri excluded-uris
:test
#'equal
)
199 (push prefix kroepfchen
))
201 (push cons koerbchen
))))
204 (define-instruction xsl
:literal-element
(args env
)
206 ((local-name &optional
(uri "") suggested-prefix
) &body body
)
208 (let ((body-thunk (compile-instruction `(progn ,@body
) env
))
209 (namespaces (remove-excluded-namespaces *namespaces
*)))
211 (with-element (local-name uri
212 :suggested-prefix suggested-prefix
213 :extra-namespaces namespaces
)
214 (funcall body-thunk ctx
))))))
216 (define-instruction xsl
:literal-attribute
(args env
)
217 (destructuring-bind ((local-name &optional uri suggested-prefix
) value
) args
218 (let ((value-thunk (compile-attribute-value-template value env
)))
220 (write-attribute local-name
222 (funcall value-thunk ctx
)
223 :suggested-prefix suggested-prefix
)))))
225 (define-instruction xsl
:text
(args env
)
226 (destructuring-bind (str) args
228 (declare (ignore ctx
))
231 (define-instruction xsl
:processing-instruction
(args env
)
232 (destructuring-bind (name &rest body
) args
233 (let ((name-thunk (compile-attribute-value-template name env
))
234 (value-thunk (compile-instruction `(progn ,@body
) env
)))
236 (write-processing-instruction
237 (funcall name-thunk ctx
)
238 (with-text-output-sink (s)
240 (funcall value-thunk ctx
))))))))
242 (define-instruction xsl
:comment
(args env
)
243 (destructuring-bind (str) args
245 (declare (ignore ctx
))
246 (write-comment str
))))
248 (define-instruction xsl
:value-of
(args env
)
249 (destructuring-bind (xpath) args
250 (let ((thunk (compile-xpath xpath env
)))
253 (write-text (xpath:string-value
(funcall thunk ctx
))))
254 "value-of ~s = ~s" xpath
:result
))))
256 (define-instruction xsl
:unescaped-value-of
(args env
)
257 (destructuring-bind (xpath) args
258 (let ((thunk (compile-xpath xpath env
)))
260 (write-unescaped (xpath:string-value
(funcall thunk ctx
)))))))
262 (define-instruction xsl
:copy-of
(args env
)
263 (destructuring-bind (xpath) args
264 (let ((thunk (compile-xpath xpath env
))
265 ;; FIXME: what was this for? --david
266 #+(or) (v (intern-variable "varName" "")))
268 (let ((result (funcall thunk ctx
)))
270 (xpath:node-set
;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
271 (xpath:map-node-set
#'copy-into-result result
))
272 (result-tree-fragment
273 (copy-into-result result
))
275 (write-text (xpath:string-value result
)))))))))
277 (defun copy-into-result (node)
279 ((result-tree-fragment-p node
)
280 (stp:do-children
(child (result-tree-fragment-node node
))
281 (copy-into-result child
)))
282 ((xpath-protocol:node-type-p node
:element
)
283 (with-element ((xpath-protocol:local-name node
)
284 (xpath-protocol:namespace-uri node
)
285 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
286 ;; FIXME: is remove-excluded-namespaces correct here?
287 :extra-namespaces
(remove-excluded-namespaces
288 (namespaces-as-alist node
)))
289 (map-pipe-eagerly #'copy-into-result
290 (xpath-protocol:attribute-pipe node
))
291 (map-pipe-eagerly #'copy-into-result
292 (xpath-protocol:child-pipe node
))))
293 ((xpath-protocol:node-type-p node
:document
)
294 (map-pipe-eagerly #'copy-into-result
295 (xpath-protocol:child-pipe node
)))
297 (copy-leaf-node node
))))
299 (defun make-sorter (spec env
)
300 (destructuring-bind (&key select lang data-type order case-order
)
302 ;; FIXME: implement case-order
303 (declare (ignore lang case-order
))
304 (let ((select-thunk (compile-xpath (or select
".") env
))
305 (numberp (equal data-type
"number"))
306 (f (if (equal order
"descending") -
1 1)))
308 (let ((i (xpath:string-value
309 (funcall select-thunk
(xpath:make-context a
))))
310 (j (xpath:string-value
311 (funcall select-thunk
(xpath:make-context b
)))))
314 (signum (- (xpath:number-value i
) (xpath:number-value j
)))
320 (defun compose-sorters (sorters)
322 (let ((this (car sorters
))
323 (next (compose-sorters (rest sorters
))))
325 (let ((d (funcall this a b
)))
331 (defun make-sort-predicate (decls env
)
334 (mapcar (lambda (x) (make-sorter x env
)) decls
))))
336 (minusp (funcall sorter a b
)))))
338 (define-instruction xsl
:for-each
(args env
)
339 (destructuring-bind (select &optional decls
&rest body
) args
340 (unless (and (consp decls
)
341 (eq (car decls
) 'declare
))
344 (let ((select-thunk (compile-xpath select env
))
345 (body-thunk (compile-instruction `(progn ,@body
) env
))
348 (make-sort-predicate (cdr decls
) env
))))
350 (let ((selected (funcall select-thunk ctx
)))
351 (unless (xpath:node-set-p selected
)
352 (xslt-error "for-each select expression should yield a node-set"))
353 (let ((nodes (xpath::force
354 (xpath::sorted-pipe-of selected
))))
356 (setf nodes
(sort (copy-list nodes
) sort-predicate
)))
358 with n
= (length nodes
)
363 (xpath:make-context node
(lambda () n
) i
)))))))))
365 (define-instruction xsl
:with-namespaces
(args env
)
366 (destructuring-bind ((&rest forms
) &rest body
) args
367 (let ((*namespaces
* *namespaces
*))
369 (destructuring-bind (prefix uri
) form
370 (push (cons prefix uri
) *namespaces
*)))
371 (compile-instruction `(progn ,@body
) env
))))
373 (define-instruction xsl
:with-excluded-namespaces
(args env
)
374 (destructuring-bind ((&rest uris
) &rest body
) args
375 (let ((*excluded-namespaces
* (append uris
*excluded-namespaces
*)))
376 (compile-instruction `(progn ,@body
) env
))))
378 ;; XSLT disallows multiple definitions of the same variable within a
379 ;; template. Local variables can shadow global variables though.
380 ;; Since our LET syntax makes it natural to shadow local variables the
381 ;; Lisp way, we check for duplicate variables only where instructed to
382 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
383 (defvar *template-variables
* nil
)
385 (define-instruction xsl
:with-duplicates-check
(args env
)
386 (let ((*template-variables
* *template-variables
*))
387 (destructuring-bind ((&rest qnames
) &rest body
) args
388 (dolist (qname qnames
)
389 (multiple-value-bind (local-name uri
)
390 (decode-qname qname env nil
)
391 (let ((key (cons local-name uri
)))
392 (when (find key
*template-variables
* :test
#'equal
)
393 (xslt-error "duplicate variable: ~A, ~A" local-name uri
))
394 (push key
*template-variables
*))))
395 (compile-instruction `(progn ,@body
) env
))))
397 (define-instruction xsl
:with-base-uri
(args env
)
398 (destructuring-bind (uri &rest body
) args
399 (let ((*instruction-base-uri
* uri
))
400 (compile-instruction `(progn ,@body
) env
))))
402 (defstruct (result-tree-fragment
403 (:constructor make-result-tree-fragment
(node)))
406 (defmethod xpath-protocol:node-p
((node result-tree-fragment
))
409 (defmethod xpath-protocol:string-value
((node result-tree-fragment
))
410 (xpath-protocol:string-value
(result-tree-fragment-node node
)))
412 (defun apply-to-result-tree-fragment (ctx thunk
)
414 (with-xml-output (stp:make-builder
)
415 (with-element ("fragment" "")
416 (funcall thunk ctx
)))))
417 (make-result-tree-fragment (stp:document-element document
))))
419 (define-instruction let
(args env
)
420 (destructuring-bind ((&rest forms
) &rest body
) args
421 (let* ((old-top (length *lexical-variable-declarations
*))
422 (vars-and-names (compile-var-bindings/nointern forms env
))
424 (loop for
((local-name . uri
) thunk
) in vars-and-names
426 (list (push-variable local-name
428 *lexical-variable-declarations
*)
430 (let ((thunk (compile-instruction `(progn ,@body
) env
)))
431 (fill *lexical-variable-declarations
* nil
:start old-top
)
433 (loop for
(index var-thunk
) in vars-and-positions
434 do
(setf (lexical-variable-value index
)
435 (funcall var-thunk ctx
)))
436 (funcall thunk ctx
))))))
438 (define-instruction let
* (args env
)
439 (destructuring-bind ((&rest forms
) &rest body
) args
441 (compile-instruction `(let (,(car forms
))
442 (let* (,@(cdr forms
))
445 (compile-instruction `(progn ,@body
) env
))))
447 (define-instruction xsl
:message
(args env
)
448 (compile-message #'warn args env
))
450 (define-instruction xsl
:terminate
(args env
)
451 (compile-message #'error args env
))
453 (defun namespaces-as-alist (element)
454 (let ((namespaces '()))
455 (do-pipe (ns (xpath-protocol:namespace-pipe element
))
456 (push (cons (xpath-protocol:local-name ns
)
457 (xpath-protocol:namespace-uri ns
))
461 (define-instruction xsl
:copy
(args env
)
462 (let ((body (compile-instruction `(progn ,@args
) env
)))
464 (let ((node (xpath:context-node ctx
)))
466 ((xpath-protocol:node-type-p node
:element
)
468 ((xpath-protocol:local-name node
)
469 (xpath-protocol:namespace-uri node
)
470 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
471 :extra-namespaces
(namespaces-as-alist node
))
473 ((xpath-protocol:node-type-p node
:document
)
476 (copy-leaf-node node
)))))))
478 (defun copy-leaf-node (node)
480 ((xpath-protocol:node-type-p node
:text
)
481 (write-text (xpath-protocol:string-value node
)))
482 ((xpath-protocol:node-type-p node
:comment
)
483 (write-comment (xpath-protocol:string-value node
)))
484 ((xpath-protocol:node-type-p node
:processing-instruction
)
485 (write-processing-instruction
486 (xpath-protocol:processing-instruction-target node
)
487 (xpath-protocol:string-value node
)))
488 ((xpath-protocol:node-type-p node
:attribute
)
490 (xpath-protocol:local-name node
)
491 (xpath-protocol:namespace-uri node
)
492 (xpath-protocol:string-value node
)
493 :suggested-prefix
(xpath-protocol:namespace-prefix node
)))
495 (error "don't know how to copy node ~A" node
))))
497 (defun compile-message (fn args env
)
498 (let ((thunk (compile-instruction `(progn ,@args
) env
)))
501 (with-xml-output (cxml:make-string-sink
)
502 (funcall thunk ctx
))))))
504 (define-instruction xsl
:apply-templates
(args env
)
505 (destructuring-bind ((&key select mode
) &rest param-binding-specs
) args
507 (when (and (consp (car param-binding-specs
))
508 (eq (caar param-binding-specs
) 'declare
))
509 (cdr (pop param-binding-specs
))))
511 (compile-xpath (or select
"child::node()") env
))
513 (compile-var-bindings param-binding-specs env
))
516 (make-sort-predicate decls env
))))
517 (multiple-value-bind (mode-local-name mode-uri
)
518 (and mode
(decode-qname mode env nil
))
520 (let ((*mode
* (if mode
521 (or (find-mode *stylesheet
*
526 (apply-templates/list
528 (xpath::sorted-pipe-of
(funcall select-thunk ctx
)))
529 (loop for
(name nil value-thunk
) in param-bindings
530 collect
(list name
(funcall value-thunk ctx
)))
531 sort-predicate
)))))))
533 (define-instruction xsl
:apply-imports
(args env
)
534 (declare (ignore args env
))
536 (declare (ignore ctx
))
537 (funcall *apply-imports
*)))
539 (define-instruction xsl
:call-template
(args env
)
540 (destructuring-bind (name &rest param-binding-specs
) args
541 (let ((param-bindings
542 (compile-var-bindings param-binding-specs env
)))
543 (multiple-value-bind (local-name uri
)
544 (decode-qname name env nil
)
545 (setf name
(cons local-name uri
)))
547 (call-template ctx name
548 (loop for
(name nil value-thunk
) in param-bindings
549 collect
(list name
(funcall value-thunk ctx
))))))))
551 (defun compile-instruction (form env
)
553 (funcall (or (get (car form
) 'xslt-instruction
)
554 (error "undefined instruction: ~A" (car form
)))
557 "instruction ~s" (car form
)))
559 ;;: WTF: "A right curly brace inside a Literal in an expression is not
560 ;;; recognized as terminating the expression."
562 ;;; Da hilft nur tagbody.
563 (defun parse-attribute-value-template (template-string)
564 (with-input-from-string (input template-string
)
565 (let ((ordinary (make-string-output-stream))
566 (xpath (make-string-output-stream))
568 (c (read-char input nil
:eof
)))
570 (let ((o (get-output-stream-string ordinary
)))
571 (when (plusp (length o
))
572 (push (list :data o
) tokens
)))
573 (let ((x (get-output-stream-string xpath
)))
574 (when (plusp (length x
))
575 (push (list :xpath x
) tokens
))))
577 (write-char c ordinary
))
579 (write-char c xpath
)))
580 (macrolet ((goto (target)
582 (setf c
(read-char input nil
:eof
))
603 (goto in-single-quote
))
605 (xslt-error "unexpected end of avt")))
614 (goto in-single-quote
))
617 (goto in-double-quote
))
619 (goto seen-closing-
}))
621 (xslt-error "unexpected end of avt")))
631 (xslt-error "unexpected end of avt")))
633 (goto in-single-quote
)
641 (xslt-error "unexpected end of avt")))
643 (goto in-double-quote
)
664 (xslt-error "unexpected closing brace in avt")
670 (defun compile-attribute-value-template (template-string env
)
676 (constantly (second x
)))
679 (xpath:compile-xpath
(second x
) env
))))
680 (parse-attribute-value-template template-string
))))
681 (values (lambda (ctx)
682 (with-output-to-string (s)
684 (write-string (xpath:string-value
(funcall fn ctx
)) s
))))
688 ;;;; Indentation for slime
690 (defmacro define-indentation
(name (&rest args
))
691 (labels ((collect-variables (list)
697 (collect-variables sub
))
699 (if (eql (mismatch "&" (symbol-name sub
)) 1)
702 `(defmacro ,name
(,@args
)
703 (declare (ignorable ,@(collect-variables args
)))
704 (error "XSL indentation helper ~A used literally in lisp code"
707 (define-indentation xsl
:element
708 ((name &key namespace use-attribute-sets
) &body body
))
709 (define-indentation xsl
:literal-element
((name &optional uri
) &body body
))
710 (define-indentation xsl
:attribute
((name &key namespace
) &body body
))
711 (define-indentation xsl
:literal-attribute
((name &optional uri
) &body body
))
712 (define-indentation xsl
:text
(str))
713 (define-indentation xsl
:processing-instruction
(name &body body
))
714 (define-indentation xsl
:comment
(str))
715 (define-indentation xsl
:value-of
(xpath))
716 (define-indentation xsl
:unescaped-value-of
(xpath))
717 (define-indentation xsl
:for-each
(select &body decls-and-body
))
718 (define-indentation xsl
:message
(&body body
))
719 (define-indentation xsl
:terminate
(&body body
))
720 (define-indentation xsl
:apply-templates
((&key select mode
) &body decls-and-body
))
721 (define-indentation xsl
:call-template
(name &rest parameters
))
722 (define-indentation xsl
:copy-of
(xpath))
726 (defun test-instruction (form document
)
727 (let ((thunk (compile-instruction form
(make-instance 'lexical-environment
)))
728 (root (cxml:parse document
(stp:make-builder
))))
729 (with-xml-output (cxml:make-string-sink
)
730 (funcall thunk
(xpath:make-context root
)))))