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