| JS.java |
1 /**
2 * Copyright (c) 2000-2009 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.util;
21
22 import com.liferay.portal.kernel.util.StringPool;
23 import com.liferay.portal.kernel.util.StringUtil;
24 import com.liferay.portal.kernel.util.UnicodeFormatter;
25
26 import java.net.URLDecoder;
27 import java.net.URLEncoder;
28
29 /**
30 * <a href="JS.java.html"><b><i>View Source</i></b></a>
31 *
32 * @author Brian Wing Shun Chan
33 *
34 */
35 public class JS {
36
37 public static String getSafeName(String name) {
38 String safeName =
39 StringUtil.replace(
40 name,
41 new String[] {
42 StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
43 },
44 new String[] {
45 StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
46 });
47
48 return safeName;
49 }
50
51 /**
52 * @deprecated Use <code>encodeURIComponent</code>.
53 */
54 public static String escape(String s) {
55 return encodeURIComponent(s);
56 }
57
58 /**
59 * @deprecated Use <code>decodeURIComponent</code>.
60 */
61 public static String unescape(String s) {
62 return decodeURIComponent(s);
63 }
64
65 public static String encodeURIComponent(String s) {
66
67 // Encode URL
68
69 try {
70 s = URLEncoder.encode(s, StringPool.UTF8);
71 }
72 catch (Exception e) {
73 }
74
75 // Adjust for JavaScript specific annoyances
76
77 s = StringUtil.replace(s, "+", "%20");
78 s = StringUtil.replace(s, "%2B", "+");
79
80 return s;
81 }
82
83 public static String decodeURIComponent(String s) {
84
85 // Get rid of all unicode
86
87 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
88
89 // Adjust for JavaScript specific annoyances
90
91 s = StringUtil.replace(s, "+", "%2B");
92 s = StringUtil.replace(s, "%20", "+");
93
94 // Decode URL
95
96 try {
97 s = URLDecoder.decode(s, StringPool.UTF8);
98 }
99 catch (Exception e) {
100 }
101
102 return s;
103 }
104
105 public static String toScript(String[] array) {
106 StringBuilder sb = new StringBuilder();
107
108 sb.append(StringPool.OPEN_BRACKET);
109
110 for (int i = 0; i < array.length; i++) {
111 sb.append(StringPool.APOSTROPHE);
112 sb.append(UnicodeFormatter.toString(array[i]));
113 sb.append(StringPool.APOSTROPHE);
114
115 if (i + 1 < array.length) {
116 sb.append(StringPool.COMMA);
117 }
118 }
119
120 sb.append(StringPool.CLOSE_BRACKET);
121
122 return sb.toString();
123 }
124
125 }