001    package org.apache.myfaces.tobago.context;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.config.Configurable;
021    import org.apache.myfaces.tobago.layout.Measure;
022    
023    import javax.faces.context.FacesContext;
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Locale;
027    
028    public class ResourceManagerUtils {
029    
030      private ResourceManagerUtils() {
031        // no instance
032      }
033    
034      public static String getProperty(FacesContext facesContext, String bundle, String key) {
035        return     org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
036            .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
037      }
038    
039      public static String getPropertyNotNull(FacesContext facesContext, String bundle, String key) {
040        String result = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
041            .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
042        if (result == null) {
043          return "???" + key + "???";
044        } else {
045          return result;
046        }
047      }
048    
049      /**
050       * Searches for an image and return it with the context path
051       */
052      public static String getImageWithPath(FacesContext facesContext, String name) {
053        return facesContext.getExternalContext().getRequestContextPath()
054            + org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
055            .getResourceManager(facesContext).getImage(facesContext, name);
056      }
057    
058      /**
059       * Searches for an image and return it with the context path
060       */
061      public static String getImageWithPath(FacesContext facesContext, String name, boolean ignoreMissing) {
062        String image = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
063            .getResourceManager(facesContext).getImage(facesContext, name, ignoreMissing);
064        if (image == null) {
065          return null;
066        } else {
067          return facesContext.getExternalContext().getRequestContextPath() + image;
068        }
069      }
070    
071      public static List<String> getStyles(FacesContext facesContext, String name) {
072        String contextPath = facesContext.getExternalContext().getRequestContextPath();
073        String[] styles = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
074            .getResourceManager(facesContext).getStyles(facesContext, name);
075        return addContextPath(styles, contextPath);
076      }
077    
078      private static List<String> addContextPath(String[] strings, String contextPath) {
079        List<String> withContext = new ArrayList<String>(strings.length);
080        for (String string : strings) {
081          withContext.add(contextPath + string);
082        }
083        return withContext;
084      }
085    
086      public static List<String> getScripts(FacesContext facesContext, String name) {
087        String contextPath = facesContext.getExternalContext().getRequestContextPath();
088        String[] scripts = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
089            .getResourceManager(facesContext).getScripts(facesContext, name);
090        return addContextPath(scripts, contextPath);
091      }
092    
093      public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) {
094        List<String> fileNames = new ArrayList<String>();
095        for (String name : names) {
096          fileNames.addAll(getScripts(facesContext, name));
097        }
098        return toJSArray(fileNames);
099      }
100    
101      public static String getStylesAsJSArray(FacesContext facesContext, String[] names) {
102        List<String> fileNames = new ArrayList<String>();
103        for (String name : names) {
104          fileNames.addAll(getStyles(facesContext, name));
105        }
106        return toJSArray(fileNames);
107      }
108    
109      public static String toJSArray(List<String> list) {
110        StringBuilder sb = new StringBuilder();
111        for (String name : list) {
112          if (sb.length() > 0) {
113            sb.append(", ");
114          }
115          sb.append('\'');
116          sb.append(name);
117          sb.append('\'');
118        }
119        return "[" + sb.toString() + "]";
120      }
121    
122      public static String getDisabledImageWithPath(FacesContext facesContext, String image) {
123        String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
124        return getImageWithPath(facesContext, filename, true);
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(FacesContext facesContext) {
131        return facesContext.getExternalContext().getRequestContextPath()
132            + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
133      }
134    
135      public static String getPageWithoutContextPath(FacesContext facesContext, String name) {
136        return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
137            .getResourceManager(facesContext).getImage(facesContext, name);
138      }
139      
140      public static Measure getThemeMeasure(FacesContext facesContext, Configurable configurable, String name) {
141        return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
142            .getResourceManager(facesContext).getThemeMeasure(
143            facesContext, configurable.getRendererType(), configurable.getCurrentMarkup(), name);
144      }
145    
146      /**
147       * Detects if the value is an absolute resource or if the value has to be processed by the
148       * theme mechanism. A resource will be treated as absolute, if the value starts with HTTP:, HTTPS:, FTP: or a slash.
149       * The case will be ignored by this check. Null values will return true.
150       *
151       * @param value the given resource link.
152       * @return true if it is an external or absolute resource.
153       */
154      public static boolean isAbsoluteResource(String value) {
155        if (value == null) {
156          return true;
157        }
158        String upper = value.toUpperCase(Locale.ENGLISH);
159        return (upper.startsWith("/")
160            || upper.startsWith("HTTP:")
161            || upper.startsWith("HTTPS:")
162            || upper.startsWith("FTP:"));
163      }
164    }