001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Brian Wing Shun Chan
027     */
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            /**
045             * @deprecated Use <code>encodeURIComponent</code>.
046             */
047            public static String escape(String s) {
048                    return encodeURIComponent(s);
049            }
050    
051            /**
052             * @deprecated Use <code>decodeURIComponent</code>.
053             */
054            public static String unescape(String s) {
055                    return decodeURIComponent(s);
056            }
057    
058            public static String encodeURIComponent(String s) {
059    
060                    // Encode URL
061    
062                    try {
063                            s = URLEncoder.encode(s, StringPool.UTF8);
064                    }
065                    catch (Exception e) {
066                    }
067    
068                    // Adjust for JavaScript specific annoyances
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                    // Get rid of all unicode
079    
080                    s = s.replaceAll("%u[0-9a-fA-F]{4}", StringPool.BLANK);
081    
082                    // Adjust for JavaScript specific annoyances
083    
084                    s = StringUtil.replace(s, "+", "%2B");
085                    s = StringUtil.replace(s, "%20", "+");
086    
087                    // Decode URL
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    }