In this documentation we assume you are familiar with Batch Task plugin structure and with Visitor Pattern. More information about Visitor Pattern can be found here.
Like in previous documentation we'll also use Project Anonymizer plugin for code example. Full source code of the project can be found here . SegmentVisitor.cs class contains the algorithm which recursive searches for words inside a segment and does all the logic for replacing the data.
What we want to achieve
Search in a segment for a particular text and transform that text in a placeholder. In our source code we use regular expressions find matches in segment text.
Dependencies
If you don't have referenced Sdl.FileTypeSupport.Framework.BilingualApi.dll and Sdl.FileTypeSupport.Framework.NativeApi.dll please add a reference to them.
From Sdl.FileTypeSupport.Framework.BilingualApi.dll we'll use IDocumentItemFactory interface.
From Sdl.FileTypeSupport.Framework.NativeApi.dll we'll use IPropertiesFactory interface. This two interfaces will allow is to create a placeholder object.
Brief API's concepts explications
Each segment you see in Studio editor programmatically is in fact List<IAbstractMarkupData> . For example if in one segment you have plain text and placeholder tag the list will have one IText object and an IPlaceholderTag both interfaces inherits IAbstractMarkupData.
Because the objects you see above are interfaces we don't have access to them out of the box. This is why need to use Visitor Pattern which will loop through all the elements and if the element is a placeholder, the method VisitPlaceholderTag() will be called automatically. If the element is a text VisitText() method will be called automatically.
Bellow you can see how you have access to text property for am IText object.
How to create a Placeholder tag using public API
var tag = IDocumentItemFactory.CreatePlaceholderTag(
IPropertiesFactory.CreatePlaceholderTagProperties("text which should be displayed"));
tag.Properties.SetMetaData("customKey", "customValue");
how to set custom metadata on your tag
We assume that we want to transform the IText object from VisitText() into a placeholder. We take the text from the object, create a new placeholder using the method above. All good but what we achieved until now is to create a new placeholder which is not added to the sdlxliff.
The tag we created must be inserted in the List<IAbstractMarkupData> on the exactly the same position where the IText is.
How replace the IText with IPlaceholder tag
Each object you receive in visitor patterns methods belongs to a "Parent", which is a IAbstractMarkupDataContainer. What we need to to:
- take the parent container:
var elementContainer = text.Parent;
- from the parent container we need to take all the subitems :
text.Parent.AllSubItems.ToList()
- remove the element at specified position:
elementContainer.AllSubItems.ToList()[postion].RemoveFromParent();
- insert the tag in the position we removed the text :
elementContainer.Insert(position, tag);