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
)))
132 (with-element (local-name uri
:suggested-prefix prefix
)
133 (funcall body-thunk ctx
))))))))
135 (define-instruction xsl
:use-attribute-sets
(args env
)
136 (destructuring-bind (str) args
137 (let ((sets (mapcar (lambda (qname)
138 (multiple-value-list (decode-qname qname env nil
)))
141 (loop for
(local-name uri nil
) in sets do
142 (dolist (thunk (find-attribute-set local-name uri
))
143 (funcall thunk ctx
)))))))
145 (define-instruction xsl
:attribute
(args env
)
146 (destructuring-bind ((name &key namespace
) &body body
) args
147 (multiple-value-bind (name-thunk constant-name-p
)
148 (compile-attribute-value-template name env
)
149 (multiple-value-bind (ns-thunk constant-ns-p
)
151 (compile-attribute-value-template namespace env
)
153 (let ((value-thunk (compile-instruction `(progn ,@body
) env
)))
154 (if (and constant-name-p constant-ns-p
)
155 (compile-attribute/constant-name name namespace env value-thunk
)
156 (compile-attribute/runtime name-thunk ns-thunk value-thunk
)))))))
158 (defun compile-attribute/constant-name
(qname namespace env value-thunk
)
159 ;; the simple case: compile-time decoding of the QName
160 (multiple-value-bind (local-name uri prefix
)
161 (decode-qname qname env nil
)
163 (setf uri namespace
))
165 (write-attribute local-name
167 (with-text-output-sink (s)
169 (funcall value-thunk ctx
)))
170 :suggested-prefix prefix
))))
172 (defun compile-attribute/runtime
(name-thunk ns-thunk value-thunk
)
173 ;; run-time decoding of the QName, but using the same namespaces
174 ;; that would have been known at compilation time.
175 (let ((namespaces *namespaces
*))
177 (let ((qname (funcall name-thunk ctx
)))
178 (multiple-value-bind (local-name uri prefix
)
179 (decode-qname/runtime qname namespaces nil
)
181 (setf uri
(funcall ns-thunk ctx
)))
182 (write-attribute local-name
184 (with-text-output-sink (s)
186 (funcall value-thunk ctx
)))
187 :suggested-prefix prefix
))))))
189 (defun remove-excluded-namespaces
190 (namespaces &optional
(excluded-uris *excluded-namespaces
*))
191 (let ((koerbchen '())
194 for cons in namespaces
195 for
(prefix . uri
) = cons
198 ((find prefix kroepfchen
:test
#'equal
))
199 ((find uri excluded-uris
:test
#'equal
)
200 (push prefix kroepfchen
))
202 (push cons koerbchen
))))
205 (define-instruction xsl
:literal-element
(args env
)
207 ((local-name &optional
(uri "") suggested-prefix
) &body body
)
209 (let ((body-thunk (compile-instruction `(progn ,@body
) env
))
210 (namespaces (remove-excluded-namespaces *namespaces
*)))
212 (with-element (local-name uri
213 :suggested-prefix suggested-prefix
214 :extra-namespaces namespaces
)
215 (funcall body-thunk ctx
))))))
217 (define-instruction xsl
:literal-attribute
(args env
)
218 (destructuring-bind ((local-name &optional uri suggested-prefix
) value
) args
219 (let ((value-thunk (compile-attribute-value-template value env
)))
221 (write-attribute local-name
223 (funcall value-thunk ctx
)
224 :suggested-prefix suggested-prefix
)))))
226 (define-instruction xsl
:text
(args env
)
227 (destructuring-bind (str) args
229 (declare (ignore ctx
))
232 (define-instruction xsl
:processing-instruction
(args env
)
233 (destructuring-bind (name &rest body
) args
234 (let ((name-thunk (compile-attribute-value-template name env
))
235 (value-thunk (compile-instruction `(progn ,@body
) env
)))
237 (write-processing-instruction
238 (funcall name-thunk ctx
)
239 (with-text-output-sink (s)
241 (funcall value-thunk ctx
))))))))
243 (define-instruction xsl
:comment
(args env
)
244 (destructuring-bind (str) args
246 (declare (ignore ctx
))
247 (write-comment str
))))
249 (define-instruction xsl
:value-of
(args env
)
250 (destructuring-bind (xpath) args
251 (let ((thunk (compile-xpath xpath env
)))
253 (write-text (xpath:string-value
(funcall thunk ctx
)))))))
255 (define-instruction xsl
:unescaped-value-of
(args env
)
256 (destructuring-bind (xpath) args
257 (let ((thunk (compile-xpath xpath env
)))
259 (write-unescaped (xpath:string-value
(funcall thunk ctx
)))))))
261 (define-instruction xsl
:copy-of
(args env
)
262 (destructuring-bind (xpath) args
263 (let ((thunk (compile-xpath xpath env
))
264 ;; FIXME: what was this for? --david
265 #+(or) (v (intern-variable "varName" "")))
267 (let ((result (funcall thunk ctx
)))
269 (xpath:node-set
;; FIXME: variables can contain node sets w/fragments inside. Maybe just fragments would do?
270 (xpath:map-node-set
#'copy-into-result result
))
271 (result-tree-fragment
272 (copy-into-result result
))
274 (write-text (xpath:string-value result
)))))))))
276 (defun copy-into-result (node)
278 ((result-tree-fragment-p node
)
279 (stp:do-children
(child (result-tree-fragment-node node
))
280 (copy-into-result child
)))
281 ((xpath-protocol:node-type-p node
:element
)
282 (with-element ((xpath-protocol:local-name node
)
283 (xpath-protocol:namespace-uri node
)
284 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
285 ;; FIXME: is remove-excluded-namespaces correct here?
286 :extra-namespaces
(remove-excluded-namespaces
287 (namespaces-as-alist node
)))
288 (map-pipe-eagerly #'copy-into-result
289 (xpath-protocol:attribute-pipe node
))
290 (map-pipe-eagerly #'copy-into-result
291 (xpath-protocol:child-pipe node
))))
292 ((xpath-protocol:node-type-p node
:document
)
293 (map-pipe-eagerly #'copy-into-result
294 (xpath-protocol:child-pipe node
)))
296 (copy-leaf-node node
))))
298 (defun make-sorter (spec env
)
299 (destructuring-bind (&key select lang data-type order case-order
)
301 ;; FIXME: implement case-order
302 (declare (ignore lang case-order
))
303 (let ((select-thunk (compile-xpath (or select
".") env
))
304 (numberp (equal data-type
"number"))
305 (f (if (equal order
"descending") -
1 1)))
307 (let ((i (xpath:string-value
308 (funcall select-thunk
(xpath:make-context a
))))
309 (j (xpath:string-value
310 (funcall select-thunk
(xpath:make-context b
)))))
313 (signum (- (xpath:number-value i
) (xpath:number-value j
)))
319 (defun compose-sorters (sorters)
321 (let ((this (car sorters
))
322 (next (compose-sorters (rest sorters
))))
324 (let ((d (funcall this a b
)))
330 (defun make-sort-predicate (decls env
)
333 (mapcar (lambda (x) (make-sorter x env
)) decls
))))
335 (minusp (funcall sorter a b
)))))
337 (define-instruction xsl
:for-each
(args env
)
338 (destructuring-bind (select &optional decls
&rest body
) args
339 (unless (and (consp decls
)
340 (eq (car decls
) 'declare
))
343 (let ((select-thunk (compile-xpath select env
))
344 (body-thunk (compile-instruction `(progn ,@body
) env
))
347 (make-sort-predicate (cdr decls
) env
))))
349 (let* ((nodes (xpath:all-nodes
(funcall select-thunk ctx
)))
352 (setf nodes
(sort nodes sort-predicate
))
353 (setf nodes
(sort nodes
#'< :key
#'document-order
)))
359 (xpath:make-context node
(lambda () n
) i
))))))))
361 (define-instruction xsl
:with-namespaces
(args env
)
362 (destructuring-bind ((&rest forms
) &rest body
) args
363 (let ((*namespaces
* *namespaces
*))
365 (destructuring-bind (prefix uri
) form
366 (push (cons prefix uri
) *namespaces
*)))
367 (compile-instruction `(progn ,@body
) env
))))
369 (define-instruction xsl
:with-excluded-namespaces
(args env
)
370 (destructuring-bind ((&rest uris
) &rest body
) args
371 (let ((*excluded-namespaces
* (append uris
*excluded-namespaces
*)))
372 (compile-instruction `(progn ,@body
) env
))))
374 ;; XSLT disallows multiple definitions of the same variable within a
375 ;; template. Local variables can shadow global variables though.
376 ;; Since our LET syntax makes it natural to shadow local variables the
377 ;; Lisp way, we check for duplicate variables only where instructed to
378 ;; by the XML syntax parser using WITH-DUPLICATES-CHECK:
379 (defvar *template-variables
* nil
)
381 (define-instruction xsl
:with-duplicates-check
(args env
)
382 (let ((*template-variables
* *template-variables
*))
383 (destructuring-bind ((&rest qnames
) &rest body
) args
384 (dolist (qname qnames
)
385 (multiple-value-bind (local-name uri
)
386 (decode-qname qname env nil
)
387 (let ((key (cons local-name uri
)))
388 (when (find key
*template-variables
* :test
#'equal
)
389 (xslt-error "duplicate variable: ~A, ~A" local-name uri
))
390 (push key
*template-variables
*))))
391 (compile-instruction `(progn ,@body
) env
))))
393 (define-instruction xsl
:with-base-uri
(args env
)
394 (destructuring-bind (uri &rest body
) args
395 (let ((*instruction-base-uri
* uri
))
396 (compile-instruction `(progn ,@body
) env
))))
398 (defstruct (result-tree-fragment
399 (:constructor make-result-tree-fragment
(node)))
402 (defmethod xpath-protocol:node-p
((node result-tree-fragment
))
405 (defmethod xpath-protocol:string-value
((node result-tree-fragment
))
406 (xpath-protocol:string-value
(result-tree-fragment-node node
)))
408 (defun apply-to-result-tree-fragment (ctx thunk
)
410 (with-xml-output (stp:make-builder
)
411 (with-element ("fragment" "")
412 (funcall thunk ctx
)))))
413 (make-result-tree-fragment (stp:document-element document
))))
415 (define-instruction let
(args env
)
416 (destructuring-bind ((&rest forms
) &rest body
) args
417 (let* ((old-top (length *lexical-variable-declarations
*))
418 (vars-and-names (compile-var-bindings/nointern forms env
))
420 (loop for
((local-name . uri
) thunk
) in vars-and-names
422 (list (push-variable local-name
424 *lexical-variable-declarations
*)
426 (let ((thunk (compile-instruction `(progn ,@body
) env
)))
427 (fill *lexical-variable-declarations
* nil
:start old-top
)
429 (loop for
(index var-thunk
) in vars-and-positions
430 do
(setf (lexical-variable-value index
)
431 (funcall var-thunk ctx
)))
432 (funcall thunk ctx
))))))
434 (define-instruction let
* (args env
)
435 (destructuring-bind ((&rest forms
) &rest body
) args
437 (compile-instruction `(let (,(car forms
))
438 (let* (,@(cdr forms
))
441 (compile-instruction `(progn ,@body
) env
))))
443 (define-instruction xsl
:message
(args env
)
444 (compile-message #'warn args env
))
446 (define-instruction xsl
:terminate
(args env
)
447 (compile-message #'error args env
))
449 (defun namespaces-as-alist (element)
450 (let ((namespaces '()))
451 (do-pipe (ns (xpath-protocol:namespace-pipe element
))
452 (push (cons (xpath-protocol:local-name ns
)
453 (xpath-protocol:namespace-uri ns
))
457 (define-instruction xsl
:copy
(args env
)
458 (let ((body (compile-instruction `(progn ,@args
) env
)))
460 (let ((node (xpath:context-node ctx
)))
462 ((xpath-protocol:node-type-p node
:element
)
464 ((xpath-protocol:local-name node
)
465 (xpath-protocol:namespace-uri node
)
466 :suggested-prefix
(xpath-protocol:namespace-prefix node
)
467 :extra-namespaces
(namespaces-as-alist node
))
469 ((xpath-protocol:node-type-p node
:document
)
472 (copy-leaf-node node
)))))))
474 (defun copy-leaf-node (node)
476 ((xpath-protocol:node-type-p node
:text
)
477 (write-text (xpath-protocol:string-value node
)))
478 ((xpath-protocol:node-type-p node
:comment
)
479 (write-comment (xpath-protocol:string-value node
)))
480 ((xpath-protocol:node-type-p node
:processing-instruction
)
481 (write-processing-instruction
482 (xpath-protocol:processing-instruction-target node
)
483 (xpath-protocol:string-value node
)))
484 ((xpath-protocol:node-type-p node
:attribute
)
486 (xpath-protocol:local-name node
)
487 (xpath-protocol:namespace-uri node
)
488 (xpath-protocol:string-value node
)
489 :suggested-prefix
(xpath-protocol:namespace-prefix node
)))
491 (error "don't know how to copy node ~A" node
))))
493 (defun compile-message (fn args env
)
494 (let ((thunk (compile-instruction `(progn ,@args
) env
)))
497 (with-xml-output (cxml:make-string-sink
)
498 (funcall thunk ctx
))))))
500 (define-instruction xsl
:apply-templates
(args env
)
501 (destructuring-bind ((&key select mode
) &rest param-binding-specs
) args
503 (when (and (consp (car param-binding-specs
))
504 (eq (caar param-binding-specs
) 'declare
))
505 (cdr (pop param-binding-specs
))))
507 (compile-xpath (or select
"child::node()") env
))
509 (compile-var-bindings param-binding-specs env
))
512 (make-sort-predicate decls env
))))
513 (multiple-value-bind (mode-local-name mode-uri
)
514 (and mode
(decode-qname mode env nil
))
516 (let ((*mode
* (if mode
517 (or (find-mode *stylesheet
*
522 (nodes (xpath:all-nodes
(funcall select-thunk ctx
))))
523 (setf nodes
(sort nodes
#'< :key
#'document-order
))
524 (apply-templates/list
526 (loop for
(name nil value-thunk
) in param-bindings
527 collect
(list name
(funcall value-thunk ctx
)))
528 sort-predicate
)))))))
530 (define-instruction xsl
:apply-imports
(args env
)
532 (declare (ignore ctx
))
533 (funcall *apply-imports
*)))
535 (define-instruction xsl
:call-template
(args env
)
536 (destructuring-bind (name &rest param-binding-specs
) args
537 (let ((param-bindings
538 (compile-var-bindings param-binding-specs env
)))
539 (multiple-value-bind (local-name uri
)
540 (decode-qname name env nil
)
541 (setf name
(cons local-name uri
)))
543 (call-template ctx name
544 (loop for
(name nil value-thunk
) in param-bindings
545 collect
(list name
(funcall value-thunk ctx
))))))))
547 (defun compile-instruction (form env
)
548 (funcall (or (get (car form
) 'xslt-instruction
)
549 (error "undefined instruction: ~A" (car form
)))
553 ;;: WTF: "A right curly brace inside a Literal in an expression is not
554 ;;; recognized as terminating the expression."
556 ;;; Da hilft nur tagbody.
557 (defun parse-attribute-value-template (template-string)
558 (with-input-from-string (input template-string
)
559 (let ((ordinary (make-string-output-stream))
560 (xpath (make-string-output-stream))
562 (c (read-char input nil
:eof
)))
564 (let ((o (get-output-stream-string ordinary
)))
565 (when (plusp (length o
))
566 (push (list :data o
) tokens
)))
567 (let ((x (get-output-stream-string xpath
)))
568 (when (plusp (length x
))
569 (push (list :xpath x
) tokens
))))
571 (write-char c ordinary
))
573 (write-char c xpath
)))
574 (macrolet ((goto (target)
576 (setf c
(read-char input nil
:eof
))
597 (goto in-single-quote
))
599 (xslt-error "unexpected end of avt")))
608 (goto in-single-quote
))
611 (goto in-double-quote
))
613 (goto seen-closing-
}))
615 (xslt-error "unexpected end of avt")))
625 (xslt-error "unexpected end of avt")))
627 (goto in-single-quote
)
635 (xslt-error "unexpected end of avt")))
637 (goto in-double-quote
)
658 (xslt-error "unexpected closing brace in avt")
664 (defun compile-attribute-value-template (template-string env
)
670 (constantly (second x
)))
673 (xpath:compile-xpath
(second x
) env
))))
674 (parse-attribute-value-template template-string
))))
675 (values (lambda (ctx)
676 (with-output-to-string (s)
678 (write-string (xpath:string-value
(funcall fn ctx
)) s
))))
682 ;;;; Indentation for slime
684 (defmacro define-indentation
(name (&rest args
))
685 (labels ((collect-variables (list)
691 (collect-variables sub
))
693 (if (eql (mismatch "&" (symbol-name sub
)) 1)
696 `(defmacro ,name
(,@args
)
697 (declare (ignorable ,@(collect-variables args
)))
698 (error "XSL indentation helper ~A used literally in lisp code"
701 (define-indentation xsl
:element
702 ((name &key namespace use-attribute-sets
) &body body
))
703 (define-indentation xsl
:literal-element
((name &optional uri
) &body body
))
704 (define-indentation xsl
:attribute
((name &key namespace
) &body body
))
705 (define-indentation xsl
:literal-attribute
((name &optional uri
) &body body
))
706 (define-indentation xsl
:text
(str))
707 (define-indentation xsl
:processing-instruction
(name &body body
))
708 (define-indentation xsl
:comment
(str))
709 (define-indentation xsl
:value-of
(xpath))
710 (define-indentation xsl
:unescaped-value-of
(xpath))
711 (define-indentation xsl
:for-each
(select &body decls-and-body
))
712 (define-indentation xsl
:message
(&body body
))
713 (define-indentation xsl
:terminate
(&body body
))
714 (define-indentation xsl
:apply-templates
((&key select mode
) &body decls-and-body
))
715 (define-indentation xsl
:call-template
(name &rest parameters
))
716 (define-indentation xsl
:copy-of
(xpath))
720 (defun test-instruction (form document
)
721 (let ((thunk (compile-instruction form
(make-instance 'lexical-environment
)))
722 (root (cxml:parse document
(stp:make-builder
))))
723 (with-xml-output (cxml:make-string-sink
)
724 (funcall thunk
(xpath:make-context root
)))))