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