OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 20 | Rev 28 | 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
 
16
import org.openconcerto.openoffice.ODPackage;
25 ilm 17
import org.openconcerto.openoffice.ODValueType;
20 ilm 18
import org.openconcerto.openoffice.XMLVersion;
19
import org.openconcerto.openoffice.spreadsheet.CellStyle;
25 ilm 20
import org.openconcerto.utils.NumberUtils;
20 ilm 21
 
25 ilm 22
import java.util.HashMap;
20 ilm 23
import java.util.List;
25 ilm 24
import java.util.Locale;
25
import java.util.Map;
20 ilm 26
 
27
import org.jdom.Element;
28
import org.jdom.Namespace;
29
 
30
// from section 16.27.23 in v1.2-cs01-part1
31
public class BooleanStyle extends DataStyle {
32
 
33
    public static final DataStyleDesc<BooleanStyle> DESC = new DataStyleDesc<BooleanStyle>(BooleanStyle.class, XMLVersion.OD, "boolean-style", "N") {
34
        @Override
35
        public BooleanStyle create(ODPackage pkg, Element e) {
36
            return new BooleanStyle(pkg, e);
37
        }
38
    };
39
 
25 ilm 40
    public static final Boolean toBoolean(Object o) {
41
        if (o instanceof Boolean)
42
            return (Boolean) o;
43
        else if (o instanceof Number)
44
            return Boolean.valueOf(!NumberUtils.areNumericallyEqual(0, (Number) o));
45
        else
46
            return null;
47
    }
48
 
49
    private static final Map<String, String> trues = new HashMap<String, String>(), falses = new HashMap<String, String>();
50
 
51
    private static final void add(final String iso3, final String trueS, final String falseS) {
52
        if (trueS == null || falseS == null)
53
            throw new NullPointerException();
54
        trues.put(iso3, trueS);
55
        falses.put(iso3, falseS);
56
    }
57
 
58
    static {
59
        add(Locale.FRENCH.getISO3Language(), "VRAI", "FAUX");
60
        add(Locale.ENGLISH.getISO3Language(), "TRUE", "FALSE");
61
        add(Locale.GERMAN.getISO3Language(), "WAHR", "FALSCH");
62
        add(Locale.ITALY.getISO3Language(), "VERO", "FALSO");
63
        add("spa", "VERDADERO", "FALSO");
64
        add("por", "VERDADEIRO", "FALSO");
65
    }
66
 
20 ilm 67
    public BooleanStyle(final ODPackage pkg, Element elem) {
25 ilm 68
        super(pkg, elem, ODValueType.BOOLEAN);
20 ilm 69
    }
70
 
71
    @Override
25 ilm 72
    protected Boolean convertNonNull(Object o) {
73
        return toBoolean(o);
74
    }
75
 
76
    @Override
20 ilm 77
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
25 ilm 78
        final Boolean b = (Boolean) o;
20 ilm 79
        final Namespace numberNS = this.getElement().getNamespace();
25 ilm 80
        final Locale styleLocale = DateStyle.getLocale(getElement());
20 ilm 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("boolean")) {
25 ilm 89
                    // TODO localize more
90
                    final String s;
91
                    final String iso3Lang = styleLocale.getISO3Language();
92
                    final String localized = b.booleanValue() ? trues.get(iso3Lang) : falses.get(iso3Lang);
93
                    if (localized != null) {
94
                        s = localized;
95
                    } else {
96
                        reportError("Boolean not localized", lenient);
97
                        s = b.toString();
98
                    }
99
                    sb.append(s);
20 ilm 100
                }
101
            }
102
        }
103
        return sb.toString();
104
    }
105
}