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