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-2003 (C) Intalio, Inc. All Rights Reserved. 42 * 43 * $Id$ 44 */ 45 package org.exolab.castor.xml.validators; 46 47 import org.exolab.castor.xml.ValidationContext; 48 import org.exolab.castor.xml.ValidationException; 49 import org.exolab.castor.xml.XMLConstants; 50 51 /** 52 * The Name Validation class. This class handles validation for XML Name 53 * production types such as NCName and NMToken 54 * 55 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 56 * @version $Revision$ $Date: 2005-12-13 14:58:48 -0700 (Tue, 13 Dec 2005) $ 57 */ 58 public class NameValidator extends StringValidator { 59 60 /** XML name type NCName. @deprecated - use XMLConstants.NAME_TYPE_NCNAME. 61 * Retained for backwards-compatility. */ 62 public static final short NCNAME = XMLConstants.NAME_TYPE_NCNAME; 63 64 /** XML name type NMTOKEN. @deprecated - use XMLConstants.NAME_TYPE_NCTOKEN. 65 * Retained for backwards-compatility. */ 66 public static final short NMTOKEN = XMLConstants.NAME_TYPE_NMTOKEN; 67 68 /** XML name type CDATA. @deprecated - use XMLConstants.NAME_TYPE_CDATA. 69 * Retained for backwards-compatility. */ 70 public static final short CDATA = XMLConstants.NAME_TYPE_CDATA; 71 72 /** Name type. */ 73 private short _type = XMLConstants.NAME_TYPE_NCNAME; 74 75 /** 76 * Creates a new NameValidator with the default validation set to NCName. 77 */ 78 public NameValidator() { 79 super(); 80 } // -- NameValidator 81 82 /** 83 * Creates a new NameValidator with the given validation type. 84 * 85 * @param type 86 * the validation type for this NameValidator 87 */ 88 public NameValidator(final short type) { 89 super(); 90 this._type = type; 91 } // -- NMTokenValidator 92 93 /** 94 * Sets whether or not a String is required (non null). 95 * 96 * @param required 97 * the flag indicating whether Strings are required 98 */ 99 public void setRequired(final boolean required) { 100 // not implemented? 101 } 102 103 /** 104 * Validates the given Object. 105 * 106 * @param value 107 * the string to validate 108 * @param context 109 * the ValidationContext 110 * @throws ValidationException if the object fails validation. 111 */ 112 public void validate(final String value, final ValidationContext context) 113 throws ValidationException { 114 super.validate(value, context); 115 116 switch (_type) { 117 case XMLConstants.NAME_TYPE_CDATA: 118 if (!ValidationUtils.isCDATA(value)) { 119 String err = "Name '" + value + "' is not a valid CDATA."; 120 throw new ValidationException(err); 121 } 122 break; 123 124 case XMLConstants.NAME_TYPE_NMTOKEN: 125 if (!ValidationUtils.isNMToken(value)) { 126 String err = "Name '" + value + "' is not a valid NMToken."; 127 throw new ValidationException(err); 128 } 129 break; 130 131 case XMLConstants.NAME_TYPE_QNAME: 132 if (!ValidationUtils.isQName(value)) { 133 String err = "Name '" + value + "' is not a valid QName."; 134 throw new ValidationException(err); 135 } 136 break; 137 138 case XMLConstants.NAME_TYPE_NCNAME: 139 default: 140 if (!ValidationUtils.isNCName(value)) { 141 String err = "Name '" + value + "' is not a valid NCName."; 142 throw new ValidationException(err); 143 } 144 break; 145 } 146 } // -- validate 147 148 /** 149 * Validates the given Object. 150 * 151 * @param object 152 * the Object to validate 153 * @throws ValidationException if the object fails validation. 154 */ 155 public void validate(final Object object) throws ValidationException { 156 validate(object, (ValidationContext) null); 157 } // -- validate 158 159 /** 160 * Validates the given Object. 161 * 162 * @param object 163 * the Object to validate 164 * @param context 165 * the ValidationContext 166 * @throws ValidationException if the object fails validation. 167 */ 168 public void validate(final Object object, final ValidationContext context) 169 throws ValidationException { 170 if (object != null) { 171 validate(object.toString(), context); 172 } else { 173 validate(null, context); 174 } 175 } // -- validate 176 177 } // -- NameValidator