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  
20  package org.apache.myfaces.tobago.facelets;
21  
22  import org.apache.myfaces.tobago.component.InputSuggest;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.config.TobagoConfig;
25  import org.apache.myfaces.tobago.event.SheetStateChangeSource;
26  import org.apache.myfaces.tobago.event.SortActionSource;
27  import org.apache.myfaces.tobago.event.TabChangeSource;
28  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
29  
30  import javax.faces.component.EditableValueHolder;
31  import javax.faces.component.UIComponent;
32  import javax.faces.context.FacesContext;
33  import javax.faces.validator.Validator;
34  import javax.faces.view.facelets.ComponentConfig;
35  import javax.faces.view.facelets.ComponentHandler;
36  import javax.faces.view.facelets.FaceletContext;
37  import javax.faces.view.facelets.MetaRuleset;
38  import java.util.HashSet;
39  import java.util.Map;
40  import java.util.Set;
41  
42  public class TobagoComponentHandler extends ComponentHandler {
43  
44    public TobagoComponentHandler(final ComponentConfig componentConfig) {
45      super(componentConfig);
46    }
47  
48    @Override
49    protected MetaRuleset createMetaRuleset(final Class aClass) {
50      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
51      if (SortActionSource.class.isAssignableFrom(aClass)) {
52        metaRuleset.addRule(SortActionSourceRule.INSTANCE);
53      }
54      if (TabChangeSource.class.isAssignableFrom(aClass)) {
55        metaRuleset.addRule(TabChangeSourceRule.INSTANCE);
56      }
57      if (SheetStateChangeSource.class.isAssignableFrom(aClass)) {
58        metaRuleset.addRule(SheetStateChangeSourceRule.INSTANCE);
59      }
60      if (Visual.class.isAssignableFrom(aClass)) {
61        metaRuleset.addRule(SupportsMarkupRule.INSTANCE);
62      }
63      if (InputSuggest.class.isAssignableFrom(aClass)) {
64        metaRuleset.addRule(SuggestMethodRule.INSTANCE);
65      }
66  
67      return metaRuleset;
68    }
69  
70    @Override
71    public void onComponentPopulated(
72        final FaceletContext context, final UIComponent component, final UIComponent parent) {
73  
74      // TODO call only if component was created
75      if (component instanceof EditableValueHolder) {
76        addDefaultValidators(context.getFacesContext(), (EditableValueHolder) component);
77      }
78    }
79  
80    private void addDefaultValidators(final FacesContext context, final EditableValueHolder component) {
81      final TobagoConfigImpl tobagoConfig = (TobagoConfigImpl) TobagoConfig.getInstance(context);
82      final Map<String, String> validatorInfoMap = tobagoConfig.getDefaultValidatorInfo();
83      if (validatorInfoMap.isEmpty()) {
84        return;
85      }
86      final Validator[] validators = component.getValidators();
87      if (validators.length > 0) {
88        final Set<String> classNames = new HashSet<>();
89        // collect classNames of validators
90        for (final Validator validator : validators) {
91          classNames.add(validator.getClass().getName());
92        }
93        validatorInfoMap.forEach((key, value) -> {
94          if (!classNames.contains(value)) {
95            component.addValidator(context.getApplication().createValidator(key));
96          }
97        });
98      } else {
99        for (final String next : validatorInfoMap.keySet()) {
100         component.addValidator(context.getApplication().createValidator(next));
101       }
102     }
103   }
104 }