1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.sxw.wordsmith;
25 
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
28 
29 import org.openoffice.xmerge.DocumentMerger;
30 import org.openoffice.xmerge.MergeException;
31 import org.openoffice.xmerge.ConverterCapabilities;
32 import org.openoffice.xmerge.converter.xml.sxw.SxwDocument;
33 import org.openoffice.xmerge.merger.DiffAlgorithm;
34 import org.openoffice.xmerge.merger.Difference;
35 import org.openoffice.xmerge.merger.NodeMergeAlgorithm;
36 import org.openoffice.xmerge.merger.Iterator;
37 import org.openoffice.xmerge.merger.DiffAlgorithm;
38 import org.openoffice.xmerge.merger.diff.ParaNodeIterator;
39 import org.openoffice.xmerge.merger.diff.IteratorLCSAlgorithm;
40 import org.openoffice.xmerge.merger.merge.DocumentMerge;
41 import org.openoffice.xmerge.merger.merge.CharacterBaseParagraphMerge;
42 import org.openoffice.xmerge.util.Debug;
43 
44 
45 /**
46  *  Wordsmith implementation of <code>DocumentMerger</code>
47  *  for the {@link
48  *  org.openoffice.xmerge.converter.xml.sxw.wordsmith.PluginFactoryImpl
49  *  PluginFactoryImpl}.</p>
50  */
51 public class DocumentMergerImpl implements DocumentMerger {
52 
53     private ConverterCapabilities cc_;
54     private org.openoffice.xmerge.Document orig = null;
55 
DocumentMergerImpl(org.openoffice.xmerge.Document doc, ConverterCapabilities cc)56     public DocumentMergerImpl(org.openoffice.xmerge.Document doc, ConverterCapabilities cc) {
57         cc_ = cc;
58         this.orig = doc;
59     }
60 
merge(org.openoffice.xmerge.Document modifiedDoc)61     public void merge(org.openoffice.xmerge.Document modifiedDoc) throws MergeException {
62 
63         SxwDocument wdoc1 = (SxwDocument) orig;
64         SxwDocument wdoc2 = (SxwDocument) modifiedDoc;
65 
66         Document doc1 = wdoc1.getContentDOM();
67         Document doc2 = wdoc2.getContentDOM();
68 
69         Iterator i1 = new ParaNodeIterator(cc_, doc1.getDocumentElement());
70         Iterator i2 = new ParaNodeIterator(cc_, doc2.getDocumentElement());
71 
72         DiffAlgorithm diffAlgo = new IteratorLCSAlgorithm();
73 
74         // find out the paragrah level diffs
75         Difference[] diffTable = diffAlgo.computeDiffs(i1, i2);
76 
77         if (Debug.isFlagSet(Debug.INFO)) {
78             Debug.log(Debug.INFO, "Diff Result: ");
79 
80             for (int i = 0; i < diffTable.length; i++) {
81                 Debug.log(Debug.INFO, diffTable[i].debug());
82             }
83         }
84 
85         // merge the paragraphs
86         NodeMergeAlgorithm charMerge = new CharacterBaseParagraphMerge();
87         DocumentMerge docMerge = new DocumentMerge(cc_, charMerge);
88 
89         Iterator result = null;
90 
91         docMerge.applyDifference(i1, i2, diffTable);
92     }
93 }
94 
95 
96