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.merger.merge;
25 
26 import org.w3c.dom.Node;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.NodeList;
29 import org.w3c.dom.NamedNodeMap;
30 
31 import org.openoffice.xmerge.ConverterCapabilities;
32 import org.openoffice.xmerge.converter.xml.OfficeConstants;
33 
34 
35 /**
36  *  Utility methods to handle sheet XML tree.
37  */
38 public class SheetUtil {
39 
40     /**
41      *  <p>Empty the content of a cell value.   This includes the following:
42      *  </p>
43      *
44      *  <p><ul><li>
45      *    Remove all of the supported attributes.
46      *  </li><li>
47      *    Remove the first <i>text:p</i> <code>Node</code> for most of the cells.
48      *  </li></ul>
49      *
50      *  @param  cc    The <code>ConverterCapabilities</code>.
51      *  @param  node  The <code>Node</code>.
52      */
emptyCell(ConverterCapabilities cc, Node node)53     public static void emptyCell(ConverterCapabilities cc, Node node) {
54 
55         NamedNodeMap attrNodes = node.getAttributes();
56 
57         if (attrNodes != null) {
58 
59             // empty the first text:p node.
60             // Note: it's not necessary only string type cell contain text:p
61             // basically, all different type of cell will contain one
62             Element cell = (Element)node;
63 
64             // get the paragraph node list
65             NodeList paraNodes =
66                 cell.getElementsByTagName(OfficeConstants.TAG_PARAGRAPH);
67 
68             Node firstParaNode = paraNodes.item(0);
69 
70             // remove the first paragraph element node
71             if (firstParaNode != null) {
72                 Node parent = firstParaNode.getParentNode();
73                 parent.removeChild(firstParaNode);
74             }
75 
76             // check all the attributes and remove those we supported in
77             // converter
78             // NOTE: for attribute list, refer to section 4.7.2 in specification
79             int len = attrNodes.getLength();
80 
81             for (int i = 0; i < len; ) {
82                 Node attr = attrNodes.item(i);
83 
84                 // when we hit the end of the attribute nodes, return
85                 // it may happen sooner as we keep on removing nodes
86                 if (attr == null) {
87                     break;
88                 }
89                 // remove the supported attr except columns repeated attribute
90                 if (cc.canConvertAttribute(OfficeConstants.TAG_TABLE_CELL,
91                                            attr.getNodeName()) &&
92                     !attr.getNodeName().equals(
93                         OfficeConstants.ATTRIBUTE_TABLE_NUM_COLUMNS_REPEATED)) {
94 
95                     attrNodes.removeNamedItem(attr.getNodeName());
96                 } else {
97                     i++;
98                 }
99             }
100         }
101     }
102 }
103 
104