LibreOffice Module xmerge (master) 1
sxc/DocumentMergerImpl.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.converter.xml.sxc;
20
21import org.w3c.dom.Node;
22import org.w3c.dom.Element;
23import org.w3c.dom.NodeList;
24
41
49public class DocumentMergerImpl implements DocumentMerger {
50
53
62 cc_ = cc;
63 this.orig = doc;
64 }
65
66 public void merge(Document modifiedDoc) throws MergeException {
67
69 SxcDocument sdoc2 = (SxcDocument)modifiedDoc;
70
71 org.w3c.dom.Document doc1 = sdoc1.getContentDOM();
72 org.w3c.dom.Document doc2 = sdoc2.getContentDOM();
73
74 Element elem1 = doc1.getDocumentElement();
75 Element elem2 = doc2.getDocumentElement();
76
77 // get table name
78 NodeList workSheetList1 =
79 elem1.getElementsByTagName(OfficeConstants.TAG_TABLE);
80 NodeList workSheetList2 =
81 elem2.getElementsByTagName(OfficeConstants.TAG_TABLE);
82
83 int numOfWorkSheet = workSheetList1.getLength();
84
85 for (int i=0; i < numOfWorkSheet; i++) {
86 Node workSheet = workSheetList1.item(i);
87
88 // try to match the workSheet
89 Node matchingWorkSheet = matchWorkSheet(workSheet, workSheetList2);
90
91 if (matchingWorkSheet != null) {
92
93 // need to put it into a row Iterator
94 // use a straight comparison algorithm then do a merge on it
95 Iterator i1 = new RowIterator(cc_, workSheet);
96 Iterator i2 = new RowIterator(cc_, matchingWorkSheet);
97
98 // find out the diff
99 DiffAlgorithm diffAlgo = new IteratorRowCompare();
100
101 // find out the paragraph level diffs
102 Difference[] diffResult = diffAlgo.computeDiffs(i1, i2);
103
104 if (Debug.isFlagSet(Debug.INFO)) {
105 Debug.log(Debug.INFO, "Diff Result: ");
106
107 for (int j = 0; j < diffResult.length; j++) {
108 Debug.log(Debug.INFO, diffResult[j].debug());
109 }
110 }
111
112 // merge back the result
114 MergeAlgorithm merger = new SheetMerge(cc_, rowMerger);
115
116 merger.applyDifference(i1, i2, diffResult);
117 }
118 }
119
120 numOfWorkSheet = workSheetList2.getLength();
121
122 // for those workSheet from target don't have a matching one in the
123 // original workSheet list, we add it
124
125 // find out the office body node first
126 NodeList officeBodyList =
127 elem1.getElementsByTagName(OfficeConstants.TAG_OFFICE_BODY);
128
129 Node officeBody = officeBodyList.item(0);
130
131 // for each WorkSheets, try to see whether we have it or not
132 for (int j=0; j < numOfWorkSheet; j++) {
133 Node workSheet= workSheetList2.item(j);
134
135 // try to match the workSheet
136 Node matchingWorkSheet = matchWorkSheet(workSheet, workSheetList1);
137
138 // add the new WorkSheet to the original document iff match not found
139 if (matchingWorkSheet == null) {
140 Node cloneNode = XmlUtil.deepClone(officeBody, workSheet);
141 officeBody.appendChild(cloneNode);
142 }
143 }
144 }
145
155 private Node matchWorkSheet(Node orgSheet, NodeList modSheetList) {
156
157 Node matchSheet = null;
158
159 String orgTableName = ((Element)orgSheet).getAttribute(
161
162 if (orgTableName == null)
163 return null;
164
165 int numOfWorkSheet = modSheetList.getLength();
166
167 String modTableName;
168
169 for (int i=0; i < numOfWorkSheet; i++) {
170 modTableName = ((Element)modSheetList.item(i)).getAttribute(
172 if (modTableName == null)
173 continue;
174
175 if (orgTableName.equals(modTableName)) {
176 matchSheet = modSheetList.item(i);
177 break;
178 }
179 }
180
181 return matchSheet;
182 }
183}
This Exception is thrown by merge algorithms.
Generic small device implementation of DocumentMerger for the SxcPluginFactory.
DocumentMergerImpl(org.openoffice.xmerge.Document doc, ConverterCapabilities cc)
Constructor.
Node matchWorkSheet(Node orgSheet, NodeList modSheetList)
Try to find a WorkSheet from the modified WorkSheetList that has a matching table name from the origi...
void merge(Document modifiedDoc)
This method will find the changes that had happened in the modifiedDoc Document object given the desi...
This class is an implementation of OfficeDocument for the SXC format.
This is the Difference basic unit.
Definition: Difference.java:27
String debug()
Display debug information.
A very simple and direct difference algorithm for row Node objects in a spreadsheet.
This is an implementation of the Iterator interface and extends NodeIterator.
This is an implementation of the NodeMergeAlgorithm interface.
This class extends the DocumentMerge class.
Definition: SheetMerge.java:37
This class is used for logging debug messages.
Definition: Debug.java:39
static final int INFO
Informational messages.
Definition: Debug.java:42
static boolean isFlagSet(int f)
Checks if flag is set.
Definition: Debug.java:176
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
Class containing static utility methods for handling XML trees.
Definition: XmlUtil.java:30
static Node deepClone(Node oldNode, Node newNode)
Perform a deep clone of certain Node which will base on the document Node of the old Node.
Definition: XmlUtil.java:42
A ConverterCapabilities object is used by DocumentMerger implementations.
A DocumentMerger can merge changes from a modified "Device" Document to the assigned original "Office...
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
This interface contains constants for StarOffice XML tags, attributes (StarCalc cell types,...
String TAG_OFFICE_BODY
Element tag for office:body.
String ATTRIBUTE_TABLE_NAME
Attribute tag for table:name of element table:table.
This is the difference algorithm interface.
Difference[] computeDiffs(Iterator orgSeq, Iterator modSeq)
Returns a Difference array.
This is an interface used by the DiffAlgorithm and MergeAlgorithm to access a Document.
Definition: Iterator.java:27
This is the MergeAlgorithm interface.
void applyDifference(Iterator orgSeq, Iterator modSeq, Difference[] differences)
This method is to merge the difference to an Iterator.
This is an interface for a MergeAlgorithm to merge two Node objects.
int i
Document and PluginFactory implementations for XML based formats.
Provides implementations for the Iterator interface and related support classes.
Provides implementations for the MergeAlgorithm interface, the NodeMergeAlgorithm interface,...
The DiffAlgorithm and MergeAlgorithm are used to provide the merge capabilities of this project.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19