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