Class Generator
Base class for generators for generating documents based on document templates. This class also contains the interface needed for instantiating a generator.
Inherited Members
Namespace: SuperOffice.IO
Assembly: SoCore.dll
Syntax
public abstract class Generator
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(inputTemplateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
Constructors
Generator(Stream, TagContainer, GeneratorEncoding)
Protected constructor used by sub-classes
Declaration
protected Generator(Stream template, TagContainer tags, GeneratorEncoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| GeneratorEncoding | encoding | Format of the document. |
Fields
OptionSkipLtGtTags
Base class for generators for generating documents based on document templates. This class also contains the interface needed for instantiating a generator.
Declaration
public const string OptionSkipLtGtTags = "SkipLtGtTags"
Field Value
| Type | Description |
|---|---|
| string |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(inputTemplateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
RegExTag
Regular expression extracting a tag from a string. Use
tag = RegExTag.Match(tag).
There is a two-second timeout to guard against runaway regex, which can happen if the document template is chock-full of deeply nested tags. Some customers have experienced this, and it leads to 100% CPU indefinitely.
Declaration
protected static Regex RegExTag
Field Value
| Type | Description |
|---|---|
| Regex |
Remarks
The regular expression expression is formated as <code>(?<=([{<\u2264]))[a-z][a-z|0-9|?|.|#]{3}(?=[\x20\x09]*[}>\u2265])</code>
Properties
Encoding
Encoding for the template
Declaration
public GeneratorEncoding Encoding { get; set; }
Property Value
| Type | Description |
|---|---|
| GeneratorEncoding |
OldEncoding
Holds the old encoding. Used if encoding is changed during template substitution
Declaration
public GeneratorEncoding OldEncoding { get; set; }
Property Value
| Type | Description |
|---|---|
| GeneratorEncoding |
Tags
Container holding all the template tags
Declaration
public TagContainer Tags { get; }
Property Value
| Type | Description |
|---|---|
| TagContainer |
Template
Stream to read the template from.
Declaration
public Stream Template { get; }
Property Value
| Type | Description |
|---|---|
| Stream |
UiCulture
Culture used to parse multi language strings
Declaration
public string UiCulture { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
AddExtensionMapping(string, string)
Used by clients to add template extension mappings.
Declaration
public static void AddExtensionMapping(string fromExt, string toExt)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fromExt | A new template type, like ".xlsz" |
| string | toExt | An existing, known template type, like ".xlsx" |
AddTagRegex(Regex)
Adds a new regex used to find tag matches in input files
Declaration
public void AddTagRegex(Regex newTagMatchRegex)
Parameters
| Type | Name | Description |
|---|---|---|
| Regex | newTagMatchRegex | Regex that search for tags, and returns the 4 character tag name |
GenerateAsync(Stream, CancellationToken)
Generate from the Template to the Output.
Stream to output toDeclaration
public abstract Task GenerateAsync(Stream output, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | output | |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task |
GenerateAsync(CancellationToken)
Generate from the Template and return the result as a string.
Declaration
public Task<string> GenerateAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> | String containing the result of template variable substitution using the provided template. |
GetDefaultTextEncoding(GeneratorEncoding)
Base class for generators for generating documents based on document templates. This class also contains the interface needed for instantiating a generator.
Declaration
public static Encoding GetDefaultTextEncoding(GeneratorEncoding ge)
Parameters
| Type | Name | Description |
|---|---|---|
| GeneratorEncoding | ge |
Returns
| Type | Description |
|---|---|
| Encoding |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(inputTemplateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
GetEncoding(string)
Get encoding type based on a file name.
Declaration
public static GeneratorEncoding GetEncoding(string fileName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | fileName | File name to get encoding from. |
Returns
| Type | Description |
|---|---|
| GeneratorEncoding | GeneratorEncoding based on the file name. |
Remarks
Only the extension of the file name is used to identify encoding.
GetGenerator(Stream, TagContainer, GeneratorEncoding)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(Stream template, TagContainer tags, GeneratorEncoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| GeneratorEncoding | encoding | Format of the document. |
Returns
| Type | Description |
|---|---|
| Generator | Generator that can be used to generate a document. |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(templateStream, documentStream, templateTags, GeneratorEncoding.MsWord);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
GetGenerator(Stream, TagContainer, GeneratorEncoding, string)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(Stream template, TagContainer tags, GeneratorEncoding encoding, string uiCulture)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| GeneratorEncoding | encoding | Format of the document. |
| string | uiCulture | Culuture used to parse multi language strings. |
Returns
| Type | Description |
|---|---|
| Generator | Generator that can be used to generate a document. |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(templateStream, documentStream, templateTags, GeneratorEncoding.MsWord);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
GetGenerator(Stream, TagContainer, string)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(Stream template, TagContainer tags, string fileName)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| string | fileName | File name used to determine the encoding for the generator. The GetEncoding(string) method is used to determine the encoding. |
Returns
| Type | Description |
|---|---|
| Generator | Generator that can be used to generate a document. |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(templateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
GetGenerator(Stream, TagContainer, string, string)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(Stream template, TagContainer tags, string fileName, string uiCulture)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | template | Stream holding a template. |
| TagContainer | tags | Class holding values for the tags used by the generator |
| string | fileName | File name used to determine the encoding for the generator. The GetEncoding(string) method is used to determine the encoding. |
| string | uiCulture | Culuture used to parse multi language strings. |
Returns
| Type | Description |
|---|---|
| Generator | Generator that can be used to generate a document. |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(templateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
GetGenerator(string, TagContainer, GeneratorEncoding)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(string inputText, TagContainer tags, GeneratorEncoding encoding)
Parameters
| Type | Name | Description |
|---|---|---|
| string | inputText | |
| TagContainer | tags | |
| GeneratorEncoding | encoding |
Returns
| Type | Description |
|---|---|
| Generator |
GetGenerator(string, TagContainer, GeneratorEncoding, string)
Create a new instance of the generator.
Declaration
public static Generator GetGenerator(string inputText, TagContainer tags, GeneratorEncoding encoding, string uiCulture)
Parameters
| Type | Name | Description |
|---|---|---|
| string | inputText | |
| TagContainer | tags | |
| GeneratorEncoding | encoding | |
| string | uiCulture |
Returns
| Type | Description |
|---|---|
| Generator |
GetTagDataAsync(string, CancellationToken)
Obtain the value of a tag.
Declaration
protected Task<string> GetTagDataAsync(string tag, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | tag | tag to get the value for. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> | Null if it is not a valid tag, else a string containing the tag data. If it is a valid tag without any data, an empty string is returned. |
Remarks
The regular expression RegExTag is used to match the tag itself.
HasOption(string)
Base class for generators for generating documents based on document templates. This class also contains the interface needed for instantiating a generator.
Declaration
protected static bool HasOption(string option)
Parameters
| Type | Name | Description |
|---|---|---|
| string | option |
Returns
| Type | Description |
|---|---|
| bool |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(inputTemplateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);
SetOption(string)
Base class for generators for generating documents based on document templates. This class also contains the interface needed for instantiating a generator.
Declaration
public static void SetOption(string option)
Parameters
| Type | Name | Description |
|---|---|---|
| string | option |
Examples
DocumentTemplate template = new DocumentTemplate(documentInfo.DocumentTemplateInfo);
Stream templateStream = template.Load();
Document document = Document.GetDocument(documentInfo);
Generator generator = Generator.GetGenerator(inputTemplateStream, templateTags, documentInfo.DocumentTemplateInfo.FileName);
Stream documentStream = ProviderStream.GetProviderStream(document, ProviderStream.AccessMode.Write);
generator.Generate(documentStream);