1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.UnicodeFormatter;
21  
22  import java.net.URLDecoder;
23  import java.net.URLEncoder;
24  
25  /**
26   * <a href="JS.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class JS {
31  
32      public static String getSafeName(String name) {
33          String safeName =
34              StringUtil.replace(
35                  name,
36                  new String[] {
37                      StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
38                  },
39                  new String[] {
40                      StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
41                  });
42  
43          return safeName;
44      }
45  
46      /**
47       * @deprecated Use <code>encodeURIComponent</code>.
48       */
49      public static String escape(String s) {
50          return encodeURIComponent(s);
51      }
52  
53      /**
54       * @deprecated Use <code>decodeURIComponent</code>.
55       */
56      public static String unescape(String s) {
57          return decodeURIComponent(s);
58      }
59  
60      public static String encodeURIComponent(String s) {
61  
62          // Encode URL
63  
64          try {
65              s = URLEncoder.encode(s, StringPool.UTF8);
66          }
67          catch (Exception e) {
68          }
69  
70          // Adjust for JavaScript specific annoyances
71  
72          s = StringUtil.replace(s, "+", "%20");
73          s = StringUtil.replace(s, "%2B", "+");
74  
75          return s;
76      }
77  
78      public static String decodeURIComponent(String s) {
79  
80          // Get rid of all unicode
81  
82          s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
83  
84          // Adjust for JavaScript specific annoyances
85  
86          s = StringUtil.replace(s, "+", "%2B");
87          s = StringUtil.replace(s, "%20", "+");
88  
89          // Decode URL
90  
91          try {
92              s = URLDecoder.decode(s, StringPool.UTF8);
93          }
94          catch (Exception e) {
95          }
96  
97          return s;
98      }
99  
100     public static String toScript(String[] array) {
101         StringBundler sb = new StringBundler(array.length * 4 + 2);
102 
103         sb.append(StringPool.OPEN_BRACKET);
104 
105         for (int i = 0; i < array.length; i++) {
106             sb.append(StringPool.APOSTROPHE);
107             sb.append(UnicodeFormatter.toString(array[i]));
108             sb.append(StringPool.APOSTROPHE);
109 
110             if (i + 1 < array.length) {
111                 sb.append(StringPool.COMMA);
112             }
113         }
114 
115         sb.append(StringPool.CLOSE_BRACKET);
116 
117         return sb.toString();
118     }
119 
120 }