001
014
015 package com.liferay.util;
016
017 import com.liferay.portal.kernel.util.StringBundler;
018 import com.liferay.portal.kernel.util.StringPool;
019 import com.liferay.portal.kernel.util.StringUtil;
020 import com.liferay.portal.kernel.util.UnicodeFormatter;
021
022 import java.net.URLDecoder;
023 import java.net.URLEncoder;
024
025
028 public class JS {
029
030 public static String getSafeName(String name) {
031 String safeName =
032 StringUtil.replace(
033 name,
034 new String[] {
035 StringPool.SPACE, StringPool.DASH, StringPool.PERIOD
036 },
037 new String[] {
038 StringPool.BLANK, StringPool.BLANK, StringPool.BLANK
039 });
040
041 return safeName;
042 }
043
044
047 public static String escape(String s) {
048 return encodeURIComponent(s);
049 }
050
051
054 public static String unescape(String s) {
055 return decodeURIComponent(s);
056 }
057
058 public static String encodeURIComponent(String s) {
059
060
061
062 try {
063 s = URLEncoder.encode(s, StringPool.UTF8);
064 }
065 catch (Exception e) {
066 }
067
068
069
070 s = StringUtil.replace(s, "+", "%20");
071 s = StringUtil.replace(s, "%2B", "+");
072
073 return s;
074 }
075
076 public static String decodeURIComponent(String s) {
077
078
079
080 s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
081
082
083
084 s = StringUtil.replace(s, "+", "%2B");
085 s = StringUtil.replace(s, "%20", "+");
086
087
088
089 try {
090 s = URLDecoder.decode(s, StringPool.UTF8);
091 }
092 catch (Exception e) {
093 }
094
095 return s;
096 }
097
098 public static String toScript(String[] array) {
099 StringBundler sb = new StringBundler(array.length * 4 + 2);
100
101 sb.append(StringPool.OPEN_BRACKET);
102
103 for (int i = 0; i < array.length; i++) {
104 sb.append(StringPool.APOSTROPHE);
105 sb.append(UnicodeFormatter.toString(array[i]));
106 sb.append(StringPool.APOSTROPHE);
107
108 if (i + 1 < array.length) {
109 sb.append(StringPool.COMMA);
110 }
111 }
112
113 sb.append(StringPool.CLOSE_BRACKET);
114
115 return sb.toString();
116 }
117
118 }