This project has retired. For details please refer to its Attic page.
JspPrintWriterAdapter xref
View Javadoc

1   /*
2    * $Id: JspPrintWriterAdapter.java 1306435 2012-03-28 15:39:11Z nlebas $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  package org.apache.tiles.request.jsp;
22  
23  import java.io.IOException;
24  import java.io.PrintWriter;
25  
26  import javax.servlet.jsp.JspWriter;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Adapts a {@link JspWriter} to a {@link PrintWriter}, swallowing {@link IOException}.
33   *
34   * @version $Rev: 1306435 $ $Date: 2012-03-29 02:39:11 +1100 (Thu, 29 Mar 2012) $
35   */
36  public class JspPrintWriterAdapter extends PrintWriter {
37  
38      /**
39       * The JSP writer.
40       */
41      private JspWriter writer;
42  
43      /**
44       * The logging object.
45       */
46      private final Logger log = LoggerFactory.getLogger(this.getClass());
47  
48      /**
49       * Constructor.
50       *
51       * @param writer The JSP writer.
52       */
53      public JspPrintWriterAdapter(JspWriter writer) {
54          super(writer);
55          this.writer = writer;
56      }
57  
58      /**
59       * Returns the original JSP writer.
60       *
61       * @return The JSP writer.
62       */
63      public JspWriter getJspWriter() {
64          return writer;
65      }
66  
67      /** {@inheritDoc} */
68      @Override
69      public PrintWriter append(char c) {
70          try {
71              writer.append(c);
72          } catch (IOException e) {
73              log.error("Error when writing in JspWriter", e);
74              setError();
75          }
76          return this;
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public PrintWriter append(CharSequence csq, int start, int end) {
82          try {
83              writer.append(csq, start, end);
84          } catch (IOException e) {
85              log.error("Error when writing in JspWriter", e);
86              setError();
87          }
88          return this;
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public PrintWriter append(CharSequence csq) {
94          try {
95              writer.append(csq);
96          } catch (IOException e) {
97              log.error("Error when writing in JspWriter", e);
98              setError();
99          }
100         return this;
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public void close() {
106         try {
107             writer.close();
108         } catch (IOException e) {
109             log.error("Error when writing in JspWriter", e);
110             setError();
111         }
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public void flush() {
117         try {
118             writer.flush();
119         } catch (IOException e) {
120             log.error("Error when writing in JspWriter", e);
121             setError();
122         }
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public void print(boolean b) {
128         try {
129             writer.print(b);
130         } catch (IOException e) {
131             log.error("Error when writing in JspWriter", e);
132             setError();
133         }
134     }
135 
136     /** {@inheritDoc} */
137     @Override
138     public void print(char c) {
139         try {
140             writer.print(c);
141         } catch (IOException e) {
142             log.error("Error when writing in JspWriter", e);
143             setError();
144         }
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public void print(char[] s) {
150         try {
151             writer.print(s);
152         } catch (IOException e) {
153             log.error("Error when writing in JspWriter", e);
154             setError();
155         }
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public void print(double d) {
161         try {
162             writer.print(d);
163         } catch (IOException e) {
164             log.error("Error when writing in JspWriter", e);
165             setError();
166         }
167     }
168 
169     /** {@inheritDoc} */
170     @Override
171     public void print(float f) {
172         try {
173             writer.print(f);
174         } catch (IOException e) {
175             log.error("Error when writing in JspWriter", e);
176             setError();
177         }
178     }
179 
180     /** {@inheritDoc} */
181     @Override
182     public void print(int i) {
183         try {
184             writer.print(i);
185         } catch (IOException e) {
186             log.error("Error when writing in JspWriter", e);
187             setError();
188         }
189     }
190 
191     /** {@inheritDoc} */
192     @Override
193     public void print(long l) {
194         try {
195             writer.print(l);
196         } catch (IOException e) {
197             log.error("Error when writing in JspWriter", e);
198             setError();
199         }
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     public void print(Object obj) {
205         try {
206             writer.print(obj);
207         } catch (IOException e) {
208             log.error("Error when writing in JspWriter", e);
209             setError();
210         }
211     }
212 
213     /** {@inheritDoc} */
214     @Override
215     public void print(String s) {
216         try {
217             writer.print(s);
218         } catch (IOException e) {
219             log.error("Error when writing in JspWriter", e);
220             setError();
221         }
222     }
223 
224     /** {@inheritDoc} */
225     @Override
226     public void println() {
227         try {
228             writer.println();
229         } catch (IOException e) {
230             log.error("Error when writing in JspWriter", e);
231             setError();
232         }
233     }
234 
235     /** {@inheritDoc} */
236     @Override
237     public void println(boolean x) {
238         try {
239             writer.println(x);
240         } catch (IOException e) {
241             log.error("Error when writing in JspWriter", e);
242             setError();
243         }
244     }
245 
246     /** {@inheritDoc} */
247     @Override
248     public void println(char x) {
249         try {
250             writer.println(x);
251         } catch (IOException e) {
252             log.error("Error when writing in JspWriter", e);
253             setError();
254         }
255     }
256 
257     /** {@inheritDoc} */
258     @Override
259     public void println(char[] x) {
260         try {
261             writer.println(x);
262         } catch (IOException e) {
263             log.error("Error when writing in JspWriter", e);
264             setError();
265         }
266     }
267 
268     /** {@inheritDoc} */
269     @Override
270     public void println(double x) {
271         try {
272             writer.println(x);
273         } catch (IOException e) {
274             log.error("Error when writing in JspWriter", e);
275             setError();
276         }
277     }
278 
279     /** {@inheritDoc} */
280     @Override
281     public void println(float x) {
282         try {
283             writer.println(x);
284         } catch (IOException e) {
285             log.error("Error when writing in JspWriter", e);
286             setError();
287         }
288     }
289 
290     /** {@inheritDoc} */
291     @Override
292     public void println(int x) {
293         try {
294             writer.println(x);
295         } catch (IOException e) {
296             log.error("Error when writing in JspWriter", e);
297             setError();
298         }
299     }
300 
301     /** {@inheritDoc} */
302     @Override
303     public void println(long x) {
304         try {
305             writer.println(x);
306         } catch (IOException e) {
307             log.error("Error when writing in JspWriter", e);
308             setError();
309         }
310     }
311 
312     /** {@inheritDoc} */
313     @Override
314     public void println(Object x) {
315         try {
316             writer.println(x);
317         } catch (IOException e) {
318             log.error("Error when writing in JspWriter", e);
319             setError();
320         }
321     }
322 
323     /** {@inheritDoc} */
324     @Override
325     public void println(String x) {
326         try {
327             writer.println(x);
328         } catch (IOException e) {
329             log.error("Error when writing in JspWriter", e);
330             setError();
331         }
332     }
333 
334     /** {@inheritDoc} */
335     @Override
336     public void write(char[] buf, int off, int len) {
337         try {
338             writer.write(buf, off, len);
339         } catch (IOException e) {
340             log.error("Error when writing in JspWriter", e);
341             setError();
342         }
343     }
344 
345     /** {@inheritDoc} */
346     @Override
347     public void write(char[] buf) {
348         try {
349             writer.write(buf);
350         } catch (IOException e) {
351             log.error("Error when writing in JspWriter", e);
352             setError();
353         }
354     }
355 
356     /** {@inheritDoc} */
357     @Override
358     public void write(int c) {
359         try {
360             writer.write(c);
361         } catch (IOException e) {
362             log.error("Error when writing in JspWriter", e);
363             setError();
364         }
365     }
366 
367     /** {@inheritDoc} */
368     @Override
369     public void write(String s, int off, int len) {
370         try {
371             writer.write(s, off, len);
372         } catch (IOException e) {
373             log.error("Error when writing in JspWriter", e);
374             setError();
375         }
376     }
377 
378     /** {@inheritDoc} */
379     @Override
380     public void write(String s) {
381         try {
382             writer.write(s);
383         } catch (IOException e) {
384             log.error("Error when writing in JspWriter", e);
385             setError();
386         }
387     }
388 }