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.form.translator;
016    
017    import org.apache.hivemind.HiveMind;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.form.AbstractFormComponentContributor;
021    import org.apache.tapestry.form.FormComponentContributorContext;
022    import org.apache.tapestry.form.IFormComponent;
023    import org.apache.tapestry.form.ValidationMessages;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidationConstants;
026    import org.apache.tapestry.valid.ValidatorException;
027    
028    import java.util.Locale;
029    
030    /**
031     * Abstract {@link Translator} implementation that provides default behavior for trimming, null
032     * object, and empty text handling.
033     * 
034     * @author Paul Ferraro
035     * @since 4.0
036     */
037    public abstract class AbstractTranslator extends AbstractFormComponentContributor implements
038            Translator
039    {
040        private boolean _trim;
041    
042        private String _message;
043    
044        public AbstractTranslator()
045        {
046        }
047    
048        // Needed until HIVEMIND-134 fix is available
049        public AbstractTranslator(String initializer)
050        {
051            super(initializer);
052        }
053    
054        /**
055         * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
056         *      Locale, java.lang.Object)
057         */
058        public String format(IFormComponent field, Locale locale, Object object)
059        {
060            if (object == null)
061                return "";
062    
063            return formatObject(field, locale, object);
064        }
065    
066        /**
067         * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
068         *      ValidationMessages, java.lang.String)
069         */
070        public Object parse(IFormComponent field, ValidationMessages messages, String text)
071                throws ValidatorException
072        {
073            String value = text == null ? null : (_trim ? text.trim() : text);
074    
075            return HiveMind.isBlank(value) ? getValueForEmptyInput() : parseText(field, messages, value);
076        }
077    
078        protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
079    
080        protected abstract Object parseText(IFormComponent field, ValidationMessages messages, String text) 
081        throws ValidatorException;
082    
083        /**
084         * The value to be used when the value supplied in the request is blank (null or empty). The
085         * default value is null, but some subclasses may override.
086         * 
087         * @see #parse(IFormComponent, ValidationMessages, String)
088         * @return null, subclasses may override
089         */
090        protected Object getValueForEmptyInput()
091        {
092            return null;
093        }
094    
095        protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
096        {
097            String label = field.getDisplayName();
098            
099            Object[] parameters = getMessageParameters(messages.getLocale(), label);
100            
101            return messages.formatValidationMessage(_message, key, parameters);
102        }
103    
104        protected Object[] getMessageParameters(Locale locale, String label)
105        {
106            return new Object[] { label };
107        }
108    
109        /**
110         * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(IMarkupWriter, IRequestCycle, FormComponentContributorContext, IFormComponent)
111         */
112        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
113                FormComponentContributorContext context, IFormComponent field)
114        {
115            super.renderContribution(writer, cycle, context, field);
116            
117            if (_trim) {
118                JSONObject profile = context.getProfile();
119                
120                accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId());
121            }
122        }
123    
124        public boolean isTrim()
125    
126        {
127            return _trim;
128        }
129    
130        public void setTrim(boolean trim)
131        {
132            _trim = trim;
133        }
134    
135        public String getMessage()
136        {
137            return _message;
138        }
139    
140        public void setMessage(String message)
141        {
142            _message = message;
143        }
144    }