|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
FormatTranslator.java | - | 88.9% | 83.3% | 86.7% |
|
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; | |
18 | import java.text.ParseException; | |
19 | import java.util.Locale; | |
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 _pattern = defaultPattern(); | |
34 | ||
35 | protected abstract String defaultPattern(); | |
36 | ||
37 | /** | |
38 | * @see org.apache.tapestry.form.translator.AbstractTranslator#formatObject(org.apache.tapestry.form.IFormComponent, | |
39 | * java.lang.Object) | |
40 | */ | |
41 | 4 | protected String formatObject(IFormComponent field, Object 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 | 4 | Format format = getFormat(field.getPage().getLocale()); |
47 | ||
48 | 4 | 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 | 10 | protected Object parseText(IFormComponent field, String text) throws ValidatorException |
56 | { | |
57 | 10 | Format format = getFormat(field.getPage().getLocale()); |
58 | ||
59 | 10 | try |
60 | { | |
61 | 10 | return format.parseObject(text); |
62 | } | |
63 | catch (ParseException e) | |
64 | { | |
65 | 4 | throw new ValidatorException(buildMessage(field, getMessageKey()), getConstraint()); |
66 | } | |
67 | } | |
68 | ||
69 | protected abstract ValidationConstraint getConstraint(); | |
70 | ||
71 | protected abstract Format getFormat(Locale locale); | |
72 | ||
73 | protected abstract String getMessageKey(); | |
74 | ||
75 | 21 | public String getPattern() |
76 | { | |
77 | 21 | return _pattern; |
78 | } | |
79 | ||
80 | 4 | public void setPattern(String pattern) |
81 | { | |
82 | 4 | _pattern = pattern; |
83 | } | |
84 | ||
85 | 22 | public FormatTranslator() |
86 | { | |
87 | } | |
88 | ||
89 | // Needed until HIVEMIND-134 fix is available | |
90 | 0 | public FormatTranslator(String initializer) |
91 | { | |
92 | 0 | super(initializer); |
93 | } | |
94 | } |
|