KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > wap > renderkit > WmlResponseWriterImpl


1 /*
2  * Copyright 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.myfaces.wap.renderkit;
17
18 import java.util.*;
19 import java.io.Writer JavaDoc;
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 /**
28  *
29  * @author <a HREF="mailto:Jiri.Zaloudek@ivancice.cz">Jiri Zaloudek</a> (latest modification by $Author: matzew $)
30  * @version $Revision: 1.1 $ $Date: 2004/12/30 09:37:27 $
31  * $Log: WmlResponseWriterImpl.java,v $
32  * Revision 1.1 2004/12/30 09:37:27 matzew
33  * added a new RenderKit for WML. Thanks to Jirí Žaloudek
34  *
35  */

36
37 public class WmlResponseWriterImpl extends ResponseWriter {
38     private static Log log = LogFactory.getLog(WmlResponseWriterImpl.class);
39     
40     private static final String JavaDoc DEFAUL_CONTENT_TYPE = "text/vnd.wap.wml";
41     private static final String JavaDoc DEFAULT_CHARACTER_ENCODING = "UTF-8";
42     
43     private Writer JavaDoc writer;
44     private String JavaDoc contentType;
45     private String JavaDoc characterEncoding;
46     private String JavaDoc 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     /** Creates a new instance of WmlResponseWriter */
56     public WmlResponseWriterImpl(Writer JavaDoc writer, String JavaDoc contentType, String JavaDoc 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 JavaDoc writer) {
74         WmlResponseWriterImpl newWriter = new WmlResponseWriterImpl(writer, getContentType(), getCharacterEncoding());
75         return(newWriter);
76     }
77     
78     public void startElement(String JavaDoc name, javax.faces.component.UIComponent uIComponent) throws java.io.IOException JavaDoc {
79         if (name == null) {
80             throw new NullPointerException JavaDoc("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 JavaDoc name) throws java.io.IOException JavaDoc {
91         if (name == null) {
92             throw new NullPointerException JavaDoc("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             // we will get here only if no text or attribute was written after the start element was opened
103
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 JavaDoc value, String JavaDoc componentPropertyName) throws java.io.IOException JavaDoc {
116         if (value == null) {
117             throw new NullPointerException JavaDoc("text name must not be null");
118         }
119         
120         closeStartTagIfNecessary();
121         if(value == null)
122             return;
123         
124         String JavaDoc strValue = value.toString(); //TODO: Use converter for value?
125
writer.write(strValue);
126     }
127     
128     public void writeText(char cbuf[], int off, int len) throws java.io.IOException JavaDoc {
129         if (cbuf == null) {
130             throw new NullPointerException JavaDoc("cbuf name must not be null");
131         }
132         if (cbuf.length < off + len) {
133             throw new IndexOutOfBoundsException JavaDoc((off + len) + " > " + cbuf.length);
134         }
135         
136         closeStartTagIfNecessary();
137         
138         writer.write(cbuf, off, len);
139     }
140     
141     public void writeComment(Object JavaDoc value) throws java.io.IOException JavaDoc {
142         if (value == null) {
143             throw new NullPointerException JavaDoc("comment name must not be null");
144         }
145         
146         closeStartTagIfNecessary();
147         writer.write("<!--");
148         writer.write(value.toString()); //TODO: Escaping: must not have "-->" inside!
149
writer.write("-->");
150     }
151     
152     public void writeAttribute(String JavaDoc name, Object JavaDoc value, String JavaDoc componentPropertyName) throws java.io.IOException JavaDoc {
153         if (name == null) {
154             throw new NullPointerException JavaDoc("attributeName name must not be null");
155         }
156         if (!startTagOpen) {
157             throw new IllegalStateException JavaDoc("Must be called before the start element is closed (attribute '" + name + "')");
158         }
159         
160         if (value instanceof Boolean JavaDoc) {
161             if (((Boolean JavaDoc)value).booleanValue()) {
162                 // name as value for XHTML compatibility
163
writer.write(' ');
164                 writer.write(name);
165                 writer.write("=\"");
166                 writer.write(name);
167                 writer.write('"');
168             }
169         }
170         else {
171             String JavaDoc strValue = value.toString(); //TODO: Use converter for value
172
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 JavaDoc getContentType() {
181         return contentType;
182     }
183     
184     public String JavaDoc getCharacterEncoding() {
185         return characterEncoding;
186     }
187     
188     public void startDocument() throws java.io.IOException JavaDoc {
189         // do nothing
190
}
191     
192     public void endDocument() throws java.io.IOException JavaDoc {
193         flush();
194         writer.flush();
195     }
196     
197     public void writeURIAttribute(String JavaDoc name, Object JavaDoc value, String JavaDoc componentPropertyName) throws java.io.IOException JavaDoc {
198         if (name == null) {
199             throw new NullPointerException JavaDoc("attributeName name must not be null");
200         }
201         if (!startTagOpen) {
202             throw new IllegalStateException JavaDoc("Must be called before the start element is closed (attribute '" + name + "')");
203         }
204         
205         String JavaDoc strValue = value.toString(); //TODO: Use converter for value?
206
writer.write(' ');
207         writer.write(name);
208         writer.write("=\"" + strValue + "\"");
209     }
210     
211     private void closeStartTagIfNecessary() throws java.io.IOException JavaDoc {
212         if (startTagOpen) {
213             if (s_emptyHtmlElements.contains(startElementName.toLowerCase())) {
214                 writer.write(" />");
215                 // make null, this will cause NullPointer in some invalid element nestings
216
// (better than doing nothing)
217
startElementName = null;
218             }
219             else {
220                 writer.write('>');
221             }
222             startTagOpen = false;
223         }
224     }
225     
226     public void flush() throws java.io.IOException JavaDoc {
227         // API doc says we should not flush the underlying writer
228
//_writer.flush();
229
// but rather clear any values buffered by this ResponseWriter:
230
closeStartTagIfNecessary();
231     }
232     
233     public void write(char cbuf[], int off, int len) throws java.io.IOException JavaDoc {
234         closeStartTagIfNecessary();
235         writer.write(cbuf, off, len);
236     }
237     
238     public void write(int c) throws java.io.IOException JavaDoc {
239         closeStartTagIfNecessary();
240         writer.write(c);
241     }
242     
243     public void write(char cbuf[]) throws java.io.IOException JavaDoc {
244         closeStartTagIfNecessary();
245         writer.write(cbuf);
246     }
247     
248     public void close() throws java.io.IOException JavaDoc {
249         if (startTagOpen) {
250             // we will get here only if no text was written after the start element was opened
251
writer.write(" />");
252         }
253         writer.close();
254     }
255     
256 }
257
Popular Tags