1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  
29  import java.net.URLDecoder;
30  import java.net.URLEncoder;
31  
32  /**
33   * <a href="JS.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class JS {
39  
40      public static final String ENCODING = "UTF-8";
41  
42      public static String getSafeName(String name) {
43          String safeName =
44              StringUtil.replace(
45                  name,
46                  new String[] {
47                      StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
48                  },
49                  new String[] {
50                      StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
51                  });
52  
53          return safeName;
54      }
55  
56      /**
57       * @deprecated Use <code>encodeURIComponent</code>.
58       */
59      public static String escape(String s) {
60          return encodeURIComponent(s);
61      }
62  
63      /**
64       * @deprecated Use <code>decodeURIComponent</code>.
65       */
66      public static String unescape(String s) {
67          return decodeURIComponent(s);
68      }
69  
70      public static String encodeURIComponent(String s) {
71  
72          // Encode URL
73  
74          try {
75              s = URLEncoder.encode(s, ENCODING);
76          }
77          catch (Exception e) {
78          }
79  
80          // Adjust for JavaScript specific annoyances
81  
82          s = StringUtil.replace(s, "+", "%20");
83          s = StringUtil.replace(s, "%2B", "+");
84  
85          return s;
86      }
87  
88      public static String decodeURIComponent(String s) {
89  
90          // Get rid of all unicode
91  
92          s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
93  
94          // Adjust for JavaScript specific annoyances
95  
96          s = StringUtil.replace(s, "+", "%2B");
97          s = StringUtil.replace(s, "%20", "+");
98  
99          // Decode URL
100 
101         try {
102             s = URLDecoder.decode(s, ENCODING);
103         }
104         catch (Exception e) {
105         }
106 
107         return s;
108     }
109 
110     public static String toScript(String[] array) {
111         StringMaker sm = new StringMaker();
112 
113         sm.append("new Array(");
114 
115         for (int i = 0; i < array.length; i++) {
116             sm.append("'" + array[i] + "'");
117 
118             if (i + 1 < array.length) {
119                 sm.append(",");
120             }
121         }
122 
123         sm.append(")");
124 
125         return sm.toString();
126     }
127 
128 }