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 /**
061 * Searchs for an image and return it with the context path
062 */
063 public static String getImageWithPath(
064 FacesContext facesContext, String name, boolean ignoreMissing) {
065 String image = ResourceManagerFactory.getResourceManager(facesContext)
066 .getImage(facesContext.getViewRoot(), name, ignoreMissing);
067 if (image == null) {
068 return null;
069 } else {
070 return facesContext.getExternalContext().getRequestContextPath() + image;
071 }
072 }
073
074 public static List<String> getStyles(FacesContext facesContext, String name) {
075 UIViewRoot viewRoot = facesContext.getViewRoot();
076 String contextPath = facesContext.getExternalContext().getRequestContextPath();
077 String[] styles = ResourceManagerFactory.getResourceManager(facesContext).getStyles(viewRoot, name);
078 return addContextPath(styles, contextPath);
079 }
080
081 private static List<String> addContextPath(String[] strings, String contextPath) {
082 List<String> withContext = new ArrayList<String>(strings.length);
083 for (String string : strings) {
084 withContext.add(contextPath + string);
085 }
086 return withContext;
087 }
088
089 public static List<String> getScripts(FacesContext facesContext, String name) {
090 UIViewRoot viewRoot = facesContext.getViewRoot();
091 String contextPath = facesContext.getExternalContext().getRequestContextPath();
092 String[] scripts = ResourceManagerFactory.getResourceManager(facesContext)
093 .getScripts(viewRoot, name);
094 return addContextPath(scripts, contextPath);
095 }
096
097 public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) {
098 List<String> fileNames = new ArrayList<String>();
099 for (String name : names) {
100 fileNames.addAll(getScripts(facesContext, name));
101 }
102 return toJSArray(fileNames);
103 }
104
105 public static String getStylesAsJSArray(FacesContext facesContext, String[] names) {
106 List<String> fileNames = new ArrayList<String>();
107 for (String name : names) {
108 fileNames.addAll(getStyles(facesContext, name));
109 }
110 return toJSArray(fileNames);
111 }
112
113 public static String toJSArray(List<String> list) {
114 StringBuilder sb = new StringBuilder();
115 for (String name : list) {
116 if (sb.length() > 0) {
117 sb.append(", ");
118 }
119 sb.append('\'');
120 sb.append(name);
121 sb.append('\'');
122 }
123 return "[" + sb.toString() + "]";
124 }
125
126 public static String getDisabledImageWithPath(FacesContext facesContext, String image) {
127 String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
128 return getImageWithPath(facesContext, filename, true);
129 }
130
131 public static String getImageWithPath(FacesContext facesContext, String image, CommandRendererHelper helper) {
132 String imageWithPath = null;
133 if (helper.isDisabled()) {
134 imageWithPath = getDisabledImageWithPath(facesContext, image);
135 }
136 if (imageWithPath == null) {
137 imageWithPath = getImageWithPath(facesContext, image);
138 }
139 return imageWithPath;
140 }
141
142 public static String getBlankPage(FacesContext facesContext) {
143 return facesContext.getExternalContext().getRequestContextPath()
144 + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
145 }
146
147 public static String getPageWithoutContextPath(FacesContext facesContext, String name) {
148 return ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext.getViewRoot(), name);
149 }
150 }