| Processing Files and Application Logic |
Implement the functionality that changes the status of document segments.
Go back to the MyCustomBatchTask.cs class. This class is triggered when the user decides to run the batch task. This class implements the following interface:
// A batch task plug-in needs to implement the AbstractFileContentProcessingAutomaticTask interface public class MyCustomBatchTask : AbstractFileContentProcessingAutomaticTask
Declare a member that stores the plug-in settings as well as a string variable that is used to construct the XML stream for the task report content:
// These variables store the plug-in settings, // the report string content // and the language direction private MyCustomBatchTaskSettings _settings; private string _reportContent;
// The task is initialized // This means that the settings are retrieved, // and we start constructing the report content string protected override void OnInitializeTask() { _settings = GetSetting<MyCustomBatchTaskSettings>(); _reportContent +="<report segmentStatus='" + ((ConfirmationLevel)_settings.ConfirmationLevelSetting).ToString() + "'>"; }
You can programmatically access the file that is currently processed through the following member. In a 'Hello World'-type implementation you could just output the name and path of the processed file.
// Here we continue constructing the report string. // Also we trigger the actual task by creating a FileReader object // to which we pass the settings and the SDL XLIFF file name protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter) { // We output each file name in the report // and the date/time at which processing started _reportContent += "<file>"; _reportContent += "<fileName>" + projectFile.Name + "</fileName>"; _reportContent += "<language>" + projectFile.Language.ToString() + "</language>"; _reportContent += "<processTime>" + DateTime.Now.ToString() + "</processTime>"; // We initialize the class that performs the actual work FileReader _task = new FileReader(_settings, projectFile.LocalFilePath); multiFileConverter.AddBilingualProcessor(_task); multiFileConverter.Parse(); _reportContent += "</file>"; }
The FileReader.cs class needs to reference the following libraries:
// This class performs the actual work, it needs to implement the AbstractBilingualContentProcessor // interface to process bilingual SDL XLIFF files public class FileReader : AbstractBilingualContentProcessor
Add the following members to store the task settings, the file name and path of the SDL XLIFF file to be processed, and the text file name and path that is used to output the exported segments:
// Variables to retrieve the task settings // as well as the SDL XLIFF file patch to process and // the path of the TXT file that contains the exported content private readonly MyCustomBatchTaskSettings _taskSettings; private readonly string _inputFilePath; private StreamWriter _outFile;
With the following member we start by creating the text output file. This file is created in the same folder as the corresponding SDL XLIFF file and the *.txt extension is appended.
// We use this member to create the TXT export file public override void SetFileProperties(IFileProperties fileInfo) { _outFile = new StreamWriter(_inputFilePath + ".txt"); }
In the next step, we loop through each paragraph unit of the SDL XLIFF file. We make sure to process only paragraph units that actually contain segments. When we encounter a paragraph unit that only contains structure tags (i.e. no localizable segments), we abort. When looping through the segment pairs we write all segments with the selected confirmation status to the output text file:
// This member loops through all the segments, determines the segment status, // and then outputs the content to the text file (if applicable) public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit) { // Check if this paragraph actually contains segments // If not, it is just a structure tag content, which is not processed if (paragraphUnit.IsStructure) { return; } // If the paragraph contains segment pairs, we loop through them, // determine their confirmation status, and depending on the status // output the text content to a TXT file foreach (ISegmentPair item in paragraphUnit.SegmentPairs) { int segmentStatus = _taskSettings.ConfirmationLevelSetting; if (item.Properties.ConfirmationLevel == (ConfirmationLevel)segmentStatus) _outFile.WriteLine(item.Source + ";" + item.Target); } }
Once the file processing is done, we close the text output file.
// Here we close the TXT file public override void FileComplete() { base.FileComplete(); _outFile.Close(); }
The following member is required by the interface, although our implementation does not actually use it. The file complete member is called when processing a file is finished. However, as users could merge SDL XLIFF files, the following member must be present to determine what happens when the process has been completed for the entire merged file.
// Not really used in this implementation. // Users can merge files and process them as one. // If a single file is processed, the FileComlete member is invoked // If all single files in a merged file are completed, then the Complete member is invoked public override void Complete() { base.Complete(); }
Go back to the MyCustomBatchTask.cs class. Here we do the following:
We continue constructing the XML string for the report by adding the name of the file currently processed, its target language and the date/time at which it was processed.
We create a FileReader object to which we pass the current SDL XLIFF file name as well as our settings object:
// Here we continue constructing the report string. // Also we trigger the actual task by creating a FileReader object // to which we pass the settings and the SDL XLIFF file name protected override void ConfigureConverter(ProjectFile projectFile, IMultiFileConverter multiFileConverter) { // We output each file name in the report // and the date/time at which processing started _reportContent += "<file>"; _reportContent += "<fileName>" + projectFile.Name + "</fileName>"; _reportContent += "<language>" + projectFile.Language.ToString() + "</language>"; _reportContent += "<processTime>" + DateTime.Now.ToString() + "</processTime>"; // We initialize the class that performs the actual work FileReader _task = new FileReader(_settings, projectFile.LocalFilePath); multiFileConverter.AddBilingualProcessor(_task); multiFileConverter.Parse(); _reportContent += "</file>"; }
We complete the task by adding the closing XML report string with the closing root element. Then we generate the report using the CreateReport method implemented by the interface.
// At the end of the task, we output the public override void TaskComplete() { // We close the XML report string _reportContent += "</report>"; // We call the method that actually creates the report in Studio // In this method we pass the report name, description, the actual // XML report content string and the optional language direction parameter. CreateReport("SDK Sample Task Report", "Sample batch task plug-in report", _reportContent); }