Coverage Report - org.apache.tapestry.form.translator.AbstractTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTranslator
0%
0/25
0%
0/10
1.429
 
 1  
 // Copyright 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.form.translator;
 16  
 
 17  
 import org.apache.hivemind.HiveMind;
 18  
 import org.apache.tapestry.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.form.AbstractFormComponentContributor;
 21  
 import org.apache.tapestry.form.FormComponentContributorContext;
 22  
 import org.apache.tapestry.form.IFormComponent;
 23  
 import org.apache.tapestry.form.ValidationMessages;
 24  
 import org.apache.tapestry.json.JSONObject;
 25  
 import org.apache.tapestry.valid.ValidationConstants;
 26  
 import org.apache.tapestry.valid.ValidatorException;
 27  
 
 28  
 import java.util.Locale;
 29  
 
 30  
 /**
 31  
  * Abstract {@link Translator} implementation that provides default behavior for trimming, null
 32  
  * object, and empty text handling.
 33  
  * 
 34  
  * @author Paul Ferraro
 35  
  * @since 4.0
 36  
  */
 37  
 public abstract class AbstractTranslator extends AbstractFormComponentContributor implements
 38  
         Translator
 39  
 {
 40  
     private boolean _trim;
 41  
 
 42  
     private String _message;
 43  
 
 44  
     public AbstractTranslator()
 45  0
     {
 46  0
     }
 47  
 
 48  
     // Needed until HIVEMIND-134 fix is available
 49  
     public AbstractTranslator(String initializer)
 50  
     {
 51  0
         super(initializer);
 52  0
     }
 53  
 
 54  
     /**
 55  
      * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
 56  
      *      Locale, java.lang.Object)
 57  
      */
 58  
     public String format(IFormComponent field, Locale locale, Object object)
 59  
     {
 60  0
         if (object == null)
 61  0
             return "";
 62  
 
 63  0
         return formatObject(field, locale, object);
 64  
     }
 65  
 
 66  
     /**
 67  
      * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
 68  
      *      ValidationMessages, java.lang.String)
 69  
      */
 70  
     public Object parse(IFormComponent field, ValidationMessages messages, String text)
 71  
             throws ValidatorException
 72  
     {
 73  0
         String value = text == null ? null : (_trim ? text.trim() : text);
 74  
 
 75  0
         return HiveMind.isBlank(value) ? getValueForEmptyInput() : parseText(field, messages, value);
 76  
     }
 77  
 
 78  
     protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
 79  
 
 80  
     protected abstract Object parseText(IFormComponent field, ValidationMessages messages, String text) 
 81  
     throws ValidatorException;
 82  
 
 83  
     /**
 84  
      * The value to be used when the value supplied in the request is blank (null or empty). The
 85  
      * default value is null, but some subclasses may override.
 86  
      * 
 87  
      * @see #parse(IFormComponent, ValidationMessages, String)
 88  
      * @return null, subclasses may override
 89  
      */
 90  
     protected Object getValueForEmptyInput()
 91  
     {
 92  0
         return null;
 93  
     }
 94  
 
 95  
     protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
 96  
     {
 97  0
         String label = field.getDisplayName();
 98  
         
 99  0
         Object[] parameters = getMessageParameters(messages.getLocale(), label);
 100  
         
 101  0
         return messages.formatValidationMessage(_message, key, parameters);
 102  
     }
 103  
 
 104  
     protected Object[] getMessageParameters(Locale locale, String label)
 105  
     {
 106  0
         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  0
         super.renderContribution(writer, cycle, context, field);
 116  
         
 117  0
         if (_trim) {
 118  0
             JSONObject profile = context.getProfile();
 119  
             
 120  0
             accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId());
 121  
         }
 122  0
     }
 123  
 
 124  
     public boolean isTrim()
 125  
 
 126  
     {
 127  0
         return _trim;
 128  
     }
 129  
 
 130  
     public void setTrim(boolean trim)
 131  
     {
 132  0
         _trim = trim;
 133  0
     }
 134  
 
 135  
     public String getMessage()
 136  
     {
 137  0
         return _message;
 138  
     }
 139  
 
 140  
     public void setMessage(String message)
 141  
     {
 142  0
         _message = message;
 143  0
     }
 144  
 }