Class 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 Detail

      • EMPTYSTRING

        public static final XString EMPTYSTRING
        Empty string XString object
    • Constructor Detail

      • XString

        public XString​(java.lang.String val)
        Construct a XNodeSet object.
        Parameters:
        val - String object this will wrap.
    • Method Detail

      • getType

        public int getType()
        Tell what kind of class this is.
        Overrides:
        getType in class XObject
        Returns:
        CLASS_UNKNOWN
      • getTypeString

        public java.lang.String getTypeString()
        Given a request type, return the equivalent string. For diagnostic purposes.
        Overrides:
        getTypeString in class XObject
        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.
        Overrides:
        num in class XObject
        Returns:
        0.0
      • 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.
        Overrides:
        bool in class XObject
        Returns:
        false
      • xstr

        public XString xstr()
        Cast result object to a string.
        Overrides:
        xstr in class XObject
        Returns:
        The string this wraps or the empty string if null
      • str

        public java.lang.String str()
        Cast result object to a string.
        Overrides:
        str in class XObject
        Returns:
        The object as 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 from 0 to length() - 1. The first character of the sequence is at index 0, the next at index 1, 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 the index 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 than srcEnd
        • srcEnd is greater than the length of this string
        • dstBegin is negative
        • dstBegin+(srcEnd-srcBegin) is larger than dst.length
        java.lang.NullPointerException - if dst is null
      • equals

        public boolean equals​(XObject obj2)
        Tell if two objects are functionally equal.
        Overrides:
        equals in class XObject
        Parameters:
        obj2 - Object to compare this to
        Returns:
        True if this object is equal to the given object
      • equals

        public boolean equals​(java.lang.String obj2)
        Compares this string to the specified String. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
        Parameters:
        obj2 - the object to compare this String against.
        Returns:
        true if the Strings 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 is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
        Parameters:
        obj2 - the object to compare this String against.
        Returns:
        true if the String 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 class java.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 index toffset; false otherwise. The result is false if toffset is negative or greater than the length of this String object; otherwise the result is the same as the result of the expression
                 this.subString(toffset).startsWith(prefix)
                 
        Throws:
        java.lang.NullPointerException - if prefix is null.
      • 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 that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.
        Throws:
        java.lang.NullPointerException - if prefix is null.
      • 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)
         
        is 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 - if str is null.
      • 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 - if beginIndex is negative or larger than the length of this String 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 specified beginIndex and extends to the character at index endIndex - 1 . Thus the length of the substring is endIndex-beginIndex.
        Parameters:
        beginIndex - the beginning index, inclusive.
        endIndex - the ending index, exclusive.
        Returns:
        the specified substring.
        Throws:
        java.lang.IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
      • 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 of S 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 interface XPathVisitable
        Overrides:
        callVisitors in class XObject
        Parameters:
        visitor - The visitor whose appropriate method will be called.