001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.html;
030:
031: import java.io.*;
032:
033: import javax.faces.component.*;
034: import javax.faces.context.*;
035: import javax.faces.render.*;
036:
037: public class HtmlResponseWriter extends ResponseWriter {
038: private String _contentType;
039: private String _encoding;
040:
041: private Writer _out;
042: private boolean _inElement;
043:
044: HtmlResponseWriter(Writer out, String contentType, String encoding) {
045: _out = out;
046: _contentType = contentType;
047: _encoding = encoding;
048: }
049:
050: public void write(char[] buffer, int offset, int length)
051: throws IOException {
052: if (_inElement)
053: closeElement();
054:
055: _out.write(buffer, offset, length);
056: }
057:
058: public void write(char ch) throws IOException {
059: if (_inElement)
060: closeElement();
061:
062: _out.write(ch);
063: }
064:
065: public void write(String v) throws IOException {
066: if (_inElement)
067: closeElement();
068:
069: _out.write(v);
070: }
071:
072: public String getContentType() {
073: return _contentType;
074: }
075:
076: public String getCharacterEncoding() {
077: return _encoding;
078: }
079:
080: public void flush() throws IOException {
081: _out.flush();
082: }
083:
084: public void startDocument() throws IOException {
085: }
086:
087: public void endDocument() throws IOException {
088: }
089:
090: public void startElement(String name, UIComponent component)
091: throws IOException {
092: if (_inElement)
093: closeElement();
094:
095: _out.write("<");
096: _out.write(name);
097: _inElement = true;
098: }
099:
100: public void endElement(String name) throws IOException {
101: if (_inElement)
102: closeElement();
103:
104: _out.write("</");
105: _out.write(name);
106: _out.write(">");
107: }
108:
109: public void writeAttribute(String name, Object value,
110: String property) throws IOException {
111: /*
112: if (name.equals(value)) {
113: _out.write(' ');
114: _out.write(name);
115: }
116: else {
117: */
118: _out.write(' ');
119: _out.write(name);
120: _out.write("=\"");
121: _out.write(String.valueOf(value));
122: _out.write("\"");
123: //}
124: }
125:
126: public void writeURIAttribute(String name, Object value,
127: String property) throws IOException {
128: writeAttribute(name, value, property);
129: }
130:
131: public void writeComment(Object comment) throws IOException {
132: if (_inElement)
133: closeElement();
134:
135: _out.write("<!--");
136: _out.write(String.valueOf(comment));
137: _out.write("-->");
138: }
139:
140: public void writeText(Object text, String property)
141: throws IOException {
142: if (_inElement)
143: closeElement();
144:
145: _out.write(String.valueOf(text));
146: }
147:
148: public void writeText(char[] text, int offset, int length)
149: throws IOException {
150: if (_inElement)
151: closeElement();
152:
153: _out.write(text, offset, length);
154: }
155:
156: public ResponseWriter cloneWithWriter(Writer out) {
157: return new HtmlResponseWriter(out, _contentType, _encoding);
158: }
159:
160: private void closeElement() throws IOException {
161: _out.write(">");
162: _inElement = false;
163: }
164:
165: public void close() throws IOException {
166: }
167:
168: public String toString() {
169: return "HtmlResponseWriter[]";
170: }
171: }
|