1
22
23 package com.liferay.util;
24
25 import com.liferay.portal.kernel.util.StringMaker;
26 import com.liferay.portal.kernel.util.StringPool;
27 import com.liferay.portal.kernel.util.StringUtil;
28
29 import java.net.URLDecoder;
30 import java.net.URLEncoder;
31
32
38 public class JS {
39
40 public static final String ENCODING = "UTF-8";
41
42 public static String getSafeName(String name) {
43 String safeName =
44 StringUtil.replace(
45 name,
46 new String[] {
47 StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
48 },
49 new String[] {
50 StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
51 });
52
53 return safeName;
54 }
55
56
59 public static String escape(String s) {
60 return encodeURIComponent(s);
61 }
62
63
66 public static String unescape(String s) {
67 return decodeURIComponent(s);
68 }
69
70 public static String encodeURIComponent(String s) {
71
72
74 try {
75 s = URLEncoder.encode(s, ENCODING);
76 }
77 catch (Exception e) {
78 }
79
80
82 s = StringUtil.replace(s, "+", "%20");
83 s = StringUtil.replace(s, "%2B", "+");
84
85 return s;
86 }
87
88 public static String decodeURIComponent(String s) {
89
90
92 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
93
94
96 s = StringUtil.replace(s, "+", "%2B");
97 s = StringUtil.replace(s, "%20", "+");
98
99
101 try {
102 s = URLDecoder.decode(s, ENCODING);
103 }
104 catch (Exception e) {
105 }
106
107 return s;
108 }
109
110 public static String toScript(String[] array) {
111 StringMaker sm = new StringMaker();
112
113 sm.append("new Array(");
114
115 for (int i = 0; i < array.length; i++) {
116 sm.append("'" + array[i] + "'");
117
118 if (i + 1 < array.length) {
119 sm.append(",");
120 }
121 }
122
123 sm.append(")");
124
125 return sm.toString();
126 }
127
128 }