KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > utils > XMLCharacterRecognizer


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 /**
22  * Class used to verify whether the specified <var>ch</var>
23  * conforms to the XML 1.0 definition of whitespace.
24  * @xsl.usage internal
25  */

26 public class XMLCharacterRecognizer
27 {
28
29   /**
30    * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
31    * of whitespace. Refer to <A HREF="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
32    * the definition of <CODE>S</CODE></A> for details.
33    * @param ch Character to check as XML whitespace.
34    * @return =true if <var>ch</var> is XML whitespace; otherwise =false.
35    */

36   public static boolean isWhiteSpace(char ch)
37   {
38     return (ch == 0x20) || (ch == 0x09) || (ch == 0xD) || (ch == 0xA);
39   }
40
41   /**
42    * Tell if the string is whitespace.
43    *
44    * @param ch Character array to check as XML whitespace.
45    * @param start Start index of characters in the array
46    * @param length Number of characters in the array
47    * @return True if the characters in the array are
48    * XML whitespace; otherwise, false.
49    */

50   public static boolean isWhiteSpace(char ch[], int start, int length)
51   {
52
53     int end = start + length;
54
55     for (int s = start; s < end; s++)
56     {
57       if (!isWhiteSpace(ch[s]))
58         return false;
59     }
60
61     return true;
62   }
63
64   /**
65    * Tell if the string is whitespace.
66    *
67    * @param buf StringBuffer to check as XML whitespace.
68    * @return True if characters in buffer are XML whitespace, false otherwise
69    */

70   public static boolean isWhiteSpace(StringBuffer JavaDoc buf)
71   {
72
73     int n = buf.length();
74
75     for (int i = 0; i < n; i++)
76     {
77       if (!isWhiteSpace(buf.charAt(i)))
78         return false;
79     }
80
81     return true;
82   }
83   
84   /**
85    * Tell if the string is whitespace.
86    *
87    * @param buf StringBuffer to check as XML whitespace.
88    * @return True if characters in buffer are XML whitespace, false otherwise
89    */

90   public static boolean isWhiteSpace(String JavaDoc s)
91   {
92
93     if(null != s)
94     {
95       int n = s.length();
96   
97       for (int i = 0; i < n; i++)
98       {
99         if (!isWhiteSpace(s.charAt(i)))
100           return false;
101       }
102     }
103
104     return true;
105   }
106
107 }
108
Popular Tags