forked from google/gdata-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.java
More file actions
288 lines (246 loc) · 7.93 KB
/
Copy pathCell.java
File metadata and controls
288 lines (246 loc) · 7.93 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright (c) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gdata.data.spreadsheet;
import com.google.gdata.util.common.xml.XmlWriter;
import com.google.gdata.data.Extension;
import com.google.gdata.data.ExtensionDescription;
import com.google.gdata.data.ExtensionProfile;
import com.google.gdata.util.ParseException;
import com.google.gdata.util.XmlParser;
import org.xml.sax.Attributes;
import java.io.IOException;
import java.util.ArrayList;
/**
* GData schema extension describing a spreadsheet formula.
*
*
*
*/
public class Cell implements Extension {
/** The positional row number starting with 1 (-1 for unspecified) */
private int row = -1;
/** The positional column number starting with 1 (-1 for unspecified). */
private int col = -1;
/** The formula of the cell (null if the formula is omitted). */
private String inputValue = null;
/** The calculated numeric value of this cell. */
private Number numericValue = null;
/**
* The evaluated value of the cell in String from (null if unspecified).
*/
private String value = null;
/**
* Initializes to blank for XML parsing.
*/
public Cell() {
}
/**
* Constructs this Cell with all fields, but since some of these fields
* cannot be written to the server, this constructor is private.
*
* @param inRow row number
* @param inCol column number
* @param inNumericValue the input of the cell
* @param inValue the result of its calculation
*/
private Cell(int inRow, int inCol,
String inInputValue, Number inNumericValue, String inValue) {
row = inRow;
col = inCol;
inputValue = inInputValue;
value = inValue;
numericValue = inNumericValue;
}
/**
* Initializes a cell where the column is known.
*
* @param inRow the row number starting with 1 (-1 for unspecified)
* @param inCol the column number starting with 1 (-1 for unspecified)
* @param inInputValue the formula (null for unspecified)
*/
public Cell(int inRow, int inCol, String inInputValue) {
this(inRow, inCol, inInputValue, null, null);
}
/**
* Creates a cell for the server library; it is not appropriate for
* client side use (the server may reject these cells).
*/
public static Cell createFullCell(int inRow, int inCol,
String inInputValue, Number inCalculatedValue, String inValue) {
return new Cell(inRow, inCol, inInputValue,
inCalculatedValue, inValue);
}
/**
* Yields the positional row number starting with 1.
*
* @return the row number, or -1 if the row is not specified
*/
public int getRow() {
return row;
}
/**
* Yields the column number starting with 1.
*
* @return the positional column number, or -1 if the column is not specified
*/
public int getCol() {
return col;
}
/**
* Yields the formula reference of the cell.
*
*
* An "=" sign signifies that there is a formula computed on the fly.
* Otherwise it is simply data that is entered into the sheet.
*/
public String getInputValue() {
return inputValue;
}
/**
* Gets the calculated numeric value.
*
* @return the raw numeric value, or null if it is non-numeric
*/
public Number getNumericValue() {
return numericValue;
}
/**
* Gets the double-precision value.
*
* @return the double value, or Double.NaN if no number specified
*/
public double getDoubleValue() {
if (numericValue == null) {
return Double.NaN;
} else {
return numericValue.doubleValue();
}
}
/**
* Yields the evaluated, formatted value of this cell.
*
*
* @return the evaluated and formatted value (null if not specified)
*/
public String getValue() {
return value;
}
/**
* Creates a new cell with a new input value, for the purpose of updating.
*
* The new cell cannot contain a calculation result value, because values
* cannot be updated.
*
* @param newInputValue the new input value, starting with '=' for a formula,
* otherwise just a plain string
* @return a newly created "cell" object
*/
public Cell withNewInputValue(String newInputValue) {
return new Cell(row, col, newInputValue, null, null);
}
/**
* Returns the suggested extension description.
* @param repeats whether this cell might be repeated in parent context
*/
public static ExtensionDescription getDefaultDescription(boolean repeats) {
ExtensionDescription desc = new ExtensionDescription();
desc.setExtensionClass(Cell.class);
desc.setNamespace(Namespaces.gSpreadNs);
desc.setLocalName("cell");
desc.setRepeatable(repeats);
return desc;
}
/**
* Writes this cell as XML, omitting any unspecified fields.
*/
public void generate(XmlWriter w, ExtensionProfile extProfile)
throws IOException {
ArrayList<XmlWriter.Attribute> attrs =
new ArrayList<XmlWriter.Attribute>();
if (row > 0) {
attrs.add(new XmlWriter.Attribute("row", String.valueOf(row)));
}
if (col > 0) {
attrs.add(new XmlWriter.Attribute("col", String.valueOf(col)));
}
if (inputValue != null) {
attrs.add(new XmlWriter.Attribute("inputValue", inputValue));
}
if (numericValue != null) {
attrs.add(new XmlWriter.Attribute("numericValue",
numericValue.toString()));
}
w.simpleElement(Namespaces.gSpreadNs, "cell", attrs, value);
}
/**
* Yields an XML handler for parsing a Cell element.
*/
public XmlParser.ElementHandler getHandler(ExtensionProfile extProfile,
String namespace,
String localName,
Attributes attrs)
throws ParseException, IOException {
return new Handler();
}
/**
* Parser for a <gd:cell>.
*/
private class Handler extends XmlParser.ElementHandler {
/**
* Initializes this instance given the extension profile.
*/
public Handler() {
}
/**
* Handles an attribute (such as row, col, inputValue).
*/
public void processAttribute(String namespace,
String localName,
String attributeData)
throws ParseException {
if (namespace.equals("")) {
if (localName.equals("row")) {
Cell.this.row = Integer.parseInt(attributeData);
} else if (localName.equals("col")) {
Cell.this.col = Integer.parseInt(attributeData);
} else if (localName.equals("inputValue")) {
Cell.this.inputValue = attributeData;
} else if (localName.equals("numericValue")) {
try {
// (right now their backend is entirely double anyway)
Cell.this.numericValue = Double.valueOf(attributeData);
} catch (NumberFormatException nfe) {
throw new ParseException("Invalid numericValue.");
}
}
}
}
/**
* Processes the end of the tag.
*/
public void processEndElement() throws ParseException {
/*
* This insidious-looking line of code copies
* this handler's value (<gd:cell>..stuff..</gd:cell>)
* into the cell value.
*/
Cell.this.value = this.value;
// Take empty fields and make them null.
if (Cell.this.value != null && Cell.this.value.equals("")) {
Cell.this.value = null;
}
}
}
}