View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.internal.util;
21  
22  import javax.faces.convert.ConverterException;
23  import javax.faces.convert.DateTimeConverter;
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Locale;
27  
28  /**
29   * This code is taken from myfaces core.
30   * TODO: Should be sharable (e.g. myfaces-commons).
31   * <p/>
32   */
33  public final class DateFormatUtils {
34  
35    private static final String TYPE_DATE = "date";
36    private static final String TYPE_TIME = "time";
37    private static final String TYPE_BOTH = "both";
38    private static final String STYLE_DEFAULT = "default";
39    private static final String STYLE_MEDIUM = "medium";
40    private static final String STYLE_SHORT = "short";
41    private static final String STYLE_LONG = "long";
42    private static final String STYLE_FULL = "full";
43  
44    private DateFormatUtils() {
45    }
46  
47    /**
48     * Find a pattern for the converter.
49     * Returns the pattern inside the converter, if any.
50     * Otherwise compute the pattern.
51     *
52     * @return the patter or null, if DateFormat.getDateInstance() returns no SimpleDateFormat.
53     */
54    public static String findPattern(DateTimeConverter converter) {
55      String pattern = converter.getPattern();
56  
57      if (pattern == null) {
58        DateFormat dateFormat = getDateFormat(
59            converter.getType(), converter.getDateStyle(),
60            converter.getTimeStyle(), converter.getLocale());
61        if (dateFormat instanceof SimpleDateFormat) {
62          SimpleDateFormat format = (SimpleDateFormat) dateFormat;
63          pattern = format.toPattern();
64        }
65      }
66  
67      return pattern;
68    }
69  
70    public static DateFormat getDateFormat(String type, String dateStyle, String timeStyle, Locale locale) {
71      DateFormat format;
72      if (type.equals(TYPE_DATE)) {
73        format = DateFormat.getDateInstance(calcStyle(dateStyle), locale);
74      } else if (type.equals(TYPE_TIME)) {
75        format = DateFormat.getTimeInstance(calcStyle(timeStyle), locale);
76      } else if (type.equals(TYPE_BOTH)) {
77        format = DateFormat.getDateTimeInstance(calcStyle(dateStyle),
78            calcStyle(timeStyle),
79            locale);
80      } else {
81        throw new ConverterException("invalid type '" + type + "'");
82      }
83  
84      // format cannot be lenient (JSR-127)
85      format.setLenient(false);
86      return format;
87    }
88  
89    private static int calcStyle(String name) {
90      if (name.equals(STYLE_DEFAULT)) {
91        return DateFormat.DEFAULT;
92      }
93      if (name.equals(STYLE_MEDIUM)) {
94        return DateFormat.MEDIUM;
95      }
96      if (name.equals(STYLE_SHORT)) {
97        return DateFormat.SHORT;
98      }
99      if (name.equals(STYLE_LONG)) {
100       return DateFormat.LONG;
101     }
102     if (name.equals(STYLE_FULL)) {
103       return DateFormat.FULL;
104     }
105 
106     throw new ConverterException("invalid style '" + name + "'");
107   }
108 
109 }