View Javadoc

1   /*
2    * $Id: PageConfiguration.java,v 1.4 2007/04/20 13:43:05 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.tools;
5   
6   import java.util.Locale;
7   import java.util.Map;
8   import java.util.TimeZone;
9   
10  import javax.faces.FacesException;
11  import javax.faces.component.UIComponent;
12  import javax.faces.component.UIViewRoot;
13  import javax.faces.context.FacesContext;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.rcfaces.core.component.capability.ILiteralLocaleCapability;
18  import org.rcfaces.core.internal.Constants;
19  import org.rcfaces.core.internal.capability.IPageConfigurator;
20  import org.rcfaces.core.internal.converter.LocaleConverter;
21  import org.rcfaces.core.internal.renderkit.AbstractProcessContext;
22  import org.rcfaces.core.internal.renderkit.IProcessContext;
23  
24  /**
25   * 
26   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
27   * @version $Revision: 1.4 $ $Date: 2007/04/20 13:43:05 $
28   */
29  public class PageConfiguration {
30      private static final String REVISION = "$Revision: 1.4 $";
31  
32      private static final Log LOG = LogFactory.getLog(PageConfiguration.class);
33  
34      private static final String SCRIPT_TYPE_PROPERTY = "org.rcfaces.core.internal.PageConfiguration.SCRIPT_TYPE";
35  
36      private static final String LITERAL_LOCALE_PROPERTY = "org.rcfaces.core.internal.PageConfiguration.LITERAL_LOCALE";
37  
38      private static final String LITERAL_LOCALE_PARAMETER = Constants
39              .getPackagePrefix()
40              + ".LITERAL_LOCALE";
41  
42      public static void setPageConfigurator(FacesContext facesContext,
43              IPageConfigurator pageConfigurator) {
44  
45          String pageScriptType = pageConfigurator.getPageScriptType();
46          if (pageScriptType != null) {
47              setAttribute(facesContext, SCRIPT_TYPE_PROPERTY, pageScriptType);
48          }
49  
50          Locale locale = pageConfigurator.getLiteralLocale();
51          if (locale != null) {
52              setAttribute(facesContext, LITERAL_LOCALE_PROPERTY, locale);
53          }
54      }
55  
56      private static void setAttribute(FacesContext facesContext, String name,
57              Object value) {
58  
59          if (facesContext == null) {
60              facesContext = FacesContext.getCurrentInstance();
61          }
62  
63          Map requestMap = facesContext.getExternalContext().getRequestMap();
64          requestMap.put(name, value);
65  
66          UIViewRoot viewRoot = facesContext.getViewRoot();
67          if (viewRoot != null) {
68              Map viewAttributes = viewRoot.getAttributes();
69  
70              viewAttributes.put(name, value);
71          }
72      }
73  
74      private static Object getAttribute(FacesContext facesContext, String name) {
75  
76          if (facesContext == null) {
77              facesContext = FacesContext.getCurrentInstance();
78          }
79  
80          Map requestMap = facesContext.getExternalContext().getRequestMap();
81          Object value = requestMap.get(name);
82          if (value != null) {
83              return value;
84          }
85  
86          UIViewRoot viewRoot = facesContext.getViewRoot();
87          if (viewRoot != null) {
88              Map viewAttributes = viewRoot.getAttributes();
89  
90              value = viewAttributes.get(name);
91              if (value != null) {
92                  return value;
93              }
94          }
95  
96          return null;
97      }
98  
99      private static final IPageConfigurator getPageConfiguration(
100             FacesContext facesContext) {
101         UIViewRoot viewRoot = facesContext.getViewRoot();
102         if (viewRoot == null) {
103             return null;
104         }
105 
106         return (IPageConfigurator) ComponentTools.findComponent(viewRoot,
107                 IPageConfigurator.class);
108     }
109 
110     public static final String getScriptType(FacesContext facesContext) {
111 
112         if (facesContext == null) {
113             facesContext = FacesContext.getCurrentInstance();
114         }
115 
116         String scriptType = (String) getAttribute(facesContext,
117                 SCRIPT_TYPE_PROPERTY);
118 
119         IPageConfigurator scriptTypeConfigurator = getPageConfiguration(facesContext);
120         if (scriptTypeConfigurator != null) {
121             scriptType = scriptTypeConfigurator.getPageScriptType();
122 
123             if (scriptType != null) {
124                 if (LOG.isDebugEnabled()) {
125                     LOG.debug("Script type detected = " + scriptType);
126                 }
127 
128                 setAttribute(facesContext, SCRIPT_TYPE_PROPERTY, scriptType);
129                 return scriptType;
130             }
131         }
132 
133         if (LOG.isDebugEnabled()) {
134             LOG.debug("No script type detected !");
135         }
136 
137         return null;
138     }
139 
140     public static Locale getLiteralLocale(IProcessContext processContext,
141             UIComponent original) {
142 
143         UIComponent component = original;
144 
145         Locale locale = null;
146         for (; component != null; component = component.getParent()) {
147 
148             if (component instanceof ILiteralLocaleCapability) {
149 
150                 locale = ((ILiteralLocaleCapability) component)
151                         .getLiteralLocale();
152                 if (locale != null) {
153                     return locale;
154                 }
155 
156                 continue;
157             }
158         }
159 
160         if (processContext == null) {
161             processContext = AbstractProcessContext
162                     .getProcessContext(FacesContext.getCurrentInstance());
163         }
164 
165         FacesContext facesContext = processContext.getFacesContext();
166 
167         locale = processContext.getDefaultLiteralLocale();
168         if (locale != null) {
169             return locale;
170         }
171 
172         locale = (Locale) getAttribute(facesContext, LITERAL_LOCALE_PROPERTY);
173         if (locale != null) {
174             return locale;
175         }
176 
177         locale = getDefaultLiteralLocale(facesContext);
178         if (locale != null) {
179             setAttribute(facesContext, LITERAL_LOCALE_PROPERTY, locale);
180             return locale;
181         }
182 
183         if (original == null) {
184             throw new FacesException(
185                     "You must specify a default locale for literals !");
186         }
187 
188         throw new FacesException(
189                 "You must specify a default locale for literals for component: "
190                         + original.getId());
191     }
192 
193     public static Locale getDefaultLiteralLocale(FacesContext facesContext) {
194         IPageConfigurator scriptTypeConfigurator = getPageConfiguration(facesContext);
195         if (scriptTypeConfigurator != null) {
196             Locale locale = scriptTypeConfigurator.getLiteralLocale();
197 
198             if (locale != null) {
199                 if (LOG.isDebugEnabled()) {
200                     LOG.debug("Default locale detected = " + locale);
201                 }
202 
203                 return locale;
204             }
205         }
206 
207         Map applicationInitMap = facesContext.getExternalContext()
208                 .getInitParameterMap();
209         String value = (String) applicationInitMap
210                 .get(LITERAL_LOCALE_PARAMETER);
211         if (value == null) {
212             return null;
213         }
214 
215         Locale locale = (Locale) LocaleConverter.SINGLETON.getAsObject(null,
216                 null, value);
217         if (locale != null) {
218             return locale;
219         }
220 
221         throw new FacesException("Unknown locale name '" + value
222                 + "' defined into application init parameters. (web.xml)");
223     }
224 
225     public static TimeZone getLiteralTimeZone(IProcessContext processContext,
226             UIComponent component) {
227         // TODO Auto-generated method stub
228         return null;
229     }
230 }