Wiki

Clone wiki

XLIFF Toolkit / Rewriting

Rewriting a Document

<Table of Content>

Using XLIFFReader and XLIFFWriter you can easily read a document, make some changes and re-write it.

To re-write you simply pass the events provided by the reader to the writer. If you need to perform some modifications, make them in the corresponding resource before passing the event to the writer.

For example, the following code adds a note on each <unit> element in the input document:

#!java
try (
   XLIFFReader reader = new XLIFFReader();
   XLIFFWriter writer = new XLIFFWriter()
)
{
   reader.open(new File("myDocument1.xlf"));
   writer.create(new File("myDocument2.xlf"), null);
   // Loop through the events
   while ( reader.hasNext() ) {
      Event event = reader.next();
      // Do whatever modifications you need
      // Here we add a note to any unit we find
      if ( event.isUnit() ) {
         Unit unit = event.getUnit();
         unit.getNotes().add(new Note("My note!"));
      }
      // Then re-write each event
      writer.writeEvent(event);
   }
   // Call writer.close() and reader.close() here
   // if you don't use a try-with-resources like this example.
}

Note that you can also use the XLIFFProcessor class to perform the same type of tasks with less code.

Updated