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 import java.util.Locale; 027 028 public class ResourceManagerUtil { 029 030 private ResourceManagerUtil() { 031 } 032 033 public static String getProperty( 034 FacesContext facesContext, String bundle, String key) { 035 return ResourceManagerFactory.getResourceManager(facesContext) 036 .getProperty(facesContext.getViewRoot(), bundle, key); 037 } 038 039 public static String getPropertyNotNull( 040 FacesContext facesContext, String bundle, String key) { 041 UIViewRoot viewRoot = facesContext.getViewRoot(); 042 String result = ResourceManagerFactory.getResourceManager(facesContext) 043 .getProperty(viewRoot, bundle, key); 044 if (result == null) { 045 return "???" + key + "???"; 046 } else { 047 return result; 048 } 049 } 050 051 /** 052 * Searchs for an image and return it with the context path 053 */ 054 public static String getImageWithPath( 055 FacesContext facesContext, String name) { 056 return facesContext.getExternalContext().getRequestContextPath() 057 + ResourceManagerFactory.getResourceManager(facesContext) 058 .getImage(facesContext.getViewRoot(), name); 059 } 060 061 /** 062 * Searchs for an image and return it with the context path 063 */ 064 public static String getImageWithPath( 065 FacesContext facesContext, String name, boolean ignoreMissing) { 066 String image = ResourceManagerFactory.getResourceManager(facesContext) 067 .getImage(facesContext.getViewRoot(), name, ignoreMissing); 068 if (image == null) { 069 return null; 070 } else { 071 return facesContext.getExternalContext().getRequestContextPath() + image; 072 } 073 } 074 075 public static List<String> getStyles(FacesContext facesContext, String name) { 076 UIViewRoot viewRoot = facesContext.getViewRoot(); 077 String contextPath = facesContext.getExternalContext().getRequestContextPath(); 078 String[] styles = ResourceManagerFactory.getResourceManager(facesContext).getStyles(viewRoot, name); 079 return addContextPath(styles, contextPath); 080 } 081 082 private static List<String> addContextPath(String[] strings, String contextPath) { 083 List<String> withContext = new ArrayList<String>(strings.length); 084 for (String string : strings) { 085 withContext.add(contextPath + string); 086 } 087 return withContext; 088 } 089 090 public static List<String> getScripts(FacesContext facesContext, String name) { 091 UIViewRoot viewRoot = facesContext.getViewRoot(); 092 String contextPath = facesContext.getExternalContext().getRequestContextPath(); 093 String[] scripts = ResourceManagerFactory.getResourceManager(facesContext) 094 .getScripts(viewRoot, name); 095 return addContextPath(scripts, contextPath); 096 } 097 098 public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) { 099 List<String> fileNames = new ArrayList<String>(); 100 for (String name : names) { 101 fileNames.addAll(getScripts(facesContext, name)); 102 } 103 return toJSArray(fileNames); 104 } 105 106 public static String getStylesAsJSArray(FacesContext facesContext, String[] names) { 107 List<String> fileNames = new ArrayList<String>(); 108 for (String name : names) { 109 fileNames.addAll(getStyles(facesContext, name)); 110 } 111 return toJSArray(fileNames); 112 } 113 114 public static String toJSArray(List<String> list) { 115 StringBuilder sb = new StringBuilder(); 116 for (String name : list) { 117 if (sb.length() > 0) { 118 sb.append(", "); 119 } 120 sb.append('\''); 121 sb.append(name); 122 sb.append('\''); 123 } 124 return "[" + sb.toString() + "]"; 125 } 126 127 public static String getDisabledImageWithPath(FacesContext facesContext, String image) { 128 String filename = ResourceUtils.addPostfixToFilename(image, "Disabled"); 129 return getImageWithPath(facesContext, filename, true); 130 } 131 132 public static String getImageWithPath(FacesContext facesContext, String image, CommandRendererHelper helper) { 133 String imageWithPath = null; 134 if (helper.isDisabled()) { 135 imageWithPath = getDisabledImageWithPath(facesContext, image); 136 } 137 if (imageWithPath == null) { 138 imageWithPath = getImageWithPath(facesContext, image); 139 } 140 return imageWithPath; 141 } 142 143 public static String getBlankPage(FacesContext facesContext) { 144 return facesContext.getExternalContext().getRequestContextPath() 145 + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html"; 146 } 147 148 public static String getPageWithoutContextPath(FacesContext facesContext, String name) { 149 return ResourceManagerFactory.getResourceManager(facesContext).getImage(facesContext.getViewRoot(), name); 150 } 151 152 /** 153 * Detects if the value is an absolute resource or if the value has to be processed by the 154 * theme mechanism. A resource will be treated as absolute, if the value starts with HTTP:, HTTPS:, FTP: or a slash. 155 * The case will be ignored by this check. Null values will return true. 156 * 157 * @param value the given resource link. 158 * @return true if it is an external or absolute resource. 159 */ 160 public static boolean isAbsoluteResource(String value) { 161 if (value == null) { 162 return true; 163 } 164 String upper = value.toUpperCase(Locale.ENGLISH); 165 return (upper.startsWith("/") 166 || upper.startsWith("HTTP:") 167 || upper.startsWith("HTTPS:") 168 || upper.startsWith("FTP:")); 169 } 170 }