OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 21 | Rev 65 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21 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.StyleProperties;
18
import org.openconcerto.openoffice.StyleProperties;
18
import org.openconcerto.openoffice.XMLVersion;
19
import org.openconcerto.openoffice.XMLVersion;
19
import org.openconcerto.openoffice.spreadsheet.CellStyle;
20
import org.openconcerto.openoffice.spreadsheet.CellStyle;
-
 
21
import org.openconcerto.utils.convertor.NumberConvertor;
20
 
22
 
21
import java.math.BigDecimal;
23
import java.math.BigDecimal;
22
import java.text.DecimalFormat;
24
import java.text.DecimalFormat;
23
import java.text.DecimalFormatSymbols;
25
import java.text.DecimalFormatSymbols;
24
import java.text.SimpleDateFormat;
26
import java.text.SimpleDateFormat;
25
import java.util.Calendar;
27
import java.util.Calendar;
26
import java.util.Date;
28
import java.util.Date;
-
 
29
import java.util.GregorianCalendar;
27
import java.util.List;
30
import java.util.List;
28
import java.util.Locale;
31
import java.util.Locale;
29
 
32
 
30
import org.jdom.Attribute;
33
import org.jdom.Attribute;
31
import org.jdom.Element;
34
import org.jdom.Element;
Line 33... Line 36...
33
 
36
 
