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 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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