| JS.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 *
5 *
6 *
7 * The contents of this file are subject to the terms of the Liferay Enterprise
8 * Subscription License ("License"). You may not use this file except in
9 * compliance with the License. You can obtain a copy of the License by
10 * contacting Liferay, Inc. See the License for the specific language governing
11 * permissions and limitations under the License, including but not limited to
12 * distribution rights 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 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(StringPool.APOSTROPHE);
114 sb.append(UnicodeFormatter.toString(array[i]));
115 sb.append(StringPool.APOSTROPHE);
116
117 if (i + 1 < array.length) {
118 sb.append(StringPool.COMMA);
119 }
120 }
121
122 sb.append(StringPool.CLOSE_BRACKET);
123
124 return sb.toString();
125 }
126
127 }