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;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.component.ValueHolder;
23  import javax.faces.convert.Converter;
24  import javax.faces.view.facelets.FaceletContext;
25  import javax.faces.view.facelets.MetaRule;
26  import javax.faces.view.facelets.Metadata;
27  import javax.faces.view.facelets.MetadataTarget;
28  import javax.faces.view.facelets.TagAttribute;
29  
30  /**
31   * 
32   * @author Jacob Hookom
33   * @version $Id$
34   */
35  public final class ValueHolderRule extends MetaRule
36  {
37  
38      final static class LiteralConverterMetadata extends Metadata
39      {
40  
41          private final String converterId;
42  
43          public LiteralConverterMetadata(String converterId)
44          {
45              this.converterId = converterId;
46          }
47  
48          public void applyMetadata(FaceletContext ctx, Object instance)
49          {
50              ((ValueHolder) instance).setConverter(ctx.getFacesContext().getApplication()
51                      .createConverter(this.converterId));
52          }
53      }
54  
55      final static class DynamicConverterMetadata2 extends Metadata
56      {
57  
58          private final TagAttribute attr;
59  
60          public DynamicConverterMetadata2(TagAttribute attr)
61          {
62              this.attr = attr;
63          }
64  
65          public void applyMetadata(FaceletContext ctx, Object instance)
66          {
67              ((UIComponent) instance).setValueExpression("converter", attr.getValueExpression(ctx, Converter.class));
68          }
69      }
70  
71      final static class LiteralValueMetadata extends Metadata
72      {
73  
74          private final String value;
75  
76          public LiteralValueMetadata(String value)
77          {
78              this.value = value;
79          }
80  
81          public void applyMetadata(FaceletContext ctx, Object instance)
82          {
83              ((ValueHolder) instance).setValue(this.value);
84          }
85      }
86  
87      final static class DynamicValueExpressionMetadata extends Metadata
88      {
89  
90          private final TagAttribute attr;
91  
92          public DynamicValueExpressionMetadata(TagAttribute attr)
93          {
94              this.attr = attr;
95          }
96  
97          public void applyMetadata(FaceletContext ctx, Object instance)
98          {
99              ((UIComponent) instance).setValueExpression("value", attr.getValueExpression(ctx, Object.class));
100         }
101     }
102 
103     public final static ValueHolderRule INSTANCE = new ValueHolderRule();
104 
105     public Metadata applyRule(String name, TagAttribute attribute, MetadataTarget meta)
106     {
107         if (meta.isTargetInstanceOf(ValueHolder.class))
108         {
109 
110             if ("converter".equals(name))
111             {
112                 if (attribute.isLiteral())
113                 {
114                     return new LiteralConverterMetadata(attribute.getValue());
115                 }
116                 else
117                 {
118                     return new DynamicConverterMetadata2(attribute);
119                 }
120             }
121 
122             if ("value".equals(name))
123             {
124                 if (attribute.isLiteral())
125                 {
126                     return new LiteralValueMetadata(attribute.getValue());
127                 }
128                 else
129                 {
130                     return new DynamicValueExpressionMetadata(attribute);
131                 }
132             }
133         }
134         return null;
135     }
136 
137 }