34
// from section 16.27.10 in v1.2-cs01-part1
37
// from section 16.27.10 in v1.2-cs01-part1
35
public class DateStyle extends DataStyle {
38
public class DateStyle extends DataStyle {
36
 
39
 
37
    // see http://download.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
40
    // see http://download.oracle.com/javase/6/docs/technotes/guides/intl/calendar.doc.html
38
    private static final Locale BUDDHIST_LOCALE = new Locale("th", "TH");
41
    private static final Calendar BUDDHIST_CAL = Calendar.getInstance(new Locale("th", "TH"));
39
    private static final Locale JAPANESE_LOCALE = new Locale("ja", "JP", "JP");
42
    private static final Calendar JAPANESE_CAL = Calendar.getInstance(new Locale("ja", "JP", "JP"));
40
    private static final Locale GREGORIAN_LOCALE = new Locale("fr", "FR");
43
    private static final Calendar GREGORIAN_CAL = new GregorianCalendar();
41
 
44
 
42
    public static final DataStyleDesc<DateStyle> DESC = new DataStyleDesc<DateStyle>(DateStyle.class, XMLVersion.OD, "date-style", "N") {
45
    public static final DataStyleDesc<DateStyle> DESC = new DataStyleDesc<DateStyle>(DateStyle.class, XMLVersion.OD, "date-style", "N") {
43
        @Override
46
        @Override
44
        public DateStyle create(ODPackage pkg, Element e) {
47
        public DateStyle create(ODPackage pkg, Element e) {
45
            return new DateStyle(pkg, e);
48
            return new DateStyle(pkg, e);
Line 61... Line 64...
61
            res = Locale.getDefault();
64
            res = Locale.getDefault();
62
        }
65
        }
63
        return res;
66
        return res;
64
    }
67
    }
65
 
68
 
66
    private static final Locale getCalendarLocale(final Element elem, Locale defaultLocale) {
69
    private static final Calendar getCalendar(final Element elem, Calendar defaultCal) {
67
        final Locale res;
70
        final Calendar res;
68
        final String cal = elem.getAttributeValue("calendar", elem.getNamespace());
71
        final String cal = elem.getAttributeValue("calendar", elem.getNamespace());
69
        if (cal == null) {
72
        if (cal == null) {
70
            res = defaultLocale;
73
            res = defaultCal;
71
        } else if ("buddhist".equals(cal)) {
74
        } else if ("buddhist".equals(cal)) {
72
            res = BUDDHIST_LOCALE;
75
            res = BUDDHIST_CAL;
73
        } else if ("gengou".equals(cal)) {
76
        } else if ("gengou".equals(cal)) {
74
            res = JAPANESE_LOCALE;
77
            res = JAPANESE_CAL;
75
        } else if ("gregorian".equals(cal)) {
78
        } else if ("gregorian".equals(cal)) {
76
            res = GREGORIAN_LOCALE;
79
            res = GREGORIAN_CAL;
77
        } else {
80
        } else {
78
            throw new IllegalArgumentException("Unsupported calendar : " + cal);
81
            throw new IllegalArgumentException("Unsupported calendar : " + cal);
79
        }
82
        }
80
        return res;
83
        return res;
81
    }
84
    }
82
 
85
 
83
    private static final Locale getCalendarLocale(final Locale locale) {
-
 
84
        final Locale res;
-
 
85
        if (locale.equals(BUDDHIST_LOCALE) || locale.equals(JAPANESE_LOCALE)) {
-
 
86
            res = locale;
-
 
87
        } else {
-
 
88
            res = GREGORIAN_LOCALE;
-
 
89
        }
-
 
90
        return res;
-
 
91
    }
-
 
92
 
-
 
93
    static String formatSecondFraction(final Locale styleLocale, final BigDecimal seconds, final int decPlaces) {
86
    static String formatSecondFraction(final Locale styleLocale, final BigDecimal seconds, final int decPlaces) {
94
        if (decPlaces > 0) {
87
        if (decPlaces > 0) {
95
            final DecimalFormat decFormat = new DecimalFormat();
88
            final DecimalFormat decFormat = new DecimalFormat();
96
            decFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(styleLocale));
89
            decFormat.setDecimalFormatSymbols(new DecimalFormatSymbols(styleLocale));
97
            decFormat.setMinimumIntegerDigits(0);
90
            decFormat.setMinimumIntegerDigits(0);
Line 104... Line 97...
104
            return "";
97
            return "";
105
        }
98
        }
106
    }
99
    }
107
 
100
 
108
    public DateStyle(final ODPackage pkg, Element elem) {
101
    public DateStyle(final ODPackage pkg, Element elem) {
109
        super(pkg, elem, Date.class);
102
        super(pkg, elem, ODValueType.DATE);
-
 
103
    }
-
 
104
 
-
 
105
    @Override
-
 
106
    protected Object convertNonNull(Object o) {
-
 
107
        if (o instanceof Number)
-
 
108
            return getEpoch().getDate(NumberConvertor.toBigDecimal((Number) o));
-
 
109
        else
-
 
110
            return null;
-
 
111
    }
-
 
112
 
-
 
113
    private final void format(final StringBuilder res, final StringBuilder pattern, final Locale styleLocale, final Calendar currentCalendar, final Date d) {
-
 
114
        if (pattern.length() > 0) {
-
 
115
            final SimpleDateFormat fmt = new SimpleDateFormat(pattern.toString(), styleLocale);
-
 
116
            pattern.setLength(0);
-
 
117
            fmt.setCalendar((Calendar) currentCalendar.clone());
-
 
118
            res.append(fmt.format(d));
-
 
119
        }
110
    }
120
    }
111
 
121
 
112
    @Override
122
    @Override
113
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
123
    public String format(Object o, CellStyle defaultStyle, boolean lenient) {
114
        final Date d = o instanceof Calendar ? ((Calendar) o).getTime() : (Date) o;
124
        final Date d = o instanceof Calendar ? ((Calendar) o).getTime() : (Date) o;
115
        final Namespace numberNS = this.getElement().getNamespace();
125
        final Namespace numberNS = this.getElement().getNamespace();
116
        final Locale styleLocale = getLocale(getElement());
126
        final Locale styleLocale = getLocale(getElement());
117
        final Locale styleCalendarLocale = getCalendarLocale(styleLocale);
127
        final Calendar styleCalendar = Calendar.getInstance(styleLocale);
118
        final StringBuilder res = new StringBuilder();
128
        final StringBuilder res = new StringBuilder();
119
 
129
 
120
        Locale currentCalendarLocale = styleCalendarLocale;
130
        Calendar currentCalendar = styleCalendar;
121
        final StringBuilder sb = new StringBuilder();
131
        final StringBuilder sb = new StringBuilder();
122
 
132
 
123
        @SuppressWarnings("unchecked")
133
        @SuppressWarnings("unchecked")
124
        final List<Element> children = this.getElement().getChildren();
134
        final List<Element> children = this.getElement().getChildren();
125
        for (final Element elem : children) {
135
        for (final Element elem : children) {
126
            if (elem.getNamespace().equals(numberNS)) {
136
            if (elem.getNamespace().equals(numberNS)) {
127
                final Locale calendarLocaleElem = getCalendarLocale(elem, styleCalendarLocale);
137
                final Calendar calendarLocaleElem = getCalendar(elem, styleCalendar);
128
                if (!calendarLocaleElem.equals(currentCalendarLocale)) {
138
                if (!calendarLocaleElem.equals(currentCalendar)) {
129
                    if (sb.length() > 0) {
-
 
130
                        res.append(new SimpleDateFormat(sb.toString(), currentCalendarLocale).format(d));
139
                    format(res, sb, styleLocale, currentCalendar, d);
131
                        sb.setLength(0);
-
 
132
                    }
-
 
133
                    currentCalendarLocale = calendarLocaleElem;
140
                    currentCalendar = calendarLocaleElem;
134
                }
141
                }
135
 
142
 
136
                if (elem.getName().equals("text")) {
143
                if (elem.getName().equals("text")) {
137
                    DataStyle.addStringLiteral(sb, elem.getText());
144
                    DataStyle.addStringLiteral(sb, elem.getText());
138
                } else if (elem.getName().equals("era")) {
145
                } else if (elem.getName().equals("era")) {
139
                    sb.append(isShort(elem) ? "G" : "GGGG");
146
                    sb.append(isShort(elem) ? "G" : "GGGG");
140
                } else if (elem.getName().equals("year")) {
147
                } else if (elem.getName().equals("year")) {
141
                    sb.append(isShort(elem) ? "yy" : "yyyy");
148
                    sb.append(isShort(elem) ? "yy" : "yyyy");
142
                } else if (elem.getName().equals("quarter")) {
149
                } else if (elem.getName().equals("quarter")) {
-
 
150
                    final Calendar cal = (Calendar) currentCalendar.clone();
-
 
151
                    cal.setTime(d);
-
 
152
                    final double quarterLength = cal.getActualMaximum(Calendar.MONTH) / 4.0;
143
                    final int quarter = Calendar.getInstance(GREGORIAN_LOCALE).get(Calendar.MONTH) / 3 + 1;
153
                    final int quarter = (int) (cal.get(Calendar.MONTH) / quarterLength + 1);
-
 
154
                    assert quarter >= 1 && quarter <= 4;
144
                    // TODO localize and honor short/long style
155
                    // TODO localize and honor short/long style
145
                    reportError("Quarters are not localized", lenient);
156
                    reportError("Quarters are not localized", lenient);
146
                    DataStyle.addStringLiteral(sb, isShort(elem) ? "Q" + quarter : "Q" + quarter);
157
                    DataStyle.addStringLiteral(sb, isShort(elem) ? "Q" + quarter : "Q" + quarter);
147
                } else if (elem.getName().equals("month")) {
158
                } else if (elem.getName().equals("month")) {
148
                    final Attribute possessive = elem.getAttribute("possessive-form", numberNS);
159
                    final Attribute possessive = elem.getAttribute("possessive-form", numberNS);
Line 182... Line 193...
182
                        DataStyle.addStringLiteral(sb, fractionPart);
193
                        DataStyle.addStringLiteral(sb, fractionPart);
183
                    }
194
                    }
184
                }
195
                }
185
            }
196
            }
186
        }
197
        }
187
        return new SimpleDateFormat(sb.toString(), currentCalendarLocale).format(d);
198
        format(res, sb, styleLocale, currentCalendar, d);
-
 
199
        return res.toString();
188
    }
200
    }
189
}
201
}