1
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
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
49 public static String escape(String s) {
50 return encodeURIComponent(s);
51 }
52
53
56 public static String unescape(String s) {
57 return decodeURIComponent(s);
58 }
59
60 public static String encodeURIComponent(String s) {
61
62
64 try {
65 s = URLEncoder.encode(s, StringPool.UTF8);
66 }
67 catch (Exception e) {
68 }
69
70
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
82 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
83
84
86 s = StringUtil.replace(s, "+", "%2B");
87 s = StringUtil.replace(s, "%20", "+");
88
89
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 }