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