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: 105   Methods: 6
NCLOC: 57   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FormatTranslator.java 50% 91.7% 100% 90%
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.text.Format;
 18    import java.text.ParseException;
 19    import java.util.Locale;
 20   
 21    import org.apache.hivemind.HiveMind;
 22    import org.apache.hivemind.util.PropertyUtils;
 23    import org.apache.tapestry.form.IFormComponent;
 24    import org.apache.tapestry.form.ValidationMessages;
 25    import org.apache.tapestry.valid.ValidationConstraint;
 26    import org.apache.tapestry.valid.ValidatorException;
 27   
 28    /**
 29    * Abstract {@link Translator} implementation for {@link java.text.Format}-based translators.
 30    *
 31    * @author Paul Ferraro
 32    * @since 4.0
 33    */
 34    public abstract class FormatTranslator extends AbstractTranslator
 35    {
 36    private String _pattern;
 37   
 38    protected abstract String defaultPattern();
 39   
 40    /**
 41    * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent,
 42    * Locale, java.lang.Object)
 43    */
 44  7 protected String formatObject(IFormComponent field, Locale locale, Object object)
 45    {
 46    // Get a new format each time, because (a) have to account for locale and (b) formatters are
 47    // not thread safe.
 48   
 49  7 Format format = getFormat(locale);
 50   
 51  7 return format.format(object);
 52    }
 53   
 54    /**
 55    * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent,
 56    * ValidationMessages, java.lang.String)
 57    */
 58  10 protected Object parseText(IFormComponent field, ValidationMessages messages, String text)
 59    throws ValidatorException
 60    {
 61  10 Format format = getFormat(messages.getLocale());
 62   
 63  10 try
 64    {
 65  10 return format.parseObject(text);
 66    }
 67    catch (ParseException ex)
 68    {
 69  4 throw new ValidatorException(buildMessage(messages, field, getMessageKey()),
 70    getConstraint());
 71    }
 72    }
 73   
 74    protected abstract ValidationConstraint getConstraint();
 75   
 76    protected abstract Format getFormat(Locale locale);
 77   
 78    protected abstract String getMessageKey();
 79   
 80  24 public String getPattern()
 81    {
 82  24 return _pattern;
 83    }
 84   
 85  8 public void setPattern(String pattern)
 86    {
 87  8 _pattern = pattern;
 88    }
 89   
 90  25 public FormatTranslator()
 91    {
 92  25 _pattern = defaultPattern();
 93    }
 94   
 95    // Needed until HIVEMIND-134 fix is available
 96  1 public FormatTranslator(String initializer)
 97    {
 98  1 PropertyUtils.configureProperties(this, initializer);
 99   
 100  1 if (HiveMind.isBlank(_pattern))
 101    {
 102  0 _pattern = defaultPattern();
 103    }
 104    }
 105    }