View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.parameters;
23  
24  import javax.el.ValueExpression;
25  import javax.faces.view.facelets.ComponentConfig;
26  import javax.faces.view.facelets.ComponentHandler;
27  import javax.faces.view.facelets.FaceletContext;
28  import javax.faces.view.facelets.MetaRule;
29  import javax.faces.view.facelets.MetaRuleset;
30  import javax.faces.view.facelets.Metadata;
31  import javax.faces.view.facelets.MetadataTarget;
32  import javax.faces.view.facelets.TagAttribute;
33  
34  /**
35   * {@link ComponentHandler} for the {@link AssignableParameter} component.
36   *
37   * @author Thomas Andraschko / last modified by $Author$
38   * @version $Revision$
39   * @since 0.5
40   */
41  public class AssignableParameterHandler extends ComponentHandler {
42  
43      private static final AssignToMetaRule META_RULE = new AssignToMetaRule();
44  
45      /**
46       * {@link MetaRule} for the <code>assignTo</code> of the {@link AssignableParameter}.
47       *
48       * @author Thomas Andraschko / last modified by $Author$
49       * @version $Revision$
50       */
51      private static final class AssignToMetaRule extends MetaRule {
52  
53          @Override
54          public Metadata applyRule(final String name, final TagAttribute attribute, final MetadataTarget meta) {
55              if (meta.isTargetInstanceOf(AssignableParameter.class) && AssignableParameter.PropertyKeys.assignTo.toString().equals(name)) {
56                  return new AssignToValueExpressionMetadata(attribute);
57              }
58  
59              return null;
60          }
61      }
62  
63      /**
64       * {@link Metadata} for the <code>assignTo</code> of the {@link AssignableParameter}.
65       *
66       * @author Thomas Andraschko / last modified by $Author$
67       * @version $Revision$
68       */
69      private static final class AssignToValueExpressionMetadata extends Metadata {
70  
71          private final TagAttribute attribute;
72  
73          public AssignToValueExpressionMetadata(final TagAttribute attribute) {
74              this.attribute = attribute;
75          }
76  
77          @Override
78          public void applyMetadata(final FaceletContext context, final Object instance) {
79              final AssignableParameter param = (AssignableParameter) instance;
80              final ValueExpression valueExpression = attribute.getValueExpression(context, Object.class);
81  
82              param.setAssignTo(valueExpression);
83          }
84      }
85  
86      public AssignableParameterHandler(final ComponentConfig config) {
87          super(config);
88      }
89  
90      @Override
91      protected MetaRuleset createMetaRuleset(final Class type) {
92          final MetaRuleset metaRuleset = super.createMetaRuleset(type);
93          metaRuleset.addRule(META_RULE);
94  
95          return metaRuleset;
96      }
97  }