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