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
)
33 (declaim (optimize (debug 2)))
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
53 :format-arguments args
)))
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
62 (let ((doit (gensym)))
63 `(flet ((,doit
() ,form
))
70 (defun compile-xpath (xpath &optional env
)
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
)
87 (map-pipe-eagerly #'(lambda (,var
) ,@body
) ,pipe
)
91 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
93 (defparameter *initial-namespaces
*
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)
110 (multiple-value-bind (prefix local-name
)
111 (cxml::split-qname str
)
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
)
129 (if (or prefix
(not attributep
))
130 (xpath:environment-find-namespace env 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
)
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
)))
152 (assert (not (eq result
'unbound
))))
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
)))
162 (assert (not (eq result
'unbound
))))
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-variable
170 ((env xslt-environment
) lname uri
)
172 (find-variable-index lname uri
*lexical-variable-declarations
*)))
175 (declare (ignore ctx
))
176 (svref *lexical-variable-values
* index
)))))
178 (defclass lexical-xslt-environment
(xslt-environment) ())
180 (defmethod xpath:environment-find-variable
181 ((env lexical-xslt-environment
) lname uri
)
182 (or (call-next-method)
184 (find-variable-index lname uri
*global-variable-declarations
*)))
187 (declare (ignore ctx
))
188 (svref *global-variable-values
* index
))))))
190 (defclass global-variable-environment
(xslt-environment)
191 ((initial-global-variable-thunks
192 :initarg
:initial-global-variable-thunks
193 :accessor initial-global-variable-thunks
)))
195 (defmethod xpath:environment-find-variable
196 ((env global-variable-environment
) lname uri
)
197 (or (call-next-method)
198 (gethash (cons lname uri
) (initial-global-variable-thunks env
))))
201 ;;;; TEXT-OUTPUT-SINK
203 ;;;; A sink that serializes only text and will error out on any other
206 (defmacro with-text-output-sink
((var) &body body
)
207 `(invoke-with-text-output-sink (lambda (,var
) ,@body
)))
209 (defclass text-output-sink
(sax:default-handler
)
210 ((target :initarg
:target
:accessor text-output-sink-target
)))
212 (defmethod sax:characters
((sink text-output-sink
) data
)
213 (write-string data
(text-output-sink-target sink
)))
215 (defun invoke-with-text-output-sink (fn)
216 (with-output-to-string (s)
217 (funcall fn
(make-instance 'text-output-sink
:target s
))))
222 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
223 (defvar *xsl
* "http://www.w3.org/1999/XSL/Transform")
224 (defvar *xml
* "http://www.w3.org/XML/1998/namespace")
225 (defvar *html
* "http://www.w3.org/1999/xhtml"))
227 (defun of-name (local-name)
228 (stp:of-name local-name
*xsl
*))
230 (defun namep (node local-name
)
231 (and (typep node
'(or stp
:element stp
:attribute
))
232 (equal (stp:namespace-uri node
) *xsl
*)
233 (equal (stp:local-name node
) local-name
)))
236 ;;;; PARSE-STYLESHEET
238 (defstruct stylesheet
239 (modes (make-hash-table :test
'equal
))
240 (global-variables ())
241 (output-specification (make-output-specification))
243 (named-templates (make-hash-table :test
'equal
))
244 (attribute-sets (make-hash-table :test
'equal
)))
246 (defstruct mode
(templates nil
))
248 (defun find-mode (stylesheet local-name
&optional uri
)
249 (gethash (cons local-name uri
) (stylesheet-modes stylesheet
)))
251 (defun ensure-mode (stylesheet &optional local-name uri
)
252 (or (find-mode stylesheet local-name uri
)
253 (setf (gethash (cons local-name uri
) (stylesheet-modes stylesheet
))
256 (defun ensure-mode/qname
(stylesheet qname env
)
258 (multiple-value-bind (local-name uri
)
259 (decode-qname qname env nil
)
260 (ensure-mode stylesheet local-name uri
))
261 (find-mode stylesheet nil
)))
263 (defun acons-namespaces (element &optional
(bindings *namespaces
*))
264 (map-namespace-declarations (lambda (prefix uri
)
265 (push (cons prefix uri
) bindings
))
269 (defvar *excluded-namespaces
* (list *xsl
*))
270 (defvar *empty-mode
*)
272 (defvar *xsl-include-stack
* nil
)
274 (defun parse-stylesheet-to-stp (input uri-resolver
)
275 (let* ((d (cxml:parse input
(make-text-normalizer (cxml-stp:make-builder
))))
276 (<transform
> (stp:document-element d
)))
277 (strip-stylesheet <transform
>)
278 ;; FIXME: handle embedded stylesheets
279 (unless (and (equal (stp:namespace-uri
<transform
>) *xsl
*)
280 (or (equal (stp:local-name
<transform
>) "transform")
281 (equal (stp:local-name
<transform
>) "stylesheet")))
282 (xslt-error "not a stylesheet"))
283 (dolist (include (stp:filter-children
(of-name "include") <transform
>))
284 (let* ((uri (puri:merge-uris
(stp:attribute-value include
"href")
285 (stp:base-uri include
)))
286 (uri (if uri-resolver
287 (funcall uri-resolver
(puri:render-uri uri nil
))
289 (str (puri:render-uri uri nil
))
292 (cxml::uri-to-pathname uri
)
293 (cxml:xml-parse-error
(c)
294 (xslt-error "cannot find included stylesheet ~A: ~A"
298 :element-type
'(unsigned-byte 8)
299 :if-does-not-exist nil
)
301 (xslt-error "cannot find included stylesheet ~A at ~A"
303 (when (find str
*xsl-include-stack
* :test
#'equal
)
304 (xslt-error "recursive inclusion of ~A" uri
))
305 (let* ((*xsl-include-stack
* (cons str
*xsl-include-stack
*))
306 (<transform
>2 (parse-stylesheet-to-stp stream uri-resolver
)))
307 (stp:do-children
(child <transform
>2)
308 (stp:insert-child-after
<transform
>
311 (stp:detach include
)))))
314 (defvar *instruction-base-uri
*)
315 (defvar *apply-imports-limit
*)
316 (defvar *import-priority
*)
318 (defun parse-1-stylesheet (env stylesheet designator uri-resolver
)
319 (let* ((<transform
> (parse-stylesheet-to-stp designator uri-resolver
))
320 (*instruction-base-uri
* (stp:base-uri
<transform
>))
321 (*namespaces
* (acons-namespaces <transform
>))
322 (*apply-imports-limit
* (1+ *import-priority
*)))
323 (dolist (import (stp:filter-children
(of-name "import") <transform
>))
324 (let ((uri (puri:merge-uris
(stp:attribute-value import
"href")
325 (stp:base-uri import
))))
326 (parse-imported-stylesheet env stylesheet uri uri-resolver
)))
327 (incf *import-priority
*)
328 (parse-exclude-result-prefixes! <transform
> env
)
329 (parse-global-variables! stylesheet
<transform
>)
330 (parse-templates! stylesheet
<transform
> env
)
331 (parse-output! stylesheet
<transform
>)
332 (parse-strip/preserve-space
! stylesheet
<transform
> env
)
333 (parse-attribute-sets! stylesheet
<transform
> env
)))
335 (defvar *xsl-import-stack
* nil
)
337 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver
)
338 (let* ((uri (if uri-resolver
339 (funcall uri-resolver
(puri:render-uri uri nil
))
341 (str (puri:render-uri uri nil
))
344 (cxml::uri-to-pathname uri
)
345 (cxml:xml-parse-error
(c)
346 (xslt-error "cannot find imported stylesheet ~A: ~A"
350 :element-type
'(unsigned-byte 8)
351 :if-does-not-exist nil
)
353 (xslt-error "cannot find imported stylesheet ~A at ~A"
355 (when (find str
*xsl-import-stack
* :test
#'equal
)
356 (xslt-error "recursive inclusion of ~A" uri
))
357 (let ((*xsl-import-stack
* (cons str
*xsl-import-stack
*)))
358 (parse-1-stylesheet env stylesheet stream uri-resolver
)))))
360 (defun parse-stylesheet (designator &key uri-resolver
)
361 (let* ((*import-priority
* 0)
362 (puri:*strict-parse
* nil
)
363 (stylesheet (make-stylesheet))
364 (env (make-instance 'lexical-xslt-environment
))
365 (*excluded-namespaces
* *excluded-namespaces
*)
366 (*global-variable-declarations
* (make-empty-declaration-array)))
367 (ensure-mode stylesheet nil
)
368 (parse-1-stylesheet env stylesheet designator uri-resolver
)
369 ;; reverse attribute sets:
370 (let ((table (stylesheet-attribute-sets stylesheet
)))
371 (maphash (lambda (k v
)
372 (setf (gethash k table
) (nreverse v
)))
376 (defun parse-attribute-sets! (stylesheet <transform
> env
)
377 (dolist (elt (stp:filter-children
(of-name "attribute-set") <transform
>))
379 (mapcar (lambda (qname)
380 (multiple-value-list (decode-qname qname env nil
)))
382 (stp:attribute-value elt
"use-attribute-sets"))))
384 (stp:map-children
'list
#'parse-instruction elt
))
385 (*lexical-variable-declarations
*
386 (make-empty-declaration-array))
388 (compile-instruction `(progn ,@instructions
) env
))
389 (n-variables (length *lexical-variable-declarations
*)))
392 (loop for
(local-name uri nil
) in sets do
393 (dolist (thunk (find-attribute-set local-name uri
))
394 (funcall thunk ctx
)))
395 (let ((*lexical-variable-values
*
396 (make-variable-value-array n-variables
)))
397 (funcall thunk ctx
)))))
398 (gethash (multiple-value-bind (local-name uri
)
399 (decode-qname (stp:attribute-value elt
"name") env nil
)
400 (cons local-name uri
))
401 (stylesheet-attribute-sets stylesheet
)))))
403 (defun parse-exclude-result-prefixes! (<transform
> env
)
404 (stp:with-attributes
(exclude-result-prefixes) <transform
>
405 (dolist (prefix (words (or exclude-result-prefixes
"")))
406 (when (equal prefix
"#default")
408 (push (or (xpath:environment-find-namespace env prefix
)
409 (xslt-error "namespace not found: ~A" prefix
))
410 *excluded-namespaces
*))))
412 (xpath:with-namespaces
((nil #.
*xsl
*))
413 (defun parse-strip/preserve-space
! (stylesheet <transform
> env
)
414 (dolist (elt (stp:filter-children
(lambda (x)
415 (or (namep x
"strip-space")
416 (namep x
"preserve-space")))
418 (let ((*namespaces
* (acons-namespaces elt
))
420 (if (equal (stp:local-name elt
) "strip-space")
423 (dolist (name-test (words (stp:attribute-value elt
"elements")))
424 (let* ((pos (search ":*" name-test
))
427 ((eql pos
(- (length name-test
) 2))
428 (let* ((prefix (subseq name-test
0 pos
))
430 (xpath:environment-find-namespace env prefix
)))
431 (unless (xpath::nc-name-p prefix
)
432 (xslt-error "not an NCName: ~A" prefix
))
433 (lambda (local-name uri
)
434 (declare (ignore local-name
))
435 (if (equal uri name-test-uri
)
438 ((equal name-test
"*")
439 (lambda (local-name uri
)
440 (declare (ignore local-name uri
))
443 (multiple-value-bind (name-test-local-name name-test-uri
)
444 (decode-qname name-test env nil
)
445 (lambda (local-name uri
)
446 (if (and (equal local-name name-test-local-name
)
447 (equal uri name-test-uri
))
450 (push test-function
(stylesheet-strip-tests stylesheet
))))))))
452 (defstruct (output-specification
453 (:conc-name
"OUTPUT-"))
459 (defun parse-output! (stylesheet <transform
>)
460 (let ((outputs (stp:filter-children
(of-name "output") <transform
>)))
464 ;; - concatenate cdata-section-elements
465 ;; - the others must not conflict
466 (error "oops, merging of output elements not supported yet"))
467 (let ((<output
> (car outputs
))
468 (spec (stylesheet-output-specification stylesheet
)))
469 (stp:with-attributes
(;; version
478 ;;; cdata-section-elements
481 (setf (output-method spec
) method
)
482 (setf (output-indent spec
) indent
)
483 (setf (output-encoding spec
) encoding
)
484 (setf (output-omit-xml-declaration spec
) omit-xml-declaration
))))))
486 (defun make-empty-declaration-array ()
487 (make-array 1 :fill-pointer
0 :adjustable t
))
489 (defun make-variable-value-array (n-lexical-variables)
490 (make-array n-lexical-variables
:initial-element
'unbound
))
492 (defun compile-global-variable (<variable
> env
) ;; also for <param>
493 (stp:with-attributes
(name select
) <variable
>
494 (when (and select
(stp:list-children
<variable
>))
495 (xslt-error "variable with select and body"))
496 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
499 (compile-xpath select env
))
500 ((stp:list-children
<variable
>)
501 (let* ((inner-sexpr `(progn ,@(parse-body <variable
>)))
502 (inner-thunk (compile-instruction inner-sexpr env
)))
504 (apply-to-result-tree-fragment ctx inner-thunk
))))
507 (declare (ignore ctx
))
509 (n-lexical-variables (length *lexical-variable-declarations
*)))
511 (let ((*lexical-variable-values
*
512 (make-variable-value-array n-lexical-variables
)))
513 (funcall inner ctx
))))))
515 (defstruct (variable-information
516 (:constructor make-variable
)
517 (:conc-name
"VARIABLE-"))
525 (defun parse-global-variable! (<variable
> global-env
) ;; also for <param>
526 (let ((*namespaces
* (acons-namespaces <variable
>))
527 (qname (stp:attribute-value
<variable
> "name")))
529 (xslt-error "name missing in ~A" (stp:local-name
<variable
>)))
530 (multiple-value-bind (local-name uri
)
531 (decode-qname qname global-env nil
)
532 ;; For the normal compilation environment of templates, install it
533 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
534 (let ((index (intern-global-variable local-name uri
)))
535 ;; For the evaluation of a global variable itself, build a thunk
536 ;; that lazily resolves other variables, stored into
537 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
538 (let* ((value-thunk :unknown
)
539 (global-variable-thunk
541 (let ((v (global-variable-value index nil
)))
543 (xslt-error "recursive variable definition"))
546 ;; (print (list :computing index))
547 (setf (global-variable-value index
) 'seen
)
548 (setf (global-variable-value index
)
549 (funcall value-thunk ctx
))
550 #+nil
(print (list :done-computing index
551 (global-variable-value index
)))
552 #+nil
(global-variable-value index
))
554 #+nil
(print (list :have
560 (compile-global-variable <variable
> global-env
)))))
561 (setf (gethash (cons local-name uri
)
562 (initial-global-variable-thunks global-env
))
563 global-variable-thunk
)
564 (make-variable :index index
565 :local-name local-name
567 :thunk global-variable-thunk
568 :param-p
(namep <variable
> "param")
569 :thunk-setter thunk-setter
))))))
571 (xpath:with-namespaces
((nil #.
*xsl
*))
572 (defun parse-global-variables! (stylesheet <transform
>)
573 (let* ((table (make-hash-table :test
'equal
))
574 (global-env (make-instance 'global-variable-environment
575 :initial-global-variable-thunks table
))
578 (<variable
> (xpath:evaluate
"variable|param" <transform
>))
579 (let ((var (parse-global-variable! <variable
> global-env
)))
583 (and (equal (variable-local-name a
)
584 (variable-local-name b
))
585 (equal (variable-uri a
)
587 (xslt-error "duplicate definition for global variable ~A"
588 (variable-local-name var
)))
590 ;; now that the global environment knows about all variables, run the
591 ;; thunk setters to perform their compilation
592 (mapc (lambda (spec) (funcall (variable-thunk-setter spec
))) specs
)
593 (setf (stylesheet-global-variables stylesheet
) specs
))))
595 (defun parse-templates! (stylesheet <transform
> env
)
597 (dolist (<template
> (stp:filter-children
(of-name "template") <transform
>))
598 (let ((*namespaces
* (acons-namespaces <template
>)))
599 (dolist (template (compile-template <template
> env i
))
600 (let ((name (template-name template
)))
602 (let* ((table (stylesheet-named-templates stylesheet
))
603 (head (car (gethash name table
))))
604 (when (and head
(eql (template-import-priority head
)
605 (template-import-priority template
)))
606 ;; fixme: is this supposed to be a run-time error?
607 (xslt-error "conflicting templates for ~A" name
))
608 (push template
(gethash name table
)))
609 (let ((mode (ensure-mode/qname stylesheet
610 (template-mode-qname template
)
612 (setf (template-mode template
) mode
)
613 (push template
(mode-templates mode
)))))))
617 ;;;; APPLY-STYLESHEET
619 (defvar *stylesheet
*)
622 (deftype xml-designator
() '(or runes
:xstream runes
:rod array stream pathname
))
624 (defstruct (parameter
625 (:constructor make-parameter
(value local-name
&optional uri
)))
630 (defun find-parameter-value (local-name uri parameters
)
631 (dolist (p parameters
)
632 (when (and (equal (parameter-local-name p
) local-name
)
633 (equal (parameter-uri p
) uri
))
634 (return (parameter-value p
)))))
636 (defvar *uri-resolver
*)
638 (defun parse-allowing-microsoft-bom (pathname handler
)
639 (with-open-file (s pathname
:element-type
'(unsigned-byte 8))
640 (unless (and (eql (read-byte s nil
) #xef
)
641 (eql (read-byte s nil
) #xbb
)
642 (eql (read-byte s nil
) #xbf
))
644 (cxml:parse s handler
)))
646 (defun %document
(uri-string base-uri
)
648 (puri:merge-uris uri-string base-uri
))
651 (funcall *uri-resolver
* (puri:render-uri absolute-uri nil
))
655 (cxml::uri-to-pathname resolved-uri
)
656 (cxml:xml-parse-error
(c)
657 (xslt-error "cannot find referenced document ~A: ~A"
661 (parse-allowing-microsoft-bom pathname
(stp:make-builder
))
662 ((or file-error cxml
:xml-parse-error
) (c)
663 (xslt-error "cannot parse referenced document ~A: ~A"
666 (make-whitespace-stripper document
667 (stylesheet-strip-tests *stylesheet
*))))
668 (when (puri:uri-fragment absolute-uri
)
669 (xslt-error "use of fragment identifiers in document() not supported"))
670 (record-document-order xpath-root-node
)
673 (xpath::define-xpath-function
/lazy
675 (object &optional node-set
)
676 (let ((instruction-base-uri *instruction-base-uri
*))
678 (declare (ignore ctx
))
679 (let* ((object (funcall object
))
680 (node-set (and node-set
(funcall node-set
)))
683 ;; FIXME: should use first node of the node set
684 ;; _in document order_
685 (xpath-protocol:base-uri
(xpath:first-node node-set
)))))
686 (xpath::make-node-set
687 (if (xpath:node-set-p object
)
688 (xpath:map-node-set-
>list
690 (%document
(xpath:string-value node
)
691 (or uri
(xpath-protocol:base-uri node
))))
693 (list (%document
(xpath:string-value object
)
694 (or uri instruction-base-uri
)))))))))
696 (defvar *document-order
*)
698 (defun apply-stylesheet
699 (stylesheet source-document
&key output parameters uri-resolver
)
700 (when (typep stylesheet
'xml-designator
)
701 (setf stylesheet
(parse-stylesheet stylesheet
)))
702 (when (typep source-document
'xml-designator
)
703 (setf source-document
(cxml:parse source-document
(stp:make-builder
))))
704 (invoke-with-output-sink
707 (let* ((puri:*strict-parse
* nil
)
708 (*stylesheet
* stylesheet
)
709 (*mode
* (find-mode stylesheet nil
))
710 (*document-order
* (make-hash-table))
711 (*empty-mode
* (make-mode))
712 (global-variable-specs
713 (stylesheet-global-variables stylesheet
))
714 (*global-variable-values
*
715 (make-variable-value-array (length global-variable-specs
)))
716 (*uri-resolver
* uri-resolver
)
718 (make-whitespace-stripper
720 (stylesheet-strip-tests stylesheet
)))
721 (ctx (xpath:make-context xpath-root-node
)))
722 (record-document-order xpath-root-node
)
724 (when (variable-param-p spec
)
726 (find-parameter-value (variable-local-name spec
)
730 (setf (global-variable-value (variable-index spec
))
732 global-variable-specs
)
734 (funcall (variable-thunk spec
) ctx
))
735 global-variable-specs
)
736 #+nil
(print global-variable-specs
)
737 #+nil
(print *global-variable-values
*)
738 (apply-templates ctx
))
739 (xpath:xpath-error
(c)
740 (xslt-error "~A" c
))))
744 ;;; FIXME: this completely negates the benefits of doing whitespace stripping
745 ;;; incrementally. If we need to handle the ordering issues like this, we
746 ;;; should also do whitespace stripping right here.
747 (defun record-document-order (node)
748 (let ((n (hash-table-count *document-order
* )))
749 (labels ((recurse (node)
750 (setf (gethash node
*document-order
*) n
)
754 (xpath-protocol:namespace-pipe node
)))
757 (xpath-protocol:attribute-pipe node
)))
760 (xpath-protocol:child-pipe node
)))))
763 (defun document-order (node)
764 (gethash node
*document-order
*))
766 (defun find-attribute-set (local-name uri
)
767 (or (gethash (cons local-name uri
) (stylesheet-attribute-sets *stylesheet
*))
768 (xslt-error "no such attribute set: ~A/~A" local-name uri
)))
770 (defun apply-templates/list
(list &optional param-bindings sort-predicate
)
772 (setf list
(sort list sort-predicate
)))
773 (let* ((n (length list
))
779 (apply-templates (xpath:make-context child s
/d i
)
782 (defvar *stack-limit
* 200)
784 (defun invoke-with-stack-limit (fn)
785 (let ((*stack-limit
* (1- *stack-limit
*)))
786 (unless (plusp *stack-limit
*)
787 (xslt-error "*stack-limit* reached; stack overflow"))
790 (defun invoke-template (ctx template param-bindings
)
791 (let ((*lexical-variable-values
*
792 (make-variable-value-array (template-n-variables template
))))
795 for
(name-cons value
) in param-bindings
796 for
(nil index nil
) = (find name-cons
797 (template-params template
)
802 (xslt-error "invalid template parameter ~A" name-cons
))
803 (setf (lexical-variable-value index
) value
))
804 (funcall (template-body template
) ctx
))))
806 (defun apply-default-templates (ctx)
807 (let ((node (xpath:context-node ctx
)))
809 ((or (xpath-protocol:node-type-p node
:processing-instruction
)
810 (xpath-protocol:node-type-p node
:comment
)))
811 ((or (xpath-protocol:node-type-p node
:text
)
812 (xpath-protocol:node-type-p node
:attribute
))
813 (write-text (xpath-protocol:string-value node
)))
815 (apply-templates/list
817 (xpath-protocol:child-pipe node
)))))))
819 (defvar *apply-imports
*)
821 (defun apply-applicable-templates (ctx templates param-bindings finally
)
822 (labels ((apply-imports ()
824 (let* ((this (pop templates
))
825 (low (template-apply-imports-limit this
))
826 (high (template-import-priority this
)))
830 (<= low
(template-import-priority x
) high
))
832 (invoke-template ctx this param-bindings
))
834 (let ((*apply-imports
* #'apply-imports
))
837 (defun apply-templates (ctx &optional param-bindings
)
838 (apply-applicable-templates ctx
842 (apply-default-templates ctx
))))
844 (defun call-template (ctx name
&optional param-bindings
)
845 (apply-applicable-templates ctx
846 (find-named-templates name
)
849 (error "cannot find named template: ~s"
852 (defun find-templates (ctx)
853 (let* ((matching-candidates
854 (remove-if-not (lambda (template)
855 (template-matches-p template ctx
))
856 (mode-templates *mode
*)))
858 (if matching-candidates
861 :key
#'template-import-priority
))
863 (priority-groups (make-array npriorities
:initial-element nil
)))
864 (dolist (template matching-candidates
)
866 (elt priority-groups
(template-import-priority template
))))
867 ;;; (print (map 'list #'length priority-groups))
870 for i from
(1- npriorities
) downto
0
871 for group
= (elt priority-groups i
)
872 for template
= (maximize #'template
< group
)
876 (defun find-named-templates (name)
877 (gethash name
(stylesheet-named-templates *stylesheet
*)))
879 (defun template< (a b
) ;assuming same import priority
880 (let ((p (template-priority a
))
881 (q (template-priority b
)))
886 (xslt-cerror "conflicting templates:~_~A,~_~A"
887 (template-match-expression a
)
888 (template-match-expression b
))
889 (< (template-position a
) (template-position b
))))))
891 (defun maximize (< things
)
893 (let ((max (car things
)))
894 (dolist (other (cdr things
))
895 (when (funcall < max other
)
899 (defun template-matches-p (template ctx
)
900 (find (xpath:context-node ctx
)
901 (xpath:all-nodes
(funcall (template-match-thunk template
) ctx
))))
903 (defun invoke-with-output-sink (fn stylesheet output
)
906 (with-open-file (s output
908 :element-type
'(unsigned-byte 8)
909 :if-exists
:rename-and-delete
)
910 (invoke-with-output-sink fn stylesheet s
)))
912 (invoke-with-output-sink fn
914 (make-output-sink stylesheet output
)))
915 ((or hax
:abstract-handler sax
:abstract-handler
)
916 (with-xml-output output
919 (defun make-output-sink (stylesheet stream
)
922 (let ((et (stream-element-type stream
)))
924 ((or (null et
) (subtypep et
'(unsigned-byte 8)))
925 (runes:make-octet-stream-ystream stream
))
926 ((subtypep et
'character
)
927 (runes:make-character-stream-ystream stream
))))
928 (runes:make-rod-ystream
)))
929 (output-spec (stylesheet-output-specification stylesheet
))
930 (omit-xml-declaration-p
931 (equal (output-omit-xml-declaration output-spec
) "yes"))
933 (make-instance 'cxml
::sink
935 :omit-xml-declaration-p omit-xml-declaration-p
)))
936 (if (equalp (output-method (stylesheet-output-specification stylesheet
))
938 (make-instance 'combi-sink
939 :hax-target
(make-instance 'chtml
::sink
941 :sax-target sax-target
942 :encoding
(output-encoding output-spec
))
959 (defun expression-priority (form)
960 (let ((step (second form
)))
961 (if (and (null (cddr form
))
963 (eq :child
(car step
))
965 (let ((name (second step
)))
969 (or (eq (car name
) :qname
)
970 (eq (car name
) :processing-instruction
))))
973 (or (eq (car name
) :namespace
)
980 (defun valid-expression-p (expr)
983 ((eq (first expr
) :path
)
985 (let ((filter (third x
)))
986 (or (null filter
) (valid-expression-p filter
))))
988 ((eq (first expr
) :variable
) ;(!)
991 (every #'valid-expression-p
(cdr expr
)))))
993 (defun parse-pattern (str)
994 ;; zzz check here for anything not allowed as an XSLT pattern
995 ;; zzz can we hack id() and key() here?
996 (let ((form (xpath:parse-xpath str
)))
998 (xslt-error "not a valid pattern: ~A" str
))
999 (mapcar (lambda (case)
1000 (unless (eq (car case
) :path
) ;zzz: filter statt path
1001 (xslt-error "not a valid pattern: ~A" str
))
1002 (unless (valid-expression-p case
)
1003 (xslt-error "invalid filter"))
1005 (if (eq (car form
) :union
)
1009 (defun compile-value-thunk (value env
)
1010 (if (and (listp value
) (eq (car value
) 'progn
))
1011 (let ((inner-thunk (compile-instruction value env
)))
1013 (apply-to-result-tree-fragment ctx inner-thunk
)))
1014 (compile-xpath value env
)))
1016 (defun compile-var-bindings/nointern
(forms env
)
1018 for
(name value
) in forms
1019 collect
(multiple-value-bind (local-name uri
)
1020 (decode-qname name env nil
)
1021 (list (cons local-name uri
)
1022 (compile-value-thunk value env
)))))
1024 (defun compile-var-bindings (forms env
)
1026 for
(cons thunk
) in
(compile-var-bindings/nointern forms env
)
1027 for
(local-name . uri
) = cons
1029 (push-variable local-name
1031 *lexical-variable-declarations
*)
1034 (defun compile-template (<template
> env position
)
1035 (stp:with-attributes
(match name priority mode
) <template
>
1036 (unless (or name match
)
1037 (xslt-error "missing match in template"))
1038 (multiple-value-bind (params body-pos
)
1041 for child in
(stp:list-children
<template
>)
1042 while
(namep child
"param")
1043 collect
(parse-param child
) into params
1044 finally
(return (values params i
)))
1045 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
1046 (param-bindings (compile-var-bindings params env
))
1047 (body (parse-body <template
> body-pos
(mapcar #'car params
)))
1048 (body-thunk (compile-instruction `(progn ,@body
) env
))
1051 ;; set params that weren't initialized by apply-templates
1052 (loop for
(name index param-thunk
) in param-bindings
1053 when
(eq (lexical-variable-value index nil
) 'unbound
)
1054 do
(setf (lexical-variable-value index
)
1055 (funcall param-thunk ctx
)))
1056 (funcall body-thunk ctx
)))
1057 (n-variables (length *lexical-variable-declarations
*)))
1060 (multiple-value-bind (local-name uri
)
1061 (decode-qname name env nil
)
1063 (make-template :name
(cons local-name uri
)
1064 :import-priority
*import-priority
*
1065 :apply-imports-limit
*apply-imports-limit
*
1066 :params param-bindings
1067 :body outer-body-thunk
1068 :n-variables n-variables
))))
1070 (mapcar (lambda (expression)
1074 (:path
(:ancestor-or-self
:node
)
1075 ,@(cdr expression
)))
1078 (parse-number:parse-number priority
)
1079 (expression-priority expression
))))
1080 (make-template :match-expression expression
1081 :match-thunk match-thunk
1082 :import-priority
*import-priority
*
1083 :apply-imports-limit
*apply-imports-limit
*
1087 :params param-bindings
1088 :body outer-body-thunk
1089 :n-variables n-variables
)))
1090 (parse-pattern match
))))))))
1092 (xuriella::parse-stylesheet
#p
"/home/david/src/lisp/xuriella/test.xsl")