OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Rev 65 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 25
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.ODPackage;
16
import org.openconcerto.openoffice.ODPackage;
-
 
17
import org.openconcerto.openoffice.ODValueType;
17
import org.openconcerto.openoffice.XMLVersion;
18
import org.openconcerto.openoffice.XMLVersion;
18
import org.openconcerto.openoffice.spreadsheet.CellStyle;
19
import org.openconcerto.openoffice.spreadsheet.CellStyle;
19
 
20
 
20
import java.text.DecimalFormatSymbols;
21
import java.text.DecimalFormatSymbols;
21
import java.util.List;
22
import java.util.List;
22
 
23
 
23
import org.jdom.Element;
24
import org.jdom.Element;
24
import org.jdom.Namespace;
25
import org.jdom.Namespace;
25
 
26
 
26
// from section 16.27.7 in v1.2-cs01-part1
27
// from section 16.27.7 in v1.2-cs01-part1
27
public class CurrencyStyle extends DataStyle {
28
public class CurrencyStyle extends DataStyle {
28
 
29
 
29
    public static final DataStyleDesc<CurrencyStyle> DESC = new DataStyleDesc<CurrencyStyle>(CurrencyStyle.class, XMLVersion.OD, "currency-style", "N") {
30
    public static final DataStyleDesc<CurrencyStyle> DESC = new DataStyleDesc<CurrencyStyle>(CurrencyStyle.class, XMLVersion.OD, "currency-style", "N") {
30
        @Override
31
        @Override
31
        public CurrencyStyle create(ODPackage pkg, Element e) {
32
        public CurrencyStyle create(ODPackage pkg, Element e) {
32
            return new CurrencyStyle(pkg, e);
33
            return new CurrencyStyle(pkg, e);
33
        }
34
        }
34
    };
35
    };
35
 
36
 
36
    public CurrencyStyle(final ODPackage pkg, Element elem) {
37
    public CurrencyStyle(final ODPackage pkg, Element elem) {
37
        super(pkg, elem, Number.class);
38
        super(pkg, elem, ODValueType.CURRENCY);
-
 
39
    }
-
 
40
 
-
 
41
    @Override
-
 
42
    protected Object convertNonNull(Object o) {
-
 
43
        return NumberStyle.toNumber(o, getEpoch());
38
    }
44
    }
39
 
45
 
40
    @Override
46
    @Override
41
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
47
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
42
        final Number n = (Number) o;
48
        final Number n = (Number) o;
43
        final Namespace numberNS = this.getElement().getNamespace();
49
        final Namespace numberNS = this.getElement().getNamespace();
44
        final StringBuilder sb = new StringBuilder();
50
        final StringBuilder sb = new StringBuilder();
45
        @SuppressWarnings("unchecked")
51
        @SuppressWarnings("unchecked")
46
        final List<Element> children = this.getElement().getChildren();
52
        final List<Element> children = this.getElement().getChildren();
47
        for (final Element elem : children) {
53
        for (final Element elem : children) {
48
            if (elem.getNamespace().equals(numberNS)) {
54
            if (elem.getNamespace().equals(numberNS)) {
49
                if (elem.getName().equals("text")) {
55
                if (elem.getName().equals("text")) {
50
                    sb.append(elem.getText());
56
                    sb.append(elem.getText());
51
                } else if (elem.getName().equals("number")) {
57
                } else if (elem.getName().equals("number")) {
52
                    // ATTN OpenOffice Fix (it generates <text>-</text>, so we have to use the
58
                    // ATTN OpenOffice Fix (it generates <text>-</text>, so we have to use the
53
                    // absolute value)
59
                    // absolute value)
54
                    final int multiplier = n.doubleValue() > 0 ? 1 : -1;
60
                    final int multiplier = n.doubleValue() > 0 ? 1 : -1;
55
                    sb.append(formatNumberOrScientificNumber(elem, n, multiplier, defaultStyle));
61
                    sb.append(formatNumberOrScientificNumber(elem, n, multiplier, defaultStyle));
56
                } else if (elem.getName().equals("currency-symbol")) {
62
                } else if (elem.getName().equals("currency-symbol")) {
57
                    if (elem.getTextTrim().length() > 0) {
63
                    if (elem.getTextTrim().length() > 0) {
58
                        sb.append(elem.getText());
64
                        sb.append(elem.getText());
59
                    } else {
65
                    } else {
60
                        sb.append(new DecimalFormatSymbols(DateStyle.getLocale(elem)).getCurrencySymbol());
66
                        sb.append(new DecimalFormatSymbols(DateStyle.getLocale(elem)).getCurrencySymbol());
61
                    }
67
                    }
62
                }
68
                }
63
            }
69
            }
64
        }
70
        }
65
        return sb.toString();
71
        return sb.toString();
66
    }
72
    }
67
}
73
}