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.renderkit.html;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.myfaces.tobago.component.Attributes;
24  import org.apache.myfaces.tobago.component.UICommand;
25  import org.apache.myfaces.tobago.internal.util.Deprecation;
26  import org.apache.myfaces.tobago.internal.webapp.TobagoResponseWriterWrapper;
27  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
28  import org.apache.myfaces.tobago.renderkit.css.Style;
29  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
30  import org.apache.myfaces.tobago.util.ComponentUtils;
31  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import javax.faces.component.UIComponent;
36  import javax.faces.component.UIData;
37  import javax.faces.component.UIInput;
38  import javax.faces.component.UINamingContainer;
39  import javax.faces.context.FacesContext;
40  import javax.faces.context.ResponseWriter;
41  import javax.faces.model.SelectItem;
42  import java.io.IOException;
43  import java.util.List;
44  import java.util.Locale;
45  import java.util.Map;
46  
47  /**
48   * @deprecated Please use HtmlRendererUtils
49   */
50  @Deprecated
51  public final class HtmlRendererUtil {
52  
53    private static final Logger LOG = LoggerFactory.getLogger(HtmlRendererUtil.class);
54    private static final String ERROR_FOCUS_KEY = HtmlRendererUtil.class.getName() + ".ErrorFocusId";
55  
56    private HtmlRendererUtil() {
57      // to prevent instantiation
58    }
59  
60    /**
61     * @deprecated Please use HtmlRendererUtils
62     */
63    @Deprecated
64    private static boolean renderErrorFocusId(final FacesContext facesContext, final UIInput input) throws IOException {
65      if (ComponentUtils.isError(input)) {
66        if (!FacesContext.getCurrentInstance().getExternalContext().getRequestMap().containsKey(ERROR_FOCUS_KEY)) {
67          FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(ERROR_FOCUS_KEY, Boolean.TRUE);
68          final TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
69          final String id = input.getClientId(facesContext);
70          writer.writeJavascript("Tobago.errorFocusId = '" + id + "';");
71          return true;
72        } else {
73          return true;
74        }
75      }
76      return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().containsKey(ERROR_FOCUS_KEY);
77    }
78  
79    /**
80     * @deprecated Please use HtmlRendererUtils
81     */
82    @Deprecated
83    public static void renderFocusId(final FacesContext facesContext, final UIComponent component)
84        throws IOException {
85      if (component instanceof UIInput) {
86        renderFocusId(facesContext, (UIInput) component);
87      }
88    }
89  
90    /**
91     * @deprecated Please use HtmlRendererUtils
92     */
93    @Deprecated
94    public static void renderFocusId(final FacesContext facesContext, final UIInput component)
95        throws IOException {
96      HtmlRendererUtils.renderFocusId(facesContext, component);
97    }
98  
99    /**
100    * @deprecated Please use HtmlRendererUtils
101    */
102   @Deprecated
103   public static void createCssClass(final FacesContext facesContext, final UIComponent component) {
104     HtmlRendererUtils.createCssClass(facesContext, component);
105   }
106 
107   /**
108    * @deprecated Please use HtmlRendererUtils
109    */
110   @Deprecated
111   public static String getRendererName(final FacesContext facesContext, final UIComponent component) {
112     return HtmlRendererUtils.getRendererName(facesContext, component);
113   }
114 
115   /**
116    * @deprecated Please use HtmlRendererUtils
117    */
118   @Deprecated
119   public static void writeLabelWithAccessKey(final TobagoResponseWriter writer, final LabelWithAccessKey label)
120       throws IOException {
121     HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
122   }
123 
124   /**
125    * @deprecated Please use HtmlRendererUtils
126    */
127   @Deprecated
128   public static void setDefaultTransition(final FacesContext facesContext, final boolean transition)
129       throws IOException {
130     writeScriptLoader(facesContext, null, new String[]{"Tobago.transition = " + transition + ";"});
131   }
132 
133   /**
134    * @deprecated Please use HtmlRendererUtils
135    */
136   @Deprecated
137   public static void addClickAcceleratorKey(
138       final FacesContext facesContext, final String clientId, final char key)
139       throws IOException {
140     addClickAcceleratorKey(facesContext, clientId, key, null);
141   }
142 
143   /**
144    * @deprecated Please use HtmlRendererUtils
145    */
146   @Deprecated
147   public static void addClickAcceleratorKey(
148       final FacesContext facesContext, final String clientId, final char key, final String modifier)
149       throws IOException {
150     final String str
151         = createOnclickAcceleratorKeyJsStatement(clientId, key, modifier);
152     writeScriptLoader(facesContext, null, new String[]{str});
153   }
154 
155   /**
156    * @deprecated Please use HtmlRendererUtils
157    */
158   @Deprecated
159   public static void addAcceleratorKey(
160       final FacesContext facesContext, final String func, final char key) throws IOException {
161     addAcceleratorKey(facesContext, func, key, null);
162   }
163 
164   /**
165    * @deprecated Please use HtmlRendererUtils
166    */
167   @Deprecated
168   public static void addAcceleratorKey(
169       final FacesContext facesContext, final String func, final char key, final String modifier)
170       throws IOException {
171     final String str = createAcceleratorKeyJsStatement(func, key, modifier);
172     writeScriptLoader(facesContext, null, new String[]{str});
173   }
174 
175   /**
176    * @deprecated Please use HtmlRendererUtils
177    */
178   @Deprecated
179   public static String createOnclickAcceleratorKeyJsStatement(
180       final String clientId, final char key, final String modifier) {
181     final String func = "Tobago.clickOnElement('" + clientId + "');";
182     return createAcceleratorKeyJsStatement(func, key, modifier);
183   }
184 
185   /**
186    * @deprecated Please use HtmlRendererUtils
187    */
188   @Deprecated
189   public static String createAcceleratorKeyJsStatement(
190       final String func, final char key, final String modifier) {
191     return HtmlRendererUtils.createAcceleratorKeyJsStatement(func, key, modifier);
192   }
193 
194   /**
195    * @deprecated Please use setter;
196    */
197   @Deprecated
198   public static void replaceStyleAttribute(
199       final UIComponent component, final String styleAttribute, final String value) {
200     Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
201   }
202 
203   /**
204    * @deprecated Please use setter;
205    */
206   @Deprecated
207   public static void replaceStyleAttribute(final UIComponent component, final String attribute,
208       final String styleAttribute, final String value) {
209     Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
210   }
211 
212   /**
213    * @deprecated Please use setter;
214    */
215   @Deprecated
216   public static void replaceStyleAttribute(final UIComponent component, final String styleAttribute, final int value) {
217     Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
218   }
219 
220   /**
221    * @deprecated Please use setter;
222    */
223   @Deprecated
224   public static void replaceStyleAttribute(final UIComponent component, final String attribute,
225       final String styleAttribute, final int value) {
226     Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
227 
228   }
229 
230   /**
231    * @deprecated Please use HtmlRendererUtils
232    */
233   @Deprecated
234   private static Style ensureStyleAttributeMap(final UIComponent component) {
235     return ensureStyleAttributeMap(component, Attributes.STYLE);
236   }
237 
238   /**
239    * @deprecated Please use HtmlRendererUtils
240    */
241   @Deprecated
242   private static Style ensureStyleAttributeMap(final UIComponent component, final String attribute) {
243     final Map attributes = component.getAttributes();
244     Style style = (Style) attributes.get(attribute);
245     if (style == null) {
246       style = new Style();
247       attributes.put(attribute, style);
248     }
249     return style;
250   }
251 
252   /**
253    * @deprecated Please use HtmlRendererUtils
254    */
255   @Deprecated
256   public static String replaceStyleAttribute(String style, final String name,
257       final String value) {
258     style = removeStyleAttribute(style != null ? style : "", name);
259     return style + " " + name + ": " + value + ";";
260   }
261 
262   /**
263    * @deprecated Please use HtmlRendererUtils
264    */
265   @Deprecated
266   public static String removeStyleAttribute(final String style, final String name) {
267     if (style == null) {
268       return null;
269     }
270     final String pattern = name + "\\s*?:[^;]*?;";
271     return style.replaceAll(pattern, "").trim();
272   }
273 
274   /**
275    * @deprecated Please use setter;
276    */
277   @Deprecated
278   public static void removeStyleAttribute(final UIComponent component, final String name) {
279     Deprecation.LOG.error("HtmlRendererUtils.removeStyleAttribute() no longer supported. Use setter.");
280   }
281 
282   /**
283    * @deprecated Please use StyleClasses.ensureStyleClasses(component).add(clazz);
284    */
285   @Deprecated
286   public static void addCssClass(final UIComponent component, final String clazz) {
287     StyleClasses.ensureStyleClasses(component).addFullQualifiedClass(clazz);
288   }
289 
290   @Deprecated
291   public static void createHeaderAndBodyStyles(final FacesContext facesContext, final UIComponent component) {
292     Deprecation.LOG.error("HtmlRendererUtils.createHeaderAndBodyStyles() no longer supported");
293   }
294 
295   @Deprecated
296   public static void createHeaderAndBodyStyles(
297       final FacesContext facesContext, final UIComponent component, final boolean width) {
298     Deprecation.LOG.error("HtmlRendererUtils.createHeaderAndBodyStyles() no longer supported");
299   }
300 
301   /**
302    * @deprecated Please use StyleClasses.ensureStyleClasses(component).updateClassAttribute(renderer, component);
303    */
304   @Deprecated
305   public static void updateClassAttribute(
306       final String cssClass, final String rendererName, final UIComponent component) {
307     throw new UnsupportedOperationException(
308         "Please use StyleClasses.ensureStyleClasses(component).updateClassAttribute(renderer, component)");
309   }
310 
311   /**
312    * @deprecated Please use StyleClasses.addMarkupClass()
313    */
314   @Deprecated
315   public static void addMarkupClass(final UIComponent component, final String rendererName,
316       final String subComponent, final StringBuilder tobagoClass) {
317     throw new UnsupportedOperationException("Please use StyleClasses.addMarkupClass()");
318   }
319 
320   /**
321    * @deprecated Please use StyleClasses.addMarkupClass()
322    */
323   @Deprecated
324   public static void addMarkupClass(
325       final UIComponent component, final String rendererName, final StyleClasses classes) {
326     classes.addMarkupClass(component, rendererName);
327   }
328 
329   /**
330    * @deprecated
331    */
332   @Deprecated
333   public static void addImageSources(
334       final FacesContext facesContext, final TobagoResponseWriter writer, final String src, final String id)
335       throws IOException {
336     Deprecation.LOG.error("using deprecated API");
337   }
338 
339   /**
340    * @deprecated Please use HtmlRendererUtils
341    */
342   @Deprecated
343   public static String createSrc(final String src, final String ext) {
344     final int dot = src.lastIndexOf('.');
345     if (dot == -1) {
346       LOG.warn("Image src without extension: '" + src + "'");
347       return src;
348     } else {
349       return src.substring(0, dot) + ext + src.substring(dot);
350     }
351   }
352 
353   /**
354    * @deprecated Please use HtmlRendererUtils
355    */
356   @Deprecated
357   public static TobagoResponseWriter getTobagoResponseWriter(final FacesContext facesContext) {
358 
359     final ResponseWriter writer = facesContext.getResponseWriter();
360     if (writer instanceof TobagoResponseWriter) {
361       return (TobagoResponseWriter) writer;
362     } else {
363       return new TobagoResponseWriterWrapper(writer);
364     }
365   }
366 
367   /**
368    * @deprecated use TobagoResponseWriter.writeJavascript()
369    */
370   @Deprecated
371   public static void writeJavascript(final ResponseWriter writer, final String script) throws IOException {
372     startJavascript(writer);
373     writer.write(script);
374     endJavascript(writer);
375   }
376 
377   /**
378    * @deprecated use TobagoResponseWriter.writeJavascript()
379    */
380   @Deprecated
381   public static void startJavascript(final ResponseWriter writer) throws IOException {
382     writer.startElement(HtmlElements.SCRIPT, null);
383     writer.writeAttribute(HtmlAttributes.TYPE, "text/javascript", null);
384     writer.write("\n<!--\n");
385   }
386 
387   /**
388    * @deprecated use TobagoResponseWriter.writeJavascript()
389    */
390   @Deprecated
391   public static void endJavascript(final ResponseWriter writer) throws IOException {
392     writer.write("\n// -->\n");
393     writer.endElement(HtmlElements.SCRIPT);
394   }
395 
396   /**
397    * @deprecated Please use HtmlRendererUtils
398    */
399   @Deprecated
400   public static void writeScriptLoader(final FacesContext facesContext, final String script)
401       throws IOException {
402     writeScriptLoader(facesContext, new String[]{script}, null);
403   }
404 
405   /**
406    * @deprecated Please use HtmlRendererUtils
407    */
408   @Deprecated
409   public static void writeScriptLoader(
410       final FacesContext facesContext, final String[] scripts, final String[] afterLoadCmds)
411       throws IOException {
412     HtmlRendererUtils.writeScriptLoader(facesContext, scripts, afterLoadCmds);
413   }
414 
415   /**
416    * @deprecated Please use HtmlRendererUtils
417    */
418   @Deprecated
419   public static void writeStyleLoader(
420       final FacesContext facesContext, final String[] styles) throws IOException {
421     HtmlRendererUtils.writeStyleLoader(facesContext, styles);
422   }
423 
424   /**
425    * @deprecated Please use HtmlRendererUtils
426    */
427   @Deprecated
428   public static String getTitleFromTipAndMessages(final FacesContext facesContext, final UIComponent component) {
429     final String messages = ComponentUtils.getFacesMessageAsString(facesContext, component);
430     return HtmlRendererUtil.addTip(messages, component.getAttributes().get(Attributes.TIP));
431   }
432 
433   /**
434    * @deprecated Please use HtmlRendererUtils
435    */
436   @Deprecated
437   public static String addTip(String title, final Object tip) {
438     if (tip != null) {
439       if (title != null && title.length() > 0) {
440         title += " :: ";
441       } else {
442         title = "";
443       }
444       title += tip;
445     }
446     return title;
447   }
448 
449   /**
450    * @deprecated Please use HtmlRendererUtils
451    */
452   @Deprecated
453   public static void renderSelectItems(
454       final UIInput component, final List<SelectItem> items, final Object[] values,
455       final TobagoResponseWriter writer, final FacesContext facesContext) throws IOException {
456     HtmlRendererUtils.renderSelectItems(component, items, values, writer, facesContext);
457   }
458 
459   /**
460    * @deprecated Please use HtmlRendererUtils
461    */
462   @Deprecated
463   public static String getComponentIds(
464       final FacesContext context, final UIComponent component, final String[] componentId) {
465     final StringBuilder sb = new StringBuilder();
466     for (final String id : componentId) {
467       if (!StringUtils.isBlank(id)) {
468         if (sb.length() > 0) {
469           sb.append(",");
470         }
471         final String clientId = getComponentId(context, component, id);
472         if (clientId != null) {
473           sb.append(clientId);
474         }
475       }
476     }
477     return sb.toString();
478   }
479 
480   /**
481    * @deprecated Please use HtmlRendererUtils
482    */
483   @Deprecated
484   public static String getComponentId(
485       final FacesContext context, final UIComponent component, final String componentId) {
486     final UIComponent partiallyComponent = ComponentUtils.findComponent(component, componentId);
487     if (partiallyComponent != null) {
488       final String clientId = partiallyComponent.getClientId(context);
489       if (partiallyComponent instanceof UIData) {
490         final int rowIndex = ((UIData) partiallyComponent).getRowIndex();
491         if (rowIndex >= 0 && clientId.endsWith(Integer.toString(rowIndex))) {
492           return clientId.substring(0, clientId.lastIndexOf(UINamingContainer.getSeparatorChar(context)));
493         }
494       }
495       return clientId;
496     }
497     LOG.error("No Component found for id " + componentId + " search base component " + component.getClientId(context));
498     return null;
499   }
500 
501   /**
502    * @deprecated Please use HtmlRendererUtils
503    */
504   @Deprecated
505   public static String toStyleString(final String key, final Integer value) {
506     final StringBuilder buf = new StringBuilder();
507     buf.append(key);
508     buf.append(":");
509     buf.append(value);
510     buf.append("px; ");
511     return buf.toString();
512   }
513 
514   /**
515    * @deprecated Please use HtmlRendererUtils
516    */
517   @Deprecated
518   public static String toStyleString(final String key, final String value) {
519     final StringBuilder buf = new StringBuilder();
520     buf.append(key);
521     buf.append(":");
522     buf.append(value);
523     buf.append("; ");
524     return buf.toString();
525   }
526 
527   /**
528    * @deprecated Please use HtmlRendererUtils
529    */
530   @Deprecated
531   public static void renderTip(final UIComponent component, final TobagoResponseWriter writer) throws IOException {
532     HtmlRendererUtils.renderTip(component, writer);
533   }
534 
535   /**
536    * @deprecated Please use HtmlRendererUtils
537    */
538   @Deprecated
539   public static void renderImageTip(final UIComponent component, final TobagoResponseWriter writer) throws IOException {
540     HtmlRendererUtils.renderImageTip(component, writer);
541   }
542 
543   /**
544    * @deprecated Please use HtmlRendererUtils
545    */
546   @Deprecated
547   public static String getJavascriptString(final String str) {
548     if (str != null) {
549       return "\"" + str + "\"";
550     }
551     return null;
552   }
553 
554   /**
555    * @deprecated Please use HtmlRendererUtils
556    */
557   @Deprecated
558   public static String getRenderedPartiallyJavascriptArray(final FacesContext facesContext, final UICommand command) {
559     if (command == null) {
560       return null;
561     }
562     final String[] list = command.getRenderedPartially();
563     final StringBuilder strBuilder = new StringBuilder();
564     strBuilder.append("[");
565     for (int i = 0; i < list.length; i++) {
566       if (i != 0) {
567         strBuilder.append(",");
568       }
569       strBuilder.append("\"");
570       strBuilder.append(HtmlRendererUtil.getComponentId(facesContext, command, list[i]));
571       strBuilder.append("\"");
572     }
573     strBuilder.append("]");
574     return strBuilder.toString();
575   }
576 
577   /**
578    * @deprecated Please use HtmlRendererUtils
579    */
580   @Deprecated
581   public static String getJavascriptArray(final String[] list) {
582     final StringBuilder strBuilder = new StringBuilder();
583     strBuilder.append("[");
584     for (int i = 0; i < list.length; i++) {
585       if (i != 0) {
586         strBuilder.append(",");
587       }
588       strBuilder.append("\"");
589       strBuilder.append(list[i]);
590       strBuilder.append("\"");
591     }
592     strBuilder.append("]");
593     return strBuilder.toString();
594   }
595 
596   /**
597    * @deprecated Please use HtmlRendererUtils
598    */
599   @Deprecated
600   public static void renderDojoDndSource(final FacesContext context, final UIComponent component)
601       throws IOException {
602     HtmlRendererUtils.renderDojoDndSource(context, component);
603   }
604 
605   /**
606    * @deprecated Please use HtmlRendererUtils
607    */
608   @Deprecated
609   public static void renderDojoDndItem(
610       final UIComponent component, final TobagoResponseWriter writer, final boolean addStyle)
611       throws IOException {
612     HtmlRendererUtils.renderDojoDndItem(component, writer, addStyle);
613   }
614 
615   /**
616    * @deprecated Please use HtmlRendererUtils
617    */
618   @Deprecated
619   private static String createDojoDndType(final UIComponent component, final String clientId, final String dojoType) {
620     return HtmlRendererUtils.createDojoDndType(component, clientId, dojoType);
621   }
622 
623   /**
624    * @deprecated Please use HtmlRendererUtils
625    */
626   @Deprecated
627   public static void checkForCommandFacet(
628       final UIComponent component, final FacesContext facesContext, final TobagoResponseWriter writer)
629       throws IOException {
630     HtmlRendererUtils.checkForCommandFacet(component, facesContext, writer);
631   }
632 
633   /**
634    * @deprecated Please use HtmlRendererUtils
635    */
636   @Deprecated
637   public static void checkForCommandFacet(
638       final UIComponent component, final List<String> clientIds, final FacesContext facesContext,
639       final TobagoResponseWriter writer) throws IOException {
640     HtmlRendererUtils.checkForCommandFacet(component, clientIds, facesContext, writer);
641   }
642 
643   /**
644    * @deprecated Please use HtmlRendererUtils
645    */
646   @Deprecated
647   public static void removeStyleClasses(final UIComponent cell) {
648     final Object obj = cell.getAttributes().get(Attributes.STYLE_CLASS);
649     if (obj != null && obj instanceof StyleClasses && cell.getRendererType() != null) {
650       final StyleClasses styleClasses = (StyleClasses) obj;
651       if (!styleClasses.isEmpty()) {
652         final String rendererName = cell.getRendererType().substring(0, 1).toLowerCase(Locale.ENGLISH)
653             + cell.getRendererType().substring(1);
654         styleClasses.removeTobagoClasses(rendererName);
655       }
656       if (styleClasses.isEmpty()) {
657         cell.getAttributes().remove(Attributes.STYLE_CLASS);
658       }
659     }
660   }
661 }