Batch API: How to lock part of target segment

Hello,

I like to program a batch task that checks the target files for specific strings and locks them if found. Can you tell me how to achieve this, please.

I think I have to program a processor but I dont know from which class I have to inherit from. 

Is it even the right way to achieve this with a batch task?

Thanks for your help in advance.

Parents
  • Hi Robert,

    Please take a look at the Export to Excel sample application on Github:

    https://github.com/sdl/Sdl-Community/tree/master/Export%20to%20Excel/TestTask

    In order, first you have to inherit from AbstractFileContentProcessingAutomaticTask as shown at ExportToExcelTask.cs:

    * Since you want to modify the sdlxliff file make sure you override OnFileComplete and make it return true as shown below.

    The sample application does not do this because it does not modify the file.

    Fullscreen
    1
    2
    3
    4
    public override bool OnFileComplete(ProjectFile projectFile, IMultiFileConverter multiFileConverter)
    {
    return true;
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

    https://github.com/sdl/Sdl-Community/blob/master/Export%20to%20Excel/TestTask/ExportToExcelTask.cs

    Next you add your processor by calling AddBilingualProcessor:

    * It is important here you wrap it in BilingualContentHandlerAdapter and your class inherits from AbstractBilingualContentHandler

    * You could also inherit from IBilingualContentHandler directly, AbstractBilingualContentHandler is just there as a convenience so you can override only what you need

    Fullscreen
    1
    multiFileConverter.AddBilingualProcessor(new BilingualContentHandlerAdapter(new SegmentLocker(settings));
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

    The SegmentLocker class would look like this:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    public class SegmentLocker : AbstractBilingualContentHandler
    {
    private readonly MySettings settings;
    public SourceSegmentProcessor(MySettings settings)
    {
    this.settings = settings;
    }
    public override void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
    {
    // Check if it is structure unit and ignore
    if (paragraphUnit.IsStructure) { return; }
    // Check each segment and lock here
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     Check out how the sample implementation does it here. However, note it uses AbstractBilingualContentProcessor which is not for writing to the file.

    https://github.com/sdl/Sdl-Community/blob/master/Export%20to%20Excel/TestTask/FileReader.cs#L27

  • Hello Jesse,

    thank you so far. Now I am able to change the content of segements but I still have problems locking the wished parts.

    First I wrote a class that inherits from the IMarkupDataVisitor. So I was able to iterate the different content types. In the VisitText method I tried to surround text with a lock tag but I don't know how. Can you show me how?

    Then I tried to work directly on the segment pairs. There I was able to add or insert IAbstractMarkupData, but don't know how to get the index of a string from a segment, so that I can replace that string with an IAbstractMarkupData. Maybe you can help me there too?!

    Thank you.

  • Hi Robert,

    The following is *very* crude, but it should show the general idea:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    foreach (var pair in paragraphUnit.SegmentPairs)
    {
    var source = pair.Source; // This assumes there is only one IText in the source, but you need to to use the visitor pattern // In real code I would do this in VisitText
    var markup = source.First();
    if (markup is IText)
    {
    IText txt = (IText)markup;
    // Use Split here to break the segment at the point you want to insert the inline tag
    var remainderPart = txt.Split(txt.ToString().IndexOf("Hello")); // Remove the text you will replace with the inline tag
    var tmp = remainderPart.Properties.Text;
    remainderPart.Properties.Text = tmp.Replace("Hello", ""); // Create the placeholder
    var placeHolderTagProps = ItemFactory.PropertiesFactory.CreatePlaceholderTagProperties(@"<locked name=""Hello""/>");
    var placeHolderTag = ItemFactory.CreatePlaceholderTag(placeHolderTagProps);
    // The beginning part remains so add the rest afterwards
    source.Add(placeHolderTag);
    source.Add(remainderPart);
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

     All it does it take "Hello"s and make them placeholders.

    You should be able to adapt the above with your implementation of IAbstractMarkupData.

Reply
  • Hi Robert,

    The following is *very* crude, but it should show the general idea:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    foreach (var pair in paragraphUnit.SegmentPairs)
    {
    var source = pair.Source; // This assumes there is only one IText in the source, but you need to to use the visitor pattern // In real code I would do this in VisitText
    var markup = source.First();
    if (markup is IText)
    {
    IText txt = (IText)markup;
    // Use Split here to break the segment at the point you want to insert the inline tag
    var remainderPart = txt.Split(txt.ToString().IndexOf("Hello")); // Remove the text you will replace with the inline tag
    var tmp = remainderPart.Properties.Text;
    remainderPart.Properties.Text = tmp.Replace("Hello", ""); // Create the placeholder
    var placeHolderTagProps = ItemFactory.PropertiesFactory.CreatePlaceholderTagProperties(@"<locked name=""Hello""/>");
    var placeHolderTag = ItemFactory.CreatePlaceholderTag(placeHolderTagProps);
    // The beginning part remains so add the rest afterwards
    source.Add(placeHolderTag);
    source.Add(remainderPart);
    }
    }
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

     

     All it does it take "Hello"s and make them placeholders.

    You should be able to adapt the above with your implementation of IAbstractMarkupData.

Children