1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.context; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
import java.util.Iterator; |
23 | |
import java.util.Map; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 0 | public class PartialResponseWriter extends ResponseWriterWrapper |
29 | |
{ |
30 | |
public static final String RENDER_ALL_MARKER = "javax.faces.ViewRoot"; |
31 | |
public static final String VIEW_STATE_MARKER = "javax.faces.ViewState"; |
32 | |
|
33 | |
private ResponseWriter _wrapped; |
34 | |
private boolean hasChanges; |
35 | |
private String insertType; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public PartialResponseWriter(ResponseWriter writer) |
42 | 0 | { |
43 | 0 | _wrapped = writer; |
44 | 0 | } |
45 | |
|
46 | |
public void delete(String targetId) throws IOException |
47 | |
{ |
48 | 0 | startChanges(); |
49 | |
|
50 | 0 | _wrapped.startElement ("delete", null); |
51 | 0 | _wrapped.writeAttribute ("id", targetId, null); |
52 | 0 | _wrapped.endElement ("delete"); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
@Override |
59 | |
public void endDocument() throws IOException |
60 | |
{ |
61 | 0 | if (hasChanges) |
62 | |
{ |
63 | |
|
64 | |
|
65 | 0 | endInsert(); |
66 | |
|
67 | 0 | _wrapped.endElement ("changes"); |
68 | |
|
69 | 0 | hasChanges = false; |
70 | |
} |
71 | |
|
72 | 0 | _wrapped.endElement ("partial-response"); |
73 | 0 | } |
74 | |
|
75 | |
public void endError() throws IOException |
76 | |
{ |
77 | |
|
78 | |
|
79 | 0 | _wrapped.endCDATA(); |
80 | 0 | _wrapped.endElement ("error-message"); |
81 | 0 | _wrapped.endElement ("error"); |
82 | 0 | } |
83 | |
|
84 | |
public void endEval() throws IOException |
85 | |
{ |
86 | |
|
87 | |
|
88 | 0 | _wrapped.endCDATA(); |
89 | 0 | _wrapped.endElement ("eval"); |
90 | 0 | } |
91 | |
|
92 | |
public void endExtension() throws IOException |
93 | |
{ |
94 | 0 | _wrapped.endElement ("extension"); |
95 | 0 | } |
96 | |
|
97 | |
public void endInsert() throws IOException |
98 | |
{ |
99 | 0 | if (insertType == null) |
100 | |
{ |
101 | |
|
102 | |
|
103 | 0 | return; |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | 0 | _wrapped.endCDATA(); |
109 | 0 | _wrapped.endElement (insertType); |
110 | 0 | _wrapped.endElement ("insert"); |
111 | |
|
112 | 0 | insertType = null; |
113 | 0 | } |
114 | |
|
115 | |
public void endUpdate() throws IOException |
116 | |
{ |
117 | 0 | _wrapped.endCDATA(); |
118 | 0 | _wrapped.endElement ("update"); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
@Override |
125 | |
public ResponseWriter getWrapped() |
126 | |
{ |
127 | 0 | return _wrapped; |
128 | |
} |
129 | |
|
130 | |
public void redirect(String url) throws IOException |
131 | |
{ |
132 | 0 | _wrapped.startElement ("redirect", null); |
133 | 0 | _wrapped.writeAttribute ("url", url, null); |
134 | 0 | _wrapped.endElement ("redirect"); |
135 | 0 | } |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
@Override |
141 | |
public void startDocument() throws IOException |
142 | |
{ |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | 0 | _wrapped.startElement ("partial-response", null); |
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | 0 | hasChanges = false; |
154 | 0 | insertType = null; |
155 | 0 | } |
156 | |
|
157 | |
public void startError(String errorName) throws IOException |
158 | |
{ |
159 | 0 | _wrapped.startElement ("error", null); |
160 | |
|
161 | 0 | _wrapped.startElement ("error-name", null); |
162 | 0 | _wrapped.write (errorName); |
163 | 0 | _wrapped.endElement ("error-name"); |
164 | |
|
165 | 0 | _wrapped.startElement ("error-message", null); |
166 | 0 | startCDATA(); |
167 | |
|
168 | |
|
169 | 0 | } |
170 | |
|
171 | |
@Override |
172 | |
public void startCDATA() throws IOException |
173 | |
{ |
174 | 0 | _wrapped.startCDATA(); |
175 | 0 | } |
176 | |
|
177 | |
@Override |
178 | |
public void endCDATA() throws IOException |
179 | |
{ |
180 | 0 | _wrapped.endCDATA(); |
181 | 0 | } |
182 | |
|
183 | |
public void startEval() throws IOException |
184 | |
{ |
185 | 0 | startChanges(); |
186 | |
|
187 | 0 | _wrapped.startElement ("eval", null); |
188 | 0 | startCDATA(); |
189 | |
|
190 | |
|
191 | 0 | } |
192 | |
|
193 | |
public void startExtension(Map<String, String> attributes) throws IOException |
194 | |
{ |
195 | |
Iterator<String> attrNames; |
196 | |
|
197 | 0 | startChanges(); |
198 | |
|
199 | 0 | _wrapped.startElement ("extension", null); |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | 0 | attrNames = attributes.keySet().iterator(); |
205 | |
|
206 | 0 | while (attrNames.hasNext()) |
207 | |
{ |
208 | 0 | String attrName = attrNames.next(); |
209 | |
|
210 | 0 | _wrapped.writeAttribute (attrName, attributes.get (attrName), null); |
211 | 0 | } |
212 | |
|
213 | |
|
214 | 0 | } |
215 | |
|
216 | |
public void startInsertAfter(String targetId) throws IOException |
217 | |
{ |
218 | 0 | startInsertCommon ("after", targetId); |
219 | 0 | } |
220 | |
|
221 | |
public void startInsertBefore(String targetId) throws IOException |
222 | |
{ |
223 | 0 | startInsertCommon ("before", targetId); |
224 | 0 | } |
225 | |
|
226 | |
public void startUpdate(String targetId) throws IOException |
227 | |
{ |
228 | 0 | startChanges(); |
229 | |
|
230 | 0 | _wrapped.startElement ("update", null); |
231 | 0 | _wrapped.writeAttribute ("id", targetId, null); |
232 | 0 | startCDATA(); |
233 | |
|
234 | |
|
235 | 0 | } |
236 | |
|
237 | |
public void updateAttributes(String targetId, Map<String, String> attributes) throws IOException |
238 | |
{ |
239 | |
Iterator<String> attrNames; |
240 | |
|
241 | 0 | startChanges(); |
242 | |
|
243 | 0 | _wrapped.startElement ("attributes", null); |
244 | 0 | _wrapped.writeAttribute ("id", targetId, null); |
245 | |
|
246 | 0 | attrNames = attributes.keySet().iterator(); |
247 | |
|
248 | 0 | while (attrNames.hasNext()) |
249 | |
{ |
250 | 0 | String attrName = attrNames.next(); |
251 | |
|
252 | 0 | _wrapped.startElement ("attribute", null); |
253 | 0 | _wrapped.writeAttribute ("name", attrName, null); |
254 | 0 | _wrapped.writeAttribute ("value", attributes.get (attrName), null); |
255 | 0 | _wrapped.endElement ("attribute"); |
256 | 0 | } |
257 | |
|
258 | 0 | _wrapped.endElement ("attributes"); |
259 | 0 | } |
260 | |
|
261 | |
private void startChanges () throws IOException |
262 | |
{ |
263 | 0 | if (!hasChanges) |
264 | |
{ |
265 | 0 | _wrapped.startElement ("changes", null); |
266 | |
|
267 | 0 | hasChanges = true; |
268 | |
} |
269 | 0 | } |
270 | |
|
271 | |
private void startInsertCommon (String type, String targetId) throws IOException |
272 | |
{ |
273 | 0 | if (insertType != null) |
274 | |
{ |
275 | |
|
276 | |
|
277 | 0 | return; |
278 | |
} |
279 | |
|
280 | 0 | insertType = type; |
281 | |
|
282 | 0 | startChanges(); |
283 | |
|
284 | 0 | _wrapped.startElement ("insert", null); |
285 | 0 | _wrapped.startElement (insertType, null); |
286 | 0 | _wrapped.writeAttribute ("id", targetId, null); |
287 | 0 | startCDATA(); |
288 | |
|
289 | |
|
290 | 0 | } |
291 | |
} |