Disallow variables in key use
[xuriella.git] / xslt.lisp
blob0060b2e057224db89740ed0bf77e8bb6d20ae89f
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
8 ;;; are met:
9 ;;;
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
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.
17 ;;;
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella)
32 #+sbcl
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"))
42 ;;;; XSLT-ERROR
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 ;; Many errors in XSLT are "recoverable", with a specified action that must
56 ;; be taken if the error isn't raised. My original plan was to implement
57 ;; such issues as continuable conditions, so that users are alerted about
58 ;; portability issues with their stylesheet, but can contiue anyway.
60 ;; However, our current test suite driver compares against Saxon results,
61 ;; and Saxon recovers (nearly) always. So our coverage of these errors
62 ;; is very incomplete.
64 ;; Re-enable this code once we can check that it's actually being used
65 ;; everywhere.
66 (defun xslt-cerror (fmt &rest args)
67 (declare (ignore fmt args))
68 #+(or)
69 (with-simple-restart (recover "recover")
70 (error 'recoverable-xslt-error
71 :format-control fmt
72 :format-arguments args)))
74 (defvar *debug* nil)
76 (defmacro handler-case* (form &rest clauses)
77 ;; like HANDLER-CASE if *DEBUG* is off. If it's on, don't establish
78 ;; a handler at all so that we see the real stack traces. (We could use
79 ;; HANDLER-BIND here and check at signalling time, but doesn't seem
80 ;; important.)
81 (let ((doit (gensym)))
82 `(flet ((,doit () ,form))
83 (if *debug*
84 (,doit)
85 (handler-case
86 (,doit)
87 ,@clauses)))))
89 (defmacro with-resignalled-errors ((&optional) &body body)
90 `(invoke-with-resignalled-errors (lambda () ,@body)))
92 (defun invoke-with-resignalled-errors (fn)
93 (handler-bind
94 ((xpath:xpath-error
95 (lambda (c)
96 (xslt-error "~A" c)))
97 (babel-encodings:character-encoding-error
98 (lambda (c)
99 (xslt-error "~A" c))))
100 (funcall fn)))
102 (defun compile-xpath (xpath &optional env)
103 (with-resignalled-errors ()
104 (xpath:compile-xpath xpath env)))
106 (defmacro with-stack-limit ((&optional) &body body)
107 `(invoke-with-stack-limit (lambda () ,@body)))
109 (defparameter *without-xslt-current-p* nil)
111 (defmacro without-xslt-current ((&optional) &body body)
112 `(invoke-without-xslt-current (lambda () ,@body)))
114 (defun invoke-without-xslt-current (fn)
115 (let ((*without-xslt-current-p* t))
116 (funcall fn)))
118 ;;; (defun invoke-without-xslt-current (fn)
119 ;;; (let ((non-extensions (gethash "" xpath::*extensions*))
120 ;;; (xpath::*extensions*
121 ;;; ;; hide XSLT extensions
122 ;;; (make-hash-table :test #'equal)))
123 ;;; (setf (gethash "" xpath::*extensions*) non-extensions)
124 ;;; (funcall fn)))
127 ;;;; Helper functions and macros
129 (defun check-for-invalid-attributes (valid-names node)
130 (labels ((check-attribute (a)
131 (unless
132 (let ((uri (stp:namespace-uri a)))
133 (or (and (plusp (length uri)) (not (equal uri *xsl*)))
134 (find (cons (stp:local-name a) uri)
135 valid-names
136 :test #'equal)))
137 (xslt-error "attribute ~A not allowed on ~A"
138 (stp:local-name a)
139 (stp:local-name node)))))
140 (stp:map-attributes nil #'check-attribute node)))
142 (defmacro only-with-attributes ((&rest specs) node &body body)
143 (let ((valid-names
144 (mapcar (lambda (entry)
145 (if (and (listp entry) (cdr entry))
146 (destructuring-bind (name &optional (uri ""))
147 (cdr entry)
148 (cons name uri))
149 (cons (string-downcase
150 (princ-to-string
151 (symbol-name entry)))
152 "")))
153 specs))
154 (%node (gensym)))
155 `(let ((,%NODE ,node))
156 (check-for-invalid-attributes ',valid-names ,%NODE)
157 (stp:with-attributes ,specs ,%NODE
158 ,@body))))
160 (defun map-pipe-eagerly (fn pipe)
161 (xpath::enumerate pipe :key fn :result nil))
163 (defmacro do-pipe ((var pipe &optional result) &body body)
164 `(block nil
165 (map-pipe-eagerly #'(lambda (,var) ,@body) ,pipe)
166 ,result))
169 ;;;; XSLT-ENVIRONMENT and XSLT-CONTEXT
171 (defparameter *namespaces*
172 '((nil . "")
173 ("xmlns" . #"http://www.w3.org/2000/xmlns/")
174 ("xml" . #"http://www.w3.org/XML/1998/namespace")))
176 (defvar *global-variable-declarations*)
177 (defvar *lexical-variable-declarations*)
179 (defvar *global-variable-values*)
180 (defvar *lexical-variable-values*)
182 (defclass xslt-environment () ())
184 (defun split-qname (str)
185 (handler-case
186 (multiple-value-bind (prefix local-name)
187 (cxml::split-qname str)
188 (unless
189 ;; FIXME: cxml should really offer a function that does
190 ;; checks for NCName and QName in a sensible way for user code.
191 ;; cxml::split-qname is tailored to the needs of the parser.
193 ;; For now, let's just check the syntax explicitly.
194 (and (or (null prefix) (xpath::nc-name-p prefix))
195 (xpath::nc-name-p local-name))
196 (xslt-error "not a qname: ~A" str))
197 (values prefix local-name))
198 (cxml:well-formedness-violation ()
199 (xslt-error "not a qname: ~A" str))))
201 (defun decode-qname (qname env attributep)
202 (unless qname
203 (xslt-error "missing name"))
204 (multiple-value-bind (prefix local-name)
205 (split-qname qname)
206 (values local-name
207 (if (or prefix (not attributep))
208 (xpath-sys:environment-find-namespace env (or prefix ""))
210 prefix)))
212 (defmethod xpath-sys:environment-find-namespace ((env xslt-environment) prefix)
213 (or (cdr (assoc prefix *namespaces* :test 'equal))
214 ;; zzz gross hack.
215 ;; Change the entire code base to represent "no prefix" as the
216 ;; empty string consistently. unparse.lisp has already been changed.
217 (and (equal prefix "")
218 (cdr (assoc nil *namespaces* :test 'equal)))
219 (and (eql prefix nil)
220 (cdr (assoc "" *namespaces* :test 'equal)))))
222 (defun find-variable-index (local-name uri table)
223 (position (cons local-name uri) table :test 'equal))
225 (defun intern-global-variable (local-name uri)
226 (or (find-variable-index local-name uri *global-variable-declarations*)
227 (push-variable local-name uri *global-variable-declarations*)))
229 (defun push-variable (local-name uri table)
230 (prog1
231 (length table)
232 (vector-push-extend (cons local-name uri) table)))
234 (defun lexical-variable-value (index &optional (errorp t))
235 (let ((result (svref *lexical-variable-values* index)))
236 (when errorp
237 (assert (not (eq result 'unbound))))
238 result))
240 (defun (setf lexical-variable-value) (newval index)
241 (assert (not (eq newval 'unbound)))
242 (setf (svref *lexical-variable-values* index) newval))
244 (defun global-variable-value (index &optional (errorp t))
245 (let ((result (svref *global-variable-values* index)))
246 (when errorp
247 (assert (not (eq result 'unbound))))
248 result))
250 (defun (setf global-variable-value) (newval index)
251 (assert (not (eq newval 'unbound)))
252 (setf (svref *global-variable-values* index) newval))
254 (defmethod xpath-sys:environment-find-function
255 ((env xslt-environment) lname uri)
256 (if (string= uri "")
257 (or (xpath-sys:find-xpath-function lname *xsl*)
258 (xpath-sys:find-xpath-function lname uri))
259 (xpath-sys:find-xpath-function lname uri)))
261 (defmethod xpath-sys:environment-find-variable
262 ((env xslt-environment) lname uri)
263 (let ((index
264 (find-variable-index lname uri *lexical-variable-declarations*)))
265 (when index
266 (lambda (ctx)
267 (declare (ignore ctx))
268 (svref *lexical-variable-values* index)))))
270 (defclass lexical-xslt-environment (xslt-environment) ())
272 (defmethod xpath-sys:environment-find-variable
273 ((env lexical-xslt-environment) lname uri)
274 (or (call-next-method)
275 (let ((index
276 (find-variable-index lname uri *global-variable-declarations*)))
277 (when index
278 (xslt-trace-thunk
279 (lambda (ctx)
280 (declare (ignore ctx))
281 (svref *global-variable-values* index))
282 "global ~s (uri ~s) = ~s" lname uri :result)))))
284 (defclass key-environment (xslt-environment) ())
286 (defmethod xpath-sys:environment-find-variable
287 ((env key-environment) lname uri)
288 (declare (ignore lname uri))
289 (xslt-error "disallowed variable reference"))
291 (defclass global-variable-environment (xslt-environment)
292 ((initial-global-variable-thunks
293 :initarg :initial-global-variable-thunks
294 :accessor initial-global-variable-thunks)))
296 (defmethod xpath-sys:environment-find-variable
297 ((env global-variable-environment) lname uri)
298 (or (call-next-method)
299 (gethash (cons lname uri) (initial-global-variable-thunks env))))
302 ;;;; TOPLEVEL-TEXT-OUTPUT-SINK
303 ;;;;
304 ;;;; A sink that serializes only text not contained in any element.
306 (defmacro with-toplevel-text-output-sink ((var) &body body)
307 `(invoke-with-toplevel-text-output-sink (lambda (,var) ,@body)))
309 (defclass toplevel-text-output-sink (sax:default-handler)
310 ((target :initarg :target :accessor text-output-sink-target)
311 (depth :initform 0 :accessor textoutput-sink-depth)))
313 (defmethod sax:start-element ((sink toplevel-text-output-sink)
314 namespace-uri local-name qname attributes)
315 (declare (ignore namespace-uri local-name qname attributes))
316 (incf (textoutput-sink-depth sink)))
318 (defmethod sax:characters ((sink toplevel-text-output-sink) data)
319 (when (zerop (textoutput-sink-depth sink))
320 (write-string data (text-output-sink-target sink))))
322 (defmethod sax:unescaped ((sink toplevel-text-output-sink) data)
323 (sax:characters sink data))
325 (defmethod sax:end-element ((sink toplevel-text-output-sink)
326 namespace-uri local-name qname)
327 (declare (ignore namespace-uri local-name qname))
328 (decf (textoutput-sink-depth sink)))
330 (defun invoke-with-toplevel-text-output-sink (fn)
331 (with-output-to-string (s)
332 (funcall fn (make-instance 'toplevel-text-output-sink :target s))))
335 ;;;; TEXT-FILTER
336 ;;;;
337 ;;;; A sink that passes through only text (at any level) and turns to
338 ;;;; into unescaped characters.
340 (defclass text-filter (sax:default-handler)
341 ((target :initarg :target :accessor text-filter-target)))
343 (defmethod sax:characters ((sink text-filter) data)
344 (sax:unescaped (text-filter-target sink) data))
346 (defmethod sax:unescaped ((sink text-filter) data)
347 (sax:unescaped (text-filter-target sink) data))
349 (defmethod sax:end-document ((sink text-filter))
350 (sax:end-document (text-filter-target sink)))
352 (defun make-text-filter (target)
353 (make-instance 'text-filter :target target))
356 ;;;; ESCAPER
357 ;;;;
358 ;;;; A sink that recovers from sax:unescaped using sax:characters, as per
359 ;;;; XSLT 16.4.
361 (defclass escaper (cxml:broadcast-handler)
364 (defmethod sax:unescaped ((sink escaper) data)
365 (sax:characters sink data))
367 (defun make-escaper (target)
368 (make-instance 'escaper :handlers (list target)))
371 ;;;; Names
373 (defun of-name (local-name)
374 (stp:of-name local-name *xsl*))
376 (defun namep (node local-name)
377 (and (typep node '(or stp:element stp:attribute))
378 (equal (stp:namespace-uri node) *xsl*)
379 (equal (stp:local-name node) local-name)))
382 ;;;; PARSE-STYLESHEET
384 (defstruct stylesheet
385 (modes (make-hash-table :test 'equal))
386 (global-variables (make-empty-declaration-array))
387 (output-specification (make-output-specification))
388 (strip-tests nil)
389 (strip-thunk nil)
390 (named-templates (make-hash-table :test 'equal))
391 (attribute-sets (make-hash-table :test 'equal))
392 (keys (make-hash-table :test 'equal))
393 (namespace-aliases (make-hash-table :test 'equal))
394 (decimal-formats (make-hash-table :test 'equal))
395 (initial-global-variable-thunks (make-hash-table :test 'equal)))
397 (defstruct mode
398 (templates nil)
399 (match-thunk (lambda (ignore) (declare (ignore ignore)) nil)))
401 (defun find-mode (stylesheet local-name &optional uri)
402 (gethash (cons local-name uri) (stylesheet-modes stylesheet)))
404 (defun ensure-mode (stylesheet &optional local-name uri)
405 (or (find-mode stylesheet local-name uri)
406 (setf (gethash (cons local-name uri) (stylesheet-modes stylesheet))
407 (make-mode))))
409 (defun ensure-mode/qname (stylesheet qname env)
410 (if qname
411 (multiple-value-bind (local-name uri)
412 (decode-qname qname env nil)
413 (ensure-mode stylesheet local-name uri))
414 (find-mode stylesheet nil)))
416 (defun acons-namespaces (element &optional (bindings *namespaces*))
417 (map-namespace-declarations (lambda (prefix uri)
418 (push (cons prefix uri) bindings))
419 element)
420 bindings)
422 (defun find-key (name stylesheet)
423 (or (gethash name (stylesheet-keys stylesheet))
424 (xslt-error "unknown key: ~a" name)))
426 (defun make-key (match use) (cons match use))
428 (defun key-match (key) (car key))
430 (defun key-use (key) (cdr key))
432 (defun add-key (stylesheet name match use)
433 (if (gethash name (stylesheet-keys stylesheet))
434 (xslt-error "duplicate key: ~a" name)
435 (setf (gethash name (stylesheet-keys stylesheet))
436 (make-key match use))))
438 (defvar *excluded-namespaces* (list *xsl*))
439 (defvar *empty-mode*)
440 (defvar *default-mode*)
442 (defvar *xsl-include-stack* nil)
444 (defun uri-to-pathname (uri)
445 (cxml::uri-to-pathname (puri:parse-uri uri)))
447 ;; Why this extra check for literal result element used as stylesheets,
448 ;; instead of a general check for every literal result element? Because
449 ;; Stylesheet__91804 says so.
450 (defun check-Errors_err035 (literal-result-element)
451 (let ((*namespaces* (acons-namespaces literal-result-element))
452 (env (make-instance 'lexical-xslt-environment)))
453 (stp:with-attributes ((extension-element-prefixes
454 "extension-element-prefixes"
455 *xsl*))
456 literal-result-element
457 (dolist (prefix (words (or extension-element-prefixes "")))
458 (if (equal prefix "#default")
459 (setf prefix nil)
460 (unless (cxml-stp-impl::nc-name-p prefix)
461 (xslt-error "invalid prefix: ~A" prefix)))
462 (let ((uri
463 (or (xpath-sys:environment-find-namespace env prefix)
464 (xslt-error "namespace not found: ~A" prefix))))
465 (when (equal uri (stp:namespace-uri literal-result-element))
466 (xslt-error "literal result element used as stylesheet, but is ~
467 declared as an extension element")))))))
469 (defun unwrap-2.3 (document)
470 (let ((literal-result-element (stp:document-element document))
471 (new-template (stp:make-element "template" *xsl*))
472 (new-document-element (stp:make-element "stylesheet" *xsl*)))
473 (check-Errors_err035 literal-result-element)
474 (setf (stp:attribute-value new-document-element "version")
475 (or (stp:attribute-value literal-result-element "version" *xsl*)
476 (xslt-error "not a stylesheet: root element lacks xsl:version")))
477 (setf (stp:attribute-value new-template "match") "/")
478 (setf (stp:document-element document) new-document-element)
479 (stp:append-child new-document-element new-template)
480 (stp:append-child new-template literal-result-element)
481 new-document-element))
483 (defun parse-stylesheet-to-stp (input uri-resolver)
484 (let* ((d (cxml:parse input (make-text-normalizer (cxml-stp:make-builder))))
485 (<transform> (stp:document-element d)))
486 (unless (equal (stp:namespace-uri <transform>) *xsl*)
487 (setf <transform> (unwrap-2.3 d)))
488 (strip-stylesheet <transform>)
489 (unless (and (equal (stp:namespace-uri <transform>) *xsl*)
490 (or (equal (stp:local-name <transform>) "transform")
491 (equal (stp:local-name <transform>) "stylesheet")))
492 (xslt-error "not a stylesheet"))
493 (check-for-invalid-attributes '(("version" . "")
494 ("exclude-result-prefixes" . "")
495 ("extension-element-prefixes" . ""))
496 <transform>)
497 (let ((invalid
498 (or (stp:find-child-if (of-name "stylesheet") <transform>)
499 (stp:find-child-if (of-name "transform") <transform>))))
500 (when invalid
501 (xslt-error "invalid top-level element ~A" (stp:local-name invalid))))
502 (dolist (include (stp:filter-children (of-name "include") <transform>))
503 (let* ((uri (puri:merge-uris (or (stp:attribute-value include "href")
504 (xslt-error "include without href"))
505 (stp:base-uri include)))
506 (uri (if uri-resolver
507 (funcall uri-resolver (puri:render-uri uri nil))
508 uri))
509 (str (puri:render-uri uri nil))
510 (pathname
511 (handler-case
512 (uri-to-pathname uri)
513 (cxml:xml-parse-error (c)
514 (xslt-error "cannot find included stylesheet ~A: ~A"
515 uri c)))))
516 (with-open-file
517 (stream pathname
518 :element-type '(unsigned-byte 8)
519 :if-does-not-exist nil)
520 (unless stream
521 (xslt-error "cannot find included stylesheet ~A at ~A"
522 uri pathname))
523 (when (find str *xsl-include-stack* :test #'equal)
524 (xslt-error "recursive inclusion of ~A" uri))
525 (let* ((*xsl-include-stack* (cons str *xsl-include-stack*))
526 (<transform>2 (parse-stylesheet-to-stp stream uri-resolver)))
527 (stp:insert-child-after <transform>
528 (stp:copy <transform>2)
529 include)
530 (stp:detach include)))))
531 <transform>))
533 (defvar *instruction-base-uri*) ;misnamed, is also used in other attributes
534 (defvar *apply-imports-limit*)
535 (defvar *import-priority*)
536 (defvar *extension-namespaces*)
537 (defvar *forwards-compatible-p*)
539 (defmacro do-toplevel ((var xpath <transform>) &body body)
540 `(map-toplevel (lambda (,var) ,@body) ,xpath ,<transform>))
542 (defun map-toplevel (fn xpath <transform>)
543 (dolist (node (list-toplevel xpath <transform>))
544 (let ((*namespaces* *namespaces*))
545 (xpath:do-node-set (ancestor (xpath:evaluate "ancestor::node()" node))
546 (when (xpath-protocol:node-type-p ancestor :element)
547 (setf *namespaces* (acons-namespaces ancestor))))
548 (funcall fn node))))
550 (defun list-toplevel (xpath <transform>)
551 (labels ((recurse (sub)
552 (let ((subsubs
553 (xpath-sys:pipe-of
554 (xpath:evaluate "transform|stylesheet" sub))))
555 (xpath::append-pipes
556 (xpath-sys:pipe-of (xpath:evaluate xpath sub))
557 (xpath::mappend-pipe #'recurse subsubs)))))
558 (xpath::sort-nodes (recurse <transform>))))
560 (defmacro with-import-magic ((node env) &body body)
561 `(invoke-with-import-magic (lambda () ,@body) ,node ,env))
563 (defun invoke-with-import-magic (fn node env)
564 (unless (or (namep node "stylesheet") (namep node "transform"))
565 (setf node (stp:parent node)))
566 (let ((*excluded-namespaces* (list *xsl*))
567 (*extension-namespaces* '())
568 (*forwards-compatible-p*
569 (not (equal (stp:attribute-value node "version") "1.0"))))
570 (parse-exclude-result-prefixes! node env)
571 (parse-extension-element-prefixes! node env)
572 (funcall fn)))
574 (defun parse-1-stylesheet (env stylesheet designator uri-resolver)
575 (let* ((<transform> (parse-stylesheet-to-stp designator uri-resolver))
576 (instruction-base-uri (stp:base-uri <transform>))
577 (namespaces (acons-namespaces <transform>))
578 (apply-imports-limit (1+ *import-priority*))
579 (continuations '()))
580 (let ((*namespaces* namespaces))
581 (invoke-with-import-magic (constantly t) <transform> env))
582 (do-toplevel (elt "node()" <transform>)
583 (let ((version (stp:attribute-value (stp:parent elt) "version")))
584 (cond
585 ((null version)
586 (xslt-error "stylesheet lacks version"))
587 ((equal version "1.0")
588 (if (typep elt 'stp:element)
589 (when (or (equal (stp:namespace-uri elt) "")
590 (and (equal (stp:namespace-uri elt) *xsl*)
591 (not (find (stp:local-name elt)
592 '("key" "template" "output"
593 "strip-space" "preserve-space"
594 "attribute-set" "namespace-alias"
595 "decimal-format" "variable" "param"
596 "import" "include"
597 ;; for include handling:
598 "stylesheet" "transform")
599 :test #'equal))))
600 (xslt-error "unknown top-level element ~A" (stp:local-name elt)))
601 (xslt-error "text at top-level"))))))
602 (macrolet ((with-specials ((&optional) &body body)
603 `(let ((*instruction-base-uri* instruction-base-uri)
604 (*namespaces* namespaces)
605 (*apply-imports-limit* apply-imports-limit))
606 ,@body)))
607 (with-specials ()
608 (do-toplevel (import "import" <transform>)
609 (let ((uri (puri:merge-uris (or (stp:attribute-value import "href")
610 (xslt-error "import without href"))
611 (stp:base-uri import))))
612 (push (parse-imported-stylesheet env stylesheet uri uri-resolver)
613 continuations))))
614 (let ((import-priority
615 (incf *import-priority*))
616 (var-cont (prepare-global-variables stylesheet <transform>)))
617 ;; delay the rest of compilation until we've seen all global
618 ;; variables:
619 (lambda ()
620 (mapc #'funcall (nreverse continuations))
621 (with-specials ()
622 (let ((*import-priority* import-priority))
623 (funcall var-cont)
624 (parse-keys! stylesheet <transform> env)
625 (parse-templates! stylesheet <transform> env)
626 (parse-output! stylesheet <transform>)
627 (parse-strip/preserve-space! stylesheet <transform> env)
628 (parse-attribute-sets! stylesheet <transform> env)
629 (parse-namespace-aliases! stylesheet <transform> env)
630 (parse-decimal-formats! stylesheet <transform> env))))))))
632 (defvar *xsl-import-stack* nil)
634 (defun parse-imported-stylesheet (env stylesheet uri uri-resolver)
635 (let* ((uri (if uri-resolver
636 (funcall uri-resolver (puri:render-uri uri nil))
637 uri))
638 (str (puri:render-uri uri nil))
639 (pathname
640 (handler-case
641 (uri-to-pathname uri)
642 (cxml:xml-parse-error (c)
643 (xslt-error "cannot find imported stylesheet ~A: ~A"
644 uri c)))))
645 (with-open-file
646 (stream pathname
647 :element-type '(unsigned-byte 8)
648 :if-does-not-exist nil)
649 (unless stream
650 (xslt-error "cannot find imported stylesheet ~A at ~A"
651 uri pathname))
652 (when (find str *xsl-import-stack* :test #'equal)
653 (xslt-error "recursive inclusion of ~A" uri))
654 (let ((*xsl-import-stack* (cons str *xsl-import-stack*)))
655 (parse-1-stylesheet env stylesheet stream uri-resolver)))))
657 (defvar *included-attribute-sets*)
659 (defun parse-stylesheet (designator &key uri-resolver)
660 (with-resignalled-errors ()
661 (xpath:with-namespaces ((nil #.*xsl*))
662 (let* ((*import-priority* 0)
663 (xpath:*allow-variables-in-patterns* nil)
664 (puri:*strict-parse* nil)
665 (stylesheet (make-stylesheet))
666 (env (make-instance 'lexical-xslt-environment))
667 (*excluded-namespaces* *excluded-namespaces*)
668 (*global-variable-declarations* (make-empty-declaration-array))
669 (*included-attribute-sets* nil))
670 (ensure-mode stylesheet nil)
671 (funcall (parse-1-stylesheet env stylesheet designator uri-resolver))
672 ;; reverse attribute sets:
673 (let ((table (stylesheet-attribute-sets stylesheet)))
674 (maphash (lambda (k v)
675 (setf (gethash k table) (nreverse v)))
676 table))
677 ;; for Errors_err011
678 (dolist (sets *included-attribute-sets*)
679 (loop for (local-name uri nil) in sets do
680 (find-attribute-set local-name uri stylesheet)))
681 ;; add default df
682 (unless (find-decimal-format "" "" stylesheet nil)
683 (setf (find-decimal-format "" "" stylesheet)
684 (make-decimal-format)))
685 ;; compile a template matcher for each mode:
686 (loop
687 for mode being each hash-value in (stylesheet-modes stylesheet)
689 (setf (mode-match-thunk mode)
690 (xpath:make-pattern-matcher
691 (mapcar #'template-compiled-pattern
692 (mode-templates mode)))))
693 ;; and for the strip tests
694 (setf (stylesheet-strip-thunk stylesheet)
695 (let ((patterns (stylesheet-strip-tests stylesheet)))
696 (and patterns
697 (xpath:make-pattern-matcher
698 (mapcar #'strip-test-compiled-pattern patterns)))))
699 stylesheet))))
701 (defun parse-attribute-sets! (stylesheet <transform> env)
702 (do-toplevel (elt "attribute-set" <transform>)
703 (with-import-magic (elt env)
704 (push (let* ((sets
705 (mapcar (lambda (qname)
706 (multiple-value-list (decode-qname qname env nil)))
707 (words
708 (stp:attribute-value elt "use-attribute-sets"))))
709 (instructions
710 (stp:map-children
711 'list
712 (lambda (child)
713 (unless
714 (and (typep child 'stp:element)
715 (or (and (equal (stp:namespace-uri child) *xsl*)
716 (equal (stp:local-name child)
717 "attribute"))
718 (find (stp:namespace-uri child)
719 *extension-namespaces*
720 :test 'equal)))
721 (xslt-error "non-attribute found in attribute set"))
722 (parse-instruction child))
723 elt))
724 (*lexical-variable-declarations*
725 (make-empty-declaration-array))
726 (thunk
727 (compile-instruction `(progn ,@instructions) env))
728 (n-variables (length *lexical-variable-declarations*)))
729 (push sets *included-attribute-sets*)
730 (lambda (ctx)
731 (with-stack-limit ()
732 (loop for (local-name uri nil) in sets do
733 (dolist (thunk (find-attribute-set local-name uri))
734 (funcall thunk ctx)))
735 (let ((*lexical-variable-values*
736 (make-variable-value-array n-variables)))
737 (funcall thunk ctx)))))
738 (gethash (multiple-value-bind (local-name uri)
739 (decode-qname (or (stp:attribute-value elt "name")
740 (xslt-error "missing name"))
742 nil)
743 (cons local-name uri))
744 (stylesheet-attribute-sets stylesheet))))))
746 (defun parse-namespace-aliases! (stylesheet <transform> env)
747 (do-toplevel (elt "namespace-alias" <transform>)
748 (stp:with-attributes (stylesheet-prefix result-prefix) elt
749 (setf (gethash
750 (if (equal stylesheet-prefix "#default")
752 (xpath-sys:environment-find-namespace env stylesheet-prefix))
753 (stylesheet-namespace-aliases stylesheet))
754 (xpath-sys:environment-find-namespace
756 (if (equal result-prefix "#default")
758 result-prefix))))))
760 (defun parse-decimal-formats! (stylesheet <transform> env)
761 (do-toplevel (elt "decimal-format" <transform>)
762 (stp:with-attributes (name
763 ;; strings
764 infinity
765 (nan "NaN")
766 ;; characters:
767 decimal-separator
768 grouping-separator
769 zero-digit
770 percent
771 per-mille
772 digit
773 pattern-separator
774 minus-sign)
776 (multiple-value-bind (local-name uri)
777 (if name
778 (decode-qname name env nil)
779 (values "" ""))
780 (let ((current (find-decimal-format local-name uri stylesheet nil))
781 (new
782 (let ((seen '()))
783 (flet ((chr (key x)
784 (when x
785 (unless (eql (length x) 1)
786 (xslt-error "not a single character: ~A" x))
787 (let ((chr (elt x 0)))
788 (when (find chr seen)
789 (xslt-error
790 "conflicting decimal format characters: ~A"
791 chr))
792 (push chr seen)
793 (list key chr))))
794 (str (key x)
795 (when x
796 (list key x))))
797 (apply #'make-decimal-format
798 (append (str :infinity infinity)
799 (str :nan nan)
800 (chr :decimal-separator decimal-separator)
801 (chr :grouping-separator grouping-separator)
802 (chr :zero-digit zero-digit)
803 (chr :percent percent)
804 (chr :per-mille per-mille)
805 (chr :digit digit)
806 (chr :pattern-separator pattern-separator)
807 (chr :minus-sign minus-sign)))))))
808 (if current
809 (unless (decimal-format= current new)
810 (xslt-error "decimal format mismatch for ~S" local-name))
811 (setf (find-decimal-format local-name uri stylesheet) new)))))))
813 (defun parse-exclude-result-prefixes! (node env)
814 (stp:with-attributes (exclude-result-prefixes)
815 node
816 (dolist (prefix (words (or exclude-result-prefixes "")))
817 (if (equal prefix "#default")
818 (setf prefix nil)
819 (unless (cxml-stp-impl::nc-name-p prefix)
820 (xslt-error "invalid prefix: ~A" prefix)))
821 (push (or (xpath-sys:environment-find-namespace env prefix)
822 (xslt-error "namespace not found: ~A" prefix))
823 *excluded-namespaces*))))
825 (defun parse-extension-element-prefixes! (node env)
826 (stp:with-attributes (extension-element-prefixes)
827 node
828 (dolist (prefix (words (or extension-element-prefixes "")))
829 (if (equal prefix "#default")
830 (setf prefix nil)
831 (unless (cxml-stp-impl::nc-name-p prefix)
832 (xslt-error "invalid prefix: ~A" prefix)))
833 (let ((uri
834 (or (xpath-sys:environment-find-namespace env prefix)
835 (xslt-error "namespace not found: ~A" prefix))))
836 (unless (equal uri *xsl*)
837 (push uri *extension-namespaces*)
838 (push uri *excluded-namespaces*))))))
840 (defun parse-nametest-tokens (str)
841 (labels ((check (boolean)
842 (unless boolean
843 (xslt-error "invalid nametest token")))
844 (check-null (boolean)
845 (check (not boolean))))
846 (cons
847 :patterns
848 (mapcar (lambda (name-test)
849 (destructuring-bind (&optional path &rest junk)
850 (cdr (xpath:parse-pattern-expression name-test))
851 (check-null junk)
852 (check (eq (car path) :path))
853 (destructuring-bind (&optional child &rest junk) (cdr path)
854 (check-null junk)
855 (check (eq (car child) :child))
856 (destructuring-bind (nodetest &rest junk) (cdr child)
857 (check-null junk)
858 (check (or (stringp nodetest)
859 (eq nodetest '*)
860 (and (consp nodetest)
861 (or (eq (car nodetest) :namespace)
862 (eq (car nodetest) :qname)))))))
863 path))
864 (words str)))))
866 (defstruct strip-test
867 compiled-pattern
868 priority
869 position
870 value)
872 (defun parse-strip/preserve-space! (stylesheet <transform> env)
873 (let ((i 0))
874 (do-toplevel (elt "strip-space|preserve-space" <transform>)
875 (let ((*namespaces* (acons-namespaces elt))
876 (value
877 (if (equal (stp:local-name elt) "strip-space")
878 :strip
879 :preserve)))
880 (dolist (expression
881 (cdr (parse-nametest-tokens
882 (stp:attribute-value elt "elements"))))
883 (let* ((compiled-pattern
884 (car (without-xslt-current ()
885 (xpath:compute-patterns
886 `(:patterns ,expression)
887 *import-priority*
888 "will set below"
889 env))))
890 (strip-test
891 (make-strip-test :compiled-pattern compiled-pattern
892 :priority (expression-priority expression)
893 :position i
894 :value value)))
895 (setf (xpath:pattern-value compiled-pattern) strip-test)
896 (push strip-test (stylesheet-strip-tests stylesheet)))))
897 (incf i))))
899 (defstruct (output-specification
900 (:conc-name "OUTPUT-"))
901 method
902 indent
903 omit-xml-declaration
904 encoding
905 doctype-system
906 doctype-public)
908 (defun parse-output! (stylesheet <transform>)
909 (dolist (<output> (list-toplevel "output" <transform>))
910 (let ((spec (stylesheet-output-specification stylesheet)))
911 (stp:with-attributes ( ;; version
912 method
913 indent
914 encoding
915 ;;; media-type
916 doctype-system
917 doctype-public
918 omit-xml-declaration
919 ;;; standalone
920 ;;; cdata-section-elements
922 <output>
923 (when method
924 (setf (output-method spec) method))
925 (when indent
926 (setf (output-indent spec) indent))
927 (when encoding
928 (setf (output-encoding spec) encoding))
929 (when doctype-system
930 (setf (output-doctype-system spec) doctype-system))
931 (when doctype-public
932 (setf (output-doctype-public spec) doctype-public))
933 (when omit-xml-declaration
934 (setf (output-omit-xml-declaration spec) omit-xml-declaration))
935 ;;; (when cdata-section-elements
936 ;;; (setf (output-cdata-section-elements spec)
937 ;;; (concatenate 'string
938 ;;; (output-cdata-section-elements spec)
939 ;;; " "
940 ;;; cdata-section-elements)))
941 ))))
943 (defun make-empty-declaration-array ()
944 (make-array 1 :fill-pointer 0 :adjustable t))
946 (defun make-variable-value-array (n-lexical-variables)
947 (make-array n-lexical-variables :initial-element 'unbound))
949 (defun compile-global-variable (<variable> env) ;; also for <param>
950 (stp:with-attributes (name select) <variable>
951 (when (and select (stp:list-children <variable>))
952 (xslt-error "variable with select and body"))
953 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
954 (inner (cond
955 (select
956 (compile-xpath select env))
957 ((stp:list-children <variable>)
958 (let* ((inner-sexpr `(progn ,@(parse-body <variable>)))
959 (inner-thunk (compile-instruction inner-sexpr env)))
960 (lambda (ctx)
961 (apply-to-result-tree-fragment ctx inner-thunk))))
963 (lambda (ctx)
964 (declare (ignore ctx))
965 ""))))
966 (n-lexical-variables (length *lexical-variable-declarations*)))
967 (xslt-trace-thunk
968 (lambda (ctx)
969 (let* ((*lexical-variable-values*
970 (make-variable-value-array n-lexical-variables)))
971 (funcall inner ctx)))
972 "global ~s (~s) = ~s" name select :result))))
974 (defstruct (variable-chain
975 (:constructor make-variable-chain)
976 (:conc-name "VARIABLE-CHAIN-"))
977 definitions
978 index
979 local-name
980 thunk
981 uri)
983 (defstruct (import-variable
984 (:constructor make-variable)
985 (:conc-name "VARIABLE-"))
986 value-thunk
987 value-thunk-setter
988 param-p)
990 (defun parse-global-variable! (stylesheet <variable> global-env)
991 (let* ((*namespaces* (acons-namespaces <variable>))
992 (instruction-base-uri (stp:base-uri <variable>))
993 (*instruction-base-uri* instruction-base-uri)
994 (*excluded-namespaces* (list *xsl*))
995 (*extension-namespaces* '())
996 (qname (stp:attribute-value <variable> "name")))
997 (with-import-magic (<variable> global-env)
998 (unless qname
999 (xslt-error "name missing in ~A" (stp:local-name <variable>)))
1000 (multiple-value-bind (local-name uri)
1001 (decode-qname qname global-env nil)
1002 ;; For the normal compilation environment of templates, install it
1003 ;; into *GLOBAL-VARIABLE-DECLARATIONS*:
1004 (let ((index (intern-global-variable local-name uri)))
1005 ;; For the evaluation of a global variable itself, build a thunk
1006 ;; that lazily resolves other variables, stored into
1007 ;; INITIAL-GLOBAL-VARIABLE-THUNKS:
1008 (let* ((value-thunk :unknown)
1009 (sgv (stylesheet-global-variables stylesheet))
1010 (chain
1011 (if (< index (length sgv))
1012 (elt sgv index)
1013 (make-variable-chain
1014 :index index
1015 :local-name local-name
1016 :uri uri)))
1017 (next (car (variable-chain-definitions chain)))
1018 (global-variable-thunk
1019 (lambda (ctx)
1020 (let ((v (global-variable-value index nil)))
1021 (cond
1022 ((eq v 'seen)
1023 (unless next
1024 (xslt-error "no next definition for: ~A"
1025 local-name))
1026 (funcall (variable-value-thunk next) ctx))
1027 ((eq v 'unbound)
1028 (setf (global-variable-value index) 'seen)
1029 (setf (global-variable-value index)
1030 (funcall value-thunk ctx)))
1032 v)))))
1033 (excluded-namespaces *excluded-namespaces*)
1034 (extension-namespaces *extension-namespaces*)
1035 (variable
1036 (make-variable :param-p (namep <variable> "param")))
1037 (value-thunk-setter
1038 (lambda ()
1039 (let* ((*instruction-base-uri* instruction-base-uri)
1040 (*excluded-namespaces* excluded-namespaces)
1041 (*extension-namespaces* extension-namespaces)
1043 (compile-global-variable <variable> global-env)))
1044 (setf value-thunk fn)
1045 (setf (variable-value-thunk variable) fn)))))
1046 (setf (variable-value-thunk-setter variable)
1047 value-thunk-setter)
1048 (setf (gethash (cons local-name uri)
1049 (initial-global-variable-thunks global-env))
1050 global-variable-thunk)
1051 (setf (variable-chain-thunk chain) global-variable-thunk)
1052 (push variable (variable-chain-definitions chain))
1053 chain))))))
1055 (defun parse-keys! (stylesheet <transform> env)
1056 (xpath:with-namespaces ((nil #.*xsl*))
1057 (do-toplevel (<key> "key" <transform>)
1058 (let ((*instruction-base-uri* (stp:base-uri <key>)))
1059 (stp:with-attributes (name match use) <key>
1060 (unless name (xslt-error "key name attribute not specified"))
1061 (unless match (xslt-error "key match attribute not specified"))
1062 (unless use (xslt-error "key use attribute not specified"))
1063 (multiple-value-bind (local-name uri)
1064 (decode-qname name env nil)
1065 (add-key stylesheet
1066 (cons local-name uri)
1067 (compile-xpath `(xpath:xpath ,(parse-key-pattern match))
1068 env)
1069 (compile-xpath use
1070 (make-instance 'key-environment)))))))))
1072 (defun prepare-global-variables (stylesheet <transform>)
1073 (xpath:with-namespaces ((nil #.*xsl*))
1074 (let* ((igvt (stylesheet-initial-global-variable-thunks stylesheet))
1075 (global-env (make-instance 'global-variable-environment
1076 :initial-global-variable-thunks igvt))
1077 (chains '()))
1078 (do-toplevel (<variable> "variable|param" <transform>)
1079 (let ((chain
1080 (parse-global-variable! stylesheet <variable> global-env)))
1081 (xslt-trace "parsing global variable ~s (uri ~s)"
1082 (variable-chain-local-name chain)
1083 (variable-chain-uri chain))
1084 (when (find chain
1085 chains
1086 :test (lambda (a b)
1087 (and (equal (variable-chain-local-name a)
1088 (variable-chain-local-name b))
1089 (equal (variable-chain-uri a)
1090 (variable-chain-uri b)))))
1091 (xslt-error "duplicate definition for global variable ~A"
1092 (variable-chain-local-name chain)))
1093 (push chain chains)))
1094 (setf chains (nreverse chains))
1095 (let ((table (stylesheet-global-variables stylesheet))
1096 (newlen (length *global-variable-declarations*)))
1097 (adjust-array table newlen :fill-pointer newlen)
1098 (dolist (chain chains)
1099 (setf (elt table (variable-chain-index chain)) chain)))
1100 (lambda ()
1101 ;; now that the global environment knows about all variables, run the
1102 ;; thunk setters to perform their compilation
1103 (mapc (lambda (chain)
1104 (dolist (var (variable-chain-definitions chain))
1105 (funcall (variable-value-thunk-setter var))))
1106 chains)))))
1108 (defun parse-templates! (stylesheet <transform> env)
1109 (let ((i 0))
1110 (do-toplevel (<template> "template" <transform>)
1111 (let ((*namespaces* (acons-namespaces <template>))
1112 (*instruction-base-uri* (stp:base-uri <template>)))
1113 (with-import-magic (<template> env)
1114 (dolist (template (compile-template <template> env i))
1115 (let ((name (template-name template)))
1116 (if name
1117 (let* ((table (stylesheet-named-templates stylesheet))
1118 (head (car (gethash name table))))
1119 (when (and head (eql (template-import-priority head)
1120 (template-import-priority template)))
1121 ;; fixme: is this supposed to be a run-time error?
1122 (xslt-error "conflicting templates for ~A" name))
1123 (push template (gethash name table)))
1124 (let ((mode (ensure-mode/qname stylesheet
1125 (template-mode-qname template)
1126 env)))
1127 (setf (template-mode template) mode)
1128 (push template (mode-templates mode))))))))
1129 (incf i))))
1132 ;;;; APPLY-STYLESHEET
1134 (defvar *stylesheet*)
1136 (deftype xml-designator () '(or runes:xstream runes:rod array stream pathname))
1138 (defun unalias-uri (uri)
1139 (let ((result
1140 (gethash uri (stylesheet-namespace-aliases *stylesheet*)
1141 uri)))
1142 (check-type result string)
1143 result))
1145 (defstruct (parameter
1146 (:constructor make-parameter (value local-name &optional uri)))
1147 (uri "")
1148 local-name
1149 value)
1151 (defun find-parameter-value (local-name uri parameters)
1152 (dolist (p parameters)
1153 (when (and (equal (parameter-local-name p) local-name)
1154 (equal (parameter-uri p) uri))
1155 (return (parameter-value p)))))
1157 (defvar *uri-resolver*)
1159 (defun parse-allowing-microsoft-bom (pathname handler)
1160 (with-open-file (s pathname :element-type '(unsigned-byte 8))
1161 (unless (and (eql (read-byte s nil) #xef)
1162 (eql (read-byte s nil) #xbb)
1163 (eql (read-byte s nil) #xbf))
1164 (file-position s 0))
1165 (cxml:parse s handler)))
1167 (defvar *documents*)
1169 (defun %document (uri-string base-uri)
1170 (let* ((absolute-uri
1171 (puri:merge-uris uri-string (or base-uri "")))
1172 (resolved-uri
1173 (if *uri-resolver*
1174 (funcall *uri-resolver* (puri:render-uri absolute-uri nil))
1175 absolute-uri))
1176 (pathname
1177 (handler-case
1178 (uri-to-pathname resolved-uri)
1179 (cxml:xml-parse-error (c)
1180 (xslt-error "cannot find referenced document ~A: ~A"
1181 resolved-uri c))))
1182 (xpath-root-node
1183 (or (gethash pathname *documents*)
1184 (setf (gethash pathname *documents*)
1185 (make-whitespace-stripper
1186 (handler-case
1187 (parse-allowing-microsoft-bom pathname
1188 (stp:make-builder))
1189 ((or file-error cxml:xml-parse-error) (c)
1190 (xslt-error "cannot parse referenced document ~A: ~A"
1191 pathname c)))
1192 (stylesheet-strip-thunk *stylesheet*))))))
1193 (when (puri:uri-fragment absolute-uri)
1194 (xslt-error "use of fragment identifiers in document() not supported"))
1195 xpath-root-node))
1197 (xpath-sys:define-extension xslt *xsl*)
1199 (defun document-base-uri (node)
1200 (xpath-protocol:base-uri
1201 (cond
1202 ((xpath-protocol:node-type-p node :document)
1203 (xpath::find-in-pipe-if
1204 (lambda (x)
1205 (xpath-protocol:node-type-p x :element))
1206 (xpath-protocol:child-pipe node)))
1207 ((xpath-protocol:node-type-p node :element)
1208 node)
1210 (xpath-protocol:parent-node node)))))
1212 (xpath-sys:define-xpath-function/lazy
1213 xslt :document
1214 (object &optional node-set)
1215 (let ((instruction-base-uri *instruction-base-uri*))
1216 (lambda (ctx)
1217 (let* ((object (funcall object ctx))
1218 (node-set (and node-set (funcall node-set ctx)))
1219 (base-uri
1220 (if node-set
1221 (document-base-uri (xpath::textually-first-node node-set))
1222 instruction-base-uri)))
1223 (xpath-sys:make-node-set
1224 (if (xpath:node-set-p object)
1225 (xpath:map-node-set->list
1226 (lambda (node)
1227 (%document (xpath:string-value node)
1228 (if node-set
1229 base-uri
1230 (document-base-uri node))))
1231 object)
1232 (list (%document (xpath:string-value object) base-uri))))))))
1234 (xpath-sys:define-xpath-function/lazy xslt :key (name object)
1235 (let ((namespaces *namespaces*))
1236 (lambda (ctx)
1237 (let* ((qname (xpath:string-value (funcall name ctx)))
1238 (object (funcall object ctx))
1239 (expanded-name
1240 (multiple-value-bind (local-name uri)
1241 (decode-qname/runtime qname namespaces nil)
1242 (cons local-name uri)))
1243 (key (find-key expanded-name *stylesheet*)))
1244 (labels ((get-by-key (value)
1245 (let ((value (xpath:string-value value)))
1246 (xpath::filter-pipe
1247 #'(lambda (node)
1248 (let ((uses
1249 (xpath:evaluate-compiled (key-use key) node)))
1250 (if (xpath:node-set-p uses)
1251 (xpath::find-in-pipe
1252 value
1253 (xpath-sys:pipe-of uses)
1254 :key #'xpath:string-value
1255 :test #'equal)
1256 (equal value (xpath:string-value uses)))))
1257 (xpath-sys:pipe-of
1258 (xpath:node-set-value
1259 (xpath:evaluate-compiled (key-match key) ctx)))))))
1260 (xpath-sys:make-node-set
1261 (xpath::sort-pipe
1262 (if (xpath:node-set-p object)
1263 (xpath::mappend-pipe #'get-by-key (xpath-sys:pipe-of object))
1264 (get-by-key object)))))))))
1266 ;; FIXME: add alias mechanism for XPath extensions in order to avoid duplication
1268 (xpath-sys:define-xpath-function/lazy xslt :current ()
1269 (when *without-xslt-current-p*
1270 (xslt-error "current() not allowed here"))
1271 #'(lambda (ctx)
1272 (xpath-sys:make-node-set
1273 (xpath-sys:make-pipe
1274 (xpath:context-starting-node ctx)
1275 nil))))
1277 (xpath-sys:define-xpath-function/lazy xslt :unparsed-entity-uri (name)
1278 #'(lambda (ctx)
1279 (or (xpath-protocol:unparsed-entity-uri (xpath:context-node ctx)
1280 (funcall name ctx))
1281 "")))
1283 (defun %get-node-id (node)
1284 (when (xpath:node-set-p node)
1285 (setf node (xpath::textually-first-node node)))
1286 (when node
1287 (let ((id (xpath-sys:get-node-id node))
1288 (highest-base-uri
1289 (loop
1290 for parent = node then next
1291 for next = (xpath-protocol:parent-node parent)
1292 for this-base-uri = (xpath-protocol:base-uri parent)
1293 for highest-base-uri = (if (plusp (length this-base-uri))
1294 this-base-uri
1295 highest-base-uri)
1296 while next
1297 finally (return highest-base-uri))))
1298 ;; Heuristic: Reverse it so that the /home/david/alwaysthesame prefix is
1299 ;; checked only if everything else matches.
1301 ;; This might be pointless premature optimization, but I like the idea :-)
1302 (nreverse (concatenate 'string highest-base-uri "//" id)))))
1304 (xpath-sys:define-xpath-function/lazy xslt :generate-id (&optional node-set-thunk)
1305 (if node-set-thunk
1306 #'(lambda (ctx)
1307 (%get-node-id (xpath:node-set-value (funcall node-set-thunk ctx))))
1308 #'(lambda (ctx)
1309 (%get-node-id (xpath:context-node ctx)))))
1311 (declaim (special *available-instructions*))
1313 (xpath-sys:define-xpath-function/lazy xslt :element-available (qname)
1314 (let ((namespaces *namespaces*))
1315 #'(lambda (ctx)
1316 (let ((qname (funcall qname ctx)))
1317 (multiple-value-bind (local-name uri)
1318 (decode-qname/runtime qname namespaces nil)
1319 (and (equal uri *xsl*)
1320 (gethash local-name *available-instructions*)
1321 t))))))
1323 (xpath-sys:define-xpath-function/lazy xslt :function-available (qname)
1324 (let ((namespaces *namespaces*))
1325 #'(lambda (ctx)
1326 (let ((qname (funcall qname ctx)))
1327 (multiple-value-bind (local-name uri)
1328 (decode-qname/runtime qname namespaces nil)
1329 (and (zerop (length uri))
1330 (or (xpath-sys:find-xpath-function local-name *xsl*)
1331 (xpath-sys:find-xpath-function local-name uri))
1332 t))))))
1334 (xpath-sys:define-xpath-function/lazy xslt :system-property (qname)
1335 (let ((namespaces *namespaces*))
1336 (lambda (ctx)
1337 (let ((qname (funcall qname ctx)))
1338 (multiple-value-bind (local-name uri)
1339 (decode-qname/runtime qname namespaces nil)
1340 (if (equal uri *xsl*)
1341 (cond
1342 ((equal local-name "version")
1343 "1")
1344 ((equal local-name "vendor")
1345 "Xuriella")
1346 ((equal local-name "vendor-uri")
1347 "http://repo.or.cz/w/xuriella.git")
1349 ""))
1350 ""))))))
1352 (defun apply-stylesheet
1353 (stylesheet source-designator
1354 &key output parameters uri-resolver navigator)
1355 (when (typep stylesheet 'xml-designator)
1356 (setf stylesheet
1357 (handler-bind
1358 ((cxml:xml-parse-error
1359 (lambda (c)
1360 (xslt-error "cannot parse stylesheet: ~A" c))))
1361 (parse-stylesheet stylesheet))))
1362 (with-resignalled-errors ()
1363 (invoke-with-output-sink
1364 (lambda ()
1365 (let* ((*documents* (make-hash-table :test 'equal))
1366 (xpath:*navigator* (or navigator :default-navigator))
1367 (puri:*strict-parse* nil)
1368 (*stylesheet* stylesheet)
1369 (*empty-mode* (make-mode))
1370 (*default-mode* (find-mode stylesheet nil))
1371 (global-variable-chains
1372 (stylesheet-global-variables stylesheet))
1373 (*global-variable-values*
1374 (make-variable-value-array (length global-variable-chains)))
1375 (*uri-resolver* uri-resolver)
1376 (source-document
1377 (if (typep source-designator 'xml-designator)
1378 (cxml:parse source-designator (stp:make-builder))
1379 source-designator))
1380 (xpath-root-node
1381 (make-whitespace-stripper
1382 source-document
1383 (stylesheet-strip-thunk stylesheet)))
1384 (ctx (xpath:make-context xpath-root-node)))
1385 (when (pathnamep source-designator)
1386 (setf (gethash source-designator *documents*) xpath-root-node))
1387 (map nil
1388 (lambda (chain)
1389 (let ((head (car (variable-chain-definitions chain))))
1390 (when (variable-param-p head)
1391 (let ((value
1392 (find-parameter-value
1393 (variable-chain-local-name chain)
1394 (variable-chain-uri chain)
1395 parameters)))
1396 (when value
1397 (setf (global-variable-value
1398 (variable-chain-index chain))
1399 value))))))
1400 global-variable-chains)
1401 (map nil
1402 (lambda (chain)
1403 (funcall (variable-chain-thunk chain) ctx))
1404 global-variable-chains)
1405 ;; zzz we wouldn't have to mask float traps here if we used the
1406 ;; XPath API properly. Unfortunately I've been using FUNCALL
1407 ;; everywhere instead of EVALUATE, so let's paper over that
1408 ;; at a central place to be sure:
1409 (xpath::with-float-traps-masked ()
1410 (apply-templates ctx :mode *default-mode*))))
1411 (stylesheet-output-specification stylesheet)
1412 output)))
1414 (defun find-attribute-set (local-name uri &optional (stylesheet *stylesheet*))
1415 (or (gethash (cons local-name uri) (stylesheet-attribute-sets stylesheet))
1416 (xslt-error "no such attribute set: ~A/~A" local-name uri)))
1418 (defun apply-templates/list (list &key param-bindings sort-predicate mode)
1419 (when sort-predicate
1420 (setf list
1421 (mapcar #'xpath:context-node
1422 (stable-sort (contextify-node-list list)
1423 sort-predicate))))
1424 (let* ((n (length list))
1425 (s/d (lambda () n)))
1426 (loop
1427 for i from 1
1428 for child in list
1430 (apply-templates (xpath:make-context child s/d i)
1431 :param-bindings param-bindings
1432 :mode mode))))
1434 (defvar *stack-limit* 200)
1436 (defun invoke-with-stack-limit (fn)
1437 (let ((*stack-limit* (1- *stack-limit*)))
1438 (unless (plusp *stack-limit*)
1439 (xslt-error "*stack-limit* reached; stack overflow"))
1440 (funcall fn)))
1442 (defun invoke-template (ctx template param-bindings)
1443 (let ((*lexical-variable-values*
1444 (make-variable-value-array (template-n-variables template))))
1445 (with-stack-limit ()
1446 (loop
1447 for (name-cons value) in param-bindings
1448 for (nil index nil) = (find name-cons
1449 (template-params template)
1450 :test #'equal
1451 :key #'car)
1453 (when index
1454 (setf (lexical-variable-value index) value)))
1455 (funcall (template-body template) ctx))))
1457 (defun apply-default-templates (ctx mode)
1458 (let ((node (xpath:context-node ctx)))
1459 (cond
1460 ((or (xpath-protocol:node-type-p node :processing-instruction)
1461 (xpath-protocol:node-type-p node :comment)))
1462 ((or (xpath-protocol:node-type-p node :text)
1463 (xpath-protocol:node-type-p node :attribute))
1464 (write-text (xpath-protocol:node-text node)))
1466 (apply-templates/list
1467 (xpath::force
1468 (xpath-protocol:child-pipe node))
1469 :mode mode)))))
1471 (defvar *apply-imports*)
1473 (defun apply-applicable-templates (ctx templates param-bindings finally)
1474 (labels ((apply-imports (&optional actual-param-bindings)
1475 (if templates
1476 (let* ((this (pop templates))
1477 (low (template-apply-imports-limit this))
1478 (high (template-import-priority this)))
1479 (setf templates
1480 (remove-if-not
1481 (lambda (x)
1482 (<= low (template-import-priority x) high))
1483 templates))
1484 (invoke-template ctx this actual-param-bindings))
1485 (funcall finally))))
1486 (let ((*apply-imports* #'apply-imports))
1487 (apply-imports param-bindings))))
1489 (defun apply-templates (ctx &key param-bindings mode)
1490 (apply-applicable-templates ctx
1491 (find-templates ctx (or mode *default-mode*))
1492 param-bindings
1493 (lambda ()
1494 (apply-default-templates ctx mode))))
1496 (defun call-template (ctx name &optional param-bindings)
1497 (apply-applicable-templates ctx
1498 (find-named-templates name)
1499 param-bindings
1500 (lambda ()
1501 (error "cannot find named template: ~s"
1502 name))))
1504 (defun find-templates (ctx mode)
1505 (let* ((matching-candidates
1506 (xpath:matching-values (mode-match-thunk mode)
1507 (xpath:context-node ctx)))
1508 (npriorities
1509 (if matching-candidates
1510 (1+ (reduce #'max
1511 matching-candidates
1512 :key #'template-import-priority))
1514 (priority-groups (make-array npriorities :initial-element nil)))
1515 (dolist (template matching-candidates)
1516 (push template
1517 (elt priority-groups (template-import-priority template))))
1518 (loop
1519 for i from (1- npriorities) downto 0
1520 for group = (elt priority-groups i)
1521 for template = (maximize #'template< group)
1522 when template
1523 collect template)))
1525 (defun find-named-templates (name)
1526 (gethash name (stylesheet-named-templates *stylesheet*)))
1528 (defun template< (a b) ;assuming same import priority
1529 (let ((p (template-priority a))
1530 (q (template-priority b)))
1531 (cond
1532 ((< p q) t)
1533 ((> p q) nil)
1535 (xslt-cerror "conflicting templates:~_~A,~_~A"
1536 (template-match-expression a)
1537 (template-match-expression b))
1538 (< (template-position a) (template-position b))))))
1540 (defun maximize (< things)
1541 (when things
1542 (let ((max (car things)))
1543 (dolist (other (cdr things))
1544 (when (funcall < max other)
1545 (setf max other)))
1546 max)))
1548 (defun invoke-with-output-sink (fn output-spec output)
1549 (etypecase output
1550 (pathname
1551 (with-open-file (s output
1552 :direction :output
1553 :element-type '(unsigned-byte 8)
1554 :if-exists :rename-and-delete)
1555 (invoke-with-output-sink fn output-spec s)))
1556 ((or stream null)
1557 (invoke-with-output-sink fn
1558 output-spec
1559 (make-output-sink output-spec output)))
1560 ((or hax:abstract-handler sax:abstract-handler)
1561 (with-xml-output output
1562 (when (typep output '(or combi-sink auto-detect-sink))
1563 (sax:start-dtd output
1564 :autodetect-me-please
1565 (output-doctype-public output-spec)
1566 (output-doctype-system output-spec)))
1567 (funcall fn)))))
1569 (defun make-output-sink (output-spec stream)
1570 (let* ((ystream
1571 (if stream
1572 (let ((et (stream-element-type stream)))
1573 (cond
1574 ((or (null et) (subtypep et '(unsigned-byte 8)))
1575 (runes:make-octet-stream-ystream stream))
1576 ((subtypep et 'character)
1577 (runes:make-character-stream-ystream stream))))
1578 (runes:make-rod-ystream)))
1579 (omit-xml-declaration-p
1580 (equal (output-omit-xml-declaration output-spec) "yes"))
1581 (sink-encoding (or (output-encoding output-spec) "UTF-8"))
1582 (sax-target
1583 (progn
1584 (setf (runes:ystream-encoding ystream)
1585 (cxml::find-output-encoding sink-encoding))
1586 (make-instance 'cxml::sink
1587 :ystream ystream
1588 :omit-xml-declaration-p omit-xml-declaration-p
1589 :encoding sink-encoding))))
1590 (flet ((make-combi-sink ()
1591 (make-instance 'combi-sink
1592 :hax-target (make-instance 'chtml::sink
1593 :ystream ystream)
1594 :sax-target sax-target
1595 :encoding sink-encoding)))
1596 (let ((method-key
1597 (cond
1598 ((equalp (output-method output-spec) "HTML") :html)
1599 ((equalp (output-method output-spec) "TEXT") :text)
1600 ((equalp (output-method output-spec) "XML") :xml)
1601 (t nil))))
1602 (cond
1603 ((and (eq method-key :html)
1604 (null (output-doctype-system output-spec))
1605 (null (output-doctype-public output-spec)))
1606 (make-combi-sink))
1607 ((eq method-key :text)
1608 (make-text-filter sax-target))
1609 ((and (eq method-key :xml)
1610 (null (output-doctype-system output-spec)))
1611 sax-target)
1613 (make-auto-detect-sink (make-combi-sink) method-key)))))))
1615 (defstruct template
1616 match-expression
1617 compiled-pattern
1618 name
1619 import-priority
1620 apply-imports-limit
1621 priority
1622 position
1623 mode
1624 mode-qname
1625 params
1626 body
1627 n-variables)
1629 (defun expression-priority (form)
1630 (let ((step (second form)))
1631 (if (and (null (cddr form))
1632 (listp step)
1633 (member (car step) '(:child :attribute))
1634 (null (cddr step)))
1635 (let ((name (second step)))
1636 (cond
1637 ((or (stringp name)
1638 (and (consp name)
1639 (or (eq (car name) :qname)
1640 (eq (car name) :processing-instruction))))
1641 0.0)
1642 ((and (consp name)
1643 (or (eq (car name) :namespace)
1644 (eq (car name) '*)))
1645 -0.25)
1647 -0.5)))
1648 0.5)))
1650 (defun parse-xpath (str)
1651 (with-resignalled-errors ()
1652 (xpath:parse-xpath str)))
1654 (defun parse-key-pattern (str)
1655 (let ((parsed
1656 (mapcar #'(lambda (item)
1657 `(:path (:root :node)
1658 (:descendant-or-self *)
1659 ,@(cdr item)))
1660 (parse-pattern str))))
1661 (if (null (rest parsed))
1662 (first parsed)
1663 `(:union ,@parsed))))
1665 (defun parse-pattern (str)
1666 (with-resignalled-errors ()
1667 (cdr (xpath::parse-pattern-expression str))))
1669 (defun compile-value-thunk (value env)
1670 (if (and (listp value) (eq (car value) 'progn))
1671 (let ((inner-thunk (compile-instruction value env)))
1672 (lambda (ctx)
1673 (apply-to-result-tree-fragment ctx inner-thunk)))
1674 (compile-xpath value env)))
1676 (defun compile-var-binding (name value env)
1677 (multiple-value-bind (local-name uri)
1678 (decode-qname name env nil)
1679 (let ((thunk (xslt-trace-thunk
1680 (compile-value-thunk value env)
1681 "local variable ~s = ~s" name :result)))
1682 (list (cons local-name uri)
1683 (push-variable local-name
1685 *lexical-variable-declarations*)
1686 thunk))))
1688 (defun compile-var-bindings (forms env)
1689 (loop
1690 for (name value) in forms
1691 collect (compile-var-binding name value env)))
1693 (defun compile-template (<template> env position)
1694 (stp:with-attributes (match name priority mode) <template>
1695 (unless (or name match)
1696 (xslt-error "missing match in template"))
1697 (multiple-value-bind (params body-pos)
1698 (loop
1699 for i from 0
1700 for child in (stp:list-children <template>)
1701 while (namep child "param")
1702 collect (parse-param child) into params
1703 finally (return (values params i)))
1704 (let* ((*lexical-variable-declarations* (make-empty-declaration-array))
1705 (param-bindings (compile-var-bindings params env))
1706 (body (parse-body <template> body-pos (mapcar #'car params)))
1707 (body-thunk (compile-instruction `(progn ,@body) env))
1708 (outer-body-thunk
1709 (xslt-trace-thunk
1710 #'(lambda (ctx)
1711 (unwind-protect
1712 (progn
1713 ;; set params that weren't initialized by apply-templates
1714 (loop for (name index param-thunk) in param-bindings
1715 when (eq (lexical-variable-value index nil) 'unbound)
1716 do (setf (lexical-variable-value index)
1717 (funcall param-thunk ctx)))
1718 (funcall body-thunk ctx))))
1719 "template: match = ~s name = ~s" match name))
1720 (n-variables (length *lexical-variable-declarations*)))
1721 (append
1722 (when name
1723 (multiple-value-bind (local-name uri)
1724 (decode-qname name env nil)
1725 (list
1726 (make-template :name (cons local-name uri)
1727 :import-priority *import-priority*
1728 :apply-imports-limit *apply-imports-limit*
1729 :params param-bindings
1730 :body outer-body-thunk
1731 :n-variables n-variables))))
1732 (when match
1733 (mapcar (lambda (expression)
1734 (let* ((compiled-pattern
1735 (xslt-trace-thunk
1736 (car (without-xslt-current ()
1737 (xpath:compute-patterns
1738 `(:patterns ,expression)
1740 :dummy
1741 env)))
1742 "match-thunk for template (match ~s): ~s --> ~s"
1743 match expression :result))
1744 (p (if priority
1745 (xpath::parse-xnum priority)
1746 (expression-priority expression)))
1748 (progn
1749 (unless (and (numberp p)
1750 (not (xpath::inf-p p))
1751 (not (xpath::nan-p p)))
1752 (xslt-error "failed to parse priority"))
1753 (float p 1.0d0)))
1754 (template
1755 (make-template :match-expression expression
1756 :compiled-pattern compiled-pattern
1757 :import-priority *import-priority*
1758 :apply-imports-limit *apply-imports-limit*
1759 :priority p
1760 :position position
1761 :mode-qname mode
1762 :params param-bindings
1763 :body outer-body-thunk
1764 :n-variables n-variables)))
1765 (setf (xpath:pattern-value compiled-pattern)
1766 template)
1767 template))
1768 (cdr (xpath:parse-pattern-expression match)))))))))
1769 #+(or)
1770 (xuriella::parse-stylesheet #p"/home/david/src/lisp/xuriella/test.xsl")