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 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 StringMaker sm = new StringMaker();
110
111 sm.append("new Array(");
112
113 for (int i = 0; i < array.length; i++) {
114 sm.append("'" + array[i] + "'");
115
116 if (i + 1 < array.length) {
117 sm.append(",");
118 }
119 }
120
121 sm.append(")");
122
123 return sm.toString();
124 }
125
126 }