1   /**
2    * Copyright (c) 2000-2009 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.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.UnicodeFormatter;
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 String getSafeName(String name) {
41          String safeName =
42              StringUtil.replace(
43                  name,
44                  new String[] {
45                      StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
46                  },
47                  new String[] {
48                      StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
49                  });
50  
51          return safeName;
52      }
53  
54      /**
55       * @deprecated Use <code>encodeURIComponent</code>.
56       */
57      public static String escape(String s) {
58          return encodeURIComponent(s);
59      }
60  
61      /**
62       * @deprecated Use <code>decodeURIComponent</code>.
63       */
64      public static String unescape(String s) {
65          return decodeURIComponent(s);
66      }
67  
68      public static String encodeURIComponent(String s) {
69  
70          // Encode URL
71  
72          try {
73              s = URLEncoder.encode(s, StringPool.UTF8);
74          }
75          catch (Exception e) {
76          }
77  
78          // Adjust for JavaScript specific annoyances
79  
80          s = StringUtil.replace(s, "+", "%20");
81          s = StringUtil.replace(s, "%2B", "+");
82  
83          return s;
84      }
85  
86      public static String decodeURIComponent(String s) {
87  
88          // Get rid of all unicode
89  
90          s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
91  
92          // Adjust for JavaScript specific annoyances
93  
94          s = StringUtil.replace(s, "+", "%2B");
95          s = StringUtil.replace(s, "%20", "+");
96  
97          // Decode URL
98  
99          try {
100             s = URLDecoder.decode(s, StringPool.UTF8);
101         }
102         catch (Exception e) {
103         }
104 
105         return s;
106     }
107 
108     public static String toScript(String[] array) {
109         StringBuilder sb = new StringBuilder();
110 
111         sb.append(StringPool.OPEN_BRACKET);
112 
113         for (int i = 0; i < array.length; i++) {
114             sb.append(StringPool.APOSTROPHE);
115             sb.append(UnicodeFormatter.toString(array[i]));
116             sb.append(StringPool.APOSTROPHE);
117 
118             if (i + 1 < array.length) {
119                 sb.append(StringPool.COMMA);
120             }
121         }
122 
123         sb.append(StringPool.CLOSE_BRACKET);
124 
125         return sb.toString();
126     }
127 
128 }