Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 143   Methods: 12
NCLOC: 78   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractTranslator.java 90% 77.8% 66.7% 77.5%
coverage coverage
 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 java.util.Locale;
 18   
 19    import org.apache.hivemind.HiveMind;
 20    import org.apache.tapestry.IMarkupWriter;
 21    import org.apache.tapestry.IRequestCycle;
 22    import org.apache.tapestry.form.AbstractFormComponentContributor;
 23    import org.apache.tapestry.form.FormComponentContributorContext;
 24    import org.apache.tapestry.form.IFormComponent;
 25    import org.apache.tapestry.form.ValidationMessages;
 26    import org.apache.tapestry.valid.ValidatorException;
 27   
 28    /**
 29    * Abstract {@link Translator} implementation that provides default behavior for trimming, null
 30    * object, and empty text handling.
 31    *
 32    * @author Paul Ferraro
 33    * @since 4.0
 34    */
 35    public abstract class AbstractTranslator extends AbstractFormComponentContributor implements
 36    Translator
 37    {
 38    private boolean _trim;
 39   
 40    private String _message;
 41   
 42  35 public AbstractTranslator()
 43    {
 44    }
 45   
 46    // Needed until HIVEMIND-134 fix is available
 47  0 public AbstractTranslator(String initializer)
 48    {
 49  0 super(initializer);
 50    }
 51   
 52    /**
 53    * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent,
 54    * Locale, java.lang.Object)
 55    */
 56  12 public String format(IFormComponent field, Locale locale, Object object)
 57    {
 58  12 if (object == null)
 59  3 return "";
 60   
 61  9 return formatObject(field, locale, object);
 62    }
 63   
 64    /**
 65    * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent,
 66    * ValidationMessages, java.lang.String)
 67    */
 68  15 public Object parse(IFormComponent field, ValidationMessages messages, String text)
 69    throws ValidatorException
 70    {
 71  15 String value = text == null ? null : (_trim ? text.trim() : text);
 72   
 73  15 return HiveMind.isBlank(value) ? getValueForEmptyInput()
 74    : parseText(field, messages, value);
 75    }
 76   
 77    protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
 78   
 79    protected abstract Object parseText(IFormComponent field, ValidationMessages messages,
 80    String text) throws ValidatorException;
 81   
 82    /**
 83    * The value to be used when the value supplied in the request is blank (null or empty). The
 84    * default value is null, but some subclasses may override.
 85    *
 86    * @see #parse(IFormComponent, ValidationMessages, String)
 87    * @return null, subclasses may override
 88    */
 89  1 protected Object getValueForEmptyInput()
 90    {
 91  1 return null;
 92    }
 93   
 94  7 protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
 95    {
 96  7 String label = field.getDisplayName();
 97   
 98  7 Object[] parameters = getMessageParameters(messages.getLocale(), label);
 99   
 100  7 return messages.formatValidationMessage(_message, key, parameters);
 101    }
 102   
 103  0 protected Object[] getMessageParameters(Locale locale, String label)
 104    {
 105  0 return new Object[]
 106    { label };
 107    }
 108   
 109    /**
 110    * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle,
 111    * org.apache.tapestry.form.IFormComponent)
 112    */
 113  7 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 114    FormComponentContributorContext context, IFormComponent field)
 115    {
 116  7 super.renderContribution(writer, cycle, context, field);
 117   
 118  7 if (_trim)
 119  3 context.addSubmitHandler("function (event) { Tapestry.trim_field_value('"
 120    + field.getClientId() + "'); }");
 121    }
 122   
 123  0 public boolean isTrim()
 124   
 125    {
 126  0 return _trim;
 127    }
 128   
 129  6 public void setTrim(boolean trim)
 130    {
 131  6 _trim = trim;
 132    }
 133   
 134  0 public String getMessage()
 135    {
 136  0 return _message;
 137    }
 138   
 139  3 public void setMessage(String message)
 140    {
 141  3 _message = message;
 142    }
 143    }