1 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 ; 22 import org.xml.sax.helpers.AttributesImpl ; 23 24 import javax.faces.FacesException; 25 import javax.faces.component.UIComponent; 26 import javax.faces.context.ResponseWriter; 27 import java.io.IOException ; 28 import java.io.Writer ; 29 30 36 public class XMLResponseWriter extends ResponseWriter { 37 private String contentType; 38 private String encoding; 39 private XMLConsumer xmlConsumer; 40 41 private boolean closeStart; 42 private String name; 43 private AttributesImpl attrs; 44 45 private char charHolder[]; 46 47 48 public XMLResponseWriter(XMLConsumer xmlConsumer, String contentType, String 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 (); 54 this.charHolder = new char[1]; 55 } 56 57 public String getContentType() { 58 return contentType; 59 } 60 61 public String 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 name, UIComponent component) throws IOException { 77 closeStartIfNecessary(); 78 this.name = name; 79 this.closeStart = true; 80 } 81 82 public void endElement(String name) throws IOException { 83 closeStartIfNecessary(); 84 try { 85 this.xmlConsumer.endElement("", name, name); 86 } catch (SAXException e) { 87 throw new CascadingIOException("SAXException", e); 88 } 89 } 90 91 public void writeAttribute(String name, Object value, String 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 name, Object value, String componentPropertyName) throws IOException { 102 this.attrs.addAttribute("", name, name, "CDATA", value.toString()); 103 } 104 105 public void writeComment(Object comment) throws IOException { 106 closeStartIfNecessary(); 107 char[] ch = comment.toString().toCharArray(); 108 try { 109 this.xmlConsumer.comment(ch, 0, ch.length); 110 } catch (SAXException e) { 111 throw new CascadingIOException("SAXException", e); 112 } 113 } 114 115 public void writeText(Object text, String componentPropertyName) throws IOException { 116 closeStartIfNecessary(); 117 char[] ch = text.toString().toCharArray(); 118 try { 119 this.xmlConsumer.characters(ch, 0, ch.length); 120 } catch (SAXException 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 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 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 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 ("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 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 str) throws IOException { 194 closeStartIfNecessary(); 195 writeText(str.toCharArray()); 196 } 197 198 public void write(String str, int off, int len) throws IOException { 199 closeStartIfNecessary(); 200 writeText(str.toCharArray(), off, len); 201 } 202 } 203 | Popular Tags |