1 16 package org.apache.myfaces.wap.renderkit; 17 18 import java.util.*; 19 import java.io.Writer ; 20 21 import javax.faces.context.ResponseWriter; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.myfaces.renderkit.html.util.HTMLEncoder; 26 27 36 37 public class WmlResponseWriterImpl extends ResponseWriter { 38 private static Log log = LogFactory.getLog(WmlResponseWriterImpl.class); 39 40 private static final String DEFAUL_CONTENT_TYPE = "text/vnd.wap.wml"; 41 private static final String DEFAULT_CHARACTER_ENCODING = "UTF-8"; 42 43 private Writer writer; 44 private String contentType; 45 private String characterEncoding; 46 private String startElementName; 47 private boolean startTagOpen; 48 49 private static final Set s_emptyHtmlElements = new HashSet(); 50 static { 51 s_emptyHtmlElements.add("img"); 52 s_emptyHtmlElements.add("postfield"); 53 } 54 55 56 public WmlResponseWriterImpl(Writer writer, String contentType, String characterEncoding) { 57 log.debug("created object " + this.getClass().getName()); 58 this.writer = writer; 59 this.contentType = contentType; 60 this.characterEncoding = characterEncoding; 61 62 if (contentType == null) { 63 log.info("Content type is null, using default value: " + DEFAUL_CONTENT_TYPE); 64 this.contentType = DEFAUL_CONTENT_TYPE; 65 } 66 67 if (characterEncoding == null) { 68 log.info("Character encoding is null, using default value: " + DEFAULT_CHARACTER_ENCODING); 69 this.characterEncoding = DEFAULT_CHARACTER_ENCODING; 70 } 71 } 72 73 public ResponseWriter cloneWithWriter(Writer writer) { 74 WmlResponseWriterImpl newWriter = new WmlResponseWriterImpl(writer, getContentType(), getCharacterEncoding()); 75 return(newWriter); 76 } 77 78 public void startElement(String name, javax.faces.component.UIComponent uIComponent) throws java.io.IOException { 79 if (name == null) { 80 throw new NullPointerException ("elementName name must not be null"); 81 } 82 83 closeStartTagIfNecessary(); 84 writer.write('<'); 85 writer.write(name); 86 startElementName = name; 87 startTagOpen = true; 88 } 89 90 public void endElement(String name) throws java.io.IOException { 91 if (name == null) { 92 throw new NullPointerException ("elementName name must not be null"); 93 } 94 95 if (log.isWarnEnabled()) { 96 if (startElementName != null && !name.equals(startElementName)) { 97 log.warn("WML nesting warning on closing " + name + ": element " + startElementName + " not explicitly closed"); 98 } 99 } 100 101 if(startTagOpen) { 102 writer.write(" />"); 104 startTagOpen = false; 105 } 106 else { 107 writer.write("</"); 108 writer.write(name); 109 writer.write('>'); 110 } 111 112 startElementName = null; 113 } 114 115 public void writeText(Object value, String componentPropertyName) throws java.io.IOException { 116 if (value == null) { 117 throw new NullPointerException ("text name must not be null"); 118 } 119 120 closeStartTagIfNecessary(); 121 if(value == null) 122 return; 123 124 String strValue = value.toString(); writer.write(strValue); 126 } 127 128 public void writeText(char cbuf[], int off, int len) throws java.io.IOException { 129 if (cbuf == null) { 130 throw new NullPointerException ("cbuf name must not be null"); 131 } 132 if (cbuf.length < off + len) { 133 throw new IndexOutOfBoundsException ((off + len) + " > " + cbuf.length); 134 } 135 136 closeStartTagIfNecessary(); 137 138 writer.write(cbuf, off, len); 139 } 140 141 public void writeComment(Object value) throws java.io.IOException { 142 if (value == null) { 143 throw new NullPointerException ("comment name must not be null"); 144 } 145 146 closeStartTagIfNecessary(); 147 writer.write("<!--"); 148 writer.write(value.toString()); writer.write("-->"); 150 } 151 152 public void writeAttribute(String name, Object value, String componentPropertyName) throws java.io.IOException { 153 if (name == null) { 154 throw new NullPointerException ("attributeName name must not be null"); 155 } 156 if (!startTagOpen) { 157 throw new IllegalStateException ("Must be called before the start element is closed (attribute '" + name + "')"); 158 } 159 160 if (value instanceof Boolean ) { 161 if (((Boolean )value).booleanValue()) { 162 writer.write(' '); 164 writer.write(name); 165 writer.write("=\""); 166 writer.write(name); 167 writer.write('"'); 168 } 169 } 170 else { 171 String strValue = value.toString(); writer.write(' '); 173 writer.write(name); 174 writer.write("=\""); 175 writer.write(HTMLEncoder.encode(strValue, false, false)); 176 writer.write('"'); 177 } 178 } 179 180 public String getContentType() { 181 return contentType; 182 } 183 184 public String getCharacterEncoding() { 185 return characterEncoding; 186 } 187 188 public void startDocument() throws java.io.IOException { 189 } 191 192 public void endDocument() throws java.io.IOException { 193 flush(); 194 writer.flush(); 195 } 196 197 public void writeURIAttribute(String name, Object value, String componentPropertyName) throws java.io.IOException { 198 if (name == null) { 199 throw new NullPointerException ("attributeName name must not be null"); 200 } 201 if (!startTagOpen) { 202 throw new IllegalStateException ("Must be called before the start element is closed (attribute '" + name + "')"); 203 } 204 205 String strValue = value.toString(); writer.write(' '); 207 writer.write(name); 208 writer.write("=\"" + strValue + "\""); 209 } 210 211 private void closeStartTagIfNecessary() throws java.io.IOException { 212 if (startTagOpen) { 213 if (s_emptyHtmlElements.contains(startElementName.toLowerCase())) { 214 writer.write(" />"); 215 startElementName = null; 218 } 219 else { 220 writer.write('>'); 221 } 222 startTagOpen = false; 223 } 224 } 225 226 public void flush() throws java.io.IOException { 227 closeStartTagIfNecessary(); 231 } 232 233 public void write(char cbuf[], int off, int len) throws java.io.IOException { 234 closeStartTagIfNecessary(); 235 writer.write(cbuf, off, len); 236 } 237 238 public void write(int c) throws java.io.IOException { 239 closeStartTagIfNecessary(); 240 writer.write(c); 241 } 242 243 public void write(char cbuf[]) throws java.io.IOException { 244 closeStartTagIfNecessary(); 245 writer.write(cbuf); 246 } 247 248 public void close() throws java.io.IOException { 249 if (startTagOpen) { 250 writer.write(" />"); 252 } 253 writer.close(); 254 } 255 256 } 257 | Popular Tags |