public final class FileBuilder extends Object
FileBuilder.build()
to materialize the template using
the supplied parameters/settings.
The create file(s) get names derived from the template and the existing target folder
contents (so that the filenames do not conflict with existing files). A desired
filename can be set up by FileBuilder.name(java.lang.String)
.
The file build request will be forwarded to CreateFromTemplateHandler
s; if none
CreateFromTemplateHandler.accept(org.netbeans.api.templates.CreateDescriptor)
s the request, the default procedure takes place,
depending on the #defaultMode
setting (default: FileBuilder.Mode.COPY
).
There are several values predefined:
Date
object representing the creation time
Modifier and Type | Class and Description |
---|---|
static class |
FileBuilder.Mode
Determines the default procedure for copying the template in
FileBuilder.createFromTemplate(org.openide.filesystems.FileObject, org.openide.filesystems.FileObject, java.lang.String, java.util.Map<java.lang.String, ?>, org.netbeans.api.templates.FileBuilder.Mode) . |
Modifier and Type | Field and Description |
---|---|
static String |
ATTR_TEMPLATE_HANDLER
A specific
CreateFromTemplateHandler to be used with this particular template. |
static String |
ATTR_TEMPLATE_MERGE_FOLDERS
A flag used by the default template processing to create files from template structures in existing folders
rather than create unique folder names.
|
static String |
ATTR_TEMPLATE_OPEN_FILE
Marks file(s) in the template, that should be opened in the editor.
|
Constructor and Description |
---|
FileBuilder(FileObject template,
FileObject target)
Creates a new FileBuilder for a specific template and target folder.
|
Modifier and Type | Method and Description |
---|---|
List<FileObject> |
build()
Creates the file(s) from template.
|
CreateDescriptor |
createDescriptor(boolean collectAttributes)
Creates a descriptor from the current Builder's state.
|
static FileObject |
createFromTemplate(FileObject f,
FileObject folder,
String name,
Map<String,?> attributes,
FileBuilder.Mode defaultMode)
Creates a new file based on the template.
|
FileBuilder |
defaultMode(FileBuilder.Mode m)
Specifies the behaviour to be used when no
CreateFromTemplateHandler accepts the template. |
static FileBuilder |
fromDescriptor(CreateDescriptor desc)
Creates a Builder based on the CreateDescriptor.
|
FileBuilder |
name(String n)
Sets the desired target file's name.
|
FileBuilder |
param(String n,
Object v)
Adds a parameter to the template.
|
FileBuilder |
useFormat(Format def)
Uses the specified formatter for file creation.
|
FileBuilder |
useLocale(Locale l)
Specifies the locale to be used during file creation.
|
FileBuilder |
useLookup(Lookup contextLookup)
Use specific Lookup for creation.
|
FileBuilder |
withParameters(Map<String,?> params)
Includes parameters for the template.
|
public static final String ATTR_TEMPLATE_HANDLER
CreateFromTemplateHandler
to be used with this particular template.
If present on a template, will be used as a default. Still can be overriden by a lookup-registered
registered CreateFromTemplateHandler
.public static final String ATTR_TEMPLATE_MERGE_FOLDERS
public static final String ATTR_TEMPLATE_OPEN_FILE
Boolean.TRUE
, the template handler should the file created from the file template from
the FileBuilder.build()
method. Note the attribute serves as a hint, so the Handler may choose to ignore
the instruction.public FileBuilder(@NonNull FileObject template, @NonNull FileObject target)
template
- the template to use.target
- the target folder; must already exist.@NonNull public static FileBuilder fromDescriptor(@NonNull CreateDescriptor desc)
CreateDescriptor
. The client may change the attributes.
The method may be useful when creating secondary files; for example target and all attributes
are retained. During FileBuilder.build()
, attributes may be redefined as needed for the
additional file, just like in normal Builder operation.
The new FileBuilder instance is completely indepenent of the original Descriptor. If the CreateDescriptor supports additional properties in the future, using this method guarantees that they will be transferred to the FileBuilder copy.
desc
- the original descriptorpublic FileBuilder useLookup(Lookup contextLookup)
contextLookup
- the Lookup passed to template creation subprocessespublic FileBuilder useLocale(@NonNull Locale l)
l
- the localepublic FileBuilder name(String n)
n
- the namepublic FileBuilder withParameters(@NullAllowed Map<String,?> params)
CreateDescriptor.FREE_FILE_EXTENSION
and
CreateDescriptor.PREFORMATTED_TEMPLATE
are processed and appropriate properties modified on the
CreateDescriptor.params
- the string-value pairspublic FileBuilder param(@NonNull String n, Object v)
CreateDescriptor.FREE_FILE_EXTENSION
and
CreateDescriptor.PREFORMATTED_TEMPLATE
are processed and appropriate properties modified on the
CreateDescriptor.n
- parameter namev
- the valuepublic FileBuilder defaultMode(@NonNull FileBuilder.Mode m)
CreateFromTemplateHandler
accepts the template.m
- the default processing modefor details
public FileBuilder useFormat(@NonNull Format def)
FileBuilder.Mode.FORMAT
. If the supplied Format instance happens to be a
MapFormat
, the templating code will pass parameters produced by
CreateFromTemplateAttributes
to the format when the target file
contents is generated.def
- the format to usefor details on different modes
@CheckForNull public List<FileObject> build() throws IOException
IOException
- if the creation fails@NonNull public CreateDescriptor createDescriptor(boolean collectAttributes)
CreateFromTemplateAttributes
providers;
the caller must process the providers, if it wishes to get additional attributes.
The Descriptor can be used to collect information from attribute providers or manually
trigger file creation in template handler.
The operation changes the FileBuilder state.
collectAttributes
- if true, attribute providers are asked to add their attributes
to the builder/descriptor.@CheckForNull public static FileObject createFromTemplate(@NonNull FileObject f, @NonNull FileObject folder, @NullAllowed String name, @NullAllowed Map<String,?> attributes, FileBuilder.Mode defaultMode) throws IOException
FileBuilder
introduction.
The method will collect parameters
tied to the template using registered CreateFromTemplateAttributes
providers,
and will try to locate a willing CreateFromTemplateHandler
that will create
the target file. If no such handler exists, and the defaultCopy
parameter is true,
the file contents is just copied to the target location.
If the name
parameter is null, the function attempts to compute a suitable name
from the file.
The default copy algorithm uses the supplied FileBuilder.Mode.FORMAT
to
process tokens.
If the passed name
is null
, the implementation will pick a free name based on
the template's own name (see FileUtil.findFreeFileName(org.openide.filesystems.FileObject, java.lang.String, java.lang.String)
).
f
- the template filefolder
- the target folder, must existname
- the desired name. If null
, the implementation will choose the name.attributes
- values to apply on the template. May be null
= no values.defaultMode
- the mode of operations to usenull
if no creation handler is located.IOException
- when an I/O operation fails