Coverage Report - org.apache.myfaces.shared.renderkit.html.util.JavascriptUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
JavascriptUtils
0%
0/67
0%
0/46
5
 
 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  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  
         // utility class, do not instantiate
 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  
     /**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  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  
                 // allowed char
 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  
                     // pad single hex digit values with '0' on the left
 147  0
                     buf.append('0');
 148  
                 }
 149  
 
 150  0
                 if (c < 128)
 151  
                 {
 152  
                     // first 128 chars match their byte representation in UTF-8
 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  
                             // intVal will be >= 128
 173  0
                             intVal = 256 + intVal;
 174  
                         }
 175  0
                         else if (intVal < 16)
 176  
                         {
 177  
                             // pad single hex digit values with '0' on the left
 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;    //create later on demand
 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  
 }