annotate NGSrich_0.5.5/src/org/jdom/CDATA.java @ 0:89ad0a9cca52 default tip

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