1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.util.ArrayUtil;
18  import com.liferay.portal.kernel.util.CharPool;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.util.Normalizer;
24  
25  /**
26   * <a href="FriendlyURLNormalizer.java.html"><b><i>View Source</i></b></a>
27   *
28   * @author Brian Wing Shun Chan
29   */
30  public class FriendlyURLNormalizer {
31  
32      public static String normalize(String friendlyURL) {
33          return normalize(friendlyURL, null);
34      }
35  
36      public static String normalize(String friendlyURL, char[] replaceChars) {
37          if (Validator.isNull(friendlyURL)) {
38              return friendlyURL;
39          }
40  
41          friendlyURL = GetterUtil.getString(friendlyURL);
42          friendlyURL = friendlyURL.toLowerCase();
43          friendlyURL = Normalizer.normalizeToAscii(friendlyURL);
44  
45          char[] charArray = friendlyURL.toCharArray();
46  
47          for (int i = 0; i < charArray.length; i++) {
48              char oldChar = charArray[i];
49  
50              char newChar = oldChar;
51  
52              if (ArrayUtil.contains(_REPLACE_CHARS, oldChar) ||
53                  ((replaceChars != null) &&
54                   ArrayUtil.contains(replaceChars, oldChar))) {
55  
56                  newChar = CharPool.DASH;
57              }
58  
59              if (oldChar != newChar) {
60                  charArray[i] = newChar;
61              }
62          }
63  
64          friendlyURL = new String(charArray);
65  
66          while (friendlyURL.contains(StringPool.DASH + StringPool.DASH)) {
67              friendlyURL = StringUtil.replace(
68                  friendlyURL, StringPool.DASH + StringPool.DASH,
69                  StringPool.DASH);
70          }
71  
72          if (friendlyURL.startsWith(StringPool.DASH)) {
73              friendlyURL = friendlyURL.substring(1, friendlyURL.length());
74          }
75  
76          if (friendlyURL.endsWith(StringPool.DASH)) {
77              friendlyURL = friendlyURL.substring(0, friendlyURL.length() - 1);
78          }
79  
80          return friendlyURL;
81      }
82  
83      private static final char[] _REPLACE_CHARS = new char[] {
84          ' ', ',', '\\', '\'', '\"', '(', ')', '{', '}', '?', '#', '@', '+',
85          '~', ';', '$', '%'
86      };
87  
88  }