Desktop tool for browsing account info from EVE-Online
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CDATA.java 7.7KB

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