View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.util;
21  
22  import org.apache.commons.collections.list.SetUniqueList;
23  import org.apache.commons.collections.set.ListOrderedSet;
24  
25  import javax.faces.context.FacesContext;
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.List;
29  import java.util.Set;
30  
31  @SuppressWarnings("unchecked")
32  public final class FacesContextUtils {
33  
34    private static final String TOBAGO_ENCTYPE = "org.apache.myfaces.tobago.enctype";
35    private static final String TOBAGO_SCRIPT_FILES = "org.apache.myfaces.tobago.scriptFiles";
36    private static final String TOBAGO_STYLE_FILES = "org.apache.myfaces.tobago.styleFiles";
37    private static final String TOBAGO_FOCUS_ID = "org.apache.myfaces.tobago.focusId";
38  
39    private FacesContextUtils() {
40    }
41  
42    public static void setFocusId(final FacesContext context, final String focusId) {
43      context.getAttributes().put(TOBAGO_FOCUS_ID, focusId);
44    }
45  
46    public static String getFocusId(final FacesContext context) {
47      return (String) context.getAttributes().get(TOBAGO_FOCUS_ID);
48    }
49  
50    public static String getEnctype(final FacesContext context) {
51      return (String) context.getAttributes().get(TOBAGO_ENCTYPE);
52    }
53  
54    public static void setEnctype(final FacesContext context, final String enctype) {
55      context.getAttributes().put(TOBAGO_ENCTYPE, enctype);
56    }
57  
58    public static List<String> getScriptFiles(final FacesContext context) {
59      final List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
60      if (list == null) {
61        return Collections.emptyList();
62      }
63      return list;
64    }
65    
66    public static void addScriptFile(final FacesContext context, final String file) {
67      List<String> list = (List<String>) context.getAttributes().get(TOBAGO_SCRIPT_FILES);
68      if (list == null) {
69        list = SetUniqueList.decorate(new ArrayList());
70        context.getAttributes().put(TOBAGO_SCRIPT_FILES, list);
71      }
72      list.add(file);
73    }
74  
75    public static Set<String> getStyleFiles(final FacesContext context) {
76      final Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
77      if (set == null) {
78        return Collections.emptySet();
79      }
80      return set;
81    }
82  
83    public static void addStyleFile(final FacesContext context, final String script) {
84      Set<String> set = (Set<String>) context.getAttributes().get(TOBAGO_STYLE_FILES);
85      if (set == null) {
86        set = new ListOrderedSet();
87        context.getAttributes().put(TOBAGO_STYLE_FILES, set);
88      }
89      set.add(script);
90    }
91  }