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-sys:environment-find-namespace env prefix
)
134 (defmethod xpath-sys: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-sys:environment-find-function
170 ((env xslt-environment
) lname uri
)
172 (or (xpath-sys:find-xpath-function lname
*xsl
*)
173 (xpath-sys:find-xpath-function lname uri
))
174 (xpath-sys:find-xpath-function lname uri
)))
176 (defmethod xpath-sys:environment-find-variable
177 ((env xslt-environment
) lname uri
)
179 (find-variable-index lname uri
*lexical-variable-declarations
*)))
182 (declare (ignore ctx
))
183 (svref *lexical-variable-values
* index
)))))
185 (defclass lexical-xslt-environment
(xslt-environment) ())
187 (defmethod xpath-sys:environment-find-variable
188 ((env lexical-xslt-environment
) lname uri
)
189 (or (call-next-method)
191 (find-variable-index lname uri
*global-variable-declarations
*)))
195 (declare (ignore ctx
))
196 (svref *global-variable-values
* index
))
197 "global ~s (uri ~s) = ~s" lname uri
:result
)))))
199 (defclass global-variable-environment
(xslt-environment)
200 ((initial-global-variable-thunks
201 :initarg
:initial-global-variable-thunks
202 :accessor initial-global-variable-thunks
)))
204 (defmethod xpath-sys:environment-find-variable
205 ((env global-variable-environment
) lname uri
)
206 (or (call-next-method)
207 (gethash (cons lname uri
) (initial-global-variable-thunks env
))))
210 ;;;; TEXT-OUTPUT-SINK
212 ;;;; A sink that serializes only text and will error out on any other
215 (defmacro with-text-output-sink
((var) &body body
)
216 `(invoke-with-text-output-sink (lambda (,var
) ,@body
)))
218 (defclass text-output-sink
(sax:default-handler
)
219 ((target :initarg
:target
:accessor text-output-sink-target
)))
221 (defmethod sax:characters
((sink text-output-sink
) data
)
222 (write-string data
(text-output-sink-target sink
)))
224 (defun invoke-with-text-output-sink (fn)
225 (with-output-to-string (s)
226 (funcall fn
(make-instance 'text-output-sink
:target s
))))
231 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
232 (defvar *xsl
* "http://www.w3.org/1999/XSL/Transform")
233 (defvar *xml
* "http://www.w3.org/XML/1998/namespace")
234 (defvar *html
* "http://www.w3.org/1999/xhtml"))
236 (defun of-name (local-name)
237 (stp:of-name local-name
*xsl
*))
239 (defun namep (node local-name
)
240 (and (typep node
'(or stp
:element stp
:attribute
))
241 (equal (stp:namespace-uri node
) *xsl
*)
242 (equal (stp:local-name node
) local-name
)))
245 ;;;; PARSE-STYLESHEET
247 (defstruct stylesheet
248 (modes (make-hash-table :test
'equal
))
249 (global-variables ())
250 (output-specification (make-output-specification))
252 (named-templates (make-hash-table :test
'equal
))
253 (attribute-sets (make-hash-table :test
'equal
))
254 (keys (make-hash-table :test
'equal
)))
256 (defstruct mode
(templates nil
))
258 (defun find-mode (stylesheet local-name
&optional uri
)
259 (gethash (cons local-name uri
) (stylesheet-modes stylesheet
)))
261 (defun ensure-mode (stylesheet &optional local-name uri
)
262 (or (find-mode stylesheet local-name uri
)
263 (setf (gethash (cons local-name uri
) (stylesheet-modes stylesheet
))
266 (defun ensure-mode/qname
(stylesheet qname env
)
268 (multiple-value-bind (local-name uri
)
269 (decode-qname qname env nil
)
270 (ensure-mode stylesheet local-name uri
))
271 (find-mode stylesheet nil
)))
273 (defun acons-namespaces (element &optional
(bindings *namespaces
*))
274 (map-namespace-declarations (lambda (prefix uri
)
275 (push (cons prefix uri
) bindings
))
279 (defun find-key (name stylesheet
)
280 (or (gethash name
(stylesheet-keys stylesheet
))
281 (xslt-error "unknown key: ~a" name
)))
283 (defun make-key (match use
) (cons match use
))
285 (defun key-match (key) (car key
))
287 (defun key-use (key) (cdr key
))
289 (defun add-key (stylesheet name match use
)
290 (if (gethash name
(stylesheet-keys stylesheet
))
291 (xslt-error "duplicate key: ~a" name
)
292 (setf (gethash name
(stylesheet-keys stylesheet
))
293 (make-key match use
))))
295 (defvar *excluded-namespaces
* (list *xsl
*))
296 (defvar *empty-mode
*)
298 (defvar *xsl-include-stack
* nil
)
300 (defun uri-to-pathname (uri)
301 (cxml::uri-to-pathname
(puri:parse-uri uri
)))
303 (defun parse-stylesheet-to-stp (input uri-resolver
)
304 (let* ((d (cxml:parse input
(make-text-normalizer (cxml-stp:make-builder
))))
305 (<transform
> (stp:document-element d
)))
306 (strip-stylesheet <transform
>)
307 ;; FIXME: handle embedded stylesheets
308 (unless (and (equal (stp:namespace-uri
<transform
>) *xsl
*)
309 (or (equal (stp:local-name
<transform
>) "transform")
310 (equal (stp:local-name
<transform
>) "stylesheet")))
311 (xslt-error "not a stylesheet"))
312 (dolist (include (stp:filter-children
(of-name "include") <transform
>))
313 (let* ((uri (puri:merge-uris
(stp:attribute-value include
"href")
314 (stp:base-uri include
)))
315 (uri (if uri-resolver
316 (funcall uri-resolver
(puri:render-uri uri nil
))
318 (str (puri:render-uri uri nil
))
321 (uri-to-pathname uri
)
322 (cxml:xml-parse-error
(c)
323 (xslt-error "cannot find included stylesheet ~A: ~A"
327 :element-type
'(unsigned-byte 8)
328 :if-does-not-exist nil
)
330 (xslt-error "cannot find included stylesheet ~A at ~A"
332 (when (find str
*xsl-include-stack
* :test
#'equal
)
333 (xslt-error "recursive inclusion of ~A" uri
))
334 (let* ((*xsl-include-stack
* (cons str
*xsl-include-stack
*))
335 (<transform
>2 (parse-stylesheet-to-stp stream uri-resolver
)))
336 (stp:do-children
(child <transform
>2)
337 (stp:insert-child-after
<transform
>
340 (stp:detach include
)))))
343 (defvar *instruction-base-uri
*)
344 (defvar *apply-imports-limit
*)
345 (defvar *import-priority
*)
347 (defun parse-1-stylesheet (env stylesheet designator uri-resolver
)
348 (let* ((<transform
> (parse-stylesheet-to-stp designator uri-resolver
))
349 (*instruction-base-uri
* (stp:base-uri
<transform
>))
350 (*namespaces
* (acons-namespaces <transform
>))
351 (*apply-imports-limit
* (1+ *import-priority
*)))
352 (dolist (import (stp:filter-children
(of-name "import") <transform
>))
353 (let ((uri (puri:merge-uris
(stp:attribute-value import
"href")
354 (stp:base-uri import
))))
355 (parse-imported-stylesheet env stylesheet uri uri-resolver
)))
356 (incf *import-priority
*)
357 (parse-exclude-result-prefixes! <transform
> env
)
358 (parse-global-variables! stylesheet
<transform
>)
359 (parse-keys! stylesheet
<transform
> env
)
360 (parse-templates! stylesheet
<transform
> env
)
361 (parse-output! stylesheet
<transform
>)
362 (parse-strip/preserve-space
! stylesheet
<transform
> env
)
363 (parse-attribute-sets! stylesheet
<transform
> env
)))
365 (defvar *xsl-import-stack
* nil
)
367 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver
)
368 (let* ((uri (if uri-resolver
369 (funcall uri-resolver
(puri:render-uri uri nil
))
371 (str (puri:render-uri uri nil
))
374 (uri-to-pathname uri
)
375 (cxml:xml-parse-error
(c)
376 (xslt-error "cannot find imported stylesheet ~A: ~A"
380 :element-type
'(unsigned-byte 8)
381 :if-does-not-exist nil
)
383 (xslt-error "cannot find imported stylesheet ~A at ~A"
385 (when (find str
*xsl-import-stack
* :test
#'equal
)
386 (xslt-error "recursive inclusion of ~A" uri
))
387 (let ((*xsl-import-stack
* (cons str
*xsl-import-stack
*)))
388 (parse-1-stylesheet env stylesheet stream uri-resolver
)))))
390 (defun parse-stylesheet (designator &key uri-resolver
)
391 (let* ((*import-priority
* 0)
392 (puri:*strict-parse
* nil
)
393 (stylesheet (make-stylesheet))
394 (env (make-instance 'lexical-xslt-environment
))
395 (*excluded-namespaces
* *excluded-namespaces
*)
396 (*global-variable-declarations
* (make-empty-declaration-array)))
397 (ensure-mode stylesheet nil
)
398 (parse-1-stylesheet env stylesheet designator uri-resolver
)
399 ;; reverse attribute sets:
400 (let ((table (stylesheet-attribute-sets stylesheet
)))
401 (maphash (lambda (k v
)
402 (setf (gethash k table
) (nreverse v
)))
406 (defun parse-attribute-sets! (stylesheet <transform
> env
)
407 (dolist (elt (stp:filter-children
(of-name "attribute-set") <transform
>))
409 (mapcar (lambda (qname)
410 (multiple-value-list (decode-qname qname env nil
)))
412 (stp:attribute-value elt
"use-attribute-sets"))))
414 (stp:map-children
'list
#'parse-instruction elt
))
415 (*lexical-variable-declarations
*
416 (make-empty-declaration-array))
418 (compile-instruction `(progn ,@instructions
) env
))
419 (n-variables (length *lexical-variable-declarations
*)))
422 (loop for
(local-name uri nil
) in sets do
423 (dolist (thunk (find-attribute-set local-name uri
))
424 (funcall thunk ctx
)))
425 (let ((*lexical-variable-values
*
426 (make-variable-value-array n-variables
)))
427 (funcall thunk ctx
)))))
428 (gethash (multiple-value-bind (local-name uri
)
429 (decode-qname (stp:attribute-value elt
"name") env nil
)
430 (cons local-name uri
))
431 (stylesheet-attribute-sets stylesheet
)))))
433 (defun parse-exclude-result-prefixes! (<transform
> env
)
434 (stp:with-attributes
(exclude-result-prefixes) <transform
>
435 (dolist (prefix (words (or exclude-result-prefixes
"")))
436 (when (equal prefix
"#default")
438 (push (or (xpath-sys:environment-find-namespace env prefix
)
439 (xslt-error "namespace not found: ~A" prefix
))
440 *excluded-namespaces
*))))
442 (defun parse-strip/preserve-space
! (stylesheet <transform
> env
)
443 (xpath:with-namespaces
((nil #.
*xsl
*))
444 (dolist (elt (stp:filter-children
(lambda (x)
445 (or (namep x
"strip-space")
446 (namep x
"preserve-space")))
448 (let ((*namespaces
* (acons-namespaces elt
))
450 (if (equal (stp:local-name elt
) "strip-space")
453 (dolist (name-test (words (stp:attribute-value elt
"elements")))
454 (let* ((pos (search ":*" name-test
))
457 ((eql pos
(- (length name-test
) 2))
458 (let* ((prefix (subseq name-test
0 pos
))
460 (xpath-sys:environment-find-namespace env prefix
)))
461 (unless (xpath::nc-name-p prefix
)
462 (xslt-error "not an NCName: ~A" prefix
))
463 (lambda (local-name uri
)
464 (declare (ignore local-name
))
465 (if (equal uri name-test-uri
)
468 ((equal name-test
"*")
469 (lambda (local-name uri
)
470 (declare (ignore local-name uri
))
473 (multiple-value-bind (name-test-local-name name-test-uri
)
474 (decode-qname name-test env nil
)
475 (lambda (local-name uri
)
476 (if (and (equal local-name name-test-local-name
)
477 (equal uri name-test-uri
))
480 (push test-function
(stylesheet-strip-tests stylesheet
))))))))
482 (defstruct (output-specification
483 (:conc-name
"OUTPUT-"))
489 (defun parse-output! (stylesheet <transform
>)
490 (let ((outputs (stp:filter-children
(of-name "output") <transform
>)))
494 ;; - concatenate cdata-section-elements
495 ;; - the others must not conflict
496 (error "oops, merging of output elements not supported yet"))
497 (let ((<output
> (car outputs
))
498 (spec (stylesheet-output-specification stylesheet
)))
499 (stp:with-attributes
(;; version
508 ;;; cdata-section-elements
511 (setf (output-method spec
) method
)
512 (setf (output-indent spec
) indent
)
513 (setf (output-encoding spec
) encoding
)
514 (setf (output-omit-xml-declaration spec
) omit-xml-declaration
))))))
516 (defun make-empty-declaration-array ()
517 (make-array 1 :fill-pointer
0 :adjustable t
))
519 (defun make-variable-value-array (n-lexical-variables)
520 (make-array n-lexical-variables
:initial-element
'unbound
))
522 (defun compile-global-variable (<variable
> env
) ;; also for <param>
523 (stp:with-attributes
(name select
) <variable
>
524 (when (and select
(stp:list-children
<variable
>))
525 (xslt-error "variable with select and body"))
526 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
529 (compile-xpath select env
))
530 ((stp:list-children
<variable
>)
531 (let* ((inner-sexpr `(progn ,@(parse-body <variable
>)))
532 (inner-thunk (compile-instruction inner-sexpr env
)))
534 (apply-to-result-tree-fragment ctx inner-thunk
))))
537 (declare (ignore ctx
))
539 (n-lexical-variables (length *lexical-variable-declarations
*)))
542 (let* ((*lexical-variable-values
*
543 (make-variable-value-array n-lexical-variables
)))
544 (funcall inner ctx
)))
545 "global ~s (~s) = ~s" name select
:result
))))
547 (defstruct (variable-information
548 (:constructor make-variable
)
549 (:conc-name
"VARIABLE-"))
557 (defun parse-global-variable! (<variable
> global-env
) ;; also for <param>
558 (let ((*namespaces
* (acons-namespaces <variable
>))
559 (qname (stp:attribute-value
<variable
> "name")))
561 (xslt-error "name missing in ~A" (stp:local-name
<variable
>)))
562 (multiple-value-bind (local-name uri
)
563 (decode-qname qname global-env nil
)
564 ;; For the normal compilation environment of templates, install it
565 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
566 (let ((index (intern-global-variable local-name uri
)))
567 ;; For the evaluation of a global variable itself, build a thunk
568 ;; that lazily resolves other variables, stored into
569 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
570 (let* ((value-thunk :unknown
)
571 (global-variable-thunk
573 (let ((v (global-variable-value index nil
)))
575 (xslt-error "recursive variable definition"))
578 ;; (print (list :computing index))
579 (setf (global-variable-value index
) 'seen
)
580 (setf (global-variable-value index
)
581 (funcall value-thunk ctx
))
582 #+nil
(print (list :done-computing index
583 (global-variable-value index
)))
584 #+nil
(global-variable-value index
))
586 #+nil
(print (list :have
592 (compile-global-variable <variable
> global-env
)))))
593 (setf (gethash (cons local-name uri
)
594 (initial-global-variable-thunks global-env
))
595 global-variable-thunk
)
596 (make-variable :index index
597 :local-name local-name
599 :thunk global-variable-thunk
600 :param-p
(namep <variable
> "param")
601 :thunk-setter thunk-setter
))))))
603 (defun parse-keys! (stylesheet <transform
> env
)
604 (xpath:with-namespaces
((nil #.
*xsl
*))
606 (<key
> (xpath:evaluate
"key" <transform
>))
607 (stp:with-attributes
(name match use
) <key
>
608 (unless name
(xslt-error "key name attribute not specified"))
609 (unless match
(xslt-error "key match attribute not specified"))
610 (unless use
(xslt-error "key use attribute not specified"))
611 (add-key stylesheet name
612 (compile-xpath `(xpath:xpath
,(parse-key-pattern match
)) env
)
613 (compile-xpath use env
))))))
615 (defun parse-global-variables! (stylesheet <transform
>)
616 (xpath:with-namespaces
((nil #.
*xsl
*))
617 (let* ((table (make-hash-table :test
'equal
))
618 (global-env (make-instance 'global-variable-environment
619 :initial-global-variable-thunks table
))
622 (<variable
> (xpath:evaluate
"variable|param" <transform
>))
623 (let ((var (parse-global-variable! <variable
> global-env
)))
624 (xslt-trace "parsing global variable ~s (uri ~s)"
625 (variable-local-name var
)
630 (and (equal (variable-local-name a
)
631 (variable-local-name b
))
632 (equal (variable-uri a
)
634 (xslt-error "duplicate definition for global variable ~A"
635 (variable-local-name var
)))
637 ;; now that the global environment knows about all variables, run the
638 ;; thunk setters to perform their compilation
639 (setf specs
(nreverse specs
))
640 (mapc (lambda (spec) (funcall (variable-thunk-setter spec
))) specs
)
641 (setf (stylesheet-global-variables stylesheet
) specs
))))
643 (defun parse-templates! (stylesheet <transform
> env
)
645 (dolist (<template
> (stp:filter-children
(of-name "template") <transform
>))
646 (let ((*namespaces
* (acons-namespaces <template
>)))
647 (dolist (template (compile-template <template
> env i
))
648 (let ((name (template-name template
)))
650 (let* ((table (stylesheet-named-templates stylesheet
))
651 (head (car (gethash name table
))))
652 (when (and head
(eql (template-import-priority head
)
653 (template-import-priority template
)))
654 ;; fixme: is this supposed to be a run-time error?
655 (xslt-error "conflicting templates for ~A" name
))
656 (push template
(gethash name table
)))
657 (let ((mode (ensure-mode/qname stylesheet
658 (template-mode-qname template
)
660 (setf (template-mode template
) mode
)
661 (push template
(mode-templates mode
)))))))
665 ;;;; APPLY-STYLESHEET
667 (defvar *stylesheet
*)
670 (deftype xml-designator
() '(or runes
:xstream runes
:rod array stream pathname
))
672 (defstruct (parameter
673 (:constructor make-parameter
(value local-name
&optional uri
)))
678 (defun find-parameter-value (local-name uri parameters
)
679 (dolist (p parameters
)
680 (when (and (equal (parameter-local-name p
) local-name
)
681 (equal (parameter-uri p
) uri
))
682 (return (parameter-value p
)))))
684 (defvar *uri-resolver
*)
686 (defun parse-allowing-microsoft-bom (pathname handler
)
687 (with-open-file (s pathname
:element-type
'(unsigned-byte 8))
688 (unless (and (eql (read-byte s nil
) #xef
)
689 (eql (read-byte s nil
) #xbb
)
690 (eql (read-byte s nil
) #xbf
))
692 (cxml:parse s handler
)))
694 (defun %document
(uri-string base-uri
)
696 (puri:merge-uris uri-string base-uri
))
699 (funcall *uri-resolver
* (puri:render-uri absolute-uri nil
))
703 (uri-to-pathname resolved-uri
)
704 (cxml:xml-parse-error
(c)
705 (xslt-error "cannot find referenced document ~A: ~A"
709 (parse-allowing-microsoft-bom pathname
(stp:make-builder
))
710 ((or file-error cxml
:xml-parse-error
) (c)
711 (xslt-error "cannot parse referenced document ~A: ~A"
714 (make-whitespace-stripper document
715 (stylesheet-strip-tests *stylesheet
*))))
716 (when (puri:uri-fragment absolute-uri
)
717 (xslt-error "use of fragment identifiers in document() not supported"))
720 (xpath-sys:define-extension xslt
*xsl
*)
722 (xpath-sys:define-xpath-function
/lazy
724 (object &optional node-set
)
725 (let ((instruction-base-uri *instruction-base-uri
*))
727 (let* ((object (funcall object ctx
))
728 (node-set (and node-set
(funcall node-set ctx
)))
731 ;; FIXME: should use first node of the node set
732 ;; _in document order_
733 (xpath-protocol:base-uri
(xpath:first-node node-set
)))))
734 (xpath-sys:make-node-set
735 (if (xpath:node-set-p object
)
736 (xpath:map-node-set-
>list
738 (%document
(xpath:string-value node
)
739 (or uri
(xpath-protocol:base-uri node
))))
741 (list (%document
(xpath:string-value object
)
742 (or uri instruction-base-uri
)))))))))
744 (xpath-sys:define-xpath-function
/eager xslt
:key
(name object
)
745 (let ((key (find-key (xpath:string-value name
) *stylesheet
*)))
746 (labels ((get-by-key (value)
747 (let ((value (xpath:string-value value
)))
750 (equal value
(xpath:string-value
751 (xpath:evaluate-compiled
752 (key-use key
) node
))))
754 (xpath:node-set-value
755 (xpath:evaluate-compiled
756 (key-match key
) xpath
:context
)))))))
757 (xpath-sys:make-node-set
759 (if (xpath:node-set-p object
)
760 (xpath::mappend-pipe
#'get-by-key
(xpath-sys:pipe-of object
))
761 (get-by-key object
)))))))
763 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
765 (xpath-sys:define-xpath-function
/lazy xslt
:current
()
767 (xpath-sys:make-node-set
769 (xpath:context-starting-node ctx
)
772 (xpath-sys:define-xpath-function
/lazy xslt
:generate-id
(&optional node-set-thunk
)
775 (xpath-sys:get-node-id
(xpath:node-set-value
(funcall node-set-thunk ctx
))))
777 (xpath-sys:get-node-id
(xpath:context-node ctx
)))))
779 (defun apply-stylesheet
780 (stylesheet source-document
781 &key output parameters uri-resolver navigator
)
782 (when (typep stylesheet
'xml-designator
)
783 (setf stylesheet
(parse-stylesheet stylesheet
)))
784 (when (typep source-document
'xml-designator
)
785 (setf source-document
(cxml:parse source-document
(stp:make-builder
))))
786 (invoke-with-output-sink
789 (let* ((xpath:*navigator
* (or navigator
:default-navigator
))
790 (puri:*strict-parse
* nil
)
791 (*stylesheet
* stylesheet
)
792 (*mode
* (find-mode stylesheet nil
))
793 (*empty-mode
* (make-mode))
794 (global-variable-specs
795 (stylesheet-global-variables stylesheet
))
796 (*global-variable-values
*
797 (make-variable-value-array (length global-variable-specs
)))
798 (*uri-resolver
* uri-resolver
)
800 (make-whitespace-stripper
802 (stylesheet-strip-tests stylesheet
)))
803 (ctx (xpath:make-context xpath-root-node
)))
805 (when (variable-param-p spec
)
807 (find-parameter-value (variable-local-name spec
)
811 (setf (global-variable-value (variable-index spec
))
813 global-variable-specs
)
815 (funcall (variable-thunk spec
) ctx
))
816 global-variable-specs
)
817 #+nil
(print global-variable-specs
)
818 #+nil
(print *global-variable-values
*)
819 (apply-templates ctx
))
820 (xpath:xpath-error
(c)
821 (xslt-error "~A" c
))))
825 (defun find-attribute-set (local-name uri
)
826 (or (gethash (cons local-name uri
) (stylesheet-attribute-sets *stylesheet
*))
827 (xslt-error "no such attribute set: ~A/~A" local-name uri
)))
829 (defun apply-templates/list
(list &optional param-bindings sort-predicate
)
831 (setf list
(sort list sort-predicate
)))
832 (let* ((n (length list
))
838 (apply-templates (xpath:make-context child s
/d i
)
841 (defvar *stack-limit
* 200)
843 (defun invoke-with-stack-limit (fn)
844 (let ((*stack-limit
* (1- *stack-limit
*)))
845 (unless (plusp *stack-limit
*)
846 (xslt-error "*stack-limit* reached; stack overflow"))
849 (defun invoke-template (ctx template param-bindings
)
850 (let ((*lexical-variable-values
*
851 (make-variable-value-array (template-n-variables template
))))
854 for
(name-cons value
) in param-bindings
855 for
(nil index nil
) = (find name-cons
856 (template-params template
)
861 (xslt-error "invalid template parameter ~A" name-cons
))
862 (setf (lexical-variable-value index
) value
))
863 (funcall (template-body template
) ctx
))))
865 (defun apply-default-templates (ctx)
866 (let ((node (xpath:context-node ctx
)))
868 ((or (xpath-protocol:node-type-p node
:processing-instruction
)
869 (xpath-protocol:node-type-p node
:comment
)))
870 ((or (xpath-protocol:node-type-p node
:text
)
871 (xpath-protocol:node-type-p node
:attribute
))
872 (write-text (xpath-protocol:node-text node
)))
874 (apply-templates/list
876 (xpath-protocol:child-pipe node
)))))))
878 (defvar *apply-imports
*)
880 (defun apply-applicable-templates (ctx templates param-bindings finally
)
881 (labels ((apply-imports ()
883 (let* ((this (pop templates
))
884 (low (template-apply-imports-limit this
))
885 (high (template-import-priority this
)))
889 (<= low
(template-import-priority x
) high
))
891 (invoke-template ctx this param-bindings
))
893 (let ((*apply-imports
* #'apply-imports
))
896 (defun apply-templates (ctx &optional param-bindings
)
897 (apply-applicable-templates ctx
901 (apply-default-templates ctx
))))
903 (defun call-template (ctx name
&optional param-bindings
)
904 (apply-applicable-templates ctx
905 (find-named-templates name
)
908 (error "cannot find named template: ~s"
911 (defun find-templates (ctx)
912 (let* ((matching-candidates
913 (remove-if-not (lambda (template)
914 (template-matches-p template ctx
))
915 (mode-templates *mode
*)))
917 (if matching-candidates
920 :key
#'template-import-priority
))
922 (priority-groups (make-array npriorities
:initial-element nil
)))
923 (dolist (template matching-candidates
)
925 (elt priority-groups
(template-import-priority template
))))
926 ;;; (print (map 'list #'length priority-groups))
929 for i from
(1- npriorities
) downto
0
930 for group
= (elt priority-groups i
)
931 for template
= (maximize #'template
< group
)
935 (defun find-named-templates (name)
936 (gethash name
(stylesheet-named-templates *stylesheet
*)))
938 (defun template< (a b
) ;assuming same import priority
939 (let ((p (template-priority a
))
940 (q (template-priority b
)))
945 (xslt-cerror "conflicting templates:~_~A,~_~A"
946 (template-match-expression a
)
947 (template-match-expression b
))
948 (< (template-position a
) (template-position b
))))))
950 (defun maximize (< things
)
952 (let ((max (car things
)))
953 (dolist (other (cdr things
))
954 (when (funcall < max other
)
958 (defun template-matches-p (template ctx
)
959 (find (xpath:context-node ctx
)
960 (xpath:all-nodes
(funcall (template-match-thunk template
) ctx
))))
962 (defun invoke-with-output-sink (fn stylesheet output
)
965 (with-open-file (s output
967 :element-type
'(unsigned-byte 8)
968 :if-exists
:rename-and-delete
)
969 (invoke-with-output-sink fn stylesheet s
)))
971 (invoke-with-output-sink fn
973 (make-output-sink stylesheet output
)))
974 ((or hax
:abstract-handler sax
:abstract-handler
)
975 (with-xml-output output
978 (defun make-output-sink (stylesheet stream
)
981 (let ((et (stream-element-type stream
)))
983 ((or (null et
) (subtypep et
'(unsigned-byte 8)))
984 (runes:make-octet-stream-ystream stream
))
985 ((subtypep et
'character
)
986 (runes:make-character-stream-ystream stream
))))
987 (runes:make-rod-ystream
)))
988 (output-spec (stylesheet-output-specification stylesheet
))
989 (omit-xml-declaration-p
990 (equal (output-omit-xml-declaration output-spec
) "yes"))
992 (make-instance 'cxml
::sink
994 :omit-xml-declaration-p omit-xml-declaration-p
)))
995 (if (equalp (output-method (stylesheet-output-specification stylesheet
))
997 (make-instance 'combi-sink
998 :hax-target
(make-instance 'chtml
::sink
1000 :sax-target sax-target
1001 :encoding
(output-encoding output-spec
))
1018 (defun expression-priority (form)
1019 (let ((step (second form
)))
1020 (if (and (null (cddr form
))
1022 (eq :child
(car step
))
1024 (let ((name (second step
)))
1028 (or (eq (car name
) :qname
)
1029 (eq (car name
) :processing-instruction
))))
1032 (or (eq (car name
) :namespace
)
1033 (eq (car name
) '*)))
1039 (defun valid-expression-p (expr)
1042 ((eq (first expr
) :path
)
1044 (let ((filter (third x
)))
1045 (or (null filter
) (valid-expression-p filter
))))
1047 ((eq (first expr
) :variable
) ;(!)
1050 (every #'valid-expression-p
(cdr expr
)))))
1052 (defun parse-xpath (str)
1054 (xpath:parse-xpath str
)
1055 (xpath:xpath-error
(c)
1056 (xslt-error "~A" c
))))
1058 (defun parse-key-pattern (str)
1060 (mapcar #'(lambda (item)
1061 `(:path
(:root
:node
)
1062 (:descendant-or-self
*)
1064 (parse-pattern str
))))
1065 (if (null (rest parsed
))
1067 `(:union
,@parsed
))))
1069 (defun parse-pattern (str)
1070 ;; zzz check here for anything not allowed as an XSLT pattern
1071 ;; zzz can we hack id() and key() here?
1072 (let ((form (parse-xpath str
)))
1073 (unless (consp form
)
1074 (xslt-error "not a valid pattern: ~A" str
))
1075 (labels ((process-form (form)
1076 (cond ((eq (car form
) :union
)
1077 (alexandria:mappend
#'process-form
(rest form
)))
1078 ((not (eq (car form
) :path
)) ;zzz: filter statt path
1079 (xslt-error "not a valid pattern: ~A" str
))
1080 ((not (valid-expression-p form
))
1081 (xslt-error "invalid filter"))
1083 (process-form form
))))
1085 (defun compile-value-thunk (value env
)
1086 (if (and (listp value
) (eq (car value
) 'progn
))
1087 (let ((inner-thunk (compile-instruction value env
)))
1089 (apply-to-result-tree-fragment ctx inner-thunk
)))
1090 (compile-xpath value env
)))
1092 (defun compile-var-bindings/nointern
(forms env
)
1094 for
(name value
) in forms
1095 collect
(multiple-value-bind (local-name uri
)
1096 (decode-qname name env nil
)
1097 (list (cons local-name uri
)
1099 (compile-value-thunk value env
)
1100 "local variable ~s = ~s" name
:result
)))))
1102 (defun compile-var-bindings (forms env
)
1104 for
(cons thunk
) in
(compile-var-bindings/nointern forms env
)
1105 for
(local-name . uri
) = cons
1107 (push-variable local-name
1109 *lexical-variable-declarations
*)
1112 (defun compile-template (<template
> env position
)
1113 (stp:with-attributes
(match name priority mode
) <template
>
1114 (unless (or name match
)
1115 (xslt-error "missing match in template"))
1116 (multiple-value-bind (params body-pos
)
1119 for child in
(stp:list-children
<template
>)
1120 while
(namep child
"param")
1121 collect
(parse-param child
) into params
1122 finally
(return (values params i
)))
1123 (let* ((*lexical-variable-declarations
* (make-empty-declaration-array))
1124 (param-bindings (compile-var-bindings params env
))
1125 (body (parse-body <template
> body-pos
(mapcar #'car params
)))
1126 (body-thunk (compile-instruction `(progn ,@body
) env
))
1132 ;; set params that weren't initialized by apply-templates
1133 (loop for
(name index param-thunk
) in param-bindings
1134 when
(eq (lexical-variable-value index nil
) 'unbound
)
1135 do
(setf (lexical-variable-value index
)
1136 (funcall param-thunk ctx
)))
1137 (funcall body-thunk ctx
))))
1138 "template: match = ~s name = ~s" match name
))
1139 (n-variables (length *lexical-variable-declarations
*)))
1142 (multiple-value-bind (local-name uri
)
1143 (decode-qname name env nil
)
1145 (make-template :name
(cons local-name uri
)
1146 :import-priority
*import-priority
*
1147 :apply-imports-limit
*apply-imports-limit
*
1148 :params param-bindings
1149 :body outer-body-thunk
1150 :n-variables n-variables
))))
1152 (mapcar (lambda (expression)
1157 (:path
(:ancestor-or-self
:node
)
1158 ,@(cdr expression
)))
1160 "match-thunk for template (match ~s): ~s --> ~s"
1161 match expression
:result
))
1163 (parse-number:parse-number priority
)
1164 (expression-priority expression
))))
1165 (make-template :match-expression expression
1166 :match-thunk match-thunk
1167 :import-priority
*import-priority
*
1168 :apply-imports-limit
*apply-imports-limit
*
1172 :params param-bindings
1173 :body outer-body-thunk
1174 :n-variables n-variables
)))
1175 (parse-pattern match
))))))))
1177 (xuriella::parse-stylesheet
#p
"/home/david/src/lisp/xuriella/test.xsl")