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  package org.apache.myfaces.shared_orchestra.taglib.core;
    20
    21  import java.util.Locale;
    22  import java.util.TimeZone;
    23
    24  import javax.faces.context.FacesContext;
    25  import javax.faces.convert.Converter;
    26  import javax.faces.convert.DateTimeConverter;
    27  import javax.faces.el.ValueBinding;
    28  import javax.faces.webapp.ConverterTag;
    29  import javax.faces.webapp.UIComponentTag;
    30  import javax.servlet.jsp.JspException;
    31
    32  import org.apache.myfaces.shared_orchestra.util.LocaleUtils;
    33
    34  /**
    35   * @author Manfred Geiler (latest modification by $Author: schof $)
    36   * @version $Revision: 382015 $ $Date: 2006-03-01 14:47:11 +0100 (Wed, 01 Mar 2006) $
    37   */
    38  public class ConvertDateTimeTagBase
    39          extends ConverterTag
    40  {
    41      private static final long serialVersionUID = -757757296071312897L;
    42      private String _dateStyle = "default"; // the default value as required by the spec (default in this case)
    43      private String _locale = null;
    44      private String _pattern = null;
    45      private String _timeStyle = "default"; // the default value as required by the spec (default in this case)
    46      private String _timeZone = null;
    47      private String _type = null;
    48
    49      public void setDateStyle(String dateStyle)
    50      {
    51          _dateStyle = dateStyle;
    52      }
    53
    54      public void setLocale(String locale)
    55      {
    56          _locale = locale;
    57      }
    58
    59      public void setPattern(String pattern)
    60      {
    61          _pattern = pattern;
    62      }
    63
    64      public void setTimeStyle(String timeStyle)
    65      {
    66          _timeStyle = timeStyle;
    67      }
    68
    69      public void setTimeZone(String timeZone)
    70      {
    71          _timeZone = timeZone;
    72      }
    73
    74      public void setType(String type)
    75      {
    76          _type = type;
    77      }
    78
    79      protected Converter createConverter() throws JspException
    80      {
    81          DateTimeConverter converter = (DateTimeConverter)super.createConverter();
    82
    83          FacesContext facesContext = FacesContext.getCurrentInstance();
    84          setConverterDateStyle(facesContext, converter, _dateStyle);
    85          setConverterLocale(facesContext, converter, _locale);
    86          setConverterPattern(facesContext, converter, _pattern);
    87          setConverterTimeStyle(facesContext, converter, _timeStyle);
    88          setConverterTimeZone(facesContext, converter, _timeZone);
    89          setConverterType(facesContext, converter, _type);
    90
    91          return converter;
    92      }
    93
    94      protected static void setConverterLocale(FacesContext facesContext,
    95                                               DateTimeConverter converter,
    96                                               String value)
    97      {
    98         Locale locale = null;
    99         Object _value = null;
    100
    101         if (value == null) return;
    102         if (UIComponentTag.isValueReference(value))
    103         {
    104             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    105             _value = vb.getValue(facesContext);
    106             if(_value instanceof Locale)
    107             {
    108                 locale = (Locale) _value;
    109             }
    110             else
    111             {
    112                 locale = LocaleUtils.converterTagLocaleFromString(_value.toString());
    113             }
    114         }
    115         else
    116         {
    117             locale = LocaleUtils.converterTagLocaleFromString(    value);
    118         }
    119         converter.setLocale(locale);
    120     }
    121
    122
    123     private static void setConverterDateStyle(FacesContext facesContext,
    124                                               DateTimeConverter converter,
    125                                               String value)
    126     {
    127         if (value == null) return;
    128         if (UIComponentTag.isValueReference(value))
    129         {
    130             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    131             converter.setDateStyle((String)vb.getValue(facesContext));
    132         }
    133         else
    134         {
    135             converter.setDateStyle(value);
    136         }
    137     }
    138
    139     private static void setConverterPattern(FacesContext facesContext,
    140                                             DateTimeConverter converter,
    141                                             String value)
    142     {
    143         if (value == null) return;
    144         if (UIComponentTag.isValueReference(value))
    145         {
    146             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    147             converter.setPattern((String)vb.getValue(facesContext));
    148         }
    149         else
    150         {
    151             converter.setPattern(value);
    152         }
    153     }
    154
    155     private static void setConverterTimeStyle(FacesContext facesContext,
    156                                               DateTimeConverter converter,
    157                                               String value)
    158     {
    159         if (value == null) return;
    160         if (UIComponentTag.isValueReference(value))
    161         {
    162             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    163             converter.setTimeStyle((String)vb.getValue(facesContext));
    164         }
    165         else
    166         {
    167             converter.setTimeStyle(value);
    168         }
    169     }
    170
    171     private static void setConverterTimeZone(FacesContext facesContext,
    172                                              DateTimeConverter converter,
    173                                              String value)
    174     {
    175         if (value == null) return;
    176          if (UIComponentTag.isValueReference(value))
    177          {
    178             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    179             TimeZone timeZone = null;
    180             Object _value = vb.getValue(facesContext);
    181             if (_value instanceof TimeZone)
    182             {
    183               timeZone = (TimeZone) _value;
    184             }
    185             else
    186             {
    187               timeZone = TimeZone.getTimeZone(_value.toString());
    188             }
    189            converter.setTimeZone(timeZone);
    190         }
    191         else
    192         {
    193             converter.setTimeZone(TimeZone.getTimeZone(value));
    194         }
    195     }
    196
    197     private static void setConverterType(FacesContext facesContext,
    198                                          DateTimeConverter converter,
    199                                          String value)
    200     {
    201         if (value == null) return;
    202         if (UIComponentTag.isValueReference(value))
    203         {
    204             ValueBinding vb = facesContext.getApplication().createValueBinding(value);
    205             converter.setType((String)vb.getValue(facesContext));
    206         }
    207         else
    208         {
    209             converter.setType(value);
    210         }
    211     }
    212
    213 }