KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > FormatHandlerSupport


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.format;
14
15 import java.text.MessageFormat JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Locale JavaDoc;
19
20
21 /**
22  * base class for FormatHandlers
23  */

24 public abstract class FormatHandlerSupport implements FormatHandler {
25   protected String JavaDoc name;
26   protected String JavaDoc pattern;
27   protected String JavaDoc errorMessage;
28   protected ArrayList JavaDoc patterns = new ArrayList JavaDoc();
29   protected Locale JavaDoc locale;
30
31   /**
32    * adds a pattern for a specific locale
33    */

34   public void addPattern(Pattern p) {
35     patterns.add(p);
36   }
37
38   /**
39    * returns the pattern for a given locale. First it checks if a Pattern child
40    * exists if a child exists with the same language as locale. If not, the pattern
41    * property of this is returned.
42    * @param userPattern a pattern that the user may have provided. If not null
43    * and not empty, the userPattern will be returned.
44    */

45   protected String JavaDoc findPattern(String JavaDoc userPattern) {
46     
47     if (userPattern != null && userPattern.length() > 0)
48       return userPattern;
49       
50     Iterator JavaDoc it = patterns.iterator();
51
52     while (it.hasNext()) {
53       Pattern p = (Pattern) it.next();
54
55       if (locale.getLanguage().equals(p.getLanguage())) {
56         return p.getPattern();
57       }
58     }
59
60     return this.getPattern();
61   }
62
63   protected String JavaDoc getErrorMessage(String JavaDoc userInput) {
64     String JavaDoc errorMessage = null;
65
66     Iterator JavaDoc it = patterns.iterator();
67
68     while (it.hasNext()) {
69       Pattern p = (Pattern) it.next();
70
71       if (locale.getLanguage().equals(p.getLanguage())) {
72         errorMessage = p.getErrorMessage();
73       }
74     }
75
76     // no locale specific error message found?
77
if (errorMessage == null) {
78       errorMessage = getErrorMessage();
79     }
80
81     if (errorMessage == null) {
82       return userInput;
83     }
84
85     return MessageFormat.format(errorMessage, new Object JavaDoc[] { userInput });
86   }
87
88   /**
89    * Returns the name.
90    * @return String
91    */

92   public String JavaDoc getName() {
93     return name;
94   }
95
96   /**
97    * Returns the default pattern
98    * @return String
99    */

100   public String JavaDoc getPattern() {
101     return pattern;
102   }
103
104   /**
105    * Sets the name.
106    * @param name The name to set
107    */

108   public void setName(String JavaDoc name) {
109     this.name = name;
110   }
111
112   /**
113    * Sets the default pattern.
114    * @param pattern The pattern to set
115    */

116   public void setPattern(String JavaDoc pattern) {
117     this.pattern = pattern;
118   }
119
120   /**
121    * Returns the errorMessage.
122    * @return String
123    */

124   public String JavaDoc getErrorMessage() {
125     return errorMessage;
126   }
127
128   /**
129    * Sets the errorMessage.
130    * @param errorMessage The errorMessage to set
131    */

132   public void setErrorMessage(String JavaDoc errorMessage) {
133     this.errorMessage = errorMessage;
134   }
135
136   /**
137    * Returns the locale.
138    * @return Locale
139    */

140   public Locale JavaDoc getLocale() {
141     return locale;
142   }
143
144   /**
145    * Sets the locale.
146    * @param locale The locale to set
147    */

148   public void setLocale(Locale JavaDoc locale) {
149     this.locale = locale;
150   }
151 }
Popular Tags