Package org.htmlunit.xpath.objects
Class XString
- java.lang.Object
-
- org.htmlunit.xpath.Expression
-
- org.htmlunit.xpath.objects.XObject
-
- org.htmlunit.xpath.objects.XString
-
- All Implemented Interfaces:
javax.xml.transform.SourceLocator
,ExpressionNode
,XPathVisitable
public class XString extends XObject
This class represents an XPath string object, and is capable of converting the string to other types, such as a number.
-
-
Field Summary
Fields Modifier and Type Field Description static XString
EMPTYSTRING
Empty string XString object-
Fields inherited from class org.htmlunit.xpath.objects.XObject
CLASS_BOOLEAN, CLASS_NODESET, CLASS_NULL, CLASS_NUMBER, CLASS_RTREEFRAG, CLASS_STRING, CLASS_UNKNOWN, m_obj
-
-
Constructor Summary
Constructors Constructor Description XString(java.lang.String val)
Construct a XNodeSet object.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
bool()
Cast result object to a boolean.void
callVisitors(XPathVisitor visitor)
This will traverse the hierarchy, calling the visitor for each member.char
charAt(int index)
Returns the character at the specified index.boolean
equals(java.lang.Object obj2)
boolean
equals(java.lang.String obj2)
Compares this string to the specifiedString
.boolean
equals(XObject obj2)
Tell if two objects are functionally equal.boolean
equals(XString obj2)
Compares this string to the specified object.XString
fixWhiteSpace(boolean trimHead, boolean trimTail, boolean doublePunctuationSpaces)
Conditionally trim all leading and trailing whitespace in the specified String.void
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.int
getType()
Tell what kind of class this is.java.lang.String
getTypeString()
Given a request type, return the equivalent string.boolean
hasString()
Tell if this object contains a java String object.int
indexOf(XString str)
Returns the index within this string of the first occurrence of the specified substring.private static boolean
isSpace(char ch)
Returns whether the specified ch conforms to the XML 1.0 definition of whitespace.int
length()
Returns the length of this string.double
num()
Cast result object to a number.boolean
startsWith(XString prefix)
Tests if this string starts with the specified prefix.boolean
startsWith(XString prefix, int toffset)
Tests if this string starts with the specified prefix beginning a specified index.java.lang.String
str()
Cast result object to a string.XString
substring(int beginIndex)
Returns a new string that is a substring of this string.XString
substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string.double
toDouble()
Convert a string to a double -- Allowed input is in fixed notation ddd.fff.XString
trim()
Removes white space from both ends of this string.XString
xstr()
Cast result object to a string.-
Methods inherited from class org.htmlunit.xpath.objects.XObject
boolWithSideEffects, deepEquals, detach, error, error, execute, greaterThan, greaterThanOrEqual, iter, lessThan, lessThanOrEqual, mutableNodeset, nodelist, nodeset, notEquals, numWithSideEffects, object, setObject, toString
-
Methods inherited from class org.htmlunit.xpath.Expression
asIterator, asNode, assertion, bool, canTraverseOutsideSubtree, error, execute, execute, execute, exprGetParent, exprSetParent, getColumnNumber, getExpressionOwner, getLineNumber, getPublicId, getSystemId, isSameClass, isStableNumber, num
-
-
-
-
Field Detail
-
EMPTYSTRING
public static final XString EMPTYSTRING
Empty string XString object
-
-
Method Detail
-
getType
public int getType()
Tell what kind of class this is.
-
getTypeString
public java.lang.String getTypeString()
Given a request type, return the equivalent string. For diagnostic purposes.- Overrides:
getTypeString
in classXObject
- Returns:
- type string "#UNKNOWN" + object class name
-
hasString
public boolean hasString()
Tell if this object contains a java String object.- Returns:
- true if this XMLString can return a string without creating one.
-
num
public double num()
Cast result object to a number. Always issues an error.
-
toDouble
public double toDouble()
Convert a string to a double -- Allowed input is in fixed notation ddd.fff.- Returns:
- A double value representation of the string, or return Double.NaN if the string can not be converted.
-
bool
public boolean bool()
Cast result object to a boolean. Always issues an error.
-
xstr
public XString xstr()
Cast result object to a string.
-
str
public java.lang.String str()
Cast result object to a string.
-
length
public int length()
Returns the length of this string.- Returns:
- the length of the sequence of characters represented by this object.
-
charAt
public char charAt(int index)
Returns the character at the specified index. An index ranges from0
tolength() - 1
. The first character of the sequence is at index0
, the next at index1
, and so on, as for array indexing.- Parameters:
index
- the index of the character.- Returns:
- the character at the specified index of this string. The first character is at index
0
. - Throws:
java.lang.IndexOutOfBoundsException
- if theindex
argument is negative or not less than the length of this string.
-
getChars
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copies characters from this string into the destination character array.- Parameters:
srcBegin
- index of the first character in the string to copy.srcEnd
- index after the last character in the string to copy.dst
- the destination array.dstBegin
- the start offset in the destination array.- Throws:
java.lang.IndexOutOfBoundsException
- If any of the following is true:srcBegin
is negative.srcBegin
is greater thansrcEnd
srcEnd
is greater than the length of this stringdstBegin
is negativedstBegin+(srcEnd-srcBegin)
is larger thandst.length
java.lang.NullPointerException
- ifdst
isnull
-
equals
public boolean equals(XObject obj2)
Tell if two objects are functionally equal.
-
equals
public boolean equals(java.lang.String obj2)
Compares this string to the specifiedString
. The result istrue
if and only if the argument is notnull
and is aString
object that represents the same sequence of characters as this object.- Parameters:
obj2
- the object to compare thisString
against.- Returns:
true
if theString
s are equal;false
otherwise.- See Also:
String.compareTo(java.lang.String)
,String.equalsIgnoreCase(java.lang.String)
-
equals
public boolean equals(XString obj2)
Compares this string to the specified object. The result istrue
if and only if the argument is notnull
and is aString
object that represents the same sequence of characters as this object.- Parameters:
obj2
- the object to compare thisString
against.- Returns:
true
if theString
are equal;false
otherwise.- See Also:
String.compareTo(java.lang.String)
,String.equalsIgnoreCase(java.lang.String)
-
equals
public boolean equals(java.lang.Object obj2)
- Overrides:
equals
in classjava.lang.Object
-
startsWith
public boolean startsWith(XString prefix, int toffset)
Tests if this string starts with the specified prefix beginning a specified index.- Parameters:
prefix
- the prefix.toffset
- where to begin looking in the string.- Returns:
true
if the character sequence represented by the argument is a prefix of the substring of this object starting at indextoffset
;false
otherwise. The result isfalse
iftoffset
is negative or greater than the length of thisString
object; otherwise the result is the same as the result of the expressionthis.subString(toffset).startsWith(prefix)
- Throws:
java.lang.NullPointerException
- ifprefix
isnull
.
-
startsWith
public boolean startsWith(XString prefix)
Tests if this string starts with the specified prefix.- Parameters:
prefix
- the prefix.- Returns:
true
if the character sequence represented by the argument is a prefix of the character sequence represented by this string;false
otherwise. Note also thattrue
will be returned if the argument is an empty string or is equal to thisString
object as determined by theequals(Object)
method.- Throws:
java.lang.NullPointerException
- ifprefix
isnull
.
-
indexOf
public int indexOf(XString str)
Returns the index within this string of the first occurrence of the specified substring. The integer returned is the smallest value k such that:this.startsWith(str, k)
true
.- Parameters:
str
- any string.- Returns:
- if the string argument occurs as a substring within this object, then the index of the
first character of the first such substring is returned; if it does not occur as a
substring,
-1
is returned. - Throws:
java.lang.NullPointerException
- ifstr
isnull
.
-
substring
public XString substring(int beginIndex)
Returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.Examples:
"unhappy".substring(2) returns "happy" "Harbison".substring(3) returns "bison" "emptiness".substring(9) returns "" (an empty string)
- Parameters:
beginIndex
- the beginning index, inclusive.- Returns:
- the specified substring.
- Throws:
java.lang.IndexOutOfBoundsException
- ifbeginIndex
is negative or larger than the length of thisString
object.
-
substring
public XString substring(int beginIndex, int endIndex)
Returns a new string that is a substring of this string. The substring begins at the specifiedbeginIndex
and extends to the character at indexendIndex - 1
. Thus the length of the substring isendIndex-beginIndex
.- Parameters:
beginIndex
- the beginning index, inclusive.endIndex
- the ending index, exclusive.- Returns:
- the specified substring.
- Throws:
java.lang.IndexOutOfBoundsException
- if thebeginIndex
is negative, orendIndex
is larger than the length of thisString
object, orbeginIndex
is larger thanendIndex
.
-
trim
public XString trim()
Removes white space from both ends of this string.- Returns:
- this string, with white space removed from the front and end.
-
isSpace
private static boolean isSpace(char ch)
Returns whether the specified ch conforms to the XML 1.0 definition of whitespace. Refer to the definition ofS
for details.- Parameters:
ch
- Character to check as XML whitespace.- Returns:
- =true if ch is XML whitespace; otherwise =false.
-
fixWhiteSpace
public XString fixWhiteSpace(boolean trimHead, boolean trimTail, boolean doublePunctuationSpaces)
Conditionally trim all leading and trailing whitespace in the specified String. All strings of white space are replaced by a single space character (#x20), except spaces after punctuation which receive double spaces if doublePunctuationSpaces is true. This function may be useful to a formatter, but to get first class results, the formatter should probably do it's own white space handling based on the semantics of the formatting object.- Parameters:
trimHead
- Trim leading whitespace?trimTail
- Trim trailing whitespace?doublePunctuationSpaces
- Use double spaces for punctuation?- Returns:
- The trimmed string.
-
callVisitors
public void callVisitors(XPathVisitor visitor)
This will traverse the hierarchy, calling the visitor for each member. If the called visitor method returns false, the subtree should not be called.- Specified by:
callVisitors
in interfaceXPathVisitable
- Overrides:
callVisitors
in classXObject
- Parameters:
visitor
- The visitor whose appropriate method will be called.
-
-