Save This Page
Home » Castor-1.3-src » org.exolab.castor » xml » [javadoc | source]
    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-2002 (C) Intalio, Inc. All Rights Reserved.
   42    *
   43    * This file was originally developed by Keith Visco during the
   44    * course of employment at Intalio Inc.
   45    * All portions of this file developed by Keith Visco after Jan 19 2005 are
   46    * Copyright (C) 2005 Keith Visco. All Rights Reserved.
   47    *
   48    * $Id$
   49    */
   50   package org.exolab.castor.xml;
   51   
   52   import org.apache.commons.logging.Log;
   53   import org.apache.commons.logging.LogFactory;
   54   import org.castor.xml.BackwardCompatibilityContext;
   55   import org.castor.xml.InternalContext;
   56   import org.exolab.castor.mapping.FieldDescriptor;
   57   import org.exolab.castor.xml.location.XPathLocation;
   58   import org.exolab.castor.xml.validators.ClassValidator;
   59   
   60   /**
   61    * A class which can perform Validation on an Object model. This class uses the
   62    * ClassDescriptors and FieldDescriptors to perform the validation.
   63    *
   64    * @author <a href="mailto:keith AT kvisco DOT com">Keith Visco</a>
   65    * @version $Revision$ $Date: 2005-02-28 17:43:25 -0700 (Mon, 28 Feb 2005) $
   66    */
   67   public class Validator implements ClassValidator {
   68       private static final Log LOG = LogFactory.getLog(Validator.class);
   69   
   70       /**
   71        * Creates a new Validator.
   72        */
   73       public Validator() {
   74           super();
   75       } //-- Validator
   76   
   77       /**
   78        * Validates the given Object.
   79        *
   80        * @param object the Object to validate
   81        * @throws ValidationException if validation fails.
   82        */
   83       public void validate(final Object object) throws ValidationException {
   84           validate(object, (ValidationContext) null);
   85       }
   86   
   87       /**
   88        * Validates the given Object.
   89        *
   90        * @param object the Object to validate
   91        * @param context the ValidationContext to use during validation.
   92        * @throws ValidationException if validation fails.
   93        */
   94       public void validate(final Object object, final ValidationContext context)
   95                                                        throws ValidationException {
   96           if (object == null) {
   97               throw new ValidationException("Cannot validate a null Object.");
   98           }
   99   
  100           if (context == null) {
  101               ValidationContext v2 = new ValidationContext();
  102               InternalContext ic = new BackwardCompatibilityContext();
  103               ic.setClassLoader(object.getClass().getClassLoader());
  104               v2.setInternalContext(ic);
  105               validate(object, v2);
  106               return;
  107           }
  108   
  109           if (context.getClassDescriptorResolver() == null) {
  110               String message = "ClassDescriptorResolver from context must not be null!";
  111               LOG.warn(message);
  112               throw new IllegalStateException(message);
  113           }
  114   
  115           XMLClassDescriptor classDesc = null;
  116   
  117           if (!MarshalFramework.isPrimitive(object.getClass())) {
  118               try {
  119                   classDesc = (XMLClassDescriptor) context.getClassDescriptorResolver().resolve(object.getClass());
  120               } catch (ResolverException rx) {
  121                   throw new ValidationException(rx);
  122               }
  123           }
  124   
  125           //-- we cannot validate an object if ClassDescriptor is null
  126           if (classDesc == null) {
  127               return;
  128           }
  129   
  130           XMLFieldDescriptor fieldDesc = null;
  131   
  132           try {
  133               TypeValidator validator = classDesc.getValidator();
  134               if (validator != null) {
  135                   validator.validate(object, context);
  136               } else {
  137                   // Default validation -- just validate each field
  138                   FieldDescriptor[] fields = classDesc.getFields();
  139                   if (fields != null) {
  140                       for (int i = 0; i < fields.length; i++) {
  141                           fieldDesc = (XMLFieldDescriptor) fields[i];
  142                           if (fieldDesc == null) {
  143                               continue;
  144                           }
  145                           FieldValidator fieldValidator = fieldDesc.getValidator();
  146                           if (fieldValidator != null) {
  147                               fieldValidator.validate(object, context);
  148                           }
  149                       }
  150                   }
  151               }
  152           } catch (ValidationException vx) {
  153               //-- add location information
  154               XPathLocation loc = (XPathLocation) vx.getLocation();
  155               if (loc == null) {
  156                   loc = new XPathLocation();
  157                   vx.setLocation(loc);
  158                   if (fieldDesc != null) {
  159                       if (fieldDesc.getNodeType() == NodeType.Attribute) {
  160                           loc.addAttribute(fieldDesc.getXMLName());
  161                       } else {
  162                           loc.addChild(fieldDesc.getXMLName());
  163                       }
  164                   }
  165               }
  166               if (classDesc.getXMLName() != null) {
  167                    loc.addParent(classDesc.getXMLName());
  168               }
  169               throw vx;
  170           }
  171           
  172   //        checkUnresolvedIdrefs(context);
  173   
  174       }
  175       
  176       public void checkUnresolvedIdrefs(ValidationContext context) throws ValidationException {
  177           if (context.getUnresolvedIdRefs().size() > 0) {
  178               String err = "Unresolved IDREfs: " + context.getUnresolvedIdRefs().toString();
  179               throw new ValidationException(err);
  180           }
  181       }
  182   
  183       // TODO: add cleanup life-cycle method to be called from outside
  184   
  185   }

Save This Page
Home » Castor-1.3-src » org.exolab.castor » xml » [javadoc | source]