001    /**
002     * Copyright (c) 2000-present 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.CharPool;
019    import com.liferay.portal.kernel.util.FriendlyURLNormalizer;
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    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Shuyang Zhou
031     */
032    @DoPrivileged
033    public class FriendlyURLNormalizerImpl implements FriendlyURLNormalizer {
034    
035            @Override
036            public String normalize(String friendlyURL) {
037                    return normalize(friendlyURL, false);
038            }
039    
040            /**
041             * @deprecated As of 7.0.0, with no direct replacement
042             */
043            @Deprecated
044            @Override
045            public String normalize(String friendlyURL, Pattern friendlyURLPattern) {
046                    if (Validator.isNull(friendlyURL)) {
047                            return friendlyURL;
048                    }
049    
050                    friendlyURL = StringUtil.toLowerCase(friendlyURL);
051                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
052    
053                    Matcher matcher = friendlyURLPattern.matcher(friendlyURL);
054    
055                    friendlyURL = matcher.replaceAll(StringPool.DASH);
056    
057                    StringBuilder sb = new StringBuilder(friendlyURL.length());
058    
059                    for (int i = 0; i < friendlyURL.length(); i++) {
060                            char c = friendlyURL.charAt(i);
061    
062                            if (c == CharPool.DASH) {
063                                    if ((i == 0) || (CharPool.DASH != sb.charAt(sb.length() - 1))) {
064                                            sb.append(CharPool.DASH);
065                                    }
066                            }
067                            else {
068                                    sb.append(c);
069                            }
070                    }
071    
072                    if (sb.length() == friendlyURL.length()) {
073                            return friendlyURL;
074                    }
075    
076                    return sb.toString();
077            }
078    
079            @Override
080            public String normalizeWithPeriodsAndSlashes(String friendlyURL) {
081                    return normalize(friendlyURL, true);
082            }
083    
084            protected String normalize(String friendlyURL, boolean periodsAndSlashes) {
085                    if (Validator.isNull(friendlyURL)) {
086                            return friendlyURL;
087                    }
088    
089                    friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
090    
091                    StringBuilder sb = new StringBuilder(friendlyURL.length());
092    
093                    boolean modified = false;
094    
095                    for (int i = 0; i < friendlyURL.length(); i++) {
096                            char c = friendlyURL.charAt(i);
097    
098                            if (((CharPool.LOWER_CASE_A <= c) &&
099                                     (c <= CharPool.LOWER_CASE_Z)) ||
100                                    ((CharPool.NUMBER_0 <= c) && (c <= CharPool.NUMBER_9)) ||
101                                    (c == CharPool.UNDERLINE)) {
102    
103                                    sb.append(c);
104                            }
105                            else if ((CharPool.UPPER_CASE_A <= c) &&
106                                             (c <= CharPool.UPPER_CASE_Z)) {
107    
108                                    sb.append((char)(c + 32));
109    
110                                    modified = true;
111                            }
112                            else if (!periodsAndSlashes &&
113                                             ((c == CharPool.SLASH) || (c == CharPool.PERIOD))) {
114    
115                                    sb.append(c);
116                            }
117                            else {
118                                    if ((i == 0) || (CharPool.DASH != sb.charAt(sb.length() - 1))) {
119                                            sb.append(CharPool.DASH);
120                                    }
121    
122                                    modified = true;
123                            }
124                    }
125    
126                    if (modified) {
127                            return sb.toString();
128                    }
129    
130                    return friendlyURL;
131            }
132    
133    }