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: 125   Methods: 10
NCLOC: 63   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
NumberTranslator.java - 100% 100% 100%
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.DecimalFormat;
 18    import java.text.DecimalFormatSymbols;
 19    import java.text.Format;
 20    import java.util.Locale;
 21   
 22    import org.apache.hivemind.util.StringUtils;
 23    import org.apache.tapestry.IForm;
 24    import org.apache.tapestry.IMarkupWriter;
 25    import org.apache.tapestry.IRequestCycle;
 26    import org.apache.tapestry.form.FormComponentContributorContext;
 27    import org.apache.tapestry.form.IFormComponent;
 28    import org.apache.tapestry.valid.ValidationConstraint;
 29    import org.apache.tapestry.valid.ValidationStrings;
 30   
 31    /**
 32    * A {@link java.text.DecimalFormat}-based {@link Translator} implementation.
 33    *
 34    * @author Paul Ferraro
 35    * @since 4.0
 36    */
 37    public class NumberTranslator extends FormatTranslator
 38    {
 39   
 40  11 public NumberTranslator()
 41    {
 42    }
 43   
 44    // Needed until HIVEMIND-134 fix is available
 45  1 public NumberTranslator(String initializer)
 46    {
 47  1 super(initializer);
 48    }
 49   
 50    /**
 51    * @see org.apache.tapestry.form.AbstractFormComponentContributor#defaultScript()
 52    */
 53  12 protected String defaultScript()
 54    {
 55  12 return "/org/apache/tapestry/form/translator/NumberTranslator.js";
 56    }
 57   
 58    /**
 59    * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
 60    */
 61  11 protected String defaultPattern()
 62    {
 63  11 return "#";
 64    }
 65   
 66    /**
 67    * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
 68    */
 69  8 protected Format getFormat(Locale locale)
 70    {
 71  8 return getDecimalFormat(locale);
 72    }
 73   
 74  13 public DecimalFormat getDecimalFormat(Locale locale)
 75    {
 76  13 return new DecimalFormat(getPattern(), new DecimalFormatSymbols(locale));
 77    }
 78   
 79    /**
 80    * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
 81    */
 82  5 protected String getMessageKey()
 83    {
 84  5 return ValidationStrings.INVALID_NUMBER;
 85    }
 86   
 87    /**
 88    * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
 89    * java.lang.String)
 90    */
 91  5 protected Object[] getMessageParameters(Locale locale, String label)
 92    {
 93  5 String pattern = getDecimalFormat(locale).toLocalizedPattern();
 94   
 95  5 return new Object[]
 96    { label, pattern };
 97    }
 98   
 99    /**
 100    * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter,
 101    * org.apache.tapestry.IRequestCycle, FormComponentContributorContext, org.apache.tapestry.form.IFormComponent)
 102    */
 103  3 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field)
 104    {
 105  3 super.renderContribution(writer, cycle, context, field);
 106   
 107  3 String message = buildMessage(field, getMessageKey());
 108  3 IForm form = field.getForm();
 109   
 110    // Escape backslashes and single quotes in the message
 111  3 message = StringUtils.replace(message, "\\", "\\\\");
 112  3 message = StringUtils.replace(message, "'", "\\'");
 113   
 114  3 addSubmitHandler(form, "validate_number(event, document." + form.getName() + "."
 115    + field.getName() + ",'" + message + "')");
 116    }
 117   
 118    /**
 119    * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
 120    */
 121  2 protected ValidationConstraint getConstraint()
 122    {
 123  2 return ValidationConstraint.NUMBER_FORMAT;
 124    }
 125    }