001/* 002 * Copyright 2013 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms; 017 018import java.text.Normalizer; 019import java.util.regex.Matcher; 020import java.util.regex.Pattern; 021 022import org.apache.commons.lang.StringUtils; 023 024/** 025 * Helper for filtering name in order to use it into 026 * a JCR node name or into a segment of an UR. 027 */ 028public final class FilterNameHelper 029{ 030 private static final Pattern __PAGE_PATTERN = Pattern.compile("^([0-9-_]*)[a-z].*$"); 031 032 private FilterNameHelper() 033 { 034 // empty 035 } 036 037 /** 038 * Filter a name for using it into an URI. 039 * @param name the name to filter. 040 * @return the name filtered. 041 */ 042 public static String filterName(String name) 043 { 044 // FIXME Page name can not start with a number 045 046 // Use lower case 047 // then remove accents 048 // then replace contiguous spaces with one dash 049 // and finally remove non-alphanumeric characters except - 050 String filteredName = Normalizer.normalize(name.toLowerCase(), Normalizer.Form.NFD).replaceAll("[\\p{InCombiningDiacriticalMarks}]", "").trim(); 051 filteredName = filteredName.replaceAll("œ", "oe").replaceAll("æ", "ae").replaceAll(" +", "-").replaceAll("[^\\w-]", "-").replaceAll("-+", "-"); 052 053 Matcher m = __PAGE_PATTERN.matcher(filteredName); 054 if (!m.matches()) 055 { 056 throw new IllegalArgumentException(filteredName + " doesn't match the expected regular expression : " + __PAGE_PATTERN.pattern()); 057 } 058 059 filteredName = filteredName.substring(m.end(1)); 060 061 // Remove characters '-' and '_' at the start and the end of the string 062 return StringUtils.strip(filteredName, "-_"); 063 } 064}