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