View Javadoc

1   /* Copyright 2004 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  
16  package guessNumber;
17  
18  import java.util.Locale;
19  import java.util.ResourceBundle;
20  import java.util.MissingResourceException;
21  import javax.faces.application.Application;
22  import javax.faces.application.FacesMessage;
23  import javax.faces.context.FacesContext;
24  import java.text.MessageFormat;
25  
26  /***
27   * 
28   * <p>supported filters: <code>package</code> and
29   * <code>protection</code>.</p>
30   */
31  
32  public class MessageFactory extends Object
33  {
34      //
35      // Protected Constants
36      //
37  
38      //
39      // Class Variables
40      //
41  
42      //
43      // Instance Variables
44      //
45  
46      // Attribute Instance Variables
47  
48      // Relationship Instance Variables
49  
50      //
51      // Constructors and Initializers    
52      //
53  
54      private MessageFactory() {
55      }
56  
57      //
58      // Class methods
59      //
60  
61      //
62      // General Methods
63      //
64      
65      public static String substituteParams(Locale locale, String msgtext, Object params[]) {
66          String localizedStr = null;
67          
68          if (params == null || msgtext == null ) {
69              return msgtext;
70          }    
71          StringBuffer b = new StringBuffer(100);
72          MessageFormat mf = new MessageFormat(msgtext);
73          if (locale != null) {
74              mf.setLocale(locale);
75              b.append(mf.format(params));
76              localizedStr = b.toString();
77          }    
78          return localizedStr;
79      }
80  
81      /***
82  
83      * This version of getMessage() is used in the RI for localizing RI
84      * specific messages.
85  
86      */
87  
88      public static FacesMessage getMessage(String messageId, Object params[]) {
89          Locale locale = null;
90          FacesContext context = FacesContext.getCurrentInstance();
91          // context.getViewRoot() may not have been initialized at this point.
92          if (context != null && context.getViewRoot() != null) {
93              locale = context.getViewRoot().getLocale();
94              if (locale == null) {
95                  locale = Locale.getDefault();
96              }
97          } else {
98              locale = Locale.getDefault();
99          }
100         
101 	return getMessage(locale, messageId, params);
102     }
103 
104     public static FacesMessage getMessage(Locale locale, String messageId, 
105 					   Object params[]) {
106 	FacesMessage result = null;
107 	String 
108 	    summary = null,
109 	    detail = null,
110 	    bundleName = null;
111 	ResourceBundle bundle = null;
112 
113 	// see if we have a user-provided bundle
114 	if (null != (bundleName = getApplication().getMessageBundle())) {
115 	    if (null != 
116 		(bundle = 
117 		 ResourceBundle.getBundle(bundleName, locale,
118 					  getCurrentLoader(bundleName)))) {
119 		// see if we have a hit
120 		try {
121 		    summary = bundle.getString(messageId);
122 		}
123 		catch (MissingResourceException e) {
124 		}
125 	    }
126 	}
127 	
128 	// we couldn't find a summary in the user-provided bundle
129 	if (null == summary) {
130 	    // see if we have a summary in the app provided bundle
131 	    bundle = ResourceBundle.getBundle(FacesMessage.FACES_MESSAGES, 
132 					      locale,
133 					      getCurrentLoader(bundleName));
134 	    if (null == bundle) {
135 		throw new NullPointerException();
136 	    }
137 	    // see if we have a hit
138 	    try {
139 		summary = bundle.getString(messageId);
140 	    }
141 	    catch (MissingResourceException e) {
142 	    }
143 	}
144 	
145 	// we couldn't find a summary anywhere!  Return null
146 	if (null == summary) {
147 	    return null;
148 	}
149 
150 	// At this point, we have a summary and a bundle.
151 	if (null == summary || null == bundle) {
152 	    throw new NullPointerException();
153 	}
154 	summary = substituteParams(locale, summary, params);
155 
156 	try {
157 	    detail = substituteParams(locale,
158 				      bundle.getString(messageId + "_detail"), 
159 				      params);
160 	}
161 	catch (MissingResourceException e) {
162 	}
163 
164         return (new FacesMessage(summary, detail));
165     }
166 
167 
168     //
169     // Methods from MessageFactory
170     // 
171     public static FacesMessage getMessage(FacesContext context, String messageId) {
172         return getMessage(context, messageId, null);
173     }    
174     
175     public static FacesMessage getMessage(FacesContext context, String messageId,
176 					   Object params[]) {
177         if (context == null || messageId == null ) {
178             throw new NullPointerException("One or more parameters could be null");
179         }
180         Locale locale = null;
181         // viewRoot may not have been initialized at this point.
182         if (context != null && context.getViewRoot() != null) {
183             locale = context.getViewRoot().getLocale();
184         } else {
185             locale = Locale.getDefault();
186         }
187 	if (null == locale) {
188 	    throw new NullPointerException();
189 	}
190         FacesMessage message = getMessage(locale, messageId, params);
191         if (message != null) {
192             return message;
193         }
194         locale = Locale.getDefault();
195         return (getMessage(locale, messageId, params));
196     }  
197     
198     public static FacesMessage getMessage(FacesContext context, String messageId,
199                                        Object param0) {
200         return getMessage(context, messageId, new Object[]{param0});                                       
201     }                                       
202     
203     public static FacesMessage getMessage(FacesContext context, String messageId,
204                                        Object param0, Object param1) {
205          return getMessage(context, messageId, new Object[]{param0, param1});                                        
206     }                                       
207 
208     public static FacesMessage getMessage(FacesContext context, String messageId,
209                                        Object param0, Object param1,
210                                        Object param2) {
211          return getMessage(context, messageId, 
212              new Object[]{param0, param1, param2});                                        
213     }                                       
214 
215     public static FacesMessage getMessage(FacesContext context, String messageId,
216                                        Object param0, Object param1,
217                                        Object param2, Object param3) {
218          return getMessage(context, messageId, 
219                  new Object[]{param0, param1, param2, param3});                                        
220     }                                       
221 
222     protected static Application getApplication() {
223         return (FacesContext.getCurrentInstance().getApplication());
224     }
225 
226     protected static ClassLoader getCurrentLoader(Object fallbackClass) {
227         ClassLoader loader =
228 	    Thread.currentThread().getContextClassLoader();
229 	if (loader == null) {
230 	    loader = fallbackClass.getClass().getClassLoader();
231 	}
232 	return loader;
233     }
234 
235 
236 } // end of class MessageFactory