jaxp/src/com/sun/org/apache/xpath/internal/objects/XString.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700 (2012-04-12)
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xpath/internal/objects/XString.java@7f561c08de6b
child 12458 d601e4bba306
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: XString.java,v 1.2.4.1 2005/09/14 20:47:20 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xpath.internal.objects;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.util.Locale;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xml.internal.dtm.DTM;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xml.internal.utils.XMLCharacterRecognizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xml.internal.utils.XMLString;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xml.internal.utils.XMLStringFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xpath.internal.ExpressionOwner;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xpath.internal.XPathContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xpath.internal.XPathVisitor;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
 * This class represents an XPath string object, and is capable of
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * converting the string to other types, such as a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * @xsl.usage general
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
public class XString extends XObject implements XMLString
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
    static final long serialVersionUID = 2020470518395094525L;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
  /** Empty string XString object */
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
  public static final XString EMPTYSTRING = new XString("");
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
   * Construct a XString object.  This constructor exists for derived classes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
   * @param val String object this will wrap.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
  protected XString(Object val)
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
    super(val);
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * Construct a XNodeSet object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * @param val String object this will wrap.
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  public XString(String val)
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
    super(val);
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * Tell that this is a CLASS_STRING.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * @return type CLASS_STRING
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  public int getType()
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
    return CLASS_STRING;
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * Given a request type, return the equivalent string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * For diagnostic purposes.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * @return type string "#STRING"
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
  public String getTypeString()
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
    return "#STRING";
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   * Tell if this object contains a java String object.
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   * @return true if this XMLString can return a string without creating one.
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  public boolean hasString()
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   * Cast result object to a number.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   * @return 0.0 if this string is null, numeric value of this string
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   * or NaN
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  public double num()
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    return toDouble();
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
   * Convert a string to a double -- Allowed input is in fixed
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * notation ddd.fff.
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * @return A double value representation of the string, or return Double.NaN
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   * if the string can not be converted.
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
  public double toDouble()
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
    /* XMLCharacterRecognizer.isWhiteSpace(char c) methods treats the following
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
     * characters as white space characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
     * ht - horizontal tab, nl - newline , cr - carriage return and sp - space
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
     * trim() methods by default also takes care of these white space characters
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
     * So trim() method is used to remove leading and trailing white spaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        XMLString s = trim();
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        double result = Double.NaN;
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
        for (int i = 0; i < s.length(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
                char c = s.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    if (c != '-' && c != '.' && ( c < 0X30 || c > 0x39)) {
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
            // The character is not a '-' or a '.' or a digit
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
            // then return NaN because something is wrong.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
                        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
        try
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
                result = Double.parseDouble(s.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
        } catch (NumberFormatException e){}
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
        return result;
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
}
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   * Cast result object to a boolean.
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
   * @return True if the length of this string object is greater
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
   * than 0.
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
  public boolean bool()
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    return str().length() > 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   * Cast result object to a string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   * @return The string this wraps or the empty string if null
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
  public XMLString xstr()
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
    return this;
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * Cast result object to a string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   * @return The string this wraps or the empty string if null
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
  public String str()
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    return (null != m_obj) ? ((String) m_obj) : "";
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * Cast result object to a result tree fragment.
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   * @param support Xpath context to use for the conversion
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   * @return A document fragment with this string as a child node
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
  public int rtf(XPathContext support)
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    DTM frag = support.createDocumentFragment();
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    frag.appendTextChild(str());
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
    return frag.getDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * Directly call the
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   * characters method on the passed ContentHandler for the
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   * string-value. Multiple calls to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   * ContentHandler's characters methods may well occur for a single call to
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   * this method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   * @param ch A non-null reference to a ContentHandler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
  public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
          throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
    String str = str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
    ch.characters(str.toCharArray(), 0, str.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   * Directly call the
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   * comment method on the passed LexicalHandler for the
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
   * string-value.
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   * @param lh A non-null reference to a LexicalHandler.
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   * @throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh)
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
          throws org.xml.sax.SAXException
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    String str = str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
    lh.comment(str.toCharArray(), 0, str.length());
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   * Returns the length of this string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
   * @return  the length of the sequence of characters represented by this
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
   *          object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
  public int length()
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
    return str().length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
   * Returns the character at the specified index. An index ranges
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
   * from <code>0</code> to <code>length() - 1</code>. The first character
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
   * of the sequence is at index <code>0</code>, the next at index
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
   * <code>1</code>, and so on, as for array indexing.
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   * @param      index   the index of the character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
   * @return     the character at the specified index of this string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
   *             The first character is at index <code>0</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   * @exception  IndexOutOfBoundsException  if the <code>index</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   *             argument is negative or not less than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
   *             string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
  public char charAt(int index)
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
    return str().charAt(index);
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   * Copies characters from this string into the destination character
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
   * array.
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   * @param      srcBegin   index of the first character in the string
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   *                        to copy.
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   * @param      srcEnd     index after the last character in the string
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   *                        to copy.
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * @param      dst        the destination array.
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * @param      dstBegin   the start offset in the destination array.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   * @exception IndexOutOfBoundsException If any of the following
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
   *            is true:
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
   *            <ul><li><code>srcBegin</code> is negative.
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   *            <li><code>srcBegin</code> is greater than <code>srcEnd</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   *            <li><code>srcEnd</code> is greater than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   *                string
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   *            <li><code>dstBegin</code> is negative
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   *            <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
   *                <code>dst.length</code></ul>
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
   * @exception NullPointerException if <code>dst</code> is <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
  public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin)
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    str().getChars(srcBegin, srcEnd, dst, dstBegin);
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
   * Tell if two objects are functionally equal.
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
   * @param obj2 Object to compare this to
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   * @return true if the two objects are equal
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
   * @throws javax.xml.transform.TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
  public boolean equals(XObject obj2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
    // In order to handle the 'all' semantics of
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
    // nodeset comparisons, we always call the
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
    // nodeset function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
    int t = obj2.getType();
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
            if (XObject.CLASS_NODESET == t)
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
              return obj2.equals(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
            // If at least one object to be compared is a boolean, then each object
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
            // to be compared is converted to a boolean as if by applying the
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
            // boolean function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
            else if(XObject.CLASS_BOOLEAN == t)
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
                return obj2.bool() == bool();
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
            // Otherwise, if at least one object to be compared is a number, then each object
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
            // to be compared is converted to a number as if by applying the number function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
            else if(XObject.CLASS_NUMBER == t)
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
                return obj2.num() == num();
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    catch(javax.xml.transform.TransformerException te)
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
        throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(te);
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    // Otherwise, both objects to be compared are converted to strings as
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    // if by applying the string function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
    return xstr().equals(obj2.xstr());
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
   * Compares this string to the specified object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
   * The result is <code>true</code> if and only if the argument is not
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
   * <code>null</code> and is a <code>String</code> object that represents
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
   * the same sequence of characters as this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
   * @param obj2   the object to compare this <code>String</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
   *                     against.
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
   * @return  <code>true</code> if the <code>String </code>are equal;
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
   *          <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
   * @see     java.lang.String#compareTo(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
   * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
  public boolean equals(XMLString obj2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
    if (!obj2.hasString())
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
      return obj2.equals(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
      return str().equals(obj2.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
   * Compares this string to the specified object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
   * The result is <code>true</code> if and only if the argument is not
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
   * <code>null</code> and is a <code>String</code> object that represents
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
   * the same sequence of characters as this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
   * @param   obj2       the object to compare this <code>String</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
   *                     against.
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
   * @return  <code>true</code> if the <code>String </code>are equal;
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
   *          <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
   * @see     java.lang.String#compareTo(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
   * @see     java.lang.String#equalsIgnoreCase(java.lang.String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
  public boolean equals(Object obj2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    if (null == obj2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
      // In order to handle the 'all' semantics of
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
      // nodeset comparisons, we always call the
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
      // nodeset function.
7f561c08de6b Initial load
duke
parents:
diff changeset
   368
    else if (obj2 instanceof XNodeSet)
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
      return obj2.equals(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
    else if(obj2 instanceof XNumber)
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
        return obj2.equals(this);
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
      return str().equals(obj2.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
   * Compares this <code>String</code> to another <code>String</code>,
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
   * ignoring case considerations.  Two strings are considered equal
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
   * ignoring case if they are of the same length, and corresponding
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
   * characters in the two strings are equal ignoring case.
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
   * @param   anotherString   the <code>String</code> to compare this
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
   *                          <code>String</code> against.
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
   * @return  <code>true</code> if the argument is not <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
   *          and the <code>String</code>s are equal,
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
   *          ignoring case; <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
   * @see     #equals(Object)
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
   * @see     java.lang.Character#toLowerCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
   * @see java.lang.Character#toUpperCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
  public boolean equalsIgnoreCase(String anotherString)
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
    return str().equalsIgnoreCase(anotherString);
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
   * Compares two strings lexicographically.
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
   * @param   xstr   the <code>String</code> to be compared.
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
   * @return  the value <code>0</code> if the argument string is equal to
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
   *          this string; a value less than <code>0</code> if this string
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
   *          is lexicographically less than the string argument; and a
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
   *          value greater than <code>0</code> if this string is
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
   *          lexicographically greater than the string argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
   * @exception java.lang.NullPointerException if <code>anotherString</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
   *          is <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
  public int compareTo(XMLString xstr)
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   411
7f561c08de6b Initial load
duke
parents:
diff changeset
   412
    int len1 = this.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   413
    int len2 = xstr.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   414
    int n = Math.min(len1, len2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   415
    int i = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
    int j = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
    while (n-- != 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
      char c1 = this.charAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
      char c2 = xstr.charAt(j);
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
      if (c1 != c2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
        return c1 - c2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   426
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   427
7f561c08de6b Initial load
duke
parents:
diff changeset
   428
      i++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   429
      j++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   430
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   431
7f561c08de6b Initial load
duke
parents:
diff changeset
   432
    return len1 - len2;
7f561c08de6b Initial load
duke
parents:
diff changeset
   433
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   434
7f561c08de6b Initial load
duke
parents:
diff changeset
   435
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   436
   * Compares two strings lexicographically, ignoring case considerations.
7f561c08de6b Initial load
duke
parents:
diff changeset
   437
   * This method returns an integer whose sign is that of
7f561c08de6b Initial load
duke
parents:
diff changeset
   438
   * <code>this.toUpperCase().toLowerCase().compareTo(
7f561c08de6b Initial load
duke
parents:
diff changeset
   439
   * str.toUpperCase().toLowerCase())</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   440
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   441
   * Note that this method does <em>not</em> take locale into account,
7f561c08de6b Initial load
duke
parents:
diff changeset
   442
   * and will result in an unsatisfactory ordering for certain locales.
7f561c08de6b Initial load
duke
parents:
diff changeset
   443
   * The java.text package provides <em>collators</em> to allow
7f561c08de6b Initial load
duke
parents:
diff changeset
   444
   * locale-sensitive ordering.
7f561c08de6b Initial load
duke
parents:
diff changeset
   445
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   446
   * @param   str   the <code>String</code> to be compared.
7f561c08de6b Initial load
duke
parents:
diff changeset
   447
   * @return  a negative integer, zero, or a positive integer as the
7f561c08de6b Initial load
duke
parents:
diff changeset
   448
   *          the specified String is greater than, equal to, or less
7f561c08de6b Initial load
duke
parents:
diff changeset
   449
   *          than this String, ignoring case considerations.
7f561c08de6b Initial load
duke
parents:
diff changeset
   450
   * @see     java.text.Collator#compare(String, String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   451
   * @since   1.2
7f561c08de6b Initial load
duke
parents:
diff changeset
   452
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   453
  public int compareToIgnoreCase(XMLString str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   454
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   455
    // %REVIEW%  Like it says, @since 1.2. Doesn't exist in earlier
7f561c08de6b Initial load
duke
parents:
diff changeset
   456
    // versions of Java, hence we can't yet shell out to it. We can implement
7f561c08de6b Initial load
duke
parents:
diff changeset
   457
    // it as character-by-character compare, but doing so efficiently
7f561c08de6b Initial load
duke
parents:
diff changeset
   458
    // is likely to be (ahem) interesting.
7f561c08de6b Initial load
duke
parents:
diff changeset
   459
    //
7f561c08de6b Initial load
duke
parents:
diff changeset
   460
    // However, since nobody is actually _using_ this method yet:
7f561c08de6b Initial load
duke
parents:
diff changeset
   461
    //    return str().compareToIgnoreCase(str.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   462
7f561c08de6b Initial load
duke
parents:
diff changeset
   463
    throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   464
      new java.lang.NoSuchMethodException(
7f561c08de6b Initial load
duke
parents:
diff changeset
   465
        "Java 1.2 method, not yet implemented"));
7f561c08de6b Initial load
duke
parents:
diff changeset
   466
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   467
7f561c08de6b Initial load
duke
parents:
diff changeset
   468
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   469
   * Tests if this string starts with the specified prefix beginning
7f561c08de6b Initial load
duke
parents:
diff changeset
   470
   * a specified index.
7f561c08de6b Initial load
duke
parents:
diff changeset
   471
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   472
   * @param   prefix    the prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   473
   * @param   toffset   where to begin looking in the string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   474
   * @return  <code>true</code> if the character sequence represented by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   475
   *          argument is a prefix of the substring of this object starting
7f561c08de6b Initial load
duke
parents:
diff changeset
   476
   *          at index <code>toffset</code>; <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   477
   *          The result is <code>false</code> if <code>toffset</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   478
   *          negative or greater than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   479
   *          <code>String</code> object; otherwise the result is the same
7f561c08de6b Initial load
duke
parents:
diff changeset
   480
   *          as the result of the expression
7f561c08de6b Initial load
duke
parents:
diff changeset
   481
   *          <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   482
   *          this.subString(toffset).startsWith(prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   483
   *          </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   484
   * @exception java.lang.NullPointerException if <code>prefix</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   485
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   486
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   487
  public boolean startsWith(String prefix, int toffset)
7f561c08de6b Initial load
duke
parents:
diff changeset
   488
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   489
    return str().startsWith(prefix, toffset);
7f561c08de6b Initial load
duke
parents:
diff changeset
   490
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   491
7f561c08de6b Initial load
duke
parents:
diff changeset
   492
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   493
   * Tests if this string starts with the specified prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   494
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   495
   * @param   prefix   the prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   496
   * @return  <code>true</code> if the character sequence represented by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   497
   *          argument is a prefix of the character sequence represented by
7f561c08de6b Initial load
duke
parents:
diff changeset
   498
   *          this string; <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   499
   *          Note also that <code>true</code> will be returned if the
7f561c08de6b Initial load
duke
parents:
diff changeset
   500
   *          argument is an empty string or is equal to this
7f561c08de6b Initial load
duke
parents:
diff changeset
   501
   *          <code>String</code> object as determined by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   502
   *          {@link #equals(Object)} method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   503
   * @exception java.lang.NullPointerException if <code>prefix</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   504
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   505
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   506
  public boolean startsWith(String prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   507
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   508
    return startsWith(prefix, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   509
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   510
7f561c08de6b Initial load
duke
parents:
diff changeset
   511
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   512
   * Tests if this string starts with the specified prefix beginning
7f561c08de6b Initial load
duke
parents:
diff changeset
   513
   * a specified index.
7f561c08de6b Initial load
duke
parents:
diff changeset
   514
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   515
   * @param   prefix    the prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   516
   * @param   toffset   where to begin looking in the string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   517
   * @return  <code>true</code> if the character sequence represented by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   518
   *          argument is a prefix of the substring of this object starting
7f561c08de6b Initial load
duke
parents:
diff changeset
   519
   *          at index <code>toffset</code>; <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   520
   *          The result is <code>false</code> if <code>toffset</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   521
   *          negative or greater than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   522
   *          <code>String</code> object; otherwise the result is the same
7f561c08de6b Initial load
duke
parents:
diff changeset
   523
   *          as the result of the expression
7f561c08de6b Initial load
duke
parents:
diff changeset
   524
   *          <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   525
   *          this.subString(toffset).startsWith(prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   526
   *          </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   527
   * @exception java.lang.NullPointerException if <code>prefix</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   528
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   529
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   530
  public boolean startsWith(XMLString prefix, int toffset)
7f561c08de6b Initial load
duke
parents:
diff changeset
   531
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   532
7f561c08de6b Initial load
duke
parents:
diff changeset
   533
    int to = toffset;
7f561c08de6b Initial load
duke
parents:
diff changeset
   534
    int tlim = this.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   535
    int po = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
   536
    int pc = prefix.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
   537
7f561c08de6b Initial load
duke
parents:
diff changeset
   538
    // Note: toffset might be near -1>>>1.
7f561c08de6b Initial load
duke
parents:
diff changeset
   539
    if ((toffset < 0) || (toffset > tlim - pc))
7f561c08de6b Initial load
duke
parents:
diff changeset
   540
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   541
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   542
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   543
7f561c08de6b Initial load
duke
parents:
diff changeset
   544
    while (--pc >= 0)
7f561c08de6b Initial load
duke
parents:
diff changeset
   545
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   546
      if (this.charAt(to) != prefix.charAt(po))
7f561c08de6b Initial load
duke
parents:
diff changeset
   547
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   548
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   549
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   550
7f561c08de6b Initial load
duke
parents:
diff changeset
   551
      to++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   552
      po++;
7f561c08de6b Initial load
duke
parents:
diff changeset
   553
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   554
7f561c08de6b Initial load
duke
parents:
diff changeset
   555
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   556
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   557
7f561c08de6b Initial load
duke
parents:
diff changeset
   558
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   559
   * Tests if this string starts with the specified prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   560
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   561
   * @param   prefix   the prefix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   562
   * @return  <code>true</code> if the character sequence represented by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   563
   *          argument is a prefix of the character sequence represented by
7f561c08de6b Initial load
duke
parents:
diff changeset
   564
   *          this string; <code>false</code> otherwise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   565
   *          Note also that <code>true</code> will be returned if the
7f561c08de6b Initial load
duke
parents:
diff changeset
   566
   *          argument is an empty string or is equal to this
7f561c08de6b Initial load
duke
parents:
diff changeset
   567
   *          <code>String</code> object as determined by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   568
   *          {@link #equals(Object)} method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   569
   * @exception java.lang.NullPointerException if <code>prefix</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   570
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   571
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   572
  public boolean startsWith(XMLString prefix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   573
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   574
    return startsWith(prefix, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
   575
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   576
7f561c08de6b Initial load
duke
parents:
diff changeset
   577
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   578
   * Tests if this string ends with the specified suffix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   579
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   580
   * @param   suffix   the suffix.
7f561c08de6b Initial load
duke
parents:
diff changeset
   581
   * @return  <code>true</code> if the character sequence represented by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   582
   *          argument is a suffix of the character sequence represented by
7f561c08de6b Initial load
duke
parents:
diff changeset
   583
   *          this object; <code>false</code> otherwise. Note that the
7f561c08de6b Initial load
duke
parents:
diff changeset
   584
   *          result will be <code>true</code> if the argument is the
7f561c08de6b Initial load
duke
parents:
diff changeset
   585
   *          empty string or is equal to this <code>String</code> object
7f561c08de6b Initial load
duke
parents:
diff changeset
   586
   *          as determined by the {@link #equals(Object)} method.
7f561c08de6b Initial load
duke
parents:
diff changeset
   587
   * @exception java.lang.NullPointerException if <code>suffix</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   588
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   589
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   590
  public boolean endsWith(String suffix)
7f561c08de6b Initial load
duke
parents:
diff changeset
   591
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   592
    return str().endsWith(suffix);
7f561c08de6b Initial load
duke
parents:
diff changeset
   593
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   594
7f561c08de6b Initial load
duke
parents:
diff changeset
   595
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   596
   * Returns a hashcode for this string. The hashcode for a
7f561c08de6b Initial load
duke
parents:
diff changeset
   597
   * <code>String</code> object is computed as
7f561c08de6b Initial load
duke
parents:
diff changeset
   598
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   599
   * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
7f561c08de6b Initial load
duke
parents:
diff changeset
   600
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   601
   * using <code>int</code> arithmetic, where <code>s[i]</code> is the
7f561c08de6b Initial load
duke
parents:
diff changeset
   602
   * <i>i</i>th character of the string, <code>n</code> is the length of
7f561c08de6b Initial load
duke
parents:
diff changeset
   603
   * the string, and <code>^</code> indicates exponentiation.
7f561c08de6b Initial load
duke
parents:
diff changeset
   604
   * (The hash value of the empty string is zero.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   605
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   606
   * @return  a hash code value for this object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   607
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   608
  public int hashCode()
7f561c08de6b Initial load
duke
parents:
diff changeset
   609
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   610
    return str().hashCode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   611
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   612
7f561c08de6b Initial load
duke
parents:
diff changeset
   613
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   614
   * Returns the index within this string of the first occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   615
   * specified character. If a character with value <code>ch</code> occurs
7f561c08de6b Initial load
duke
parents:
diff changeset
   616
   * in the character sequence represented by this <code>String</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   617
   * object, then the index of the first such occurrence is returned --
7f561c08de6b Initial load
duke
parents:
diff changeset
   618
   * that is, the smallest value <i>k</i> such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   619
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   620
   * this.charAt(<i>k</i>) == ch
7f561c08de6b Initial load
duke
parents:
diff changeset
   621
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   622
   * is <code>true</code>. If no such character occurs in this string,
7f561c08de6b Initial load
duke
parents:
diff changeset
   623
   * then <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   624
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   625
   * @param   ch   a character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   626
   * @return  the index of the first occurrence of the character in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   627
   *          character sequence represented by this object, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   628
   *          <code>-1</code> if the character does not occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
   629
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   630
  public int indexOf(int ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   631
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   632
    return str().indexOf(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   633
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   634
7f561c08de6b Initial load
duke
parents:
diff changeset
   635
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   636
   * Returns the index within this string of the first occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   637
   * specified character, starting the search at the specified index.
7f561c08de6b Initial load
duke
parents:
diff changeset
   638
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   639
   * If a character with value <code>ch</code> occurs in the character
7f561c08de6b Initial load
duke
parents:
diff changeset
   640
   * sequence represented by this <code>String</code> object at an index
7f561c08de6b Initial load
duke
parents:
diff changeset
   641
   * no smaller than <code>fromIndex</code>, then the index of the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   642
   * such occurrence is returned--that is, the smallest value <i>k</i>
7f561c08de6b Initial load
duke
parents:
diff changeset
   643
   * such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   644
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   645
   * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   646
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   647
   * is true. If no such character occurs in this string at or after
7f561c08de6b Initial load
duke
parents:
diff changeset
   648
   * position <code>fromIndex</code>, then <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   649
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   650
   * There is no restriction on the value of <code>fromIndex</code>. If it
7f561c08de6b Initial load
duke
parents:
diff changeset
   651
   * is negative, it has the same effect as if it were zero: this entire
7f561c08de6b Initial load
duke
parents:
diff changeset
   652
   * string may be searched. If it is greater than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   653
   * string, it has the same effect as if it were equal to the length of
7f561c08de6b Initial load
duke
parents:
diff changeset
   654
   * this string: <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   655
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   656
   * @param   ch          a character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   657
   * @param   fromIndex   the index to start the search from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   658
   * @return  the index of the first occurrence of the character in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   659
   *          character sequence represented by this object that is greater
7f561c08de6b Initial load
duke
parents:
diff changeset
   660
   *          than or equal to <code>fromIndex</code>, or <code>-1</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   661
   *          if the character does not occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
   662
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   663
  public int indexOf(int ch, int fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   664
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   665
    return str().indexOf(ch, fromIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   666
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   667
7f561c08de6b Initial load
duke
parents:
diff changeset
   668
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   669
   * Returns the index within this string of the last occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   670
   * specified character. That is, the index returned is the largest
7f561c08de6b Initial load
duke
parents:
diff changeset
   671
   * value <i>k</i> such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   672
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   673
   * this.charAt(<i>k</i>) == ch
7f561c08de6b Initial load
duke
parents:
diff changeset
   674
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   675
   * is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   676
   * The String is searched backwards starting at the last character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   677
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   678
   * @param   ch   a character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   679
   * @return  the index of the last occurrence of the character in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   680
   *          character sequence represented by this object, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   681
   *          <code>-1</code> if the character does not occur.
7f561c08de6b Initial load
duke
parents:
diff changeset
   682
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   683
  public int lastIndexOf(int ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   684
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   685
    return str().lastIndexOf(ch);
7f561c08de6b Initial load
duke
parents:
diff changeset
   686
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   687
7f561c08de6b Initial load
duke
parents:
diff changeset
   688
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   689
   * Returns the index within this string of the last occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   690
   * specified character, searching backward starting at the specified
7f561c08de6b Initial load
duke
parents:
diff changeset
   691
   * index. That is, the index returned is the largest value <i>k</i>
7f561c08de6b Initial load
duke
parents:
diff changeset
   692
   * such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   693
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   694
   * this.charAt(k) == ch) && (k <= fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   695
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   696
   * is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   697
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   698
   * @param   ch          a character.
7f561c08de6b Initial load
duke
parents:
diff changeset
   699
   * @param   fromIndex   the index to start the search from. There is no
7f561c08de6b Initial load
duke
parents:
diff changeset
   700
   *          restriction on the value of <code>fromIndex</code>. If it is
7f561c08de6b Initial load
duke
parents:
diff changeset
   701
   *          greater than or equal to the length of this string, it has
7f561c08de6b Initial load
duke
parents:
diff changeset
   702
   *          the same effect as if it were equal to one less than the
7f561c08de6b Initial load
duke
parents:
diff changeset
   703
   *          length of this string: this entire string may be searched.
7f561c08de6b Initial load
duke
parents:
diff changeset
   704
   *          If it is negative, it has the same effect as if it were -1:
7f561c08de6b Initial load
duke
parents:
diff changeset
   705
   *          -1 is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   706
   * @return  the index of the last occurrence of the character in the
7f561c08de6b Initial load
duke
parents:
diff changeset
   707
   *          character sequence represented by this object that is less
7f561c08de6b Initial load
duke
parents:
diff changeset
   708
   *          than or equal to <code>fromIndex</code>, or <code>-1</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   709
   *          if the character does not occur before that point.
7f561c08de6b Initial load
duke
parents:
diff changeset
   710
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   711
  public int lastIndexOf(int ch, int fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   712
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   713
    return str().lastIndexOf(ch, fromIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   714
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   715
7f561c08de6b Initial load
duke
parents:
diff changeset
   716
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   717
   * Returns the index within this string of the first occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   718
   * specified substring. The integer returned is the smallest value
7f561c08de6b Initial load
duke
parents:
diff changeset
   719
   * <i>k</i> such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   720
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   721
   * this.startsWith(str, <i>k</i>)
7f561c08de6b Initial load
duke
parents:
diff changeset
   722
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   723
   * is <code>true</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   724
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   725
   * @param   str   any string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   726
   * @return  if the string argument occurs as a substring within this
7f561c08de6b Initial load
duke
parents:
diff changeset
   727
   *          object, then the index of the first character of the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   728
   *          such substring is returned; if it does not occur as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   729
   *          substring, <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   730
   * @exception java.lang.NullPointerException if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   731
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   732
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   733
  public int indexOf(String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   734
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   735
    return str().indexOf(str);
7f561c08de6b Initial load
duke
parents:
diff changeset
   736
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   737
7f561c08de6b Initial load
duke
parents:
diff changeset
   738
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   739
   * Returns the index within this string of the first occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   740
   * specified substring. The integer returned is the smallest value
7f561c08de6b Initial load
duke
parents:
diff changeset
   741
   * <i>k</i> such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   742
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   743
   * this.startsWith(str, <i>k</i>)
7f561c08de6b Initial load
duke
parents:
diff changeset
   744
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   745
   * is <code>true</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   746
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   747
   * @param   str   any string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   748
   * @return  if the string argument occurs as a substring within this
7f561c08de6b Initial load
duke
parents:
diff changeset
   749
   *          object, then the index of the first character of the first
7f561c08de6b Initial load
duke
parents:
diff changeset
   750
   *          such substring is returned; if it does not occur as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   751
   *          substring, <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   752
   * @exception java.lang.NullPointerException if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   753
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   754
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   755
  public int indexOf(XMLString str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   756
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   757
    return str().indexOf(str.toString());
7f561c08de6b Initial load
duke
parents:
diff changeset
   758
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   759
7f561c08de6b Initial load
duke
parents:
diff changeset
   760
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   761
   * Returns the index within this string of the first occurrence of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   762
   * specified substring, starting at the specified index. The integer
7f561c08de6b Initial load
duke
parents:
diff changeset
   763
   * returned is the smallest value <i>k</i> such that:
7f561c08de6b Initial load
duke
parents:
diff changeset
   764
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   765
   * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   766
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   767
   * is <code>true</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   768
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   769
   * There is no restriction on the value of <code>fromIndex</code>. If
7f561c08de6b Initial load
duke
parents:
diff changeset
   770
   * it is negative, it has the same effect as if it were zero: this entire
7f561c08de6b Initial load
duke
parents:
diff changeset
   771
   * string may be searched. If it is greater than the length of this
7f561c08de6b Initial load
duke
parents:
diff changeset
   772
   * string, it has the same effect as if it were equal to the length of
7f561c08de6b Initial load
duke
parents:
diff changeset
   773
   * this string: <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   774
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   775
   * @param   str         the substring to search for.
7f561c08de6b Initial load
duke
parents:
diff changeset
   776
   * @param   fromIndex   the index to start the search from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   777
   * @return  If the string argument occurs as a substring within this
7f561c08de6b Initial load
duke
parents:
diff changeset
   778
   *          object at a starting index no smaller than
7f561c08de6b Initial load
duke
parents:
diff changeset
   779
   *          <code>fromIndex</code>, then the index of the first character
7f561c08de6b Initial load
duke
parents:
diff changeset
   780
   *          of the first such substring is returned. If it does not occur
7f561c08de6b Initial load
duke
parents:
diff changeset
   781
   *          as a substring starting at <code>fromIndex</code> or beyond,
7f561c08de6b Initial load
duke
parents:
diff changeset
   782
   *          <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   783
   * @exception java.lang.NullPointerException if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   784
   *          <code>null</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
   785
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   786
  public int indexOf(String str, int fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   787
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   788
    return str().indexOf(str, fromIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   789
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   790
7f561c08de6b Initial load
duke
parents:
diff changeset
   791
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   792
   * Returns the index within this string of the rightmost occurrence
7f561c08de6b Initial load
duke
parents:
diff changeset
   793
   * of the specified substring.  The rightmost empty string "" is
7f561c08de6b Initial load
duke
parents:
diff changeset
   794
   * considered to occur at the index value <code>this.length()</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   795
   * The returned index is the largest value <i>k</i> such that
7f561c08de6b Initial load
duke
parents:
diff changeset
   796
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   797
   * this.startsWith(str, k)
7f561c08de6b Initial load
duke
parents:
diff changeset
   798
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   799
   * is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   800
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   801
   * @param   str   the substring to search for.
7f561c08de6b Initial load
duke
parents:
diff changeset
   802
   * @return  if the string argument occurs one or more times as a substring
7f561c08de6b Initial load
duke
parents:
diff changeset
   803
   *          within this object, then the index of the first character of
7f561c08de6b Initial load
duke
parents:
diff changeset
   804
   *          the last such substring is returned. If it does not occur as
7f561c08de6b Initial load
duke
parents:
diff changeset
   805
   *          a substring, <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   806
   * @exception java.lang.NullPointerException  if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   807
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   808
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   809
  public int lastIndexOf(String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   810
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   811
    return str().lastIndexOf(str);
7f561c08de6b Initial load
duke
parents:
diff changeset
   812
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   813
7f561c08de6b Initial load
duke
parents:
diff changeset
   814
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   815
   * Returns the index within this string of the last occurrence of
7f561c08de6b Initial load
duke
parents:
diff changeset
   816
   * the specified substring.
7f561c08de6b Initial load
duke
parents:
diff changeset
   817
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   818
   * @param   str         the substring to search for.
7f561c08de6b Initial load
duke
parents:
diff changeset
   819
   * @param   fromIndex   the index to start the search from. There is no
7f561c08de6b Initial load
duke
parents:
diff changeset
   820
   *          restriction on the value of fromIndex. If it is greater than
7f561c08de6b Initial load
duke
parents:
diff changeset
   821
   *          the length of this string, it has the same effect as if it
7f561c08de6b Initial load
duke
parents:
diff changeset
   822
   *          were equal to the length of this string: this entire string
7f561c08de6b Initial load
duke
parents:
diff changeset
   823
   *          may be searched. If it is negative, it has the same effect
7f561c08de6b Initial load
duke
parents:
diff changeset
   824
   *          as if it were -1: -1 is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   825
   * @return  If the string argument occurs one or more times as a substring
7f561c08de6b Initial load
duke
parents:
diff changeset
   826
   *          within this object at a starting index no greater than
7f561c08de6b Initial load
duke
parents:
diff changeset
   827
   *          <code>fromIndex</code>, then the index of the first character of
7f561c08de6b Initial load
duke
parents:
diff changeset
   828
   *          the last such substring is returned. If it does not occur as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   829
   *          substring starting at <code>fromIndex</code> or earlier,
7f561c08de6b Initial load
duke
parents:
diff changeset
   830
   *          <code>-1</code> is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   831
   * @exception java.lang.NullPointerException if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   832
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   833
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   834
  public int lastIndexOf(String str, int fromIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   835
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   836
    return str().lastIndexOf(str, fromIndex);
7f561c08de6b Initial load
duke
parents:
diff changeset
   837
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   838
7f561c08de6b Initial load
duke
parents:
diff changeset
   839
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   840
   * Returns a new string that is a substring of this string. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   841
   * substring begins with the character at the specified index and
7f561c08de6b Initial load
duke
parents:
diff changeset
   842
   * extends to the end of this string. <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   843
   * Examples:
7f561c08de6b Initial load
duke
parents:
diff changeset
   844
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   845
   * "unhappy".substring(2) returns "happy"
7f561c08de6b Initial load
duke
parents:
diff changeset
   846
   * "Harbison".substring(3) returns "bison"
7f561c08de6b Initial load
duke
parents:
diff changeset
   847
   * "emptiness".substring(9) returns "" (an empty string)
7f561c08de6b Initial load
duke
parents:
diff changeset
   848
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   849
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   850
   * @param      beginIndex   the beginning index, inclusive.
7f561c08de6b Initial load
duke
parents:
diff changeset
   851
   * @return     the specified substring.
7f561c08de6b Initial load
duke
parents:
diff changeset
   852
   * @exception  IndexOutOfBoundsException  if
7f561c08de6b Initial load
duke
parents:
diff changeset
   853
   *             <code>beginIndex</code> is negative or larger than the
7f561c08de6b Initial load
duke
parents:
diff changeset
   854
   *             length of this <code>String</code> object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   855
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   856
  public XMLString substring(int beginIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   857
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   858
    return new XString(str().substring(beginIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   859
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   860
7f561c08de6b Initial load
duke
parents:
diff changeset
   861
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   862
   * Returns a new string that is a substring of this string. The
7f561c08de6b Initial load
duke
parents:
diff changeset
   863
   * substring begins at the specified <code>beginIndex</code> and
7f561c08de6b Initial load
duke
parents:
diff changeset
   864
   * extends to the character at index <code>endIndex - 1</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   865
   * Thus the length of the substring is <code>endIndex-beginIndex</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   866
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   867
   * @param      beginIndex   the beginning index, inclusive.
7f561c08de6b Initial load
duke
parents:
diff changeset
   868
   * @param      endIndex     the ending index, exclusive.
7f561c08de6b Initial load
duke
parents:
diff changeset
   869
   * @return     the specified substring.
7f561c08de6b Initial load
duke
parents:
diff changeset
   870
   * @exception  IndexOutOfBoundsException  if the
7f561c08de6b Initial load
duke
parents:
diff changeset
   871
   *             <code>beginIndex</code> is negative, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   872
   *             <code>endIndex</code> is larger than the length of
7f561c08de6b Initial load
duke
parents:
diff changeset
   873
   *             this <code>String</code> object, or
7f561c08de6b Initial load
duke
parents:
diff changeset
   874
   *             <code>beginIndex</code> is larger than
7f561c08de6b Initial load
duke
parents:
diff changeset
   875
   *             <code>endIndex</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   876
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   877
  public XMLString substring(int beginIndex, int endIndex)
7f561c08de6b Initial load
duke
parents:
diff changeset
   878
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   879
    return new XString(str().substring(beginIndex, endIndex));
7f561c08de6b Initial load
duke
parents:
diff changeset
   880
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   881
7f561c08de6b Initial load
duke
parents:
diff changeset
   882
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   883
   * Concatenates the specified string to the end of this string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   884
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   885
   * @param   str   the <code>String</code> that is concatenated to the end
7f561c08de6b Initial load
duke
parents:
diff changeset
   886
   *                of this <code>String</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   887
   * @return  a string that represents the concatenation of this object's
7f561c08de6b Initial load
duke
parents:
diff changeset
   888
   *          characters followed by the string argument's characters.
7f561c08de6b Initial load
duke
parents:
diff changeset
   889
   * @exception java.lang.NullPointerException if <code>str</code> is
7f561c08de6b Initial load
duke
parents:
diff changeset
   890
   *          <code>null</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   891
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   892
  public XMLString concat(String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   893
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   894
7f561c08de6b Initial load
duke
parents:
diff changeset
   895
    // %REVIEW% Make an FSB here?
7f561c08de6b Initial load
duke
parents:
diff changeset
   896
    return new XString(str().concat(str));
7f561c08de6b Initial load
duke
parents:
diff changeset
   897
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   898
7f561c08de6b Initial load
duke
parents:
diff changeset
   899
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   900
   * Converts all of the characters in this <code>String</code> to lower
7f561c08de6b Initial load
duke
parents:
diff changeset
   901
   * case using the rules of the given <code>Locale</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   902
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   903
   * @param locale use the case transformation rules for this locale
7f561c08de6b Initial load
duke
parents:
diff changeset
   904
   * @return the String, converted to lowercase.
7f561c08de6b Initial load
duke
parents:
diff changeset
   905
   * @see     java.lang.Character#toLowerCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   906
   * @see     java.lang.String#toUpperCase(Locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   907
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   908
  public XMLString toLowerCase(Locale locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   909
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   910
    return new XString(str().toLowerCase(locale));
7f561c08de6b Initial load
duke
parents:
diff changeset
   911
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   912
7f561c08de6b Initial load
duke
parents:
diff changeset
   913
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   914
   * Converts all of the characters in this <code>String</code> to lower
7f561c08de6b Initial load
duke
parents:
diff changeset
   915
   * case using the rules of the default locale, which is returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   916
   * by <code>Locale.getDefault</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   917
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   918
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   919
   * @return  the string, converted to lowercase.
7f561c08de6b Initial load
duke
parents:
diff changeset
   920
   * @see     java.lang.Character#toLowerCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   921
   * @see     java.lang.String#toLowerCase(Locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   922
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   923
  public XMLString toLowerCase()
7f561c08de6b Initial load
duke
parents:
diff changeset
   924
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   925
    return new XString(str().toLowerCase());
7f561c08de6b Initial load
duke
parents:
diff changeset
   926
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   927
7f561c08de6b Initial load
duke
parents:
diff changeset
   928
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   929
   * Converts all of the characters in this <code>String</code> to upper
7f561c08de6b Initial load
duke
parents:
diff changeset
   930
   * case using the rules of the given locale.
7f561c08de6b Initial load
duke
parents:
diff changeset
   931
   * @param locale use the case transformation rules for this locale
7f561c08de6b Initial load
duke
parents:
diff changeset
   932
   * @return the String, converted to uppercase.
7f561c08de6b Initial load
duke
parents:
diff changeset
   933
   * @see     java.lang.Character#toUpperCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   934
   * @see     java.lang.String#toLowerCase(Locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   935
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   936
  public XMLString toUpperCase(Locale locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   937
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   938
    return new XString(str().toUpperCase(locale));
7f561c08de6b Initial load
duke
parents:
diff changeset
   939
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   940
7f561c08de6b Initial load
duke
parents:
diff changeset
   941
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   942
   * Converts all of the characters in this <code>String</code> to upper
7f561c08de6b Initial load
duke
parents:
diff changeset
   943
   * case using the rules of the default locale, which is returned
7f561c08de6b Initial load
duke
parents:
diff changeset
   944
   * by <code>Locale.getDefault</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   945
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   946
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   947
   * If no character in this string has a different uppercase version,
7f561c08de6b Initial load
duke
parents:
diff changeset
   948
   * based on calling the <code>toUpperCase</code> method defined by
7f561c08de6b Initial load
duke
parents:
diff changeset
   949
   * <code>Character</code>, then the original string is returned.
7f561c08de6b Initial load
duke
parents:
diff changeset
   950
   * <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   951
   * Otherwise, this method creates a new <code>String</code> object
7f561c08de6b Initial load
duke
parents:
diff changeset
   952
   * representing a character sequence identical in length to the
7f561c08de6b Initial load
duke
parents:
diff changeset
   953
   * character sequence represented by this <code>String</code> object and
7f561c08de6b Initial load
duke
parents:
diff changeset
   954
   * with every character equal to the result of applying the method
7f561c08de6b Initial load
duke
parents:
diff changeset
   955
   * <code>Character.toUpperCase</code> to the corresponding character of
7f561c08de6b Initial load
duke
parents:
diff changeset
   956
   * this <code>String</code> object. <p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   957
   * Examples:
7f561c08de6b Initial load
duke
parents:
diff changeset
   958
   * <blockquote><pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
   959
   * "Fahrvergn&uuml;gen".toUpperCase() returns "FAHRVERGN&Uuml;GEN"
7f561c08de6b Initial load
duke
parents:
diff changeset
   960
   * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!"
7f561c08de6b Initial load
duke
parents:
diff changeset
   961
   * </pre></blockquote>
7f561c08de6b Initial load
duke
parents:
diff changeset
   962
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   963
   * @return  the string, converted to uppercase.
7f561c08de6b Initial load
duke
parents:
diff changeset
   964
   * @see     java.lang.Character#toUpperCase(char)
7f561c08de6b Initial load
duke
parents:
diff changeset
   965
   * @see     java.lang.String#toUpperCase(Locale)
7f561c08de6b Initial load
duke
parents:
diff changeset
   966
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   967
  public XMLString toUpperCase()
7f561c08de6b Initial load
duke
parents:
diff changeset
   968
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   969
    return new XString(str().toUpperCase());
7f561c08de6b Initial load
duke
parents:
diff changeset
   970
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   971
7f561c08de6b Initial load
duke
parents:
diff changeset
   972
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   973
   * Removes white space from both ends of this string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   974
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   975
   * @return  this string, with white space removed from the front and end.
7f561c08de6b Initial load
duke
parents:
diff changeset
   976
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   977
  public XMLString trim()
7f561c08de6b Initial load
duke
parents:
diff changeset
   978
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   979
    return new XString(str().trim());
7f561c08de6b Initial load
duke
parents:
diff changeset
   980
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   981
7f561c08de6b Initial load
duke
parents:
diff changeset
   982
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   983
   * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
7f561c08de6b Initial load
duke
parents:
diff changeset
   984
   * of whitespace.  Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
7f561c08de6b Initial load
duke
parents:
diff changeset
   985
   * the definition of <CODE>S</CODE></A> for details.
7f561c08de6b Initial load
duke
parents:
diff changeset
   986
   * @param   ch      Character to check as XML whitespace.
7f561c08de6b Initial load
duke
parents:
diff changeset
   987
   * @return          =true if <var>ch</var> is XML whitespace; otherwise =false.
7f561c08de6b Initial load
duke
parents:
diff changeset
   988
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   989
  private static boolean isSpace(char ch)
7f561c08de6b Initial load
duke
parents:
diff changeset
   990
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   991
    return XMLCharacterRecognizer.isWhiteSpace(ch);  // Take the easy way out for now.
7f561c08de6b Initial load
duke
parents:
diff changeset
   992
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   993
7f561c08de6b Initial load
duke
parents:
diff changeset
   994
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   995
   * Conditionally trim all leading and trailing whitespace in the specified String.
7f561c08de6b Initial load
duke
parents:
diff changeset
   996
   * All strings of white space are
7f561c08de6b Initial load
duke
parents:
diff changeset
   997
   * replaced by a single space character (#x20), except spaces after punctuation which
7f561c08de6b Initial load
duke
parents:
diff changeset
   998
   * receive double spaces if doublePunctuationSpaces is true.
7f561c08de6b Initial load
duke
parents:
diff changeset
   999
   * This function may be useful to a formatter, but to get first class
7f561c08de6b Initial load
duke
parents:
diff changeset
  1000
   * results, the formatter should probably do it's own white space handling
7f561c08de6b Initial load
duke
parents:
diff changeset
  1001
   * based on the semantics of the formatting object.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1002
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
  1003
   * @param   trimHead    Trim leading whitespace?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1004
   * @param   trimTail    Trim trailing whitespace?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1005
   * @param   doublePunctuationSpaces    Use double spaces for punctuation?
7f561c08de6b Initial load
duke
parents:
diff changeset
  1006
   * @return              The trimmed string.
7f561c08de6b Initial load
duke
parents:
diff changeset
  1007
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1008
  public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail,
7f561c08de6b Initial load
duke
parents:
diff changeset
  1009
                                 boolean doublePunctuationSpaces)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1010
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1011
7f561c08de6b Initial load
duke
parents:
diff changeset
  1012
    // %OPT% !!!!!!!
7f561c08de6b Initial load
duke
parents:
diff changeset
  1013
    int len = this.length();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1014
    char[] buf = new char[len];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1015
7f561c08de6b Initial load
duke
parents:
diff changeset
  1016
    this.getChars(0, len, buf, 0);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1017
7f561c08de6b Initial load
duke
parents:
diff changeset
  1018
    boolean edit = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1019
    int s;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1020
7f561c08de6b Initial load
duke
parents:
diff changeset
  1021
    for (s = 0; s < len; s++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1022
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1023
      if (isSpace(buf[s]))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1024
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1025
        break;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1026
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1027
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1028
7f561c08de6b Initial load
duke
parents:
diff changeset
  1029
    /* replace S to ' '. and ' '+ -> single ' '. */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1030
    int d = s;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1031
    boolean pres = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1032
