/*
* Copyright 2006-2007 Dan Shellman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.iscreen.impl;
import java.util.List;
import java.util.Locale;
import org.iscreen.DocumentationIterator;
import org.iscreen.ObjectValidationException;
import org.iscreen.ValidationException;
import org.iscreen.ValidationService;
/**
* This is the base class implementation of the ValidationService interface.
*
* @author Shellman, Dan
*/
public abstract class BaseValidationService implements ValidationService
{
protected String id;
protected Locale defaultLocale;
/**
* Constructor taking unique id. The unique id is required. In addition,
* the default locale is provided.
*
* @param uniqueId The unique id for this service.
* @param locale The default locale for this service.
*/
public BaseValidationService( String uniqueId, Locale locale )
{
id = uniqueId;
defaultLocale = locale;
} //end BaseValidationService()
public void validate( Object obj ) throws ValidationException
{
validate( obj, defaultLocale );
} //end validate()
public void validate( Object obj, Locale locale ) throws ValidationException
{
ContextBean contextBean;
DefaultValidatorContext context;
ValidatorWrapper wrapper;
List failures;
//Basic algorithm:
//Loop through validators and perform the following:
// -Initialize an ContextBean and ValidatorContext
// -Call the validator wrapper's validate() method with context and bean
// -Store any validation failures
//Once done looping through validators, if there are more than zero
//validation failures, create a ValidationException, stuff the validation
//failures in there, and throw it. Otherwise, return.
contextBean = constructContextBean( obj );
context = constructContext( contextBean, locale );
resetValidatorIteration();
wrapper = getNextValidator();
while ( wrapper != null )
{
boolean continueFlag;
context.setValidator( wrapper );
continueFlag = wrapper.validate( context, contextBean, obj );
if ( continueFlag == false )
{
break;
}
wrapper = getNextValidator();
}
//Finally, check to see if there were any failures.
if ( context.getCount() > 0 )
{
throw new ValidationException( context.getFailures(),
context.getWarnings() );
}
} //end validate()
public void validateObject( Object obj, Locale locale )
{
try
{
validate( obj, locale );
}
catch ( ValidationException e )
{
throw new ObjectValidationException( e );
}
} //end validateObject()
public void validateObject( Object obj )
{
validateObject( obj, defaultLocale );
} //end validateObject()
public String getServiceName()
{
return id;
} //end getServiceName()
/**
* Retrieves an iterator of String values representing the documentation
* configured for this validation service (the underlying validation set).
*
* @return Returns an iterator of Strings representing documentation.
*/
public DocumentationIterator getDocumentation()
{
DocumentationIterator docIt = new DocumentationIterator();
ValidatorWrapper wrapper;
resetValidatorIteration();
wrapper = getNextValidator();
while ( wrapper != null )
{
docIt.addIterator( wrapper.getDoc() );
wrapper = getNextValidator();
}
return docIt;
} //end getDocumentation()
// ***
// Protected methods
// ***
/**
* Retrieves the "next" Validator in the set of Validators that this
* validation service is configured to use. If there are no more
* Validators, then this method should return null. It should return
* the first Validator after a call to resetValidatorIteration().
*
* @return Returns the next ValidatorWrapper in the set of validators.
*/
protected abstract ValidatorWrapper getNextValidator();
/**
* Resets the iteration of Validators using the getNextValidator() method.
*/
protected abstract void resetValidatorIteration();
/**
* This can be over-written by sub-classes, but it's not really necessary,
* since this service requires a particular implementation of the
* ValidatorContext.
*
*
* @param root The ContextBean that the context needs.
* @param locale The locale to use in generating error messages.
* @return Returns a constructed and configured DefaultValidatorContext.
*/
protected DefaultValidatorContext constructContext( ContextBean root, Locale locale )
{
return new DefaultValidatorContext( root, locale );
} //end constructContext()
/**
* This can be over-written by sub-classes, but it's not really necessary,
* since this service requires an OGNL root object.
*
*
* @return Returns an instance of ContextBean fully configured.
*/
protected ContextBean constructContextBean( Object objToValidate )
{
ContextBean root;
root = new ContextBean();
root.setBean( objToValidate );
return root;
} //end constructContextBean()
} //end BaseValidationService
|