Coverage Report - org.apache.tapestry.form.translator.DateTranslator
 
Classes in this File Line Coverage Branch Coverage Complexity
DateTranslator
0%
0/30
0%
0/4
1.167
 
 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 org.apache.hivemind.util.PropertyUtils;
 18  
 import org.apache.tapestry.IMarkupWriter;
 19  
 import org.apache.tapestry.IRequestCycle;
 20  
 import org.apache.tapestry.form.FormComponentContributorContext;
 21  
 import org.apache.tapestry.form.IFormComponent;
 22  
 import org.apache.tapestry.json.JSONLiteral;
 23  
 import org.apache.tapestry.json.JSONObject;
 24  
 import org.apache.tapestry.valid.ValidationConstants;
 25  
 import org.apache.tapestry.valid.ValidationConstraint;
 26  
 import org.apache.tapestry.valid.ValidationStrings;
 27  
 
 28  
 import java.text.DateFormatSymbols;
 29  
 import java.text.Format;
 30  
 import java.text.SimpleDateFormat;
 31  
 import java.util.Locale;
 32  
 
 33  
 /**
 34  
  * A {@link java.text.SimpleDateFormat}-based {@link Translator} implementation.
 35  
  * 
 36  
  * @author Paul Ferraro
 37  
  * @since 4.0
 38  
  */
 39  
 public class DateTranslator extends FormatTranslator
 40  
 {
 41  0
     private boolean _lenient=true;
 42  
 
 43  0
     protected SimpleDateFormat _rfc339Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
 44  
     
 45  
     public DateTranslator()
 46  0
     {
 47  0
     }
 48  
     
 49  
     // Needed until HIVEMIND-134 fix is available
 50  
     public DateTranslator(String initializer)
 51  0
     {
 52  0
         PropertyUtils.configureProperties(this, initializer);
 53  0
     }
 54  
     
 55  
     /**
 56  
      * @see org.apache.tapestry.form.translator.FormatTranslator#defaultPattern()
 57  
      */
 58  
     protected String defaultPattern()
 59  
     {
 60  0
         return "MM/dd/yyyy";
 61  
     }
 62  
     
 63  
     /**
 64  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getFormat(java.util.Locale)
 65  
      */
 66  
     protected Format getFormat(Locale locale)
 67  
     {
 68  0
         return getDateFormat(locale);
 69  
     }
 70  
 
 71  
     /**
 72  
      * Get the RFC339 equivalent for the given object.
 73  
      *
 74  
      * @param input The object to be formatted.
 75  
      * 
 76  
      * @return A string value compliant with rfc339 internet time.
 77  
      */
 78  
     public String formatRfc3339(Object input)
 79  
     {
 80  0
         return _rfc339Format.format(input);
 81  
     }
 82  
 
 83  
     public SimpleDateFormat getDateFormat(Locale locale)
 84  
     {
 85  0
         SimpleDateFormat ret = new SimpleDateFormat(getPattern(), new DateFormatSymbols(locale));
 86  0
         ret.setLenient(_lenient);
 87  
         
 88  0
         return ret;
 89  
     }
 90  
     
 91  
     /**
 92  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getMessageKey()
 93  
      */
 94  
     protected String getMessageKey()
 95  
     {
 96  0
         return ValidationStrings.INVALID_DATE;
 97  
     }
 98  
     
 99  
     /**
 100  
      * @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
 101  
      *      java.lang.String)
 102  
      */
 103  
     protected Object[] getMessageParameters(Locale locale, String label)
 104  
     {
 105  0
         String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale);
 106  
         
 107  0
         return new Object[] { label, pattern };
 108  
     }
 109  
     
 110  
     /**
 111  
      * 
 112  
      * {@inheritDoc}
 113  
      */
 114  
     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
 115  
             FormComponentContributorContext context, IFormComponent field)
 116  
     {
 117  0
         super.renderContribution(writer, cycle, context, field);
 118  
         
 119  0
         String message = buildMessage(context, field, getMessageKey());
 120  
         
 121  0
         JSONObject profile = context.getProfile();
 122  0
         if (!profile.has(ValidationConstants.CONSTRAINTS)) {
 123  0
             profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
 124  
         }
 125  
         
 126  0
         JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
 127  
         
 128  0
         context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");");
 129  
         
 130  0
         accumulateProperty(cons, field.getClientId(), 
 131  
                 new JSONLiteral("[tapestry.form.datetime.isValidDate,{"
 132  
                         + "datePattern:" 
 133  
                         + JSONObject.quote(getPattern())
 134  
                         + (isLenient() ? "" : ",strict:true")
 135  
                         + "}]"));
 136  
         
 137  0
         accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, message);
 138  0
     }
 139  
     
 140  
     /**
 141  
      * @see org.apache.tapestry.form.translator.FormatTranslator#getConstraint()
 142  
      */
 143  
     protected ValidationConstraint getConstraint()
 144  
     {
 145  0
         return ValidationConstraint.DATE_FORMAT;
 146  
     }
 147  
     
 148  
     public void setLenient(boolean value)
 149  
     {
 150  0
         _lenient = value;
 151  0
     }
 152  
     
 153  
     public boolean isLenient()
 154  
     {
 155  0
         return _lenient;
 156  
     }
 157  
 }