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 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 20 Rev 25
Line 12... Line 12...
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;
-
 
20
import org.openconcerto.utils.NumberUtils;
19
 
21
 
-
 
22
import java.util.HashMap;
20
import java.util.List;
23
import java.util.List;
-
 
24
import java.util.Locale;
-
 
25
import java.util.Map;
21
 
26
 
22
import org.jdom.Element;
27
import org.jdom.Element;
23
import org.jdom.Namespace;
28
import org.jdom.Namespace;
24
 
29
 
25
// from section 16.27.23 in v1.2-cs01-part1
30
// from section 16.27.23 in v1.2-cs01-part1
Line 30... Line 35...
30
        public BooleanStyle create(ODPackage pkg, Element e) {
35
        public BooleanStyle create(ODPackage pkg, Element e) {
31
            return new BooleanStyle(pkg, e);
36
            return new BooleanStyle(pkg, e);
32
        }
37
        }
33
    };
38
    };
34
 
39
 
-
 
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
 
35
    public BooleanStyle(final ODPackage pkg, Element elem) {
67
    public BooleanStyle(final ODPackage pkg, Element elem) {
36
        super(pkg, elem, Boolean.class);
68
        super(pkg, elem, ODValueType.BOOLEAN);
-
 
69
    }
-
 
70
 
-
 
71
    @Override
-
 
72
    protected Boolean convertNonNull(Object o) {
-
 
73
        return toBoolean(o);
37
    }
74
    }
38
 
75
 
39
    @Override
76
    @Override
40
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
77
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
-
 
78
        final Boolean b = (Boolean) o;
41
        final Namespace numberNS = this.getElement().getNamespace();
79
        final Namespace numberNS = this.getElement().getNamespace();
-
 
80
        final Locale styleLocale = DateStyle.getLocale(getElement());
42
        final StringBuilder sb = new StringBuilder();
81
        final StringBuilder sb = new StringBuilder();
43
        @SuppressWarnings("unchecked")
82
        @SuppressWarnings("unchecked")
44
        final List<Element> children = this.getElement().getChildren();
83
        final List<Element> children = this.getElement().getChildren();
45
        for (final Element elem : children) {
84
        for (final Element elem : children) {
46
            if (elem.getNamespace().equals(numberNS)) {
85
            if (elem.getNamespace().equals(numberNS)) {
47
                if (elem.getName().equals("text")) {
86
                if (elem.getName().equals("text")) {
48
                    sb.append(elem.getText());
87
                    sb.append(elem.getText());
49
                } else if (elem.getName().equals("boolean")) {
88
                } else if (elem.getName().equals("boolean")) {
50
                    // TODO localize
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 {
51
                    reportError("Boolean not localized", lenient);
96
                        reportError("Boolean not localized", lenient);
52
                    sb.append(o.toString());
97
                        s = b.toString();
-
 
98
                    }
-
 
99
                    sb.append(s);
53
                }
100
                }
54
            }
101
            }
55
        }
102
        }
56
        return sb.toString();
103
        return sb.toString();
57
    }
104
    }