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