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 public static final String NUXEO_MAX_SEGMENT_SIZE_PROPERTY = PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY; 039 040 @Override 041 public String generatePathSegment(DocumentModel doc) { 042 return generatePathSegment(doc.getTitle()); 043 } 044 045 @Override 046 public String generatePathSegment(String s) { 047 if (s == null) { 048 s = ""; 049 } 050 s = s.trim(); 051 if (s.length() > getMaxSize()) { 052 s = s.substring(0, getMaxSize()).trim(); 053 } 054 s = s.replace('/', '-'); 055 s = s.replace('\\', '-'); 056 if (stupidRegexp.matcher(s).matches()) { 057 return IdUtils.generateStringId(); 058 } 059 return s; 060 } 061 062 @Override 063 public int getMaxSize() { 064 ConfigurationService cs = Framework.getService(ConfigurationService.class); 065 return Integer.parseInt(cs.getProperty(PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY, "24")); 066 } 067 068}