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.css.Classes; 021 import org.apache.myfaces.tobago.renderkit.css.Style; 022 import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes; 023 import org.apache.myfaces.tobago.renderkit.html.HtmlElements; 024 import org.apache.myfaces.tobago.renderkit.html.StyleClasses; 025 026 import javax.faces.component.UIComponent; 027 import javax.faces.context.ResponseWriter; 028 import java.io.IOException; 029 import java.io.Writer; 030 031 /** 032 * This provides an alternative ResponseWriter interfaces, which allows optimizations. 033 * E. g. some attributes needed to to be escaped. 034 * <p/> 035 * Date: 08.05.2007 13:51:43 036 */ 037 public abstract class TobagoResponseWriter extends ResponseWriter { 038 039 // same as in ResponseWriter 040 041 @Override 042 public abstract void startElement(String name, UIComponent component) throws IOException; 043 044 /** 045 * @deprecated Use {@link #startElement(String, UIComponent) startElement(name, null)} instead. 046 */ 047 @Deprecated 048 public void startElement(String name) throws IOException { 049 startElement(name, null); 050 } 051 052 @Override 053 public abstract void endElement(String name) throws IOException; 054 055 public abstract void write(String string) throws IOException; 056 057 @Override 058 public abstract void writeComment(Object comment) throws IOException; 059 060 @Override 061 public abstract ResponseWriter cloneWithWriter(Writer writer); 062 063 /** 064 * @deprecated Should not directly called via this interface. There is be a special method which might be better. 065 */ 066 @Deprecated 067 public abstract void writeAttribute(String name, Object value, final String property) throws IOException; 068 069 /** 070 * @deprecated Should not directly called via this interface. There is be a special method which might be better. 071 */ 072 @Deprecated 073 public abstract void writeText(Object text, String property) throws IOException; 074 075 @Override 076 public abstract void flush() throws IOException; 077 078 // others (not from ResponseWriter) 079 080 /** 081 * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string, 082 * if it is not necessary. 083 */ 084 public abstract void writeAttribute(String name, String string, boolean escape) throws IOException; 085 086 /** 087 * Writes a boolean attribute. The value will not escaped. 088 */ 089 public void writeAttribute(String name, boolean on) throws IOException { 090 if (on) { 091 writeAttribute(name, name, false); 092 } 093 } 094 095 /** 096 * Writes a integer attribute. The value will not escaped. 097 */ 098 public void writeAttribute(String name, int number) throws IOException { 099 writeAttribute(name, Integer.toString(number), false); 100 } 101 102 /** 103 * Writes a propery as attribute. The value will be escaped. 104 */ 105 public void writeAttributeFromComponent(String name, String property) throws IOException { 106 writeAttribute(name, null, property); 107 } 108 109 /** 110 * Write the id attribute. The value will not escaped. 111 */ 112 public void writeIdAttribute(String id) throws IOException { 113 writeAttribute(HtmlAttributes.ID, id, false); 114 } 115 116 /** 117 * Write the name attribute. The value will not escaped. 118 */ 119 public void writeNameAttribute(String name) throws IOException { 120 writeAttribute(HtmlAttributes.NAME, name, false); 121 } 122 123 /** 124 * Write the class attribute. The value will not escaped. 125 * @deprecated since Tobago 1.5.0 126 */ 127 @Deprecated 128 public void writeClassAttribute(String cssClass) throws IOException { 129 writeAttribute(HtmlAttributes.CLASS, cssClass, false); 130 } 131 132 /** 133 * Write the class attribute. The value will not escaped. 134 * @deprecated since Tobago 1.5.0 135 */ 136 @Deprecated 137 public void writeClassAttribute(StyleClasses styleClasses) throws IOException { 138 writeAttribute(HtmlAttributes.CLASS, styleClasses.toString(), false); 139 } 140 141 /** 142 * Write the class attribute. The value will not escaped. 143 * <br/> 144 * <b>Note:</b> For backward compatibility the value of the Attribute 145 * <code>{@link org.apache.myfaces.tobago.component.Attributes#STYLE_CLASS}</code> 146 * will be inserted additionally. This function will be removed in later releases. 147 * @param classes The abstract representation of the css class string, normally created by the renderer. 148 */ 149 public void writeClassAttribute(Classes classes) throws IOException { 150 String styleClasses = getStyleClasses(); 151 String stringValue = classes.getStringValue(); 152 if (styleClasses != null) { 153 stringValue += " " + styleClasses; 154 } 155 writeAttribute(HtmlAttributes.CLASS, stringValue, false); 156 } 157 158 @Deprecated 159 public abstract String getStyleClasses(); 160 161 /** 162 * Write the class attribute. The value will not escaped. 163 * @deprecated since Tobago 1.5.0 164 */ 165 @Deprecated 166 public abstract void writeClassAttribute() throws IOException; 167 168 /** 169 * Write the style attribute. The value will not escaped. 170 */ 171 public void writeStyleAttribute(Style style) throws IOException { 172 if (style != null) { 173 writeAttribute(HtmlAttributes.STYLE, style.encode(), false); 174 } 175 } 176 177 /** 178 * Write the style attribute. The value will not escaped. 179 * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead. 180 */ 181 @Deprecated 182 public void writeStyleAttribute(String style) throws IOException { 183 writeAttribute(HtmlAttributes.STYLE, style, false); 184 } 185 186 public void writeJavascript(String script) throws IOException { 187 startJavascript(); 188 write(script); 189 endJavascript(); 190 } 191 192 public void endJavascript() throws IOException { 193 // write("\n// -->\n"); // todo: for XHMTL we may need 194 endElement(HtmlElements.SCRIPT); 195 } 196 197 public void startJavascript() throws IOException { 198 startElement(HtmlElements.SCRIPT, null); 199 writeAttribute(HtmlAttributes.TYPE, "text/javascript", false); 200 // write("\n<!--\n"); 201 } 202 203 /** 204 * Write text content. The text will be escaped. 205 */ 206 public void writeText(String text) throws IOException { 207 writeText(text, null); 208 } 209 210 /** 211 * Writes a property as text. The text will be escaped. 212 */ 213 public void writeTextFromComponent(String property) throws IOException { 214 writeText(null, property); 215 } 216 217 public String getContentTypeWithCharSet() { 218 String contentType = getContentType(); 219 if (contentType == null) { 220 contentType = "text/html"; 221 } 222 String characterEncoding = getCharacterEncoding(); 223 if (characterEncoding == null) { 224 characterEncoding = "UTF-8"; 225 } 226 227 StringBuilder builder = new StringBuilder(contentType); 228 builder.append(";charset="); 229 builder.append(characterEncoding); 230 return builder.toString(); 231 232 } 233 }