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.portal.util;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.util.Normalizer;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class FriendlyURLNormalizer {
029    
030            public static String normalize(String friendlyURL) {
031                    return normalize(friendlyURL, null);
032            }
033    
034            public static String normalize(String friendlyURL, char[] replaceChars) {
035                    if (Validator.isNull(friendlyURL)) {
036                            return friendlyURL;
037                    }
038    
039                    friendlyURL = GetterUtil.getString(friendlyURL);
040                    friendlyURL = friendlyURL.toLowerCase();
041                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
042    
043                    char[] charArray = friendlyURL.toCharArray();
044    
045                    for (int i = 0; i < charArray.length; i++) {
046                            char oldChar = charArray[i];
047    
048                            char newChar = oldChar;
049    
050                            if (ArrayUtil.contains(_REPLACE_CHARS, oldChar) ||
051                                    ((replaceChars != null) &&
052                                     ArrayUtil.contains(replaceChars, oldChar))) {
053    
054                                    newChar = CharPool.DASH;
055                            }
056    
057                            if (oldChar != newChar) {
058                                    charArray[i] = newChar;
059                            }
060                    }
061    
062                    friendlyURL = new String(charArray);
063    
064                    while (friendlyURL.contains(StringPool.DASH + StringPool.DASH)) {
065                            friendlyURL = StringUtil.replace(
066                                    friendlyURL, StringPool.DASH + StringPool.DASH,
067                                    StringPool.DASH);
068                    }
069    
070                    if (friendlyURL.startsWith(StringPool.DASH)) {
071                            friendlyURL = friendlyURL.substring(1, friendlyURL.length());
072                    }
073    
074                    if (friendlyURL.endsWith(StringPool.DASH)) {
075                            friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
076                    }
077    
078                    return friendlyURL;
079            }
080    
081            private static final char[] _REPLACE_CHARS = new char[] {
082                    ' ', ',', '\\', '\'', '\"', '(', ')', '{', '}', '?', '#', '@', '+',
083                    '~', ';', '$', '%'
084            };
085    
086    }