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.custom.validatebeanbehavior;
20  
21  import javax.faces.FacesException;
22  import javax.faces.component.UIComponent;
23  import javax.faces.component.UIForm;
24  import javax.faces.component.UIInput;
25  import javax.faces.component.UIMessages;
26  import java.util.ArrayList;
27  import java.util.List;
28  
29  /**
30   * Common component tree utilities.
31   *
32   * @author Jan-Kees van Andel
33   */
34  final class ComponentUtils {
35  
36      /**
37       * Find and return the parent form for the given component.
38       *
39       * @param component The component.
40       * @return The parent form, never <code>null</code>.
41       * @throws FacesException if no parent form is found.
42       */
43      static UIForm findParentForm(UIComponent component) {
44          while (component.getParent() != null) {
45              component = component.getParent();
46              if (component instanceof UIForm) {
47                  return (UIForm) component;
48              }
49          }
50          throw new FacesException("No parent form found");
51      }
52  
53      /**
54       * Find and return all UIInput instances in the given form.
55       * <p/>
56       * This method doesn't care about nesting. It looks through the entire form and returns all UIInput components.
57       *
58       * @param form The form to search in.
59       * @return A list with all UIInput components in the form, never <code>null</code>. If nothing is found,
60       *         an empty {java.util.List} is returned.
61       */
62      static List<UIInput> findInputsInForm(UIForm form) {
63          return findInputsInTree(form);
64      }
65  
66      /**
67       * Find all UIInput instances that are children of the given component tree.
68       *
69       * @param component The component tree to search in.
70       * @return A list with all UIInput components in the component, never <code>null</code>. If nothing is found,
71       *         an empty {java.util.List} is returned.
72       */
73      private static List<UIInput> findInputsInTree(final UIComponent component) {
74          final List<UIInput> ret = new ArrayList<UIInput>();
75  
76          if (component != null) {
77              final List<UIComponent> children = component.getChildren();
78              for (final UIComponent child : children) {
79                  if (child instanceof UIInput) {
80                      ret.add((UIInput) child);
81                  } else {
82                      ret.addAll(findInputsInTree(child));
83                  }
84              }
85          }
86  
87          return ret;
88      }
89  
90      /**
91       * Find the first UIMessages instance in the given component tree.
92       *
93       * @param component The component tree to search in.
94       * @return The first UIMessages instance, or <code>null</code> if nothing is found.
95       */
96      static UIMessages findMessagesInTree(final UIComponent component) {
97          final List<UIComponent> children = component.getChildren();
98          for (final UIComponent child : children) {
99              if (child instanceof UIMessages) {
100                 return (UIMessages) child;
101             } else {
102                 final UIMessages messages = findMessagesInTree(child);
103                 if (messages != null) {
104                     return messages;
105                 }
106             }
107         }
108         return null;
109     }
110 }