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 java.util.Locale;
018    
019    import org.apache.hivemind.HiveMind;
020    import org.apache.tapestry.IMarkupWriter;
021    import org.apache.tapestry.IRequestCycle;
022    import org.apache.tapestry.form.AbstractFormComponentContributor;
023    import org.apache.tapestry.form.FormComponentContributorContext;
024    import org.apache.tapestry.form.IFormComponent;
025    import org.apache.tapestry.form.ValidationMessages;
026    import org.apache.tapestry.json.JSONObject;
027    import org.apache.tapestry.valid.ValidationConstants;
028    import org.apache.tapestry.valid.ValidatorException;
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()
076                    : parseText(field, messages, value);
077        }
078    
079        protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
080    
081        protected abstract Object parseText(IFormComponent field, ValidationMessages messages,
082                String text) throws ValidatorException;
083    
084        /**
085         * The value to be used when the value supplied in the request is blank (null or empty). The
086         * default value is null, but some subclasses may override.
087         * 
088         * @see #parse(IFormComponent, ValidationMessages, String)
089         * @return null, subclasses may override
090         */
091        protected Object getValueForEmptyInput()
092        {
093            return null;
094        }
095    
096        protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
097        {
098            String label = field.getDisplayName();
099            
100            Object[] parameters = getMessageParameters(messages.getLocale(), label);
101            
102            return messages.formatValidationMessage(_message, key, parameters);
103        }
104    
105        protected Object[] getMessageParameters(Locale locale, String label)
106        {
107            return new Object[] { label };
108        }
109    
110        /**
111         * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle,
112         *      org.apache.tapestry.form.IFormComponent)
113         */
114        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
115                FormComponentContributorContext context, IFormComponent field)
116        {
117            super.renderContribution(writer, cycle, context, field);
118            
119            if (_trim) {
120                JSONObject profile = context.getProfile();
121                
122                accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId());
123            }
124        }
125    
126        public boolean isTrim()
127    
128        {
129            return _trim;
130        }
131    
132        public void setTrim(boolean trim)
133        {
134            _trim = trim;
135        }
136    
137        public String getMessage()
138        {
139            return _message;
140        }
141    
142        public void setMessage(String message)
143        {
144            _message = message;
145        }
146    }