summaryrefslogtreecommitdiff
path: root/xmerge/java/org/openoffice/xmerge/merger/merge/PositionBaseRowMerge.java
blob: 0683733eb00de9db58a4351260ac6f18ca2ce733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2008 by Sun Microsystems, Inc.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * $RCSfile: PositionBaseRowMerge.java,v $
 * $Revision: 1.3 $
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

package org.openoffice.xmerge.merger.merge;

import java.util.List;

import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;

import org.openoffice.xmerge.ConverterCapabilities;
import org.openoffice.xmerge.merger.Difference;
import org.openoffice.xmerge.merger.Iterator;
import org.openoffice.xmerge.merger.DiffAlgorithm;
import org.openoffice.xmerge.merger.NodeMergeAlgorithm;
import org.openoffice.xmerge.merger.diff.CellNodeIterator;
import org.openoffice.xmerge.converter.xml.OfficeConstants;
import org.openoffice.xmerge.util.XmlUtil;
import org.openoffice.xmerge.util.Debug;


/**
 *  This is an implementation of the <code>NodeMergeAlgorithm</code>
 *  interface.  It is used to merge two rows using a positional
 *  comparison base method.
 */
public final class PositionBaseRowMerge implements NodeMergeAlgorithm {

    /**  The capabilities of this converter. */
    private ConverterCapabilities cc_;


    /**
     *  Constructor.
     *
     *  @param  cc  The <code>ConverterCapabilities</code>.
     */
    public PositionBaseRowMerge(ConverterCapabilities cc) {
        cc_ = cc;
    }


    public void merge(Node orgRow, Node modRow) {

        Iterator orgCells = new CellNodeIterator(cc_, orgRow);
        Iterator modCells = new CellNodeIterator(cc_, modRow);

        mergeCellSequences(orgCells, modCells);
    }


    // used to compare the cell 1 by 1
    private void mergeCellSequences(Iterator orgSeq, Iterator modSeq) {

        int i, j;
        boolean needMerge = true;
        Element orgCell, modCell;

        Object orgSeqObject = orgSeq.start();
        Object modSeqObject = modSeq.start();

        while (orgSeqObject != null) {


            needMerge = true;

            if (modSeqObject ==  null)  {
                // no corresponding cell in the target, empty out the cell
                SheetUtil.emptyCell(cc_, (Node)orgSeqObject);
                orgSeqObject = orgSeq.next();

            } else {

                // compare the cell directly
                if (!orgSeq.equivalent(orgSeqObject, modSeqObject)) {

                    orgCell = (Element)orgSeqObject;
                    modCell = (Element)modSeqObject;

                    // check whether the original cell with multiple column
                    // if so, need to split one out for merge
                    String orgColRepeated = orgCell.getAttribute(
                        OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED);
                    String modColRepeated = modCell.getAttribute(
                        OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED);

                    int orgColNum = 1;
                    int modColNum = 1;

                    if (orgColRepeated.length() > 0) {
                        orgColNum =
                            Integer.valueOf(orgColRepeated).intValue();
                    }
                    if (modColRepeated.length() > 0) {
                        modColNum =
                            Integer.valueOf(modColRepeated).intValue();
                    }

                    // try to find out the common number of repeated cols
                    if (orgColNum == modColNum) {
                        orgSeqObject = orgSeq.next();
                        modSeqObject = modSeq.next();

                    // cut the original cell into 2 half, first half
                    // have the repeated attribute = modify cell attr
                    } else if (orgColNum > modColNum) {
                        Element orgSplitCell = splitColRepeatedCell(
                                    orgCell, modColNum,
                                    orgColNum - modColNum);
                        // it may equal after the split!
                        if (orgSeq.equivalent(orgSplitCell, modCell)) {
                            needMerge = false;
                        }
                        orgCell = orgSplitCell;
                        modSeqObject = modSeq.next();

                    // cut the modified cell into 2 half, first half
                    // have the repeated attribute = original cell attr
                    } else {
                        Element modSplitCell = splitColRepeatedCell(
                                    modCell, orgColNum,
                                    modColNum - orgColNum);
                        // it may equal after the split!
                        if (modSeq.equivalent(orgCell, modSplitCell)) {
                            needMerge = false;
                        }
                        modCell = modSplitCell;
                        orgSeqObject = orgSeq.next();
                    }

                    if (needMerge) {
                        mergeCells(orgCell, modCell);
                    }

                } else {
                    // cells are equivalent, move on to next one.
                    orgSeqObject = orgSeq.next();
                    modSeqObject = modSeq.next();
                } // end if-else
            } // end if-else
        } // end while loop

        // get the one of the original cell, so that the cloned node
        // can base it to find the document node
        orgCell = (Element)orgSeq.start();

        // add any extra cells to the original cell sequence.
        for (; modSeqObject != null; modSeqObject = modSeq.next()) {
            Node clonedNode = XmlUtil.deepClone(orgCell, (Node)modSeqObject);
            Node parent = orgCell.getParentNode();
            parent.appendChild(clonedNode);
        }
    }


    private Element splitColRepeatedCell(Element orgCell,
                                         int splitNum, int orgNum) {
        // NOTE: should we really want to do deep clone?
        // in most the case, it is an empty cell, but the
        // specification didn't forbid any node to use multiple
        // column attributes. i.e. the node can contain text
        // nodes or other things under it.
        Element splitCell = (Element)(orgCell.cloneNode(true));

        if (splitNum > 1) {
            splitCell.setAttribute(
              OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED,
              String.valueOf(splitNum));
        } else if (splitNum == 1) {
            splitCell.removeAttribute(
              OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED);
        }
        if (orgNum > 1) {
            orgCell.setAttribute(
              OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED,
              String.valueOf(orgNum));
        } else if (orgNum == 1) {
            orgCell.removeAttribute(
              OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED);
        }

        Node parentNode = orgCell.getParentNode();
        parentNode.insertBefore(splitCell, orgCell);

        return splitCell;
    }


    private void mergeCells(Element orgCell, Element modCell) {

        // remove all the supported attributes and possible text child for
        // string cells
        SheetUtil.emptyCell(cc_, orgCell);

        // copy all the supported attributes and possible text child from
        // the modified cell
        NamedNodeMap attrNodes = modCell.getAttributes();

        if (attrNodes != null) {

            // copy the first text:p node. As it's not necessary only string
            // type cell can have a text:p section.
            NodeList paraNodes =
                modCell.getElementsByTagName(OfficeConstants.TAG_PARAGRAPH);

            Node firstParaNode = paraNodes.item(0);

            // try to clone the node
            if (firstParaNode != null) {

                Node clonedNode = XmlUtil.deepClone(orgCell, firstParaNode);

                // insert as the first child of the original cell
                Node firstChild = orgCell.getFirstChild();
                if (firstChild != null) {
                    orgCell.insertBefore(clonedNode, firstChild);
                } else {
                    orgCell.appendChild(clonedNode);
                }
            }

            // check all the attributes and copy those we supported in
            // converter
            // NOTE: for attribute list, refer to section 4.7.2 in specification
            int len = attrNodes.getLength();

            for (int i = 0; i < len; i++) {
                Node attr = attrNodes.item(i);

                // copy the supported attrs
                if (cc_.canConvertAttribute(OfficeConstants.TAG_TABLE_CELL,
                                           attr.getNodeName())) {
                    orgCell.setAttribute(attr.getNodeName(),
                                         attr.getNodeValue());
                }
            }
        }
    }
}