View Javadoc

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