19f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 29f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Licensed to the Apache Software Foundation (ASF) under one 39f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or more contributor license agreements. See the NOTICE file 49f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed with this work for additional information 59f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * regarding copyright ownership. The ASF licenses this file 69f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to you under the Apache License, Version 2.0 (the "License"); 79f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * you may not use this file except in compliance with the License. 89f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * You may obtain a copy of the License at 99f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Unless required by applicable law or agreed to in writing, software 139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * See the License for the specific language governing permissions and 169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * limitations under the License. 179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/* 199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * $Id: XString.java 570108 2007-08-27 13:30:57Z zongaro $ 209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpackage org.apache.xpath.objects; 229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport java.util.Locale; 249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.dtm.DTM; 269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.utils.XMLCharacterRecognizer; 279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.utils.XMLString; 289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xml.utils.XMLStringFactory; 299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.ExpressionOwner; 309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathContext; 319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonimport org.apache.xpath.XPathVisitor; 329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson/** 349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This class represents an XPath string object, and is capable of 359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * converting the string to other types, such as a number. 369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @xsl.usage general 379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilsonpublic class XString extends XObject implements XMLString 399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson{ 409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson static final long serialVersionUID = 2020470518395094525L; 419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** Empty string XString object */ 439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public static final XString EMPTYSTRING = new XString(""); 449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Construct a XString object. This constructor exists for derived classes. 479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param val String object this will wrap. 499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson protected XString(Object val) 519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson super(val); 539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Construct a XNodeSet object. 579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param val String object this will wrap. 599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XString(String val) 619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson super(val); 639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tell that this is a CLASS_STRING. 679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return type CLASS_STRING 699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int getType() 719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return CLASS_STRING; 739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Given a request type, return the equivalent string. 779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * For diagnostic purposes. 789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return type string "#STRING" 809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public String getTypeString() 829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return "#STRING"; 849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tell if this object contains a java String object. 889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return true if this XMLString can return a string without creating one. 909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean hasString() 929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return true; 949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Cast result object to a number. 989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return 0.0 if this string is null, numeric value of this string 1009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * or NaN 1019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public double num() 1039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return toDouble(); 1059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Convert a string to a double -- Allowed input is in fixed 1099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * notation ddd.fff. 1109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return A double value representation of the string, or return Double.NaN 1129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * if the string can not be converted. 1139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public double toDouble() 1159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /* XMLCharacterRecognizer.isWhiteSpace(char c) methods treats the following 1179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * characters as white space characters. 1189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * ht - horizontal tab, nl - newline , cr - carriage return and sp - space 1199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * trim() methods by default also takes care of these white space characters 1209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * So trim() method is used to remove leading and trailing white spaces. 1219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson XMLString s = trim(); 1239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson double result = Double.NaN; 1249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (int i = 0; i < s.length(); i++) 1259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char c = s.charAt(i); 1279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (c != '-' && c != '.' && ( c < 0X30 || c > 0x39)) { 1289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // The character is not a '-' or a '.' or a digit 1299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // then return NaN because something is wrong. 1309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return result; 1319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 1349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson result = Double.parseDouble(s.toString()); 1369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } catch (NumberFormatException e){} 1379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return result; 1399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 1409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Cast result object to a boolean. 1439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return True if the length of this string object is greater 1459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * than 0. 1469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean bool() 1489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().length() > 0; 1509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Cast result object to a string. 1549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return The string this wraps or the empty string if null 1569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString xstr() 1589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return this; 1609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Cast result object to a string. 1649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return The string this wraps or the empty string if null 1669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public String str() 1689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return (null != m_obj) ? ((String) m_obj) : ""; 1709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Cast result object to a result tree fragment. 1749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param support Xpath context to use for the conversion 1769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return A document fragment with this string as a child node 1789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 1799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int rtf(XPathContext support) 1809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 1819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson DTM frag = support.createDocumentFragment(); 1839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson frag.appendTextChild(str()); 1859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return frag.getDocument(); 1879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 1889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 1899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 1909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Directly call the 1919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * characters method on the passed ContentHandler for the 1929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string-value. Multiple calls to the 1939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * ContentHandler's characters methods may well occur for a single call to 1949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this method. 1959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch A non-null reference to a ContentHandler. 1979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 1989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws org.xml.sax.SAXException 1999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void dispatchCharactersEvents(org.xml.sax.ContentHandler ch) 2019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws org.xml.sax.SAXException 2029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String str = str(); 2059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson ch.characters(str.toCharArray(), 0, str.length()); 2079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Directly call the 2119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * comment method on the passed LexicalHandler for the 2129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string-value. 2139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param lh A non-null reference to a LexicalHandler. 2159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws org.xml.sax.SAXException 2179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void dispatchAsComment(org.xml.sax.ext.LexicalHandler lh) 2199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throws org.xml.sax.SAXException 2209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson String str = str(); 2239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson lh.comment(str.toCharArray(), 0, str.length()); 2259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the length of this string. 2299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the length of the sequence of characters represented by this 2319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object. 2329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int length() 2349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().length(); 2369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the character at the specified index. An index ranges 2409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * from <code>0</code> to <code>length() - 1</code>. The first character 2419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of the sequence is at index <code>0</code>, the next at index 2429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>1</code>, and so on, as for array indexing. 2439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param index the index of the character. 2459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the character at the specified index of this string. 2469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The first character is at index <code>0</code>. 2479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception IndexOutOfBoundsException if the <code>index</code> 2489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is negative or not less than the length of this 2499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string. 2509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public char charAt(int index) 2529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().charAt(index); 2549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Copies characters from this string into the destination character 2589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * array. 2599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param srcBegin index of the first character in the string 2619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to copy. 2629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param srcEnd index after the last character in the string 2639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * to copy. 2649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param dst the destination array. 2659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param dstBegin the start offset in the destination array. 2669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception IndexOutOfBoundsException If any of the following 2679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is true: 2689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <ul><li><code>srcBegin</code> is negative. 2699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li><code>srcBegin</code> is greater than <code>srcEnd</code> 2709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li><code>srcEnd</code> is greater than the length of this 2719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string 2729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li><code>dstBegin</code> is negative 2739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <li><code>dstBegin+(srcEnd-srcBegin)</code> is larger than 2749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>dst.length</code></ul> 2759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception NullPointerException if <code>dst</code> is <code>null</code> 2769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) 2789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson str().getChars(srcBegin, srcEnd, dst, dstBegin); 2809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 2819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 2839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tell if two objects are functionally equal. 2849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param obj2 Object to compare this to 2869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return true if the two objects are equal 2889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 2899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @throws javax.xml.transform.TransformerException 2909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 2919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean equals(XObject obj2) 2929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 2939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 2949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // In order to handle the 'all' semantics of 2959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // nodeset comparisons, we always call the 2969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // nodeset function. 2979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int t = obj2.getType(); 2989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson try 2999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (XObject.CLASS_NODESET == t) 3019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.equals(this); 3029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // If at least one object to be compared is a boolean, then each object 3039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // to be compared is converted to a boolean as if by applying the 3049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // boolean function. 3059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if(XObject.CLASS_BOOLEAN == t) 3069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.bool() == bool(); 3079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Otherwise, if at least one object to be compared is a number, then each object 3089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // to be compared is converted to a number as if by applying the number function. 3099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if(XObject.CLASS_NUMBER == t) 3109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.num() == num(); 3119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson catch(javax.xml.transform.TransformerException te) 3139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new org.apache.xml.utils.WrappedRuntimeException(te); 3159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Otherwise, both objects to be compared are converted to strings as 3189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // if by applying the string function. 3199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return xstr().equals(obj2.xstr()); 3209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 3239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares this string to the specified <code>String</code>. 3249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The result is <code>true</code> if and only if the argument is not 3259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code> and is a <code>String</code> object that represents 3269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the same sequence of characters as this object. 3279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param obj2 the object to compare this <code>String</code> against. 3299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the <code>String</code>s are equal; 3309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>false</code> otherwise. 3319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#compareTo(java.lang.String) 3329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#equalsIgnoreCase(java.lang.String) 3339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 3349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean equals(String obj2) { 3359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().equals(obj2); 3369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 3399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares this string to the specified object. 3409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The result is <code>true</code> if and only if the argument is not 3419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code> and is a <code>String</code> object that represents 3429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the same sequence of characters as this object. 3439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param obj2 the object to compare this <code>String</code> 3459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * against. 3469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the <code>String </code>are equal; 3479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>false</code> otherwise. 3489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#compareTo(java.lang.String) 3499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#equalsIgnoreCase(java.lang.String) 3509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 3519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean equals(XMLString obj2) 3529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (obj2 != null) { 3549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (!obj2.hasString()) { 3559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.equals(str()); 3569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } else { 3579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().equals(obj2.toString()); 3589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return false; 3619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 3649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares this string to the specified object. 3659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The result is <code>true</code> if and only if the argument is not 3669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code> and is a <code>String</code> object that represents 3679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the same sequence of characters as this object. 3689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param obj2 the object to compare this <code>String</code> 3709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * against. 3719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the <code>String </code>are equal; 3729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>false</code> otherwise. 3739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#compareTo(java.lang.String) 3749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#equalsIgnoreCase(java.lang.String) 3759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 3769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean equals(Object obj2) 3779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 3789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (null == obj2) 3809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return false; 3819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // In order to handle the 'all' semantics of 3839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // nodeset comparisons, we always call the 3849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // nodeset function. 3859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if (obj2 instanceof XNodeSet) 3869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.equals(this); 3879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else if(obj2 instanceof XNumber) 3889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return obj2.equals(this); 3899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 3909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().equals(obj2.toString()); 3919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 3929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 3939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 3949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares this <code>String</code> to another <code>String</code>, 3959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * ignoring case considerations. Two strings are considered equal 3969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * ignoring case if they are of the same length, and corresponding 3979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * characters in the two strings are equal ignoring case. 3989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 3999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param anotherString the <code>String</code> to compare this 4009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> against. 4019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the argument is not <code>null</code> 4029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * and the <code>String</code>s are equal, 4039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * ignoring case; <code>false</code> otherwise. 4049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see #equals(Object) 4059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toLowerCase(char) 4069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toUpperCase(char) 4079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 4089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean equalsIgnoreCase(String anotherString) 4099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 4109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().equalsIgnoreCase(anotherString); 4119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 4149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares two strings lexicographically. 4159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 4169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param xstr the <code>String</code> to be compared. 4179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 4189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the value <code>0</code> if the argument string is equal to 4199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this string; a value less than <code>0</code> if this string 4209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is lexicographically less than the string argument; and a 4219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * value greater than <code>0</code> if this string is 4229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * lexicographically greater than the string argument. 4239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>anotherString</code> 4249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is <code>null</code>. 4259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 4269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int compareTo(XMLString xstr) 4279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 4289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int len1 = this.length(); 4309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int len2 = xstr.length(); 4319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int n = Math.min(len1, len2); 4329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int i = 0; 4339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int j = 0; 4349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (n-- != 0) 4369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 4379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char c1 = this.charAt(i); 4389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char c2 = xstr.charAt(j); 4399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (c1 != c2) 4419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 4429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return c1 - c2; 4439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson i++; 4469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson j++; 4479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return len1 - len2; 4509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 4539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Compares two strings lexicographically, ignoring case considerations. 4549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This method returns an integer whose sign is that of 4559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>this.toUpperCase().toLowerCase().compareTo( 4569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * str.toUpperCase().toLowerCase())</code>. 4579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 4589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Note that this method does <em>not</em> take locale into account, 4599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * and will result in an unsatisfactory ordering for certain locales. 4609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The java.text package provides <em>collators</em> to allow 4619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * locale-sensitive ordering. 4629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 4639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str the <code>String</code> to be compared. 4649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return a negative integer, zero, or a positive integer as the 4659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the specified String is greater than, equal to, or less 4669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * than this String, ignoring case considerations. 4679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.text.Collator#compare(String, String) 4689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @since 1.2 4699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 4709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int compareToIgnoreCase(XMLString str) 4719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 4729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %REVIEW% Like it says, @since 1.2. Doesn't exist in earlier 4739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // versions of Java, hence we can't yet shell out to it. We can implement 4749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // it as character-by-character compare, but doing so efficiently 4759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // is likely to be (ahem) interesting. 4769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // 4779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // However, since nobody is actually _using_ this method yet: 4789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // return str().compareToIgnoreCase(str.toString()); 4799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson throw new org.apache.xml.utils.WrappedRuntimeException( 4819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson new java.lang.NoSuchMethodException( 4829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson "Java 1.2 method, not yet implemented")); 4839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 4849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 4859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 4869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tests if this string starts with the specified prefix beginning 4879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * a specified index. 4889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 4899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param prefix the prefix. 4909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param toffset where to begin looking in the string. 4919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the character sequence represented by the 4929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is a prefix of the substring of this object starting 4939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * at index <code>toffset</code>; <code>false</code> otherwise. 4949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The result is <code>false</code> if <code>toffset</code> is 4959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * negative or greater than the length of this 4969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> object; otherwise the result is the same 4979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as the result of the expression 4989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <pre> 4999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.subString(toffset).startsWith(prefix) 5009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre> 5019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>prefix</code> is 5029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 5039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 5049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean startsWith(String prefix, int toffset) 5059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().startsWith(prefix, toffset); 5079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 5109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tests if this string starts with the specified prefix. 5119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 5129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param prefix the prefix. 5139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the character sequence represented by the 5149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is a prefix of the character sequence represented by 5159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this string; <code>false</code> otherwise. 5169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Note also that <code>true</code> will be returned if the 5179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is an empty string or is equal to this 5189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> object as determined by the 5199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * {@link #equals(Object)} method. 5209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>prefix</code> is 5219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 5229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 5239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean startsWith(String prefix) 5249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return startsWith(prefix, 0); 5269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 5299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tests if this string starts with the specified prefix beginning 5309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * a specified index. 5319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 5329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param prefix the prefix. 5339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param toffset where to begin looking in the string. 5349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the character sequence represented by the 5359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is a prefix of the substring of this object starting 5369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * at index <code>toffset</code>; <code>false</code> otherwise. 5379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The result is <code>false</code> if <code>toffset</code> is 5389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * negative or greater than the length of this 5399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> object; otherwise the result is the same 5409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as the result of the expression 5419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <pre> 5429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.subString(toffset).startsWith(prefix) 5439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre> 5449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>prefix</code> is 5459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 5469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 5479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean startsWith(XMLString prefix, int toffset) 5489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int to = toffset; 5519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int tlim = this.length(); 5529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int po = 0; 5539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int pc = prefix.length(); 5549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // Note: toffset might be near -1>>>1. 5569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if ((toffset < 0) || (toffset > tlim - pc)) 5579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return false; 5599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson while (--pc >= 0) 5629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (this.charAt(to) != prefix.charAt(po)) 5649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return false; 5669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson to++; 5699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson po++; 5709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return true; 5739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 5769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tests if this string starts with the specified prefix. 5779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 5789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param prefix the prefix. 5799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the character sequence represented by the 5809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is a prefix of the character sequence represented by 5819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this string; <code>false</code> otherwise. 5829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Note also that <code>true</code> will be returned if the 5839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is an empty string or is equal to this 5849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> object as determined by the 5859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * {@link #equals(Object)} method. 5869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>prefix</code> is 5879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 5889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 5899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean startsWith(XMLString prefix) 5909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 5919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return startsWith(prefix, 0); 5929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 5939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 5949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 5959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Tests if this string ends with the specified suffix. 5969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 5979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param suffix the suffix. 5989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return <code>true</code> if the character sequence represented by the 5999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * argument is a suffix of the character sequence represented by 6009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this object; <code>false</code> otherwise. Note that the 6019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * result will be <code>true</code> if the argument is the 6029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * empty string or is equal to this <code>String</code> object 6039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as determined by the {@link #equals(Object)} method. 6049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>suffix</code> is 6059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 6069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 6079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public boolean endsWith(String suffix) 6089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 6099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().endsWith(suffix); 6109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 6119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 6129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 6139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns a hashcode for this string. The hashcode for a 6149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>String</code> object is computed as 6159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 6169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] 6179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 6189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * using <code>int</code> arithmetic, where <code>s[i]</code> is the 6199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <i>i</i>th character of the string, <code>n</code> is the length of 6209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the string, and <code>^</code> indicates exponentiation. 6219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (The hash value of the empty string is zero.) 6229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 6239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return a hash code value for this object. 6249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 6259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int hashCode() 6269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 6279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().hashCode(); 6289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 6299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 6309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 6319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the first occurrence of the 6329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified character. If a character with value <code>ch</code> occurs 6339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * in the character sequence represented by this <code>String</code> 6349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object, then the index of the first such occurrence is returned -- 6359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * that is, the smallest value <i>k</i> such that: 6369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 6379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.charAt(<i>k</i>) == ch 6389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 6399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is <code>true</code>. If no such character occurs in this string, 6409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * then <code>-1</code> is returned. 6419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 6429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch a character. 6439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the index of the first occurrence of the character in the 6449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * character sequence represented by this object, or 6459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>-1</code> if the character does not occur. 6469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 6479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int indexOf(int ch) 6489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 6499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().indexOf(ch); 6509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 6519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 6529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 6539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the first occurrence of the 6549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified character, starting the search at the specified index. 6559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 6569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * If a character with value <code>ch</code> occurs in the character 6579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * sequence represented by this <code>String</code> object at an index 6589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * no smaller than <code>fromIndex</code>, then the index of the first 6599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * such occurrence is returned--that is, the smallest value <i>k</i> 6609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * such that: 6619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 6629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * (this.charAt(<i>k</i>) == ch) && (<i>k</i> >= fromIndex) 6639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 6649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is true. If no such character occurs in this string at or after 6659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * position <code>fromIndex</code>, then <code>-1</code> is returned. 6669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 6679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * There is no restriction on the value of <code>fromIndex</code>. If it 6689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is negative, it has the same effect as if it were zero: this entire 6699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string may be searched. If it is greater than the length of this 6709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string, it has the same effect as if it were equal to the length of 6719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this string: <code>-1</code> is returned. 6729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 6739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch a character. 6749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param fromIndex the index to start the search from. 6759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the index of the first occurrence of the character in the 6769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * character sequence represented by this object that is greater 6779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * than or equal to <code>fromIndex</code>, or <code>-1</code> 6789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * if the character does not occur. 6799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 6809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int indexOf(int ch, int fromIndex) 6819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 6829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().indexOf(ch, fromIndex); 6839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 6849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 6859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 6869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the last occurrence of the 6879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified character. That is, the index returned is the largest 6889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * value <i>k</i> such that: 6899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 6909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.charAt(<i>k</i>) == ch 6919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 6929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is true. 6939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The String is searched backwards starting at the last character. 6949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 6959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch a character. 6969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the index of the last occurrence of the character in the 6979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * character sequence represented by this object, or 6989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>-1</code> if the character does not occur. 6999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 7009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int lastIndexOf(int ch) 7019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 7029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().lastIndexOf(ch); 7039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 7049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 7059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 7069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the last occurrence of the 7079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified character, searching backward starting at the specified 7089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * index. That is, the index returned is the largest value <i>k</i> 7099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * such that: 7109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 7119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.charAt(k) == ch) && (k <= fromIndex) 7129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 7139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is true. 7149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 7159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch a character. 7169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param fromIndex the index to start the search from. There is no 7179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * restriction on the value of <code>fromIndex</code>. If it is 7189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * greater than or equal to the length of this string, it has 7199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the same effect as if it were equal to one less than the 7209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * length of this string: this entire string may be searched. 7219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * If it is negative, it has the same effect as if it were -1: 7229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * -1 is returned. 7239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the index of the last occurrence of the character in the 7249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * character sequence represented by this object that is less 7259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * than or equal to <code>fromIndex</code>, or <code>-1</code> 7269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * if the character does not occur before that point. 7279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 7289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int lastIndexOf(int ch, int fromIndex) 7299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 7309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().lastIndexOf(ch, fromIndex); 7319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 7329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 7339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 7349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the first occurrence of the 7359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified substring. The integer returned is the smallest value 7369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <i>k</i> such that: 7379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 7389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.startsWith(str, <i>k</i>) 7399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 7409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is <code>true</code>. 7419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 7429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str any string. 7439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return if the string argument occurs as a substring within this 7449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object, then the index of the first character of the first 7459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * such substring is returned; if it does not occur as a 7469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * substring, <code>-1</code> is returned. 7479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 7489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 7499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 7509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int indexOf(String str) 7519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 7529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().indexOf(str); 7539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 7549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 7559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 7569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the first occurrence of the 7579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified substring. The integer returned is the smallest value 7589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <i>k</i> such that: 7599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 7609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.startsWith(str, <i>k</i>) 7619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 7629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is <code>true</code>. 7639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 7649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str any string. 7659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return if the string argument occurs as a substring within this 7669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object, then the index of the first character of the first 7679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * such substring is returned; if it does not occur as a 7689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * substring, <code>-1</code> is returned. 7699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 7709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 7719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 7729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int indexOf(XMLString str) 7739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 7749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().indexOf(str.toString()); 7759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 7769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 7779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 7789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the first occurrence of the 7799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * specified substring, starting at the specified index. The integer 7809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * returned is the smallest value <i>k</i> such that: 7819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 7829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.startsWith(str, <i>k</i>) && (<i>k</i> >= fromIndex) 7839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 7849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is <code>true</code>. 7859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 7869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * There is no restriction on the value of <code>fromIndex</code>. If 7879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * it is negative, it has the same effect as if it were zero: this entire 7889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string may be searched. If it is greater than the length of this 7899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * string, it has the same effect as if it were equal to the length of 7909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this string: <code>-1</code> is returned. 7919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 7929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str the substring to search for. 7939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param fromIndex the index to start the search from. 7949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return If the string argument occurs as a substring within this 7959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * object at a starting index no smaller than 7969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>fromIndex</code>, then the index of the first character 7979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of the first such substring is returned. If it does not occur 7989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as a substring starting at <code>fromIndex</code> or beyond, 7999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>-1</code> is returned. 8009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 8019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code> 8029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 8039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int indexOf(String str, int fromIndex) 8049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 8059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().indexOf(str, fromIndex); 8069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 8079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 8089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 8099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the rightmost occurrence 8109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of the specified substring. The rightmost empty string "" is 8119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * considered to occur at the index value <code>this.length()</code>. 8129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * The returned index is the largest value <i>k</i> such that 8139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 8149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this.startsWith(str, k) 8159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 8169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * is true. 8179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 8189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str the substring to search for. 8199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return if the string argument occurs one or more times as a substring 8209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * within this object, then the index of the first character of 8219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the last such substring is returned. If it does not occur as 8229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * a substring, <code>-1</code> is returned. 8239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 8249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 8259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 8269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int lastIndexOf(String str) 8279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 8289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().lastIndexOf(str); 8299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 8309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 8319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 8329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns the index within this string of the last occurrence of 8339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the specified substring. 8349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 8359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str the substring to search for. 8369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param fromIndex the index to start the search from. There is no 8379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * restriction on the value of fromIndex. If it is greater than 8389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the length of this string, it has the same effect as if it 8399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * were equal to the length of this string: this entire string 8409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * may be searched. If it is negative, it has the same effect 8419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * as if it were -1: -1 is returned. 8429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return If the string argument occurs one or more times as a substring 8439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * within this object at a starting index no greater than 8449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>fromIndex</code>, then the index of the first character of 8459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the last such substring is returned. If it does not occur as a 8469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * substring starting at <code>fromIndex</code> or earlier, 8479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>-1</code> is returned. 8489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 8499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 8509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 8519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public int lastIndexOf(String str, int fromIndex) 8529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 8539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return str().lastIndexOf(str, fromIndex); 8549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 8559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 8569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 8579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns a new string that is a substring of this string. The 8589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * substring begins with the character at the specified index and 8599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * extends to the end of this string. <p> 8609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Examples: 8619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 8629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * "unhappy".substring(2) returns "happy" 8639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * "Harbison".substring(3) returns "bison" 8649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * "emptiness".substring(9) returns "" (an empty string) 8659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 8669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 8679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param beginIndex the beginning index, inclusive. 8689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the specified substring. 8699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception IndexOutOfBoundsException if 8709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>beginIndex</code> is negative or larger than the 8719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * length of this <code>String</code> object. 8729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 8739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString substring(int beginIndex) 8749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 8759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().substring(beginIndex)); 8769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 8779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 8789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 8799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns a new string that is a substring of this string. The 8809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * substring begins at the specified <code>beginIndex</code> and 8819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * extends to the character at index <code>endIndex - 1</code>. 8829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Thus the length of the substring is <code>endIndex-beginIndex</code>. 8839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 8849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param beginIndex the beginning index, inclusive. 8859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param endIndex the ending index, exclusive. 8869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the specified substring. 8879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception IndexOutOfBoundsException if the 8889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>beginIndex</code> is negative, or 8899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>endIndex</code> is larger than the length of 8909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this <code>String</code> object, or 8919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>beginIndex</code> is larger than 8929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>endIndex</code>. 8939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 8949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString substring(int beginIndex, int endIndex) 8959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 8969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().substring(beginIndex, endIndex)); 8979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 8989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 8999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Concatenates the specified string to the end of this string. 9019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param str the <code>String</code> that is concatenated to the end 9039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of this <code>String</code>. 9049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return a string that represents the concatenation of this object's 9059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * characters followed by the string argument's characters. 9069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @exception java.lang.NullPointerException if <code>str</code> is 9079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>null</code>. 9089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString concat(String str) 9109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %REVIEW% Make an FSB here? 9139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().concat(str)); 9149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Converts all of the characters in this <code>String</code> to lower 9189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * case using the rules of the given <code>Locale</code>. 9199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param locale use the case transformation rules for this locale 9219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the String, converted to lowercase. 9229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toLowerCase(char) 9239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#toUpperCase(Locale) 9249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString toLowerCase(Locale locale) 9269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().toLowerCase(locale)); 9289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Converts all of the characters in this <code>String</code> to lower 9329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * case using the rules of the default locale, which is returned 9339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * by <code>Locale.getDefault</code>. 9349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 9359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the string, converted to lowercase. 9379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toLowerCase(char) 9389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#toLowerCase(Locale) 9399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString toLowerCase() 9419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().toLowerCase()); 9439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Converts all of the characters in this <code>String</code> to upper 9479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * case using the rules of the given locale. 9489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param locale use the case transformation rules for this locale 9499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the String, converted to uppercase. 9509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toUpperCase(char) 9519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#toLowerCase(Locale) 9529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString toUpperCase(Locale locale) 9549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().toUpperCase(locale)); 9569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Converts all of the characters in this <code>String</code> to upper 9609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * case using the rules of the default locale, which is returned 9619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * by <code>Locale.getDefault</code>. 9629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 9649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * If no character in this string has a different uppercase version, 9659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * based on calling the <code>toUpperCase</code> method defined by 9669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>Character</code>, then the original string is returned. 9679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <p> 9689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Otherwise, this method creates a new <code>String</code> object 9699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * representing a character sequence identical in length to the 9709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * character sequence represented by this <code>String</code> object and 9719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * with every character equal to the result of applying the method 9729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <code>Character.toUpperCase</code> to the corresponding character of 9739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * this <code>String</code> object. <p> 9749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Examples: 9759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * <blockquote><pre> 9769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * "Fahrvergnügen".toUpperCase() returns "FAHRVERGNÜGEN" 9779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * "Visit Ljubinje!".toUpperCase() returns "VISIT LJUBINJE!" 9789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * </pre></blockquote> 9799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return the string, converted to uppercase. 9819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.Character#toUpperCase(char) 9829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see java.lang.String#toUpperCase(Locale) 9839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString toUpperCase() 9859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().toUpperCase()); 9879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 9909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Removes white space from both ends of this string. 9919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 9929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return this string, with white space removed from the front and end. 9939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 9949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString trim() 9959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 9969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return new XString(str().trim()); 9979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 9989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 9999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 10009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition 10019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * of whitespace. Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S"> 10029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * the definition of <CODE>S</CODE></A> for details. 10039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param ch Character to check as XML whitespace. 10049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return =true if <var>ch</var> is XML whitespace; otherwise =false. 10059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 10069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson private static boolean isSpace(char ch) 10079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return XMLCharacterRecognizer.isWhiteSpace(ch); // Take the easy way out for now. 10099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 10129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * Conditionally trim all leading and trailing whitespace in the specified String. 10139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * All strings of white space are 10149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * replaced by a single space character (#x20), except spaces after punctuation which 10159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * receive double spaces if doublePunctuationSpaces is true. 10169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * This function may be useful to a formatter, but to get first class 10179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * results, the formatter should probably do it's own white space handling 10189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * based on the semantics of the formatting object. 10199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * 10209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param trimHead Trim leading whitespace? 10219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param trimTail Trim trailing whitespace? 10229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @param doublePunctuationSpaces Use double spaces for punctuation? 10239f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @return The trimmed string. 10249f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 10259f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public XMLString fixWhiteSpace(boolean trimHead, boolean trimTail, 10269f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson boolean doublePunctuationSpaces) 10279f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10289f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10299f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson // %OPT% !!!!!!! 10309f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int len = this.length(); 10319f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char[] buf = new char[len]; 10329f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10339f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson this.getChars(0, len, buf, 0); 10349f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10359f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson boolean edit = false; 10369f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int s; 10379f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10389f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (s = 0; s < len; s++) 10399f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10409f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (isSpace(buf[s])) 10419f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10429f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson break; 10439f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10449f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10459f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10469f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /* replace S to ' '. and ' '+ -> single ' '. */ 10479f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int d = s; 10489f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson boolean pres = false; 10499f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10509f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson for (; s < len; s++) 10519f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10529f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char c = buf[s]; 10539f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10549f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (isSpace(c)) 10559f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10569f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (!pres) 10579f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10589f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (' ' != c) 10599f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10609f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson edit = true; 10619f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10629f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10639f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson buf[d++] = ' '; 10649f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10659f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (doublePunctuationSpaces && (s != 0)) 10669f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10679f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson char prevChar = buf[s - 1]; 10689f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10699f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (!((prevChar == '.') || (prevChar == '!') 10709f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson || (prevChar == '?'))) 10719f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10729f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pres = true; 10739f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10749f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10759f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 10769f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10779f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pres = true; 10789f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10799f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10809f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 10819f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10829f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson edit = true; 10839f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pres = true; 10849f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10859f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10869f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson else 10879f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10889f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson buf[d++] = c; 10899f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson pres = false; 10909f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10919f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10929f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10939f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (trimTail && 1 <= d && ' ' == buf[d - 1]) 10949f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 10959f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson edit = true; 10969f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 10979f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson d--; 10989f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 10999f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11009f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson int start = 0; 11019f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11029f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson if (trimHead && 0 < d && ' ' == buf[0]) 11039f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 11049f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson edit = true; 11059f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11069f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson start++; 11079f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 11089f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11099f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson XMLStringFactory xsf = XMLStringFactoryImpl.getFactory(); 11109f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11119f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson return edit ? xsf.newstr(new String(buf, start, d - start)) : this; 11129f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 11139f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11149f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson /** 11159f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson * @see org.apache.xpath.XPathVisitable#callVisitors(ExpressionOwner, XPathVisitor) 11169f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson */ 11179f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson public void callVisitors(ExpressionOwner owner, XPathVisitor visitor) 11189f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson { 11199f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson visitor.visitStringLiteral(owner, this); 11209f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson } 11219f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson 11229f8118474e9513f7a5b7d2a05e4a0fb15d1a6569Jesse Wilson} 1123