Using DiffAlgorithm to Generate Differences
public Difference  diff(Document mod, Document orig,
SxwDocument wdoc1 = (SxwDocument) orig;
SxwDocument wdoc2 = (SxwDocument) mod;
Document doc1 = wdoc1.getContentDOM();
Document doc2 = wdoc2.getContentDOM();
Iterator i1 = new ParaNodeIterator(cc, doc1.getDocumentElement());
Iterator i2 = new ParaNodeIterator(cc, doc2.getDocumentElement());
DiffAlgorithm diffAlgo = new IteratorLCSAlgorithm();
// find out the paragraph level diffs
Difference diffTable = diffAlgo.computeDiffs(i1, i2);
Example 6: Difference Generation
The DOM trees of both documents are retrieved and an appropriate iterator - the ParaNodeIterator class in
this case - is instantiated for each DOM tree.
The iterator takes as a parameter to its constructor an instance of the ConverterCapabilities class for the
specific document type. The
specify which OpenOffice document tags and
attributes are supported on the device document format.
Next, an instance of an appropriate
is created, in this case the
vector is created. This vector forms the input to the merge.
Note that the merge algorithm needs access to the iterators used to generate the differences - they are
an input to the
method call during merge.
The merge process works on the principle of taking two DOM trees and iterating over them to create a new
DOM tree that represents the "most-up-to-date" version of the document. Nodes for the new merged DOM
tree are selected based on the differences generated by the difference process as described above.
The present merge API supports a 2 way merge -this is when you modify a document on a device and you
want to merge it back to the original OpenOffice document.
3-way merges are not currently supported. A 3-way merge is required when both the original document
and the device document have been modified, and the changes from both need to be merged
The MergeAlgorithm Interface
interface has a single method,
which takes as input two iterators
- an iterator for the original document, and an iterator for the modified document - and the generated
vector. All merge algorithms should implement this interface. In order to carry out a merge you
need to instantiate an instance of a class that implements the
interface and call its
The code fragment below illustrates the
method of the class that implements the
DocumentMerger interface for a fictitious device document.
Developing a Plugin
Page 20 of 25