001: /*--
002:
003: $Id: CDATA.java,v 1.1 2005/04/27 09:32:39 wittek Exp $
004:
005: Copyright (C) 2000-2004 Jason Hunter & Brett McLaughlin.
006: All rights reserved.
007:
008: Redistribution and use in source and binary forms, with or without
009: modification, are permitted provided that the following conditions
010: are met:
011:
012: 1. Redistributions of source code must retain the above copyright
013: notice, this list of conditions, and the following disclaimer.
014:
015: 2. Redistributions in binary form must reproduce the above copyright
016: notice, this list of conditions, and the disclaimer that follows
017: these conditions in the documentation and/or other materials
018: provided with the distribution.
019:
020: 3. The name "JDOM" must not be used to endorse or promote products
021: derived from this software without prior written permission. For
022: written permission, please contact <request_AT_jdom_DOT_org>.
023:
024: 4. Products derived from this software may not be called "JDOM", nor
025: may "JDOM" appear in their name, without prior written permission
026: from the JDOM Project Management <request_AT_jdom_DOT_org>.
027:
028: In addition, we request (but do not require) that you include in the
029: end-user documentation provided with the redistribution and/or in the
030: software itself an acknowledgement equivalent to the following:
031: "This product includes software developed by the
032: JDOM Project (http://www.jdom.org/)."
033: Alternatively, the acknowledgment may be graphical using the logos
034: available at http://www.jdom.org/images/logos.
035:
036: THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: DISCLAIMED. IN NO EVENT SHALL THE JDOM AUTHORS OR THE PROJECT
040: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: SUCH DAMAGE.
048:
049: This software consists of voluntary contributions made by many
050: individuals on behalf of the JDOM Project and was originally
051: created by Jason Hunter <jhunter_AT_jdom_DOT_org> and
052: Brett McLaughlin <brett_AT_jdom_DOT_org>. For more information
053: on the JDOM Project, please see <http://www.jdom.org/>.
054:
055: */
056:
057: package org.jdom;
058:
059: /**
060: * An XML CDATA section. Represents character-based content within an XML
061: * document that should be output within special CDATA tags. Semantically it's
062: * identical to a simple {@link Text} object, but output behavior is different.
063: * CDATA makes no guarantees about the underlying textual representation of
064: * character data, but does expose that data as a Java String.
065: *
066: * @version $Revision: 1.1 $, $Date: 2005/04/27 09:32:39 $
067: * @author Dan Schaffer
068: * @author Brett McLaughlin
069: * @author Jason Hunter
070: * @author Bradley S. Huffman
071: */
072: public class CDATA extends Text {
073:
074: private static final String CVS_ID = "@(#) $RCSfile: CDATA.java,v $ $Revision: 1.1 $ $Date: 2005/04/27 09:32:39 $ $Name: $";
075:
076: /**
077: * This is the protected, no-args constructor standard in all JDOM
078: * classes. It allows subclassers to get a raw instance with no
079: * initialization.
080: */
081: protected CDATA() {
082: }
083:
084: /**
085: * This constructor creates a new <code>CDATA</code> node, with the
086: * supplied string value as it's character content.
087: *
088: * @param str the node's character content.
089: * @throws IllegalDataException if <code>str</code> contains an
090: * illegal character such as a vertical tab (as determined
091: * by {@link org.jdom.Verifier#checkCharacterData})
092: * or the CDATA end delimiter <code>]]></code>.
093: */
094: public CDATA(String str) {
095: setText(str);
096: }
097:
098: /**
099: * 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
154: value += str;
155: }
156:
157: /**
158: * This returns a <code>String</code> representation of the
159: * <code>CDATA</code> node, suitable for debugging. If the XML
160: * representation of the <code>CDATA</code> node is desired,
161: * either <code>{@link #getText}</code> or
162: * {@link org.jdom.output.XMLOutputter#output(CDATA, java.io.Writer)}</code>
163: * should be used.
164: *
165: * @return <code>String</code> - information about this node.
166: */
167: public String toString() {
168: return new StringBuffer(64).append("[CDATA: ")
169: .append(getText()).append("]").toString();
170: }
171: }
|