001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.loader;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.util.PrintfFormat;
033
034import java.util.Iterator;
035
036/**
037 * Provides methods to generate file names either for the <code>urlName</code> mapping
038 * or when using a "new" operation in the context of the direct edit interface.<p>
039 *
040 * @since 8.0.0
041 */
042public interface I_CmsFileNameGenerator {
043
044    /** The "number" macro. */
045    String MACRO_NUMBER = "number";
046
047    /** Format for file create parameter. */
048    PrintfFormat NUMBER_FORMAT = new PrintfFormat("%0.5d");
049
050    /**
051     * Returns a unique copy filename for the given base name and the parent folder.<p>
052     * For example from the given baseName 'test.txt' the copy file name 'test_copy.txt' will be generated.<p>
053     *
054     * @param cms the current OpenCms user context
055     * @param parentFolder the parent folder of the file
056     * @param baseName the base file name
057     *
058     * @return the unique file name
059     */
060    String getCopyFileName(CmsObject cms, String parentFolder, String baseName);
061
062    /**
063     * Generates a new file name based on the provided OpenCms user context and name pattern.<p>
064     *
065     * Used by the collector API as well as the galleries introduced with OpenCms 8 (ADE).
066     *
067     * @param cms the current OpenCms user context
068     * @param namePattern the pattern to be used when generating the new resource name
069     * @param defaultDigits the default number of digits to use for numbering the created file names
070     *
071     * @return a new resource name based on the provided OpenCms user context and name pattern
072     *
073     * @throws CmsException in case something goes wrong
074     */
075    String getNewFileName(CmsObject cms, String namePattern, int defaultDigits) throws CmsException;
076
077    /**
078     * Generates a new file name based on the provided OpenCms user context and name pattern.<p>
079     *
080     * Used by the collector API as well as the galleries introduced with OpenCms 8 (ADE).
081     *
082     * @param cms the current OpenCms user context
083     * @param namePattern the pattern to be used when generating the new resource name
084     * @param defaultDigits the default number of digits to use for numbering the created file names
085     * @param explorerMode if true, tries the name without a number, and also automatically inserts an underscore before the number
086     *
087     * @return a new resource name based on the provided OpenCms user context and name pattern
088     *
089     * @throws CmsException in case something goes wrong
090     */
091    String getNewFileName(CmsObject cms, String namePattern, int defaultDigits, boolean explorerMode)
092    throws CmsException;
093
094    /**
095     * Returns a unique filename for the given base name and the parent folder.<p>
096     *
097     * @param cms the current OpenCms user context
098     * @param parentFolder the parent folder of the file
099     * @param baseName the proposed file name
100     *
101     * @return the unique file name
102     */
103    String getUniqueFileName(CmsObject cms, String parentFolder, String baseName);
104
105    /**
106     * Returns a sequence of URL name candidates for the given base name as an iterator.<p>
107     *
108     * This is used by the <code>urlName</code> mapping of XML contents which enable SEO friendly URLs
109     * automatically generated for example from the resource title.<p>
110     *
111     * Usually the first URL name from this sequence which does not already exist for a different
112     * resource will be used for the URL name mapping.<p>
113     *
114     * @param baseName the base name
115     *
116     * @return the sequence of URL name candidates
117     *
118     * @throws CmsException if something goes wrong
119     */
120    Iterator<String> getUrlNameSequence(String baseName) throws CmsException;
121
122    /**
123     * Initializes this instance with an admin CMS context.
124     *
125     * @param cms the admin CMS context
126     */
127    default void setAdminCms(CmsObject cms) {}
128}