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.view.facelets.tag.jsf.core;
20  
21  import java.util.TimeZone;
22  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.convert.Converter;
26  import javax.faces.convert.DateTimeConverter;
27  import javax.faces.view.facelets.ConverterConfig;
28  import javax.faces.view.facelets.ConverterHandler;
29  import javax.faces.view.facelets.FaceletContext;
30  import javax.faces.view.facelets.FaceletException;
31  import javax.faces.view.facelets.MetaRuleset;
32  import javax.faces.view.facelets.TagAttribute;
33  import javax.faces.view.facelets.TagAttributeException;
34  
35  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag;
36  import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
37  
38  /**
39   * Register a DateTimeConverter instance on the UIComponent associated with the closest parent UIComponent custom
40   * action. <p/> See <a target="_new"
41   * href="http://java.sun.com/j2ee/javaserverfaces/1.1_01/docs/tlddocs/f/convertDateTime.html">tag documentation</a>.
42   * 
43   * @deprecated use 
44   * @author Jacob Hookom
45   * @version $Id$
46   */
47  @JSFFaceletTag(
48          name = "f:convertDateTime",
49          bodyContent = "empty", 
50          converterClass="javax.faces.convert.DateTimeConverter")
51  public final class ConvertDateTimeHandler extends ConverterHandler
52  {
53  
54      private final TagAttribute dateStyle;
55  
56      private final TagAttribute locale;
57  
58      private final TagAttribute pattern;
59  
60      private final TagAttribute timeStyle;
61  
62      private final TagAttribute timeZone;
63  
64      private final TagAttribute type;
65  
66      /**
67       * @param config
68       */
69      public ConvertDateTimeHandler(ConverterConfig config)
70      {
71          super(config);
72          this.dateStyle = this.getAttribute("dateStyle");
73          this.locale = this.getAttribute("locale");
74          this.pattern = this.getAttribute("pattern");
75          this.timeStyle = this.getAttribute("timeStyle");
76          this.timeZone = this.getAttribute("timeZone");
77          this.type = this.getAttribute("type");
78      }
79  
80      /**
81       * Returns a new DateTimeConverter
82       * 
83       * @see DateTimeConverter
84       * @see javax.faces.view.facelets.ConverterHandler#createConverter(javax.faces.view.facelets.FaceletContext)
85       */
86      protected Converter createConverter(FaceletContext ctx) throws FacesException, ELException, FaceletException
87      {
88          return ctx.getFacesContext().getApplication().createConverter(DateTimeConverter.CONVERTER_ID);
89  
90      }
91  
92      /**
93       * Implements tag spec, see taglib documentation.
94       * 
95       * @see org.apache.myfaces.view.facelets.tag.ObjectHandler#setAttributes(javax.faces.view.facelets.FaceletContext, java.lang.Object)
96       */
97      public void setAttributes(FaceletContext ctx, Object obj)
98      {
99          DateTimeConverter c = (DateTimeConverter) obj;
100         if (this.locale != null)
101         {
102             c.setLocale(ComponentSupport.getLocale(ctx, this.locale));
103         }
104         if (this.pattern != null)
105         {
106             c.setPattern(this.pattern.getValue(ctx));
107         }
108         else
109         {
110             if (this.type != null)
111             {
112                 c.setType(this.type.getValue(ctx));
113             }
114             if (this.dateStyle != null)
115             {
116                 c.setDateStyle(this.dateStyle.getValue(ctx));
117             }
118             if (this.timeStyle != null)
119             {
120                 c.setTimeStyle(this.timeStyle.getValue(ctx));
121             }
122         }
123 
124         if (this.timeZone != null)
125         {
126             Object t = this.timeZone.getObject(ctx);
127             if (t != null)
128             {
129                 if (t instanceof TimeZone)
130                 {
131                     c.setTimeZone((TimeZone) t);
132                 }
133                 else if (t instanceof String)
134                 {
135                     TimeZone tz = TimeZone.getTimeZone((String) t);
136                     c.setTimeZone(tz);
137                 }
138                 else
139                 {
140                     throw new TagAttributeException(this.tag, this.timeZone,
141                                 "Illegal TimeZone, must evaluate to either a java.util.TimeZone or String, is type: "
142                                 + t.getClass());
143                 }
144             }
145         }
146     }
147 
148     protected MetaRuleset createMetaRuleset(Class type)
149     {
150         return super.createMetaRuleset(type).ignoreAll();
151     }
152 }