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