View Javadoc
1   package com.acumenvelocity.ath.steps;
2   
3   import net.sf.okapi.common.Event;
4   import net.sf.okapi.common.IResource;
5   import net.sf.okapi.common.pipeline.BasePipelineStep;
6   import net.sf.okapi.common.resource.ISegments;
7   import net.sf.okapi.common.resource.ITextUnit;
8   import net.sf.okapi.common.resource.Segment;
9   import net.sf.okapi.common.resource.TextContainer;
10  
11  public abstract class BaseMergerStep extends BasePipelineStep {
12  
13    private long position;
14  
15    protected abstract void setTargetSegment(ITextUnit tu, Segment sseg, Segment tseg,
16        long position);
17  
18    @Override
19    protected Event handleStartDocument(Event event) {
20      // Reset the position within the document
21      position = 0;
22      return super.handleStartDocument(event);
23    }
24  
25    public static boolean checkTu(ITextUnit tu) {
26      if (!tu.isTranslatable()) {
27        return false;
28      }
29  
30      TextContainer source = tu.getSource();
31  
32      if (source == null) {
33        return false;
34      }
35  
36      return source.hasText();
37    }
38  
39    /**
40     * Override to handle TU in a different way
41     * @param tu
42     */
43    protected void processTu(ITextUnit tu) {
44      TextContainer source = tu.getSource();
45      TextContainer target = tu.getTarget(getTargetLocale());
46  
47      if (target == null) {
48        target = tu.createTarget(getTargetLocale(), true, IResource.COPY_ALL);
49      }
50  
51      ISegments ssegs = source.getSegments();
52      ISegments tsegs = target.getSegments();
53  
54      for (Segment sseg : ssegs) {
55        if (sseg == null) {
56          continue;
57        }
58  
59        position++;      
60        Segment tseg = tsegs.get(sseg.getId());
61        
62        if (tseg == null) {
63          continue;
64        }
65        
66        setTargetSegment(tu, sseg, tseg, position);
67      }
68    }
69  
70    @Override
71    protected Event handleTextUnit(Event event) {
72      ITextUnit tu = event.getTextUnit();
73  
74      if (checkTu(tu)) {
75        processTu(tu);
76      }
77  
78      return super.handleTextUnit(event);
79    }
80  }