OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | 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
 *
185 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
20 ilm 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
 
65 ilm 33
    static final DataStyleDesc<BooleanStyle> DESC = new DataStyleDesc<BooleanStyle>(BooleanStyle.class, XMLVersion.OD, "boolean-style", "N") {
20 ilm 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
 
61 ilm 49
    public static final String toString(final boolean b, final Locale locale, final boolean lenient) {
50
        final ResourceBundle bundle = ResourceBundle.getBundle(I18nUtils.RSRC_BASENAME, locale);
51
        if (!bundle.getLocale().getLanguage().equals(locale.getLanguage()))
52
            reportError("Boolean not localized", lenient);
53
        return bundle.getString(I18nUtils.getBooleanKey(b)).toUpperCase(locale);
54
    }
55
 
20 ilm 56
    public BooleanStyle(final ODPackage pkg, Element elem) {
25 ilm 57
        super(pkg, elem, ODValueType.BOOLEAN);
20 ilm 58
    }
59
 
60
    @Override
25 ilm 61
    protected Boolean convertNonNull(Object o) {
62
        return toBoolean(o);
63
    }
64
 
65
    @Override
20 ilm 66
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
25 ilm 67
        final Boolean b = (Boolean) o;
20 ilm 68
        final Namespace numberNS = this.getElement().getNamespace();
174 ilm 69
        final Locale styleLocale = this.getLocale();
20 ilm 70
        final StringBuilder sb = new StringBuilder();
71
        @SuppressWarnings("unchecked")
72
        final List<Element> children = this.getElement().getChildren();
73
        for (final Element elem : children) {
74
            if (elem.getNamespace().equals(numberNS)) {
75
                if (elem.getName().equals("text")) {
76
                    sb.append(elem.getText());
77
                } else if (elem.getName().equals("boolean")) {
61 ilm 78
                    sb.append(toString(b, styleLocale, lenient));
20 ilm 79
                }
80
            }
81
        }
82
        return sb.toString();
83
    }
84
}