Package org.opencms.loader
Class CmsDefaultFileNameGenerator
java.lang.Object
org.opencms.loader.CmsDefaultFileNameGenerator
- All Implemented Interfaces:
I_CmsFileNameGenerator
The default class used for generating file names either for the
urlName
mapping
or when using a "new" operation in the context of the direct edit interface.- Since:
- 8.0.0
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
Factory to use for resolving the %(number) macro. -
Field Summary
Fields inherited from interface org.opencms.loader.I_CmsFileNameGenerator
MACRO_NUMBER, NUMBER_FORMAT
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptiongetCopyFileName
(CmsObject cms, String parentFolder, String baseName) Returns a unique copy filename for the given base name and the parent folder.getNewFileName
(CmsObject cms, String namePattern, int defaultDigits) Generates a new file name based on the provided OpenCms user context and name pattern.getNewFileName
(CmsObject userCms, String namePattern, int defaultDigits, boolean explorerMode) Returns a new resource name based on the provided OpenCms user context and name pattern.protected String
getNewFileNameFromList
(Set<String> fileNames, String checkPattern, int defaultDigits, boolean explorerMode) Internal method for file name generation, decoupled for testing.getUniqueFileName
(CmsObject cms, String parentFolder, String baseName) Returns a unique filename for the given base name and the parent folder.getUrlNameSequence
(String baseName) This default implementation will just generate a 5 digit sequence that is appended to the resource name in case of a collision of names.static boolean
hasNumberMacro
(String pattern) Checks the given pattern for the number macro.static String
removeExtension
(String path) Removes the file extension if it only consists of letters.void
setAdminCms
(CmsObject cms) Initializes this instance with an admin CMS context.
-
Field Details
-
COPY_FILE_NAME_INSERT
The copy file name insert.- See Also:
-
-
Constructor Details
-
CmsDefaultFileNameGenerator
public CmsDefaultFileNameGenerator()
-
-
Method Details
-
hasNumberMacro
Checks the given pattern for the number macro.- Parameters:
pattern
- the pattern to check- Returns:
true
if the pattern contains the macro
-
removeExtension
Removes the file extension if it only consists of letters.- Parameters:
path
- the path from which to remove the file extension- Returns:
- the path without the file extension
-
getCopyFileName
Description copied from interface:I_CmsFileNameGenerator
Returns a unique copy filename for the given base name and the parent folder.For example from the given baseName 'test.txt' the copy file name 'test_copy.txt' will be generated.
- Specified by:
getCopyFileName
in interfaceI_CmsFileNameGenerator
- Parameters:
cms
- the current OpenCms user contextparentFolder
- the parent folder of the filebaseName
- the base file name- Returns:
- the unique file name
- See Also:
-
getNewFileName
public String getNewFileName(CmsObject cms, String namePattern, int defaultDigits) throws CmsException Description copied from interface:I_CmsFileNameGenerator
Generates a new file name based on the provided OpenCms user context and name pattern.Used by the collector API as well as the galleries introduced with OpenCms 8 (ADE).
- Specified by:
getNewFileName
in interfaceI_CmsFileNameGenerator
- Parameters:
cms
- the current OpenCms user contextnamePattern
- the pattern to be used when generating the new resource namedefaultDigits
- the default number of digits to use for numbering the created file names- Returns:
- a new resource name based on the provided OpenCms user context and name pattern
- Throws:
CmsException
- in case something goes wrong- See Also:
-
getNewFileName
public String getNewFileName(CmsObject userCms, String namePattern, int defaultDigits, boolean explorerMode) throws CmsException Returns a new resource name based on the provided OpenCms user context and name pattern.The pattern in this default implementation must be a path which may contain the macro
%(number)
. This will be replaced by the first "n" digit sequence for which the resulting file name is not already used. For example the pattern"/file_%(number).xml"
would result in something like"/file_00003.xml"
.Alternatively, the macro can have the form
%(number:n)
withn = {1...9}
, for example%(number:6)
. In this case the default digits will be ignored and instead the digits provided as "n" will be used.- Specified by:
getNewFileName
in interfaceI_CmsFileNameGenerator
- Parameters:
cms
- the current OpenCms user contextnamePattern
- the pattern to be used when generating the new resource namedefaultDigits
- the default number of digits to use for numbering the created file namesexplorerMode
- if true, the file name is first tried without a numeric macro, also underscores are inserted automatically before the number macro and don't need to be part of the name pattern- Returns:
- a new resource name based on the provided OpenCms user context and name pattern
- Throws:
CmsException
- in case something goes wrong
-
getUniqueFileName
Description copied from interface:I_CmsFileNameGenerator
Returns a unique filename for the given base name and the parent folder.- Specified by:
getUniqueFileName
in interfaceI_CmsFileNameGenerator
- Parameters:
cms
- the current OpenCms user contextparentFolder
- the parent folder of the filebaseName
- the proposed file name- Returns:
- the unique file name
- See Also:
-
getUrlNameSequence
This default implementation will just generate a 5 digit sequence that is appended to the resource name in case of a collision of names.- Specified by:
getUrlNameSequence
in interfaceI_CmsFileNameGenerator
- Parameters:
baseName
- the base name- Returns:
- the sequence of URL name candidates
- See Also:
-
setAdminCms
Description copied from interface:I_CmsFileNameGenerator
Initializes this instance with an admin CMS context.- Specified by:
setAdminCms
in interfaceI_CmsFileNameGenerator
- Parameters:
cms
- the admin CMS context- See Also:
-
getNewFileNameFromList
protected String getNewFileNameFromList(Set<String> fileNames, String checkPattern, int defaultDigits, boolean explorerMode) Internal method for file name generation, decoupled for testing.- Parameters:
fileNames
- the list of file names already existing in the foldercheckPattern
- the pattern to be used when generating the new resource namedefaultDigits
- the default number of digits to use for numbering the created file namesexplorerMode
- if true, first the file name without a number is tried, and also an underscore is automatically inserted before the number macro- Returns:
- a new resource name based on the provided OpenCms user context and name pattern
-