KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > facelets > mock > MockResponseWriter


1 /**
2  * Licensed under the Common Development and Distribution License,
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.sun.com/cddl/
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */

14 package com.sun.facelets.mock;
15
16
17 import java.io.IOException JavaDoc;
18 import java.io.Writer JavaDoc;
19
20 import javax.faces.component.UIComponent;
21 import javax.faces.context.ResponseWriter;
22
23
24 /**
25  * @author Jacob Hookom
26  *
27  */

28 public class MockResponseWriter extends ResponseWriter {
29
30     private final Writer JavaDoc writer;
31     private boolean startOpen;
32     
33     public MockResponseWriter(Writer JavaDoc writer) {
34         this.writer = writer;
35     }
36     
37     /* (non-Javadoc)
38      * @see javax.faces.context.ResponseWriter#getContentType()
39      */

40     public String JavaDoc getContentType() {
41         return "text/html";
42     }
43
44     /* (non-Javadoc)
45      * @see javax.faces.context.ResponseWriter#getCharacterEncoding()
46      */

47     public String JavaDoc getCharacterEncoding() {
48         return "UTF-8";
49     }
50
51     /* (non-Javadoc)
52      * @see javax.faces.context.ResponseWriter#flush()
53      */

54     public void flush() throws IOException JavaDoc {
55         // TODO Auto-generated method stub
56

57     }
58
59     /* (non-Javadoc)
60      * @see javax.faces.context.ResponseWriter#startDocument()
61      */

62     public void startDocument() throws IOException JavaDoc {
63         // TODO Auto-generated method stub
64

65     }
66
67     /* (non-Javadoc)
68      * @see javax.faces.context.ResponseWriter#endDocument()
69      */

70     public void endDocument() throws IOException JavaDoc {
71         // TODO Auto-generated method stub
72

73     }
74
75     private void closeStart() throws IOException JavaDoc {
76         if (this.startOpen) {
77             this.writer.write('>');
78         }
79         this.startOpen = false;
80     }
81     
82     /* (non-Javadoc)
83      * @see javax.faces.context.ResponseWriter#startElement(java.lang.String, javax.faces.component.UIComponent)
84      */

85     public void startElement(String JavaDoc arg0, UIComponent arg1) throws IOException JavaDoc {
86         this.closeStart();
87         this.writer.write('<');
88         this.writer.write(arg0);
89         this.startOpen = true;
90     }
91
92     /* (non-Javadoc)
93      * @see javax.faces.context.ResponseWriter#endElement(java.lang.String)
94      */

95     public void endElement(String JavaDoc arg0) throws IOException JavaDoc {
96         if (this.startOpen) {
97             this.writer.write("/>");
98             this.startOpen = false;
99         } else {
100             this.writer.write("</");
101             this.writer.write(arg0);
102             this.writer.write('>');
103         }
104
105     }
106
107     /* (non-Javadoc)
108      * @see javax.faces.context.ResponseWriter#writeAttribute(java.lang.String, java.lang.Object, java.lang.String)
109      */

110     public void writeAttribute(String JavaDoc arg0, Object JavaDoc arg1, String JavaDoc arg2)
111             throws IOException JavaDoc {
112         if (arg1 != null) {
113             writer.write(' ');
114             writer.write(arg0);
115             writer.write("=\"");
116             writer.write(arg1.toString());
117             writer.write('"');
118         }
119     }
120
121     /* (non-Javadoc)
122      * @see javax.faces.context.ResponseWriter#writeURIAttribute(java.lang.String, java.lang.Object, java.lang.String)
123      */

124     public void writeURIAttribute(String JavaDoc arg0, Object JavaDoc arg1, String JavaDoc arg2)
125             throws IOException JavaDoc {
126         this.writeAttribute(arg0, arg1, arg2);
127     }
128
129     /* (non-Javadoc)
130      * @see javax.faces.context.ResponseWriter#writeComment(java.lang.Object)
131      */

132     public void writeComment(Object JavaDoc arg0) throws IOException JavaDoc {
133         this.closeStart();
134         this.writer.write("<!-- ");
135         this.writer.write(arg0 != null ? arg0.toString() : "null");
136         this.writer.write(" -->");
137     }
138
139     /* (non-Javadoc)
140      * @see javax.faces.context.ResponseWriter#writeText(java.lang.Object, java.lang.String)
141      */

142     public void writeText(Object JavaDoc arg0, String JavaDoc arg1) throws IOException JavaDoc {
143         this.closeStart();
144         this.writer.write(arg0 != null ? arg0.toString() : "null");
145     }
146
147     /* (non-Javadoc)
148      * @see javax.faces.context.ResponseWriter#writeText(char[], int, int)
149      */

150     public void writeText(char[] arg0, int arg1, int arg2) throws IOException JavaDoc {
151         this.closeStart();
152         this.writer.write(arg0, arg1, arg2);
153     }
154
155     /* (non-Javadoc)
156      * @see javax.faces.context.ResponseWriter#cloneWithWriter(java.io.Writer)
157      */

158     public ResponseWriter cloneWithWriter(Writer JavaDoc arg0) {
159         return new MockResponseWriter(arg0);
160     }
161
162     /* (non-Javadoc)
163      * @see java.io.Writer#write(char[], int, int)
164      */

165     public void write(char[] cbuf, int off, int len) throws IOException JavaDoc {
166         this.writer.write(cbuf, off, len);
167     }
168
169     /* (non-Javadoc)
170      * @see java.io.Writer#close()
171      */

172     public void close() throws IOException JavaDoc {
173         // TODO Auto-generated method stub
174

175     }
176
177 }
178
Popular Tags