001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.faces.renderkit;
018:
019: import org.apache.cocoon.CascadingIOException;
020: import org.apache.cocoon.xml.XMLConsumer;
021:
022: import org.xml.sax.SAXException;
023: import org.xml.sax.helpers.AttributesImpl;
024:
025: import javax.faces.FacesException;
026: import javax.faces.component.UIComponent;
027: import javax.faces.context.ResponseWriter;
028: import java.io.IOException;
029: import java.io.Writer;
030:
031: /**
032: * JSF Response Writer writing SAX events into the XMLConsumer
033: *
034: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
035: * @version CVS $Id: XMLResponseWriter.java 433543 2006-08-22 06:22:54Z crossley $
036: */
037: public class XMLResponseWriter extends ResponseWriter {
038: private String contentType;
039: private String encoding;
040: private XMLConsumer xmlConsumer;
041:
042: private boolean closeStart;
043: private String name;
044: private AttributesImpl attrs;
045:
046: private char charHolder[];
047:
048: public XMLResponseWriter(XMLConsumer xmlConsumer,
049: String contentType, String encoding) throws FacesException {
050: this .contentType = contentType != null ? contentType
051: : "application/xml";
052: this .encoding = encoding;
053: this .xmlConsumer = xmlConsumer;
054:
055: this .attrs = new AttributesImpl();
056: this .charHolder = new char[1];
057: }
058:
059: public String getContentType() {
060: return contentType;
061: }
062:
063: public String getCharacterEncoding() {
064: return encoding;
065: }
066:
067: public void startDocument() throws IOException {
068: }
069:
070: public void endDocument() throws IOException {
071: closeStartIfNecessary();
072: }
073:
074: public void flush() throws IOException {
075: closeStartIfNecessary();
076: }
077:
078: public void startElement(String name, UIComponent component)
079: throws IOException {
080: closeStartIfNecessary();
081: this .name = name;
082: this .closeStart = true;
083: }
084:
085: public void endElement(String name) throws IOException {
086: closeStartIfNecessary();
087: try {
088: this .xmlConsumer.endElement("", name, name);
089: } catch (SAXException e) {
090: throw new CascadingIOException("SAXException", e);
091: }
092: }
093:
094: public void writeAttribute(String name, Object value,
095: String componentPropertyName) throws IOException {
096: if (value == null) {
097: this .attrs.addAttribute("", name, name, "CDATA", "");
098: } else if (Boolean.TRUE.equals(value)) {
099: this .attrs.addAttribute("", name, name, "CDATA", name);
100: } else {
101: this .attrs.addAttribute("", name, name, "CDATA", value
102: .toString());
103: }
104: }
105:
106: public void writeURIAttribute(String name, Object value,
107: String componentPropertyName) throws IOException {
108: this .attrs.addAttribute("", name, name, "CDATA", value
109: .toString());
110: }
111:
112: public void writeComment(Object comment) throws IOException {
113: closeStartIfNecessary();
114: char[] ch = comment.toString().toCharArray();
115: try {
116: this .xmlConsumer.comment(ch, 0, ch.length);
117: } catch (SAXException e) {
118: throw new CascadingIOException("SAXException", e);
119: }
120: }
121:
122: public void writeText(Object text, String componentPropertyName)
123: throws IOException {
124: closeStartIfNecessary();
125: char[] ch = text.toString().toCharArray();
126: try {
127: this .xmlConsumer.characters(ch, 0, ch.length);
128: } catch (SAXException e) {
129: throw new CascadingIOException("SAXException", e);
130: }
131: }
132:
133: public void writeText(char text) throws IOException {
134: closeStartIfNecessary();
135: charHolder[0] = text;
136: try {
137: this .xmlConsumer.characters(charHolder, 0, 1);
138: } catch (SAXException e) {
139: throw new CascadingIOException("SAXException", e);
140: }
141: }
142:
143: public void writeText(char text[]) throws IOException {
144: closeStartIfNecessary();
145: try {
146: this .xmlConsumer.characters(text, 0, text.length);
147: } catch (SAXException e) {
148: throw new CascadingIOException("SAXException", e);
149: }
150: }
151:
152: public void writeText(char text[], int off, int len)
153: throws IOException {
154: closeStartIfNecessary();
155: try {
156: this .xmlConsumer.characters(text, off, len);
157: } catch (SAXException e) {
158: throw new CascadingIOException("SAXException", e);
159: }
160: }
161:
162: public ResponseWriter cloneWithWriter(Writer writer) {
163: if (!(writer instanceof XMLResponseWriter)) {
164: throw new IllegalArgumentException(
165: "Expected XMLResponseWriter got " + writer);
166: }
167: return new XMLResponseWriter(
168: ((XMLResponseWriter) writer).xmlConsumer,
169: getContentType(), getCharacterEncoding());
170: }
171:
172: private void closeStartIfNecessary() throws IOException {
173: if (closeStart) {
174: try {
175: this .xmlConsumer.startElement("", this .name, this .name,
176: this .attrs);
177: } catch (SAXException e) {
178: throw new CascadingIOException("SAXException", e);
179: }
180: this .attrs.clear();
181: closeStart = false;
182: }
183: }
184:
185: public void close() throws IOException {
186: closeStartIfNecessary();
187: }
188:
189: public void write(char cbuf) throws IOException {
190: closeStartIfNecessary();
191: writeText(cbuf);
192: }
193:
194: public void write(char cbuf[], int off, int len) throws IOException {
195: closeStartIfNecessary();
196: writeText(cbuf);
197: }
198:
199: public void write(int c) throws IOException {
200: closeStartIfNecessary();
201: writeText((char) c);
202: }
203:
204: public void write(String str) throws IOException {
205: closeStartIfNecessary();
206: writeText(str.toCharArray());
207: }
208:
209: public void write(String str, int off, int len) throws IOException {
210: closeStartIfNecessary();
211: writeText(str.toCharArray(), off, len);
212: }
213: }
|