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