1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: XMLStringDefault.java,v 1.3 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. import java.util.Locale;
  21. /**
  22. * The default implementation of the XMLString interface,
  23. * which is just a simple wrapper of a String object.
  24. */
  25. public class XMLStringDefault implements XMLString
  26. {
  27. private String m_str;
  28. /**
  29. * Create a XMLStringDefault object from a String
  30. */
  31. public XMLStringDefault(String str)
  32. {
  33. m_str = str;
  34. }
  35. /**
  36. * Directly call the
  37. * characters method on the passed ContentHandler for the
  38. * string-value. Multiple calls to the
  39. * ContentHandler's characters methods may well occur for a single call to
  40. * this method.
  41. *
  42. * @param ch A non-null reference to a ContentHandler.
  43. *
  44. * @throws org.xml.sax.SAXException
  45. */
  46. public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
  47. throws org.xml.sax.SAXException
  48. {
  49. }
  50. /**
  51. * Directly call the
  52. * comment method on the passed LexicalHandler for the
  53. * string-value.
  54. *
  55. * @param lh A non-null reference to a LexicalHandler.
  56. *
  57. * @throws org.xml.sax.SAXException
  58. */
  59. public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
  60. throws org.xml.sax.SAXException
  61. {
  62. }
  63. /**
  64. * Conditionally trim all leading and trailing whitespace in the specified String.
  65. * All strings of white space are
  66. * replaced by a single space character (#x20), except spaces after punctuation which
  67. * receive double spaces if doublePunctuationSpaces is true.
  68. * This function may be useful to a formatter, but to get first class
  69. * results, the formatter should probably do it's own white space handling
  70. * based on the semantics of the formatting object.
  71. *
  72. * @param trimHead Trim leading whitespace?
  73. * @param trimTail Trim trailing whitespace?
  74. * @param doublePunctuationSpaces Use double spaces for punctuation?
  75. * @return The trimmed string.
  76. */
  77. public XMLString fixWhiteSpace(boolean trimHead,
  78. boolean trimTail,
  79. boolean doublePunctuationSpaces)
  80. {
  81. return new XMLStringDefault(m_str.trim());
  82. }
  83. /**
  84. * Returns the length of this string.
  85. *
  86. * @return the length of the sequence of characters represented by this
  87. * object.
  88. */
  89. public int length()
  90. {
  91. return m_str.length();
  92. }
  93. /**
  94. * Returns the character at the specified index. An index ranges
  95. * from <code>0</code> to <code>length() - 1</code>. The first character
  96. * of the sequence is at index <code>0</code>, the next at index
  97. * <code>1</code>, and so on, as for array indexing.
  98. *
  99. * @param index the index of the character.
  100. * @return the character at the specified index of this string.
  101. * The first character is at index <code>0</code>.
  102. * @exception IndexOutOfBoundsException if the <code>index</code>
  103. * argument is negative or not less than the length of this
  104. * string.
  105. */
  106. public char charAt(int index)
  107. {
  108. return m_str.charAt(index);
  109. }
  110. /**
  111. * Copies characters from this string into the destination character
  112. * array.
  113. *
  114. * @param srcBegin index of the first character in the string
  115. * to copy.
  116. * @param srcEnd index after the last character in the string
  117. * to copy.
  118. * @param dst the destination array.
  119. * @param dstBegin the start offset in the destination array.
  120. * @exception IndexOutOfBoundsException If any of the following
  121. * is true:
  122. * <ul><li><code>srcBegin</code> is negative.
  123. * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
  124. * <li><code>srcEnd</code> is greater than the length of this
  125. * string
  126. * <li><code>dstBegin</code> is negative
  127. * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
  128. * <code>dst.length</code></ul>
  129. * @exception NullPointerException if <code>dst</code> is <code>null</code>
  130. */
  131. public void getChars(int srcBegin, int srcEnd, char dst[],
  132. int dstBegin)
  133. {
  134. int destIndex = dstBegin;
  135. for (int i = srcBegin; i < srcEnd; i++)
  136. {
  137. dst[destIndex++] = m_str.charAt(i);
  138. }
  139. }
  140. /**
  141. * Compares this string to the specified object.
  142. * The result is <code>true</code> if and only if the argument is not
  143. * <code>null</code> and is a <code>String</code> object that represents
  144. * the same sequence of characters as this object.
  145. *
  146. * @param anObject the object to compare this <code>String</code>
  147. * against.
  148. * @return <code>true</code> if the <code>String </code>are equal;
  149. * <code>false</code> otherwise.
  150. * @see java.lang.String#compareTo(java.lang.String)
  151. * @see java.lang.String#equalsIgnoreCase(java.lang.String)
  152. */
  153. public boolean equals(XMLString anObject)
  154. {
  155. return m_str.equals(anObject.toString());
  156. }
  157. /**
  158. * Compares this string to the specified object.
  159. * The result is <code>true</code> if and only if the argument is not
  160. * <code>null</code> and is a <code>String</code> object that represents
  161. * the same sequence of characters as this object.
  162. *
  163. * @param anObject the object to compare this <code>String</code>
  164. * against.
  165. * @return <code>true</code> if the <code>String </code>are equal;
  166. * <code>false</code> otherwise.
  167. * @see java.lang.String#compareTo(java.lang.String)
  168. * @see java.lang.String#equalsIgnoreCase(java.lang.String)
  169. */
  170. public boolean equals(Object anObject)
  171. {
  172. return m_str.equals(anObject);
  173. }
  174. /**
  175. * Compares this <code>String</code> to another <code>String</code>,
  176. * ignoring case considerations. Two strings are considered equal
  177. * ignoring case if they are of the same length, and corresponding
  178. * characters in the two strings are equal ignoring case.
  179. *
  180. * @param anotherString the <code>String</code> to compare this
  181. * <code>String</code> against.
  182. * @return <code>true</code> if the argument is not <code>null</code>
  183. * and the <code>String</code>s are equal,
  184. * ignoring case; <code>false</code> otherwise.
  185. * @see #equals(Object)
  186. * @see java.lang.Character#toLowerCase(char)
  187. * @see java.lang.Character#toUpperCase(char)
  188. */
  189. public boolean equalsIgnoreCase(String anotherString)
  190. {
  191. return m_str.equalsIgnoreCase(anotherString);
  192. }
  193. /**
  194. * Compares two strings lexicographically.
  195. *
  196. * @param anotherString the <code>String</code> to be compared.
  197. * @return the value <code>0</code> if the argument string is equal to
  198. * this string; a value less than <code>0</code> if this string
  199. * is lexicographically less than the string argument; and a
  200. * value greater than <code>0</code> if this string is
  201. * lexicographically greater than the string argument.
  202. * @exception java.lang.NullPointerException if <code>anotherString</code>
  203. * is <code>null</code>.
  204. */
  205. public int compareTo(XMLString anotherString)
  206. {
  207. return m_str.compareTo(anotherString.toString());
  208. }
  209. /**
  210. * Compares two strings lexicographically, ignoring case considerations.
  211. * This method returns an integer whose sign is that of
  212. * <code>this.toUpperCase().toLowerCase().compareTo(
  213. * str.toUpperCase().toLowerCase())</code>.
  214. * <p>
  215. * Note that this method does <em>not</em> take locale into account,
  216. * and will result in an unsatisfactory ordering for certain locales.
  217. * The java.text package provides <em>collators</em> to allow
  218. * locale-sensitive ordering.
  219. *
  220. * @param str the <code>String</code> to be compared.
  221. * @return a negative integer, zero, or a positive integer as the
  222. * the specified String is greater than, equal to, or less
  223. * than this String, ignoring case considerations.
  224. * @see java.text.Collator#compare(String, String)
  225. * @since 1.2
  226. */
  227. public int compareToIgnoreCase(XMLString str)
  228. {
  229. return m_str.compareToIgnoreCase(str.toString());
  230. }
  231. /**
  232. * Tests if this string starts with the specified prefix beginning
  233. * a specified index.
  234. *
  235. * @param prefix the prefix.
  236. * @param toffset where to begin looking in the string.
  237. * @return <code>true</code> if the character sequence represented by the
  238. * argument is a prefix of the substring of this object starting
  239. * at index <code>toffset</code> <code>false</code> otherwise.
  240. * The result is <code>false</code> if <code>toffset</code> is
  241. * negative or greater than the length of this
  242. * <code>String</code> object; otherwise the result is the same
  243. * as the result of the expression
  244. * <pre>
  245. * this.subString(toffset).startsWith(prefix)
  246. * </pre>
  247. * @exception java.lang.NullPointerException if <code>prefix</code> is
  248. * <code>null</code>.
  249. */
  250. public boolean startsWith(String prefix, int toffset)
  251. {
  252. return m_str.startsWith(prefix, toffset);
  253. }
  254. /**
  255. * Tests if this string starts with the specified prefix beginning
  256. * a specified index.
  257. *
  258. * @param prefix the prefix.
  259. * @param toffset where to begin looking in the string.
  260. * @return <code>true</code> if the character sequence represented by the
  261. * argument is a prefix of the substring of this object starting
  262. * at index <code>toffset</code> <code>false</code> otherwise.
  263. * The result is <code>false</code> if <code>toffset</code> is
  264. * negative or greater than the length of this
  265. * <code>String</code> object; otherwise the result is the same
  266. * as the result of the expression
  267. * <pre>
  268. * this.subString(toffset).startsWith(prefix)
  269. * </pre>
  270. * @exception java.lang.NullPointerException if <code>prefix</code> is
  271. * <code>null</code>.
  272. */
  273. public boolean startsWith(XMLString prefix, int toffset)
  274. {
  275. return m_str.startsWith(prefix.toString(), toffset);
  276. }
  277. /**
  278. * Tests if this string starts with the specified prefix.
  279. *
  280. * @param prefix the prefix.
  281. * @return <code>true</code> if the character sequence represented by the
  282. * argument is a prefix of the character sequence represented by
  283. * this string; <code>false</code> otherwise.
  284. * Note also that <code>true</code> will be returned if the
  285. * argument is an empty string or is equal to this
  286. * <code>String</code> object as determined by the
  287. * {@link #equals(Object)} method.
  288. * @exception java.lang.NullPointerException if <code>prefix</code> is
  289. * <code>null</code>.
  290. * @since JDK1. 0
  291. */
  292. public boolean startsWith(String prefix)
  293. {
  294. return m_str.startsWith(prefix);
  295. }
  296. /**
  297. * Tests if this string starts with the specified prefix.
  298. *
  299. * @param prefix the prefix.
  300. * @return <code>true</code> if the character sequence represented by the
  301. * argument is a prefix of the character sequence represented by
  302. * this string; <code>false</code> otherwise.
  303. * Note also that <code>true</code> will be returned if the
  304. * argument is an empty string or is equal to this
  305. * <code>String</code> object as determined by the
  306. * {@link #equals(Object)} method.
  307. * @exception java.lang.NullPointerException if <code>prefix</code> is
  308. * <code>null</code>.
  309. * @since JDK1. 0
  310. */
  311. public boolean startsWith(XMLString prefix)
  312. {
  313. return m_str.startsWith(prefix.toString());
  314. }
  315. /**
  316. * Tests if this string ends with the specified suffix.
  317. *
  318. * @param suffix the suffix.
  319. * @return <code>true</code> if the character sequence represented by the
  320. * argument is a suffix of the character sequence represented by
  321. * this object; <code>false</code> otherwise. Note that the
  322. * result will be <code>true</code> if the argument is the
  323. * empty string or is equal to this <code>String</code> object
  324. * as determined by the {@link #equals(Object)} method.
  325. * @exception java.lang.NullPointerException if <code>suffix</code> is
  326. * <code>null</code>.
  327. */
  328. public boolean endsWith(String suffix)
  329. {
  330. return m_str.endsWith(suffix);
  331. }
  332. /**
  333. * Returns a hashcode for this string. The hashcode for a
  334. * <code>String</code> object is computed as
  335. * <blockquote><pre>
  336. * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
  337. * </pre></blockquote>
  338. * using <code>int</code> arithmetic, where <code>s[i]</code> is the
  339. * <i>i</i>th character of the string, <code>n</code> is the length of
  340. * the string, and <code>^</code> indicates exponentiation.
  341. * (The hash value of the empty string is zero.)
  342. *
  343. * @return a hash code value for this object.
  344. */
  345. public int hashCode()
  346. {
  347. return m_str.hashCode();
  348. }
  349. /**
  350. * Returns the index within this string of the first occurrence of the
  351. * specified character. If a character with value <code>ch</code> occurs
  352. * in the character sequence represented by this <code>String</code>
  353. * object, then the index of the first such occurrence is returned --
  354. * that is, the smallest value <i>k</i> such that:
  355. * <blockquote><pre>
  356. * this.charAt(<i>k</i>) == ch
  357. * </pre></blockquote>
  358. * is <code>true</code>. If no such character occurs in this string,
  359. * then <code>-1</code> is returned.
  360. *
  361. * @param ch a character.
  362. * @return the index of the first occurrence of the character in the
  363. * character sequence represented by this object, or
  364. * <code>-1</code> if the character does not occur.
  365. */
  366. public int indexOf(int ch)
  367. {
  368. return m_str.indexOf(ch);
  369. }
  370. /**
  371. * Returns the index within this string of the first occurrence of the
  372. * specified character, starting the search at the specified index.
  373. * <p>
  374. * If a character with value <code>ch</code> occurs in the character
  375. * sequence represented by this <code>String</code> object at an index
  376. * no smaller than <code>fromIndex</code>, then the index of the first
  377. * such occurrence is returned--that is, the smallest value <i>k</i>
  378. * such that:
  379. * <blockquote><pre>
  380. * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
  381. * </pre></blockquote>
  382. * is true. If no such character occurs in this string at or after
  383. * position <code>fromIndex</code>, then <code>-1</code> is returned.
  384. * <p>
  385. * There is no restriction on the value of <code>fromIndex</code>. If it
  386. * is negative, it has the same effect as if it were zero: this entire
  387. * string may be searched. If it is greater than the length of this
  388. * string, it has the same effect as if it were equal to the length of
  389. * this string: <code>-1</code> is returned.
  390. *
  391. * @param ch a character.
  392. * @param fromIndex the index to start the search from.
  393. * @return the index of the first occurrence of the character in the
  394. * character sequence represented by this object that is greater
  395. * than or equal to <code>fromIndex</code>, or <code>-1</code>
  396. * if the character does not occur.
  397. */
  398. public int indexOf(int ch, int fromIndex)
  399. {
  400. return m_str.indexOf(ch, fromIndex);
  401. }
  402. /**
  403. * Returns the index within this string of the last occurrence of the
  404. * specified character. That is, the index returned is the largest
  405. * value <i>k</i> such that:
  406. * <blockquote><pre>
  407. * this.charAt(<i>k</i>) == ch
  408. * </pre></blockquote>
  409. * is true.
  410. * The String is searched backwards starting at the last character.
  411. *
  412. * @param ch a character.
  413. * @return the index of the last occurrence of the character in the
  414. * character sequence represented by this object, or
  415. * <code>-1</code> if the character does not occur.
  416. */
  417. public int lastIndexOf(int ch)
  418. {
  419. return m_str.lastIndexOf(ch);
  420. }
  421. /**
  422. * Returns the index within this string of the last occurrence of the
  423. * specified character, searching backward starting at the specified
  424. * index. That is, the index returned is the largest value <i>k</i>
  425. * such that:
  426. * <blockquote><pre>
  427. * this.charAt(k) == ch) && (k <= fromIndex)
  428. * </pre></blockquote>
  429. * is true.
  430. *
  431. * @param ch a character.
  432. * @param fromIndex the index to start the search from. There is no
  433. * restriction on the value of <code>fromIndex</code>. If it is
  434. * greater than or equal to the length of this string, it has
  435. * the same effect as if it were equal to one less than the
  436. * length of this string: this entire string may be searched.
  437. * If it is negative, it has the same effect as if it were -1:
  438. * -1 is returned.
  439. * @return the index of the last occurrence of the character in the
  440. * character sequence represented by this object that is less
  441. * than or equal to <code>fromIndex</code>, or <code>-1</code>
  442. * if the character does not occur before that point.
  443. */
  444. public int lastIndexOf(int ch, int fromIndex)
  445. {
  446. return m_str.lastIndexOf(ch, fromIndex);
  447. }
  448. /**
  449. * Returns the index within this string of the first occurrence of the
  450. * specified substring. The integer returned is the smallest value
  451. * <i>k</i> such that:
  452. * <blockquote><pre>
  453. * this.startsWith(str, <i>k</i>)
  454. * </pre></blockquote>
  455. * is <code>true</code>.
  456. *
  457. * @param str any string.
  458. * @return if the string argument occurs as a substring within this
  459. * object, then the index of the first character of the first
  460. * such substring is returned; if it does not occur as a
  461. * substring, <code>-1</code> is returned.
  462. * @exception java.lang.NullPointerException if <code>str</code> is
  463. * <code>null</code>.
  464. */
  465. public int indexOf(String str)
  466. {
  467. return m_str.indexOf(str);
  468. }
  469. /**
  470. * Returns the index within this string of the first occurrence of the
  471. * specified substring. The integer returned is the smallest value
  472. * <i>k</i> such that:
  473. * <blockquote><pre>
  474. * this.startsWith(str, <i>k</i>)
  475. * </pre></blockquote>
  476. * is <code>true</code>.
  477. *
  478. * @param str any string.
  479. * @return if the string argument occurs as a substring within this
  480. * object, then the index of the first character of the first
  481. * such substring is returned; if it does not occur as a
  482. * substring, <code>-1</code> is returned.
  483. * @exception java.lang.NullPointerException if <code>str</code> is
  484. * <code>null</code>.
  485. */
  486. public int indexOf(XMLString str)
  487. {
  488. return m_str.indexOf(str.toString());
  489. }
  490. /**
  491. * Returns the index within this string of the first occurrence of the
  492. * specified substring, starting at the specified index. The integer
  493. * returned is the smallest value <i>k</i> such that:
  494. * <blockquote><pre>
  495. * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)
  496. * </pre></blockquote>
  497. * is <code>true</code>.
  498. * <p>
  499. * There is no restriction on the value of <code>fromIndex</code>. If
  500. * it is negative, it has the same effect as if it were zero: this entire
  501. * string may be searched. If it is greater than the length of this
  502. * string, it has the same effect as if it were equal to the length of
  503. * this string: <code>-1</code> is returned.
  504. *
  505. * @param str the substring to search for.
  506. * @param fromIndex the index to start the search from.
  507. * @return If the string argument occurs as a substring within this
  508. * object at a starting index no smaller than
  509. * <code>fromIndex</code>, then the index of the first character
  510. * of the first such substring is returned. If it does not occur
  511. * as a substring starting at <code>fromIndex</code> or beyond,
  512. * <code>-1</code> is returned.
  513. * @exception java.lang.NullPointerException if <code>str</code> is
  514. * <code>null</code>
  515. */
  516. public int indexOf(String str, int fromIndex)
  517. {
  518. return m_str.indexOf(str, fromIndex);
  519. }
  520. /**
  521. * Returns the index within this string of the rightmost occurrence
  522. * of the specified substring. The rightmost empty string "" is
  523. * considered to occur at the index value <code>this.length()</code>.
  524. * The returned index is the largest value <i>k</i> such that
  525. * <blockquote><pre>
  526. * this.startsWith(str, k)
  527. * </pre></blockquote>
  528. * is true.
  529. *
  530. * @param str the substring to search for.
  531. * @return if the string argument occurs one or more times as a substring
  532. * within this object, then the index of the first character of
  533. * the last such substring is returned. If it does not occur as
  534. * a substring, <code>-1</code> is returned.
  535. * @exception java.lang.NullPointerException if <code>str</code> is
  536. * <code>null</code>.
  537. */
  538. public int lastIndexOf(String str)
  539. {
  540. return m_str.lastIndexOf(str);
  541. }
  542. /**
  543. * Returns the index within this string of the last occurrence of
  544. * the specified substring.
  545. *
  546. * @param str the substring to search for.
  547. * @param fromIndex the index to start the search from. There is no
  548. * restriction on the value of fromIndex. If it is greater than
  549. * the length of this string, it has the same effect as if it
  550. * were equal to the length of this string: this entire string
  551. * may be searched. If it is negative, it has the same effect
  552. * as if it were -1: -1 is returned.
  553. * @return If the string argument occurs one or more times as a substring
  554. * within this object at a starting index no greater than
  555. * <code>fromIndex</code>, then the index of the first character of
  556. * the last such substring is returned. If it does not occur as a
  557. * substring starting at <code>fromIndex</code> or earlier,
  558. * <code>-1</code> is returned.
  559. * @exception java.lang.NullPointerException if <code>str</code> is
  560. * <code>null</code>.
  561. */
  562. public int lastIndexOf(String str, int fromIndex)
  563. {
  564. return m_str.lastIndexOf(str, fromIndex);
  565. }
  566. /**
  567. * Returns a new string that is a substring of this string. The
  568. * substring begins with the character at the specified index and
  569. * extends to the end of this string. <p>
  570. * Examples:
  571. * <blockquote><pre>
  572. * "unhappy".substring(2) returns "happy"
  573. * "Harbison".substring(3) returns "bison"
  574. * "emptiness".substring(9) returns "" (an empty string)
  575. * </pre></blockquote>
  576. *
  577. * @param beginIndex the beginning index, inclusive.
  578. * @return the specified substring.
  579. * @exception IndexOutOfBoundsException if
  580. * <code>beginIndex</code> is negative or larger than the
  581. * length of this <code>String</code> object.
  582. */
  583. public XMLString substring(int beginIndex)
  584. {
  585. return new XMLStringDefault(m_str.substring(beginIndex));
  586. }
  587. /**
  588. * Returns a new string that is a substring of this string. The
  589. * substring begins at the specified <code>beginIndex</code> and
  590. * extends to the character at index <code>endIndex - 1</code>.
  591. * Thus the length of the substring is <code>endIndex-beginIndex</code>.
  592. *
  593. * @param beginIndex the beginning index, inclusive.
  594. * @param endIndex the ending index, exclusive.
  595. * @return the specified substring.
  596. * @exception IndexOutOfBoundsException if the
  597. * <code>beginIndex</code> is negative, or
  598. * <code>endIndex</code> is larger than the length of
  599. * this <code>String</code> object, or
  600. * <code>beginIndex</code> is larger than
  601. * <code>endIndex</code>.
  602. */
  603. public XMLString substring(int beginIndex, int endIndex)
  604. {
  605. return new XMLStringDefault(m_str.substring(beginIndex, endIndex));
  606. }
  607. /**
  608. * Concatenates the specified string to the end of this string.
  609. *
  610. * @param str the <code>String</code> that is concatenated to the end
  611. * of this <code>String</code>.
  612. * @return a string that represents the concatenation of this object's
  613. * characters followed by the string argument's characters.
  614. * @exception java.lang.NullPointerException if <code>str</code> is
  615. * <code>null</code>.
  616. */
  617. public XMLString concat(String str)
  618. {
  619. return new XMLStringDefault(m_str.concat(str));
  620. }
  621. /**
  622. * Converts all of the characters in this <code>String</code> to lower
  623. * case using the rules of the given <code>Locale</code>.
  624. *
  625. * @param locale use the case transformation rules for this locale
  626. * @return the String, converted to lowercase.
  627. * @see java.lang.Character#toLowerCase(char)
  628. * @see java.lang.String#toUpperCase(Locale)
  629. */
  630. public XMLString toLowerCase(Locale locale)
  631. {
  632. return new XMLStringDefault(m_str.toLowerCase(locale));
  633. }
  634. /**
  635. * Converts all of the characters in this <code>String</code> to lower
  636. * case using the rules of the default locale, which is returned
  637. * by <code>Locale.getDefault</code>.
  638. * <p>
  639. *
  640. * @return the string, converted to lowercase.
  641. * @see java.lang.Character#toLowerCase(char)
  642. * @see java.lang.String#toLowerCase(Locale)
  643. */
  644. public XMLString toLowerCase()
  645. {
  646. return new XMLStringDefault(m_str.toLowerCase());
  647. }
  648. /**
  649. * Converts all of the characters in this <code>String</code> to upper
  650. * case using the rules of the given locale.
  651. * @param locale use the case transformation rules for this locale
  652. * @return the String, converted to uppercase.
  653. * @see java.lang.Character#toUpperCase(char)
  654. * @see java.lang.String#toLowerCase(Locale)
  655. */
  656. public XMLString toUpperCase(Locale locale)
  657. {
  658. return new XMLStringDefault(m_str.toUpperCase(locale));
  659. }
  660. /**
  661. * Converts all of the characters in this <code>String</code> to upper
  662. * case using the rules of the default locale, which is returned
  663. * by <code>Locale.getDefault</code>.
  664. *
  665. * <p>
  666. * If no character in this string has a different uppercase version,
  667. * based on calling the <code>toUpperCase</code> method defined by
  668. * <code>Character</code>, then the original string is returned.
  669. * <p>
  670. * Otherwise, this method creates a new <code>String</code> object
  671. * representing a character sequence identical in length to the
  672. * character sequence represented by this <code>String</code> object and
  673. * with every character equal to the result of applying the method
  674. * <code>Character.toUpperCase</code> to the corresponding character of
  675. * this <code>String</code> object. <p>
  676. * Examples:
  677. * <blockquote><pre>
  678. * "Fahrvergngen".toUpperCase() returns "FAHRVERGNGEN"
  679. * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!"
  680. * </pre></blockquote>
  681. *
  682. * @return the string, converted to uppercase.
  683. * @see java.lang.Character#toUpperCase(char)
  684. * @see java.lang.String#toUpperCase(Locale)
  685. */
  686. public XMLString toUpperCase()
  687. {
  688. return new XMLStringDefault(m_str.toUpperCase());
  689. }
  690. /**
  691. * Removes white space from both ends of this string.
  692. * <p>
  693. * If this <code>String</code> object represents an empty character
  694. * sequence, or the first and last characters of character sequence
  695. * represented by this <code>String</code> object both have codes
  696. * greater than <code>'\u0020'</code> (the space character), then a
  697. * reference to this <code>String</code> object is returned.
  698. * <p>
  699. * Otherwise, if there is no character with a code greater than
  700. * <code>'\u0020'</code> in the string, then a new
  701. * <code>String</code> object representing an empty string is created
  702. * and returned.
  703. * <p>
  704. * Otherwise, let <i>k</i> be the index of the first character in the
  705. * string whose code is greater than <code>'\u0020'</code>, and let
  706. * <i>m</i> be the index of the last character in the string whose code
  707. * is greater than <code>'\u0020'</code>. A new <code>String</code>
  708. * object is created, representing the substring of this string that
  709. * begins with the character at index <i>k</i> and ends with the
  710. * character at index <i>m</i>-that is, the result of
  711. * <code>this.substring(<i>k</i>, <i>m</i>+1)</code>.
  712. * <p>
  713. * This method may be used to trim
  714. * {@link Character#isSpace(char) whitespace} from the beginning and end
  715. * of a string; in fact, it trims all ASCII control characters as well.
  716. *
  717. * @return this string, with white space removed from the front and end.
  718. */
  719. public XMLString trim()
  720. {
  721. return new XMLStringDefault(m_str.trim());
  722. }
  723. /**
  724. * This object (which is already a string!) is itself returned.
  725. *
  726. * @return the string itself.
  727. */
  728. public String toString()
  729. {
  730. return m_str;
  731. }
  732. /**
  733. * Tell if this object contains a java String object.
  734. *
  735. * @return true if this XMLString can return a string without creating one.
  736. */
  737. public boolean hasString()
  738. {
  739. return true;
  740. }
  741. /**
  742. * Convert a string to a double -- Allowed input is in fixed
  743. * notation ddd.fff.
  744. *
  745. * @return A double value representation of the string, or return Double.NaN
  746. * if the string can not be converted.
  747. */
  748. public double toDouble()
  749. {
  750. try {
  751. return Double.valueOf(m_str).doubleValue();
  752. }
  753. catch (NumberFormatException nfe)
  754. {
  755. return Double.NaN;
  756. }
  757. }
  758. }