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