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_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   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
    33   * @author Anton Koinov
    34   * @version $Revision: 684008 $ $Date: 2008-08-08 11:06:41 -0500 (Fri, 08 Aug 2008) $
    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          // utility class, do not instantiate
    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     /**Don't use this function - except when compatibility with the RI is a must,
    107      * as in the name for the clear form parameters script.
    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                 // allowed char
    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                     // pad single hex digit values with '0' on the left
    143                     buf.append('0');
    144                 }
    145
    146                 if (c < 128)
    147                 {
    148                     // first 128 chars match their byte representation in UTF-8
    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                             // intVal will be >= 128
    169                             intVal = 256 + intVal;
    170                         }
    171                         else if (intVal < 16)
    172                         {
    173                             // pad single hex digit values with '0' on the left
    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;    //create later on demand
    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         //TODO/FIXME (manolito): This info should be better stored in the viewroot component and not in the session
    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 }