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