001 // Copyright 2005 The Apache Software Foundation 002 // 003 // Licensed under the Apache License, Version 2.0 (the "License"); 004 // you may not use this file except in compliance with the License. 005 // You may obtain a copy of the License at 006 // 007 // http://www.apache.org/licenses/LICENSE-2.0 008 // 009 // Unless required by applicable law or agreed to in writing, software 010 // distributed under the License is distributed on an "AS IS" BASIS, 011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 012 // See the License for the specific language governing permissions and 013 // limitations under the License. 014 015 package org.apache.tapestry.form.translator; 016 017 import java.text.Format; 018 import java.text.ParseException; 019 import java.util.Locale; 020 021 import org.apache.hivemind.HiveMind; 022 import org.apache.hivemind.util.PropertyUtils; 023 import org.apache.tapestry.form.IFormComponent; 024 import org.apache.tapestry.form.ValidationMessages; 025 import org.apache.tapestry.valid.ValidationConstraint; 026 import org.apache.tapestry.valid.ValidatorException; 027 028 /** 029 * Abstract {@link Translator} implementation for {@link java.text.Format}-based translators. 030 * 031 * @author Paul Ferraro 032 * @since 4.0 033 */ 034 public abstract class FormatTranslator extends AbstractTranslator 035 { 036 private String _pattern; 037 038 protected abstract String defaultPattern(); 039 040 /** 041 * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent, 042 * Locale, java.lang.Object) 043 */ 044 protected String formatObject(IFormComponent field, Locale locale, Object object) 045 { 046 // Get a new format each time, because (a) have to account for locale and (b) formatters are 047 // not thread safe. 048 049 Format format = getFormat(locale); 050 051 return format.format(object); 052 } 053 054 /** 055 * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent, 056 * ValidationMessages, java.lang.String) 057 */ 058 protected Object parseText(IFormComponent field, ValidationMessages messages, String text) 059 throws ValidatorException 060 { 061 Format format = getFormat(messages.getLocale()); 062 063 try 064 { 065 return format.parseObject(text); 066 } 067 catch (ParseException ex) 068 { 069 throw new ValidatorException(buildMessage(messages, field, getMessageKey()), 070 getConstraint()); 071 } 072 } 073 074 protected abstract ValidationConstraint getConstraint(); 075 076 protected abstract Format getFormat(Locale locale); 077 078 protected abstract String getMessageKey(); 079 080 public String getPattern() 081 { 082 return _pattern; 083 } 084 085 public void setPattern(String pattern) 086 { 087 _pattern = pattern; 088 } 089 090 public FormatTranslator() 091 { 092 _pattern = defaultPattern(); 093 } 094 095 // Needed until HIVEMIND-134 fix is available 096 public FormatTranslator(String initializer) 097 { 098 PropertyUtils.configureProperties(this, initializer); 099 100 if (HiveMind.isBlank(_pattern)) 101 { 102 _pattern = defaultPattern(); 103 } 104 } 105 }