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 org.apache.hivemind.HiveMind;
018 import org.apache.hivemind.util.PropertyUtils;
019 import org.apache.tapestry.form.IFormComponent;
020 import org.apache.tapestry.form.ValidationMessages;
021 import org.apache.tapestry.valid.ValidationConstraint;
022 import org.apache.tapestry.valid.ValidatorException;
023
024 import java.text.Format;
025 import java.text.ParseException;
026 import java.util.Locale;
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 public FormatTranslator()
039 {
040 _pattern = defaultPattern();
041 }
042
043 //TODO: Needed until HIVEMIND-134 fix is available
044 public FormatTranslator(String initializer)
045 {
046 PropertyUtils.configureProperties(this, initializer);
047
048 if (HiveMind.isBlank(_pattern))
049 {
050 _pattern = defaultPattern();
051 }
052 }
053
054 protected abstract String defaultPattern();
055
056 /**
057 * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent,
058 * Locale, java.lang.Object)
059 */
060 protected String formatObject(IFormComponent field, Locale locale, Object object)
061 {
062 // Get a new format each time, because (a) have to account for locale and (b) formatters are
063 // not thread safe.
064
065 Format format = getFormat(locale);
066
067 return format.format(object);
068 }
069
070 /**
071 * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent,
072 * ValidationMessages, java.lang.String)
073 */
074 protected Object parseText(IFormComponent field, ValidationMessages messages, String text)
075 throws ValidatorException
076 {
077 Format format = getFormat(messages.getLocale());
078
079 try
080 {
081 return format.parseObject(text);
082 }
083 catch (ParseException ex)
084 {
085 throw new ValidatorException(buildMessage(messages, field, getMessageKey()),
086 getConstraint());
087 }
088 }
089
090 protected abstract ValidationConstraint getConstraint();
091
092 protected abstract Format getFormat(Locale locale);
093
094 protected abstract String getMessageKey();
095
096 public String getPattern()
097 {
098 return _pattern;
099 }
100
101 public void setPattern(String pattern)
102 {
103 _pattern = pattern;
104 }
105
106 /**
107 * Gets the pattern encapsulated by this translator, subclasses may optionally use the
108 * passed in {@link Locale} to return patterns specific to that locale.
109 *
110 * @param locale The locale to use to format the pattern, if applicable.
111 * @return The pattern used to format/parse objects.
112 */
113 public String getPattern(Locale locale)
114 {
115 return _pattern;
116 }
117 }