001    /**
002     * Copyright (c) 2000-2013 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.portal.util;
016    
017    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.FriendlyURLNormalizer;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    import com.liferay.util.Normalizer;
026    
027    import java.util.Arrays;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    @DoPrivileged
034    public class FriendlyURLNormalizerImpl implements FriendlyURLNormalizer {
035    
036            @Override
037            public String normalize(String friendlyURL) {
038                    return normalize(friendlyURL, null);
039            }
040    
041            @Override
042            public String normalize(String friendlyURL, char[] replaceChars) {
043                    if (Validator.isNull(friendlyURL)) {
044                            return friendlyURL;
045                    }
046    
047                    friendlyURL = GetterUtil.getString(friendlyURL);
048                    friendlyURL = friendlyURL.toLowerCase();
049                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
050    
051                    StringBuilder sb = null;
052    
053                    int index = 0;
054    
055                    for (int i = 0; i < friendlyURL.length(); i++) {
056                            char c = friendlyURL.charAt(i);
057    
058                            if ((Arrays.binarySearch(_REPLACE_CHARS, c) >= 0) ||
059                                    ((replaceChars != null) &&
060                                     ArrayUtil.contains(replaceChars, c))) {
061    
062                                    if (sb == null) {
063                                            sb = new StringBuilder();
064                                    }
065    
066                                    if (i > index) {
067                                            sb.append(friendlyURL.substring(index, i));
068                                    }
069    
070                                    sb.append(CharPool.DASH);
071    
072                                    index = i + 1;
073                            }
074                    }
075    
076                    if (sb != null) {
077                            if (index < friendlyURL.length()) {
078                                    sb.append(friendlyURL.substring(index));
079                            }
080    
081                            friendlyURL = sb.toString();
082                    }
083    
084                    while (friendlyURL.contains(StringPool.DOUBLE_DASH)) {
085                            friendlyURL = StringUtil.replace(
086                                    friendlyURL, StringPool.DOUBLE_DASH, StringPool.DASH);
087                    }
088    
089                    /*if (friendlyURL.startsWith(StringPool.DASH)) {
090                            friendlyURL = friendlyURL.substring(1);
091                    }
092    
093                    if (friendlyURL.endsWith(StringPool.DASH)) {
094                            friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
095                    }*/
096    
097                    return friendlyURL;
098            }
099    
100            private static final char[] _REPLACE_CHARS;
101    
102            static {
103                    char[] replaceChars = new char[] {
104                            ' ', ',', '\\', '\'', '\"', '(', ')', '[', ']', '{', '}', '?', '#',
105                            '@', '+', '~', ';', '$', '%', '!', '=', ':', '&', '\u00a3',
106                            '\u2018', '\u2019', '\u201c', '\u201d'
107                    };
108    
109                    Arrays.sort(replaceChars);
110    
111                    _REPLACE_CHARS = replaceChars;
112            }
113    
114    }