001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 11/07/2005 00:32:01
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util;
044:
045: /**
046: * Normalizes an URL.
047: * Normalization means replacing blank spaces by underlines,
048: * changing special chars by its regular form and etc.
049: * @author Rafael Steil
050: * @version $Id: URLNormalizer.java,v 1.4 2006/08/20 22:47:42 rafaelsteil Exp $
051: */
052: public class URLNormalizer {
053: public static final int LIMIT = 30;
054:
055: /**
056: * Normalizes an URL.
057: * The url will be truncated at {@link #LIMIT} chars
058: * @param url the url to normalize
059: * @return the normalized url
060: * @see #normalize(String, int, boolean)
061: */
062: public static String normalize(String url) {
063: return normalize(url, LIMIT, true);
064: }
065:
066: /**
067: *
068: * @param url the url to normalize
069: * @param limit do not process more than <code>limit + 1</code> chars
070: * @param friendlyTruncate If <code>true</code>, will try to not cut a word if
071: * more than <code>limit</code> chars were processed. It will stop in the next
072: * special char
073: * @return the normalized url
074: */
075: public static String normalize(String url, int limit,
076: boolean friendlyTruncate) {
077: char[] chars = url.toCharArray();
078:
079: StringBuffer sb = new StringBuffer(url.length());
080:
081: for (int i = 0; i < chars.length; i++) {
082: if (i <= limit
083: || (friendlyTruncate && i > limit && sb.charAt(sb
084: .length() - 1) != '_')) {
085:
086: if (Character.isSpaceChar(chars[i]) || chars[i] == '-') {
087: if (friendlyTruncate && i > limit) {
088: break;
089: }
090:
091: if (i > 0 && sb.charAt(sb.length() - 1) != '_') {
092: sb.append('_');
093: }
094: }
095:
096: if (Character.isLetterOrDigit(chars[i])) {
097: sb.append(chars[i]);
098: } else if (friendlyTruncate && i > limit) {
099: break;
100: }
101: }
102: }
103:
104: return sb.toString().toLowerCase();
105: }
106: }
|