0
|
1 /*--
|
|
2
|
|
3 $Id: CDATA.java,v 1.32 2007/11/10 05:28:58 jhunter Exp $
|
|
4
|
|
5 Copyright (C) 2000-2007 Jason Hunter & Brett McLaughlin.
|
|
6 All rights reserved.
|
|
7
|
|
8 Redistribution and use in source and binary forms, with or without
|
|
9 modification, are permitted provided that the following conditions
|
|
10 are met:
|
|
11
|
|
12 1. Redistributions of source code must retain the above copyright
|
|
13 notice, this list of conditions, and the following disclaimer.
|
|
14
|
|
15 2. Redistributions in binary form must reproduce the above copyright
|
|
16 notice, this list of conditions, and the disclaimer that follows
|
|
17 these conditions in the documentation and/or other materials
|
|
18 provided with the distribution.
|
|
19
|
|
20 3. The name "JDOM" must not be used to endorse or promote products
|
|
21 derived from this software without prior written permission. For
|
|
22 written permission, please contact <request_AT_jdom_DOT_org>.
|
|
23
|
|
24 4. Products derived from this software may not be called "JDOM", nor
|
|
25 may "JDOM" appear in their name, without prior written permission
|
|
26 from the JDOM Project Management <request_AT_jdom_DOT_org>.
|
|
27
|
|
28 In addition, we request (but do not require) that you include in the
|
|
29 end-user documentation provided with the redistribution and/or in the
|
|
30 software itself an acknowledgement equivalent to the following:
|
|
31 "This product includes software developed by the
|
|
32 JDOM Project (http://www.jdom.org/)."
|
|
33 Alternatively, the acknowledgment may be graphical using the logos
|
|
34 available at http://www.jdom.org/images/logos.
|
|
35
|
|
36 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
37 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
38 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
39 DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
|
|
40 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
41 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
42 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
43 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
44 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
45 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
46 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
47 SUCH DAMAGE.
|
|
48
|
|
49 This software consists of voluntary contributions made by many
|
|
50 individuals on behalf of the JDOM Project and was originally
|
|
51 created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
|
|
52 Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
|
|
53 on the JDOM Project, please see <http://www.jdom.org/>.
|
|
54
|
|
55 */
|
|
56
|
|
57 package org.jdom;
|
|
58
|
|
59 /**
|
|
60 * An XML CDATA section. Represents character-based content within an XML
|
|
61 * document that should be output within special CDATA tags. Semantically it's
|
|
62 * identical to a simple {@link Text} object, but output behavior is different.
|
|
63 * CDATA makes no guarantees about the underlying textual representation of
|
|
64 * character data, but does expose that data as a Java String.
|
|
65 *
|
|
66 * @version $Revision: 1.32 $, $Date: 2007/11/10 05:28:58 $
|
|
67 * @author Dan Schaffer
|
|
68 * @author Brett McLaughlin
|
|
69 * @author Jason Hunter
|
|
70 * @author Bradley S. Huffman
|
|
71 * @author Victor Toni
|
|
72 */
|
|
73 public class CDATA extends Text {
|
|
74
|
|
75 private static final String CVS_ID =
|
|
76 "@(#) $RCSfile: CDATA.java,v $ $Revision: 1.32 $ $Date: 2007/11/10 05:28:58 $ $Name: jdom_1_1_1 $";
|
|
77
|
|
78 /**
|
|
79 * This is the protected, no-args constructor standard in all JDOM
|
|
80 * classes. It allows subclassers to get a raw instance with no
|
|
81 * initialization.
|
|
82 */
|
|
83 protected CDATA() { }
|
|
84
|
|
85 /**
|
|
86 * This constructor creates a new <code>CDATA</code> node, with the
|
|
87 * supplied string value as it's character content.
|
|
88 *
|
|
89 * @param string the node's character content.
|
|
90 * @throws IllegalDataException if <code>str</code> contains an
|
|
91 * illegal character such as a vertical tab (as determined
|
|
92 * by {@link org.jdom.Verifier#checkCharacterData})
|
|
93 * or the CDATA end delimiter <code>]]></code>.
|
|
94 */
|
|
95 public CDATA(final String string) {
|
|
96 setText(string);
|
|
97 }
|
|
98
|
|
99 /**
|
|
100 * This will set the value of this <code>CDATA</code> node.
|
|
101 *
|
|
102 * @param str value for node's content.
|
|
103 * @return the object on which the method was invoked
|
|
104 * @throws IllegalDataException if <code>str</code> contains an
|
|
105 * illegal character such as a vertical tab (as determined
|
|
106 * by {@link org.jdom.Verifier#checkCharacterData})
|
|
107 * or the CDATA end delimiter <code>]]></code>.
|
|
108 */
|
|
109 public Text setText(final String str) {
|
|
110 // Overrides Text.setText() because this needs to check that CDATA rules
|
|
111 // are enforced. We could have a separate Verifier check for CDATA
|
|
112 // beyond Text and call that alone before super.setText().
|
|
113
|
|
114 if (str == null || "".equals(str)) {
|
|
115 value = EMPTY_STRING;
|
|
116 return this;
|
|
117 }
|
|
118
|
|
119 final String reason = Verifier.checkCDATASection(str);
|
|
120 if (reason != null) {
|
|
121 throw new IllegalDataException(str, "CDATA section", reason);
|
|
122 }
|
|
123
|
|
124 value = str;
|
|
125
|
|
126 return this;
|
|
127 }
|
|
128
|
|
129 /**
|
|
130 * This will append character content to whatever content already
|
|
131 * exists within this <code>CDATA</code> node.
|
|
132 *
|
|
133 * @param str character content to append.
|
|
134 * @throws IllegalDataException if <code>str</code> contains an
|
|
135 * illegal character such as a vertical tab (as determined
|
|
136 * by {@link org.jdom.Verifier#checkCharacterData})
|
|
137 * or the CDATA end delimiter <code>]]></code>.
|
|
138 */
|
|
139 public void append(final String str) {
|
|
140 // Overrides Text.append(String) because this needs to check that CDATA
|
|
141 // rules are enforced. We could have a separate Verifier check for CDATA
|
|
142 // beyond Text and call that alone before super.setText().
|
|
143
|
|
144 if (str == null || "".equals(str)) {
|
|
145 return;
|
|
146 }
|
|
147
|
|
148 // we need a temp value to ensure that the value is changed _after_
|
|
149 // validation
|
|
150 final String tmpValue;
|
|
151 if (value == EMPTY_STRING) {
|
|
152 tmpValue = str;
|
|
153 } else {
|
|
154 tmpValue = value + str;
|
|
155 }
|
|
156
|
|
157 // we have to do late checking since the end of a CDATA section could
|
|
158 // have been created by concating both strings:
|
|
159 // "]" + "]>"
|
|
160 // or
|
|
161 // "]]" + ">"
|
|
162 // TODO: maybe this could be optimized for this two cases
|
|
163 final String reason = Verifier.checkCDATASection(tmpValue);
|
|
164 if (reason != null) {
|
|
165 throw new IllegalDataException(str, "CDATA section", reason);
|
|
166 }
|
|
167
|
|
168 value = tmpValue;
|
|
169 }
|
|
170
|
|
171 /**
|
|
172 * This will append the content of another <code>Text</code> node
|
|
173 * to this node.
|
|
174 *
|
|
175 * @param text Text node to append.
|
|
176 */
|
|
177 public void append(final Text text) {
|
|
178 // Overrides Text.append(Text) because this needs to check that CDATA
|
|
179 // rules are enforced. We could have a separate Verifier check for CDATA
|
|
180 // beyond Text and call that alone before super.setText().
|
|
181
|
|
182 if (text == null) {
|
|
183 return;
|
|
184 }
|
|
185 append(text.getText());
|
|
186 }
|
|
187
|
|
188 /**
|
|
189 * This returns a <code>String</code> representation of the
|
|
190 * <code>CDATA</code> node, suitable for debugging. If the XML
|
|
191 * representation of the <code>CDATA</code> node is desired,
|
|
192 * either <code>{@link #getText}</code> or
|
|
193 * {@link org.jdom.output.XMLOutputter#output(CDATA, java.io.Writer)}</code>
|
|
194 * should be used.
|
|
195 *
|
|
196 * @return <code>String</code> - information about this node.
|
|
197 */
|
|
198 public String toString() {
|
|
199 return new StringBuffer(64)
|
|
200 .append("[CDATA: ")
|
|
201 .append(getText())
|
|
202 .append("]")
|
|
203 .toString();
|
|
204 }
|
|
205 }
|