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.commons.converter;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.lang.reflect.Method;
23  
24  import com.sun.facelets.FaceletContext;
25  import com.sun.facelets.el.LegacyValueBinding;
26  import com.sun.facelets.tag.MetaRule;
27  import com.sun.facelets.tag.Metadata;
28  import com.sun.facelets.tag.MetadataTarget;
29  import com.sun.facelets.tag.TagAttribute;
30  import com.sun.facelets.tag.TagAttributeException;
31  
32  final class _TimeZoneRule extends MetaRule {
33  
34      final static class ValueBindingMetadata extends Metadata {
35  
36          private final String name;
37  
38          private final TagAttribute attr;
39  
40          private final Class type;
41  
42          public ValueBindingMetadata(String name, Class type,
43                  TagAttribute attr) {
44              this.name = name;
45              this.attr = attr;
46              this.type = type;
47          }
48  
49          public void applyMetadata(FaceletContext ctx, Object instance) {
50              ((ConverterBase) instance).setValueBinding(this.name, new LegacyValueBinding(this.attr
51                      .getValueExpression(ctx, this.type)));
52          }
53  
54      }
55      
56      final static class LiteralPropertyMetadata extends Metadata
57      {
58  
59          private final Method method;
60  
61          private final TagAttribute attribute;
62  
63          private Object[] value;
64  
65          public LiteralPropertyMetadata(Method method, TagAttribute attribute)
66          {
67              this.method = method;
68              this.attribute = attribute;
69          }
70  
71          public void applyMetadata(FaceletContext ctx, Object instance)
72          {
73              if (value == null)
74              {
75                  String str = this.attribute.getValue();
76                  value = new Object[] { java.util.TimeZone.getTimeZone( str ) };
77              }
78              try
79              {
80                  method.invoke(instance, this.value);
81              }
82              catch (InvocationTargetException e)
83              {
84                  throw new TagAttributeException(this.attribute, e.getCause());
85              }
86              catch (Exception e)
87              {
88                  throw new TagAttributeException(this.attribute, e);
89              }
90          }
91  
92      }
93  
94      public final static _TimeZoneRule Instance = new _TimeZoneRule();
95  
96      public _TimeZoneRule() {
97          super();
98      }
99  
100     public Metadata applyRule(String name, TagAttribute attribute,
101             MetadataTarget meta) {
102         if (meta.isTargetInstanceOf(ConverterBase.class)) {
103 
104             if ("timeZone".equals(name))
105             {
106                 // if component and dynamic, then must set expression
107                 if (!attribute.isLiteral()) {
108                     return new ValueBindingMetadata(name, Object.class, attribute);
109                 }
110                 else
111                 {
112                     Method m = meta.getWriteMethod(name);
113 
114                     return new LiteralPropertyMetadata(m, attribute);
115                 }
116             }
117         }
118         return null;
119     }
120 }