Check for disallowed attributes on <stylesheet>
[xuriella.git] / parser.lisp
blob8c70d1bd649f5a59f76b7f2f4c5f6de794f9bad0
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 (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)
42 (let ((bindings '())
43 (excluded-uris '()))
44 (map-namespace-declarations (lambda (prefix uri)
45 (push (list prefix uri) bindings))
46 child)
47 (stp:with-attributes ((erp "exclude-result-prefixes" *xsl*))
48 child
49 (dolist (prefix (words (or erp "")))
50 (when (equal prefix "#default")
51 (setf prefix nil))
52 (push (or (stp:find-namespace prefix child)
53 (xslt-error "namespace not found: ~A" prefix))
54 excluded-uris)))
55 (if (or bindings excluded-uris)
56 `((xsl:with-namespaces ,bindings
57 (xsl:with-excluded-namespaces ,excluded-uris
58 ,@exprs)))
59 exprs))
60 exprs))
62 (defun parse-body (node &optional (start 0) (param-names '()))
63 (let ((n (stp:count-children-if #'identity node)))
64 (labels ((recurse (i)
65 (when (< i n)
66 (let ((child (stp:nth-child i node)))
67 (maybe-wrap-namespaces
68 child
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)))
80 (if param-names
81 `((xsl:with-duplicates-check (,@param-names)
82 ,@result))
83 result)))))
85 (defun parse-param (node)
86 ;; FIXME: empty body?
87 (only-with-attributes (name select) node
88 (unless name
89 (xslt-error "name not specified for parameter"))
90 (when (and select (stp:list-children node))
91 (xslt-error "param with select and body"))
92 (list name
93 (or select
94 `(progn ,@(parse-body node))))))
96 (defun parse-instruction (node)
97 (typecase node
98 (stp:element
99 (let ((expr
100 (cond
101 ((equal (stp:namespace-uri node) *xsl*)
102 (let ((sym (find-symbol (stp:local-name node) :xuriella)))
103 (cond
104 (sym
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*
113 :test #'equal)
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))
121 expr
122 `(xsl:with-base-uri ,(stp:base-uri node)
123 ,expr))))
124 (stp:text
125 `(xsl:text ,(stp:data node)))))
127 (defun parse-instruction/literal-element (node)
128 (let ((le
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
138 (,(stp:local-name a)
139 ,(stp:namespace-uri a)
140 ,(stp:namespace-prefix a))
141 ,(stp:value a)))
142 ,@(parse-body node)))
143 (version (stp:attribute-value node "version" *xsl*))
144 (extensions '()))
145 (stp:with-attributes ((eep "extension-element-prefixes" *xsl*))
146 node
147 (dolist (prefix (words (or eep "")))
148 (when (equal prefix "#default")
149 (setf prefix nil))
150 (push (or (stp:find-namespace prefix node)
151 (xslt-error "namespace not found: ~A" prefix))
152 extensions)))
153 (when extensions
154 (setf le
155 `(xsl:with-extension-namespaces ,extensions
156 (xsl:with-excluded-namespaces ,extensions
157 ,le))))
158 (when version
159 (setf le
160 `(xsl:with-version ,version
161 ,le)))
162 le))
164 (defun parse-fallback-children (node)
165 (let ((fallbacks
166 (loop
167 for fallback in (stp:filter-children (of-name "fallback") node)
168 append (parse-body fallback))))
169 (if fallbacks
170 `(progn ,@fallbacks)
171 `(xsl:terminate
172 (xsl:text
173 "no fallback children in unknown element using forwards compatible processing")))))
175 (defmacro define-instruction-parser (name (node-var) &body body)
176 `(progn
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.))
181 ,@body)))
183 (define-instruction-parser |fallback| (node)
184 (only-with-attributes () node
185 '(progn)))
187 (define-instruction-parser |apply-templates| (node)
188 (only-with-attributes (select mode) node
189 (multiple-value-bind (decls rest)
190 (loop
191 for i from 0
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)
199 (declare ,@decls)
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))
205 rest)))))
207 (define-instruction-parser |apply-imports| (node)
208 `(xsl:apply-imports))
210 (define-instruction-parser |call-template| (node)
211 (only-with-attributes (name) node
212 `(xsl:call-template
213 ,name ,@(stp:map-children 'list
214 (lambda (clause)
215 (if (namep clause "with-param")
216 (parse-param clause)
217 (xslt-error "undefined instruction: ~A"
218 (stp:local-name clause))))
219 node))))
221 (define-instruction-parser |if| (node)
222 (only-with-attributes (test) node
223 `(when ,test
224 ,@(parse-body node))))
226 (define-instruction-parser |choose| (node)
227 (only-with-attributes () node
228 `(cond
229 ,@(stp:map-children 'list
230 (lambda (clause)
231 (cond
232 ((namep clause "when")
233 (only-with-attributes (test) clause
234 `(,test
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)))))
241 node))))
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
281 `(xsl:copy
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)
291 (loop
292 for i from 0
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
298 (declare ,@decls)
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
304 :lang ,lang
305 :data-type ,data-type
306 :order ,order
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)
318 node
319 `(xsl:number :level ,level
320 :count ,count
321 :from ,from
322 :value ,value
323 :format ,format
324 :lang ,lang
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
333 :indent ,indent
334 :doctype-public ,doctype-public
335 :doctype-system ,doctype-system)
336 ,@(parse-body node))))