Namespace nodes don't have a namespace-uri()
[xuriella.git] / unparse.lisp
blob9c37fb092627ab0b568af5da12a3494db1745875
1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; Copyright (c) 2004 David Lichteblau (for headcraft.de)
5 ;;; All rights reserved.
7 ;;; Redistribution and use in source and binary forms, with or without
8 ;;; modification, are permitted provided that the following conditions
9 ;;; are met:
10 ;;;
11 ;;; * Redistributions of source code must retain the above copyright
12 ;;; notice, this list of conditions and the following disclaimer.
13 ;;;
14 ;;; * Redistributions in binary form must reproduce the above
15 ;;; copyright notice, this list of conditions and the following
16 ;;; disclaimer in the documentation and/or other materials
17 ;;; provided with the distribution.
18 ;;;
19 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
20 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 (in-package :xuriella)
34 ;;; Convenience functions for serialization to SAX, similar in syntax
35 ;;; to what cxml offers, but with namespace handling as required for XSLT.
37 (defvar *current-element*)
38 (defvar *sink*)
39 (defvar *start-tag-written-p*)
41 (defmacro with-xml-output (sink &body body)
42 `(invoke-with-xml-output (lambda () ,@body) ,sink))
44 (defmacro with-output-sink-bound ((var) &body body)
45 `(invoke-with-output-sink-bound (lambda (,var) ,@body)))
47 (defun invoke-with-xml-output (fn sink)
48 (let ((*sink* sink)
49 (*current-element* nil)
50 (*start-tag-written-p* t))
51 (sax:start-document *sink*)
52 (funcall fn)
53 (sax:end-document *sink*)))
55 (defun invoke-with-output-sink-bound (fn)
56 (maybe-emit-start-tag)
57 (funcall fn *sink*))
59 (defmacro with-element
60 ((local-name uri &key suggested-prefix extra-namespaces process-aliases)
61 &body body)
62 `(invoke-with-element (lambda () ,@body)
63 ,local-name
64 ,uri
65 :suggested-prefix ,suggested-prefix
66 :extra-namespaces ,extra-namespaces
67 :process-aliases ,process-aliases))
69 (defun doctype (name public-id system-id &optional internal-subset)
70 (sax:start-dtd *sink* name public-id system-id)
71 (when internal-subset
72 (sax:unparsed-internal-subset *sink* internal-subset))
73 (sax:end-dtd *sink*))
75 (defun maybe-emit-start-tag ()
76 (let ((elt *current-element*))
77 (when (and elt (not *start-tag-written-p*))
78 (setf *start-tag-written-p* t)
79 (let* ((local-name (sink-element-local-name elt))
80 (uri (sink-element-uri elt))
81 (suggested-prefix (sink-element-suggested-prefix elt))
82 (prefix (ensure-prefix-for-uri elt uri suggested-prefix))
83 (qname (if (plusp (length prefix))
84 (concatenate 'string prefix ":" local-name)
85 local-name))
86 (attrs '()))
87 (setf (sink-element-actual-qname elt) qname)
88 (dolist (attr (sink-element-attributes elt))
89 (push (convert-attribute elt attr) attrs))
90 (loop
91 for (prefix . uri) in (sink-element-new-namespaces elt) do
92 (sax:start-prefix-mapping *sink* prefix uri)
93 (push (make-xmlns-attribute prefix uri) attrs))
94 (sax:start-element *sink* uri local-name qname attrs)))))
96 (defun convert-attribute (elt attr)
97 (let* ((local-name (sink-attribute-local-name attr))
98 (uri (sink-attribute-uri attr))
99 (suggested-prefix (sink-attribute-suggested-prefix attr))
100 (prefix (ensure-prefix-for-uri elt uri suggested-prefix))
101 (qname (if (plusp (length prefix))
102 (concatenate 'string prefix ":" local-name)
103 local-name)))
104 (sax:make-attribute :namespace-uri uri
105 :local-name local-name
106 :qname qname
107 :value (sink-attribute-value attr))))
109 (defun sink-element-find-uri (prefix elt)
110 (cdr
111 (find prefix
112 (sink-element-all-namespaces elt)
113 :key #'car
114 :test #'equal)))
116 (defun ensure-prefix-for-uri (elt uri &optional suggested-prefix)
117 (let* ((prefix-cons
118 (find uri
119 (sink-element-all-namespaces elt)
120 :key #'cdr
121 :test #'equal))
122 (prefix (car prefix-cons))
123 (cross-check
124 (when prefix-cons
125 (sink-element-find-uri prefix elt))))
126 (if (and prefix-cons (equal cross-check uri))
127 prefix
128 (loop
129 for i from 0
130 for prefix = suggested-prefix then (format nil "ns-~D" i)
131 while
132 (sink-element-find-uri prefix elt)
133 finally
134 (push-sink-element-namespace elt prefix uri)
135 (return prefix)))))
137 (defun make-xmlns-attribute (prefix uri)
138 (sax:make-attribute
139 :namespace-uri #"http://www.w3.org/2000/xmlns/"
140 :local-name prefix
141 :qname (if (zerop (length prefix))
142 "xmlns"
143 (concatenate 'string "xmlns:" prefix))
144 :value uri))
146 (defstruct sink-element
147 local-name
149 suggested-prefix
150 all-namespaces
151 new-namespaces
152 attributes
153 actual-qname)
155 (defstruct sink-attribute
156 local-name
158 suggested-prefix
159 value)
161 (defparameter *initial-namespaces*
162 '((nil . "")
163 ("xmlns" . #"http://www.w3.org/2000/xmlns/")
164 ("xml" . #"http://www.w3.org/XML/1998/namespace")))
166 (defun invoke-with-element
167 (fn local-name uri &key suggested-prefix extra-namespaces process-aliases)
168 (check-type local-name string)
169 (check-type uri string)
170 (check-type suggested-prefix (or null string))
171 (maybe-emit-start-tag)
172 (when process-aliases
173 (setf uri (unalias-uri uri)))
174 (let* ((parent *current-element*)
175 (elt (make-sink-element
176 :local-name local-name
177 :uri uri
178 :suggested-prefix suggested-prefix
179 :all-namespaces (if parent
180 (sink-element-all-namespaces parent)
181 *initial-namespaces*)
182 :new-namespaces nil
183 :attributes nil))
184 (*current-element* elt)
185 (*start-tag-written-p* nil))
186 (process-extra-namespaces elt extra-namespaces process-aliases)
187 (multiple-value-prog1
188 (funcall fn)
189 (maybe-emit-start-tag)
190 (sax:end-element *sink* uri local-name (sink-element-actual-qname elt))
191 (loop
192 for (prefix . uri) in (sink-element-new-namespaces elt) do
193 (sax:end-prefix-mapping *sink* prefix)))))
195 (defun process-extra-namespace (elt prefix uri process-aliases)
196 (when process-aliases
197 (setf uri (unalias-uri uri)))
198 (unless
199 ;; allow earlier conses in extra-namespaces to hide later ones.
200 ;; FIXME: add a good explanation here why we need to do this both
201 ;; here and in remove-extra-namespaces.
202 (find prefix
203 (sink-element-new-namespaces elt)
204 :key #'car
205 :test #'equal)
206 (let ((previous (sink-element-find-uri prefix elt)))
207 (unless
208 ;; no need to declare what has already been done
209 (equal uri previous)
210 (push-sink-element-namespace elt prefix uri)))))
212 (defun process-extra-namespaces (elt extra-namespaces process-aliases)
213 (loop for (prefix . uri) in extra-namespaces do
214 (process-extra-namespace elt prefix uri process-aliases)))
216 (defun push-sink-element-namespace (elt prefix uri)
217 (cond
218 ((equal prefix "xml")
219 (assert (equal uri "http://www.w3.org/XML/1998/namespace")))
220 ((equal prefix "xmlns")
221 (assert (equal uri "http://www.w3.org/2000/xmlns/")))
223 (let ((cons (cons prefix uri)))
224 (push cons (sink-element-all-namespaces elt))
225 (push cons (sink-element-new-namespaces elt))))))
227 (defun write-attribute
228 (local-name uri value &key suggested-prefix process-aliases)
229 (check-type local-name string)
230 (check-type uri string)
231 (check-type value string)
232 (check-type suggested-prefix (or null string))
233 (when process-aliases
234 (setf uri (unalias-uri uri)))
235 (cond
236 ((null *current-element*)
237 (xslt-error "attribute outside of element"))
238 (*start-tag-written-p*
239 (xslt-cerror "attribute after start tag"))
240 ((equal local-name "xmlns")
241 (xslt-error "attribute named xmlns"))
243 (setf (sink-element-attributes *current-element*)
244 (cons (make-sink-attribute :local-name local-name
245 :uri uri
246 :suggested-prefix suggested-prefix
247 :value value)
248 (delete-if (lambda (x)
249 (and (equal (sink-attribute-local-name x)
250 local-name)
251 (equal (sink-attribute-uri x) uri)))
252 (sink-element-attributes *current-element*)))))))
254 (defun write-extra-namespace (prefix uri process-aliases)
255 (check-type prefix string)
256 (check-type uri string)
257 (cond
258 ((null *current-element*)
259 (xslt-error "attribute outside of element"))
260 (*start-tag-written-p*
261 (xslt-cerror "namespace after start tag"))
263 (process-extra-namespace *current-element* prefix uri process-aliases))))
265 (defun write-text (data)
266 (maybe-emit-start-tag)
267 (sax:characters *sink* data)
268 data)
270 (defun write-comment (data)
271 (maybe-emit-start-tag)
272 (sax:comment *sink* data)
273 data)
275 (defun write-processing-instruction (target data)
276 (maybe-emit-start-tag)
277 (sax:processing-instruction *sink* target data)
278 data)
280 (defun write-unescaped (str)
281 (maybe-emit-start-tag)
282 (sax:unescaped *sink* str))