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.

Text.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*--
  2. $Id: Text.java,v 1.25 2007/11/10 05:28:59 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. * Character-based XML content. Provides a modular, parentable method of
  48. * representing text. Text makes no guarantees about the underlying textual
  49. * representation of character data, but does expose that data as a Java String.
  50. *
  51. * @version $Revision: 1.25 $, $Date: 2007/11/10 05:28:59 $
  52. * @author Brett McLaughlin
  53. * @author Jason Hunter
  54. * @author Bradley S. Huffman
  55. */
  56. public class Text extends Content {
  57. private static final String CVS_ID =
  58. "@(#) $RCSfile: Text.java,v $ $Revision: 1.25 $ $Date: 2007/11/10 05:28:59 $ $Name: jdom_1_1 $";
  59. static final String EMPTY_STRING = "";
  60. /** The actual character content */
  61. // XXX See http://www.servlets.com/archive/servlet/ReadMsg?msgId=8612
  62. // from elharo for a description of why Java characters may not suffice
  63. // long term
  64. protected String value;
  65. /**
  66. * This is the protected, no-args constructor standard in all JDOM
  67. * classes. It allows subclassers to get a raw instance with no
  68. * initialization.
  69. */
  70. protected Text() { }
  71. /**
  72. * This constructor creates a new <code>Text</code> node, with the
  73. * supplied string value as it's character content.
  74. *
  75. * @param str the node's character content.
  76. * @throws IllegalDataException if <code>str</code> contains an
  77. * illegal character such as a vertical tab (as determined
  78. * by {@link org.jdom.Verifier#checkCharacterData})
  79. */
  80. public Text(String str) {
  81. setText(str);
  82. }
  83. /**
  84. * This returns the value of this <code>Text</code> node as a Java
  85. * <code>String</code>.
  86. *
  87. * @return <code>String</code> - character content of this node.
  88. */
  89. public String getText() {
  90. return value;
  91. }
  92. /**
  93. * This returns the textual content with all surrounding whitespace
  94. * removed. If only whitespace exists, the empty string is returned.
  95. *
  96. * @return trimmed text content or empty string
  97. */
  98. public String getTextTrim() {
  99. return getText().trim();
  100. }
  101. /**
  102. * This returns the textual content with all surrounding whitespace
  103. * removed and internal whitespace normalized to a single space. If
  104. * only whitespace exists, the empty string is returned.
  105. *
  106. * @return normalized text content or empty string
  107. */
  108. public String getTextNormalize() {
  109. return normalizeString(getText());
  110. }
  111. /**
  112. * This returns a new string with all surrounding whitespace
  113. * removed and internal whitespace normalized to a single space. If
  114. * only whitespace exists, the empty string is returned.
  115. * <p>
  116. * Per XML 1.0 Production 3 whitespace includes: #x20, #x9, #xD, #xA
  117. * </p>
  118. *
  119. * @param str string to be normalized.
  120. * @return normalized string or empty string
  121. */
  122. public static String normalizeString(String str) {
  123. if (str == null)
  124. return EMPTY_STRING;
  125. char[] c = str.toCharArray();
  126. char[] n = new char[c.length];
  127. boolean white = true;
  128. int pos = 0;
  129. for (int i = 0; i < c.length; i++) {
  130. if (" \t\n\r".indexOf(c[i]) != -1) {
  131. if (!white) {
  132. n[pos++] = ' ';
  133. white = true;
  134. }
  135. }
  136. else {
  137. n[pos++] = c[i];
  138. white = false;
  139. }
  140. }
  141. if (white && pos > 0) {
  142. pos--;
  143. }
  144. return new String(n, 0, pos);
  145. }
  146. /**
  147. * This will set the value of this <code>Text</code> node.
  148. *
  149. * @param str value for node's content.
  150. * @return the object on which the method was invoked
  151. * @throws IllegalDataException if <code>str</code> contains an
  152. * illegal character such as a vertical tab (as determined
  153. * by {@link org.jdom.Verifier#checkCharacterData})
  154. */
  155. public Text setText(String str) {
  156. String reason;
  157. if (str == null) {
  158. value = EMPTY_STRING;
  159. return this;
  160. }
  161. if ((reason = Verifier.checkCharacterData(str)) != null) {
  162. throw new IllegalDataException(str, "character content", reason);
  163. }
  164. value = str;
  165. return this;
  166. }
  167. /**
  168. * This will append character content to whatever content already
  169. * exists within this <code>Text</code> node.
  170. *
  171. * @param str character content to append.
  172. * @throws IllegalDataException if <code>str</code> contains an
  173. * illegal character such as a vertical tab (as determined
  174. * by {@link org.jdom.Verifier#checkCharacterData})
  175. */
  176. public void append(String str) {
  177. String reason;
  178. if (str == null) {
  179. return;
  180. }
  181. if ((reason = Verifier.checkCharacterData(str)) != null) {
  182. throw new IllegalDataException(str, "character content", reason);
  183. }
  184. if (str == EMPTY_STRING)
  185. value = str;
  186. else value += str;
  187. }
  188. /**
  189. * This will append the content of another <code>Text</code> node
  190. * to this node.
  191. *
  192. * @param text Text node to append.
  193. */
  194. public void append(Text text) {
  195. if (text == null) {
  196. return;
  197. }
  198. value += text.getText();
  199. }
  200. /**
  201. * Returns the XPath 1.0 string value of this element, which is the
  202. * text itself.
  203. *
  204. * @return the text
  205. */
  206. public String getValue() {
  207. return value;
  208. }
  209. /**
  210. * This returns a <code>String</code> representation of the
  211. * <code>Text</code> node, suitable for debugging. If the XML
  212. * representation of the <code>Text</code> node is desired,
  213. * either <code>{@link #getText}</code> or
  214. * {@link org.jdom.output.XMLOutputter#outputString(Text)}</code>
  215. * should be used.
  216. *
  217. * @return <code>String</code> - information about this node.
  218. */
  219. public String toString() {
  220. return new StringBuffer(64)
  221. .append("[Text: ")
  222. .append(getText())
  223. .append("]")
  224. .toString();
  225. }
  226. /**
  227. * This will return a clone of this <code>Text</code> node, with the
  228. * same character content, but no parent.
  229. *
  230. * @return <code>Text</code> - cloned node.
  231. */
  232. public Object clone() {
  233. Text text = (Text)super.clone();
  234. text.value = value;
  235. return text;
  236. }
  237. }