OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 25 | Rev 61 | 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;
28 ilm 21
import org.openconcerto.utils.i18n.I18nUtils;
20 ilm 22
 
23
import java.util.List;
25 ilm 24
import java.util.Locale;
28 ilm 25
import java.util.ResourceBundle;
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
 
20 ilm 49
    public BooleanStyle(final ODPackage pkg, Element elem) {
25 ilm 50
        super(pkg, elem, ODValueType.BOOLEAN);
20 ilm 51
    }
52
 
53
    @Override
25 ilm 54
    protected Boolean convertNonNull(Object o) {
55
        return toBoolean(o);
56
    }
57
 
58
    @Override
20 ilm 59
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
25 ilm 60
        final Boolean b = (Boolean) o;
20 ilm 61
        final Namespace numberNS = this.getElement().getNamespace();
25 ilm 62
        final Locale styleLocale = DateStyle.getLocale(getElement());
20 ilm 63
        final StringBuilder sb = new StringBuilder();
64
        @SuppressWarnings("unchecked")
65
        final List<Element> children = this.getElement().getChildren();
66
        for (final Element elem : children) {
67
            if (elem.getNamespace().equals(numberNS)) {
68
                if (elem.getName().equals("text")) {
69
                    sb.append(elem.getText());
70
                } else if (elem.getName().equals("boolean")) {
28 ilm 71
                    final ResourceBundle bundle = ResourceBundle.getBundle(I18nUtils.RSRC_BASENAME, styleLocale);
72
                    if (!bundle.getLocale().getLanguage().equals(styleLocale.getLanguage()))
25 ilm 73
                        reportError("Boolean not localized", lenient);
28 ilm 74
                    sb.append(bundle.getString(I18nUtils.getBooleanKey(b)).toUpperCase(styleLocale));
20 ilm 75
                }
76
            }
77
        }
78
        return sb.toString();
79
    }
80
}