1
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
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
54 public static String escape(String s) {
55 return encodeURIComponent(s);
56 }
57
58
61 public static String unescape(String s) {
62 return decodeURIComponent(s);
63 }
64
65 public static String encodeURIComponent(String s) {
66
67
69 try {
70 s = URLEncoder.encode(s, StringPool.UTF8);
71 }
72 catch (Exception e) {
73 }
74
75
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
87 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
88
89
91 s = StringUtil.replace(s, "+", "%2B");
92 s = StringUtil.replace(s, "%20", "+");
93
94
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 }