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  class SwitchableWriter extends Writer
29  {
30  
31      Writer _delegate = null;
32      ResponseSwitch _responseSwitch = null;
33  
34      public SwitchableWriter(Writer delegate, ResponseSwitch responseSwitch)
35      {
36          _delegate = delegate;
37          _responseSwitch = responseSwitch;
38      }
39  
40      public void write(String s, int start, int end) throws IOException
41      {
42          if (_responseSwitch.isEnabled())
43          {
44              _delegate.write(s, start, end);
45          }
46      }
47  
48      public void write(String s) throws IOException
49      {
50          if (_responseSwitch.isEnabled())
51          {
52              _delegate.write(s);
53          }
54      }
55  
56      public void write(char[] c, int start, int end) throws IOException
57      {
58          if (_responseSwitch.isEnabled())
59          {
60              _delegate.write(c, start, end);
61          }
62      }
63  
64      public void write(char[] c) throws IOException
65      {
66          if (_responseSwitch.isEnabled())
67          {
68              _delegate.write(c);
69  
70          }
71      }
72  
73      public void write(int i) throws IOException
74      {
75          if (_responseSwitch.isEnabled())
76          {
77              _delegate.write(i);
78          }
79      }
80  
81      public void flush() throws IOException
82      {
83          if (_responseSwitch.isEnabled())
84          {
85              _delegate.flush();
86          }
87      }
88  
89      public void close() throws IOException
90      {
91          if (_responseSwitch.isEnabled())
92          {
93              _delegate.close();
94          }
95      }
96  
97      public Writer append(char c) throws IOException
98      {
99          if (_responseSwitch.isEnabled())
100         {
101             return _delegate.append(c);
102         }
103         return this;
104     }
105 
106     public Writer append(CharSequence csq, int start, int end)
107             throws IOException
108     {
109         if (_responseSwitch.isEnabled())
110         {
111             return _delegate.append(csq, start, end);
112         }
113         return this;
114     }
115 
116     public Writer append(CharSequence csq) throws IOException
117     {
118         if (_responseSwitch.isEnabled())
119         {
120             return _delegate.append(csq);
121         }
122         return this;
123     }
124 }