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.sxc;
25 
26 import java.util.Vector;
27 import java.util.Enumeration;
28 import org.w3c.dom.NamedNodeMap;
29 import org.w3c.dom.NodeList;
30 import org.w3c.dom.Node;
31 import org.w3c.dom.Element;
32 
33 import org.openoffice.xmerge.converter.xml.OfficeConstants;
34 import org.openoffice.xmerge.util.Debug;
35 import org.openoffice.xmerge.util.XmlUtil;
36 
37 /**
38  * This is a class representing the different attributes for a worksheet
39  * contained in settings.xml.
40  *
41  * @author Martin Maher
42  */
43 public class BookSettings implements OfficeConstants {
44 
45     /**  A w3c <code>Document</code>. */
46     private org.w3c.dom.Document settings = null;
47 
48 	private boolean hasColumnRowHeaders = true;
49 	private String 	activeSheet			= new String();
50 	private Vector	worksheetSettings	= new Vector();
51 
52 	/**
53 	 * Default Constructor for a <code>BookSettings</code>
54 	 *
55 	 * @param root
56 	 */
BookSettings(Node root)57 	public BookSettings(Node root) {
58 		readNode(root);
59 	}
60 
61 	/**
62 	 * Default Constructor for a <code>BookSettings</code>
63 	 *
64 	 * @param worksheetSettings if it's a row the height, a column the width
65 	 */
BookSettings(Vector worksheetSettings)66 	public BookSettings(Vector worksheetSettings) {
67 		this.worksheetSettings = worksheetSettings;
68 	}
69 
70 	/**
71 	 *
72 	 */
setColumnRowHeaders(boolean hasColumnRowHeaders)73 	public void setColumnRowHeaders(boolean hasColumnRowHeaders) {
74 		this.hasColumnRowHeaders = hasColumnRowHeaders;
75 	}
76 
77 	/**
78 	 *
79 	 */
hasColumnRowHeaders()80 	public boolean hasColumnRowHeaders() {
81 		return hasColumnRowHeaders;
82 	}
83 
84 	/**
85 	 * Gets the <code>Vector</code> of <code>SheetSettings</code>
86 	 *
87 	 * @return <code>Vector</code> of <code>SheetSettings</code>
88 	 */
getSheetSettings()89 	public Vector getSheetSettings() {
90 		return worksheetSettings;
91 	}
92 
93 	/**
94 	 * Gets the active sheet name
95 	 *
96 	 * @return the active sheet name
97 	 */
getActiveSheet()98 	public String getActiveSheet() {
99 
100 		return activeSheet;
101 	}
102 
103 	/**
104 	 * Sets the active sheet name
105 	 *
106 	 * @param activeSheet the active sheet name
107 	 */
setActiveSheet(String activeSheet)108 	public void setActiveSheet(String activeSheet) {
109 
110 		this.activeSheet = activeSheet;
111 	}
112 
113 
114 	/**
115 	 * Adds an XML entry for a particular setting
116 	 *
117 	 * @param root the root node at which to add the xml entry
118 	 * @param attribute the name of the attribute to add
119 	 * @param type the attribute type (int, short etc)
120 	 * @param value the value of the attribute
121 	 */
addConfigItem(Node root, String attribute, String type, String value)122 	private void addConfigItem(Node root, String attribute, String type, String value) {
123 
124 		Element configItem = settings.createElement(TAG_CONFIG_ITEM);
125 		configItem.setAttribute(ATTRIBUTE_CONFIG_NAME, attribute);
126 		configItem.setAttribute(ATTRIBUTE_CONFIG_TYPE, type);
127 
128 		configItem.appendChild(settings.createTextNode(value));
129 
130 		root.appendChild(configItem);
131 	}
132 
133 	/**
134 	 * Writes out a settings.xml entry for this BookSettings object
135 	 *
136 	 * @param settings a <code>Document</code> object representing the settings.xml
137 	 * @param root the root xml node to add to
138 	 */
writeNode(org.w3c.dom.Document settings, Node root)139 	public void writeNode(org.w3c.dom.Document settings, Node root) {
140 
141 		this.settings = settings;
142 		Element configItemMapNamed		= (Element) settings.createElement(TAG_CONFIG_ITEM_MAP_NAMED);
143 		configItemMapNamed.setAttribute(ATTRIBUTE_CONFIG_NAME, "Tables");
144 		for(Enumeration e = worksheetSettings.elements();e.hasMoreElements();) {
145 			SheetSettings s = (SheetSettings) e.nextElement();
146 			s.writeNode(settings, configItemMapNamed);
147 		}
148 		addConfigItem(root, "ActiveTable", "string", activeSheet);
149 		String booleanValue = Boolean.toString(hasColumnRowHeaders);
150 		addConfigItem(root, "HasColumnRowHeaders", "boolean", booleanValue);
151 		root.appendChild(configItemMapNamed);
152 	}
153 
154 	/**
155 	 * Sets a variable based on a String value read from XML
156 	 *
157 	 * @param name xml name of the attribute to set
158 	 * @param value String value fo the attribute
159 	 */
addAttribute(String name, String value)160 	public void addAttribute(String name, String value) {
161 
162 		if(name.equals("ActiveTable")) {
163 			activeSheet = value;
164 		} else if(name.equals("HasColumnRowHeaders")) {
165 			Boolean b = Boolean.valueOf(value);
166 			hasColumnRowHeaders = b.booleanValue();
167 		}
168 	}
169 
170 	/**
171 	 * Reads document settings from xml and inits SheetSettings variables
172 	 *
173 	 * @param root XML Node to read from
174 	 */
readNode(Node root)175 	public void readNode(Node root) {
176 
177 		if (root.hasChildNodes()) {
178 
179 			NodeList nodeList = root.getChildNodes();
180             int len = nodeList.getLength();
181             for (int i = 0; i < len; i++) {
182 				Node child = nodeList.item(i);
183 
184                 if (child.getNodeType() == Node.ELEMENT_NODE) {
185                     String nodeName = child.getNodeName();
186 
187                     if (nodeName.equals(TAG_CONFIG_ITEM)) {
188 
189 						NamedNodeMap cellAtt = child.getAttributes();
190 
191             			Node configNameNode =
192 			            	cellAtt.getNamedItem(ATTRIBUTE_CONFIG_NAME);
193 
194 						String name = configNameNode.getNodeValue();
195 						NodeList nodeList2 = child.getChildNodes();
196 						int len2 = nodeList2.getLength();
197 						String s = "";
198 						for (int j = 0; j < len2; j++) {
199 							Node child2 = nodeList2.item(j);
200 							if (child2.getNodeType() == Node.TEXT_NODE) {
201 								s = child2.getNodeValue();
202 							}
203 						}
204 						addAttribute(name, s);
205 
206  					} else if (nodeName.equals(TAG_CONFIG_ITEM_MAP_NAMED)) {
207 
208 						readNode(child);
209 
210  					} else if (nodeName.equals(TAG_CONFIG_ITEM_MAP_ENTRY)) {
211 
212 						SheetSettings s = new SheetSettings(child);
213 						worksheetSettings.add(s);
214 
215                     } else {
216 
217                         Debug.log(Debug.TRACE, "<OTHERS " + XmlUtil.getNodeInfo(child) + " />");
218                     }
219                 }
220             }
221 		}
222 	}
223 }
224