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.internal.context.ResourceManagerFactory;
23  
24  import javax.faces.context.FacesContext;
25  import java.util.ArrayList;
26  import java.util.List;
27  import java.util.Locale;
28  
29  public final class ResourceManagerUtils {
30  
31    private ResourceManagerUtils() {
32      // no instance
33    }
34  
35    public static String getProperty(final FacesContext facesContext, final String bundle, final String key) {
36      return ResourceManagerFactory.getResourceManager(facesContext).getProperty(facesContext, bundle, key);
37    }
38  
39    public static String getPropertyNotNull(final FacesContext facesContext, final String bundle, final String key) {
40      final String result
41          = ResourceManagerFactory.getResourceManager(facesContext).getProperty(facesContext, bundle, key);
42      if (result == null) {
43        return "???" + key + "???";
44      } else {
45        return result;
46      }
47    }
48  
49    /**
50     * Searches for an image and return it with the context path
51     *
52     * @param facesContext the current FacesContext
53     * @param name the name of the image with extension
54     */
55    public static String getImageWithPath(final FacesContext facesContext, final String name) {
56      return getImageWithPath(facesContext, name, false);
57    }
58  
59    /**
60     * Searches for an image and return it with the context path
61     *
62     * @param facesContext the current FacesContext
63     * @param name the name of the image with extension
64     * @param ignoreMissing if set to false, an error message will be logged, when image is missing
65     */
66    public static String getImageWithPath(
67        final FacesContext facesContext, final String name, final boolean ignoreMissing) {
68      final String image
69          = ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext, name, ignoreMissing);
70      if (image == null) {
71        return null;
72      } else {
73        return facesContext.getExternalContext().getRequestContextPath() + image;
74      }
75    }
76  
77    /**
78     * Searches for an file and return it with the context path.
79     *
80     * @param facesContext the current FacesContext
81     * @param name the name of the file without extension
82     * @param extension the file extension
83     * @param ignoreMissing if set to false, an error message will be logged, when image is missing
84     */
85    public static String getFile(
86        final FacesContext facesContext, final String name, final String extension, final boolean ignoreMissing) {
87      final String image = ResourceManagerFactory.getResourceManager(facesContext)
88          .getImage(facesContext, name, extension, ignoreMissing);
89      if (image == null) {
90        return null;
91      } else {
92        return facesContext.getExternalContext().getRequestContextPath() + image;
93      }
94    }
95  
96    /**
97     * Searches for an image and return it with the context path.
98     * The extension of the image will be automatically extended (.png, .gif, .jpg).
99     * A missing image will be logged as an error.
100    *
101    * @param facesContext the current FacesContext
102    * @param name the name of the image without extension
103    */
104   public static String getImage(
105       final FacesContext facesContext, final String name) {
106     return getImage(facesContext, name, false);
107   }
108 
109   /**
110    * Searches for an image and return it with the context path.
111    * The extension of the image will be automatically extended (.png, .gif, .jpg)
112    *
113    * @param facesContext the current FacesContext
114    * @param name the name of the image without extension
115    * @param ignoreMissing if set to false, an error message will be logged, when image is missing
116    */
117   public static String getImage(
118       final FacesContext facesContext, final String name, final boolean ignoreMissing) {
119     final String image
120         = ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext, name, null, ignoreMissing);
121     if (image == null) {
122       return null;
123     } else {
124       return facesContext.getExternalContext().getRequestContextPath() + image;
125     }
126   }
127 
128   public static List<String> getStyles(final FacesContext facesContext, final String name) {
129     final String contextPath = facesContext.getExternalContext().getRequestContextPath();
130     final String[] styles = ResourceManagerFactory.getResourceManager(facesContext).getStyles(facesContext, name);
131     return addContextPath(styles, contextPath);
132   }
133 
134   private static List<String> addContextPath(final String[] strings, final String contextPath) {
135     final List<String> withContext = new ArrayList<String>(strings.length);
136     for (final String string : strings) {
137       withContext.add(contextPath + string);
138     }
139     return withContext;
140   }
141 
142   public static List<String> getScripts(final FacesContext facesContext, final String name) {
143     final String contextPath = facesContext.getExternalContext().getRequestContextPath();
144     final String[] scripts = ResourceManagerFactory.getResourceManager(facesContext).getScripts(facesContext, name);
145     return addContextPath(scripts, contextPath);
146   }
147 
148   public static String getDisabledImageWithPath(final FacesContext facesContext, final String image,
149                                                 final boolean ignoreMissing) {
150     final String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
151     return getImageWithPath(facesContext, filename, ignoreMissing);
152   }
153 
154   public static String getDisabledImage(final FacesContext facesContext, final String image,
155                                                 final boolean ignoreMissing) {
156     return getImage(facesContext, image + "Disabled", ignoreMissing);
157   }
158 
159   /**
160    * Blank page e. g. useful to set src of iframes (to prevent https problems in ie, see TOBAGO-538)
161    */
162   public static String getBlankPage(final FacesContext facesContext) {
163     return facesContext.getExternalContext().getRequestContextPath()
164         + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
165   }
166 
167   public static String getPageWithoutContextPath(final FacesContext facesContext, final String name) {
168     return ResourceManagerUtils.getImageWithPath(facesContext, name);
169   }
170 
171   /**
172    * Detects if the value is an absolute resource or if the value has to be processed by the theme mechanism. A resource
173    * will be treated as absolute, if the value starts with HTTP:, HTTPS:, FTP: or a slash. The case will be ignored by
174    * this check. Null values will return true.
175    *
176    * @param value the given resource link.
177    * @return true if it is an external or absolute resource.
178    */
179   public static boolean isAbsoluteResource(final String value) {
180     if (value == null) {
181       return true;
182     }
183     final String upper = value.toUpperCase(Locale.ENGLISH);
184     return (upper.startsWith("/")
185         || upper.startsWith("HTTP:")
186         || upper.startsWith("HTTPS:")
187         || upper.startsWith("FTP:"));
188   }
189 
190   public static int indexOfExtension(final String value) {
191     if (value == null) {
192       return -1;
193     }
194     int dot = value.lastIndexOf('.');
195     if (dot == -1) {
196       return dot;
197     }
198     int slash = value.lastIndexOf('/');
199     return dot > slash ? dot : -1;
200   }
201 
202   public static String getImageOrDisabledImageWithPath(
203       final FacesContext facesContext, final String image, final boolean disabled) {
204     return getImageOrDisabledImageWithPath(facesContext, image, disabled, false);
205   }
206 
207   public static String getImageOrDisabledImageWithPath(
208           final FacesContext facesContext, final String image, final boolean disabled, final boolean ignoreMissing) {
209     String imageWithPath = null;
210     if (disabled) {
211       imageWithPath = ResourceManagerUtils.getDisabledImageWithPath(facesContext, image, false);
212     }
213     if (imageWithPath == null) {
214       imageWithPath = ResourceManagerUtils.getImageWithPath(facesContext, image, ignoreMissing);
215     }
216     return imageWithPath;
217   }
218 
219   public static String getImageOrDisabledImage(
220       final FacesContext facesContext, final String image, final boolean disabled) {
221     return getImageOrDisabledImage(facesContext, image, disabled, false);
222   }
223 
224   public static String getImageOrDisabledImage(
225           final FacesContext facesContext, final String image, final boolean disabled, final boolean ignoreMissing) {
226     String imageWithPath = null;
227     if (disabled) {
228       imageWithPath = ResourceManagerUtils.getDisabledImage(facesContext, image, false);
229     }
230     if (imageWithPath == null) {
231       imageWithPath = ResourceManagerUtils.getImage(facesContext, image, ignoreMissing);
232     }
233     return imageWithPath;
234   }
235 }