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