Package org.opencms.loader
Class CmsDefaultFileNameGenerator
- java.lang.Object
-
- org.opencms.loader.CmsDefaultFileNameGenerator
-
- All Implemented Interfaces:
I_CmsFileNameGenerator
public class CmsDefaultFileNameGenerator extends java.lang.Object implements I_CmsFileNameGenerator
The default class used for generating file names either for theurlName
mapping or when using a "new" operation in the context of the direct edit interface.- Since:
- 8.0.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
CmsDefaultFileNameGenerator.CmsNumberFactory
Factory to use for resolving the %(number) macro.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
COPY_FILE_NAME_INSERT
The copy file name insert.-
Fields inherited from interface org.opencms.loader.I_CmsFileNameGenerator
MACRO_NUMBER, NUMBER_FORMAT
-
-
Constructor Summary
Constructors Constructor Description CmsDefaultFileNameGenerator()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
getCopyFileName(CmsObject cms, java.lang.String parentFolder, java.lang.String baseName)
Returns a unique copy filename for the given base name and the parent folder.java.lang.String
getNewFileName(CmsObject cms, java.lang.String namePattern, int defaultDigits)
Generates a new file name based on the provided OpenCms user context and name pattern.java.lang.String
getNewFileName(CmsObject userCms, java.lang.String namePattern, int defaultDigits, boolean explorerMode)
Returns a new resource name based on the provided OpenCms user context and name pattern.protected java.lang.String
getNewFileNameFromList(java.util.Set<java.lang.String> fileNames, java.lang.String checkPattern, int defaultDigits, boolean explorerMode)
Internal method for file name generation, decoupled for testing.java.lang.String
getUniqueFileName(CmsObject cms, java.lang.String parentFolder, java.lang.String baseName)
Returns a unique filename for the given base name and the parent folder.java.util.Iterator<java.lang.String>
getUrlNameSequence(java.lang.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(java.lang.String pattern)
Checks the given pattern for the number macro.static java.lang.String
removeExtension(java.lang.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 Detail
-
COPY_FILE_NAME_INSERT
public static final java.lang.String COPY_FILE_NAME_INSERT
The copy file name insert.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CmsDefaultFileNameGenerator
public CmsDefaultFileNameGenerator()
-
-
Method Detail
-
hasNumberMacro
public static boolean hasNumberMacro(java.lang.String pattern)
Checks the given pattern for the number macro.- Parameters:
pattern
- the pattern to check- Returns:
true
if the pattern contains the macro
-
removeExtension
public static java.lang.String removeExtension(java.lang.String path)
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
public java.lang.String getCopyFileName(CmsObject cms, java.lang.String parentFolder, java.lang.String baseName)
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:
I_CmsFileNameGenerator.getCopyFileName(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
-
getNewFileName
public java.lang.String getNewFileName(CmsObject cms, java.lang.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:
I_CmsFileNameGenerator.getNewFileName(org.opencms.file.CmsObject, java.lang.String, int)
-
getNewFileName
public java.lang.String getNewFileName(CmsObject userCms, java.lang.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
public java.lang.String getUniqueFileName(CmsObject cms, java.lang.String parentFolder, java.lang.String baseName)
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:
I_CmsFileNameGenerator.getUniqueFileName(org.opencms.file.CmsObject, java.lang.String, java.lang.String)
-
getUrlNameSequence
public java.util.Iterator<java.lang.String> getUrlNameSequence(java.lang.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.- Specified by:
getUrlNameSequence
in interfaceI_CmsFileNameGenerator
- Parameters:
baseName
- the base name- Returns:
- the sequence of URL name candidates
- See Also:
I_CmsFileNameGenerator.getUrlNameSequence(java.lang.String)
-
setAdminCms
public void setAdminCms(CmsObject cms)
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:
I_CmsFileNameGenerator.setAdminCms(org.opencms.file.CmsObject)
-
getNewFileNameFromList
protected java.lang.String getNewFileNameFromList(java.util.Set<java.lang.String> fileNames, java.lang.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
-
-