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.renderkit.html.CommandRendererHelper;
021    
022    import javax.faces.component.UIViewRoot;
023    import javax.faces.context.FacesContext;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    public class ResourceManagerUtil {
028    
029      private ResourceManagerUtil() {
030      }
031    
032      public static String getProperty(
033          FacesContext facesContext, String bundle, String key) {
034        return ResourceManagerFactory.getResourceManager(facesContext)
035            .getProperty(facesContext.getViewRoot(), bundle, key);
036      }
037    
038      public static String getPropertyNotNull(
039          FacesContext facesContext, String bundle, String key) {
040        UIViewRoot viewRoot = facesContext.getViewRoot();
041        String result = ResourceManagerFactory.getResourceManager(facesContext)
042            .getProperty(viewRoot, bundle, key);
043        if (result == null) {
044          return "???" + key + "???";
045        } else {
046          return result;
047        }
048      }
049    
050      /**
051       * Searchs for an image and return it with the context path
052       */
053      public static String getImageWithPath(
054          FacesContext facesContext, String name) {
055        return facesContext.getExternalContext().getRequestContextPath()
056            + ResourceManagerFactory.getResourceManager(facesContext)
057            .getImage(facesContext.getViewRoot(), name);
058      }
059    
060      public static String getBlankPage(FacesContext context) {
061        return context.getExternalContext().getRequestContextPath()
062            + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
063      }
064    
065      /**
066       * Searchs for an image and return it with the context path
067       */
068      public static String getImageWithPath(
069          FacesContext facesContext, String name, boolean ignoreMissing) {
070        String image = ResourceManagerFactory.getResourceManager(facesContext)
071            .getImage(facesContext.getViewRoot(), name, ignoreMissing);
072        if (image == null) {
073          return null;
074        } else {
075          return facesContext.getExternalContext().getRequestContextPath() + image;
076        }
077      }
078    
079      public static List<String> getStyles(FacesContext facesContext, String name) {
080        UIViewRoot viewRoot = facesContext.getViewRoot();
081        String contextPath = facesContext.getExternalContext().getRequestContextPath();
082        String[] styles = ResourceManagerFactory.getResourceManager(facesContext).getStyles(viewRoot, name);
083        return addContextPath(styles, contextPath);
084      }
085    
086      private static List<String> addContextPath(String[] strings, String contextPath) {
087        List<String> withContext = new ArrayList<String>(strings.length);
088        for (String string : strings) {
089          withContext.add(contextPath + string);
090        }
091        return withContext;
092      }
093    
094      public static List<String> getScripts(FacesContext facesContext, String name) {
095        UIViewRoot viewRoot = facesContext.getViewRoot();
096        String contextPath = facesContext.getExternalContext().getRequestContextPath();
097        String[] scripts = ResourceManagerFactory.getResourceManager(facesContext)
098            .getScripts(viewRoot, name);
099        return addContextPath(scripts, contextPath);
100      }
101    
102      public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) {
103        List<String> fileNames = new ArrayList<String>();
104        for (String name : names) {
105          fileNames.addAll(getScripts(facesContext, name));
106        }
107        return toJSArray(fileNames);
108      }
109    
110      public static String getStylesAsJSArray(FacesContext facesContext, String[] names) {
111        List<String> fileNames = new ArrayList<String>();
112        for (String name : names) {
113          fileNames.addAll(getStyles(facesContext, name));
114        }
115        return toJSArray(fileNames);
116      }
117    
118      public static String toJSArray(List<String> list) {
119        StringBuilder sb = new StringBuilder();
120        for (String name : list) {
121          if (sb.length() > 0) {
122            sb.append(", ");
123          }
124          sb.append('\'');
125          sb.append(name);
126          sb.append('\'');
127        }
128        return "[" + sb.toString() + "]";
129      }
130    
131      public static String getDisabledImageWithPath(FacesContext facesContext, String image) {
132        int dotIndex = image.lastIndexOf('.');
133        String name = image.substring(0, dotIndex);
134        String postfix = image.substring(dotIndex);
135        return getImageWithPath(facesContext, name + "Disabled" + postfix, true);
136      }
137    
138      public static String getImageWithPath(FacesContext facesContext, String image, CommandRendererHelper helper) {
139        String imageWithPath = null;
140        if (helper.isDisabled()) {
141          imageWithPath = getDisabledImageWithPath(facesContext, image);
142        }
143        if (imageWithPath == null) {
144          imageWithPath = getImageWithPath(facesContext, image);
145        }
146        return imageWithPath;
147      }
148    }