001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.util;
021    
022    import javax.faces.convert.ConverterException;
023    import javax.faces.convert.DateTimeConverter;
024    import java.text.DateFormat;
025    import java.text.SimpleDateFormat;
026    import java.util.Locale;
027    
028    /**
029     * This code is taken from myfaces core.
030     * TODO: Should be sharable (e.g. myfaces-commons).
031     * <p/>
032     * User: lofwyr
033     * Date: 06.11.2006 17:20:29
034     */
035    public class DateFormatUtils {
036    
037      private static final String TYPE_DATE = "date";
038      private static final String TYPE_TIME = "time";
039      private static final String TYPE_BOTH = "both";
040      private static final String STYLE_DEFAULT = "default";
041      private static final String STYLE_MEDIUM = "medium";
042      private static final String STYLE_SHORT = "short";
043      private static final String STYLE_LONG = "long";
044      private static final String STYLE_FULL = "full";
045    
046      private DateFormatUtils() {
047      }
048    
049      /**
050       * Find a pattern for the converter.
051       * Returns the pattern inside the converter, if any.
052       * Otherwise compute the pattern.
053       *
054       * @return the patter or null, if DateFormat.getDateInstance() returns no SimpleDateFormat.
055       */
056      public static String findPattern(DateTimeConverter converter) {
057        String pattern = converter.getPattern();
058    
059        if (pattern == null) {
060          DateFormat dateFormat = getDateFormat(
061              converter.getType(), converter.getDateStyle(),
062              converter.getTimeStyle(), converter.getLocale());
063          if (dateFormat instanceof SimpleDateFormat) {
064            SimpleDateFormat format = (SimpleDateFormat) dateFormat;
065            pattern = format.toPattern();
066          }
067        }
068    
069        return pattern;
070      }
071    
072      public static DateFormat getDateFormat(String type, String dateStyle, String timeStyle, Locale locale) {
073        DateFormat format;
074        if (type.equals(TYPE_DATE)) {
075          format = DateFormat.getDateInstance(calcStyle(dateStyle), locale);
076        } else if (type.equals(TYPE_TIME)) {
077          format = DateFormat.getTimeInstance(calcStyle(timeStyle), locale);
078        } else if (type.equals(TYPE_BOTH)) {
079          format = DateFormat.getDateTimeInstance(calcStyle(dateStyle),
080              calcStyle(timeStyle),
081              locale);
082        } else {
083          throw new ConverterException("invalid type '" + type + "'");
084        }
085    
086        // format cannot be lenient (JSR-127)
087        format.setLenient(false);
088        return format;
089      }
090    
091      private static int calcStyle(String name) {
092        if (name.equals(STYLE_DEFAULT)) {
093          return DateFormat.DEFAULT;
094        }
095        if (name.equals(STYLE_MEDIUM)) {
096          return DateFormat.MEDIUM;
097        }
098        if (name.equals(STYLE_SHORT)) {
099          return DateFormat.SHORT;
100        }
101        if (name.equals(STYLE_LONG)) {
102          return DateFormat.LONG;
103        }
104        if (name.equals(STYLE_FULL)) {
105          return DateFormat.FULL;
106        }
107    
108        throw new ConverterException("invalid style '" + name + "'");
109      }
110    
111    }