001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: XMLCharacterRecognizer.java 468655 2006-10-28 07:12:06Z minchau $
020     */
021    package org.apache.xml.utils;
022    
023    /**
024     * Class used to verify whether the specified <var>ch</var> 
025     * conforms to the XML 1.0 definition of whitespace. 
026     * @xsl.usage internal
027     */
028    public class XMLCharacterRecognizer
029    {
030    
031      /**
032       * Returns whether the specified <var>ch</var> conforms to the XML 1.0 definition
033       * of whitespace.  Refer to <A href="http://www.w3.org/TR/1998/REC-xml-19980210#NT-S">
034       * the definition of <CODE>S</CODE></A> for details.
035       * @param ch Character to check as XML whitespace.
036       * @return =true if <var>ch</var> is XML whitespace; otherwise =false.
037       */
038      public static boolean isWhiteSpace(char ch)
039      {
040        return (ch == 0x20) || (ch == 0x09) || (ch == 0xD) || (ch == 0xA);
041      }
042    
043      /**
044       * Tell if the string is whitespace.
045       *
046       * @param ch Character array to check as XML whitespace.
047       * @param start Start index of characters in the array
048       * @param length Number of characters in the array 
049       * @return True if the characters in the array are 
050       * XML whitespace; otherwise, false.
051       */
052      public static boolean isWhiteSpace(char ch[], int start, int length)
053      {
054    
055        int end = start + length;
056    
057        for (int s = start; s < end; s++)
058        {
059          if (!isWhiteSpace(ch[s]))
060            return false;
061        }
062    
063        return true;
064      }
065    
066      /**
067       * Tell if the string is whitespace.
068       *
069       * @param buf StringBuffer to check as XML whitespace.
070       * @return True if characters in buffer are XML whitespace, false otherwise
071       */
072      public static boolean isWhiteSpace(StringBuffer buf)
073      {
074    
075        int n = buf.length();
076    
077        for (int i = 0; i < n; i++)
078        {
079          if (!isWhiteSpace(buf.charAt(i)))
080            return false;
081        }
082    
083        return true;
084      }
085      
086      /**
087       * Tell if the string is whitespace.
088       *
089       * @param s String to check as XML whitespace.
090       * @return True if characters in buffer are XML whitespace, false otherwise
091       */
092      public static boolean isWhiteSpace(String s)
093      {
094    
095        if(null != s)
096        {
097          int n = s.length();
098      
099          for (int i = 0; i < n; i++)
100          {
101            if (!isWhiteSpace(s.charAt(i)))
102              return false;
103          }
104        }
105    
106        return true;
107      }
108    
109    }