001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringMaker;
024: import com.liferay.portal.kernel.util.StringPool;
025: import com.liferay.portal.kernel.util.StringUtil;
026:
027: import java.net.URLDecoder;
028: import java.net.URLEncoder;
029:
030: /**
031: * <a href="JS.java.html"><b><i>View Source</i></b></a>
032: *
033: * @author Brian Wing Shun Chan
034: *
035: */
036: public class JS {
037:
038: public static final String ENCODING = "UTF-8";
039:
040: public static String getSafeName(String name) {
041: String safeName = StringUtil.replace(name, new String[] {
042: StringPool.SPACE, StringPool.DASH, StringPool.PERIOD },
043: new String[] { StringPool.BLANK, StringPool.BLANK,
044: StringPool.BLANK });
045:
046: return safeName;
047: }
048:
049: /**
050: * @deprecated Use <code>encodeURIComponent</code>.
051: */
052: public static String escape(String s) {
053: return encodeURIComponent(s);
054: }
055:
056: /**
057: * @deprecated Use <code>decodeURIComponent</code>.
058: */
059: public static String unescape(String s) {
060: return decodeURIComponent(s);
061: }
062:
063: public static String encodeURIComponent(String s) {
064:
065: // Encode URL
066:
067: try {
068: s = URLEncoder.encode(s, ENCODING);
069: } catch (Exception e) {
070: }
071:
072: // Adjust for JavaScript specific annoyances
073:
074: s = StringUtil.replace(s, "+", "%20");
075: s = StringUtil.replace(s, "%2B", "+");
076:
077: return s;
078: }
079:
080: public static String decodeURIComponent(String s) {
081:
082: // Get rid of all unicode
083:
084: s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
085:
086: // Adjust for JavaScript specific annoyances
087:
088: s = StringUtil.replace(s, "+", "%2B");
089: s = StringUtil.replace(s, "%20", "+");
090:
091: // Decode URL
092:
093: try {
094: s = URLDecoder.decode(s, ENCODING);
095: } catch (Exception e) {
096: }
097:
098: return s;
099: }
100:
101: public static String toScript(String[] array) {
102: StringMaker sm = new StringMaker();
103:
104: sm.append("new Array(");
105:
106: for (int i = 0; i < array.length; i++) {
107: sm.append("'" + array[i] + "'");
108:
109: if (i + 1 < array.length) {
110: sm.append(",");
111: }
112: }
113:
114: sm.append(")");
115:
116: return sm.toString();
117: }
118:
119: }
|