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