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