001    // Copyright 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry.valid;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.Location;
019    import org.apache.hivemind.lib.BeanFactory;
020    import org.apache.tapestry.IBinding;
021    import org.apache.tapestry.IComponent;
022    import org.apache.tapestry.binding.AbstractBindingFactory;
023    import org.apache.tapestry.engine.IScriptSource;
024    
025    /**
026     * Uses the tapestry.valid.ValidatorBeanFactory service to obtain configuration IValidator
027     * instances.
028     * 
029     * @author Howard M. Lewis Ship
030     * @since 4.0
031     * @see org.apache.tapestry.valid.ValidatorBinding
032     */
033    public class ValidatorBindingFactory extends AbstractBindingFactory
034    {
035        private BeanFactory _validatorBeanFactory;
036    
037        private IScriptSource _scriptSource;
038        
039        public void setValidatorBeanFactory(BeanFactory validatorBeanFactory)
040        {
041            _validatorBeanFactory = validatorBeanFactory;
042        }
043    
044        public void setScriptSource(IScriptSource scriptSource)
045        {
046            _scriptSource = scriptSource;
047        }
048        
049        /**
050         * Creates and returns a {@link ValidatorBinding}. Interprets the path as a bean initializer,
051         * used to locate a particular type of validator and a particular configuration of its
052         * properties.
053         */
054    
055        public IBinding createBinding(IComponent root, String bindingDescription, String expression,
056                Location location)
057        {
058            try
059            {
060                IValidator validator = (IValidator) _validatorBeanFactory.get(expression);
061                validator.setScriptSource(_scriptSource);
062                
063                return new ValidatorBinding(bindingDescription, getValueConverter(), location,
064                        validator);
065            }
066            catch (Exception ex)
067            {
068                throw new ApplicationRuntimeException(ex.getMessage(), location, ex);
069            }
070        }
071    }