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    import java.util.regex.Pattern;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Shuyang Zhou
033     */
034    @DoPrivileged
035    public class FriendlyURLNormalizerImpl implements FriendlyURLNormalizer {
036    
037            @Override
038            public String normalize(String friendlyURL) {
039                    return normalize(friendlyURL, _friendlyURLPattern);
040            }
041    
042            /**
043             * @deprecated As of 6.2.0, replaced by {@link #normalize(String, Pattern)}
044             */
045            @Override
046            public String normalize(String friendlyURL, char[] replaceChars) {
047                    if (Validator.isNull(friendlyURL)) {
048                            return friendlyURL;
049                    }
050    
051                    friendlyURL = GetterUtil.getString(friendlyURL);
052                    friendlyURL = StringUtil.toLowerCase(friendlyURL);
053                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
054    
055                    StringBuilder sb = null;
056    
057                    int index = 0;
058    
059                    for (int i = 0; i < friendlyURL.length(); i++) {
060                            char c = friendlyURL.charAt(i);
061    
062                            if ((Arrays.binarySearch(_REPLACE_CHARS, c) >= 0) ||
063                                    ((replaceChars != null) &&
064                                     ArrayUtil.contains(replaceChars, c))) {
065    
066                                    if (sb == null) {
067                                            sb = new StringBuilder();
068                                    }
069    
070                                    if (i > index) {
071                                            sb.append(friendlyURL.substring(index, i));
072                                    }
073    
074                                    sb.append(CharPool.DASH);
075    
076                                    index = i + 1;
077                            }
078                    }
079    
080                    if (sb != null) {
081                            if (index < friendlyURL.length()) {
082                                    sb.append(friendlyURL.substring(index));
083                            }
084    
085                            friendlyURL = sb.toString();
086                    }
087    
088                    while (friendlyURL.contains(StringPool.DOUBLE_DASH)) {
089                            friendlyURL = StringUtil.replace(
090                                    friendlyURL, StringPool.DOUBLE_DASH, StringPool.DASH);
091                    }
092    
093                    /*if (friendlyURL.startsWith(StringPool.DASH)) {
094                            friendlyURL = friendlyURL.substring(1);
095                    }
096    
097                    if (friendlyURL.endsWith(StringPool.DASH)) {
098                            friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
099                    }*/
100    
101                    return friendlyURL;
102            }
103    
104            @Override
105            public String normalize(String friendlyURL, Pattern friendlyURLPattern) {
106                    if (Validator.isNull(friendlyURL)) {
107                            return friendlyURL;
108                    }
109    
110                    friendlyURL = StringUtil.toLowerCase(friendlyURL);
111                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
112                    friendlyURL = friendlyURL.replaceAll(
113                            friendlyURLPattern.pattern(), StringPool.DASH);
114                    friendlyURL = friendlyURL.replaceAll(
115                            _friendlyURLHyphenPattern.pattern(), StringPool.DASH);
116    
117                    return friendlyURL;
118            }
119    
120            private static final char[] _REPLACE_CHARS;
121    
122            static {
123                    char[] replaceChars = new char[] {
124                            ' ', ',', '\\', '\'', '\"', '(', ')', '[', ']', '{', '}', '?', '#',
125                            '@', '+', '~', ';', '$', '%', '!', '=', ':', '&', '\u00a3',
126                            '\u2013', '\u2014', '\u2018', '\u2019', '\u201c', '\u201d'
127                    };
128    
129                    Arrays.sort(replaceChars);
130    
131                    _REPLACE_CHARS = replaceChars;
132            }
133    
134            private static Pattern _friendlyURLHyphenPattern = Pattern.compile(
135                    "(-)\\1+");
136            private static Pattern _friendlyURLPattern = Pattern.compile(
137                    "[^a-z0-9./_-]");
138    
139    }