20 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
|
|
4 |
* Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
|
|
|
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.openoffice.style.data;
|
|
|
15 |
|
25 |
ilm |
16 |
import org.openconcerto.openoffice.ODEpoch;
|
20 |
ilm |
17 |
import org.openconcerto.openoffice.ODPackage;
|
25 |
ilm |
18 |
import org.openconcerto.openoffice.ODValueType;
|
20 |
ilm |
19 |
import org.openconcerto.openoffice.XMLVersion;
|
|
|
20 |
import org.openconcerto.openoffice.spreadsheet.CellStyle;
|
|
|
21 |
import org.openconcerto.openoffice.spreadsheet.MutableCell;
|
|
|
22 |
|
25 |
ilm |
23 |
import java.util.Calendar;
|
|
|
24 |
import java.util.Date;
|
20 |
ilm |
25 |
import java.util.List;
|
|
|
26 |
|
25 |
ilm |
27 |
import javax.xml.datatype.Duration;
|
|
|
28 |
|
20 |
ilm |
29 |
import org.jdom.Element;
|
|
|
30 |
import org.jdom.Namespace;
|
|
|
31 |
|
|
|
32 |
// from section 16.27.2 in v1.2-cs01-part1
|
|
|
33 |
public class NumberStyle extends DataStyle {
|
|
|
34 |
|
|
|
35 |
public static final DataStyleDesc<NumberStyle> DESC = new DataStyleDesc<NumberStyle>(NumberStyle.class, XMLVersion.OD, "number-style", "N") {
|
|
|
36 |
@Override
|
|
|
37 |
public NumberStyle create(ODPackage pkg, Element e) {
|
|
|
38 |
return new NumberStyle(pkg, e);
|
|
|
39 |
}
|
|
|
40 |
};
|
|
|
41 |
|
25 |
ilm |
42 |
public static final Number toNumber(Object value, ODEpoch epoch) {
|
|
|
43 |
final Number res;
|
|
|
44 |
if (value instanceof Number) {
|
|
|
45 |
res = (Number) value;
|
|
|
46 |
} else if (value instanceof Boolean) {
|
|
|
47 |
res = ((Boolean) value).booleanValue() ? 1 : 0;
|
|
|
48 |
} else if ((value instanceof Duration || value instanceof Date || value instanceof Calendar)) {
|
|
|
49 |
if (value instanceof Duration) {
|
|
|
50 |
res = epoch.getDays((Duration) value);
|
|
|
51 |
} else {
|
|
|
52 |
final Calendar cal;
|
|
|
53 |
if (value instanceof Calendar) {
|
|
|
54 |
cal = (Calendar) value;
|
|
|
55 |
} else {
|
|
|
56 |
cal = Calendar.getInstance();
|
|
|
57 |
cal.setTime((Date) value);
|
|
|
58 |
}
|
|
|
59 |
res = epoch.getDays(cal);
|
|
|
60 |
}
|
|
|
61 |
} else {
|
|
|
62 |
res = null;
|
|
|
63 |
}
|
|
|
64 |
return res;
|
|
|
65 |
}
|
|
|
66 |
|
20 |
ilm |
67 |
public NumberStyle(final ODPackage pkg, Element elem) {
|
25 |
ilm |
68 |
super(pkg, elem, ODValueType.FLOAT);
|
20 |
ilm |
69 |
}
|
|
|
70 |
|
|
|
71 |
@Override
|
25 |
ilm |
72 |
protected Number convertNonNull(Object value) {
|
|
|
73 |
return toNumber(value, getEpoch());
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
@Override
|
20 |
ilm |
77 |
public String format(Object o, CellStyle defaultStyle, boolean lenient) {
|
|
|
78 |
final Number n = (Number) o;
|
|
|
79 |
final Namespace numberNS = this.getElement().getNamespace();
|
|
|
80 |
final StringBuilder sb = new StringBuilder();
|
|
|
81 |
@SuppressWarnings("unchecked")
|
|
|
82 |
final List<Element> children = this.getElement().getChildren();
|
|
|
83 |
for (final Element elem : children) {
|
|
|
84 |
if (elem.getNamespace().equals(numberNS)) {
|
|
|
85 |
if (elem.getName().equals("text")) {
|
|
|
86 |
sb.append(elem.getText());
|
|
|
87 |
} else if (elem.getName().equals("number") || elem.getName().equals("scientific-number")) {
|
|
|
88 |
sb.append(formatNumberOrScientificNumber(elem, n, defaultStyle));
|
|
|
89 |
} else if (elem.getName().equals("fraction")) {
|
|
|
90 |
// TODO fractions
|
|
|
91 |
reportError("Fractions not supported", lenient);
|
|
|
92 |
sb.append(MutableCell.formatNumber(n, defaultStyle));
|
|
|
93 |
}
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
return sb.toString();
|
|
|
97 |
}
|
|
|
98 |
}
|