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 private static final Logger log = Logger.getLogger(JavascriptUtils.class.getName());
35
36 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 private static final String OLD_VIEW_ID = JavascriptUtils.class + ".OLD_VIEW_ID";
42
43
44 private JavascriptUtils()
45 {
46
47 }
48
49 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 return origIdentifier.replaceAll("-", "\\$_");
113 }
114
115 public static String getValidJavascriptName(String s, boolean checkForReservedWord)
116 {
117 if (checkForReservedWord && RESERVED_WORDS.contains(s))
118 {
119 return s + "_";
120 }
121
122 StringBuilder buf = null;
123 for (int i = 0, len = s.length(); i < len; i++)
124 {
125 char c = s.charAt(i);
126
127 if (Character.isLetterOrDigit(c))
128 {
129
130 if (buf != null)
131 {
132 buf.append(c);
133 }
134 }
135 else
136 {
137 if (buf == null)
138 {
139 buf = new StringBuilder(s.length() + 10);
140 buf.append(s.substring(0, i));
141 }
142
143 buf.append('_');
144 if (c < 16)
145 {
146
147 buf.append('0');
148 }
149
150 if (c < 128)
151 {
152
153 buf.append(Integer.toHexString(c).toUpperCase());
154 }
155 else
156 {
157 byte[] bytes;
158 try
159 {
160 bytes = Character.toString(c).getBytes("UTF-8");
161 }
162 catch (UnsupportedEncodingException e)
163 {
164 throw new RuntimeException(e);
165 }
166
167 for (int j = 0; j < bytes.length; j++)
168 {
169 int intVal = bytes[j];
170 if (intVal < 0)
171 {
172
173 intVal = 256 + intVal;
174 }
175 else if (intVal < 16)
176 {
177
178 buf.append('0');
179 }
180 buf.append(Integer.toHexString(intVal).toUpperCase());
181 }
182 }
183 }
184
185 }
186
187 return buf == null ? s : buf.toString();
188 }
189
190
191 public static String encodeString(String string)
192 {
193 if (string == null)
194 {
195 return "";
196 }
197 StringBuilder sb = null;
198 String app;
199 char c;
200 for (int i = 0; i < string.length (); ++i)
201 {
202 app = null;
203 c = string.charAt(i);
204 switch (c)
205 {
206 case '\\' : app = "\\\\"; break;
207 case '"' : app = "\\\""; break;
208 case '\'' : app = "\\'"; break;
209 case '\n' : app = "\\n"; break;
210 case '\r' : app = "\\r"; break;
211 default:
212 }
213 if (app != null)
214 {
215 if (sb == null)
216 {
217 sb = new StringBuilder(string.substring(0, i));
218 }
219 sb.append(app);
220 }
221 else
222 {
223 if (sb != null)
224 {
225 sb.append(c);
226 }
227 }
228 }
229
230 if (sb == null)
231 {
232 return string;
233 }
234 else
235 {
236 return sb.toString();
237 }
238 }
239
240 public static boolean isRenderClearJavascriptOnButton(ExternalContext externalContext)
241 {
242 MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(externalContext);
243 if (myfacesConfig.isRenderClearJavascriptOnButton())
244 {
245 return true;
246 }
247 else
248 {
249 return false;
250 }
251 }
252
253 public static boolean isSaveFormSubmitLinkIE(ExternalContext externalContext)
254 {
255 MyfacesConfig myfacesConfig = MyfacesConfig.getCurrentInstance(externalContext);
256 if (myfacesConfig.isSaveFormSubmitLinkIE())
257 {
258 return true;
259 }
260 else
261 {
262 return false;
263 }
264 }
265
266 public static void setOldViewId(ExternalContext externalContext, String viewId)
267 {
268 externalContext.getRequestMap().put(OLD_VIEW_ID, viewId);
269 }
270
271 public static String getOldViewId(ExternalContext externalContext)
272 {
273 return (String)externalContext.getRequestMap().get(OLD_VIEW_ID);
274 }
275 }