001: /**
002: * Licensed under the Common Development and Distribution License,
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at
005: *
006: * http://www.sun.com/cddl/
007: *
008: * Unless required by applicable law or agreed to in writing, software
009: * distributed under the License is distributed on an "AS IS" BASIS,
010: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
011: * implied. See the License for the specific language governing
012: * permissions and limitations under the License.
013: */package com.sun.facelets.mock;
014:
015: import java.io.IOException;
016: import java.io.Writer;
017:
018: import javax.faces.component.UIComponent;
019: import javax.faces.context.ResponseWriter;
020:
021: /**
022: * @author Jacob Hookom
023: *
024: */
025: public class MockResponseWriter extends ResponseWriter {
026:
027: private final Writer writer;
028: private boolean startOpen;
029:
030: public MockResponseWriter(Writer writer) {
031: this .writer = writer;
032: }
033:
034: /* (non-Javadoc)
035: * @see javax.faces.context.ResponseWriter#getContentType()
036: */
037: public String getContentType() {
038: return "text/html";
039: }
040:
041: /* (non-Javadoc)
042: * @see javax.faces.context.ResponseWriter#getCharacterEncoding()
043: */
044: public String getCharacterEncoding() {
045: return "UTF-8";
046: }
047:
048: /* (non-Javadoc)
049: * @see javax.faces.context.ResponseWriter#flush()
050: */
051: public void flush() throws IOException {
052: // TODO Auto-generated method stub
053:
054: }
055:
056: /* (non-Javadoc)
057: * @see javax.faces.context.ResponseWriter#startDocument()
058: */
059: public void startDocument() throws IOException {
060: // TODO Auto-generated method stub
061:
062: }
063:
064: /* (non-Javadoc)
065: * @see javax.faces.context.ResponseWriter#endDocument()
066: */
067: public void endDocument() throws IOException {
068: // TODO Auto-generated method stub
069:
070: }
071:
072: private void closeStart() throws IOException {
073: if (this .startOpen) {
074: this .writer.write('>');
075: }
076: this .startOpen = false;
077: }
078:
079: /* (non-Javadoc)
080: * @see javax.faces.context.ResponseWriter#startElement(java.lang.String, javax.faces.component.UIComponent)
081: */
082: public void startElement(String arg0, UIComponent arg1)
083: throws IOException {
084: this .closeStart();
085: this .writer.write('<');
086: this .writer.write(arg0);
087: this .startOpen = true;
088: }
089:
090: /* (non-Javadoc)
091: * @see javax.faces.context.ResponseWriter#endElement(java.lang.String)
092: */
093: public void endElement(String arg0) throws IOException {
094: if (this .startOpen) {
095: this .writer.write("/>");
096: this .startOpen = false;
097: } else {
098: this .writer.write("</");
099: this .writer.write(arg0);
100: this .writer.write('>');
101: }
102:
103: }
104:
105: /* (non-Javadoc)
106: * @see javax.faces.context.ResponseWriter#writeAttribute(java.lang.String, java.lang.Object, java.lang.String)
107: */
108: public void writeAttribute(String arg0, Object arg1, String arg2)
109: throws IOException {
110: if (arg1 != null) {
111: writer.write(' ');
112: writer.write(arg0);
113: writer.write("=\"");
114: writer.write(arg1.toString());
115: writer.write('"');
116: }
117: }
118:
119: /* (non-Javadoc)
120: * @see javax.faces.context.ResponseWriter#writeURIAttribute(java.lang.String, java.lang.Object, java.lang.String)
121: */
122: public void writeURIAttribute(String arg0, Object arg1, String arg2)
123: throws IOException {
124: this .writeAttribute(arg0, arg1, arg2);
125: }
126:
127: /* (non-Javadoc)
128: * @see javax.faces.context.ResponseWriter#writeComment(java.lang.Object)
129: */
130: public void writeComment(Object arg0) throws IOException {
131: this .closeStart();
132: this .writer.write("<!-- ");
133: this .writer.write(arg0 != null ? arg0.toString() : "null");
134: this .writer.write(" -->");
135: }
136:
137: /* (non-Javadoc)
138: * @see javax.faces.context.ResponseWriter#writeText(java.lang.Object, java.lang.String)
139: */
140: public void writeText(Object arg0, String arg1) throws IOException {
141: this .closeStart();
142: this .writer.write(arg0 != null ? arg0.toString() : "null");
143: }
144:
145: /* (non-Javadoc)
146: * @see javax.faces.context.ResponseWriter#writeText(char[], int, int)
147: */
148: public void writeText(char[] arg0, int arg1, int arg2)
149: throws IOException {
150: this .closeStart();
151: this .writer.write(arg0, arg1, arg2);
152: }
153:
154: /* (non-Javadoc)
155: * @see javax.faces.context.ResponseWriter#cloneWithWriter(java.io.Writer)
156: */
157: public ResponseWriter cloneWithWriter(Writer arg0) {
158: return new MockResponseWriter(arg0);
159: }
160:
161: /* (non-Javadoc)
162: * @see java.io.Writer#write(char[], int, int)
163: */
164: public void write(char[] cbuf, int off, int len) throws IOException {
165: this .writer.write(cbuf, off, len);
166: }
167:
168: /* (non-Javadoc)
169: * @see java.io.Writer#close()
170: */
171: public void close() throws IOException {
172: // TODO Auto-generated method stub
173:
174: }
175:
176: }
|