KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > faces > renderkit > XMLResponseWriter


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.faces.renderkit;
17
18 import org.apache.cocoon.CascadingIOException;
19 import org.apache.cocoon.xml.XMLConsumer;
20
21 import org.xml.sax.SAXException JavaDoc;
22 import org.xml.sax.helpers.AttributesImpl JavaDoc;
23
24 import javax.faces.FacesException;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.ResponseWriter;
27 import java.io.IOException JavaDoc;
28 import java.io.Writer JavaDoc;
29
30 /**
31  * JSF Response Writer writing SAX events into the XMLConsumer
32  *
33  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
34  * @version CVS $Id: XMLResponseWriter.java 46253 2004-09-17 14:36:29Z vgritsenko $
35  */

36 public class XMLResponseWriter extends ResponseWriter {
37     private String JavaDoc contentType;
38     private String JavaDoc encoding;
39     private XMLConsumer xmlConsumer;
40
41     private boolean closeStart;
42     private String JavaDoc name;
43     private AttributesImpl JavaDoc attrs;
44
45     private char charHolder[];
46
47
48     public XMLResponseWriter(XMLConsumer xmlConsumer, String JavaDoc contentType, String JavaDoc encoding) throws FacesException {
49         this.contentType = contentType != null ? contentType : "application/xml";
50         this.encoding = encoding;
51         this.xmlConsumer = xmlConsumer;
52
53         this.attrs = new AttributesImpl JavaDoc();
54         this.charHolder = new char[1];
55     }
56
57     public String JavaDoc getContentType() {
58         return contentType;
59     }
60
61     public String JavaDoc getCharacterEncoding() {
62         return encoding;
63     }
64
65     public void startDocument() throws IOException {
66     }
67
68     public void endDocument() throws IOException {
69         closeStartIfNecessary();
70     }
71
72     public void flush() throws IOException {
73         closeStartIfNecessary();
74     }
75
76     public void startElement(String JavaDoc name, UIComponent component) throws IOException {
77         closeStartIfNecessary();
78         this.name = name;
79         this.closeStart = true;
80     }
81
82     public void endElement(String JavaDoc name) throws IOException {
83         closeStartIfNecessary();
84         try {
85             this.xmlConsumer.endElement("", name, name);
86         } catch (SAXException JavaDoc e) {
87             throw new CascadingIOException("SAXException", e);
88         }
89     }
90
91     public void writeAttribute(String JavaDoc name, Object JavaDoc value, String JavaDoc componentPropertyName) throws IOException {
92         if (value == null) {
93             this.attrs.addAttribute("", name, name, "CDATA", "");
94         } else if (Boolean.TRUE.equals(value)) {
95             this.attrs.addAttribute("", name, name, "CDATA", name);
96         } else {
97             this.attrs.addAttribute("", name, name, "CDATA", value.toString());
98         }
99     }
100
101     public void writeURIAttribute(String JavaDoc name, Object JavaDoc value, String JavaDoc componentPropertyName) throws IOException {
102         this.attrs.addAttribute("", name, name, "CDATA", value.toString());
103     }
104
105     public void writeComment(Object JavaDoc comment) throws IOException {
106         closeStartIfNecessary();
107         char[] ch = comment.toString().toCharArray();
108         try {
109             this.xmlConsumer.comment(ch, 0, ch.length);
110         } catch (SAXException JavaDoc e) {
111             throw new CascadingIOException("SAXException", e);
112         }
113     }
114
115     public void writeText(Object JavaDoc text, String JavaDoc componentPropertyName) throws IOException {
116         closeStartIfNecessary();
117         char[] ch = text.toString().toCharArray();
118         try {
119             this.xmlConsumer.characters(ch, 0, ch.length);
120         } catch (SAXException JavaDoc e) {
121             throw new CascadingIOException("SAXException", e);
122         }
123     }
124
125     public void writeText(char text) throws IOException {
126         closeStartIfNecessary();
127         charHolder[0] = text;
128         try {
129             this.xmlConsumer.characters(charHolder, 0, 1);
130         } catch (SAXException JavaDoc e) {
131             throw new CascadingIOException("SAXException", e);
132         }
133     }
134
135     public void writeText(char text[]) throws IOException {
136         closeStartIfNecessary();
137         try {
138             this.xmlConsumer.characters(text, 0, text.length);
139         } catch (SAXException JavaDoc e) {
140             throw new CascadingIOException("SAXException", e);
141         }
142     }
143
144     public void writeText(char text[], int off, int len) throws IOException {
145         closeStartIfNecessary();
146         try {
147             this.xmlConsumer.characters(text, off, len);
148         } catch (SAXException JavaDoc e) {
149             throw new CascadingIOException("SAXException", e);
150         }
151     }
152
153     public ResponseWriter cloneWithWriter(Writer writer) {
154         if (!(writer instanceof XMLResponseWriter)) {
155             throw new IllegalArgumentException JavaDoc("Expected XMLResponseWriter got " + writer);
156         }
157         return new XMLResponseWriter(((XMLResponseWriter) writer).xmlConsumer,
158                                      getContentType(),
159                                      getCharacterEncoding());
160     }
161
162     private void closeStartIfNecessary() throws IOException {
163         if (closeStart) {
164             try {
165                 this.xmlConsumer.startElement("", this.name, this.name, this.attrs);
166             } catch (SAXException JavaDoc e) {
167                 throw new CascadingIOException("SAXException", e);
168             }
169             this.attrs.clear();
170             closeStart = false;
171         }
172     }
173
174     public void close() throws IOException {
175         closeStartIfNecessary();
176     }
177
178     public void write(char cbuf) throws IOException {
179         closeStartIfNecessary();
180         writeText(cbuf);
181     }
182
183     public void write(char cbuf[], int off, int len) throws IOException {
184         closeStartIfNecessary();
185         writeText(cbuf);
186     }
187
188     public void write(int c) throws IOException {
189         closeStartIfNecessary();
190         writeText((char) c);
191     }
192
193     public void write(String JavaDoc str) throws IOException {
194         closeStartIfNecessary();
195         writeText(str.toCharArray());
196     }
197
198     public void write(String JavaDoc str, int off, int len) throws IOException {
199         closeStartIfNecessary();
200         writeText(str.toCharArray(), off, len);
201     }
202 }
203
Popular Tags