1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: 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
)
33 (declaim (optimize (debug 2)))
36 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
37 (defvar *xsl
* "http://www.w3.org/1999/XSL/Transform")
38 (defvar *xml
* "http://www.w3.org/XML/1998/namespace")
39 (defvar *html
* "http://www.w3.org/1999/xhtml"))
44 (define-condition xslt-error
(simple-error)
46 (:documentation
"The class of all XSLT errors."))
48 (define-condition recoverable-xslt-error
(xslt-error)
50 (:documentation
"The class of recoverable XSLT errors."))
52 (defun xslt-error (fmt &rest args
)
53 (error 'xslt-error
:format-control fmt
:format-arguments args
))
55 (defun xslt-cerror (fmt &rest args
)
56 (with-simple-restart (recover "recover")
57 (error 'recoverable-xslt-error
59 :format-arguments args
)))
63 (defmacro handler-case
* (form &rest clauses
)
64 ;; like HANDLER-CASE if *DEBUG* is off. If it's on, don't establish
65 ;; a handler at all so that we see the real stack traces. (We could use
66 ;; HANDLER-BIND here and check at signalling time, but doesn't seem
68 (let ((doit (gensym)))
69 `(flet ((,doit
() ,form
))
76 (defun compile-xpath (xpath &optional env
)
78 (xpath:compile-xpath xpath env
)
79 (xpath:xpath-error
(c)
80 (xslt-error "~A" c
))))
82 (defmacro with-stack-limit
((&optional
) &body body
)
83 `(invoke-with-stack-limit (lambda () ,@body
)))
86 ;;;; Helper function and macro
88 (defun map-pipe-eagerly (fn pipe
)
89 (xpath::enumerate pipe
:key fn
:result nil
))
91 (defmacro do-pipe
((var pipe
&optional result
) &body body
)
93 (map-pipe-eagerly #'(lambda (,var
) ,@body
) ,pipe
)
97 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
99 (defparameter *namespaces
* *initial-namespaces
*)
101 (defvar *global-variable-declarations
*)
102 (defvar *lexical-variable-declarations
*)
104 (defvar *global-variable-values
*)
105 (defvar *lexical-variable-values
*)
107 (defclass xslt-environment
() ())
109 (defun split-qname (str)
111 (multiple-value-bind (prefix local-name
)
112 (cxml::split-qname str
)
114 ;; FIXME: cxml should really offer a function that does
115 ;; checks for NCName and QName in a sensible way for user code.
116 ;; cxml::split-qname is tailored to the needs of the parser.
118 ;; For now, let's just check the syntax explicitly.
119 (and (or (null prefix
) (xpath::nc-name-p prefix
))
120 (xpath::nc-name-p local-name
))
121 (xslt-error "not a qname: ~A" str
))
122 (values prefix local-name
))
123 (cxml:well-formedness-violation
()
124 (xslt-error "not a qname: ~A" str
))))
126 (defun decode-qname (qname env attributep
)
127 (multiple-value-bind (prefix local-name
)
130 (if (or prefix
(not attributep
))
131 (xpath-sys:environment-find-namespace env prefix
)
135 (defmethod xpath-sys:environment-find-namespace
((env xslt-environment
) prefix
)
136 (cdr (assoc prefix
*namespaces
* :test
'equal
)))
138 (defun find-variable-index (local-name uri table
)
139 (position (cons local-name uri
) table
:test
'equal
))
141 (defun intern-global-variable (local-name uri
)
142 (or (find-variable-index local-name uri
*global-variable-declarations
*)
143 (push-variable local-name uri
*global-variable-declarations
*)))
145 (defun push-variable (local-name uri table
)
148 (vector-push-extend (cons local-name uri
) table
)))
150 (defun lexical-variable-value (index &optional
(errorp t
))
151 (let ((result (svref *lexical-variable-values
* index
)))
153 (assert (not (eq result
'unbound
))))
156 (defun (setf lexical-variable-value
) (newval index
)
157 (assert (not (eq newval
'unbound
)))
158 (setf (svref *lexical-variable-values
* index
) newval
))
160 (defun global-variable-value (index &optional
(errorp t
))
161 (let ((result (svref *global-variable-values
* index
)))
163 (assert (not (eq result
'unbound
))))
166 (defun (setf global-variable-value
) (newval index
)
167 (assert (not (eq newval
'unbound
)))
168 (setf (svref *global-variable-values
* index
) newval
))
170 (defmethod xpath-sys:environment-find-function
171 ((env xslt-environment
) lname uri
)
173 (or (xpath-sys:find-xpath-function lname
*xsl
*)
174 (xpath-sys:find-xpath-function lname uri
))
175 (xpath-sys:find-xpath-function lname uri
)))
177 (defmethod xpath-sys:environment-find-variable
178 ((env xslt-environment
) lname uri
)
180 (find-variable-index lname uri
*lexical-variable-declarations
*)))
183 (declare (ignore ctx
))
184 (svref *lexical-variable-values
* index
)))))
186 (defclass lexical-xslt-environment
(xslt-environment) ())
188 (defmethod xpath-sys:environment-find-variable
189 ((env lexical-xslt-environment
) lname uri
)
190 (or (call-next-method)
192 (find-variable-index lname uri
*global-variable-declarations
*)))
196 (declare (ignore ctx
))
197 (svref *global-variable-values
* index
))
198 "global ~s (uri ~s) = ~s" lname uri
:result
)))))
200 (defclass global-variable-environment
(xslt-environment)
201 ((initial-global-variable-thunks
202 :initarg
:initial-global-variable-thunks
203 :accessor initial-global-variable-thunks
)))
205 (defmethod xpath-sys:environment-find-variable
206 ((env global-variable-environment
) lname uri
)
207 (or (call-next-method)
208 (gethash (cons lname uri
) (initial-global-variable-thunks env
))))
211 ;;;; TOPLEVEL-TEXT-OUTPUT-SINK
213 ;;;; A sink that serializes only text not contained in any element.
215 (defmacro with-toplevel-text-output-sink
((var) &body body
)
216 `(invoke-with-toplevel-text-output-sink (lambda (,var
) ,@body
)))
218 (defclass toplevel-text-output-sink
(sax:default-handler
)
219 ((target :initarg
:target
:accessor text-output-sink-target
)
220 (depth :initform
0 :accessor textoutput-sink-depth
)))
222 (defmethod sax:start-element
((sink toplevel-text-output-sink
)
223 namespace-uri local-name qname attributes
)
224 (declare (ignore namespace-uri local-name qname attributes
))
225 (incf (textoutput-sink-depth sink
)))
227 (defmethod sax:characters
((sink toplevel-text-output-sink
) data
)
228 (when (zerop (textoutput-sink-depth sink
))
229 (write-string data
(text-output-sink-target sink
))))
231 (defmethod sax:end-element
((sink toplevel-text-output-sink
)
232 namespace-uri local-name qname
)
233 (declare (ignore namespace-uri local-name qname
))
234 (decf (textoutput-sink-depth sink
)))
236 (defun invoke-with-toplevel-text-output-sink (fn)
237 (with-output-to-string (s)
238 (funcall fn
(make-instance 'toplevel-text-output-sink
:target s
))))
243 ;;;; A sink that passes through only text (at any level).
245 (defclass text-filter
(sax:default-handler
)
246 ((target :initarg
:target
:accessor text-filter-target
)))
248 (defmethod sax:characters
((sink text-filter
) data
)
249 (sax:characters
(text-filter-target sink
) data
))
251 (defmethod sax:end-document
((sink text-filter
))
252 (sax:end-document
(text-filter-target sink
)))
254 (defun make-text-filter (target)
255 (make-instance 'text-filter
:target target
))
260 (defun of-name (local-name)
261 (stp:of-name local-name
*xsl
*))
263 (defun namep (node local-name
)
264 (and (typep node
'(or stp
:element stp
:attribute
))
265 (equal (stp:namespace-uri node
) *xsl
*)
266 (equal (stp:local-name node
) local-name
)))
269 ;;;; PARSE-STYLESHEET
271 (defstruct stylesheet
272 (modes (make-hash-table :test
'equal
))
273 (global-variables (make-empty-declaration-array))
274 (output-specification (make-output-specification))
276 (named-templates (make-hash-table :test
'equal
))
277 (attribute-sets (make-hash-table :test
'equal
))
278 (keys (make-hash-table :test
'equal
))
279 (namespace-aliases (make-hash-table :test
'equal
)))
281 (defstruct mode
(templates nil
))
283 (defun find-mode (stylesheet local-name
&optional uri
)
284 (gethash (cons local-name uri
) (stylesheet-modes stylesheet
)))
286 (defun ensure-mode (stylesheet &optional local-name uri
)
287 (or (find-mode stylesheet local-name uri
)
288 (setf (gethash (cons local-name uri
) (stylesheet-modes stylesheet
))
291 (defun ensure-mode/qname
(stylesheet qname env
)
293 (multiple-value-bind (local-name uri
)
294 (decode-qname qname env nil
)
295 (ensure-mode stylesheet local-name uri
))
296 (find-mode stylesheet nil
)))
298 (defun acons-namespaces (element &optional
(bindings *namespaces
*))
299 (map-namespace-declarations (lambda (prefix uri
)
300 (push (cons prefix uri
) bindings
))
304 (defun find-key (name stylesheet
)
305 (or (gethash name
(stylesheet-keys stylesheet
))
306 (xslt-error "unknown key: ~a" name
)))
308 (defun make-key (match use
) (cons match use
))
310 (defun key-match (key) (car key
))
312 (defun key-use (key) (cdr key
))
314 (defun add-key (stylesheet name match use
)
315 (if (gethash name
(stylesheet-keys stylesheet
))
316 (xslt-error "duplicate key: ~a" name
)
317 (setf (gethash name
(stylesheet-keys stylesheet
))
318 (make-key match use
))))
320 (defvar *excluded-namespaces
* (list *xsl
*))
321 (defvar *empty-mode
*)
322 (defvar *default-mode
*)
324 (defvar *xsl-include-stack
* nil
)
326 (defun uri-to-pathname (uri)
327 (cxml::uri-to-pathname
(puri:parse-uri uri
)))
329 (defun parse-stylesheet-to-stp (input uri-resolver
)
330 (let* ((d (cxml:parse input
(make-text-normalizer (cxml-stp:make-builder
))))
331 (<transform
> (stp:document-element d
)))
332 (strip-stylesheet <transform
>)
333 ;; FIXME: handle embedded stylesheets
334 (unless (and (equal (stp:namespace-uri
<transform
>) *xsl
*)
335 (or (equal (stp:local-name
<transform
>) "transform")
336 (equal (stp:local-name
<transform
>) "stylesheet")))
337 (xslt-error "not a stylesheet"))
338 (dolist (include (stp:filter-children
(of-name "include") <transform
>))
339 (let* ((uri (puri:merge-uris
(stp:attribute-value include
"href")
340 (stp:base-uri include
)))
341 (uri (if uri-resolver
342 (funcall uri-resolver
(puri:render-uri uri nil
))
344 (str (puri:render-uri uri nil
))
347 (uri-to-pathname uri
)
348 (cxml:xml-parse-error
(c)
349 (xslt-error "cannot find included stylesheet ~A: ~A"
353 :element-type
'(unsigned-byte 8)
354 :if-does-not-exist nil
)
356 (xslt-error "cannot find included stylesheet ~A at ~A"
358 (when (find str
*xsl-include-stack
* :test
#'equal
)
359 (xslt-error "recursive inclusion of ~A" uri
))
360 (let* ((*xsl-include-stack
* (cons str
*xsl-include-stack
*))
361 (<transform
>2 (parse-stylesheet-to-stp stream uri-resolver
)))
362 (stp:insert-child-after
<transform
>
363 (stp:copy
<transform
>2)
365 (stp:detach include
)))))
368 (defvar *instruction-base-uri
*) ;misnamed, is also used in other attributes
369 (defvar *apply-imports-limit
*)
370 (defvar *import-priority
*)
371 (defvar *extension-namespaces
*)
373 (defmacro do-toplevel
((var xpath
<transform
>) &body body
)
374 `(map-toplevel (lambda (,var
) ,@body
) ,xpath
,<transform
>))
376 (defun map-toplevel (fn xpath
<transform
>)
377 (dolist (node (list-toplevel xpath
<transform
>))
378 (let ((*namespaces
* *namespaces
*))
379 (xpath:do-node-set
(ancestor (xpath:evaluate
"ancestor::node()" node
))
380 (when (xpath-protocol:node-type-p ancestor
:element
)
381 (setf *namespaces
* (acons-namespaces ancestor
))))
384 (defun list-toplevel (xpath <transform
>)
385 (labels ((recurse (sub)
388 (xpath:evaluate
"transform|stylesheet" sub
))))
390 (xpath-sys:pipe-of
(xpath:evaluate xpath sub
))
391 (xpath::mappend-pipe
#'recurse subsubs
)))))
392 (xpath::sort-nodes
(recurse <transform
>))))
394 (defmacro with-parsed-prefixes
((node env
) &body body
)
395 `(invoke-with-parsed-prefixes (lambda () ,@body
) ,node
,env
))
397 (defun invoke-with-parsed-prefixes (fn node env
)
398 (unless (or (namep node
"stylesheet") (namep node
"transform"))
399 (setf node
(stp:parent node
)))
400 (let ((*excluded-namespaces
* (list *xsl
*))
401 (*extension-namespaces
* '()))
402 (parse-exclude-result-prefixes! node env
)
403 (parse-extension-element-prefixes! node env
)
406 (defun parse-1-stylesheet (env stylesheet designator uri-resolver
)
407 (let* ((<transform
> (parse-stylesheet-to-stp designator uri-resolver
))
408 (instruction-base-uri (stp:base-uri
<transform
>))
409 (namespaces (acons-namespaces <transform
>))
410 (apply-imports-limit (1+ *import-priority
*))
412 (let ((*namespaces
* namespaces
))
413 (invoke-with-parsed-prefixes (constantly t
) <transform
> env
))
414 (macrolet ((with-specials ((&optional
) &body body
)
415 `(let ((*instruction-base-uri
* instruction-base-uri
)
416 (*namespaces
* namespaces
)
417 (*apply-imports-limit
* apply-imports-limit
))
420 (do-toplevel (import "import" <transform
>)
421 (let ((uri (puri:merge-uris
(stp:attribute-value import
"href")
422 (stp:base-uri import
))))
423 (push (parse-imported-stylesheet env stylesheet uri uri-resolver
)
425 (let ((import-priority
426 (incf *import-priority
*))
427 (var-cont (prepare-global-variables stylesheet
<transform
>)))
428 ;; delay the rest of compilation until we've seen all global
431 (mapc #'funcall
(nreverse continuations
))
433 (let ((*import-priority
* import-priority
))
435 (parse-keys! stylesheet
<transform
> env
)
436 (parse-templates! stylesheet
<transform
> env
)
437 (parse-output! stylesheet
<transform
>)
438 (parse-strip/preserve-space
! stylesheet
<transform
> env
)
439 (parse-attribute-sets! stylesheet
<transform
> env
)
440 (parse-namespace-aliases! stylesheet
<transform
> env
))))))))
442 (defvar *xsl-import-stack
* nil
)
444 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver
)
445 (let* ((uri (if uri-resolver
446 (funcall uri-resolver
(puri:render-uri uri nil
))
448 (str (puri:render-uri uri nil
))
451 (uri-to-pathname uri
)
452 (cxml:xml-parse-error
(c)
453 (xslt-error "cannot find imported stylesheet ~A: ~A"
457 :element-type
'(unsigned-byte 8)
458 :if-does-not-exist nil
)
460 (xslt-error "cannot find imported stylesheet ~A at ~A"
462 (when (find str
*xsl-import-stack
* :test
#'equal
)
463 (xslt-error "recursive inclusion of ~A" uri
))
464 (let ((*xsl-import-stack
* (cons str
*xsl-import-stack
*)))
465 (parse-1-stylesheet env stylesheet stream uri-resolver
)))))
467 (defun parse-stylesheet (designator &key uri-resolver
)
468 (xpath:with-namespaces
((nil #.
*xsl
*))
469 (let* ((*import-priority
* 0)
470 (puri:*strict-parse
* nil
)
471 (stylesheet (make-stylesheet))
472 (env (make-instance 'lexical-xslt-environment
))
473 (*excluded-namespaces
* *excluded-namespaces
*)
474 (*global-variable-declarations
* (make-empty-declaration-array)))
475 (ensure-mode stylesheet nil
)
476 (funcall (parse-1-stylesheet env stylesheet designator uri-resolver
))
477 ;; reverse attribute sets:
478 (let ((table (stylesheet-attribute-sets stylesheet
)))
479 (maphash (lambda (k v
)
480 (setf (gethash k table
) (nreverse v
)))
484 (defun parse-attribute-sets! (stylesheet <transform
> env
)
485 (do-toplevel (elt "attribute-set" <transform
>)
486 (with-parsed-prefixes (elt env
)
488 (mapcar (lambda (qname)
489 (multiple-value-list (decode-qname qname env nil
)))
491 (stp:attribute-value elt
"use-attribute-sets"))))
496 (unless (or (not (typep child
'stp
:element
))
497 (and (equal (stp:namespace-uri child
) *xsl
*)
498 (equal (stp:local-name child
)
500 (find (stp:namespace-uri child
)
501 *extension-namespaces
*
503 (xslt-error "non-attribute found in attribute set"))
504 (parse-instruction child
))
506 (*lexical-variable-declarations
*
507 (make-empty-declaration-array))
509 (compile-instruction `(progn ,@instructions
) env
))
510 (n-variables (length *lexical-variable-declarations
*)))
513 (loop for
(local-name uri nil
) in sets do
514 (dolist (thunk (find-attribute-set local-name uri
))
515 (funcall thunk ctx
)))
516 (let ((*lexical-variable-values
*
517 (make-variable-value-array n-variables
)))
518 (funcall thunk ctx
)))))
519 (gethash (multiple-value-bind (local-name uri
)
520 (decode-qname (stp:attribute-value elt
"name") env nil
)
521 (cons local-name uri
))
522 (stylesheet-attribute-sets stylesheet
))))))
524 (defun parse-namespace-aliases! (stylesheet <transform
> env
)
525 (do-toplevel (elt "namespace-alias" <transform
>)
526 (stp:with-attributes
(stylesheet-prefix result-prefix
) elt
528 (xpath-sys:environment-find-namespace env stylesheet-prefix
)
529 (stylesheet-namespace-aliases stylesheet
))
530 (xpath-sys:environment-find-namespace env result-prefix
)))))
532 (defun parse-exclude-result-prefixes! (node env
)
533 (stp:with-attributes
(exclude-result-prefixes)
535 (dolist (prefix (words (or exclude-result-prefixes
"")))
536 (if (equal prefix
"#default")
538 (unless (cxml-stp-impl::nc-name-p prefix
)
539 (xslt-error "invalid prefix: ~A" prefix
)))
540 (push (or (xpath-sys:environment-find-namespace env prefix
)
541 (xslt-error "namespace not found: ~A" prefix
))
542 *excluded-namespaces
*))))
544 (defun parse-extension-element-prefixes! (node env
)
545 (stp:with-attributes
(extension-element-prefixes)
547 (dolist (prefix (words (or extension-element-prefixes
"")))
548 (if (equal prefix
"#default")
550 (unless (cxml-stp-impl::nc-name-p prefix
)
551 (xslt-error "invalid prefix: ~A" prefix
)))
553 (or (xpath-sys:environment-find-namespace env prefix
)
554 (xslt-error "namespace not found: ~A" prefix
))))
555 (unless (equal uri
*xsl
*)
556 (push uri
*extension-namespaces
*)
557 (push uri
*excluded-namespaces
*))))))
559 (defun parse-strip/preserve-space
! (stylesheet <transform
> env
)
560 (xpath:with-namespaces
((nil #.
*xsl
*))
561 (do-toplevel (elt "strip-space|preserve-space" <transform
>)
562 (let ((*namespaces
* (acons-namespaces elt
))
564 (if (equal (stp:local-name elt
) "strip-space")
567 (dolist (name-test (words (stp:attribute-value elt
"elements")))
568 (let* ((pos (search ":*" name-test
))
571 ((eql pos
(- (length name-test
) 2))
572 (let* ((prefix (subseq name-test
0 pos
))
574 (xpath-sys:environment-find-namespace env prefix
)))
575 (unless (xpath::nc-name-p prefix
)
576 (xslt-error "not an NCName: ~A" prefix
))
577 (lambda (local-name uri
)
578 (declare (ignore local-name
))
579 (if (equal uri name-test-uri
)
582 ((equal name-test
"*")
583 (lambda (local-name uri
)
584 (declare (ignore local-name uri
))
587 (multiple-value-bind (name-test-local-name name-test-uri
)
588 (decode-qname name-test env nil
)
589 (lambda (local-name uri
)
590 (if (and (equal local-name name-test-local-name
)
591 (equal uri name-test-uri
))
594 (push test-function
(stylesheet-strip-tests stylesheet
))))))))
596 (defstruct (output-specification
597 (:conc-name
"OUTPUT-"))
603 (defun parse-output! (stylesheet <transform
>)
604 (let ((outputs (list-toplevel "output" <transform
>)))
608 ;; - concatenate cdata-section-elements
609 ;; - the others must not conflict
610 (error "oops, merging of output elements not supported yet"))
611 (let ((<output
> (car outputs
))
612 (spec (stylesheet-output-specification stylesheet
)))
613 (stp:with-attributes
(;; version
622 ;;; cdata-section-elements
625 (setf (output-method spec
) method
)
626 (setf (output-indent spec
) indent
)
627 (setf (output-encoding spec
) encoding
)
628 (setf (output-omit-xml-declaration spec
) omit-xml-declaration
))))))
630 (defun make-empty-declaration-array ()
631 (make-array 1 :fill-pointer
0 :adjustable t
))
633 (defun make-variable-value-array (n-lexical-variables)
634 (make-array n-lexical-variables
:initial-element
'unbound
))
636 (defun compile-global-variable (<variable
> env
) ;; also for <param>
637 (stp:with-attributes
(name select
) <variable
>
638 (when (and select
(stp:list-children
<variable
>))
639 (xslt-error "variable with select and body"))
640 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
643 (compile-xpath select env
))
644 ((stp:list-children
<variable
>)
645 (let* ((inner-sexpr `(progn ,@(parse-body <variable
>)))
646 (inner-thunk (compile-instruction inner-sexpr env
)))
648 (apply-to-result-tree-fragment ctx inner-thunk
))))
651 (declare (ignore ctx
))
653 (n-lexical-variables (length *lexical-variable-declarations
*)))
656 (let* ((*lexical-variable-values
*
657 (make-variable-value-array n-lexical-variables
)))
658 (funcall inner ctx
)))
659 "global ~s (~s) = ~s" name select
:result
))))
661 (defstruct (variable-information
662 (:constructor make-variable
)
663 (:conc-name
"VARIABLE-"))
671 (defun parse-global-variable! (<variable
> global-env
) ;; also for <param>
672 (let* ((*namespaces
* (acons-namespaces <variable
>))
673 (instruction-base-uri (stp:base-uri
<variable
>))
674 (*instruction-base-uri
* instruction-base-uri
)
675 (*excluded-namespaces
* (list *xsl
*))
676 (*extension-namespaces
* '())
677 (qname (stp:attribute-value
<variable
> "name")))
678 (with-parsed-prefixes (<variable
> global-env
)
680 (xslt-error "name missing in ~A" (stp:local-name
<variable
>)))
681 (multiple-value-bind (local-name uri
)
682 (decode-qname qname global-env nil
)
683 ;; For the normal compilation environment of templates, install it
684 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
685 (let ((index (intern-global-variable local-name uri
)))
686 ;; For the evaluation of a global variable itself, build a thunk
687 ;; that lazily resolves other variables, stored into
688 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
689 (let* ((value-thunk :unknown
)
690 (global-variable-thunk
692 (let ((v (global-variable-value index nil
)))
694 (xslt-error "recursive variable definition"))
697 (setf (global-variable-value index
) 'seen
)
698 (setf (global-variable-value index
)
699 (funcall value-thunk ctx
)))
702 (excluded-namespaces *excluded-namespaces
*)
703 (extension-namespaces *extension-namespaces
*)
706 (let ((*instruction-base-uri
* instruction-base-uri
)
707 (*excluded-namespaces
* excluded-namespaces
)
708 (*extension-namespaces
* extension-namespaces
))
710 (compile-global-variable <variable
> global-env
))))))
711 (setf (gethash (cons local-name uri
)
712 (initial-global-variable-thunks global-env
))
713 global-variable-thunk
)
714 (make-variable :index index
715 :local-name local-name
717 :thunk global-variable-thunk
718 :param-p
(namep <variable
> "param")
719 :thunk-setter thunk-setter
)))))))
721 (defun parse-keys! (stylesheet <transform
> env
)
722 (xpath:with-namespaces
((nil #.
*xsl
*))
723 (do-toplevel (<key
> "key" <transform
>)
724 (let ((*instruction-base-uri
* (stp:base-uri
<key
>)))
725 (stp:with-attributes
(name match use
) <key
>
726 (unless name
(xslt-error "key name attribute not specified"))
727 (unless match
(xslt-error "key match attribute not specified"))
728 (unless use
(xslt-error "key use attribute not specified"))
729 (multiple-value-bind (local-name uri
)
730 (decode-qname name env nil
)
732 (cons local-name uri
)
733 (compile-xpath `(xpath:xpath
,(parse-key-pattern match
)) env
)
734 (compile-xpath use env
))))))))
736 (defun prepare-global-variables (stylesheet <transform
>)
737 (xpath:with-namespaces
((nil #.
*xsl
*))
738 (let* ((table (make-hash-table :test
'equal
))
739 (global-env (make-instance 'global-variable-environment
740 :initial-global-variable-thunks table
))
742 (do-toplevel (<variable
> "variable|param" <transform
>)
743 (let ((var (parse-global-variable! <variable
> global-env
)))
744 (xslt-trace "parsing global variable ~s (uri ~s)"
745 (variable-local-name var
)
750 (and (equal (variable-local-name a
)
751 (variable-local-name b
))
752 (equal (variable-uri a
)
754 (xslt-error "duplicate definition for global variable ~A"
755 (variable-local-name var
)))
757 (setf specs
(nreverse specs
))
759 ;; now that the global environment knows about all variables, run the
760 ;; thunk setters to perform their compilation
761 (mapc (lambda (spec) (funcall (variable-thunk-setter spec
))) specs
)
762 (let ((table (stylesheet-global-variables stylesheet
))
763 (newlen (length *global-variable-declarations
*)))
764 (adjust-array table newlen
:fill-pointer newlen
)
766 (setf (elt table
(variable-index spec
)) spec
)))))))
768 (defun parse-templates! (stylesheet <transform
> env
)
770 (do-toplevel (<template
> "template" <transform
>)
771 (let ((*namespaces
* (acons-namespaces <template
>))
772 (*instruction-base-uri
* (stp:base-uri
<template
>)))
773 (with-parsed-prefixes (<template
> env
)
774 (dolist (template (compile-template <template
> env i
))
775 (let ((name (template-name template
)))
777 (let* ((table (stylesheet-named-templates stylesheet
))
778 (head (car (gethash name table
))))
779 (when (and head
(eql (template-import-priority head
)
780 (template-import-priority template
)))
781 ;; fixme: is this supposed to be a run-time error?
782 (xslt-error "conflicting templates for ~A" name
))
783 (push template
(gethash name table
)))
784 (let ((mode (ensure-mode/qname stylesheet
785 (template-mode-qname template
)
787 (setf (template-mode template
) mode
)
788 (push template
(mode-templates mode
))))))))
792 ;;;; APPLY-STYLESHEET
794 (defvar *stylesheet
*)
796 (deftype xml-designator
() '(or runes
:xstream runes
:rod array stream pathname
))
798 (defun unalias-uri (uri)
799 (gethash uri
(stylesheet-namespace-aliases *stylesheet
*)
802 (defstruct (parameter
803 (:constructor make-parameter
(value local-name
&optional uri
)))
808 (defun find-parameter-value (local-name uri parameters
)
809 (dolist (p parameters
)
810 (when (and (equal (parameter-local-name p
) local-name
)
811 (equal (parameter-uri p
) uri
))
812 (return (parameter-value p
)))))
814 (defvar *uri-resolver
*)
816 (defun parse-allowing-microsoft-bom (pathname handler
)
817 (with-open-file (s pathname
:element-type
'(unsigned-byte 8))
818 (unless (and (eql (read-byte s nil
) #xef
)
819 (eql (read-byte s nil
) #xbb
)
820 (eql (read-byte s nil
) #xbf
))
822 (cxml:parse s handler
)))
826 (defun %document
(uri-string base-uri
)
828 (puri:merge-uris uri-string
(or base-uri
"")))
831 (funcall *uri-resolver
* (puri:render-uri absolute-uri nil
))
835 (uri-to-pathname resolved-uri
)
836 (cxml:xml-parse-error
(c)
837 (xslt-error "cannot find referenced document ~A: ~A"
840 (or (gethash pathname
*documents
*)
841 (setf (gethash pathname
*documents
*)
842 (make-whitespace-stripper
844 (parse-allowing-microsoft-bom pathname
846 ((or file-error cxml
:xml-parse-error
) (c)
847 (xslt-error "cannot parse referenced document ~A: ~A"
849 (stylesheet-strip-tests *stylesheet
*))))))
850 (when (puri:uri-fragment absolute-uri
)
851 (xslt-error "use of fragment identifiers in document() not supported"))
854 (xpath-sys:define-extension xslt
*xsl
*)
856 (defun document-base-uri (node)
857 (xpath-protocol:base-uri
859 ((xpath-protocol:node-type-p node
:document
)
860 (xpath::find-in-pipe-if
862 (xpath-protocol:node-type-p x
:element
))
863 (xpath-protocol:child-pipe node
)))
864 ((xpath-protocol:node-type-p node
:element
)
867 (xpath-protocol:parent-node node
)))))
869 (xpath-sys:define-xpath-function
/lazy
871 (object &optional node-set
)
872 (let ((instruction-base-uri *instruction-base-uri
*))
874 (let* ((object (funcall object ctx
))
875 (node-set (and node-set
(funcall node-set ctx
)))
878 (document-base-uri (xpath::textually-first-node node-set
))
879 instruction-base-uri
)))
880 (xpath-sys:make-node-set
881 (if (xpath:node-set-p object
)
882 (xpath:map-node-set-
>list
884 (%document
(xpath:string-value node
)
887 (document-base-uri node
))))
889 (list (%document
(xpath:string-value object
) base-uri
))))))))
891 (xpath-sys:define-xpath-function
/lazy xslt
:key
(name object
)
892 (let ((namespaces *namespaces
*))
894 (let* ((qname (xpath:string-value
(funcall name ctx
)))
895 (object (funcall object ctx
))
897 (multiple-value-bind (local-name uri
)
898 (decode-qname/runtime qname namespaces nil
)
899 (cons local-name uri
)))
900 (key (find-key expanded-name
*stylesheet
*)))
901 (labels ((get-by-key (value)
902 (let ((value (xpath:string-value value
)))
906 (xpath:evaluate-compiled
(key-use key
) node
)))
907 (if (xpath:node-set-p uses
)
910 (xpath-sys:pipe-of uses
)
911 :key
#'xpath
:string-value
913 (equal value
(xpath:string-value uses
)))))
915 (xpath:node-set-value
916 (xpath:evaluate-compiled
(key-match key
) ctx
)))))))
917 (xpath-sys:make-node-set
919 (if (xpath:node-set-p object
)
920 (xpath::mappend-pipe
#'get-by-key
(xpath-sys:pipe-of object
))
921 (get-by-key object
)))))))))
923 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
925 (xpath-sys:define-xpath-function
/lazy xslt
:current
()
927 (xpath-sys:make-node-set
929 (xpath:context-starting-node ctx
)
932 (xpath-sys:define-xpath-function
/lazy xslt
:unparsed-entity-uri
(name)
934 (or (xpath-protocol:unparsed-entity-uri
(xpath:context-node ctx
)
938 (defun %get-node-id
(node)
939 (when (xpath:node-set-p node
)
940 (setf node
(xpath::textually-first-node node
)))
942 (let ((id (xpath-sys:get-node-id node
))
945 for parent
= node then next
946 for next
= (xpath-protocol:parent-node parent
)
947 for this-base-uri
= (xpath-protocol:base-uri parent
)
948 for highest-base-uri
= (if (plusp (length this-base-uri
))
952 finally
(return highest-base-uri
))))
953 ;; Heuristic: Reverse it so that the /home/david/alwaysthesame prefix is
954 ;; checked only if everything else matches.
956 ;; This might be pointless premature optimization, but I like the idea :-)
957 (nreverse (concatenate 'string highest-base-uri
"//" id
)))))
959 (xpath-sys:define-xpath-function
/lazy xslt
:generate-id
(&optional node-set-thunk
)
962 (%get-node-id
(xpath:node-set-value
(funcall node-set-thunk ctx
))))
964 (%get-node-id
(xpath:context-node ctx
)))))
966 (declaim (special *available-instructions
*))
968 (xpath-sys:define-xpath-function
/lazy xslt
:element-available
(qname)
969 (let ((namespaces *namespaces
*))
971 (let ((qname (funcall qname ctx
)))
972 (multiple-value-bind (local-name uri
)
973 (decode-qname/runtime qname namespaces nil
)
974 (and (equal uri
*xsl
*)
975 (gethash local-name
*available-instructions
*)
978 (xpath-sys:define-xpath-function
/lazy xslt
:function-available
(qname)
979 (let ((namespaces *namespaces
*))
981 (let ((qname (funcall qname ctx
)))
982 (multiple-value-bind (local-name uri
)
983 (decode-qname/runtime qname namespaces nil
)
984 (and (zerop (length uri
))
985 (or (xpath-sys:find-xpath-function local-name
*xsl
*)
986 (xpath-sys:find-xpath-function local-name uri
))
989 (defun apply-stylesheet
990 (stylesheet source-designator
991 &key output parameters uri-resolver navigator
)
992 (when (typep stylesheet
'xml-designator
)
993 (setf stylesheet
(parse-stylesheet stylesheet
)))
994 (invoke-with-output-sink
997 (let* ((*documents
* (make-hash-table :test
'equal
))
998 (xpath:*navigator
* (or navigator
:default-navigator
))
999 (puri:*strict-parse
* nil
)
1000 (*stylesheet
* stylesheet
)
1001 (*empty-mode
* (make-mode))
1002 (*default-mode
* (find-mode stylesheet nil
))
1003 (global-variable-specs
1004 (stylesheet-global-variables stylesheet
))
1005 (*global-variable-values
*
1006 (make-variable-value-array (length global-variable-specs
)))
1007 (*uri-resolver
* uri-resolver
)
1009 (if (typep source-designator
'xml-designator
)
1010 (cxml:parse source-designator
(stp:make-builder
))
1013 (make-whitespace-stripper
1015 (stylesheet-strip-tests stylesheet
)))
1016 (ctx (xpath:make-context xpath-root-node
)))
1017 (when (pathnamep source-designator
)
1018 (setf (gethash source-designator
*documents
*) xpath-root-node
))
1021 (when (variable-param-p spec
)
1023 (find-parameter-value (variable-local-name spec
)
1027 (setf (global-variable-value (variable-index spec
))
1029 global-variable-specs
)
1032 (funcall (variable-thunk spec
) ctx
))
1033 global-variable-specs
)
1034 ;; zzz we wouldn't have to mask float traps here if we used the
1035 ;; XPath API properly. Unfortunately I've been using FUNCALL
1036 ;; everywhere instead of EVALUATE, so let's paper over that
1037 ;; at a central place to be sure:
1038 (xpath::with-float-traps-masked
()
1039 (apply-templates ctx
:mode
*default-mode
*)))
1040 (xpath:xpath-error
(c)
1041 (xslt-error "~A" c
))))
1042 (stylesheet-output-specification stylesheet
)
1045 (defun find-attribute-set (local-name uri
)
1046 (or (gethash (cons local-name uri
) (stylesheet-attribute-sets *stylesheet
*))
1047 (xslt-error "no such attribute set: ~A/~A" local-name uri
)))
1049 (defun apply-templates/list
(list &key param-bindings sort-predicate mode
)
1050 (when sort-predicate
1051 (setf list
(sort list sort-predicate
)))
1052 (let* ((n (length list
))
1053 (s/d
(lambda () n
)))
1058 (apply-templates (xpath:make-context child s
/d i
)
1059 :param-bindings param-bindings
1062 (defvar *stack-limit
* 200)
1064 (defun invoke-with-stack-limit (fn)
1065 (let ((*stack-limit
* (1- *stack-limit
*)))
1066 (unless (plusp *stack-limit
*)
1067 (xslt-error "*stack-limit* reached; stack overflow"))
1070 (defun invoke-template (ctx template param-bindings
)
1071 (let ((*lexical-variable-values
*
1072 (make-variable-value-array (template-n-variables template
))))
1073 (with-stack-limit ()
1075 for
(name-cons value
) in param-bindings
1076 for
(nil index nil
) = (find name-cons
1077 (template-params template
)
1082 (setf (lexical-variable-value index
) value
)))
1083 (funcall (template-body template
) ctx
))))
1085 (defun apply-default-templates (ctx mode
)
1086 (let ((node (xpath:context-node ctx
)))
1088 ((or (xpath-protocol:node-type-p node
:processing-instruction
)
1089 (xpath-protocol:node-type-p node
:comment
)))
1090 ((or (xpath-protocol:node-type-p node
:text
)
1091 (xpath-protocol:node-type-p node
:attribute
))
1092 (write-text (xpath-protocol:node-text node
)))
1094 (apply-templates/list
1096 (xpath-protocol:child-pipe node
))
1099 (defvar *apply-imports
*)
1101 (defun apply-applicable-templates (ctx templates param-bindings finally
)
1102 (labels ((apply-imports (&optional actual-param-bindings
)
1104 (let* ((this (pop templates
))
1105 (low (template-apply-imports-limit this
))
1106 (high (template-import-priority this
)))
1110 (<= low
(template-import-priority x
) high
))
1112 (invoke-template ctx this actual-param-bindings
))
1113 (funcall finally
))))
1114 (let ((*apply-imports
* #'apply-imports
))
1115 (apply-imports param-bindings
))))
1117 (defun apply-templates (ctx &key param-bindings mode
)
1118 (apply-applicable-templates ctx
1119 (find-templates ctx
(or mode
*default-mode
*))
1122 (apply-default-templates ctx mode
))))
1124 (defun call-template (ctx name
&optional param-bindings
)
1125 (apply-applicable-templates ctx
1126 (find-named-templates name
)
1129 (error "cannot find named template: ~s"
1132 (defun find-templates (ctx mode
)
1133 (let* ((matching-candidates
1134 (remove-if-not (lambda (template)
1135 (template-matches-p template ctx
))
1136 (mode-templates mode
)))
1138 (if matching-candidates
1141 :key
#'template-import-priority
))
1143 (priority-groups (make-array npriorities
:initial-element nil
)))
1144 (dolist (template matching-candidates
)
1146 (elt priority-groups
(template-import-priority template
))))
1148 for i from
(1- npriorities
) downto
0
1149 for group
= (elt priority-groups i
)
1150 for template
= (maximize #'template
< group
)
1154 (defun find-named-templates (name)
1155 (gethash name
(stylesheet-named-templates *stylesheet
*)))
1157 (defun template< (a b
) ;assuming same import priority
1158 (let ((p (template-priority a
))
1159 (q (template-priority b
)))
1164 (xslt-cerror "conflicting templates:~_~A,~_~A"
1165 (template-match-expression a
)
1166 (template-match-expression b
))
1167 (< (template-position a
) (template-position b
))))))
1169 (defun maximize (< things
)
1171 (let ((max (car things
)))
1172 (dolist (other (cdr things
))
1173 (when (funcall < max other
)
1177 (defun template-matches-p (template ctx
)
1178 (find (xpath:context-node ctx
)
1179 (xpath:all-nodes
(funcall (template-match-thunk template
) ctx
))))
1181 (defun invoke-with-output-sink (fn output-spec output
)
1184 (with-open-file (s output
1186 :element-type
'(unsigned-byte 8)
1187 :if-exists
:rename-and-delete
)
1188 (invoke-with-output-sink fn output-spec s
)))
1190 (invoke-with-output-sink fn
1192 (make-output-sink output-spec output
)))
1193 ((or hax
:abstract-handler sax
:abstract-handler
)
1194 (with-xml-output output
1197 (defun make-output-sink (output-spec stream
)
1200 (let ((et (stream-element-type stream
)))
1202 ((or (null et
) (subtypep et
'(unsigned-byte 8)))
1203 (runes:make-octet-stream-ystream stream
))
1204 ((subtypep et
'character
)
1205 (runes:make-character-stream-ystream stream
))))
1206 (runes:make-rod-ystream
)))
1207 (omit-xml-declaration-p
1208 (equal (output-omit-xml-declaration output-spec
) "yes"))
1210 (make-instance 'cxml
::sink
1212 :omit-xml-declaration-p omit-xml-declaration-p
)))
1214 ((equalp (output-method output-spec
) "HTML")
1215 (make-instance 'combi-sink
1216 :hax-target
(make-instance 'chtml
::sink
1218 :sax-target sax-target
1219 :encoding
(output-encoding output-spec
)))
1220 ((equalp (output-method output-spec
) "TEXT")
1221 (make-text-filter sax-target
))
1239 (defun expression-priority (form)
1240 (let ((step (second form
)))
1241 (if (and (null (cddr form
))
1243 (member (car step
) '(:child
:attribute
))
1245 (let ((name (second step
)))
1249 (or (eq (car name
) :qname
)
1250 (eq (car name
) :processing-instruction
))))
1253 (or (eq (car name
) :namespace
)
1254 (eq (car name
) '*)))
1260 (defun valid-expression-p (expr)
1263 ((eq (first expr
) :path
)
1265 (let ((filter (third x
)))
1266 (or (null filter
) (valid-expression-p filter
))))
1268 ((eq (first expr
) :variable
) ;(!)
1271 (every #'valid-expression-p
(cdr expr
)))))
1273 (defun parse-xpath (str)
1275 (xpath:parse-xpath str
)
1276 (xpath:xpath-error
(c)
1277 (xslt-error "~A" c
))))
1279 ;; zzz also use naive-pattern-expression here?
1280 (defun parse-key-pattern (str)
1282 (mapcar #'(lambda (item)
1283 `(:path
(:root
:node
)
1284 (:descendant-or-self
*)
1286 (parse-pattern str
))))
1287 (if (null (rest parsed
))
1289 `(:union
,@parsed
))))
1291 (defun parse-pattern (str)
1292 ;; zzz check here for anything not allowed as an XSLT pattern
1293 ;; zzz can we hack id() and key() here?
1294 (let ((form (parse-xpath str
)))
1295 (unless (consp form
)
1296 (xslt-error "not a valid pattern: ~A" str
))
1297 (labels ((process-form (form)
1298 (cond ((eq (car form
) :union
)
1299 (alexandria:mappend
#'process-form
(rest form
)))
1300 ((not (or (eq (car form
) :path
)
1301 (and (eq (car form
) :filter
)
1302 (let ((filter (second form
)))
1304 (member (car filter
)
1306 (equal (third form
) '(:true
)))
1307 (member (car form
) '(:key
:id
))))
1308 (xslt-error "not a valid pattern: ~A ~A" str form
))
1309 ((not (valid-expression-p form
))
1310 (xslt-error "invalid filter"))
1312 (process-form form
))))
1314 (defun naive-pattern-expression (x)
1316 (:path
`(:path
(:ancestor-or-self
:node
) ,@(cdr x
)))
1317 ((:filter
:key
:id
) x
)))
1319 (defun compile-value-thunk (value env
)
1320 (if (and (listp value
) (eq (car value
) 'progn
))
1321 (let ((inner-thunk (compile-instruction value env
)))
1323 (apply-to-result-tree-fragment ctx inner-thunk
)))
1324 (compile-xpath value env
)))
1326 (defun compile-var-bindings/nointern
(forms env
)
1328 for
(name value
) in forms
1329 collect
(multiple-value-bind (local-name uri
)
1330 (decode-qname name env nil
)
1331 (list (cons local-name uri
)
1333 (compile-value-thunk value env
)
1334 "local variable ~s = ~s" name
:result
)))))
1336 (defun compile-var-bindings (forms env
)
1338 for
(cons thunk
) in
(compile-var-bindings/nointern forms env
)
1339 for
(local-name . uri
) = cons
1341 (push-variable local-name
1343 *lexical-variable-declarations
*)
1346 (defun compile-template (<template
> env position
)
1347 (stp:with-attributes
(match name priority mode
) <template
>
1348 (unless (or name match
)
1349 (xslt-error "missing match in template"))
1350 (multiple-value-bind (params body-pos
)
1353 for child in
(stp:list-children
<template
>)
1354 while
(namep child
"param")
1355 collect
(parse-param child
) into params
1356 finally
(return (values params i
)))
1357 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
1358 (param-bindings (compile-var-bindings params env
))
1359 (body (parse-body <template
> body-pos
(mapcar #'car params
)))
1360 (body-thunk (compile-instruction `(progn ,@body
) env
))
1366 ;; set params that weren't initialized by apply-templates
1367 (loop for
(name index param-thunk
) in param-bindings
1368 when
(eq (lexical-variable-value index nil
) 'unbound
)
1369 do
(setf (lexical-variable-value index
)
1370 (funcall param-thunk ctx
)))
1371 (funcall body-thunk ctx
))))
1372 "template: match = ~s name = ~s" match name
))
1373 (n-variables (length *lexical-variable-declarations
*)))
1376 (multiple-value-bind (local-name uri
)
1377 (decode-qname name env nil
)
1379 (make-template :name
(cons local-name uri
)
1380 :import-priority
*import-priority
*
1381 :apply-imports-limit
*apply-imports-limit
*
1382 :params param-bindings
1383 :body outer-body-thunk
1384 :n-variables n-variables
))))
1386 (mapcar (lambda (expression)
1391 ,(naive-pattern-expression expression
))
1393 "match-thunk for template (match ~s): ~s --> ~s"
1394 match expression
:result
))
1396 (parse-number:parse-number priority
)
1397 (expression-priority expression
))))
1398 (make-template :match-expression expression
1399 :match-thunk match-thunk
1400 :import-priority
*import-priority
*
1401 :apply-imports-limit
*apply-imports-limit
*
1405 :params param-bindings
1406 :body outer-body-thunk
1407 :n-variables n-variables
)))
1408 (parse-pattern match
))))))))
1410 (xuriella::parse-stylesheet
#p
"/home/david/src/lisp/xuriella/test.xsl")