View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.webapp;
21  
22  import org.apache.myfaces.tobago.renderkit.css.Classes;
23  import org.apache.myfaces.tobago.renderkit.css.Style;
24  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
25  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
27  
28  import javax.faces.component.UIComponent;
29  import javax.faces.context.ResponseWriter;
30  import java.io.IOException;
31  import java.io.Writer;
32  
33  /**
34   * This provides an alternative ResponseWriter interfaces, which allows optimizations.
35   * E. g. some attributes needed to to be escaped.
36   * <p/>
37   */
38  public abstract class TobagoResponseWriter extends ResponseWriter {
39  
40    // same as in ResponseWriter
41  
42    @Override
43    public abstract void startElement(String name, UIComponent component) throws IOException;
44  
45    /**
46     * @deprecated Use {@link #startElement(String, UIComponent) startElement(name, null)} instead.
47     */
48    @Deprecated
49    public void startElement(String name) throws IOException {
50      startElement(name, null);
51    }
52  
53    @Override
54    public abstract void endElement(String name) throws IOException;
55      
56    public abstract void write(String string) throws IOException;
57  
58    @Override
59    public abstract void writeComment(Object comment) throws IOException;
60  
61    @Override
62    public abstract ResponseWriter cloneWithWriter(Writer writer);
63  
64    /**
65     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
66     */
67    @Deprecated
68    public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
69  
70    /**
71     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
72     */
73    @Deprecated
74    public abstract void writeText(Object text, String property) throws IOException;
75  
76    @Override
77    public abstract void flush() throws IOException;
78  
79    // others (not from ResponseWriter)
80  
81    /**
82     * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string,
83     * if it is not necessary.
84     */
85    public abstract void writeAttribute(String name, String string, boolean escape) throws IOException;
86  
87    /**
88     * Writes a boolean attribute. The value will not escaped.
89     */
90    public void writeAttribute(String name, boolean on) throws IOException {
91      if (on) {
92        writeAttribute(name, name, false);
93      }
94    }
95  
96    /**
97     * Writes a integer attribute. The value will not escaped.
98     */
99    public void writeAttribute(String name, int number) throws IOException {
100     writeAttribute(name, Integer.toString(number), false);
101   }
102 
103   /**
104    * Writes a propery as attribute. The value will be escaped.
105    */
106   public void writeAttributeFromComponent(String name, String property) throws IOException {
107     writeAttribute(name, null, property);
108   }
109 
110   /**
111    * Write the id attribute. The value will not escaped.
112    */
113   public void writeIdAttribute(String id) throws IOException {
114     writeAttribute(HtmlAttributes.ID, id, false);
115   }
116 
117   /**
118    * Write the name attribute. The value will not escaped.
119    */
120   public void writeNameAttribute(String name) throws IOException {
121     writeAttribute(HtmlAttributes.NAME, name, false);
122   }
123 
124   /**
125    * Write the class attribute. The value will not escaped.
126    * @deprecated since Tobago 1.5.0
127    */
128   @Deprecated
129   public void writeClassAttribute(String cssClass) throws IOException {
130     writeAttribute(HtmlAttributes.CLASS, cssClass, false);
131   }
132 
133   /**
134    * Write the class attribute. The value will not escaped.
135    * @param classes The abstract representation of the css class string, normally created by the renderer.
136    */
137   public void writeClassAttribute(Classes classes) throws IOException {
138     writeAttribute(HtmlAttributes.CLASS, classes.getStringValue(), false);
139   }
140 
141   @Deprecated
142   public abstract String getStyleClasses();
143 
144   /**
145    * Write the class attribute. The value will not escaped.
146    * @deprecated since Tobago 1.5.0
147    */
148   @Deprecated
149   public abstract void writeClassAttribute() throws IOException;
150 
151   /**
152    * Write the style attribute. The value will not escaped.
153    */
154   public void writeStyleAttribute(Style style) throws IOException {
155     if (style != null) {
156       final String json = style.encodeJson();
157       if (json.length() > 2) { // empty "{}" needs not to be written
158         writeAttribute(DataAttributes.STYLE, json, true);
159         // todo: check not escaping, when it's not needed, use: style.needsToBeEscaped()
160       }
161     }
162   }
163 
164   /**
165    * Write the style attribute. The value will not escaped.
166    * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead.
167    */
168   @Deprecated
169   public void writeStyleAttribute(String style) throws IOException {
170     writeAttribute(HtmlAttributes.STYLE, style, false);
171   }
172 
173   /**
174    * @deprecated Should not be used, because it conflicts with CSP.
175    */
176   @Deprecated
177   public void writeJavascript(String script) throws IOException {
178     startJavascript();
179     write(script);
180     endJavascript();
181   }
182 
183   /**
184    * @deprecated Should not be used, because it conflicts with CSP.
185    */
186   @Deprecated
187   public void endJavascript() throws IOException {
188 //    write("\n// -->\n"); // todo: for XHMTL we may need
189     endElement(HtmlElements.SCRIPT);
190   }
191 
192   /**
193    * @deprecated Should not be used, because it conflicts with CSP.
194    */
195   @Deprecated
196   public void startJavascript() throws IOException {
197     startElement(HtmlElements.SCRIPT, null);
198     writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
199     flush(); // is needed in some cases, e. g. TOBAGO-1094
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 }