public class Generator extends Object
This uses a builder like pattern (a.k.a. fluent API) and requires to either call generate()
or
generateTo(OutputStream)
to finally generate the content.
Modifier and Type | Field and Description |
---|---|
static String |
ENCODING
If a specific output encoding is required (other than the system encoding - most definitely UTF-8) a variable
using this key can be supplied to the generator, specifying the name of the encoding to use.
|
Modifier | Constructor and Description |
---|---|
protected |
Generator() |
Modifier and Type | Method and Description |
---|---|
Generator |
applyContext(sirius.kernel.commons.Context ctx)
Applies the context to the generator.
|
Generator |
direct(String templateCode,
String handlerType)
Sets the template code to be used directly as string.
|
Generator |
encoding(String encoding)
Sets the output encoding which is used to generate the output files.
|
String |
generate()
Invokes the appropriate
ContentHandler and returns the generated content handler as string. |
Attachment |
generateAttachment(String filename)
Convenience method to directly create an
Attachment which can be added to an email. |
void |
generateTo(OutputStream out)
Calls the appropriate
ContentHandler to generate the output which is written into the given
output stream. |
sirius.kernel.commons.Context |
getContext()
Can be used by a
ContentHandler to access the context which contains all previously set variables. |
String |
getEncoding()
Can be used by a
ContentHandler to determine the effective encoding used for the generated output. |
String |
getHandlerType()
Contains the handler type.
|
InputStream |
getTemplate()
Uses the
Resolver implementations or the classloader to load the template as input stream. |
String |
getTemplateCode()
Can be used by a
ContentHandler to obtain a preset templateCode. |
String |
getTemplateName()
Can be used by a
ContentHandler to obtain a preset templateName. |
Generator |
handler(String handlerType)
Specifies which
ContentHandler is used to generate the content. |
boolean |
isTemplateEndsWith(String extension)
Can be used by a
ContentHandler to determine the effective ending of the underlying template name. |
boolean |
isTemplateFileExtension(String extension)
Can be used by a
ContentHandler to determine the file ending of the selected template. |
Generator |
put(String key,
Object value)
Adds a variable with the given name (key) and value to the internal context.
|
Generator |
useTemplate(String templateName)
Determines which template file should be used.
|
public static final String ENCODING
If possible however it is preferable to use encoding(String)
to set the encoding.
public Generator applyContext(sirius.kernel.commons.Context ctx)
This will join the given context with the one previously set (Or the system context). All values with the same name will be overwritten using the values in the given context.
ctx
- the context to be applied to the one already presentpublic Generator put(String key, Object value)
If a value with the same key was already defined, it will be overwritten.
key
- the name of the variable to setvalue
- the value of the variable to setpublic Generator encoding(String encoding)
encoding
- the encoding to use for output filespublic Generator useTemplate(String templateName)
The content is resolved by calling Resources.resolve(String)
.
templateName
- the name of the template to usepublic Generator direct(String templateCode, String handlerType)
Most probably this will be velocity code. Once a direct code is set, the template specified by
useTemplate(String)
will be ignored.
templateCode
- the template code to evaluatehandlerType
- String reference for the handler to be used
(i.e. TagliatelleContentHandler.PASTA
)public Generator handler(String handlerType)
ContentHandler
is used to generate the content.
Most of the time, the content handler is auto-detected using the file name of the template. An example
would be .pdf.vm which will force the TagliatellePDFContentHandler
to generate a PDF file using the template. However, by using generator.handler("pdf-vm")
it can be ensured, that this handler is picked, without relying on the file name.
handlerType
- the name of the handler type to use. Constants can be found by looking at the
Register
annotations of the implementing classes of ContentHandler
.public void generateTo(OutputStream out)
ContentHandler
to generate the output which is written into the given
output stream.out
- the output stream to which the generated content is writtenpublic Attachment generateAttachment(@Nonnull String filename)
Attachment
which can be added to an email.
Internally a buffer is created and generateTo(OutputStream)
is invoked to generate
the actual content of the attachment.
filename
- the filename of the attachment.MailSender.addAttachment(DataSource)
.public String generate()
ContentHandler
and returns the generated content handler as string.
Most probably the input will be a velocity template which generates readable text (which also might be XML or HTML).
public String getTemplateName()
ContentHandler
to obtain a preset templateName.public String getTemplateCode()
ContentHandler
to obtain a preset templateCode.public sirius.kernel.commons.Context getContext()
ContentHandler
to access the context which contains all previously set variables.public boolean isTemplateFileExtension(@Nonnull String extension)
ContentHandler
to determine the file ending of the selected template. This is
used to select which content handler is actually used to generate the output.extension
- the expected file extension, without a "." at the beginningpublic boolean isTemplateEndsWith(@Nonnull String extension)
ContentHandler
to determine the effective ending of the underlying template name.extension
- the expected end of the file name.isTemplateFileExtension(String)
this will not consider the first "." to be the
file extension but rather really check if the template name ends with the given extension. Therefore
for a template named test.js.vm this will return true for
isTemplateEndsWith(".vm")
but false for isTemplateFileExtension("vm")
public String getEncoding()
ContentHandler
to determine the effective encoding used for the generated output.
This is either set via encoding(String)
or by placing a variable named ENCODING
in the
context or it is the default encoding used by the JVM (most probably UTF-8).public String getHandlerType()
ContentHandler
to skip all filename checks and
always generate its output.handler(String)
or null if no handler type
was set.public InputStream getTemplate()
Resolver
implementations or the classloader to load the template as input stream.Copyright © 2018. All rights reserved.