1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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 }