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.compiler;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  
26  /**
27   *
28   * @author Leonardo Uribe
29   */
30  public final class CheckDuplicateIdFaceletUtils
31  {
32      
33      public static void checkIdsStatefulComponents (FacesContext context, UIComponent view)
34      {
35          checkIdsStatefulComponents (context, view, new HashSet<String>());
36      }
37  
38      private static void checkIdsStatefulComponents (FacesContext context, 
39              UIComponent component, Set<String> existingIds)
40      {
41          String id;
42          
43          if (component == null)
44          {
45              return;
46          }
47          
48          // Need to use this form of the client ID method so we generate the client-side ID.
49          
50          id = component.getClientId (context);
51          
52          if (existingIds.contains (id))
53          {
54              throw new IllegalStateException ("component with duplicate id \"" + id + "\" found");
55          }
56          
57          existingIds.add (id);
58          
59          int facetCount = component.getFacetCount();
60          if (facetCount > 0)
61          {
62              for (UIComponent facet : component.getFacets().values())
63              {
64                  if (!(facet instanceof UILeaf))
65                  {
66                      checkIdsStatefulComponents (context, facet, existingIds);
67                  }
68              }
69          }
70          for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
71          {
72              UIComponent child = component.getChildren().get(i);
73              if (!(child instanceof UILeaf))
74              {
75                  checkIdsStatefulComponents (context, child, existingIds);
76              }
77          }
78      }
79  
80      public static void checkIds (FacesContext context, UIComponent view)
81      {
82          checkIds (context, view, new HashSet<String>());
83      }
84      
85      private static void checkIds (FacesContext context, UIComponent component, Set<String> existingIds)
86      {
87          String id;
88          
89          if (component == null)
90          {
91              return;
92          }
93          
94          // Need to use this form of the client ID method so we generate the client-side ID.
95          
96          id = component.getClientId (context);
97          
98          if (existingIds.contains (id))
99          {
100             throw new IllegalStateException ("component with duplicate id \"" + id + "\" found");
101         }
102         
103         existingIds.add (id);
104         
105         int facetCount = component.getFacetCount();
106         if (facetCount > 0)
107         {
108             for (UIComponent facet : component.getFacets().values())
109             {
110                 checkIds (context, facet, existingIds);
111             }
112         }
113         for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
114         {
115             UIComponent child = component.getChildren().get(i);
116             checkIds (context, child, existingIds);
117         }
118     }
119 }