1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.charts.params

File DateFormatChartParam.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

22
47
5
1
155
112
20
0.43
9.4
5
4

Classes

Class Line # Actions
DateFormatChartParam 32 47 0% 20 74
0.00%
 

Contributing tests

No tests hitting this source file were found.

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 com.xpn.xwiki.plugin.charts.params;
21   
22    import java.text.DateFormat;
23    import java.text.SimpleDateFormat;
24    import java.util.HashMap;
25    import java.util.Locale;
26    import java.util.Map;
27   
28    import com.xpn.xwiki.plugin.charts.exceptions.InvalidArgumentException;
29    import com.xpn.xwiki.plugin.charts.exceptions.MissingArgumentException;
30    import com.xpn.xwiki.plugin.charts.exceptions.ParamException;
31   
 
32    public class DateFormatChartParam extends LocaleChartParam
33    {
34    public static final String TYPE = "type";
35   
36    public static final String DATE = "date";
37   
38    public static final String TIME = "time";
39   
40    public static final String DATETIME = "datetime";
41   
42    public static final String CUSTOM = "custom";
43   
44    public static final String DATE_STYLE = "date_style";
45   
46    public static final String TIME_STYLE = "time_style";
47   
48    public static final String SHORT = "short";
49   
50    public static final String MEDIUM = "medium";
51   
52    public static final String LONG = "long";
53   
54    public static final String FULL = "full";
55   
56    public static final String PATTERN = "pattern";
57   
58    private Map styleChoices;
59   
 
60  0 toggle public DateFormatChartParam(String name)
61    {
62  0 super(name);
63  0 init();
64    }
65   
 
66  0 toggle public DateFormatChartParam(String name, boolean optional)
67    {
68  0 super(name, optional);
69  0 init();
70    }
71   
 
72  0 toggle public void init()
73    {
74  0 styleChoices = new HashMap(4);
75  0 styleChoices.put(SHORT, DateFormat.SHORT);
76  0 styleChoices.put(MEDIUM, DateFormat.MEDIUM);
77  0 styleChoices.put(LONG, DateFormat.LONG);
78  0 styleChoices.put(FULL, DateFormat.FULL);
79    }
80   
 
81  0 toggle @Override
82    public Class getType()
83    {
84  0 return DateFormat.class;
85    }
86   
 
87  0 toggle @Override
88    public Object convert(String value) throws ParamException
89    {
90  0 Map map = parseMap(value);
91   
92  0 Integer dateStyle, timeStyle;
93  0 try {
94  0 dateStyle = (Integer) getChoiceArg(map, DATE_STYLE, styleChoices);
95    } catch (MissingArgumentException e) {
96  0 dateStyle = null;
97    }
98  0 try {
99  0 timeStyle = (Integer) getChoiceArg(map, TIME_STYLE, styleChoices);
100    } catch (MissingArgumentException e) {
101  0 timeStyle = null;
102    }
103   
104  0 Locale locale;
105  0 try {
106  0 locale = (Locale) super.convert(value);
107    } catch (MissingArgumentException e) {
108  0 locale = null;
109    }
110   
111  0 String type = getStringArg(map, TYPE);
112   
113  0 if (type.equals(DATE)) {
114  0 if (dateStyle != null) {
115  0 if (locale != null) {
116  0 return DateFormat.getDateInstance(dateStyle.intValue(), locale);
117    } else {
118  0 return DateFormat.getDateInstance(dateStyle.intValue());
119    }
120    } else {
121  0 return DateFormat.getDateInstance();
122    }
123  0 } else if (type.equals(TIME)) {
124  0 if (timeStyle != null) {
125  0 if (locale != null) {
126  0 return DateFormat.getTimeInstance(timeStyle.intValue(), locale);
127    } else {
128  0 return DateFormat.getTimeInstance(timeStyle.intValue());
129    }
130    } else {
131  0 return DateFormat.getDateInstance();
132    }
133  0 } else if (type.equals(DATETIME)) {
134  0 if (dateStyle != null && timeStyle != null) {
135  0 if (locale != null) {
136  0 return DateFormat.getDateTimeInstance(dateStyle.intValue(), timeStyle.intValue(), locale);
137    } else {
138  0 return DateFormat.getDateTimeInstance(dateStyle.intValue(), timeStyle.intValue());
139    }
140    } else {
141  0 return DateFormat.getDateTimeInstance();
142    }
143  0 } else if (type.equals(CUSTOM)) {
144  0 String pattern = getStringArg(map, PATTERN);
145  0 if (locale != null) {
146  0 return new SimpleDateFormat(pattern, locale);
147    } else {
148  0 return new SimpleDateFormat(pattern);
149    }
150    } else {
151  0 throw new InvalidArgumentException("Invalid value for parameter " + getName()
152    + ": Unexpected value for type argument: " + type);
153    }
154    }
155    }