7f561c08de6b Initial load
duke
parents:
diff changeset
  1033
    for (; s < len; s++)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1034
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1035
      char c = buf[s];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1036
7f561c08de6b Initial load
duke
parents:
diff changeset
  1037
      if (isSpace(c))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1038
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1039
        if (!pres)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1040
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1041
          if (' ' != c)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1042
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1043
            edit = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1044
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1045
7f561c08de6b Initial load
duke
parents:
diff changeset
  1046
          buf[d++] = ' ';
7f561c08de6b Initial load
duke
parents:
diff changeset
  1047
7f561c08de6b Initial load
duke
parents:
diff changeset
  1048
          if (doublePunctuationSpaces && (s != 0))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1049
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1050
            char prevChar = buf[s - 1];
7f561c08de6b Initial load
duke
parents:
diff changeset
  1051
7f561c08de6b Initial load
duke
parents:
diff changeset
  1052
            if (!((prevChar == '.') || (prevChar == '!')
7f561c08de6b Initial load
duke
parents:
diff changeset
  1053
                  || (prevChar == '?')))
7f561c08de6b Initial load
duke
parents:
diff changeset
  1054
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1055
              pres = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1056
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1057
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1058
          else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1059
          {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1060
            pres = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1061
          }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1062
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1063
        else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1064
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1065
          edit = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1066
          pres = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1067
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1068
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1069
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
  1070
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1071
        buf[d++] = c;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1072
        pres = false;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1073
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1074
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1075
7f561c08de6b Initial load
duke
parents:
diff changeset
  1076
    if (trimTail && 1 <= d && ' ' == buf[d - 1])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1077
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1078
      edit = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1079
