1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.component.UIPage;
26 import org.apache.myfaces.tobago.context.ResourceManagerUtils;
27 import org.apache.myfaces.tobago.internal.util.Deprecation;
28 import org.apache.myfaces.tobago.internal.util.FacesContextUtils;
29 import org.apache.myfaces.tobago.internal.webapp.TobagoResponseWriterWrapper;
30 import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
31 import org.apache.myfaces.tobago.renderkit.css.Style;
32 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
33 import org.apache.myfaces.tobago.util.ComponentUtils;
34 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 import javax.faces.component.NamingContainer;
39 import javax.faces.component.UIComponent;
40 import javax.faces.component.UIData;
41 import javax.faces.component.UIInput;
42 import javax.faces.context.FacesContext;
43 import javax.faces.context.ResponseWriter;
44 import javax.faces.model.SelectItem;
45 import java.io.IOException;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Map;
49
50
51
52
53 @Deprecated
54 public final class HtmlRendererUtil {
55
56 private static final Logger LOG = LoggerFactory.getLogger(HtmlRendererUtil.class);
57 private static final String ERROR_FOCUS_KEY = HtmlRendererUtil.class.getName() + ".ErrorFocusId";
58
59 private HtmlRendererUtil() {
60
61 }
62
63
64
65
66 @Deprecated
67 private static boolean renderErrorFocusId(final FacesContext facesContext, final UIInput input) throws IOException {
68 if (ComponentUtils.isError(input)) {
69 if (!FacesContext.getCurrentInstance().getExternalContext().getRequestMap().containsKey(ERROR_FOCUS_KEY)) {
70 FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put(ERROR_FOCUS_KEY, Boolean.TRUE);
71 TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
72 String id = input.getClientId(facesContext);
73 writer.writeJavascript("Tobago.errorFocusId = '" + id + "';");
74 return true;
75 } else {
76 return true;
77 }
78 }
79 return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().containsKey(ERROR_FOCUS_KEY);
80 }
81
82
83
84
85 @Deprecated
86 public static void renderFocusId(final FacesContext facesContext, final UIComponent component)
87 throws IOException {
88 if (component instanceof UIInput) {
89 renderFocusId(facesContext, (UIInput) component);
90 }
91 }
92
93
94
95
96 @Deprecated
97 public static void renderFocusId(final FacesContext facesContext, final UIInput component)
98 throws IOException {
99 if (renderErrorFocusId(facesContext, component)) {
100 return;
101 }
102 if (ComponentUtils.getBooleanAttribute(component, Attributes.FOCUS)) {
103 UIPage page = (UIPage) ComponentUtils.findPage(facesContext, component);
104 String id = component.getClientId(facesContext);
105 if (!StringUtils.isBlank(page.getFocusId()) && !page.getFocusId().equals(id)) {
106 LOG.warn("page focusId = \"" + page.getFocusId() + "\" ignoring new value \""
107 + id + "\"");
108 } else {
109 TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
110 writer.writeJavascript("Tobago.focusId = '" + id + "';");
111 }
112 }
113 }
114
115
116
117
118 @Deprecated
119 public static void createCssClass(FacesContext facesContext, UIComponent component) {
120 String rendererName = getRendererName(facesContext, component);
121 if (rendererName != null) {
122 StyleClasses classes = StyleClasses.ensureStyleClasses(component);
123 classes.updateClassAttributeAndMarkup(component, rendererName);
124 }
125 }
126
127
128
129
130 @Deprecated
131 public static String getRendererName(FacesContext facesContext, UIComponent component) {
132 return HtmlRendererUtils.getRendererName(facesContext, component);
133 }
134
135
136
137
138 @Deprecated
139 public static void writeLabelWithAccessKey(TobagoResponseWriter writer, LabelWithAccessKey label)
140 throws IOException {
141 int pos = label.getPos();
142 String text = label.getText();
143 if (pos == -1) {
144 writer.writeText(text);
145 } else {
146 writer.writeText(text.substring(0, pos));
147 writer.startElement(HtmlElements.U, null);
148 writer.writeText(Character.toString(text.charAt(pos)));
149 writer.endElement(HtmlElements.U);
150 writer.writeText(text.substring(pos + 1));
151 }
152 }
153
154
155
156
157 @Deprecated
158 public static void setDefaultTransition(FacesContext facesContext, boolean transition)
159 throws IOException {
160 writeScriptLoader(facesContext, null, new String[]{"Tobago.transition = " + transition + ";"});
161 }
162
163
164
165
166 @Deprecated
167 public static void addClickAcceleratorKey(
168 FacesContext facesContext, String clientId, char key)
169 throws IOException {
170 addClickAcceleratorKey(facesContext, clientId, key, null);
171 }
172
173
174
175
176 @Deprecated
177 public static void addClickAcceleratorKey(
178 FacesContext facesContext, String clientId, char key, String modifier)
179 throws IOException {
180 String str
181 = createOnclickAcceleratorKeyJsStatement(clientId, key, modifier);
182 writeScriptLoader(facesContext, null, new String[]{str});
183 }
184
185
186
187
188 @Deprecated
189 public static void addAcceleratorKey(
190 FacesContext facesContext, String func, char key) throws IOException {
191 addAcceleratorKey(facesContext, func, key, null);
192 }
193
194
195
196
197 @Deprecated
198 public static void addAcceleratorKey(
199 FacesContext facesContext, String func, char key, String modifier)
200 throws IOException {
201 String str = createAcceleratorKeyJsStatement(func, key, modifier);
202 writeScriptLoader(facesContext, null, new String[]{str});
203 }
204
205
206
207
208 @Deprecated
209 public static String createOnclickAcceleratorKeyJsStatement(
210 String clientId, char key, String modifier) {
211 String func = "Tobago.clickOnElement('" + clientId + "');";
212 return createAcceleratorKeyJsStatement(func, key, modifier);
213 }
214
215
216
217
218 @Deprecated
219 public static String createAcceleratorKeyJsStatement(
220 String func, char key, String modifier) {
221 StringBuilder buffer = new StringBuilder("new Tobago.AcceleratorKey(function() {");
222 buffer.append(func);
223 if (!func.endsWith(";")) {
224 buffer.append(';');
225 }
226 buffer.append("}, \"");
227 buffer.append(key);
228 if (modifier != null) {
229 buffer.append("\", \"");
230 buffer.append(modifier);
231 }
232 buffer.append("\");");
233 return buffer.toString();
234 }
235
236
237
238
239 @Deprecated
240 public static void replaceStyleAttribute(UIComponent component, String styleAttribute, String value) {
241 Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
242 }
243
244
245
246
247 @Deprecated
248 public static void replaceStyleAttribute(UIComponent component, String attribute,
249 String styleAttribute, String value) {
250 Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
251 }
252
253
254
255
256 @Deprecated
257 public static void replaceStyleAttribute(UIComponent component, String styleAttribute, int value) {
258 Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
259 }
260
261
262
263
264 @Deprecated
265 public static void replaceStyleAttribute(UIComponent component, String attribute,
266 String styleAttribute, int value) {
267 Deprecation.LOG.error("HtmlRendererUtils.replaceStyleAttribute() no longer supported. Use setter.");
268
269 }
270
271
272
273
274 @Deprecated
275 private static Style ensureStyleAttributeMap(UIComponent component) {
276 return ensureStyleAttributeMap(component, Attributes.STYLE);
277 }
278
279
280
281
282 @Deprecated
283 private static Style ensureStyleAttributeMap(UIComponent component, String attribute) {
284 final Map attributes = component.getAttributes();
285 Style style = (Style) attributes.get(attribute);
286 if (style == null) {
287 style = new Style();
288 attributes.put(attribute, style);
289 }
290 return style;
291 }
292
293
294
295
296 @Deprecated
297 public static String replaceStyleAttribute(String style, String name,
298 String value) {
299 style = removeStyleAttribute(style != null ? style : "", name);
300 return style + " " + name + ": " + value + ";";
301 }
302
303
304
305
306 @Deprecated
307 public static String removeStyleAttribute(String style, String name) {
308 if (style == null) {
309 return null;
310 }
311 String pattern = name + "\\s*?:[^;]*?;";
312 return style.replaceAll(pattern, "").trim();
313 }
314
315
316
317
318 @Deprecated
319 public static void removeStyleAttribute(UIComponent component, String name) {
320 Deprecation.LOG.error("HtmlRendererUtils.removeStyleAttribute() no longer supported. Use setter.");
321 }
322
323
324
325
326 @Deprecated
327 public static void addCssClass(UIComponent component, String clazz) {
328 StyleClasses.ensureStyleClasses(component).addFullQualifiedClass(clazz);
329 }
330
331 @Deprecated
332 public static void createHeaderAndBodyStyles(FacesContext facesContext, UIComponent component) {
333 Deprecation.LOG.error("HtmlRendererUtils.createHeaderAndBodyStyles() no longer supported");
334 }
335
336 @Deprecated
337 public static void createHeaderAndBodyStyles(FacesContext facesContext, UIComponent component, boolean width) {
338 Deprecation.LOG.error("HtmlRendererUtils.createHeaderAndBodyStyles() no longer supported");
339 }
340
341
342
343
344 @Deprecated
345 public static void updateClassAttribute(String cssClass, String rendererName, UIComponent component) {
346 throw new UnsupportedOperationException(
347 "Please use StyleClasses.ensureStyleClasses(component).updateClassAttribute(renderer, component)");
348 }
349
350
351
352
353 @Deprecated
354 public static void addMarkupClass(UIComponent component, String rendererName,
355 String subComponent, StringBuilder tobagoClass) {
356 throw new UnsupportedOperationException("Please use StyleClasses.addMarkupClass()");
357 }
358
359
360
361
362 @Deprecated
363 public static void addMarkupClass(UIComponent component, String rendererName, StyleClasses classes) {
364 classes.addMarkupClass(component, rendererName);
365 }
366
367
368
369
370 @Deprecated
371 public static void addImageSources(FacesContext facesContext, TobagoResponseWriter writer, String src, String id)
372 throws IOException {
373 Deprecation.LOG.error("using deprecated API");
374 }
375
376
377
378
379 @Deprecated
380 public static String createSrc(String src, String ext) {
381 int dot = src.lastIndexOf('.');
382 if (dot == -1) {
383 LOG.warn("Image src without extension: '" + src + "'");
384 return src;
385 } else {
386 return src.substring(0, dot) + ext + src.substring(dot);
387 }
388 }
389
390
391
392
393 @Deprecated
394 public static TobagoResponseWriter getTobagoResponseWriter(FacesContext facesContext) {
395
396 ResponseWriter writer = facesContext.getResponseWriter();
397 if (writer instanceof TobagoResponseWriter) {
398 return (TobagoResponseWriter) writer;
399 } else {
400 return new TobagoResponseWriterWrapper(writer);
401 }
402 }
403
404
405
406
407 @Deprecated
408 public static void writeJavascript(ResponseWriter writer, String script) throws IOException {
409 startJavascript(writer);
410 writer.write(script);
411 endJavascript(writer);
412 }
413
414
415
416
417 @Deprecated
418 public static void startJavascript(ResponseWriter writer) throws IOException {
419 writer.startElement(HtmlElements.SCRIPT, null);
420 writer.writeAttribute(HtmlAttributes.TYPE, "text/javascript", null);
421 writer.write("\n<!--\n");
422 }
423
424
425
426
427 @Deprecated
428 public static void endJavascript(ResponseWriter writer) throws IOException {
429 writer.write("\n// -->\n");
430 writer.endElement(HtmlElements.SCRIPT);
431 }
432
433
434
435
436 @Deprecated
437 public static void writeScriptLoader(FacesContext facesContext, String script)
438 throws IOException {
439 writeScriptLoader(facesContext, new String[]{script}, null);
440 }
441
442
443
444
445 @Deprecated
446 public static void writeScriptLoader(FacesContext facesContext, String[] scripts, String[] afterLoadCmds)
447 throws IOException {
448 TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
449
450 String allScripts = "[]";
451 if (scripts != null) {
452 allScripts = ResourceManagerUtils.getScriptsAsJSArray(facesContext, scripts);
453 }
454 boolean ajax = false;
455
456 ajax = FacesContextUtils.isAjax(facesContext);
457
458 writer.startJavascript();
459 writer.write("new Tobago.ScriptLoader(");
460 if (!ajax) {
461 writer.write("\n ");
462 }
463 writer.write(allScripts);
464
465 if (afterLoadCmds != null && afterLoadCmds.length > 0) {
466 writer.write(", ");
467 if (!ajax) {
468 writer.write("\n");
469 }
470 boolean first = true;
471 for (String afterLoadCmd : afterLoadCmds) {
472 String[] splittedStrings = StringUtils.split(afterLoadCmd, '\n');
473 for (String splitted : splittedStrings) {
474 writer.write(first ? " " : " + ");
475 writer.write("\"");
476 String cmd = StringUtils.replace(splitted, "\\", "\\\\");
477 cmd = StringUtils.replace(cmd, "\"", "\\\"");
478 writer.write(cmd);
479 writer.write("\"");
480 if (!ajax) {
481 writer.write("\n");
482 }
483 first = false;
484 }
485 }
486 }
487 writer.write(");");
488
489 writer.endJavascript();
490 }
491
492
493
494
495 @Deprecated
496 public static void writeStyleLoader(
497 FacesContext facesContext, String[] styles) throws IOException {
498 TobagoResponseWriter writer = HtmlRendererUtil.getTobagoResponseWriter(facesContext);
499
500 writer.startJavascript();
501 writer.write("Tobago.ensureStyleFiles(");
502 writer.write(ResourceManagerUtils.getStylesAsJSArray(facesContext, styles));
503 writer.write(");");
504 writer.endJavascript();
505 }
506
507
508
509
510 @Deprecated
511 public static String getTitleFromTipAndMessages(FacesContext facesContext, UIComponent component) {
512 String messages = ComponentUtils.getFacesMessageAsString(facesContext, component);
513 return HtmlRendererUtil.addTip(messages, component.getAttributes().get(Attributes.TIP));
514 }
515
516
517
518
519 @Deprecated
520 public static String addTip(String title, Object tip) {
521 if (tip != null) {
522 if (title != null && title.length() > 0) {
523 title += " :: ";
524 } else {
525 title = "";
526 }
527 title += tip;
528 }
529 return title;
530 }
531
532
533
534
535 @Deprecated
536 public static void renderSelectItems(
537 UIInput component, List<SelectItem> items, Object[] values,
538 TobagoResponseWriter writer, FacesContext facesContext) throws IOException {
539 HtmlRendererUtils.renderSelectItems(component, items, values, writer, facesContext);
540 }
541
542
543
544
545 @Deprecated
546 public static String getComponentIds(FacesContext context, UIComponent component, String[] componentId) {
547 StringBuilder sb = new StringBuilder();
548 for (String id : componentId) {
549 if (!StringUtils.isBlank(id)) {
550 if (sb.length() > 0) {
551 sb.append(",");
552 }
553 String clientId = getComponentId(context, component, id);
554 if (clientId != null) {
555 sb.append(clientId);
556 }
557 }
558 }
559 return sb.toString();
560 }
561
562
563
564
565 @Deprecated
566 public static String getComponentId(FacesContext context, UIComponent component, String componentId) {
567 UIComponent partiallyComponent = ComponentUtils.findComponent(component, componentId);
568 if (partiallyComponent != null) {
569 String clientId = partiallyComponent.getClientId(context);
570 if (partiallyComponent instanceof UIData) {
571 int rowIndex = ((UIData) partiallyComponent).getRowIndex();
572 if (rowIndex >= 0 && clientId.endsWith(Integer.toString(rowIndex))) {
573 return clientId.substring(0, clientId.lastIndexOf(NamingContainer.SEPARATOR_CHAR));
574 }
575 }
576 return clientId;
577 }
578 LOG.error("No Component found for id " + componentId + " search base component " + component.getClientId(context));
579 return null;
580 }
581
582
583
584
585 @Deprecated
586 public static String toStyleString(String key, Integer value) {
587 StringBuilder buf = new StringBuilder();
588 buf.append(key);
589 buf.append(":");
590 buf.append(value);
591 buf.append("px; ");
592 return buf.toString();
593 }
594
595
596
597
598 @Deprecated
599 public static String toStyleString(String key, String value) {
600 StringBuilder buf = new StringBuilder();
601 buf.append(key);
602 buf.append(":");
603 buf.append(value);
604 buf.append("; ");
605 return buf.toString();
606 }
607
608
609
610
611 @Deprecated
612 public static void renderTip(UIComponent component, TobagoResponseWriter writer) throws IOException {
613 HtmlRendererUtils.renderTip(component, writer);
614 }
615
616
617
618
619 @Deprecated
620 public static void renderImageTip(UIComponent component, TobagoResponseWriter writer) throws IOException {
621 HtmlRendererUtils.renderImageTip(component, writer);
622 }
623
624
625
626
627 @Deprecated
628 public static String getJavascriptString(String str) {
629 if (str != null) {
630 return "\"" + str + "\"";
631 }
632 return null;
633 }
634
635
636
637
638 @Deprecated
639 public static String getRenderedPartiallyJavascriptArray(FacesContext facesContext, UICommand command) {
640 if (command == null) {
641 return null;
642 }
643 String[] list = command.getRenderedPartially();
644 StringBuilder strBuilder = new StringBuilder();
645 strBuilder.append("[");
646 for (int i = 0; i < list.length; i++) {
647 if (i != 0) {
648 strBuilder.append(",");
649 }
650 strBuilder.append("\"");
651 strBuilder.append(HtmlRendererUtil.getComponentId(facesContext, command, list[i]));
652 strBuilder.append("\"");
653 }
654 strBuilder.append("]");
655 return strBuilder.toString();
656 }
657
658
659
660
661 @Deprecated
662 public static String getJavascriptArray(String[] list) {
663 StringBuilder strBuilder = new StringBuilder();
664 strBuilder.append("[");
665 for (int i = 0; i < list.length; i++) {
666 if (i != 0) {
667 strBuilder.append(",");
668 }
669 strBuilder.append("\"");
670 strBuilder.append(list[i]);
671 strBuilder.append("\"");
672 }
673 strBuilder.append("]");
674 return strBuilder.toString();
675 }
676
677
678
679
680 @Deprecated
681 public static void renderDojoDndSource(FacesContext context, UIComponent component)
682 throws IOException {
683 Object objDojoType = component.getAttributes().get("dojoType");
684 if (null != objDojoType && (objDojoType.equals("dojo.dnd.Source") || objDojoType.equals("dojo.dnd.Target"))) {
685 FacesContextUtils.addOnloadScript(context, createDojoDndType(component,
686 component.getClientId(context), String.valueOf(objDojoType)));
687 }
688 }
689
690
691
692
693 @Deprecated
694 public static void renderDojoDndItem(UIComponent component, TobagoResponseWriter writer, boolean addStyle)
695 throws IOException {
696 Object objDndType = component.getAttributes().get("dndType");
697 if (objDndType != null) {
698 writer.writeAttribute("dndType", String.valueOf(objDndType), false);
699 }
700 Object objDndData = component.getAttributes().get("dndData");
701 if (objDndData != null) {
702 writer.writeAttribute("dndData", String.valueOf(objDndData), false);
703 }
704 if (addStyle && (null != objDndType || null != objDndData)) {
705 StyleClasses styles = StyleClasses.ensureStyleClasses(component);
706 styles.addFullQualifiedClass("dojoDndItem");
707 }
708 }
709
710
711
712
713 @Deprecated
714 private static String createDojoDndType(UIComponent component, String clientId, String dojoType) {
715 StringBuilder strBuilder = new StringBuilder();
716 strBuilder.append("new ").append(dojoType).append("('").append(clientId).append("'");
717 StringBuilder parameter = new StringBuilder();
718
719 Object objHorizontal = component.getAttributes().get("horizontal");
720 if (objHorizontal != null) {
721 parameter.append("horizontal: ").append(String.valueOf(objHorizontal)).append(",");
722 }
723 Object objCopyOnly = component.getAttributes().get("copyOnly");
724 if (objCopyOnly != null) {
725 parameter.append("copyOnly: ").append(String.valueOf(objCopyOnly)).append(",");
726 }
727 Object objSkipForm = component.getAttributes().get("skipForm");
728 if (objSkipForm != null) {
729 parameter.append("skipForm: ").append(String.valueOf(objSkipForm)).append(",");
730 }
731 Object objWithHandles = component.getAttributes().get("withHandles");
732 if (objWithHandles != null) {
733 parameter.append("withHandles: ").append(String.valueOf(objWithHandles)).append(",");
734 }
735 Object objAccept = component.getAttributes().get("accept");
736 if (objAccept != null) {
737 String accept = null;
738 if (objAccept instanceof String[]) {
739 String[] allowed = (String[]) objAccept;
740 if (allowed.length > 1) {
741
742 accept = "'" + allowed[0] + "'";
743 for (int i = 1; i < allowed.length; i++) {
744 accept += ",'" + allowed[i] + "'";
745 }
746 }
747 } else {
748 accept = (String) objAccept;
749 }
750 parameter.append("accept: [").append(accept).append("],");
751 }
752 Object objSingular = component.getAttributes().get("singular");
753 if (objSingular != null) {
754 parameter.append("singular: ").append(String.valueOf(objSingular)).append(",");
755 }
756 Object objCreator = component.getAttributes().get("creator");
757 if (objCreator != null) {
758 parameter.append("creator: ").append(String.valueOf(objCreator)).append(",");
759 }
760 if (parameter.length() > 0) {
761 parameter.deleteCharAt(parameter.lastIndexOf(","));
762 strBuilder.append(",{").append(parameter).append("}");
763 }
764 strBuilder.append(");");
765 return strBuilder.toString();
766 }
767
768
769
770
771 @Deprecated
772 public static void checkForCommandFacet(UIComponent component, FacesContext facesContext, TobagoResponseWriter writer)
773 throws IOException {
774 HtmlRendererUtils.checkForCommandFacet(component, facesContext, writer);
775 }
776
777
778
779
780 @Deprecated
781 public static void checkForCommandFacet(UIComponent component, List<String> clientIds, FacesContext facesContext,
782 TobagoResponseWriter writer) throws IOException {
783 HtmlRendererUtils.checkForCommandFacet(component, clientIds, facesContext, writer);
784 }
785
786
787
788
789 @Deprecated
790 public static void removeStyleClasses(UIComponent cell) {
791 Object obj = cell.getAttributes().get(Attributes.STYLE_CLASS);
792 if (obj != null && obj instanceof StyleClasses && cell.getRendererType() != null) {
793 StyleClasses styleClasses = (StyleClasses) obj;
794 if (!styleClasses.isEmpty()) {
795 String rendererName = cell.getRendererType().substring(0, 1).toLowerCase(Locale.ENGLISH)
796 + cell.getRendererType().substring(1);
797 styleClasses.removeTobagoClasses(rendererName);
798 }
799 if (styleClasses.isEmpty()) {
800 cell.getAttributes().remove(Attributes.STYLE_CLASS);
801 }
802 }
803 }
804 }