View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      //private static final Log log = LogFactory.getLog(JavascriptUtils.class);
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          // utility class, do not instantiate
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     /**Don't use this function - except when compatibility with the RI is a must,
108      * as in the name for the clear form parameters script.
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                 // allowed char
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                     // pad single hex digit values with '0' on the left
147                     buf.append('0');
148                 }
149 
150                 if (c < 128)
151                 {
152                     // first 128 chars match their byte representation in UTF-8
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                             // intVal will be >= 128
173                             intVal = 256 + intVal;
174                         }
175                         else if (intVal < 16)
176                         {
177                             // pad single hex digit values with '0' on the left
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;    //create later on demand
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 }