1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: XMLCharacterRecognizer.java,v 1.7 2004/02/17 04:21:14 minchau Exp $
  18. */
  19. package com.sun.org.apache.xml.internal.utils;
  20. /**
  21. * Class used to verify whether the specified <var>ch</var>
  22. * conforms to the XML 1.0 definition of whitespace.
  23. * @xsl.usage internal
  24. */
  25. public class XMLCharacterRecognizer
  26. {
  27. /**
  28. * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
  29. * of whitespace. Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
  30. * the definition of <CODE>S</CODE></A> for details.
  31. * @param ch Character to check as XML whitespace.
  32. * @return =true if <var>ch</var> is XML whitespace; otherwise =false.
  33. */
  34. public static boolean isWhiteSpace(char ch)
  35. {
  36. return (ch == 0x20) || (ch == 0x09) || (ch == 0xD) || (ch == 0xA);
  37. }
  38. /**
  39. * Tell if the string is whitespace.
  40. *
  41. * @param ch Character array to check as XML whitespace.
  42. * @param start Start index of characters in the array
  43. * @param length Number of characters in the array
  44. * @return True if the characters in the array are
  45. * XML whitespace; otherwise, false.
  46. */
  47. public static boolean isWhiteSpace(char ch[], int start, int length)
  48. {
  49. int end = start + length;
  50. for (int s = start; s < end; s++)
  51. {
  52. if (!isWhiteSpace(ch[s]))
  53. return false;
  54. }
  55. return true;
  56. }
  57. /**
  58. * Tell if the string is whitespace.
  59. *
  60. * @param buf StringBuffer to check as XML whitespace.
  61. * @return True if characters in buffer are XML whitespace, false otherwise
  62. */
  63. public static boolean isWhiteSpace(StringBuffer buf)
  64. {
  65. int n = buf.length();
  66. for (int i = 0; i < n; i++)
  67. {
  68. if (!isWhiteSpace(buf.charAt(i)))
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * Tell if the string is whitespace.
  75. *
  76. * @param buf StringBuffer to check as XML whitespace.
  77. * @return True if characters in buffer are XML whitespace, false otherwise
  78. */
  79. public static boolean isWhiteSpace(String s)
  80. {
  81. if(null != s)
  82. {
  83. int n = s.length();
  84. for (int i = 0; i < n; i++)
  85. {
  86. if (!isWhiteSpace(s.charAt(i)))
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. }