Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
FormatTranslator |
|
| 1.3636363636363635;1.364 |
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.HiveMind; |
|
18 | import org.apache.hivemind.util.PropertyUtils; |
|
19 | import org.apache.tapestry.form.IFormComponent; |
|
20 | import org.apache.tapestry.form.ValidationMessages; |
|
21 | import org.apache.tapestry.valid.ValidationConstraint; |
|
22 | import org.apache.tapestry.valid.ValidatorException; |
|
23 | ||
24 | import java.text.Format; |
|
25 | import java.text.ParseException; |
|
26 | import java.util.Locale; |
|
27 | ||
28 | /** |
|
29 | * Abstract {@link Translator} implementation for {@link java.text.Format}-based translators. |
|
30 | * |
|
31 | * @author Paul Ferraro |
|
32 | * @since 4.0 |
|
33 | */ |
|
34 | public abstract class FormatTranslator extends AbstractTranslator |
|
35 | { |
|
36 | private String _pattern; |
|
37 | ||
38 | public FormatTranslator() |
|
39 | 35 | { |
40 | 35 | _pattern = defaultPattern(); |
41 | 35 | } |
42 | ||
43 | //TODO: Needed until HIVEMIND-134 fix is available |
|
44 | public FormatTranslator(String initializer) |
|
45 | 0 | { |
46 | 0 | PropertyUtils.configureProperties(this, initializer); |
47 | ||
48 | 0 | if (HiveMind.isBlank(_pattern)) |
49 | { |
|
50 | 0 | _pattern = defaultPattern(); |
51 | } |
|
52 | 0 | } |
53 | ||
54 | protected abstract String defaultPattern(); |
|
55 | ||
56 | /** |
|
57 | * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent, |
|
58 | * Locale, java.lang.Object) |
|
59 | */ |
|
60 | protected String formatObject(IFormComponent field, Locale locale, Object object) |
|
61 | { |
|
62 | // Get a new format each time, because (a) have to account for locale and (b) formatters are |
|
63 | // not thread safe. |
|
64 | ||
65 | 20 | Format format = getFormat(locale); |
66 | ||
67 | 20 | return format.format(object); |
68 | } |
|
69 | ||
70 | /** |
|
71 | * @see org.apache.tapestry.form.translator.AbstractTranslator#parseText(org.apache.tapestry.form.IFormComponent, |
|
72 | * ValidationMessages, java.lang.String) |
|
73 | */ |
|
74 | protected Object parseText(IFormComponent field, ValidationMessages messages, String text) |
|
75 | throws ValidatorException |
|
76 | { |
|
77 | 11 | Format format = getFormat(messages.getLocale()); |
78 | ||
79 | try |
|
80 | { |
|
81 | 11 | return format.parseObject(text); |
82 | } |
|
83 | 4 | catch (ParseException ex) |
84 | { |
|
85 | 4 | throw new ValidatorException(buildMessage(messages, field, getMessageKey()), |
86 | getConstraint()); |
|
87 | } |
|
88 | } |
|
89 | ||
90 | protected abstract ValidationConstraint getConstraint(); |
|
91 | ||
92 | protected abstract Format getFormat(Locale locale); |
|
93 | ||
94 | protected abstract String getMessageKey(); |
|
95 | ||
96 | public String getPattern() |
|
97 | { |
|
98 | 57 | return _pattern; |
99 | } |
|
100 | ||
101 | public void setPattern(String pattern) |
|
102 | { |
|
103 | 13 | _pattern = pattern; |
104 | 13 | } |
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 | 2 | return _pattern; |
116 | } |
|
117 | } |