1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.macro.chart.source

File LocaleConfiguration.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
23
5
1
142
65
12
0.52
4.6
5
2.4

Classes

Class Line # Actions
LocaleConfiguration 38 23 0% 12 6
0.8421052784.2%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.rendering.internal.macro.chart.source;
21   
22    import java.text.DateFormat;
23    import java.text.SimpleDateFormat;
24    import java.util.Locale;
25    import java.util.TimeZone;
26   
27    import org.apache.commons.lang3.LocaleUtils;
28    import org.xwiki.rendering.macro.MacroExecutionException;
29   
30    /**
31    * A configuration object for locale and time zone.
32    *
33    * This super class provides basic parameter validation.
34    *
35    * @version $Id: 8eccf1ec8b7f894ec508458645d10bee140dc9fb $
36    * @since 4.2M1
37    */
 
38    public class LocaleConfiguration extends AbstractConfigurator
39    {
40    /**
41    * The name of the locale parameter.
42    */
43    public static final String LOCALE_PARAM = "locale";
44   
45    /**
46    * The name of the dateformat parameter.
47    */
48    public static final String DATEFORMAT_PARAM = "date_format";
49   
50    /**
51    * The locale used for generating date format.
52    */
53    private Locale locale = Locale.getDefault();
54   
55    /**
56    * The time zone.
57    */
58    private TimeZone timeZone = TimeZone.getDefault();
59   
60    /**
61    * The dateformat. Default is taken from the server locale.
62    */
63    private DateFormat dateFormat = DateFormat.getInstance();
64   
65    /**
66    * A configured date format pattern string.
67    */
68    private String dateFormatString;
69   
70    /**
71    * Let an implementation set a parameter.
72    *
73    * @param key The key of the parameter.
74    * @param value The value of the parameter.
75    * @return {@code true} if the parameter was claimed.
76    * @throws MacroExecutionException if the parameter is invalid in some way.
77    */
 
78  144 toggle public boolean setParameter(String key, String value) throws MacroExecutionException
79    {
80  144 boolean claimed = true;
81   
82  144 if (LOCALE_PARAM.equals(key)) {
83  2 boolean valid = true;
84  2 Locale l;
85  2 try {
86  2 l = LocaleUtils.toLocale(value);
87  2 if (!LocaleUtils.isAvailableLocale(l)) {
88  0 valid = false;
89    } else {
90  2 this.locale = l;
91    }
92    } catch (IllegalArgumentException e) {
93  0 valid = false;
94    }
95  2 if (!valid) {
96  0 throw new MacroExecutionException(String.format("Invalid locale string [%s].", value));
97    }
98  142 } else if (DATEFORMAT_PARAM.equals(key)) {
99  5 this.dateFormatString = value;
100    } else {
101  137 claimed = false;
102    }
103   
104  144 return claimed;
105    }
106   
 
107  21 toggle @Override
108    public void validateParameters() throws MacroExecutionException
109    {
110  21 try {
111  21 if (dateFormatString != null) {
112  5 dateFormat = new SimpleDateFormat(dateFormatString, locale);
113    }
114    } catch (IllegalArgumentException e) {
115  0 throw new MacroExecutionException(String.format("Invalid date format [%s].", dateFormatString));
116    }
117    }
118   
119    /**
120    * @return the date format.
121    */
 
122  58 toggle public DateFormat getDateFormat()
123    {
124  58 return dateFormat;
125    }
126   
127    /**
128    * @return the locale.
129    */
 
130  122 toggle public Locale getLocale()
131    {
132  122 return locale;
133    }
134   
135    /**
136    * @return the time zone.
137    */
 
138  119 toggle public TimeZone getTimeZone()
139    {
140  119 return timeZone;
141    }
142    }