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