7f561c08de6b Initial load
duke
parents:
diff changeset
  1080
      d--;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1081
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1082
7f561c08de6b Initial load
duke
parents:
diff changeset
  1083
    int start = 0;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1084
7f561c08de6b Initial load
duke
parents:
diff changeset
  1085
    if (trimHead && 0 < d && ' ' == buf[0])
7f561c08de6b Initial load
duke
parents:
diff changeset
  1086
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1087
      edit = true;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1088
7f561c08de6b Initial load
duke
parents:
diff changeset
  1089
      start++;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1090
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1091
7f561c08de6b Initial load
duke
parents:
diff changeset
  1092
    XMLStringFactory xsf = XMLStringFactoryImpl.getFactory();
7f561c08de6b Initial load
duke
parents:
diff changeset
  1093
7f561c08de6b Initial load
duke
parents:
diff changeset
  1094
    return edit ? xsf.newstr(new String(buf, start, d - start)) : this;
7f561c08de6b Initial load
duke
parents:
diff changeset
  1095
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1096
7f561c08de6b Initial load
duke
parents:
diff changeset
  1097
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
  1098
   * @see com.sun.org.apache.xpath.internal.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1099
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
  1100
  public void callVisitors(ExpressionOwner owner, XPathVisitor visitor)
7f561c08de6b Initial load
duke
parents:
diff changeset
  1101
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
  1102
        visitor.visitStringLiteral(owner, this);
7f561c08de6b Initial load
duke
parents:
diff changeset
  1103
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
  1104
7f561c08de6b Initial load
duke
parents:
diff changeset
  1105
}