Fixed (setf document-element) in the presence of non-element children
[cxml-stp.git] / attribute.lisp
blobb76b53204bd6d4f3e03f7083c43bbc48dd6edb6d
1 ;;; -*- show-trailing-whitespace: t; indent-tabs: nil -*-
3 ;;; Copyright (c) 2007 David Lichteblau. All rights reserved.
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 (in-package :cxml-stp-impl)
31 #+sbcl
32 (declaim (optimize (debug 2)))
35 ;;;; Class ATTRIBUTE
37 (defgeneric value (attribute)
38 (:documentation
39 "@arg[attribute]{an @class{attribute}}
40 @return{a string of XML characters}
41 @short{Returns the attribute's value.}"))
43 (defgeneric (setf value) (newval attribute)
44 (:documentation
45 "@arg[newval]{a string of XML characters}
46 @arg[attribute]{an @class{attribute}}
47 @return{the value}
48 @short{Sets the attribute's value.}"))
50 (defun make-attribute (value name &optional (uri ""))
51 "@arg[value]{a string containing XML characters only}
52 @arg[name]{a string, either a QName or an NCName}
53 @arg[uri]{a string, the namespace URI}
54 @return{an @class{attribute}}
55 @short{This function creates an attribute node of the given value and name.}
57 @see{element}"
58 (let ((result (make-instance 'attribute)))
59 (multiple-value-bind (prefix local-name)
60 (cxml::split-qname name)
61 (setf prefix (or prefix ""))
62 (setf (local-name result) local-name)
63 (rename-attribute result prefix uri)
64 (setf (value result) value))
65 result))
67 (defmethod copy ((node attribute))
68 (let ((result (make-instance 'attribute)))
69 (setf (%local-name result) (%local-name node))
70 (setf (%namespace-prefix result) (%namespace-prefix node))
71 (setf (%namespace-uri result) (%namespace-uri node))
72 (setf (value result) (value node))
73 result))
75 (defmethod detach ((node attribute))
76 (when (parent node)
77 (remove-attribute (parent node) node)))
79 (defmethod string-value ((node attribute))
80 (value node))
82 (defun xml-characters-p (str)
83 (declare (optimize speed (safety 0)))
84 (every (lambda (c)
85 (let ((code (char-code c)))
86 (or (eql code 9)
87 (eql code 10)
88 (eql code 13)
89 (<= 32 code #xd7ff)
90 #+rune-is-utf-16 (<= #xD800 code #xDFFF)
91 (<= #xe000 code #xfffd)
92 #-rune-is-utf-16 (<= #x10000 code #x10ffff))))
93 (the string str)))
95 (defmethod (setf value) :before (newval (node attribute))
96 (unless (xml-characters-p newval)
97 (stp-error "new attribute value includes characters that cannot be ~
98 represented in XML at all: ~S"
99 newval)))
101 (defmethod (setf local-name) (newval (node attribute))
102 (check-nc-name newval)
103 (when (equal newval "xmlns")
104 (stp-error "attempt to represent a namespace declaration as an ATTRIBUTE"))
105 (setf (%local-name node) newval))
107 (defun xor (a b)
108 (if a (not b) b))
110 (defun rename-attribute (attribute prefix uri)
111 "@arg[attribute]{the @class{attribute} to be renamed}
112 @arg[prefix]{string, an NCName}
113 @arg[uri]{a string, the namespace URI}
114 @return{the attribute}
115 @short{This function changed namespace prefix and URI of an attribute.}
117 Since there is no default namespace for attributes, prefix and uri must
118 be changed in the same step to rename an attribute with no namespace to
119 an attribute with both namespace prefix and URI.
121 @see{local-name}"
122 (unless prefix (setf prefix ""))
123 (unless uri (setf uri ""))
124 (check-uri-like uri)
125 (when (equal prefix "xmlns")
126 (stp-error "attempt to represent a namespace declaration as an ATTRIBUTE"))
127 (when (xor (equal uri "http://www.w3.org/XML/1998/namespace")
128 (equal prefix "xml"))
129 (stp-error "prefix/URI mismatch for `xml' namespace"))
130 (cond
131 ((zerop (length prefix))
132 (unless (zerop (length uri))
133 (stp-error "attribute with URI but no prefix"))
134 (values
135 (setf (%namespace-prefix attribute) "")
136 (setf (%namespace-uri attribute) "")))
137 ((zerop (length uri))
138 (stp-error "attribute with prefix but no URI"))
140 (let ((parent (parent attribute)))
141 (when parent
142 (let ((old (find-local-namespace prefix parent)))
143 (when (and old (not (equal uri old)))
144 (stp-error "conflicting namespaces when renaming attribute")))))
145 (check-nc-name prefix)
146 (check-namespace-uri uri)
147 (values
148 (setf (%namespace-prefix attribute) prefix)
149 (setf (%namespace-uri attribute) uri))))
150 attribute)
152 (defmethod serialize ((node attribute) handler)
153 (stp-error "attempt to serialize an attribute in isolation"))
156 ;;; printing
158 (defmethod slots-for-print-object append ((node attribute))
159 '((:value value)))
161 (defreader attribute (value)
162 (setf (value this) value))