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