OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 80 | Rev 180 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
 
25 ilm 22
import java.util.Calendar;
23
import java.util.Date;
20 ilm 24
import java.util.List;
25
 
25 ilm 26
import javax.xml.datatype.Duration;
27
 
20 ilm 28
import org.jdom.Element;
29
import org.jdom.Namespace;
30
 
31
// from section 16.27.2 in v1.2-cs01-part1
32
public class NumberStyle extends DataStyle {
33
 
65 ilm 34
    static final DataStyleDesc<NumberStyle> DESC = new DataStyleDesc<NumberStyle>(NumberStyle.class, XMLVersion.OD, "number-style", "N") {
20 ilm 35
        @Override
36
        public NumberStyle create(ODPackage pkg, Element e) {
37
            return new NumberStyle(pkg, e);
38
        }
39
    };
40
 
25 ilm 41
    public static final Number toNumber(Object value, ODEpoch epoch) {
42
        final Number res;
43
        if (value instanceof Number) {
44
            res = (Number) value;
45
        } else if (value instanceof Boolean) {
46
            res = ((Boolean) value).booleanValue() ? 1 : 0;
47
        } else if ((value instanceof Duration || value instanceof Date || value instanceof Calendar)) {
48
            if (value instanceof Duration) {
49
                res = epoch.getDays((Duration) value);
50
            } else {
51
                final Calendar cal;
52
                if (value instanceof Calendar) {
53
                    cal = (Calendar) value;
54
                } else {
80 ilm 55
                    cal = ODValueType.getCalendar();
25 ilm 56
                    cal.setTime((Date) value);
57
                }
58
                res = epoch.getDays(cal);
59
            }
60
        } else {
61
            res = null;
62
        }
63
        return res;
64
    }
65
 
20 ilm 66
    public NumberStyle(final ODPackage pkg, Element elem) {
25 ilm 67
        super(pkg, elem, ODValueType.FLOAT);
20 ilm 68
    }
69
 
70
    @Override
25 ilm 71
    protected Number convertNonNull(Object value) {
72
        return toNumber(value, getEpoch());
73
    }
74
 
75
    @Override
20 ilm 76
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
77
        final Number n = (Number) o;
78
        final Namespace numberNS = this.getElement().getNamespace();
79
        final StringBuilder sb = new StringBuilder();
80
        @SuppressWarnings("unchecked")
81
        final List<Element> children = this.getElement().getChildren();
82
        for (final Element elem : children) {
83
            if (elem.getNamespace().equals(numberNS)) {
84
                if (elem.getName().equals("text")) {
85
                    sb.append(elem.getText());
86
                } else if (elem.getName().equals("number") || elem.getName().equals("scientific-number")) {
87
                    sb.append(formatNumberOrScientificNumber(elem, n, defaultStyle));
88
                } else if (elem.getName().equals("fraction")) {
89
                    // TODO fractions
90
                    reportError("Fractions not supported", lenient);
174 ilm 91
                    sb.append(getPackage().formatNumber(n, defaultStyle));
20 ilm 92
                }
93
            }
94
        }
95
        return sb.toString();
96
    }
97
}