1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.context;
21
22 import org.apache.myfaces.tobago.config.Configurable;
23 import org.apache.myfaces.tobago.layout.Measure;
24
25 import javax.faces.context.FacesContext;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Locale;
29
30 public class ResourceManagerUtils {
31
32 private ResourceManagerUtils() {
33
34 }
35
36 public static String getProperty(FacesContext facesContext, String bundle, String key) {
37 return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
38 .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
39 }
40
41 public static String getPropertyNotNull(FacesContext facesContext, String bundle, String key) {
42 String result = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
43 .getResourceManager(facesContext).getProperty(facesContext, bundle, key);
44 if (result == null) {
45 return "???" + key + "???";
46 } else {
47 return result;
48 }
49 }
50
51
52
53
54 public static String getImageWithPath(FacesContext facesContext, String name) {
55 return facesContext.getExternalContext().getRequestContextPath()
56 + org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
57 .getResourceManager(facesContext).getImage(facesContext, name);
58 }
59
60
61
62
63 public static String getImageWithPath(FacesContext facesContext, String name, boolean ignoreMissing) {
64 String image = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
65 .getResourceManager(facesContext).getImage(facesContext, name, ignoreMissing);
66 if (image == null) {
67 return null;
68 } else {
69 return facesContext.getExternalContext().getRequestContextPath() + image;
70 }
71 }
72
73 public static List<String> getStyles(FacesContext facesContext, String name) {
74 String contextPath = facesContext.getExternalContext().getRequestContextPath();
75 String[] styles = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
76 .getResourceManager(facesContext).getStyles(facesContext, name);
77 return addContextPath(styles, contextPath);
78 }
79
80 private static List<String> addContextPath(String[] strings, String contextPath) {
81 List<String> withContext = new ArrayList<String>(strings.length);
82 for (String string : strings) {
83 withContext.add(contextPath + string);
84 }
85 return withContext;
86 }
87
88 public static List<String> getScripts(FacesContext facesContext, String name) {
89 String contextPath = facesContext.getExternalContext().getRequestContextPath();
90 String[] scripts = org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
91 .getResourceManager(facesContext).getScripts(facesContext, name);
92 return addContextPath(scripts, contextPath);
93 }
94
95 public static String getScriptsAsJSArray(FacesContext facesContext, String[] names) {
96 List<String> fileNames = new ArrayList<String>();
97 for (String name : names) {
98 fileNames.addAll(getScripts(facesContext, name));
99 }
100 return toJSArray(fileNames);
101 }
102
103 public static String getStylesAsJSArray(FacesContext facesContext, String[] names) {
104 List<String> fileNames = new ArrayList<String>();
105 for (String name : names) {
106 fileNames.addAll(getStyles(facesContext, name));
107 }
108 return toJSArray(fileNames);
109 }
110
111 public static String toJSArray(List<String> list) {
112 StringBuilder sb = new StringBuilder();
113 for (String name : list) {
114 if (sb.length() > 0) {
115 sb.append(", ");
116 }
117 sb.append('\'');
118 sb.append(name);
119 sb.append('\'');
120 }
121 return "[" + sb.toString() + "]";
122 }
123
124 public static String getDisabledImageWithPath(FacesContext facesContext, String image) {
125 String filename = ResourceUtils.addPostfixToFilename(image, "Disabled");
126 return getImageWithPath(facesContext, filename, true);
127 }
128
129
130
131
132 public static String getBlankPage(FacesContext facesContext) {
133 return facesContext.getExternalContext().getRequestContextPath()
134 + "/org/apache/myfaces/tobago/renderkit/html/standard/blank.html";
135 }
136
137 public static String getPageWithoutContextPath(FacesContext facesContext, String name) {
138 return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
139 .getResourceManager(facesContext).getImage(facesContext, name);
140 }
141
142 public static Measure getThemeMeasure(FacesContext facesContext, Configurable configurable, String name) {
143 return org.apache.myfaces.tobago.internal.context.ResourceManagerFactory
144 .getResourceManager(facesContext).getThemeMeasure(
145 facesContext, configurable.getRendererType(), configurable.getCurrentMarkup(), name);
146 }
147
148
149
150
151
152
153
154
155
156 public static boolean isAbsoluteResource(String value) {
157 if (value == null) {
158 return true;
159 }
160 String upper = value.toUpperCase(Locale.ENGLISH);
161 return (upper.startsWith("/")
162 || upper.startsWith("HTTP:")
163 || upper.startsWith("HTTPS:")
164 || upper.startsWith("FTP:"));
165 }
166
167 public static String getImageOrDisabledImageWithPath(FacesContext facesContext, String image, boolean disabled) {
168 String imageWithPath = null;
169 if (disabled) {
170 imageWithPath = ResourceManagerUtils.getDisabledImageWithPath(facesContext, image);
171 }
172 if (imageWithPath == null) {
173 imageWithPath = ResourceManagerUtils.getImageWithPath(facesContext, image);
174 }
175 return imageWithPath;
176 }
177 }