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       // todo: optimize me, do not use StringBuilder
156       final StringBuilder builder = new StringBuilder();
157       builder.append(first.getName());
158       builder.append(' ');
159       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
160     }
161   }
162 
163   /**
164    * Write the command map data attribute.
165    */
166   public void writeCommandMapAttribute(final String map) throws IOException { // XXX use CommandMap instead of String
167     if (map != null) {
168       // XXX
169       writeAttribute(DataAttributes.COMMANDS, map, true);
170     }
171   }
172 
173   /**
174    * Write the class attribute. The value will not escaped.
175    */
176   public void writeClassAttribute(final CssItem first, final CssItem second) throws IOException {
177     final StringBuilder builder = new StringBuilder();
178     boolean render = false;
179     if (first != null) {
180       builder.append(first.getName());
181       builder.append(' ');
182       render = true;
183     }
184     if (second != null) {
185       builder.append(second.getName());
186       builder.append(' ');
187       render = true;
188     }
189     if (render) {
190       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
191     }
192   }
193 
194   /**
195    * Write the class attribute. The value will not escaped.
196    */
197   public void writeClassAttribute(final CssItem first, final CssItem second, final CssItem... others)
198       throws IOException {
199     final StringBuilder builder = new StringBuilder();
200     boolean render = false;
201     if (first != null) {
202       builder.append(first.getName());
203       builder.append(' ');
204       render = true;
205     }
206     if (second != null) {
207       builder.append(second.getName());
208       builder.append(' ');
209       render = true;
210     }
211     for (CssItem other : others) {
212       if (other != null) {
213         builder.append(other.getName());
214         builder.append(' ');
215         render = true;
216       }
217     }
218     if (render) {
219       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
220     }
221   }
222 
223   /**
224    * Write the style attribute. The value may be escaped (depending of the content).
225    */
226   public void writeStyleAttribute(final Style style) throws IOException {
227     if (style != null) {
228       final String json = style.encodeJson();
229       if (json.length() > 2) { // empty "{}" needs not to be written
230         writeAttribute(DataAttributes.STYLE, json, style.needsToBeEscaped());
231       }
232     }
233   }
234 
235   /**
236    * Write the style attribute. The value will not escaped.
237    * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead.
238    */
239   @Deprecated
240   public void writeStyleAttribute(final String style) throws IOException {
241     writeAttribute(HtmlAttributes.STYLE, style, false);
242   }
243 
244   /**
245    * Writes an supported icon.
246    */
247   public void writeIcon(final Icons icon, final CssItem... cssItems) throws IOException {
248     writeIcon(icon, null, cssItems);
249   }
250 
251   /**
252    * Writes an supported icon with explicit style information.
253    */
254   public void writeIcon(final Icons icon, final Style style, final CssItem... cssItems) throws IOException {
255     iconEncoder.encode(this, icon, style, cssItems);
256   }
257 
258   /**
259    * @deprecated Should not be used, because it conflicts with CSP.
260    */
261   @Deprecated
262   public void writeJavascript(final String script) throws IOException {
263     startJavascript();
264     write(script);
265     endJavascript();
266   }
267 
268   /**
269    * @deprecated Should not be used, because it conflicts with CSP.
270    */
271   @Deprecated
272   public void endJavascript() throws IOException {
273 //    write("\n// -->\n"); // todo: for XHMTL we may need
274     endElement(HtmlElements.SCRIPT);
275   }
276 
277   /**
278    * @deprecated Should not be used, because it conflicts with CSP.
279    */
280   @Deprecated
281   public void startJavascript() throws IOException {
282     startElement(HtmlElements.SCRIPT);
283     writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
284   }
285 
286   /**
287    * Write text content. The text will be escaped.
288    */
289   public void writeText(final String text) throws IOException {
290     writeText(text, null);
291   }
292 
293   public String getContentTypeWithCharSet() {
294     String contentType = getContentType();
295     if (contentType == null) {
296       contentType = "text/html";
297     }
298     String characterEncoding = getCharacterEncoding();
299     if (characterEncoding == null) {
300       characterEncoding = "UTF-8";
301     }
302 
303     return contentType + "; charset=" + characterEncoding;
304   }
305 
306   @Override
307   public void startCDATA() throws IOException {
308     write("<![CDATA[");
309   }
310 
311   @Override
312   public void endCDATA() throws IOException {
313     write("]]>");
314   }
315 
316 //  protected abstract void writeNewline() throws IOException;
317 }