View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.view;
20  
21  import java.io.IOException;
22  import java.io.Writer;
23  
24  /**
25   * Delegates the standard Writer-methods (append, close, flush, write)
26   * to the given Writer, if the ResponseSwitch is enabled
27   * 
28   * @author Werner Punz (latest modification by $Author: lu4242 $)
29   * @author Jakob Korherr 
30   * @version $Revision: 1151677 $ $Date: 2011-07-27 19:03:59 -0500 (Wed, 27 Jul 2011) $
31   */
32  class SwitchableWriter extends Writer
33  {
34  
35      Writer _delegate = null;
36      ResponseSwitch _responseSwitch = null;
37  
38      public SwitchableWriter(Writer delegate, ResponseSwitch responseSwitch)
39      {
40          _delegate = delegate;
41          _responseSwitch = responseSwitch;
42      }
43  
44      public void write(String s, int start, int end) throws IOException
45      {
46          if (_responseSwitch.isEnabled())
47          {
48              _delegate.write(s, start, end);
49          }
50      }
51  
52      public void write(String s) throws IOException
53      {
54          if (_responseSwitch.isEnabled())
55          {
56              _delegate.write(s);
57          }
58      }
59  
60      public void write(char[] c, int start, int end) throws IOException
61      {
62          if (_responseSwitch.isEnabled())
63          {
64              _delegate.write(c, start, end);
65          }
66      }
67  
68      public void write(char[] c) throws IOException
69      {
70          if (_responseSwitch.isEnabled())
71          {
72              _delegate.write(c);
73  
74          }
75      }
76  
77      public void write(int i) throws IOException
78      {
79          if (_responseSwitch.isEnabled())
80          {
81              _delegate.write(i);
82          }
83      }
84  
85      public void flush() throws IOException
86      {
87          if (_responseSwitch.isEnabled())
88          {
89              _delegate.flush();
90          }
91      }
92  
93      public void close() throws IOException
94      {
95          if (_responseSwitch.isEnabled())
96          {
97              _delegate.close();
98          }
99      }
100 
101     public Writer append(char c) throws IOException
102     {
103         if (_responseSwitch.isEnabled())
104         {
105             return _delegate.append(c);
106         }
107         return this;
108     }
109 
110     public Writer append(CharSequence csq, int start, int end)
111             throws IOException
112     {
113         if (_responseSwitch.isEnabled())
114         {
115             return _delegate.append(csq, start, end);
116         }
117         return this;
118     }
119 
120     public Writer append(CharSequence csq) throws IOException
121     {
122         if (_responseSwitch.isEnabled())
123         {
124             return _delegate.append(csq);
125         }
126         return this;
127     }
128 }