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>
41   * This provides an alternative ResponseWriter interfaces, which allows optimizations.
42   * E. g. some attributes needed to to be escaped.
43   * </p>
44   */
45  public abstract class TobagoResponseWriter extends ResponseWriter {
46  
47    private IconEncoder iconEncoder = new FontAwesomeIconEncoder();
48  
49    // same as in ResponseWriter
50  
51    /**
52     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
53     */
54    @Deprecated
55    @Override
56    public abstract void startElement(String name, UIComponent component) throws IOException;
57  
58    public abstract void startElement(HtmlElements name) throws IOException;
59  
60    /**
61     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
62     */
63    @Deprecated
64    @Override
65    public abstract void endElement(String name) throws IOException;
66      
67    public abstract void endElement(HtmlElements name) throws IOException;
68  
69    @Override
70    public abstract void write(String string) throws IOException;
71  
72    @Override
73    public abstract void writeComment(Object comment) throws IOException;
74  
75    @Override
76    public abstract ResponseWriter cloneWithWriter(Writer writer);
77  
78    /**
79     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
80     */
81    @Override
82    @Deprecated
83    public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
84  
85    /**
86     * @deprecated Should not directly called via this interface. There is be a special method which might 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,
106    * if it is not 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) throws IOException {
192     final StringBuilder builder = new StringBuilder();
193     boolean render = false;
194     if (first != null) {
195       builder.append(first.getName());
196       builder.append(' ');
197       render = true;
198     }
199     if (second != null) {
200       render |= writeCssItem(builder, second);
201     }
202     if (third != null) {
203       render |= writeCssItem(builder, third);
204     }
205     if (fourth != null) {
206       render |= writeCssItem(builder, fourth);
207     }
208     if (fifth != null) {
209       render |= writeCssItem(builder, fifth);
210     }
211     if (sixth != null) {
212       render |= writeCssItem(builder, sixth);
213     }
214     if (render) {
215       writeAttribute(HtmlAttributes.CLASS, builder.deleteCharAt(builder.length() - 1).toString(), false);
216     }
217   }
218 
219   private boolean writeCssItem(final StringBuilder builder, final CssItem... cssItems) {
220     boolean render = false;
221     for (CssItem cssItem : cssItems) {
222       if (cssItem != null) {
223         builder.append(cssItem.getName());
224         builder.append(' ');
225         render = true;
226       }
227     }
228     return render;
229   }
230 
231   /**
232    * Write the style attribute. The value may be escaped (depending of the content).
233    * @deprecated since 4.0.0. UIStyle now renders itself.
234    */
235   @Deprecated
236   public void writeStyleAttribute(final Style style) throws IOException {
237     Deprecation.LOG.warn("ignoring style: {}", style);
238   }
239 
240   /**
241    * Writes an supported icon.
242    *
243    * @deprecated 4.0.0. Use normal rendering methods.
244    */
245   @Deprecated
246   public void writeIcon(final Icons icon, final CssItem... cssItems) throws IOException {
247     iconEncoder.encode(this, icon, cssItems);
248   }
249 
250   /**
251    * Write text content. The text will be escaped.
252    */
253   public void writeText(final String text) throws IOException {
254     writeText(text, null);
255   }
256 
257   public String getContentTypeWithCharSet() {
258     String contentType = getContentType();
259     if (contentType == null) {
260       contentType = "text/html";
261     }
262     String characterEncoding = getCharacterEncoding();
263     if (characterEncoding == null) {
264       characterEncoding = "UTF-8";
265     }
266 
267     return contentType + "; charset=" + characterEncoding;
268   }
269 
270   @Override
271   public void startCDATA() throws IOException {
272     write("<![CDATA[");
273   }
274 
275   @Override
276   public void endCDATA() throws IOException {
277     write("]]>");
278   }
279 
280 //  protected abstract void writeNewline() throws IOException;
281 }