Source code: org/jdom/CDATA.java
1 /*--
2
3 $Id: CDATA.java,v 1.30 2004/02/27 11:32:57 jhunter Exp $
4
5 Copyright (C) 2000-2004 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.30 $, $Date: 2004/02/27 11:32:57 $
67 * @author Dan Schaffer
68 * @author Brett McLaughlin
69 * @author Jason Hunter
70 * @author Bradley S. Huffman
71 */
72 public class CDATA extends Text {
73
74 private static final String CVS_ID =
75 "@(#) $RCSfile: CDATA.java,v $ $Revision: 1.30 $ $Date: 2004/02/27 11:32:57 $ $Name: jdom_1_0 $";
76
77 /**
78 * This is the protected, no-args constructor standard in all JDOM
79 * classes. It allows subclassers to get a raw instance with no
80 * initialization.
81 */
82 protected CDATA() { }
83
84 /**
85 * This constructor creates a new <code>CDATA</code> node, with the
86 * supplied string value as it's character content.
87 *
88 * @param str the node's character content.
89 * @throws IllegalDataException if <code>str</code> contains an
90 * illegal character such as a vertical tab (as determined
91 * by {@link org.jdom.Verifier#checkCharacterData})
92 * or the CDATA end delimiter <code>]]></code>.
93 */
94 public CDATA(String str) {
95 setText(str);
96 }
97
98 /**
99 * This will set the value of this <code>CDATA</code> node.
100 *
101 * @param str value for node's content.
102 * @return the object on which the method was invoked
103 * @throws IllegalDataException if <code>str</code> contains an
104 * illegal character such as a vertical tab (as determined
105 * by {@link org.jdom.Verifier#checkCharacterData})
106 * or the CDATA end delimiter <code>]]></code>.
107 */
108 public Text setText(String str) {
109 // Overrides Text.setText() because this needs to check CDATA
110 // rules are enforced. We could have a separate Verifier check
111 // for CDATA beyond Text and call that alone before super.setText().
112
113 String reason;
114
115 if (str == null) {
116 value = EMPTY_STRING;
117 return this;
118 }
119
120 if ((reason = Verifier.checkCDATASection(str)) != null) {
121 throw new IllegalDataException(str, "CDATA section", reason);
122 }
123 value = str;
124 return this;
125 }
126
127 /**
128 * This will append character content to whatever content already
129 * exists within this <code>CDATA</code> node.
130 *
131 * @param str character content to append.
132 * @throws IllegalDataException if <code>str</code> contains an
133 * illegal character such as a vertical tab (as determined
134 * by {@link org.jdom.Verifier#checkCharacterData})
135 * or the CDATA end delimiter <code>]]></code>.
136 */
137 public void append(String str) {
138 // Overrides Text.setText() because this needs to check CDATA
139 // rules are enforced. We could have a separate Verifier check
140 // for CDATA beyond Text and call that alone before super.setText().
141
142 String reason;
143
144 if (str == null) {
145 return;
146 }
147 if ((reason = Verifier.checkCDATASection(str)) != null) {
148 throw new IllegalDataException(str, "CDATA section", reason);
149 }
150
151 if (value == EMPTY_STRING)
152 value = str;
153 else value += str;
154 }
155
156 /**
157 * This returns a <code>String</code> representation of the
158 * <code>CDATA</code> node, suitable for debugging. If the XML
159 * representation of the <code>CDATA</code> node is desired,
160 * either <code>{@link #getText}</code> or
161 * {@link org.jdom.output.XMLOutputter#output(CDATA, java.io.Writer)}</code>
162 * should be used.
163 *
164 * @return <code>String</code> - information about this node.
165 */
166 public String toString() {
167 return new StringBuffer(64)
168 .append("[CDATA: ")
169 .append(getText())
170 .append("]")
171 .toString();
172 }
173 }