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
38 public class JS {
39
40 public static String getSafeName(String name) {
41 String safeName =
42 StringUtil.replace(
43 name,
44 new String[] {
45 StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
46 },
47 new String[] {
48 StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
49 });
50
51 return safeName;
52 }
53
54
57 public static String escape(String s) {
58 return encodeURIComponent(s);
59 }
60
61
64 public static String unescape(String s) {
65 return decodeURIComponent(s);
66 }
67
68 public static String encodeURIComponent(String s) {
69
70
72 try {
73 s = URLEncoder.encode(s, StringPool.UTF8);
74 }
75 catch (Exception e) {
76 }
77
78
80 s = StringUtil.replace(s, "+", "%20");
81 s = StringUtil.replace(s, "%2B", "+");
82
83 return s;
84 }
85
86 public static String decodeURIComponent(String s) {
87
88
90 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
91
92
94 s = StringUtil.replace(s, "+", "%2B");
95 s = StringUtil.replace(s, "%20", "+");
96
97
99 try {
100 s = URLDecoder.decode(s, StringPool.UTF8);
101 }
102 catch (Exception e) {
103 }
104
105 return s;
106 }
107
108 public static String toScript(String[] array) {
109 StringBuilder sb = new StringBuilder();
110
111 sb.append(StringPool.OPEN_BRACKET);
112
113 for (int i = 0; i < array.length; i++) {
114 sb.append(StringPool.APOSTROPHE);
115 sb.append(UnicodeFormatter.toString(array[i]));
116 sb.append(StringPool.APOSTROPHE);
117
118 if (i + 1 < array.length) {
119 sb.append(StringPool.COMMA);
120 }
121 }
122
123 sb.append(StringPool.CLOSE_BRACKET);
124
125 return sb.toString();
126 }
127
128 }