001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.api.pathsegment; 020 021import java.util.regex.Pattern; 022 023import org.nuxeo.common.utils.IdUtils; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.runtime.api.Framework; 026import org.nuxeo.runtime.services.config.ConfigurationService; 027 028/** 029 * Service generating a path segment from the title by just removing slashes and limiting size. 030 */ 031public class PathSegmentServiceDefault implements PathSegmentService { 032 033 public Pattern stupidRegexp = Pattern.compile("^[- .,;?!:/\\\\'\"]*$"); 034 035 /** 036 * @deprecated since 7.4, use {@link PathSegmentService#NUXEO_MAX_SEGMENT_SIZE_PROPERTY} instead 037 */ 038 @Deprecated 039 public static final String NUXEO_MAX_SEGMENT_SIZE_PROPERTY = PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY; 040 041 @Override 042 public String generatePathSegment(DocumentModel doc) { 043 return generatePathSegment(doc.getTitle()); 044 } 045 046 @Override 047 public String generatePathSegment(String s) { 048 if (s == null) { 049 s = ""; 050 } 051 s = s.trim(); 052 if (s.length() > getMaxSize()) { 053 s = s.substring(0, getMaxSize()).trim(); 054 } 055 s = s.replace('/', '-'); 056 s = s.replace('\\', '-'); 057 if (stupidRegexp.matcher(s).matches()) { 058 return IdUtils.generateStringId(); 059 } 060 return s; 061 } 062 063 @Override 064 public int getMaxSize() { 065 ConfigurationService cs = Framework.getService(ConfigurationService.class); 066 return cs.getInteger(PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY, 24); 067 } 068 069}