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
24 import javax.faces.component.UIComponent;
25 import javax.faces.context.FacesContext;
26 import javax.faces.event.ExceptionQueuedEvent;
27 import javax.faces.event.ExceptionQueuedEventContext;
28 import javax.faces.view.Location;
29
30 import org.apache.myfaces.shared.renderkit.RendererUtils;
31
32
33
34
35
36 public final class CheckDuplicateIdFaceletUtils
37 {
38
39 public static void checkIdsStatefulComponents (FacesContext context, UIComponent view)
40 {
41 checkIdsStatefulComponents (context, view, new HashSet<String>());
42 }
43
44 private static void checkIdsStatefulComponents (FacesContext context,
45 UIComponent component, Set<String> existingIds)
46 {
47 String id;
48
49 if (component == null)
50 {
51 return;
52 }
53
54
55
56 id = component.getClientId (context);
57
58 if (!existingIds.add (id))
59 {
60 DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
61 throw duplicateIdException;
62 }
63
64 int facetCount = component.getFacetCount();
65 if (facetCount > 0)
66 {
67 for (UIComponent facet : component.getFacets().values())
68 {
69 if (!(facet instanceof UILeaf))
70 {
71 checkIdsStatefulComponents (context, facet, existingIds);
72 }
73 }
74 }
75 for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
76 {
77 UIComponent child = component.getChildren().get(i);
78 if (!(child instanceof UILeaf))
79 {
80 checkIdsStatefulComponents (context, child, existingIds);
81 }
82 }
83 }
84
85 public static void checkIds (FacesContext context, UIComponent view)
86 {
87 checkIds (context, view, new HashSet<String>());
88 }
89
90 private static void checkIds (FacesContext context, UIComponent component, Set<String> existingIds)
91 {
92 String id;
93
94 if (component == null)
95 {
96 return;
97 }
98
99
100
101 id = component.getClientId (context);
102
103 if (!existingIds.add (id))
104 {
105 DuplicateIdException duplicateIdException = createAndQueueException(context, component, id);
106 throw duplicateIdException;
107 }
108
109 int facetCount = component.getFacetCount();
110 if (facetCount > 0)
111 {
112 for (UIComponent facet : component.getFacets().values())
113 {
114 checkIds (context, facet, existingIds);
115 }
116 }
117 for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
118 {
119 UIComponent child = component.getChildren().get(i);
120 checkIds (context, child, existingIds);
121 }
122 }
123
124 private static DuplicateIdException createAndQueueException(FacesContext context, UIComponent component, String id)
125 {
126 String message = "Component with duplicate id \"" + id + "\" found. The first component is ";
127
128
129
130
131
132
133
134 UIComponent firstComponent = context.getViewRoot().findComponent(id);
135 Location location = (Location) firstComponent.getAttributes().get(UIComponent.VIEW_LOCATION_KEY);
136 if (location != null)
137 {
138 message += location.toString();
139 }
140 else
141 {
142
143
144 message += RendererUtils.getPathToComponent(firstComponent);
145 }
146
147
148 DuplicateIdException duplicateIdException = new DuplicateIdException
149 (message, firstComponent, component);
150
151 ExceptionQueuedEventContext exceptionContext
152 = new ExceptionQueuedEventContext(context, duplicateIdException,
153 component, context.getCurrentPhaseId());
154
155
156 context.getApplication().publishEvent(context, ExceptionQueuedEvent.class, exceptionContext);
157 return duplicateIdException;
158 }
159 }