Class Whitespace


  • public class Whitespace
    extends java.lang.Object
    This class provides helper methods and constants for handling whitespace
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Whitespace.Tokenizer
      An iterator that splits a string on whitespace boundaries, corresponding to the XPath 3.1 function tokenize#1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int ALL  
      static int COLLAPSE  
      static int IGNORABLE  
      static int NONE
      The values NONE, IGNORABLE, and ALL identify which kinds of whitespace text node should be stripped when building a source tree.
      static int PRESERVE
      The values PRESERVE, REPLACE, and COLLAPSE represent the three options for whitespace normalization.
      static int REPLACE  
      static int TRIM  
      static int UNSPECIFIED  
      static int XSLT  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.CharSequence applyWhitespaceNormalization​(int action, java.lang.CharSequence value)
      Apply schema-defined whitespace normalization to a string
      static java.lang.CharSequence collapseWhitespace​(java.lang.CharSequence in)
      Collapse whitespace as defined in XML Schema.
      static boolean containsWhitespace​(java.lang.CharSequence value)
      Determine if a string contains any whitespace
      static boolean isWhite​(char c)
      Determine if a character is whitespace
      static boolean isWhite​(java.lang.CharSequence content)
      Determine if a string is all-whitespace
      static boolean isWhitespace​(int ch)
      Test whether a character is whitespace
      static java.lang.CharSequence normalizeWhitespace​(java.lang.CharSequence in)
      Normalize whitespace as defined in XML Schema.
      static java.lang.CharSequence removeAllWhitespace​(java.lang.CharSequence value)
      Remove all whitespace characters from a string
      static java.lang.CharSequence removeLeadingWhitespace​(java.lang.CharSequence value)
      Remove leading whitespace characters from a string
      static java.lang.String trim​(java.lang.CharSequence s)
      Trim leading and trailing whitespace from a string, returning a string.
      static java.lang.CharSequence trimWhitespace​(java.lang.CharSequence in)
      Remove leading and trailing whitespace.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • PRESERVE

        public static final int PRESERVE
        The values PRESERVE, REPLACE, and COLLAPSE represent the three options for whitespace normalization. They are deliberately chosen in ascending strength order; given a number of whitespace facets, only the strongest needs to be carried out. The option TRIM is used instead of COLLAPSE when all valid values have no interior whitespace; trimming leading and trailing whitespace is then equivalent to the action of COLLAPSE, but faster.
        See Also:
        Constant Field Values
      • NONE

        public static final int NONE
        The values NONE, IGNORABLE, and ALL identify which kinds of whitespace text node should be stripped when building a source tree. UNSPECIFIED indicates that no particular request has been made. XSLT indicates that whitespace should be stripped as defined by the xsl:strip-space and xsl:preserve-space declarations in the stylesheet
        See Also:
        Constant Field Values
    • Method Detail

      • isWhitespace

        public static boolean isWhitespace​(int ch)
        Test whether a character is whitespace
        Parameters:
        ch - the character (Unicode codepoint) to be tested
        Returns:
        true if the character is one of tab, newline, carriage return, or space
      • applyWhitespaceNormalization

        public static java.lang.CharSequence applyWhitespaceNormalization​(int action,
                                                                          java.lang.CharSequence value)
        Apply schema-defined whitespace normalization to a string
        Parameters:
        action - the action to be applied: one of PRESERVE, REPLACE, or COLLAPSE
        value - the value to be normalized
        Returns:
        the value after normalization
      • removeAllWhitespace

        public static java.lang.CharSequence removeAllWhitespace​(java.lang.CharSequence value)
        Remove all whitespace characters from a string
        Parameters:
        value - the string from which whitespace is to be removed
        Returns:
        the string without its whitespace. This may be the original value if it contained no whitespace
      • removeLeadingWhitespace

        public static java.lang.CharSequence removeLeadingWhitespace​(java.lang.CharSequence value)
        Remove leading whitespace characters from a string
        Parameters:
        value - the string whose leading whitespace is to be removed
        Returns:
        the string with leading whitespace removed. This may be the original string if there was no leading whitespace
      • containsWhitespace

        public static boolean containsWhitespace​(java.lang.CharSequence value)
        Determine if a string contains any whitespace
        Parameters:
        value - the string to be tested
        Returns:
        true if the string contains a character that is XML whitespace, that is tab, newline, carriage return, or space
      • isWhite

        public static boolean isWhite​(java.lang.CharSequence content)
        Determine if a string is all-whitespace
        Parameters:
        content - the string to be tested
        Returns:
        true if the supplied string contains no non-whitespace characters
      • isWhite

        public static boolean isWhite​(char c)
        Determine if a character is whitespace
        Parameters:
        c - the character to be tested
        Returns:
        true if the character is a whitespace character
      • normalizeWhitespace

        public static java.lang.CharSequence normalizeWhitespace​(java.lang.CharSequence in)
        Normalize whitespace as defined in XML Schema. Note that this is not the same as the XPath normalize-space() function, which is supported by the collapseWhitespace(java.lang.CharSequence) method
        Parameters:
        in - the string to be normalized
        Returns:
        a copy of the string in which any whitespace character is replaced by a single space character
      • collapseWhitespace

        public static java.lang.CharSequence collapseWhitespace​(java.lang.CharSequence in)
        Collapse whitespace as defined in XML Schema. This is equivalent to the XPath normalize-space() function
        Parameters:
        in - the string whose whitespace is to be collapsed
        Returns:
        the string with any leading or trailing whitespace removed, and any internal sequence of whitespace characters replaced with a single space character.
      • trimWhitespace

        public static java.lang.CharSequence trimWhitespace​(java.lang.CharSequence in)
        Remove leading and trailing whitespace. This has the same effect as collapseWhitespace, but is cheaper, for use by data types that do not allow internal whitespace.
        Parameters:
        in - the input string whose whitespace is to be removed
        Returns:
        the result of removing excess whitespace
      • trim

        public static java.lang.String trim​(java.lang.CharSequence s)
        Trim leading and trailing whitespace from a string, returning a string. This differs from the Java trim() method in that the only characters treated as whitespace are space, \n, \r, and \t. The String#trim() method removes all C0 control characters (which is not the same thing under XML 1.1).
        Parameters:
        s - the string to be trimmed. If null is supplied, null is returned.
        Returns:
        the string with leading and trailing whitespace removed. Returns null if and only if the supplied argument is null.