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.mapping.ClassDescriptor;
17  import org.exolab.castor.mapping.FieldDescriptor;
18  import org.exolab.castor.mapping.FieldHandler;
19  import org.exolab.castor.xml.ClassDescriptorResolver;
20  import org.exolab.castor.xml.TypeValidator;
21  import org.exolab.castor.xml.ValidationContext;
22  import org.exolab.castor.xml.ValidationException;
23  
24  /**
25   * The IDREF Validation class.
26   *
27   * @author <a href="mailto:werner DOT guttmann AT gmx DOT net">Werner Guttman</a>
28   * @version $Revision: 0000 $ $Date: $
29   */
30  public class IdRefValidator implements TypeValidator {
31  
32    /**
33     * Creates a new IdRefValidator with no restrictions.
34     */
35    public IdRefValidator() {
36      super();
37    } // -- IdValidator
38  
39    /**
40     * Validates the given Object.
41     *
42     * @param object the Object to validate
43     * @param context the ValidationContext
44     * @throws ValidationException if the object fails validation.
45     */
46    public void validate(final Object object, final ValidationContext context)
47        throws ValidationException {
48      // we need a target Object
49      if (object == null) {
50        String err = "The object associated with IDREF \"" + object + "\" is null!";
51        throw new ValidationException(err);
52      }
53  
54      // get the id of the target object
55      String id = null;
56      try {
57        ClassDescriptorResolver classDescriptorResolver = context.getClassDescriptorResolver();
58        ClassDescriptor classDescriptor = classDescriptorResolver.resolve(object.getClass());
59        FieldDescriptor fieldDescriptor = classDescriptor.getIdentity();
60        FieldHandler fieldHandler = fieldDescriptor.getHandler();
61        id = (String) fieldHandler.getValue(object);
62      } catch (Exception e) {
63        String err = "The object associated with IDREF \"" + object + "\" of type "
64            + object.getClass() + " has no ID!";
65        throw new ValidationException(err);
66      }
67  
68      if (id == null) {
69        String err = "The object associated with IDREF \"" + object + "\" has no ID!";
70        throw new ValidationException(err);
71      }
72  
73      // check if referenced id exists, otherwise put it into "currently unresolved" queue
74      context.checkIdRef(id);
75  
76      // validate(value, context);
77    } // -- validate
78  
79  }