1 /* 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Intalio, Inc. For written permission, 18 * please contact info@exolab.org. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Intalio, Inc. Exolab is a registered 23 * trademark of Intalio, Inc. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 package org.exolab.castor.xml.validators; 46 47 /** 48 * A static class for performing simple validation. 49 * 50 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 51 * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $ 52 */ 53 public final class ValidationUtils { 54 55 // ----------------/ 56 // - Constructors -/ 57 // ----------------/ 58 59 /** 60 * No-arg constructor. 61 */ 62 private ValidationUtils() { 63 super(); 64 } 65 66 // ------------------/ 67 // - Public Methods -/ 68 // ------------------/ 69 70 /** 71 * Checks the given character to determine if it is a valid CombiningChar as 72 * defined by the W3C XML 1.0 Recommendation. 73 * <p> 74 * FIXME: This method needs to be properly implemented. 75 * @param ch THe character to check. 76 * 77 * @return true if the given character is a CombiningChar 78 */ 79 public static boolean isCombiningChar(final char ch) { 80 // -- NOTE: THIS METHOD IS NOT COMPLETE 81 return false; 82 } // -- isCombiningChar 83 84 /** 85 * Checks a character to see if it is a digit or not. 86 * @param ch 87 * the character to check 88 * @return true if the given character is a digit 89 */ 90 public static boolean isDigit(final char ch) { 91 return Character.isDigit(ch); 92 } // -- isDigit 93 94 /** 95 * Checks a character to see if it is a letter or not. 96 * @param ch 97 * the character to check 98 * @return true if the given character is a letter 99 */ 100 public static boolean isLetter(final char ch) { 101 return Character.isLetter(ch); 102 } // -- isLetter 103 104 /** 105 * Checks all characters of the given String to determine if they 106 * syntactically match the production of an NCName as defined by the W3C XML 107 * Namespaces recommendation. 108 * 109 * @param str 110 * the String to check 111 * @return true if the given String follows the Syntax of an NCName 112 */ 113 public static boolean isNCName(final String str) { 114 if (str == null || str.length() == 0) { 115 return false; 116 } 117 118 char[] chars = str.toCharArray(); 119 120 // -- make sure String starts with a letter or '_' 121 char ch = chars[0]; 122 if (!isLetter(ch) && ch != '_') { 123 return false; 124 } 125 126 for (int i = 1; i < chars.length; i++) { 127 if (!isNCNameChar(chars[i])) { 128 return false; 129 } 130 } 131 return true; 132 } // -- isNCName 133 134 /** 135 * Checks the the given character to determine if it is a valid NCNameChar 136 * as defined by the W3C XML Namespaces recommendation. 137 * 138 * @param ch 139 * the char to check 140 * @return true if the given char is an NCNameChar 141 */ 142 public static boolean isNCNameChar(final char ch) { 143 if (isLetter(ch) || isDigit(ch)) { 144 return true; 145 } 146 147 if (isExtender(ch) || isCombiningChar(ch)) { 148 return true; 149 } 150 151 switch (ch) { 152 case '.': 153 case '-': 154 case '_': 155 return true; 156 default: 157 return false; 158 } 159 } // -- isNCNameChar 160 161 /** 162 * Checks all characters of the given String to determine if they 163 * syntactically match the production of an NMToken. 164 * 165 * @param str 166 * the String to check 167 * @return true if the given String follows the Syntax of an NMToken 168 */ 169 public static boolean isNMToken(final String str) { 170 if (str == null) { 171 return false; 172 } 173 174 char[] chars = str.toCharArray(); 175 176 for (int i = 0; i < chars.length; i++) { 177 char ch = chars[i]; 178 if (isLetter(ch) || isDigit(ch) || isExtender(ch) || isCombiningChar(ch)) { 179 continue; 180 } 181 182 switch (ch) { 183 case '.': 184 case '-': 185 case '_': 186 case ':': 187 break; 188 default: 189 return false; 190 } 191 } 192 return true; 193 } // -- isNMToken 194 195 /** 196 * Checks all characters of the given String to determine if they 197 * syntactically match the production of a CDATA. 198 * 199 * @param str 200 * the String to check 201 * @return true if the given String follows the Syntax of an NMToken 202 */ 203 public static boolean isCDATA(final String str) { 204 if (str == null) { 205 return false; 206 } 207 208 char[] chars = str.toCharArray(); 209 210 for (int i = 0; i < chars.length; i++) { 211 char ch = chars[i]; 212 switch (ch) { 213 case '\r': 214 case '\n': 215 case '\t': 216 return false; 217 default: 218 continue; 219 } 220 } 221 return true; 222 } // -- isCDATA 223 224 /** 225 * Returns true if the given character is a valid XML Extender character, 226 * according to the XML 1.0 specification. 227 * 228 * @param ch 229 * the character to check 230 * @return true if the character is a valid XML Extender character 231 */ 232 public static boolean isExtender(final char ch) { 233 if ((ch >= 0x3031) && (ch <= 0x3035)) { 234 return true; 235 } 236 237 if ((ch >= 0x30FC) && (ch <= 0x30FE)) { 238 return true; 239 } 240 241 switch (ch) { 242 case 0x00B7: 243 case 0x02D0: 244 case 0x02D1: 245 case 0x0387: 246 case 0x0640: 247 case 0x0E46: 248 case 0x0EC6: 249 case 0x3005: 250 case 0x309D: 251 case 0x309E: 252 return true; 253 default: 254 break; 255 } 256 return false; 257 } // -- isExtender 258 259 /** 260 * Checks all characters of the given String to determine if they 261 * syntactically match the production of an QName as defined by the W3C XML 262 * Namespaces recommendation. 263 * 264 * @param str 265 * the String to check 266 * @return true if the given String follows the Syntax of an QName 267 */ 268 public static boolean isQName(final String str) { 269 if (str == null || str.length() == 0) { 270 return false; 271 } 272 273 char[] chars = str.toCharArray(); 274 275 // -- make sure String starts with a letter or '_' 276 char ch = chars[0]; 277 if (!isLetter(ch) && ch != '_') { 278 return false; 279 } 280 281 for (int i = 1; i < chars.length; i++) { 282 if (chars[i] == ':') { 283 continue; 284 } 285 286 if (!isNCNameChar(chars[i])) { 287 return false; 288 } 289 } 290 return true; 291 } //-- isQName 292 293 } //-- Validator