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.ajax.AjaxUtils;
23  import org.apache.myfaces.tobago.renderkit.css.Classes;
24  import org.apache.myfaces.tobago.renderkit.css.Style;
25  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
27  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  import java.io.IOException;
33  import java.io.Writer;
34  
35  /**
36   * This provides an alternative ResponseWriter interfaces, which allows optimizations.
37   * E. g. some attributes needed to to be escaped.
38   * <p/>
39   */
40  public abstract class TobagoResponseWriter extends ResponseWriter {
41  
42    // same as in ResponseWriter
43  
44    @Override
45    public abstract void startElement(String name, UIComponent component) throws IOException;
46  
47    /**
48     * @deprecated Use {@link #startElement(String, UIComponent) startElement(name, null)} instead.
49     */
50    @Deprecated
51    public void startElement(final String name) throws IOException {
52      startElement(name, null);
53    }
54  
55    @Override
56    public abstract void endElement(String name) throws IOException;
57      
58    public abstract void write(String string) throws IOException;
59  
60    @Override
61    public abstract void writeComment(Object comment) throws IOException;
62  
63    @Override
64    public abstract ResponseWriter cloneWithWriter(Writer writer);
65  
66    /**
67     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
68     */
69    @Deprecated
70    public abstract void writeAttribute(String name, Object value, final String property) throws IOException;
71  
72    /**
73     * @deprecated Should not directly called via this interface. There is be a special method which might be better.
74     */
75    @Deprecated
76    public abstract void writeText(Object text, String property) throws IOException;
77  
78    @Override
79    public abstract void flush() throws IOException;
80  
81    // others (not from ResponseWriter)
82  
83    /**
84     * Writes a string attribute. The renderer may set escape=false to switch of escaping of the string,
85     * if it is not necessary.
86     */
87    public abstract void writeAttribute(String name, String string, boolean escape) throws IOException;
88  
89    /**
90     * Writes a boolean attribute. The value will not escaped.
91     */
92    public void writeAttribute(final String name, final boolean on) throws IOException {
93      if (on) {
94        writeAttribute(name, name, false);
95      }
96    }
97  
98    /**
99     * Writes a integer attribute. The value will not escaped.
100    */
101   public void writeAttribute(final String name, final int number) throws IOException {
102     writeAttribute(name, Integer.toString(number), false);
103   }
104 
105   /**
106    * Writes a property as attribute. The value will be escaped.
107    * @deprecated since Tobago 2.0.9
108    */
109   @Deprecated
110   public void writeAttributeFromComponent(final String name, final String property) throws IOException {
111     writeAttribute(name, null, property);
112   }
113 
114   /**
115    * Write the id attribute. The value will not escaped.
116    */
117   public void writeIdAttribute(final String id) throws IOException {
118     writeAttribute(HtmlAttributes.ID, id, false);
119   }
120 
121   /**
122    * Write the name attribute. The value will not escaped.
123    */
124   public void writeNameAttribute(final String name) throws IOException {
125     writeAttribute(HtmlAttributes.NAME, name, false);
126   }
127 
128   /**
129    * Write the class attribute. The value will not escaped.
130    * @deprecated since Tobago 1.5.0
131    */
132   @Deprecated
133   public void writeClassAttribute(final String cssClass) throws IOException {
134     writeAttribute(HtmlAttributes.CLASS, cssClass, false);
135   }
136 
137   /**
138    * Write the class attribute. The value will not escaped.
139    * @param classes The abstract representation of the css class string, normally created by the renderer.
140    */
141   public void writeClassAttribute(final Classes classes) throws IOException {
142     writeAttribute(HtmlAttributes.CLASS, classes.getStringValue(), false);
143   }
144 
145   @Deprecated
146   public abstract String getStyleClasses();
147 
148   /**
149    * Write the class attribute. The value will not escaped.
150    * @deprecated since Tobago 1.5.0
151    */
152   @Deprecated
153   public abstract void writeClassAttribute() throws IOException;
154 
155   /**
156    * Write the style attribute. The value may be escaped (depending of the content).
157    */
158   public void writeStyleAttribute(final Style style) throws IOException {
159     if (style != null) {
160       final String json = style.encodeJson();
161       if (json.length() > 2) { // empty "{}" needs not to be written
162         final FacesContext facesContext = FacesContext.getCurrentInstance();
163         writeAttribute(DataAttributes.STYLE, json, style.needsToBeEscaped() || AjaxUtils.isAjaxRequest(facesContext));
164         // in case of AJAX we need to escape the " as long we use
165         // org.apache.myfaces.tobago.internal.webapp.JsonResponseWriter
166       }
167     }
168   }
169 
170   /**
171    * Write the style attribute. The value will not escaped.
172    * @deprecated since 1.5.0, use writeStyleAttribute(Style) instead.
173    */
174   @Deprecated
175   public void writeStyleAttribute(final String style) throws IOException {
176     writeAttribute(HtmlAttributes.STYLE, style, false);
177   }
178 
179   /**
180    * @deprecated Should not be used, because it conflicts with CSP.
181    */
182   @Deprecated
183   public void writeJavascript(final String script) throws IOException {
184     startJavascript();
185     write(script);
186     endJavascript();
187   }
188 
189   /**
190    * @deprecated Should not be used, because it conflicts with CSP.
191    */
192   @Deprecated
193   public void endJavascript() throws IOException {
194 //    write("\n// -->\n"); // todo: for XHMTL we may need
195     endElement(HtmlElements.SCRIPT);
196   }
197 
198   /**
199    * @deprecated Should not be used, because it conflicts with CSP.
200    */
201   @Deprecated
202   public void startJavascript() throws IOException {
203     startElement(HtmlElements.SCRIPT, null);
204     writeAttribute(HtmlAttributes.TYPE, "text/javascript", false);
205     flush(); // is needed in some cases, e. g. TOBAGO-1094
206 //    write("\n<!--\n");
207   }
208 
209   /**
210    * Write text content. The text will be escaped.
211    */
212   public void writeText(final String text) throws IOException {
213     writeText(text, null);
214   }
215 
216   /**
217    * Writes a property as text. The text will be escaped.
218    */
219   public void writeTextFromComponent(final String property) throws IOException {
220     writeText(null, property);
221   }
222 
223   public String getContentTypeWithCharSet() {
224     String contentType = getContentType();
225     if (contentType == null) {
226       contentType = "text/html";
227     }
228     String characterEncoding = getCharacterEncoding();
229     if (characterEncoding == null) {
230       characterEncoding = "UTF-8";
231     }
232 
233     final StringBuilder builder = new StringBuilder(contentType);
234     builder.append("; charset=");
235     builder.append(characterEncoding);
236     return builder.toString();
237 
238   }
239 }