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 org.w3c.dom.Node;
27 import org.w3c.dom.Element;
28 import org.w3c.dom.NamedNodeMap;
29 
30 import org.openoffice.xmerge.converter.xml.OfficeConstants;
31 import org.openoffice.xmerge.util.Debug;
32 import org.openoffice.xmerge.util.XmlUtil;
33 
34 /**
35  * This is a class to define a Name Definition structure. This can then be
36  * used by plugins to write or read their own definition types.
37  *
38  * @author Martin Maher
39  */
40 public class NameDefinition implements OfficeConstants {
41 
42 	private String name; 						// name which identifies the definition
43 	private String definition;					// the definition itself
44 	private String baseCellAddress;				// the basecelladdress
45 	private boolean rangeType = false;			// true if definition of type range
46 	private boolean expressionType = false;		// true if definition of type expression
47 
48 	/**
49 	 * Default Constructor for a <code>NameDefinition</code>
50 	 *
51 	 */
NameDefinition()52 	public NameDefinition() {
53 
54 	}
55 
56 	/**
57 	 * Constructor that takes a <code>Node</code> to build a
58 	 * <code>NameDefinition</code>
59 	 *
60 	 * @param root XML Node to read from
61 	 */
NameDefinition(Node root)62 	public NameDefinition(Node root) {
63 		readNode(root);
64 	}
65 
66 	/**
67 	 * Default Constructor for a <code>NameDefinition</code>
68 	 *
69 	 */
NameDefinition(String name, String definition, String baseCellAddress, boolean rangeType, boolean expressionType )70 	public NameDefinition(String name, String definition, String
71 	baseCellAddress, boolean rangeType, boolean expressionType ) {
72 		this.name = name;
73 		this.definition = definition;
74 		this.baseCellAddress = baseCellAddress;
75 		this.rangeType = rangeType;
76 		this.expressionType = expressionType;
77 	}
78 
79 	/**
80 	 * returns Name of the definition
81 	 *
82 	 * @return the name which identifies the definition
83 	 */
getName()84 	public String getName() {
85 
86 		return name;
87 	}
88 	/**
89 	 * sets the definition
90 	 *
91 	 * @param newDefinition sets the definition
92 	 */
setDefinition(String newDefinition)93 	public void setDefinition(String newDefinition) {
94 
95 		definition = newDefinition;
96 	}
97 	/**
98 	 * Returns the definition itself
99 	 *
100 	 * @return the definition
101 	 */
getDefinition()102 	public String getDefinition() {
103 
104 		return definition;
105 	}
106 
107 	/**
108 	 * Returns the base Cell address
109 	 *
110 	 * @return the base cell address
111 	 */
getBaseCellAddress()112 	public String getBaseCellAddress() {
113 
114 		return baseCellAddress;
115 	}
116 
117 	/**
118 	 * Tests if definition is of type expression
119 	 *
120 	 * @return whether or not this name definition is of type expression
121 	 */
isExpressionType()122 	public boolean isExpressionType() {
123 		return expressionType;
124 	}
125 
126 	/**
127 	 * Tests if definition is of type range
128 	 *
129 	 * @return whether or not this name definition is of type range
130 	 */
isRangeType()131 	public boolean isRangeType() {
132 		return rangeType;
133 	}
134 
135 	/**
136 	 * Writes out a content.xml entry for this NameDefinition object
137 	 *
138 	 * @param doc  a <code>Document</code> object representing the settings.xml
139 	 * @param root the root xml node to add to
140 	 */
writeNode(org.w3c.dom.Document doc, Node root)141 	public void writeNode(org.w3c.dom.Document doc, Node root) {
142 
143 			if(isRangeType()) {
144 
145         		Debug.log(Debug.TRACE, "Found Range Name : " + getName());
146 				Element namedRangeElement = (Element) doc.createElement(TAG_TABLE_NAMED_RANGE);
147 				namedRangeElement.setAttribute(ATTRIBUTE_TABLE_NAME, getName());
148 				namedRangeElement.setAttribute(ATTRIBUTE_TABLE_BASE_CELL_ADDRESS, getBaseCellAddress());
149 				namedRangeElement.setAttribute(ATTRIBUTE_TABLE_CELL_RANGE_ADDRESS, getDefinition());
150 				root.appendChild(namedRangeElement);
151 			} else if (isExpressionType()) {
152 
153         		Debug.log(Debug.TRACE, "Found Expression Name : " + getName());
154 				Element namedExpressionElement = (Element) doc.createElement(TAG_TABLE_NAMED_EXPRESSION);
155 				namedExpressionElement.setAttribute(ATTRIBUTE_TABLE_NAME, getName());
156 				namedExpressionElement.setAttribute(ATTRIBUTE_TABLE_BASE_CELL_ADDRESS,getBaseCellAddress());
157 				namedExpressionElement.setAttribute(ATTRIBUTE_TABLE_EXPRESSION, getDefinition());
158 				root.appendChild(namedExpressionElement);
159 			} else {
160 
161         		Debug.log(Debug.TRACE, "Unknown Name Definition : " + getName());
162 			}
163 	}
164 
165 	/**
166 	 * Reads document settings from xml and inits Settings variables
167 	 *
168 	 * @param root XML Node to read from
169 	 */
readNode(Node root)170 	public void readNode(Node root) {
171 
172 		String nodeName = root.getNodeName();
173 		NamedNodeMap cellAtt = root.getAttributes();
174 
175 		if (nodeName.equals(TAG_TABLE_NAMED_RANGE)) {
176 
177 			Node tableNameNode =
178 				cellAtt.getNamedItem(ATTRIBUTE_TABLE_NAME);
179 			Node tableBaseCellAddress =
180 				cellAtt.getNamedItem(ATTRIBUTE_TABLE_BASE_CELL_ADDRESS);
181 			Node tableCellRangeAddress =
182 				cellAtt.getNamedItem(ATTRIBUTE_TABLE_CELL_RANGE_ADDRESS);
183 			Debug.log(Debug.TRACE,"Named-range : " + tableNameNode.getNodeValue());
184                     	// Create a named-range name definition
185 			name = tableNameNode.getNodeValue();
186 			definition = tableCellRangeAddress.getNodeValue();
187 			baseCellAddress = tableBaseCellAddress.getNodeValue();
188 			expressionType = true;
189 			rangeType = false;
190 
191 		} else if (nodeName.equals(TAG_TABLE_NAMED_EXPRESSION)) {
192 
193             Node tableNameNode =
194 								cellAtt.getNamedItem(ATTRIBUTE_TABLE_NAME);
195 			Node tableBaseCellAddress =
196 								cellAtt.getNamedItem(ATTRIBUTE_TABLE_BASE_CELL_ADDRESS);
197 			Node tableExpression=
198 								cellAtt.getNamedItem(ATTRIBUTE_TABLE_EXPRESSION);
199 			Debug.log(Debug.TRACE,"Named-expression: " + tableNameNode.getNodeValue());
200 			// Create a named-range name definition
201 			name = tableNameNode.getNodeValue();
202 			definition = tableExpression.getNodeValue();
203 			baseCellAddress = tableBaseCellAddress.getNodeValue();
204 			expressionType = false;
205 			rangeType = true;
206 		} else {
207 			Debug.log(Debug.TRACE, "<OTHERS " + XmlUtil.getNodeInfo(root) + " />");
208 		}
209 	}
210 
211 }
212