1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella
)
32 (defun map-namespace-declarations (fn element
)
33 (let ((parent (stp:parent element
)))
34 (maphash (lambda (prefix uri
)
35 (unless (and (typep parent
'stp
:element
)
36 (equal (stp:find-namespace prefix parent
) uri
))
37 (funcall fn prefix uri
)))
38 (cxml-stp-impl::collect-local-namespaces element
))))
40 (defun maybe-wrap-namespaces (child exprs
)
41 (if (typep child
'stp
:element
)
44 (map-namespace-declarations (lambda (prefix uri
)
45 (push (list prefix uri
) bindings
))
47 (stp:with-attributes
((erp "exclude-result-prefixes" *xsl
*))
49 (dolist (prefix (words (or erp
"")))
50 (when (equal prefix
"#default")
52 (push (or (stp:find-namespace prefix child
)
53 (xslt-error "namespace not found: ~A" prefix
))
55 (if (or bindings excluded-uris
)
56 `((xsl:with-namespaces
,bindings
57 (xsl:with-excluded-namespaces
,excluded-uris
62 (defun parse-body (node &optional
(start 0) (param-names '()))
63 (let ((n (stp:count-children-if
#'identity node
)))
66 (let ((child (stp:nth-child i node
)))
67 (maybe-wrap-namespaces
69 (if (namep child
"variable")
70 (only-with-attributes (name select
) child
71 (when (and select
(stp:list-children child
))
72 (xslt-error "variable with select and body"))
73 `((let ((,name
,(or select
74 `(progn ,@(parse-body child
)))))
75 (xsl:with-duplicates-check
(,name
)
76 ,@(recurse (1+ i
))))))
77 (cons (parse-instruction child
)
78 (recurse (1+ i
)))))))))
79 (let ((result (recurse start
)))
81 `((xsl:with-duplicates-check
(,@param-names
)
85 (defun parse-param (node)
87 (only-with-attributes (name select
) node
89 (xslt-error "name not specified for parameter"))
90 (when (and select
(stp:list-children node
))
91 (xslt-error "param with select and body"))
94 `(progn ,@(parse-body node
))))))
96 (defun parse-instruction (node)
101 ((equal (stp:namespace-uri node
) *xsl
*)
102 (let ((sym (find-symbol (stp:local-name node
) :xuriella
)))
105 (parse-instruction/xsl-element sym node
))
106 (*forwards-compatible-p
*
107 (parse-fallback-children node
))
109 (xslt-error "undefined instruction: ~A"
110 (stp:local-name node
))))))
111 ((find (stp:namespace-uri node
)
112 *extension-namespaces
*
114 (parse-fallback-children node
))
116 (parse-instruction/literal-element node
))))
117 (parent (stp:parent node
)))
118 (if (and (equal (stp:base-uri node
) (stp:base-uri parent
))
119 (equal (stp:namespace-uri parent
) *xsl
*)
120 (find-symbol (stp:local-name parent
) :xuriella
))
122 `(xsl:with-base-uri
,(stp:base-uri node
)
125 `(xsl:text
,(stp:data node
)))))
127 (defun parse-instruction/literal-element
(node)
129 `(xsl:literal-element
130 (,(stp:local-name node
)
131 ,(stp:namespace-uri node
)
132 ,(stp:namespace-prefix node
))
133 (xsl:use-attribute-sets
134 ,(stp:attribute-value node
"use-attribute-sets" *xsl
*))
135 ,@(loop for a in
(stp:list-attributes node
)
136 unless
(equal (stp:namespace-uri a
) *xsl
*)
137 collect
`(xsl:literal-attribute
139 ,(stp:namespace-uri a
)
140 ,(stp:namespace-prefix a
))
142 ,@(parse-body node
)))
143 (version (stp:attribute-value node
"version" *xsl
*))
145 (stp:with-attributes
((eep "extension-element-prefixes" *xsl
*))
147 (dolist (prefix (words (or eep
"")))
148 (when (equal prefix
"#default")
150 (push (or (stp:find-namespace prefix node
)
151 (xslt-error "namespace not found: ~A" prefix
))
155 `(xsl:with-extension-namespaces
,extensions
156 (xsl:with-excluded-namespaces
,extensions
160 `(xsl:with-version
,version
164 (defun parse-fallback-children (node)
167 for fallback in
(stp:filter-children
(of-name "fallback") node
)
168 append
(parse-body fallback
))))
173 "no fallback children in unknown element using forwards compatible processing")))))
175 (defmacro define-instruction-parser
(name (node-var) &body body
)
177 (setf (gethash ,(symbol-name name
) *available-instructions
*) t
)
178 (defmethod parse-instruction/xsl-element
179 ((.name.
(eql ',name
)) ,node-var
)
180 (declare (ignore .name.
))
183 (define-instruction-parser |fallback|
(node)
184 (only-with-attributes () node
187 (define-instruction-parser |apply-templates|
(node)
188 (only-with-attributes (select mode
) node
189 (multiple-value-bind (decls rest
)
192 for cons on
(stp:list-children node
)
193 for
(child . nil
) = cons
194 while
(namep child
"sort")
195 collect
(parse-sort child
) into decls
196 finally
(return (values decls cons
)))
197 `(xsl:apply-templates
198 (:select
,select
:mode
,mode
)
200 ,@(mapcar (lambda (clause)
201 (unless (namep clause
"with-param")
202 (xslt-error "undefined instruction: ~A"
203 (stp:local-name clause
)))
204 (parse-param clause
))
207 (define-instruction-parser |apply-imports|
(node)
208 `(xsl:apply-imports
))
210 (define-instruction-parser |call-template|
(node)
211 (only-with-attributes (name) node
213 ,name
,@(stp:map-children
'list
215 (if (namep clause
"with-param")
217 (xslt-error "undefined instruction: ~A"
218 (stp:local-name clause
))))
221 (define-instruction-parser |if|
(node)
222 (only-with-attributes (test) node
224 ,@(parse-body node
))))
226 (define-instruction-parser |choose|
(node)
227 (only-with-attributes () node
229 ,@(stp:map-children
'list
232 ((namep clause
"when")
233 (only-with-attributes (test) clause
235 ,@(parse-body clause
))))
236 ((namep clause
"otherwise")
237 `(t ,@(parse-body clause
)))
239 (xslt-error "invalid <choose> clause: ~A"
240 (stp:local-name clause
)))))
243 (define-instruction-parser |element|
(node)
244 (only-with-attributes (name namespace use-attribute-sets
) node
245 `(xsl:element
(,name
:namespace
,namespace
)
246 (xsl:use-attribute-sets
,use-attribute-sets
)
247 ,@(parse-body node
))))
249 (define-instruction-parser |attribute|
(node)
250 (only-with-attributes (name namespace
) node
251 `(xsl:attribute
(,name
:namespace
,namespace
)
252 ,@(parse-body node
))))
254 (define-instruction-parser |text|
(node)
255 (only-with-attributes (select disable-output-escaping
) node
256 (if (equal disable-output-escaping
"yes")
257 `(xsl:unescaped-text
,(stp:string-value node
))
258 `(xsl:text
,(stp:string-value node
)))))
260 (define-instruction-parser |comment|
(node)
261 (only-with-attributes () node
262 `(xsl:comment
,@(parse-body node
))))
264 (define-instruction-parser |processing-instruction|
(node)
265 (only-with-attributes (name) node
266 `(xsl:processing-instruction
,name
267 ,@(parse-body node
))))
269 (define-instruction-parser |value-of|
(node)
270 (only-with-attributes (select disable-output-escaping
) node
271 (if (equal disable-output-escaping
"yes")
272 `(xsl:unescaped-value-of
,select
)
273 `(xsl:value-of
,select
))))
275 (define-instruction-parser |copy-of|
(node)
276 (only-with-attributes (select) node
277 `(xsl:copy-of
,select
)))
279 (define-instruction-parser |copy|
(node)
280 (only-with-attributes (use-attribute-sets) node
282 (xsl:use-attribute-sets
,use-attribute-sets
)
283 ,@(parse-body node
))))
285 (define-instruction-parser |variable|
(node)
286 (xslt-error "unhandled xsl:variable"))
288 (define-instruction-parser |for-each|
(node)
289 (only-with-attributes (select) node
290 (multiple-value-bind (decls body-position
)
293 for child in
(stp:list-children node
)
294 while
(namep child
"sort")
295 collect
(parse-sort child
) into decls
296 finally
(return (values decls i
)))
297 `(xsl:for-each
,select
299 ,@(parse-body node body-position
)))))
301 (defun parse-sort (node)
302 (only-with-attributes (select lang data-type order case-order
) node
303 `(sort :select
,select
305 :data-type
,data-type
307 :case-order
,case-order
)))
309 (define-instruction-parser |message|
(node)
310 (only-with-attributes (terminate) node
311 (if (equal terminate
"yes")
312 `(xsl:terminate
,@(parse-body node
))
313 `(xsl:message
,@(parse-body node
)))))
315 (define-instruction-parser |number|
(node)
316 (only-with-attributes (level count from value format lang letter-value
317 grouping-separator grouping-size
)
319 `(xsl:number
:level
,level
325 :letter-value
,letter-value
326 :grouping-separator
,grouping-separator
327 :grouping-size
,grouping-size
)))
329 (define-instruction-parser |document|
(node)
330 (only-with-attributes
331 (href method indent doctype-public doctype-system
) node
332 `(xsl:document
(,href
:method
,method
334 :doctype-public
,doctype-public
335 :doctype-system
,doctype-system
)
336 ,@(parse-body node
))))