The File Parser
When you build a filter that uses the Native API for monolingual file formats, your parser must connect to the File Type Support Framework through INativeFileTypeComponent and INativeFileParser. In most cases, the parser also implements INativeContentCycleAware, which provides information such as the original file path, source language, and encoding. The framework calls these members at key stages of parsing so that your filter can initialize, control the parsing flow, and clean up correctly.
The simplest way to implement a native parser is to derive from AbstractNativeFileParser and implement INativeContentCycleAware. You can then use the helper methods that AbstractNativeFileParser provides.
public class SimpleTextParser : AbstractNativeFileParser, INativeContentCycleAware, ISettingsAware
{
...
}
Using AbstractNativeFileParser
The AbstractNativeFileParser base class already implements INativeFilterComponent and INativeFileParser. This leaves your derived class to implement INativeContentCycleAware and, if needed, ISettingsAware. It also exposes overridable methods that simplify the implementation of a native parser.
- BeforeParsing: Called before parsing begins and before
DuringParsing()is called. Override this method to initialize variables, open files, or perform similar setup tasks after you call the base implementation. - DuringParsing: Called after the initial
BeforeParsing()call and then repeatedly for each chunk of work that the parser must process. Returntruewhile parsing should continue andfalsewhen parsing is complete. - AfterParsing: Called after
DuringParsing()returnsfalse. Override this method to close files or perform other cleanup tasks. - OnProgress: Reports file parsing progress to the framework and the user. It takes a byte value between
0and100. - OutputText: Sends translatable text to the File Type Support Framework API. For more information, see Implement the File Parser.
- Dispose: Can be overridden to release resources if the Filter Framework throws an unhandled exception. This is the last opportunity to release managed or unmanaged resources. Typical patterns include acquiring resources in
StartOfInput()and releasing them inEndOfInput(), or acquiring resources inBeforeParsing()and releasing them inAfterParsing().
Accessing file information
To access essential information such as the name of the file being processed, use INativeContentCycleAware. This interface includes SetFileProperties(). A typical implementation stores the IFileProperties parameter in a class field. One of the most useful members is FileConversionProperties, which exposes the following properties:
- OriginalFilePath: The full path to the monolingual source file.
- OriginalEncoding: The original encoding of the source file, if known.
- SourceLanguage: The original source language of the file's translatable content.
- FileSnifferInfo: Information produced by a file sniffer component. In many cases, the most useful value is
DetectedEncoding. - DependencyFiles: The list of dependency files that the parser uses or creates during parsing. This list can include the original source document. The framework stores these files in the intermediate bilingual format, then decodes them to temporary file names and makes them available to the Native API writer during native file generation. The set of dependency files can change while parsing is in progress, but it should not change after
INativeContentCycleAware.EndOfInput()is called.
Implementing INativeContentCycleAware
INativeContentCycleAware also defines two additional methods that your parser must implement. If you use the flow-control methods in AbstractNativeFileParser, these implementations may not need much code.
SetFileProperties(): Stores the file properties that the framework passes to the parser.- StartOfInput: Called after component initialization, including
SetFileProperties(), but before the framework parses and passes any content to filter components. - EndOfInput: Called after the framework finishes processing the native content.
Implementing ISettingsAware
If your parser needs access to user or file type settings, implement ISettingsAware.
- InitializeSettings: Passes an
ISettingsBundleobject and aconfigurationId(FileTypeConfigurationId). Use these values to populate the settings object that the parser requires.
public void InitializeSettings(Sdl.Core.Settings.ISettingsBundle settingsBundle, string configurationId)
{
UserSettings _userSettings = new UserSettings();
_userSettings.PopulateFromSettingsBundle(settingsBundle, configurationId);
LockPrdCodes = _userSettings.LockPrdCodes;
}
Note
This content may be out-of-date. To check the latest information on this topic, inspect the libraries using the Visual Studio Object Browser.