286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: XString.java,v 1.2.4.1 2005/09/14 20:47:20 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal.objects;
286N/A
286N/Aimport java.util.Locale;
286N/A
286N/Aimport com.sun.org.apache.xml.internal.dtm.DTM;
286N/Aimport com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer;
286N/Aimport com.sun.org.apache.xml.internal.utils.XMLString;
286N/Aimport com.sun.org.apache.xml.internal.utils.XMLStringFactory;
286N/Aimport com.sun.org.apache.xpath.internal.ExpressionOwner;
286N/Aimport com.sun.org.apache.xpath.internal.XPathContext;
286N/Aimport com.sun.org.apache.xpath.internal.XPathVisitor;
286N/A
286N/A/**
286N/A * This class represents an XPath string object, and is capable of
286N/A * converting the string to other types, such as a number.
286N/A * @xsl.usage general
286N/A */
286N/Apublic class XString extends XObject implements XMLString
286N/A{
286N/A static final long serialVersionUID = 2020470518395094525L;
286N/A
286N/A /** Empty string XString object */
286N/A public static final XString EMPTYSTRING = new XString("");
286N/A
286N/A /**
286N/A * Construct a XString object. This constructor exists for derived classes.
286N/A *
286N/A * @param val String object this will wrap.
286N/A */
286N/A protected XString(Object val)
286N/A {
286N/A super(val);
286N/A }
286N/A
286N/A /**
286N/A * Construct a XNodeSet object.
286N/A *
286N/A * @param val String object this will wrap.
286N/A */
286N/A public XString(String val)
286N/A {
286N/A super(val);
286N/A }
286N/A
286N/A /**
286N/A * Tell that this is a CLASS_STRING.
286N/A *
286N/A * @return type CLASS_STRING
286N/A */
286N/A public int getType()
286N/A {
286N/A return CLASS_STRING;
286N/A }
286N/A
286N/A /**
286N/A * Given a request type, return the equivalent string.
286N/A * For diagnostic purposes.
286N/A *
286N/A * @return type string "#STRING"
286N/A */
286N/A public String getTypeString()
286N/A {
286N/A return "#STRING";
286N/A }
286N/A
286N/A /**
286N/A * Tell if this object contains a java String object.
286N/A *
286N/A * @return true if this XMLString can return a string without creating one.
286N/A */
286N/A public boolean hasString()
286N/A {
286N/A return true;
286N/A }
286N/A
286N/A /**
286N/A * Cast result object to a number.
286N/A *
286N/A * @return 0.0 if this string is null, numeric value of this string
286N/A * or NaN
286N/A */
286N/A public double num()
286N/A {
286N/A return toDouble();
286N/A }
286N/A
286N/A /**
286N/A * Convert a string to a double -- Allowed input is in fixed
286N/A * notation ddd.fff.
286N/A *
286N/A * @return A double value representation of the string, or return Double.NaN
286N/A * if the string can not be converted.
286N/A */
286N/A public double toDouble()
286N/A {
286N/A /* XMLCharacterRecognizer.isWhiteSpace(char c) methods treats the following
286N/A * characters as white space characters.
286N/A * ht - horizontal tab, nl - newline , cr - carriage return and sp - space
286N/A * trim() methods by default also takes care of these white space characters
286N/A * So trim() method is used to remove leading and trailing white spaces.
286N/A */
286N/A XMLString s = trim();
286N/A double result = Double.NaN;
286N/A for (int i = 0; i < s.length(); i++)
286N/A {
286N/A char c = s.charAt(i);
286N/A if (c != '-' && c != '.' && ( c < 0X30 || c > 0x39)) {
286N/A // The character is not a '-' or a '.' or a digit
286N/A // then return NaN because something is wrong.
286N/A return result;
286N/A }
286N/A }
286N/A try
286N/A {
286N/A result = Double.parseDouble(s.toString());
286N/A } catch (NumberFormatException e){}
286N/A
286N/A return result;
286N/A}
286N/A
286N/A /**
286N/A * Cast result object to a boolean.
286N/A *
286N/A * @return True if the length of this string object is greater
286N/A * than 0.
286N/A */
286N/A public boolean bool()
286N/A {
286N/A return str().length() > 0;
286N/A }
286N/A
286N/A /**
286N/A * Cast result object to a string.
286N/A *
286N/A * @return The string this wraps or the empty string if null
286N/A */
286N/A public XMLString xstr()
286N/A {
286N/A return this;
286N/A }
286N/A
286N/A /**
286N/A * Cast result object to a string.
286N/A *
286N/A * @return The string this wraps or the empty string if null
286N/A */
286N/A public String str()
286N/A {
286N/A return (null != m_obj) ? ((String) m_obj) : "";
286N/A }
286N/A
286N/A /**
286N/A * Cast result object to a result tree fragment.
286N/A *
286N/A * @param support Xpath context to use for the conversion
286N/A *
286N/A * @return A document fragment with this string as a child node
286N/A */
286N/A public int rtf(XPathContext support)
286N/A {
286N/A
286N/A DTM frag = support.createDocumentFragment();
286N/A
286N/A frag.appendTextChild(str());
286N/A
286N/A return frag.getDocument();
286N/A }
286N/A
286N/A /**
286N/A * Directly call the
286N/A * characters method on the passed ContentHandler for the
286N/A * string-value. Multiple calls to the
286N/A * ContentHandler's characters methods may well occur for a single call to
286N/A * this method.
286N/A *
286N/A * @param ch A non-null reference to a ContentHandler.
286N/A *
286N/A * @throws org.xml.sax.SAXException
286N/A */
286N/A public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
286N/A throws org.xml.sax.SAXException
286N/A {
286N/A
286N/A String str = str();
286N/A
286N/A ch.characters(str.toCharArray(), 0, str.length());
286N/A }
286N/A
286N/A /**
286N/A * Directly call the
286N/A * comment method on the passed LexicalHandler for the
286N/A * string-value.
286N/A *
286N/A * @param lh A non-null reference to a LexicalHandler.
286N/A *
286N/A * @throws org.xml.sax.SAXException
286N/A */
286N/A public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
286N/A throws org.xml.sax.SAXException
286N/A {
286N/A
286N/A String str = str();
286N/A
286N/A lh.comment(str.toCharArray(), 0, str.length());
286N/A }
286N/A
286N/A /**
286N/A * Returns the length of this string.
286N/A *
286N/A * @return the length of the sequence of characters represented by this
286N/A * object.
286N/A */
286N/A public int length()
286N/A {
286N/A return str().length();
286N/A }
286N/A
286N/A /**
286N/A * Returns the character at the specified index. An index ranges
286N/A * from <code>0</code> to <code>length() - 1</code>. The first character
286N/A * of the sequence is at index <code>0</code>, the next at index
286N/A * <code>1</code>, and so on, as for array indexing.
286N/A *
286N/A * @param index the index of the character.
286N/A * @return the character at the specified index of this string.
286N/A * The first character is at index <code>0</code>.
286N/A * @exception IndexOutOfBoundsException if the <code>index</code>
286N/A * argument is negative or not less than the length of this
286N/A * string.
286N/A */
286N/A public char charAt(int index)
286N/A {
286N/A return str().charAt(index);
286N/A }
286N/A
286N/A /**
286N/A * Copies characters from this string into the destination character
286N/A * array.
286N/A *
286N/A * @param srcBegin index of the first character in the string
286N/A * to copy.
286N/A * @param srcEnd index after the last character in the string
286N/A * to copy.
286N/A * @param dst the destination array.
286N/A * @param dstBegin the start offset in the destination array.
286N/A * @exception IndexOutOfBoundsException If any of the following
286N/A * is true:
286N/A * <ul><li><code>srcBegin</code> is negative.
286N/A * <li><code>srcBegin</code> is greater than <code>srcEnd</code>
286N/A * <li><code>srcEnd</code> is greater than the length of this
286N/A * string
286N/A * <li><code>dstBegin</code> is negative
286N/A * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
286N/A * <code>dst.length</code></ul>
286N/A * @exception NullPointerException if <code>dst</code> is <code>null</code>
286N/A */
286N/A public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
286N/A {
286N/A str().getChars(srcBegin, srcEnd, dst, dstBegin);
286N/A }
286N/A
286N/A /**
286N/A * Tell if two objects are functionally equal.
286N/A *
286N/A * @param obj2 Object to compare this to
286N/A *
286N/A * @return true if the two objects are equal
286N/A *
286N/A * @throws javax.xml.transform.TransformerException
286N/A */
286N/A public boolean equals(XObject obj2)
286N/A {
286N/A
286N/A // In order to handle the 'all' semantics of
286N/A // nodeset comparisons, we always call the
286N/A // nodeset function.
286N/A int t = obj2.getType();
286N/A try
286N/A {
286N/A if (XObject.CLASS_NODESET == t)
286N/A return obj2.equals(this);
286N/A // If at least one object to be compared is a boolean, then each object
286N/A // to be compared is converted to a boolean as if by applying the
286N/A // boolean function.
286N/A else if(XObject.CLASS_BOOLEAN == t)
286N/A return obj2.bool() == bool();
286N/A // Otherwise, if at least one object to be compared is a number, then each object
286N/A // to be compared is converted to a number as if by applying the number function.
286N/A else if(XObject.CLASS_NUMBER == t)
286N/A return obj2.num() == num();
286N/A }
286N/A catch(javax.xml.transform.TransformerException te)
286N/A {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
286N/A }
286N/A
286N/A // Otherwise, both objects to be compared are converted to strings as
286N/A // if by applying the string function.
286N/A return xstr().equals(obj2.xstr());
286N/A }
286N/A
293N/A /**
293N/A * Compares this string to the specified <code>String</code>.
293N/A * The result is <code>true</code> if and only if the argument is not
293N/A * <code>null</code> and is a <code>String</code> object that represents
293N/A * the same sequence of characters as this object.
293N/A *
293N/A * @param obj2 the object to compare this <code>String</code> against.
293N/A * @return <code>true</code> if the <code>String</code>s are equal;
293N/A * <code>false</code> otherwise.
293N/A * @see java.lang.String#compareTo(java.lang.String)
293N/A * @see java.lang.String#equalsIgnoreCase(java.lang.String)
293N/A */
293N/A public boolean equals(String obj2) {
293N/A return str().equals(obj2);
293N/A }
293N/A
286N/A /**
286N/A * Compares this string to the specified object.
286N/A * The result is <code>true</code> if and only if the argument is not
286N/A * <code>null</code> and is a <code>String</code> object that represents
286N/A * the same sequence of characters as this object.
286N/A *
286N/A * @param obj2 the object to compare this <code>String</code>
286N/A * against.
286N/A * @return <code>true</code> if the <code>String </code>are equal;
286N/A * <code>false</code> otherwise.
286N/A * @see java.lang.String#compareTo(java.lang.String)
286N/A * @see java.lang.String#equalsIgnoreCase(java.lang.String)
286N/A */
286N/A public boolean equals(XMLString obj2)
286N/A {
293N/A if (obj2 != null) {
293N/A if (!obj2.hasString()) {
293N/A return obj2.equals(str());
293N/A } else {
293N/A return str().equals(obj2.toString());
293N/A }
293N/A }
293N/A return false;
286N/A }
286N/A
286N/A /**
286N/A * Compares this string to the specified object.
286N/A * The result is <code>true</code> if and only if the argument is not
286N/A * <code>null</code> and is a <code>String</code> object that represents
286N/A * the same sequence of characters as this object.
286N/A *
286N/A * @param obj2 the object to compare this <code>String</code>
286N/A * against.
286N/A * @return <code>true</code> if the <code>String </code>are equal;
286N/A * <code>false</code> otherwise.
286N/A * @see java.lang.String#compareTo(java.lang.String)
286N/A * @see java.lang.String#equalsIgnoreCase(java.lang.String)
286N/A */
286N/A public boolean equals(Object obj2)
286N/A {
286N/A
286N/A if (null == obj2)
286N/A return false;
286N/A
286N/A // In order to handle the 'all' semantics of
286N/A // nodeset comparisons, we always call the
286N/A // nodeset function.
286N/A else if (obj2 instanceof XNodeSet)
286N/A return obj2.equals(this);
286N/A else if(obj2 instanceof XNumber)
286N/A return obj2.equals(this);
286N/A else
286N/A return str().equals(obj2.toString());
286N/A }
286N/A
286N/A /**
286N/A * Compares this <code>String</code> to another <code>String</code>,
286N/A * ignoring case considerations. Two strings are considered equal
286N/A * ignoring case if they are of the same length, and corresponding
286N/A * characters in the two strings are equal ignoring case.
286N/A *
286N/A * @param anotherString the <code>String</code> to compare this
286N/A * <code>String</code> against.
286N/A * @return <code>true</code> if the argument is not <code>null</code>
286N/A * and the <code>String</code>s are equal,
286N/A * ignoring case; <code>false</code> otherwise.
286N/A * @see #equals(Object)
286N/A * @see java.lang.Character#toLowerCase(char)
286N/A * @see java.lang.Character#toUpperCase(char)
286N/A */
286N/A public boolean equalsIgnoreCase(String anotherString)
286N/A {
286N/A return str().equalsIgnoreCase(anotherString);
286N/A }
286N/A
286N/A /**
286N/A * Compares two strings lexicographically.
286N/A *
286N/A * @param xstr the <code>String</code> to be compared.
286N/A *
286N/A * @return the value <code>0</code> if the argument string is equal to
286N/A * this string; a value less than <code>0</code> if this string
286N/A * is lexicographically less than the string argument; and a
286N/A * value greater than <code>0</code> if this string is
286N/A * lexicographically greater than the string argument.
286N/A * @exception java.lang.NullPointerException if <code>anotherString</code>
286N/A * is <code>null</code>.
286N/A */
286N/A public int compareTo(XMLString xstr)
286N/A {
286N/A
286N/A int len1 = this.length();
286N/A int len2 = xstr.length();
286N/A int n = Math.min(len1, len2);
286N/A int i = 0;
286N/A int j = 0;
286N/A
286N/A while (n-- != 0)
286N/A {
286N/A char c1 = this.charAt(i);
286N/A char c2 = xstr.charAt(j);
286N/A
286N/A if (c1 != c2)
286N/A {
286N/A return c1 - c2;
286N/A }
286N/A
286N/A i++;
286N/A j++;
286N/A }
286N/A
286N/A return len1 - len2;
286N/A }
286N/A
286N/A /**
286N/A * Compares two strings lexicographically, ignoring case considerations.
286N/A * This method returns an integer whose sign is that of
286N/A * <code>this.toUpperCase().toLowerCase().compareTo(
286N/A * str.toUpperCase().toLowerCase())</code>.
286N/A * <p>
286N/A * Note that this method does <em>not</em> take locale into account,
286N/A * and will result in an unsatisfactory ordering for certain locales.
286N/A * The java.text package provides <em>collators</em> to allow
286N/A * locale-sensitive ordering.
286N/A *
286N/A * @param str the <code>String</code> to be compared.
286N/A * @return a negative integer, zero, or a positive integer as the
286N/A * the specified String is greater than, equal to, or less
286N/A * than this String, ignoring case considerations.
286N/A * @see java.text.Collator#compare(String, String)
286N/A * @since 1.2
286N/A */
286N/A public int compareToIgnoreCase(XMLString str)
286N/A {
286N/A // %REVIEW% Like it says, @since 1.2. Doesn't exist in earlier
286N/A // versions of Java, hence we can't yet shell out to it. We can implement
286N/A // it as character-by-character compare, but doing so efficiently
286N/A // is likely to be (ahem) interesting.
286N/A //
286N/A // However, since nobody is actually _using_ this method yet:
286N/A // return str().compareToIgnoreCase(str.toString());
286N/A
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(
286N/A new java.lang.NoSuchMethodException(
286N/A "Java 1.2 method, not yet implemented"));
286N/A }
286N/A
286N/A /**
286N/A * Tests if this string starts with the specified prefix beginning
286N/A * a specified index.
286N/A *
286N/A * @param prefix the prefix.
286N/A * @param toffset where to begin looking in the string.
286N/A * @return <code>true</code> if the character sequence represented by the
286N/A * argument is a prefix of the substring of this object starting
286N/A * at index <code>toffset</code>; <code>false</code> otherwise.
286N/A * The result is <code>false</code> if <code>toffset</code> is
286N/A * negative or greater than the length of this
286N/A * <code>String</code> object; otherwise the result is the same
286N/A * as the result of the expression
286N/A * <pre>
286N/A * this.subString(toffset).startsWith(prefix)
286N/A * </pre>
286N/A * @exception java.lang.NullPointerException if <code>prefix</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public boolean startsWith(String prefix, int toffset)
286N/A {
286N/A return str().startsWith(prefix, toffset);
286N/A }
286N/A
286N/A /**
286N/A * Tests if this string starts with the specified prefix.
286N/A *
286N/A * @param prefix the prefix.
286N/A * @return <code>true</code> if the character sequence represented by the
286N/A * argument is a prefix of the character sequence represented by
286N/A * this string; <code>false</code> otherwise.
286N/A * Note also that <code>true</code> will be returned if the
286N/A * argument is an empty string or is equal to this
286N/A * <code>String</code> object as determined by the
286N/A * {@link #equals(Object)} method.
286N/A * @exception java.lang.NullPointerException if <code>prefix</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public boolean startsWith(String prefix)
286N/A {
286N/A return startsWith(prefix, 0);
286N/A }
286N/A
286N/A /**
286N/A * Tests if this string starts with the specified prefix beginning
286N/A * a specified index.
286N/A *
286N/A * @param prefix the prefix.
286N/A * @param toffset where to begin looking in the string.
286N/A * @return <code>true</code> if the character sequence represented by the
286N/A * argument is a prefix of the substring of this object starting
286N/A * at index <code>toffset</code>; <code>false</code> otherwise.
286N/A * The result is <code>false</code> if <code>toffset</code> is
286N/A * negative or greater than the length of this
286N/A * <code>String</code> object; otherwise the result is the same
286N/A * as the result of the expression
286N/A * <pre>
286N/A * this.subString(toffset).startsWith(prefix)
286N/A * </pre>
286N/A * @exception java.lang.NullPointerException if <code>prefix</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public boolean startsWith(XMLString prefix, int toffset)
286N/A {
286N/A
286N/A int to = toffset;
286N/A int tlim = this.length();
286N/A int po = 0;
286N/A int pc = prefix.length();
286N/A
286N/A // Note: toffset might be near -1>>>1.
286N/A if ((toffset < 0) || (toffset > tlim - pc))
286N/A {
286N/A return false;
286N/A }
286N/A
286N/A while (--pc >= 0)
286N/A {
286N/A if (this.charAt(to) != prefix.charAt(po))
286N/A {
286N/A return false;
286N/A }
286N/A
286N/A to++;
286N/A po++;
286N/A }
286N/A
286N/A return true;
286N/A }
286N/A
286N/A /**
286N/A * Tests if this string starts with the specified prefix.
286N/A *
286N/A * @param prefix the prefix.
286N/A * @return <code>true</code> if the character sequence represented by the
286N/A * argument is a prefix of the character sequence represented by
286N/A * this string; <code>false</code> otherwise.
286N/A * Note also that <code>true</code> will be returned if the
286N/A * argument is an empty string or is equal to this
286N/A * <code>String</code> object as determined by the
286N/A * {@link #equals(Object)} method.
286N/A * @exception java.lang.NullPointerException if <code>prefix</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public boolean startsWith(XMLString prefix)
286N/A {
286N/A return startsWith(prefix, 0);
286N/A }
286N/A
286N/A /**
286N/A * Tests if this string ends with the specified suffix.
286N/A *
286N/A * @param suffix the suffix.
286N/A * @return <code>true</code> if the character sequence represented by the
286N/A * argument is a suffix of the character sequence represented by
286N/A * this object; <code>false</code> otherwise. Note that the
286N/A * result will be <code>true</code> if the argument is the
286N/A * empty string or is equal to this <code>String</code> object
286N/A * as determined by the {@link #equals(Object)} method.
286N/A * @exception java.lang.NullPointerException if <code>suffix</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public boolean endsWith(String suffix)
286N/A {
286N/A return str().endsWith(suffix);
286N/A }
286N/A
286N/A /**
286N/A * Returns a hashcode for this string. The hashcode for a
286N/A * <code>String</code> object is computed as
286N/A * <blockquote><pre>
286N/A * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
286N/A * </pre></blockquote>
286N/A * using <code>int</code> arithmetic, where <code>s[i]</code> is the
286N/A * <i>i</i>th character of the string, <code>n</code> is the length of
286N/A * the string, and <code>^</code> indicates exponentiation.
286N/A * (The hash value of the empty string is zero.)
286N/A *
286N/A * @return a hash code value for this object.
286N/A */
286N/A public int hashCode()
286N/A {
286N/A return str().hashCode();
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the first occurrence of the
286N/A * specified character. If a character with value <code>ch</code> occurs
286N/A * in the character sequence represented by this <code>String</code>
286N/A * object, then the index of the first such occurrence is returned --
286N/A * that is, the smallest value <i>k</i> such that:
286N/A * <blockquote><pre>
286N/A * this.charAt(<i>k</i>) == ch
286N/A * </pre></blockquote>
286N/A * is <code>true</code>. If no such character occurs in this string,
286N/A * then <code>-1</code> is returned.
286N/A *
286N/A * @param ch a character.
286N/A * @return the index of the first occurrence of the character in the
286N/A * character sequence represented by this object, or
286N/A * <code>-1</code> if the character does not occur.
286N/A */
286N/A public int indexOf(int ch)
286N/A {
286N/A return str().indexOf(ch);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the first occurrence of the
286N/A * specified character, starting the search at the specified index.
286N/A * <p>
286N/A * If a character with value <code>ch</code> occurs in the character
286N/A * sequence represented by this <code>String</code> object at an index
286N/A * no smaller than <code>fromIndex</code>, then the index of the first
286N/A * such occurrence is returned--that is, the smallest value <i>k</i>
286N/A * such that:
286N/A * <blockquote><pre>
286N/A * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
286N/A * </pre></blockquote>
286N/A * is true. If no such character occurs in this string at or after
286N/A * position <code>fromIndex</code>, then <code>-1</code> is returned.
286N/A * <p>
286N/A * There is no restriction on the value of <code>fromIndex</code>. If it
286N/A * is negative, it has the same effect as if it were zero: this entire
286N/A * string may be searched. If it is greater than the length of this
286N/A * string, it has the same effect as if it were equal to the length of
286N/A * this string: <code>-1</code> is returned.
286N/A *
286N/A * @param ch a character.
286N/A * @param fromIndex the index to start the search from.
286N/A * @return the index of the first occurrence of the character in the
286N/A * character sequence represented by this object that is greater
286N/A * than or equal to <code>fromIndex</code>, or <code>-1</code>
286N/A * if the character does not occur.
286N/A */
286N/A public int indexOf(int ch, int fromIndex)
286N/A {
286N/A return str().indexOf(ch, fromIndex);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the last occurrence of the
286N/A * specified character. That is, the index returned is the largest
286N/A * value <i>k</i> such that:
286N/A * <blockquote><pre>
286N/A * this.charAt(<i>k</i>) == ch
286N/A * </pre></blockquote>
286N/A * is true.
286N/A * The String is searched backwards starting at the last character.
286N/A *
286N/A * @param ch a character.
286N/A * @return the index of the last occurrence of the character in the
286N/A * character sequence represented by this object, or
286N/A * <code>-1</code> if the character does not occur.
286N/A */
286N/A public int lastIndexOf(int ch)
286N/A {
286N/A return str().lastIndexOf(ch);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the last occurrence of the
286N/A * specified character, searching backward starting at the specified
286N/A * index. That is, the index returned is the largest value <i>k</i>
286N/A * such that:
286N/A * <blockquote><pre>
286N/A * this.charAt(k) == ch) && (k <= fromIndex)
286N/A * </pre></blockquote>
286N/A * is true.
286N/A *
286N/A * @param ch a character.
286N/A * @param fromIndex the index to start the search from. There is no
286N/A * restriction on the value of <code>fromIndex</code>. If it is
286N/A * greater than or equal to the length of this string, it has
286N/A * the same effect as if it were equal to one less than the
286N/A * length of this string: this entire string may be searched.
286N/A * If it is negative, it has the same effect as if it were -1:
286N/A * -1 is returned.
286N/A * @return the index of the last occurrence of the character in the
286N/A * character sequence represented by this object that is less
286N/A * than or equal to <code>fromIndex</code>, or <code>-1</code>
286N/A * if the character does not occur before that point.
286N/A */
286N/A public int lastIndexOf(int ch, int fromIndex)
286N/A {
286N/A return str().lastIndexOf(ch, fromIndex);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the first occurrence of the
286N/A * specified substring. The integer returned is the smallest value
286N/A * <i>k</i> such that:
286N/A * <blockquote><pre>
286N/A * this.startsWith(str, <i>k</i>)
286N/A * </pre></blockquote>
286N/A * is <code>true</code>.
286N/A *
286N/A * @param str any string.
286N/A * @return if the string argument occurs as a substring within this
286N/A * object, then the index of the first character of the first
286N/A * such substring is returned; if it does not occur as a
286N/A * substring, <code>-1</code> is returned.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public int indexOf(String str)
286N/A {
286N/A return str().indexOf(str);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the first occurrence of the
286N/A * specified substring. The integer returned is the smallest value
286N/A * <i>k</i> such that:
286N/A * <blockquote><pre>
286N/A * this.startsWith(str, <i>k</i>)
286N/A * </pre></blockquote>
286N/A * is <code>true</code>.
286N/A *
286N/A * @param str any string.
286N/A * @return if the string argument occurs as a substring within this
286N/A * object, then the index of the first character of the first
286N/A * such substring is returned; if it does not occur as a
286N/A * substring, <code>-1</code> is returned.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public int indexOf(XMLString str)
286N/A {
286N/A return str().indexOf(str.toString());
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the first occurrence of the
286N/A * specified substring, starting at the specified index. The integer
286N/A * returned is the smallest value <i>k</i> such that:
286N/A * <blockquote><pre>
286N/A * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)
286N/A * </pre></blockquote>
286N/A * is <code>true</code>.
286N/A * <p>
286N/A * There is no restriction on the value of <code>fromIndex</code>. If
286N/A * it is negative, it has the same effect as if it were zero: this entire
286N/A * string may be searched. If it is greater than the length of this
286N/A * string, it has the same effect as if it were equal to the length of
286N/A * this string: <code>-1</code> is returned.
286N/A *
286N/A * @param str the substring to search for.
286N/A * @param fromIndex the index to start the search from.
286N/A * @return If the string argument occurs as a substring within this
286N/A * object at a starting index no smaller than
286N/A * <code>fromIndex</code>, then the index of the first character
286N/A * of the first such substring is returned. If it does not occur
286N/A * as a substring starting at <code>fromIndex</code> or beyond,
286N/A * <code>-1</code> is returned.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>
286N/A */
286N/A public int indexOf(String str, int fromIndex)
286N/A {
286N/A return str().indexOf(str, fromIndex);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the rightmost occurrence
286N/A * of the specified substring. The rightmost empty string "" is
286N/A * considered to occur at the index value <code>this.length()</code>.
286N/A * The returned index is the largest value <i>k</i> such that
286N/A * <blockquote><pre>
286N/A * this.startsWith(str, k)
286N/A * </pre></blockquote>
286N/A * is true.
286N/A *
286N/A * @param str the substring to search for.
286N/A * @return if the string argument occurs one or more times as a substring
286N/A * within this object, then the index of the first character of
286N/A * the last such substring is returned. If it does not occur as
286N/A * a substring, <code>-1</code> is returned.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public int lastIndexOf(String str)
286N/A {
286N/A return str().lastIndexOf(str);
286N/A }
286N/A
286N/A /**
286N/A * Returns the index within this string of the last occurrence of
286N/A * the specified substring.
286N/A *
286N/A * @param str the substring to search for.
286N/A * @param fromIndex the index to start the search from. There is no
286N/A * restriction on the value of fromIndex. If it is greater than
286N/A * the length of this string, it has the same effect as if it
286N/A * were equal to the length of this string: this entire string
286N/A * may be searched. If it is negative, it has the same effect
286N/A * as if it were -1: -1 is returned.
286N/A * @return If the string argument occurs one or more times as a substring
286N/A * within this object at a starting index no greater than
286N/A * <code>fromIndex</code>, then the index of the first character of
286N/A * the last such substring is returned. If it does not occur as a
286N/A * substring starting at <code>fromIndex</code> or earlier,
286N/A * <code>-1</code> is returned.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public int lastIndexOf(String str, int fromIndex)
286N/A {
286N/A return str().lastIndexOf(str, fromIndex);
286N/A }
286N/A
286N/A /**
286N/A * Returns a new string that is a substring of this string. The
286N/A * substring begins with the character at the specified index and
286N/A * extends to the end of this string. <p>
286N/A * Examples:
286N/A * <blockquote><pre>
286N/A * "unhappy".substring(2) returns "happy"
286N/A * "Harbison".substring(3) returns "bison"
286N/A * "emptiness".substring(9) returns "" (an empty string)
286N/A * </pre></blockquote>
286N/A *
286N/A * @param beginIndex the beginning index, inclusive.
286N/A * @return the specified substring.
286N/A * @exception IndexOutOfBoundsException if
286N/A * <code>beginIndex</code> is negative or larger than the
286N/A * length of this <code>String</code> object.
286N/A */
286N/A public XMLString substring(int beginIndex)
286N/A {
286N/A return new XString(str().substring(beginIndex));
286N/A }
286N/A
286N/A /**
286N/A * Returns a new string that is a substring of this string. The
286N/A * substring begins at the specified <code>beginIndex</code> and
286N/A * extends to the character at index <code>endIndex - 1</code>.
286N/A * Thus the length of the substring is <code>endIndex-beginIndex</code>.
286N/A *
286N/A * @param beginIndex the beginning index, inclusive.
286N/A * @param endIndex the ending index, exclusive.
286N/A * @return the specified substring.
286N/A * @exception IndexOutOfBoundsException if the
286N/A * <code>beginIndex</code> is negative, or
286N/A * <code>endIndex</code> is larger than the length of
286N/A * this <code>String</code> object, or
286N/A * <code>beginIndex</code> is larger than
286N/A * <code>endIndex</code>.
286N/A */
286N/A public XMLString substring(int beginIndex, int endIndex)
286N/A {
286N/A return new XString(str().substring(beginIndex, endIndex));
286N/A }
286N/A
286N/A /**
286N/A * Concatenates the specified string to the end of this string.
286N/A *
286N/A * @param str the <code>String</code> that is concatenated to the end
286N/A * of this <code>String</code>.
286N/A * @return a string that represents the concatenation of this object's
286N/A * characters followed by the string argument's characters.
286N/A * @exception java.lang.NullPointerException if <code>str</code> is
286N/A * <code>null</code>.
286N/A */
286N/A public XMLString concat(String str)
286N/A {
286N/A
286N/A // %REVIEW% Make an FSB here?
286N/A return new XString(str().concat(str));
286N/A }
286N/A
286N/A /**
286N/A * Converts all of the characters in this <code>String</code> to lower
286N/A * case using the rules of the given <code>Locale</code>.
286N/A *
286N/A * @param locale use the case transformation rules for this locale
286N/A * @return the String, converted to lowercase.
286N/A * @see java.lang.Character#toLowerCase(char)
286N/A * @see java.lang.String#toUpperCase(Locale)
286N/A */
286N/A public XMLString toLowerCase(Locale locale)
286N/A {
286N/A return new XString(str().toLowerCase(locale));
286N/A }
286N/A
286N/A /**
286N/A * Converts all of the characters in this <code>String</code> to lower
286N/A * case using the rules of the default locale, which is returned
286N/A * by <code>Locale.getDefault</code>.
286N/A * <p>
286N/A *
286N/A * @return the string, converted to lowercase.
286N/A * @see java.lang.Character#toLowerCase(char)
286N/A * @see java.lang.String#toLowerCase(Locale)
286N/A */
286N/A public XMLString toLowerCase()
286N/A {
286N/A return new XString(str().toLowerCase());
286N/A }
286N/A
286N/A /**
286N/A * Converts all of the characters in this <code>String</code> to upper
286N/A * case using the rules of the given locale.
286N/A * @param locale use the case transformation rules for this locale
286N/A * @return the String, converted to uppercase.
286N/A * @see java.lang.Character#toUpperCase(char)
286N/A * @see java.lang.String#toLowerCase(Locale)
286N/A */
286N/A public XMLString toUpperCase(Locale locale)
286N/A {
286N/A return new XString(str().toUpperCase(locale));
286N/A }
286N/A
286N/A /**
286N/A * Converts all of the characters in this <code>String</code> to upper
286N/A * case using the rules of the default locale, which is returned
286N/A * by <code>Locale.getDefault</code>.
286N/A *
286N/A * <p>
286N/A * If no character in this string has a different uppercase version,
286N/A * based on calling the <code>toUpperCase</code> method defined by
286N/A * <code>Character</code>, then the original string is returned.
286N/A * <p>
286N/A * Otherwise, this method creates a new <code>String</code> object
286N/A * representing a character sequence identical in length to the
286N/A * character sequence represented by this <code>String</code> object and
286N/A * with every character equal to the result of applying the method
286N/A * <code>Character.toUpperCase</code> to the corresponding character of
286N/A * this <code>String</code> object. <p>
286N/A * Examples:
286N/A * <blockquote><pre>
286N/A * "Fahrvergn&uuml;gen".toUpperCase() returns "FAHRVERGN&Uuml;GEN"
286N/A * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!"
286N/A * </pre></blockquote>
286N/A *
286N/A * @return the string, converted to uppercase.
286N/A * @see java.lang.Character#toUpperCase(char)
286N/A * @see java.lang.String#toUpperCase(Locale)
286N/A */
286N/A public XMLString toUpperCase()
286N/A {
286N/A return new XString(str().toUpperCase());
286N/A }
286N/A
286N/A /**
286N/A * Removes white space from both ends of this string.
286N/A *
286N/A * @return this string, with white space removed from the front and end.
286N/A */
286N/A public XMLString trim()
286N/A {
286N/A return new XString(str().trim());
286N/A }
286N/A
286N/A /**
286N/A * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
286N/A * of whitespace. Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
286N/A * the definition of <CODE>S</CODE></A> for details.
286N/A * @param ch Character to check as XML whitespace.
286N/A * @return =true if <var>ch</var> is XML whitespace; otherwise =false.
286N/A */
286N/A private static boolean isSpace(char ch)
286N/A {
286N/A return XMLCharacterRecognizer.isWhiteSpace(ch); // Take the easy way out for now.
286N/A }
286N/A
286N/A /**
286N/A * Conditionally trim all leading and trailing whitespace in the specified String.
286N/A * All strings of white space are
286N/A * replaced by a single space character (#x20), except spaces after punctuation which
286N/A * receive double spaces if doublePunctuationSpaces is true.
286N/A * This function may be useful to a formatter, but to get first class
286N/A * results, the formatter should probably do it's own white space handling
286N/A * based on the semantics of the formatting object.
286N/A *
286N/A * @param trimHead Trim leading whitespace?
286N/A * @param trimTail Trim trailing whitespace?
286N/A * @param doublePunctuationSpaces Use double spaces for punctuation?
286N/A * @return The trimmed string.
286N/A */
286N/A public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail,
286N/A boolean doublePunctuationSpaces)
286N/A {
286N/A
286N/A // %OPT% !!!!!!!
286N/A int len = this.length();
286N/A char[] buf = new char[len];
286N/A
286N/A this.getChars(0, len, buf, 0);
286N/A
286N/A boolean edit = false;
286N/A int s;
286N/A
286N/A for (s = 0; s < len; s++)
286N/A {
286N/A if (isSpace(buf[s]))
286N/A {
286N/A break;
286N/A }
286N/A }
286N/A
286N/A /* replace S to ' '. and ' '+ -> single ' '. */
286N/A int d = s;
286N/A boolean pres = false;
286N/A
286N/A for (; s < len; s++)
286N/A {
286N/A char c = buf[s];
286N/A
286N/A if (isSpace(c))
286N/A {
286N/A if (!pres)
286N/A {
286N/A if (' ' != c)
286N/A {
286N/A edit = true;
286N/A }
286N/A
286N/A buf[d++] = ' ';
286N/A
286N/A if (doublePunctuationSpaces && (s != 0))
286N/A {
286N/A char prevChar = buf[s - 1];
286N/A
286N/A if (!((prevChar == '.') || (prevChar == '!')
286N/A || (prevChar == '?')))
286N/A {
286N/A pres = true;
286N/A }
286N/A }
286N/A else
286N/A {
286N/A pres = true;
286N/A }
286N/A }
286N/A else
286N/A {
286N/A edit = true;
286N/A pres = true;
286N/A }
286N/A }
286N/A else
286N/A {
286N/A buf[d++] = c;
286N/A pres = false;
286N/A }
286N/A }
286N/A
286N/A if (trimTail && 1 <= d && ' ' == buf[d - 1])
286N/A {
286N/A edit = true;
286N/A
286N/A d--;
286N/A }
286N/A
286N/A int start = 0;
286N/A
286N/A if (trimHead && 0 < d && ' ' == buf[0])
286N/A {
286N/A edit = true;
286N/A
286N/A start++;
286N/A }
286N/A
286N/A XMLStringFactory xsf = XMLStringFactoryImpl.getFactory();
286N/A
286N/A return edit ? xsf.newstr(new String(buf, start, d - start)) : this;
286N/A }
286N/A
286N/A /**
286N/A * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
286N/A */
286N/A public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
286N/A {
286N/A visitor.visitStringLiteral(owner, this);
286N/A }
286N/A
286N/A}