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