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