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     if (others != null) {
212       for (CssItem other : others) {
213         if (other != null) {
214           builder.append(other.getName());
215           builder.append(' ');
216           render = true;
217         }
218       }
219     }
220     if (render) {
221       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
222     }
223   }
224 
225   /**
226    * Write the style attribute. The value may be escaped (depending of the content).
227    */
228   public void writeStyleAttribute(final Style style) throws IOException {
229     if (style != null) {
230       final String json = style.encodeJson();
231       if (json.length() > 2) { // empty "{}" needs not to be written
232         writeAttribute(DataAttributes.STYLE, json, style.needsToBeEscaped());
233       }
234     }
235   }
236 
237   /**
238    * Write the style attribute. The value will not escaped.
239    * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead.
240    */
241   @Deprecated
242   public void writeStyleAttribute(final String style) throws IOException {
243     writeAttribute(HtmlAttributes.STYLE, style, false);
244   }
245 
246   /**
247    * Writes an supported icon.
248    */
249   public void writeIcon(final Icons icon, final CssItem... cssItems) throws IOException {
250     writeIcon(icon, null, cssItems);
251   }
252 
253   /**
254    * Writes an supported icon with explicit style information.
255    */
256   public void writeIcon(final Icons icon, final Style style, final CssItem... cssItems) throws IOException {
257     iconEncoder.encode(this, icon, style, cssItems);
258   }
259 
260   /**
261    * @deprecated Should not be used, because it conflicts with CSP.
262    */
263   @Deprecated
264   public void writeJavascript(final String script) throws IOException {
265     startJavascript();
266     write(script);
267     endJavascript();
268   }
269 
270   /**
271    * @deprecated Should not be used, because it conflicts with CSP.
272    */
273   @Deprecated
274   public void endJavascript() throws IOException {
275 //    write("\n// -->\n"); // todo: for XHMTL we may need
276     endElement(HtmlElements.SCRIPT);
277   }
278 
279   /**
280    * @deprecated Should not be used, because it conflicts with CSP.
281    */
282   @Deprecated
283   public void startJavascript() throws IOException {
284     startElement(HtmlElements.SCRIPT);
285     writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
286   }
287 
288   /**
289    * Write text content. The text will be escaped.
290    */
291   public void writeText(final String text) throws IOException {
292     writeText(text, null);
293   }
294 
295   public String getContentTypeWithCharSet() {
296     String contentType = getContentType();
297     if (contentType == null) {
298       contentType = "text/html";
299     }
300     String characterEncoding = getCharacterEncoding();
301     if (characterEncoding == null) {
302       characterEncoding = "UTF-8";
303     }
304 
305     return contentType + "; charset=" + characterEncoding;
306   }
307 
308   @Override
309   public void startCDATA() throws IOException {
310     write("<![CDATA[");
311   }
312 
313   @Override
314   public void endCDATA() throws IOException {
315     write("]]>");
316   }
317 
318 //  protected abstract void writeNewline() throws IOException;
319 }