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.CssItem;
23  import org.apache.myfaces.tobago.renderkit.css.FontAwesomeIconEncoder;
24  import org.apache.myfaces.tobago.renderkit.css.IconEncoder;
25  import org.apache.myfaces.tobago.renderkit.css.Icons;
26  import org.apache.myfaces.tobago.renderkit.css.Style;
27  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
28  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
29  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlTypes;
31  import org.apache.myfaces.tobago.renderkit.html.MarkupLanguageAttributes;
32  
33  import javax.faces.component.UIComponent;
34  import javax.faces.context.ResponseWriter;
35  import java.io.IOException;
36  import java.io.Writer;
37  
38  /**
39   * <p>
40   * This provides an alternative ResponseWriter interfaces, which allows optimizations.
41   * E. g. some attributes needed to to be escaped.
42   * </p>
43   */
44  public abstract class TobagoResponseWriter extends ResponseWriter {
45  
46    private static final CssItem[] NO_CSS_ITEMS = new CssItem[0];
47  
48    private IconEncoder iconEncoder = new FontAwesomeIconEncoder();
49  
50    // same as in ResponseWriter
51  
52    /**
53     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
54     */
55    @Deprecated
56    @Override
57    public abstract void startElement(String name, UIComponent component) throws IOException;
58  
59    public abstract void startElement(HtmlElements name) throws IOException;
60  
61    /**
62     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
63     */
64    @Deprecated
65    @Override
66    public abstract void endElement(String name) throws IOException;
67      
68    public abstract void endElement(HtmlElements name) throws IOException;
69  
70    @Override
71    public abstract void write(String string) throws IOException;
72  
73    @Override
74    public abstract void writeComment(Object comment) throws IOException;
75  
76    @Override
77    public abstract ResponseWriter cloneWithWriter(Writer writer);
78  
79    /**
80     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
81     */
82    @Override
83    @Deprecated
84    public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
85  
86    /**
87     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
88     */
89    @Override
90    @Deprecated
91    public abstract void writeURIAttribute(String name, Object value, final String property) throws IOException;
92  
93    /**
94     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
95     */
96    @Override
97    @Deprecated
98    public abstract void writeText(Object text, String property) throws IOException;
99  
100   @Override
101   public abstract void flush() throws IOException;
102 
103   // others (not from ResponseWriter)
104 
105   /**
106    * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string,
107    * if it is not necessary.
108    */
109   public abstract void writeAttribute(MarkupLanguageAttributes name, String string, boolean escape) throws IOException;
110 
111   public abstract void writeAttribute(MarkupLanguageAttributes name, HtmlTypes type) throws IOException;
112 
113   /**
114    * Writes a string attribute URL encoded.
115    */
116   public abstract void writeURIAttribute(MarkupLanguageAttributes name, String string) throws IOException;
117 
118   /**
119    * Writes a boolean attribute. The value will not escaped.
120    */
121   public void writeAttribute(final MarkupLanguageAttributes name, final boolean on) throws IOException {
122     if (on) {
123       writeAttribute(name, name.getValue(), false);
124     }
125   }
126 
127   /**
128    * Writes a {@link Integer} attribute, if the value is not {@code null}. The value will not be escaped.
129    */
130   public void writeAttribute(final MarkupLanguageAttributes name, final Integer number) throws IOException {
131     if (number != null) {
132       writeAttribute(name, Integer.toString(number), false);
133     }
134   }
135 
136   /**
137    * Write the id attribute. The value will not escaped.
138    */
139   public void writeIdAttribute(final String id) throws IOException {
140     writeAttribute(HtmlAttributes.ID, id, false);
141   }
142 
143   /**
144    * Write the name attribute. The value will not escaped.
145    */
146   public void writeNameAttribute(final String name) throws IOException {
147     writeAttribute(HtmlAttributes.NAME, name, false);
148   }
149 
150   /**
151    * Write the class attribute. The value will not escaped.
152    */
153   public void writeClassAttribute(final CssItem first) throws IOException {
154     if (first != null) {
155       final StringBuilder builder = new StringBuilder();
156       builder.append(first.getName());
157       builder.append(' ');
158       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
159     }
160   }
161 
162   /**
163    * Write the class attribute. The value will not escaped.
164    */
165   public void writeClassAttribute(final CssItem first, final CssItem second) throws IOException {
166     final StringBuilder builder = new StringBuilder();
167     boolean render = false;
168     if (first != null) {
169       builder.append(first.getName());
170       builder.append(' ');
171       render = true;
172     }
173     if (second != null) {
174       builder.append(second.getName());
175       builder.append(' ');
176       render = true;
177     }
178     if (render) {
179       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
180     }
181   }
182 
183   /**
184    * Write the class attribute. The value will not escaped.
185    */
186   public void writeClassAttribute(final CssItem first, final CssItem second, final CssItem... others)
187       throws IOException {
188     final StringBuilder builder = new StringBuilder();
189     boolean render = false;
190     if (first != null) {
191       builder.append(first.getName());
192       builder.append(' ');
193       render = true;
194     }
195     if (second != null) {
196       builder.append(second.getName());
197       builder.append(' ');
198       render = true;
199     }
200     for (CssItem other : others) {
201       if (other != null) {
202         builder.append(other.getName());
203         builder.append(' ');
204         render = true;
205       }
206     }
207     if (render) {
208       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
209     }
210   }
211 
212   /**
213    * Write the style attribute. The value may be escaped (depending of the content).
214    */
215   public void writeStyleAttribute(final Style style) throws IOException {
216     if (style != null) {
217       final String json = style.encodeJson();
218       if (json.length() > 2) { // empty "{}" needs not to be written
219         writeAttribute(DataAttributes.STYLE, json, style.needsToBeEscaped());
220       }
221     }
222   }
223 
224   /**
225    * Write the style attribute. The value will not escaped.
226    * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead.
227    */
228   @Deprecated
229   public void writeStyleAttribute(final String style) throws IOException {
230     writeAttribute(HtmlAttributes.STYLE, style, false);
231   }
232 
233   /**
234    * Writes an supported icon.
235    */
236   public void writeIcon(final Icons icon, final CssItem... cssItems) throws IOException {
237     writeIcon(icon, null, cssItems);
238   }
239 
240   /**
241    * Writes an supported icon with explicit style information.
242    */
243   public void writeIcon(final Icons icon, final Style style, final CssItem... cssItems) throws IOException {
244     iconEncoder.encode(this, icon, style, cssItems);
245   }
246 
247   /**
248    * @deprecated Should not be used, because it conflicts with CSP.
249    */
250   @Deprecated
251   public void writeJavascript(final String script) throws IOException {
252     startJavascript();
253     write(script);
254     endJavascript();
255   }
256 
257   /**
258    * @deprecated Should not be used, because it conflicts with CSP.
259    */
260   @Deprecated
261   public void endJavascript() throws IOException {
262 //    write("\n// -->\n"); // todo: for XHMTL we may need
263     endElement(HtmlElements.SCRIPT);
264   }
265 
266   /**
267    * @deprecated Should not be used, because it conflicts with CSP.
268    */
269   @Deprecated
270   public void startJavascript() throws IOException {
271     startElement(HtmlElements.SCRIPT);
272     writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
273   }
274 
275   /**
276    * Write text content. The text will be escaped.
277    */
278   public void writeText(final String text) throws IOException {
279     writeText(text, null);
280   }
281 
282   public String getContentTypeWithCharSet() {
283     String contentType = getContentType();
284     if (contentType == null) {
285       contentType = "text/html";
286     }
287     String characterEncoding = getCharacterEncoding();
288     if (characterEncoding == null) {
289       characterEncoding = "UTF-8";
290     }
291 
292     return contentType + "; charset=" + characterEncoding;
293   }
294 
295   @Override
296   public void startCDATA() throws IOException {
297     write("<![CDATA[");
298   }
299 
300   @Override
301   public void endCDATA() throws IOException {
302     write("]]>");
303   }
304 
305 //  protected abstract void writeNewline() throws IOException;
306 }