KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > translator > FormatTranslator


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 JavaDoc;
18 import java.text.ParseException JavaDoc;
19 import java.util.Locale JavaDoc;
20
21 import org.apache.tapestry.form.IFormComponent;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidatorException;
24
25 /**
26  * Abstract {@link Translator} implementation for {@link java.text.Format}-based translators.
27  *
28  * @author Paul Ferraro
29  * @since 4.0
30  */

31 public abstract class FormatTranslator extends AbstractTranslator
32 {
33     private String JavaDoc _pattern = defaultPattern();
34
35     protected abstract String JavaDoc defaultPattern();
36
37     /**
38      * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent,
39      * java.lang.Object)
40      */

41     protected String JavaDoc formatObject(IFormComponent field, Object JavaDoc object)
42     {
43         // Get a new format each time, because (a) have to account for locale and (b) formatters are
44
// not thread safe.
45

46         Format JavaDoc format = getFormat(field.getPage().getLocale());
47
48         return format.format(object);
49     }
50
51     /**
52      * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent,
53      * java.lang.String)
54      */

55     protected Object JavaDoc parseText(IFormComponent field, String JavaDoc text) throws ValidatorException
56     {
57         Format JavaDoc format = getFormat(field.getPage().getLocale());
58
59         try
60         {
61             return format.parseObject(text);
62         }
63         catch (ParseException JavaDoc e)
64         {
65             throw new ValidatorException(buildMessage(field, getMessageKey()), getConstraint());
66         }
67     }
68
69     protected abstract ValidationConstraint getConstraint();
70
71     protected abstract Format JavaDoc getFormat(Locale JavaDoc locale);
72
73     protected abstract String JavaDoc getMessageKey();
74
75     public String JavaDoc getPattern()
76     {
77         return _pattern;
78     }
79
80     public void setPattern(String JavaDoc pattern)
81     {
82         _pattern = pattern;
83     }
84
85     public FormatTranslator()
86     {
87     }
88
89     // Needed until HIVEMIND-134 fix is available
90
public FormatTranslator(String JavaDoc initializer)
91     {
92         super(initializer);
93     }
94 }
95
Popular Tags