1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.shared.renderkit.html.util; |
20 | |
|
21 | |
import java.io.UnsupportedEncodingException; |
22 | |
import java.util.Arrays; |
23 | |
import java.util.HashSet; |
24 | |
import java.util.Set; |
25 | |
import java.util.logging.Logger; |
26 | |
|
27 | |
import javax.faces.context.ExternalContext; |
28 | |
|
29 | |
import org.apache.myfaces.shared.config.MyfacesConfig; |
30 | |
|
31 | |
public final class JavascriptUtils |
32 | |
{ |
33 | |
|
34 | 0 | private static final Logger log = Logger.getLogger(JavascriptUtils.class.getName()); |
35 | |
|
36 | 0 | public static final String JAVASCRIPT_DETECTED = JavascriptUtils.class.getName() + ".JAVASCRIPT_DETECTED"; |
37 | |
|
38 | |
private static final String AUTO_SCROLL_PARAM = "autoScroll"; |
39 | |
private static final String AUTO_SCROLL_FUNCTION = "getScrolling()"; |
40 | |
|
41 | 0 | private static final String OLD_VIEW_ID = JavascriptUtils.class + ".OLD_VIEW_ID"; |
42 | |
|
43 | |
|
44 | |
private JavascriptUtils() |
45 | 0 | { |
46 | |
|
47 | 0 | } |
48 | |
|
49 | 0 | private static final Set RESERVED_WORDS = |
50 | |
new HashSet(Arrays.asList(new String[]{ |
51 | |
"abstract", |
52 | |
"boolean", |
53 | |
"break", |
54 | |
"byte", |
55 | |
"case", |
56 | |
"catch", |
57 | |
"char", |
58 | |
"class", |
59 | |
"const", |
60 | |
"continue", |
61 | |
"default", |
62 | |
"delete", |
63 | |
"do", |
64 | |
"double", |
65 | |
"else", |
66 | |
"export", |
67 | |
"extends", |
68 | |
"false", |
69 | |
"final", |
70 | |
"finally", |
71 | |
"float", |
72 | |
"for", |
73 | |
"function", |
74 | |
"goto", |
75 | |
"if", |
76 | |
"implements", |
77 | |
"in", |
78 | |
"instanceof", |
79 | |
"int", |
80 | |
"long", |
81 | |
"native", |
82 | |
"new", |
83 | |
"null", |
84 | |
"package", |
85 | |
"private", |
86 | |
"protected", |
87 | |
"public", |
88 | |
"return", |
89 | |
"short", |
90 | |
"static", |
91 | |
"super", |
92 | |
"switch", |
93 | |
"synchronized", |
94 | |
"this", |
95 | |
"throw", |
96 | |
"throws", |
97 | |
"transient", |
98 | |
"true", |
99 | |
"try", |
100 | |
"typeof", |
101 | |
"var", |
102 | |
"void", |
103 | |
"while", |
104 | |
"with" |
105 | |
})); |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public static String getValidJavascriptNameAsInRI(String origIdentifier) |
111 | |
{ |
112 | 0 | return origIdentifier.replaceAll("-", "\\$_"); |
113 | |
} |
114 | |
|
115 | |
public static String getValidJavascriptName(String s, boolean checkForReservedWord) |
116 | |
{ |
117 | 0 | if (checkForReservedWord && RESERVED_WORDS.contains(s)) |
118 | |
{ |
119 | 0 | return s + "_"; |
120 | |
} |
121 | |
|
122 | 0 | StringBuilder buf = null; |
123 | 0 | for (int i = 0, len = s.length(); i < len; i++) |
124 | |
{ |
125 | 0 | char c = s.charAt(i); |
126 | |
|
127 | 0 | if (Character.isLetterOrDigit(c)) |
128 | |
{ |
129 | |
|
130 | 0 | if (buf != null) |
131 | |
{ |
132 | 0 | buf.append(c); |
133 | |
} |
134 | |
} |
135 | |
else |
136 | |
{ |
137 | 0 | if (buf == null) |
138 | |
{ |
139 | 0 | buf = new StringBuilder(s.length() + 10); |
140 | 0 | buf.append(s.substring(0, i)); |
141 | |
} |
142 | |
|
143 | 0 | buf.append('_'); |
144 | 0 | if (c < 16) |
145 | |
{ |
146 | |
|
147 | 0 | buf.append('0'); |
148 | |
} |
149 | |
|
150 | 0 | if (c < 128) |
151 | |
{ |
152 | |
|
153 | 0 | buf.append(Integer.toHexString(c).toUpperCase()); |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | |
byte[] bytes; |
158 | |
try |
159 | |
{ |
160 | 0 | bytes = Character.toString(c).getBytes("UTF-8"); |
161 | |
} |
162 | 0 | catch (UnsupportedEncodingException e) |
163 | |
{ |
164 | 0 | throw new RuntimeException(e); |
165 | 0 | } |
166 | |
|
167 | 0 | for (int j = 0; j < bytes.length; j++) |
168 | |
{ |
169 | 0 | int intVal = bytes[j]; |
170 | 0 | if (intVal < 0) |
171 | |
{ |
172 | |
|
173 | 0 | intVal = 256 + intVal; |
174 | |
} |
175 | 0 | else if (intVal < 16) |
176 | |
{ |
177 | |
|
178 | 0 | buf.append('0'); |
179 | |
} |
180 | 0 | buf.append(Integer.toHexString(intVal).toUpperCase()); |
181 | |
} |
182 | |
} |
183 | |
} |
184 | |
|
185 | |
} |
186 | |
|
187 | 0 | return buf == null ? s : buf.toString(); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
public static String encodeString(String string) |
192 | |
{ |
193 | 0 | if (string == null) |
194 | |
{ |
195 | 0 | return ""; |
196 | |
} |
197 | 0 | StringBuilder sb = null; |
198 | |
String app; |
199 | |
char c; |
200 | 0 | for (int i = 0; i < string.length (); ++i) |
201 | |
{ |
202 | 0 | app = null; |
203 | 0 | c = string.charAt(i); |
204 | 0 | switch (c) |
205 | |
{ |
206 | 0 | case '\\' : app = "\\\\"; break; |
207 | 0 | case '"' : app = "\\\""; break; |
208 | 0 | case '\'' : app = "\\'"; break; |
209 | 0 | case '\n' : app = "\\n"; break; |
210 | 0 | case '\r' : app = "\\r"; break; |
211 | |
default: |
212 | |
} |
213 | 0 | if (app != null) |
214 | |
{ |
215 | 0 | if (sb == null) |
216 | |
{ |
217 | 0 | sb = new StringBuilder(string.substring(0, i)); |
218 | |
} |
219 | 0 | sb.append(app); |
220 | |
} |
221 | |
else |
222 | |
{ |
223 | 0 | if (sb != null) |
224 | |
{ |
225 | 0 | sb.append(c); |
226 | |
} |
227 | |
} |
228 | |
} |
229 | |
|
230 | 0 | if (sb == null) |
231 | |
{ |
232 | 0 | return string; |
233 | |
} |
234 | |
else |
235 | |
{ |
236 | 0 | return sb.toString(); |
237 | |
} |
238 | |
} |
239 | |
|
240 | |
public static boolean isRenderClearJavascriptOnButton(ExternalContext externalContext) |
241 | |
{ |
242 | 0 | MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(externalContext); |
243 | 0 | if (myfacesConfig.isRenderClearJavascriptOnButton()) |
244 | |
{ |
245 | 0 | return true; |
246 | |
} |
247 | |
else |
248 | |
{ |
249 | 0 | return false; |
250 | |
} |
251 | |
} |
252 | |
|
253 | |
public static boolean isSaveFormSubmitLinkIE(ExternalContext externalContext) |
254 | |
{ |
255 | 0 | MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(externalContext); |
256 | 0 | if (myfacesConfig.isSaveFormSubmitLinkIE()) |
257 | |
{ |
258 | 0 | return true; |
259 | |
} |
260 | |
else |
261 | |
{ |
262 | 0 | return false; |
263 | |
} |
264 | |
} |
265 | |
|
266 | |
public static void setOldViewId(ExternalContext externalContext, String viewId) |
267 | |
{ |
268 | 0 | externalContext.getRequestMap().put(OLD_VIEW_ID, viewId); |
269 | 0 | } |
270 | |
|
271 | |
public static String getOldViewId(ExternalContext externalContext) |
272 | |
{ |
273 | 0 | return (String)externalContext.getRequestMap().get(OLD_VIEW_ID); |
274 | |
} |
275 | |
} |