OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | 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
 *
185 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
20 ilm 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;
180 ilm 47
        } else if (value instanceof java.time.Duration) {
48
            res = ODEpoch.getDays((java.time.Duration) value);
25 ilm 49
        } else if ((value instanceof Duration || value instanceof Date || value instanceof Calendar)) {
50
            if (value instanceof Duration) {
51
                res = epoch.getDays((Duration) value);
52
            } else {
53
                final Calendar cal;
54
                if (value instanceof Calendar) {
55
                    cal = (Calendar) value;
56
                } else {
80 ilm 57
                    cal = ODValueType.getCalendar();
25 ilm 58
                    cal.setTime((Date) value);
59
                }
60
                res = epoch.getDays(cal);
61
            }
62
        } else {
63
            res = null;
64
        }
65
        return res;
66
    }
67
 
20 ilm 68
    public NumberStyle(final ODPackage pkg, Element elem) {
25 ilm 69
        super(pkg, elem, ODValueType.FLOAT);
20 ilm 70
    }
71
 
72
    @Override
25 ilm 73
    protected Number convertNonNull(Object value) {
74
        return toNumber(value, getEpoch());
75
    }
76
 
77
    @Override
20 ilm 78
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
79
        final Number n = (Number) o;
80
        final Namespace numberNS = this.getElement().getNamespace();
81
        final StringBuilder sb = new StringBuilder();
82
        @SuppressWarnings("unchecked")
83
        final List<Element> children = this.getElement().getChildren();
84
        for (final Element elem : children) {
85
            if (elem.getNamespace().equals(numberNS)) {
86
                if (elem.getName().equals("text")) {
87
                    sb.append(elem.getText());
88
                } else if (elem.getName().equals("number") || elem.getName().equals("scientific-number")) {
180 ilm 89
                    sb.append(formatNumberOrScientificNumber(elem, n, defaultStyle, lenient));
20 ilm 90
                } else if (elem.getName().equals("fraction")) {
91
                    // TODO fractions
92
                    reportError("Fractions not supported", lenient);
180 ilm 93
                    sb.append(ODPackage.formatNumber(n, getLocale(), defaultStyle));
20 ilm 94
                }
95
            }
96
        }
97
        return sb.toString();
98
    }
99
}