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            public String normalize(String friendlyURL) {
037                    return normalize(friendlyURL, null);
038            }
039    
040            public String normalize(String friendlyURL, char[] replaceChars) {
041                    if (Validator.isNull(friendlyURL)) {
042                            return friendlyURL;
043                    }
044    
045                    friendlyURL = GetterUtil.getString(friendlyURL);
046                    friendlyURL = friendlyURL.toLowerCase();
047                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
048    
049                    StringBuilder sb = null;
050    
051                    int index = 0;
052    
053                    for (int i = 0; i < friendlyURL.length(); i++) {
054                            char c = friendlyURL.charAt(i);
055    
056                            if ((Arrays.binarySearch(_REPLACE_CHARS, c) >= 0) ||
057                                    ((replaceChars != null) &&
058                                     ArrayUtil.contains(replaceChars, c))) {
059    
060                                    if (sb == null) {
061                                            sb = new StringBuilder();
062                                    }
063    
064                                    if (i > index) {
065                                            sb.append(friendlyURL.substring(index, i));
066                                    }
067    
068                                    sb.append(CharPool.DASH);
069    
070                                    index = i + 1;
071                            }
072                    }
073    
074                    if (sb != null) {
075                            if (index < friendlyURL.length()) {
076                                    sb.append(friendlyURL.substring(index));
077                            }
078    
079                            friendlyURL = sb.toString();
080                    }
081    
082                    while (friendlyURL.contains(StringPool.DOUBLE_DASH)) {
083                            friendlyURL = StringUtil.replace(
084                                    friendlyURL, StringPool.DOUBLE_DASH, StringPool.DASH);
085                    }
086    
087                    /*if (friendlyURL.startsWith(StringPool.DASH)) {
088                            friendlyURL = friendlyURL.substring(1);
089                    }
090    
091                    if (friendlyURL.endsWith(StringPool.DASH)) {
092                            friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
093                    }*/
094    
095                    return friendlyURL;
096            }
097    
098            private static final char[] _REPLACE_CHARS;
099    
100            static {
101                    char[] replaceChars = new char[] {
102                            ' ', ',', '\\', '\'', '\"', '(', ')', '[', ']', '{', '}', '?', '#',
103                            '@', '+', '~', ';', '$', '%', '!', '=', ':', '&'
104                    };
105    
106                    Arrays.sort(replaceChars);
107    
108                    _REPLACE_CHARS = replaceChars;
109            }
110    
111    }