001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012package org.nuxeo.ecm.core.api.pathsegment; 013 014import java.util.regex.Pattern; 015 016import org.nuxeo.common.utils.IdUtils; 017import org.nuxeo.ecm.core.api.DocumentModel; 018import org.nuxeo.runtime.api.Framework; 019import org.nuxeo.runtime.services.config.ConfigurationService; 020 021/** 022 * Service generating a path segment from the title by just removing slashes and limiting size. 023 */ 024public class PathSegmentServiceDefault implements PathSegmentService { 025 026 public Pattern stupidRegexp = Pattern.compile("^[- .,;?!:/\\\\'\"]*$"); 027 028 /** 029 * @deprecated since 7.4, use {@link PathSegmentService#NUXEO_MAX_SEGMENT_SIZE_PROPERTY} instead 030 */ 031 public static final String NUXEO_MAX_SEGMENT_SIZE_PROPERTY = PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY; 032 033 @Override 034 public String generatePathSegment(DocumentModel doc) { 035 return generatePathSegment(doc.getTitle()); 036 } 037 038 @Override 039 public String generatePathSegment(String s) { 040 if (s == null) { 041 s = ""; 042 } 043 s = s.trim(); 044 if (s.length() > getMaxSize()) { 045 s = s.substring(0, getMaxSize()).trim(); 046 } 047 s = s.replace('/', '-'); 048 s = s.replace('\\', '-'); 049 if (stupidRegexp.matcher(s).matches()) { 050 return IdUtils.generateStringId(); 051 } 052 return s; 053 } 054 055 @Override 056 public int getMaxSize() { 057 ConfigurationService cs = Framework.getService(ConfigurationService.class); 058 return Integer.parseInt(cs.getProperty(PathSegmentService.NUXEO_MAX_SEGMENT_SIZE_PROPERTY, "24")); 059 } 060 061}