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.context;
21  
22  import org.apache.myfaces.tobago.config.Configurable;
23  import org.apache.myfaces.tobago.layout.Measure;
24  
25  import javax.faces.context.FacesContext;
26  import java.util.ArrayList;
27  import java.util.List;
28  import java.util.Locale;
29  
30  public class ResourceManagerUtils {
31  
32    private ResourceManagerUtils() {
33      // no instance
34    }
35  
36    public static String getProperty(FacesContext facesContext, String bundle, String key) {
37      return     org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
38          .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
39    }
40  
41    public static String getPropertyNotNull(FacesContext facesContext, String bundle, String key) {
42      String result = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
43          .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
44      if (result == null) {
45        return "???" + key + "???";
46      } else {
47        return result;
48      }
49    }
50  
51    /**
52     * Searches for an image and return it with the context path
53     */
54    public static String getImageWithPath(FacesContext facesContext, String name) {
55      return facesContext.getExternalContext().getRequestContextPath()
56          + org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
57          .getResourceManager(facesContext).getImage(facesContext, name);
58    }
59  
60    /**
61     * Searches for an image and return it with the context path
62     */
63    public static String getImageWithPath(FacesContext facesContext, String name, boolean ignoreMissing) {
64      String image = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
65          .getResourceManager(facesContext).getImage(facesContext, name, ignoreMissing);
66      if (image == null) {
67        return null;
68      } else {
69        return facesContext.getExternalContext().getRequestContextPath() + image;
70      }
71    }
72  
73    public static List<String> getStyles(FacesContext facesContext, String name) {
74      String contextPath = facesContext.getExternalContext().getRequestContextPath();
75      String[] styles = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
76          .getResourceManager(facesContext).getStyles(facesContext, name);
77      return addContextPath(styles, contextPath);
78    }
79  
80    private static List<String> addContextPath(String[] strings, String contextPath) {
81      List<String> withContext = new ArrayList<String>(strings.length);
82      for (String string : strings) {
83        withContext.add(contextPath + string);
84      }
85      return withContext;
86    }
87  
88    public static List<String> getScripts(FacesContext facesContext, String name) {
89      String contextPath = facesContext.getExternalContext().getRequestContextPath();
90      String[] scripts = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
91          .getResourceManager(facesContext).getScripts(facesContext, name);
92      return addContextPath(scripts, contextPath);
93    }
94  
95    public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) {
96      List<String> fileNames = new ArrayList<String>();
97      for (String name : names) {
98        fileNames.addAll(getScripts(facesContext, name));
99      }
100     return toJSArray(fileNames);
101   }
102 
103   public static String getStylesAsJSArray(FacesContext facesContext, String[] names) {
104     List<String> fileNames = new ArrayList<String>();
105     for (String name : names) {
106       fileNames.addAll(getStyles(facesContext, name));
107     }
108     return toJSArray(fileNames);
109   }
110 
111   public static String toJSArray(List<String> list) {
112     StringBuilder sb = new StringBuilder();
113     for (String name : list) {
114       if (sb.length() > 0) {
115         sb.append(", ");
116       }
117       sb.append('\'');
118       sb.append(name);
119       sb.append('\'');
120     }
121     return "[" + sb.toString() + "]";
122   }
123 
124   public static String getDisabledImageWithPath(FacesContext facesContext, String image) {
125     String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
126     return getImageWithPath(facesContext, filename, true);
127   }
128 
129   /**
130    * Blank page e. g. useful to set src of iframes (to prevent https problems in ie, see TOBAGO-538)
131    */
132   public static String getBlankPage(FacesContext facesContext) {
133     return facesContext.getExternalContext().getRequestContextPath()
134         + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
135   }
136 
137   public static String getPageWithoutContextPath(FacesContext facesContext, String name) {
138     return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
139         .getResourceManager(facesContext).getImage(facesContext, name);
140   }
141   
142   public static Measure getThemeMeasure(FacesContext facesContext, Configurable configurable, String name) {
143     return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
144         .getResourceManager(facesContext).getThemeMeasure(
145         facesContext, configurable.getRendererType(), configurable.getCurrentMarkup(), name);
146   }
147 
148   /**
149    * Detects if the value is an absolute resource or if the value has to be processed by the
150    * theme mechanism. A resource will be treated as absolute, if the value starts with HTTP:, HTTPS:, FTP: or a slash.
151    * The case will be ignored by this check. Null values will return true.
152    *
153    * @param value the given resource link.
154    * @return true if it is an external or absolute resource.
155    */
156   public static boolean isAbsoluteResource(String value) {
157     if (value == null) {
158       return true;
159     }
160     String upper = value.toUpperCase(Locale.ENGLISH);
161     return (upper.startsWith("/")
162         || upper.startsWith("HTTP:")
163         || upper.startsWith("HTTPS:")
164         || upper.startsWith("FTP:"));
165   }
166 
167   public static String getImageOrDisabledImageWithPath(FacesContext facesContext, String image, boolean disabled) {
168     String imageWithPath = null;
169     if (disabled) {
170       imageWithPath = ResourceManagerUtils.getDisabledImageWithPath(facesContext, image);
171     }
172     if (imageWithPath == null) {
173       imageWithPath = ResourceManagerUtils.getImageWithPath(facesContext, image);
174     }
175     return imageWithPath;
176   }
177 }