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