Clover coverage report - Code Coverage for tapestry release 4.0-beta-10
Coverage timestamp: Sat Oct 8 2005 19:08:05 EDT
file stats: LOC: 134   Methods: 12
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
AbstractTranslator.java 100% 77.8% 66.7% 78.9%
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 = _trim ? text.trim() : text;
 72   
 73  15 return HiveMind.isBlank(value) ? getEmpty() : parseText(field, messages, value);
 74    }
 75   
 76    protected abstract String formatObject(IFormComponent field, Locale locale, Object object);
 77   
 78    protected abstract Object parseText(IFormComponent field, ValidationMessages messages,
 79    String text) throws ValidatorException;
 80   
 81  1 protected Object getEmpty()
 82    {
 83  1 return null;
 84    }
 85   
 86  7 protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)
 87    {
 88  7 String label = field.getDisplayName();
 89   
 90  7 Object[] parameters = getMessageParameters(messages.getLocale(), label);
 91   
 92  7 return messages.formatValidationMessage(_message, key, parameters);
 93    }
 94   
 95  0 protected Object[] getMessageParameters(Locale locale, String label)
 96    {
 97  0 return new Object[]
 98    { label };
 99    }
 100   
 101    /**
 102    * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle,
 103    * org.apache.tapestry.form.IFormComponent)
 104    */
 105  7 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 106    FormComponentContributorContext context, IFormComponent field)
 107    {
 108  7 super.renderContribution(writer, cycle, context, field);
 109   
 110  7 if (_trim)
 111  3 context.addSubmitHandler("function (event) { Tapestry.trim_field_value('" + field.getClientId() + "'); }");
 112    }
 113   
 114  0 public boolean isTrim()
 115   
 116    {
 117  0 return _trim;
 118    }
 119   
 120  6 public void setTrim(boolean trim)
 121    {
 122  6 _trim = trim;
 123    }
 124   
 125  0 public String getMessage()
 126    {
 127  0 return _message;
 128    }
 129   
 130  3 public void setMessage(String message)
 131    {
 132  3 _message = message;
 133    }
 134    }