001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.myfaces.tobago.webapp;
021
022 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
023 import org.apache.myfaces.tobago.renderkit.html.HtmlConstants;
024 import org.apache.myfaces.tobago.renderkit.html.HtmlStyleMap;
025 import org.apache.myfaces.tobago.renderkit.html.StyleClasses;
026
027 import javax.faces.component.UIComponent;
028 import javax.faces.context.ResponseWriter;
029 import java.io.IOException;
030 import java.io.Writer;
031
032 /**
033 * This provides an alternative ResponseWriter interfaces, which allows optimizations.
034 * E. g. some attributes needed to to be escaped.
035 * <p/>
036 * User: lofwyr
037 * Date: 08.05.2007 13:51:43
038 */
039 public abstract class TobagoResponseWriter extends ResponseWriter {
040
041 // same as in ResponseWriter
042
043 public abstract void startElement(String name, UIComponent component) throws IOException;
044
045 /**
046 * @deprecated Use {@link #startElement(String, UIComponent) startElement(name, null)} instead.
047 */
048 @Deprecated
049 public void startElement(String name) throws IOException {
050 startElement(name, null);
051 }
052
053 public abstract void endElement(String name) throws IOException;
054
055 public abstract void write(String string) throws IOException;
056
057 public abstract void writeComment(Object comment) throws IOException;
058
059 public abstract ResponseWriter cloneWithWriter(Writer writer);
060
061 /**
062 * @deprecated Should not directly called via this interface. There is be a special method which might be better.
063 */
064 @Deprecated
065 public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
066
067 /**
068 * @deprecated Should not directly called via this interface. There is be a special method which might be better.
069 */
070 @Deprecated
071 public abstract void writeText(Object text, String property) throws IOException;
072
073 public abstract void flush() throws IOException;
074
075 // others (not from ResponseWriter)
076
077 /**
078 * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string,
079 * if it is not necessary.
080 */
081 public abstract void writeAttribute(String name, String string, boolean escape) throws IOException;
082
083 /**
084 * Writes a boolean attribute. The value will not escaped.
085 */
086 public void writeAttribute(String name, boolean on) throws IOException {
087 if (on) {
088 writeAttribute(name, name, false);
089 }
090 }
091
092 /**
093 * Writes a integer attribute. The value will not escaped.
094 */
095 public void writeAttribute(String name, int number) throws IOException {
096 writeAttribute(name, Integer.toString(number), false);
097 }
098
099 /**
100 * Writes a propery as attribute. The value will be escaped.
101 */
102 public void writeAttributeFromComponent(String name, String property) throws IOException {
103 writeAttribute(name, null, property);
104 }
105
106 /**
107 * Write the id attribute. The value will not escaped.
108 */
109 public void writeIdAttribute(String id) throws IOException {
110 writeAttribute(HtmlAttributes.ID, id, false);
111 }
112
113 /**
114 * Write the name attribute. The value will not escaped.
115 */
116 public void writeNameAttribute(String name) throws IOException {
117 writeAttribute(HtmlAttributes.NAME, name, false);
118 }
119
120 /**
121 * Write the class attribute. The value will not escaped.
122 */
123 public void writeClassAttribute(String cssClass) throws IOException {
124 writeAttribute(HtmlAttributes.CLASS, cssClass, false);
125 }
126
127 /**
128 * Write the class attribute. The value will not escaped.
129 */
130 public void writeClassAttribute(StyleClasses styleClasses) throws IOException {
131 writeAttribute(HtmlAttributes.CLASS, styleClasses.toString(), false);
132 }
133
134 /**
135 * Write the class attribute. The value will not escaped.
136 */
137 public abstract void writeClassAttribute() throws IOException;
138
139 /**
140 * Write the style attribute. The value will not escaped.
141 */
142 public void writeStyleAttribute(HtmlStyleMap style) throws IOException {
143 if (style != null) {
144 writeAttribute(HtmlAttributes.STYLE, style.toString(), false);
145 }
146 }
147
148 /**
149 * Write the style attribute. The value will not escaped.
150 */
151 public void writeStyleAttribute(String style) throws IOException {
152 writeAttribute(HtmlAttributes.STYLE, style, false);
153 }
154
155 /**
156 * Write the style attribute. The value will not escaped.
157 */
158 public abstract void writeStyleAttribute() throws IOException;
159
160 public void writeJavascript(String script) throws IOException {
161 startElement(HtmlConstants.SCRIPT, null);
162 writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
163 write("\n<!--\n");
164 write(script);
165 write("\n// -->\n");
166 endElement(HtmlConstants.SCRIPT);
167 }
168
169 /**
170 * Write text content. The text will be escaped.
171 */
172 public void writeText(String text) throws IOException {
173 writeText(text, null);
174 }
175
176 /**
177 * Writes a propery as text. The text will be escaped.
178 */
179 public void writeTextFromComponent(String property) throws IOException {
180 writeText(null, property);
181 }
182
183 public String getContentTypeWithCharSet() {
184 String contentType = getContentType();
185 if (contentType == null) {
186 contentType = "text/html";
187 }
188 String characterEncoding = getCharacterEncoding();
189 if (characterEncoding == null) {
190 characterEncoding = "UTF-8";
191 }
192
193 StringBuilder builder = new StringBuilder(contentType);
194 builder.append("; charset=");
195 builder.append(characterEncoding);
196 return builder.toString();
197
198 }
199 }