View Javadoc
1   /*
2    * Copyright 2006 Werner Guttmann
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.exolab.castor.xml.validators;
15  
16  import org.exolab.castor.xml.TypeValidator;
17  import org.exolab.castor.xml.ValidationContext;
18  import org.exolab.castor.xml.ValidationException;
19  
20  /**
21   * The ID Validation class.
22   *
23   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttman</a>
24   * @version $Revision: 5951 $ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $
25   */
26  public class IdValidator extends StringValidator implements TypeValidator {
27  
28    /**
29     * Creates a new IdValidator with no restrictions.
30     */
31    public IdValidator() {
32      super();
33    } // -- IdValidator
34  
35    /**
36     * Validates the given Object.
37     *
38     * @param value the string to validate
39     * @param context the ValidationContext
40     * @throws ValidationException if the object fails validation.
41     */
42    public void validate(final String value, final ValidationContext context)
43        throws ValidationException {
44      super.validate(value, context);
45    } // -- validate
46  
47    /**
48     * Validates the given Object.
49     *
50     * @param object the Object to validate
51     * @throws ValidationException if the object fails validation.
52     */
53    public void validate(final Object object) throws ValidationException {
54      validate(object, (ValidationContext) null);
55    } // -- validate
56  
57    /**
58     * Validates the given Object.
59     *
60     * @param object the Object to validate
61     * @param context the ValidationContext
62     * @throws ValidationException if the object fails validation.
63     */
64    public void validate(final Object object, final ValidationContext context)
65        throws ValidationException {
66      if (object == null) {
67        String err = "IdValidator cannot validate a null object.";
68        throw new ValidationException(err);
69      }
70  
71      String value = null;
72      if (!(object instanceof String)) {
73        throw new ValidationException("IDs should be of type String");
74      }
75  
76      value = (String) object;
77  
78      if (value.equals("")) {
79        String err = "Invalid ID value: '' is not a valid value.";
80        throw new ValidationException(err);
81      }
82  
83      context.addID(value);
84  
85      // validate(value, context);
86    } // -- validate
87  
88  } // -- IdValidator