1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.dojo;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.myfaces.renderkit.html.util.AddResource;
26 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
27 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
28
29 import javax.faces.component.UIComponent;
30 import javax.faces.context.FacesContext;
31 import javax.faces.context.ResponseWriter;
32 import javax.servlet.http.HttpServletRequest;
33 import java.io.IOException;
34 import java.lang.reflect.InvocationTargetException;
35 import java.lang.reflect.Method;
36 import java.util.*;
37 import java.util.Map.Entry;
38
39
40
41
42
43
44
45
46
47
48
49 public final class DojoUtils {
50 private static final String MYFACES_DOJO_DEBUGCONSOLE_ID = "myfaces_Dojo_Debugger";
51
52 private static final String DEBUG_CONSOLE_TYPE = "DebugConsole";
53
54 private static final String LAYOUT_ALIGN_ATTR = "layoutAlign";
55
56 private static final String DISPLAY_CLOSE_ACTION_ATTR = "displayCloseAction";
57
58 private static final String RESIZABLE_ATTR = "resizable";
59
60 private static final String HAS_SHADOW_ATTR = "hasShadow";
61
62 private static final String CONSTRAIN_TO_CONTAINER_ATTR = "constrainToContainer";
63
64 private static final String ICON_SRC_ATTR = "iconSrc";
65
66 private static final String TITLE_ATTR = "title";
67
68 private static final String INCL_TYPE_REQ_KEY = "DOJO_DEVELOPMENT_INCLUDE";
69
70 private static final Log log = LogFactory.getLog(DojoUtils.class);
71
72 private static final String DOJO_PROVIDE = "dojo.provide:";
73
74 private static final String DOJO_REQUIRE = "dojo.require:";
75
76 private static final String DOJO_NAMESPACE = "dojo.namespace:";
77
78 private static final String DJCONFIG_INITKEY = "/*djconfig init*/";
79
80 private static final String BODY_SCRIPT_INFOS_ATTRIBUTE_NAME = "bodyScriptInfos";
81
82 private static final String DOJO_FILE_UNCOMPRESSED = "dojo.js.uncompressed.js";
83
84 private static final String DOJO_FILE = "dojo.js";
85
86 private static final String DJCONFIG_REQ_KEY = "MYFACES_DJCONFIG";
87
88 private static final String DOJOEXTENSIONS_NAMESPACE = "dojo.setModulePrefix('extensions', '../dojoextensions.ResourceLoader');";
89
90 private DojoUtils() {
91
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105 public static Map getAttributeMap(FacesContext facesContext, String[] attributeNames, UIComponent component) {
106 Log log = null;
107
108 Class componentClass = component.getClass();
109 Map returnMap = new HashMap();
110 for (int cnt = 0; cnt < attributeNames.length; cnt++) {
111 try {
112 String attributeName = attributeNames[cnt];
113
114 if (attributeName.equals("id") || attributeName.equals("widgetId")) {
115 String calculatedId = DojoUtils.calculateWidgetId(facesContext, component);
116 returnMap.put("id", calculatedId);
117 } else {
118 String attributeCasedName = attributeName.substring(0, 1).toUpperCase() + attributeName.substring(1);
119 String getForm = "get" + attributeCasedName;
120 String isForm = "is" + attributeCasedName;
121 Method m = null;
122
123
124
125 while ((componentClass != null) && (m == null)) {
126 m = componentClass.getDeclaredMethod(getForm, null);
127 if (m == null) {
128
129 m = componentClass.getDeclaredMethod(isForm, null);
130 }
131 if (m == null) {
132
133 componentClass = componentClass.getSuperclass();
134 }
135 }
136 if (m != null) {
137 Object execRetval = m.invoke(component, null);
138 if (execRetval != null)
139 returnMap.put(attributeName, execRetval);
140 }
141 }
142 } catch (Exception e) {
143 if (log == null)
144 log = LogFactory.getLog(DojoUtils.class);
145
146 log.error("getAttributeMap", e);
147 }
148 }
149 return returnMap;
150 }
151
152
153
154
155
156
157
158
159
160
161
162 public static void addDebugConsole(FacesContext facesContext, UIComponent component) throws IOException {
163
164
165 if (isInlineScriptSet(facesContext, "/*DOJO DEBUGCONSOLE ON*/"))
166 return;
167
168 AddResource addResource = AddResourceFactory.getInstance(facesContext);
169 addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, "/*DOJO DEBUGCONSOLE ON*/");
170
171 ResponseWriter writer = facesContext.getResponseWriter();
172
173
174
175
176 writer.startElement(HTML.DIV_ELEM, component);
177 writer.writeAttribute(HTML.ID_ATTR, MYFACES_DOJO_DEBUGCONSOLE_ID, null);
178 writer.writeAttribute(HTML.STYLE_ATTR, "width: 400px; height: 500px; left: 200px;", null);
179 writer.endElement(HTML.DIV_ELEM);
180
181 Map attributeMap = new HashMap();
182 attributeMap.put(TITLE_ATTR, "MyFaces Dojo Debug console");
183 attributeMap.put(ICON_SRC_ATTR, "images/flatScreen.gif");
184 attributeMap.put(CONSTRAIN_TO_CONTAINER_ATTR, new Integer(1));
185 attributeMap.put(HAS_SHADOW_ATTR, new Boolean(true));
186 attributeMap.put(RESIZABLE_ATTR, new Boolean(true));
187 attributeMap.put(DISPLAY_CLOSE_ACTION_ATTR, new Boolean(true));
188 attributeMap.put(LAYOUT_ALIGN_ATTR, "client");
189 renderWidgetInitializationCode(writer, component, DEBUG_CONSOLE_TYPE, attributeMap, MYFACES_DOJO_DEBUGCONSOLE_ID, true);
190 }
191
192
193
194
195 public static boolean isDojoInitialized(FacesContext facesContext)
196 {
197 return isInlineScriptCheck(facesContext, DJCONFIG_INITKEY);
198 }
199
200 public static void addMainInclude(FacesContext facesContext, UIComponent component, String javascriptLocation, DojoConfig config) throws IOException {
201
202 AddResource addResource = AddResourceFactory.getInstance(facesContext);
203
204
205
206
207
208 if (!isInlineScriptSet(facesContext, DJCONFIG_INITKEY)) {
209 addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, DJCONFIG_INITKEY);
210 addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, config.toString());
211
212 String dojofile = ((getExpanded(facesContext) != null) && getExpanded(facesContext).booleanValue()) ? DOJO_FILE_UNCOMPRESSED : DOJO_FILE;
213
214 if (javascriptLocation != null) {
215 addResource.addJavaScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, javascriptLocation + dojofile);
216 } else {
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233 addResource.addJavaScriptAtPositionPlain(facesContext, AddResource.HEADER_BEGIN, DojoResourceLoader.class, dojofile);
234 }
235 addResource.addInlineScriptAtPosition(facesContext, AddResource.HEADER_BEGIN, DOJOEXTENSIONS_NAMESPACE);
236 }
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 public static void addNamespace(FacesContext facesContext, UIComponent component, String namespace, String location) throws IOException {
254
255 if (isInlineScriptSet(facesContext, DOJO_NAMESPACE + namespace))
256 return;
257
258 String namespaceStr = createNamespaceScript(namespace, location);
259 writeInlineScript(facesContext, component, namespaceStr);
260 }
261
262
263
264
265
266
267
268
269
270
271
272
273 public static void addProvide(FacesContext context, String provided) {
274
275 if (isInlineScriptSet(context, DOJO_PROVIDE + provided))
276 return;
277
278 AddResource addResource = AddResourceFactory.getInstance(context);
279 String providedBuilder = createDojoProvideScript(provided);
280
281 addResource.addInlineScriptAtPosition(context, AddResource.HEADER_BEGIN, providedBuilder);
282 }
283
284
285
286
287
288
289
290
291
292 public static void addProvide(FacesContext facesContext, UIComponent component, String provided) throws IOException {
293
294 if (isInlineScriptSet(facesContext, DOJO_PROVIDE + provided))
295 return;
296
297 String providedBuilder = createDojoProvideScript(provided);
298
299 writeInlineScript(facesContext, component, providedBuilder);
300
301 }
302
303
304
305
306
307
308
309
310 public static void addRequire(FacesContext facesContext, UIComponent component, String[] requires) throws IOException {
311 for (int cnt = 0; cnt < requires.length; cnt++)
312 addRequire(facesContext, component, requires[cnt]);
313 }
314
315
316
317
318
319
320
321 public static void addRequire(FacesContext facesContext, UIComponent component, String required) throws IOException {
322
323 if (isInlineScriptSet(facesContext, DOJO_REQUIRE + required))
324 return;
325
326 String requireAsScript = createDojoRequireString(required);
327 writeInlineScript(facesContext, component, requireAsScript);
328 }
329
330
331
332
333
334
335
336
337 public static String createDebugStatement(String stmnt) {
338 return "dojo.debug(\"" + stmnt + "\");\n";
339 }
340
341
342
343
344
345
346
347
348
349 public static String createDebugStatement(String stmnt, String value) {
350 return "dojo.debug(\"" + stmnt + ":\");dojo.debug(" + value + ");\n";
351 }
352
353
354
355
356
357
358
359
360 public static String createDojoProvideScript(String provided) {
361
362 StringBuffer providedBuilder = new StringBuffer(32);
363 providedBuilder.append("dojo.provide('");
364 providedBuilder.append(provided.trim());
365 providedBuilder.append("');");
366
367 return providedBuilder.toString();
368 }
369
370
371
372
373
374
375
376
377 public static String createDojoRequireString(String required) {
378 StringBuffer requiredBuilder = new StringBuffer(32);
379 requiredBuilder.append("dojo.require('");
380 requiredBuilder.append(required.trim());
381 requiredBuilder.append("');");
382
383 return requiredBuilder.toString();
384 }
385
386
387
388
389
390
391
392 public static DojoConfig getDjConfigInstance(FacesContext context) {
393
394
395
396
397
398 DojoConfig djConfig = (DojoConfig) (context.getExternalContext().getRequestMap()).get(DJCONFIG_REQ_KEY);
399
400 if (djConfig == null) {
401 djConfig = new DojoConfig();
402
403 context.getExternalContext().getRequestMap().put(DJCONFIG_REQ_KEY, djConfig);
404 }
405
406 return djConfig;
407 }
408
409
410
411
412
413
414
415 public static Boolean getExpanded(FacesContext facesContext) {
416
417
418
419 Boolean devStatus = (Boolean) facesContext.getExternalContext().getRequestMap().get(INCL_TYPE_REQ_KEY);
420 DojoConfig config = getDjConfigInstance(facesContext);
421 if (devStatus == null)
422 devStatus = new Boolean(false);
423 devStatus = new Boolean(devStatus.booleanValue() || (config.getDevelopment() != null && config.getDevelopment().booleanValue()));
424
425 return devStatus;
426 }
427
428
429
430
431
432
433
434
435
436
437 public static boolean isInlineScriptSet(FacesContext context, String inlineScript) {
438
439
440
441 Set set = getBodyScriptInfos(context.getExternalContext().getRequestMap());
442
443 if (!set.contains(inlineScript)) {
444 set.add(inlineScript);
445
446 return false;
447 }
448
449 return true;
450 }
451
452
453
454
455 public static boolean isInlineScriptCheck(FacesContext context, String inlineScript)
456 {
457
458
459
460 Set set = getBodyScriptInfos(context.getExternalContext().getRequestMap());
461
462 return set.contains(inlineScript);
463 }
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481 public static String renderWidgetInitializationCode(FacesContext facesContext, UIComponent component, String dojoType, Map paramMap) throws IOException {
482 ResponseWriter writer = facesContext.getResponseWriter();
483 String clientId = component.getClientId(facesContext);
484 return renderWidgetInitializationCode(writer, component, dojoType, paramMap, clientId, true);
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498 public static String renderWidgetInitializationCode(FacesContext facesContext, UIComponent component, String dojoType, String[] attributeNames)
499 throws IOException {
500 Map paramMap = DojoUtils.getAttributeMap(facesContext, attributeNames, component);
501 return renderWidgetInitializationCode(facesContext, component, dojoType, paramMap);
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518 public static String renderWidgetInitializationCode(ResponseWriter writer, UIComponent component, String dojoType, Map paramMap, String clientId,
519 boolean refId) throws IOException {
520
521 writer.startElement(HTML.SCRIPT_ELEM, component);
522 writer.writeAttribute(HTML.TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
523
524 String javascriptVar = (String) paramMap.get("widgetVar");
525 if (StringUtils.isBlank(javascriptVar))
526 javascriptVar = calculateWidgetVarName(clientId);
527
528 Iterator it = paramMap.entrySet().iterator();
529
530 writer.write("var ");
531 writer.write(javascriptVar);
532 writer.write(" = ");
533
534 writer.write("dojo.widget.createWidget(\"");
535 writer.write(dojoType);
536 writer.write("\",");
537
538 writer.write("{");
539 boolean first = true;
540 while (it.hasNext()) {
541 Entry entry = (Entry) it.next();
542 Object value = entry.getValue();
543 if (value != null) {
544 if (!first)
545 writer.write(",");
546 writer.write(entry.getKey().toString());
547 writer.write(":");
548
549 boolean isString = value instanceof String;
550 if (isString)
551 {
552 if( value.equals("true")
553 || value.equals("false") )
554 isString = false;
555 }
556 if (isString)
557 writer.write("'");
558 writer.write(value.toString());
559 if (isString)
560 writer.write("'");
561 first = false;
562 }
563
564 }
565 writer.write("}");
566 if (refId) {
567 writer.write(",dojo.byId('");
568 writer.write(clientId);
569 writer.write("')");
570 }
571 writer.write(");");
572
573 writer.endElement(HTML.SCRIPT_ELEM);
574 return javascriptVar;
575 }
576
577
578
579
580
581
582
583
584
585 public static String calculateWidgetVarName(String clientId) {
586 return clientId.replaceAll("\\:", "_") + "_dojoControl";
587 }
588
589
590
591
592
593 public static String calculateWidgetId(FacesContext context, UIComponent widget) {
594 String widgetVarName = "";
595 if (widget instanceof DojoWidget) {
596 widgetVarName = ((DojoWidget) widget).getWidgetId();
597 }
598 if (StringUtils.isBlank(widgetVarName)) {
599 widgetVarName = calculateWidgetVarName(widget.getClientId(context));
600 }
601 return widgetVarName;
602 }
603
604
605
606
607
608
609 public static String calculateWidgetVarName(FacesContext context, UIComponent widget) {
610 String widgetVarName = "";
611 if (widget instanceof DojoWidget) {
612 widgetVarName = ((DojoWidget) widget).getWidgetVar();
613 }
614 if (StringUtils.isBlank(widgetVarName)) {
615 widgetVarName = calculateWidgetVarName(widget.getClientId(context));
616 }
617 return widgetVarName;
618 }
619
620
621
622
623
624
625
626
627
628
629 public static void mergeExternalDjConfig(FacesContext context, DojoConfig config) {
630
631
632
633 DojoConfig configSingleton = getDjConfigInstance(context);
634 Class dcConfigClass = DojoConfig.class;
635 Method[] djConfigFieldArr = dcConfigClass.getMethods();
636
637 for (int cnt = 0; cnt < djConfigFieldArr.length; cnt++) {
638
639 try {
640 Method configPropertyField = djConfigFieldArr[cnt];
641 String methodCore = null;
642
643 if ((!configPropertyField.getName().startsWith("getClass") && configPropertyField.getName().startsWith("get"))
644 || configPropertyField.getName().startsWith("is"))
645 methodCore = (configPropertyField.getName().startsWith("get")) ? configPropertyField.getName().substring(3) : configPropertyField.getName()
646 .substring(2);
647
648 if (methodCore != null) {
649 Object val = configPropertyField.invoke(config, null);
650
651 if (val != null) {
652 Class[] setterParams = new Class[1];
653 setterParams[0] = val.getClass();
654
655 Method setMethod = dcConfigClass.getMethod("set" + methodCore, setterParams);
656
657 if (setMethod != null) {
658 Object[] setterArgs = new Object[1];
659 setterArgs[0] = val;
660 setMethod.invoke(configSingleton, setterArgs);
661 }
662 }
663 }
664 } catch (IllegalArgumentException e) {
665 log.error(e);
666 } catch (SecurityException e) {
667 log.error(e);
668 } catch (IllegalAccessException e) {
669 log.error(e);
670 } catch (InvocationTargetException e) {
671 log.error(e);
672 } catch (NoSuchMethodException e) {
673 log.error(e);
674 }
675 }
676
677 }
678
679
680
681
682
683
684
685
686
687
688
689 public static void setExpanded(FacesContext facesContext, Boolean expanded) {
690
691
692 facesContext.getExternalContext().getRequestMap().put(INCL_TYPE_REQ_KEY, expanded);
693 }
694
695
696
697
698
699
700
701
702
703
704 public static void writeDebugStatement(ResponseWriter writer, String stmnt) throws IOException {
705 stmnt = createDebugStatement(stmnt);
706 writer.write(stmnt);
707 }
708
709
710
711
712
713
714
715
716
717
718
719
720 private static String createNamespaceScript(String namespace, String location) {
721 StringBuffer namespaceBuilder = new StringBuffer(32);
722 namespaceBuilder.append("dojo.hostenv.setModulePrefix('");
723 namespaceBuilder.append(namespace);
724 namespaceBuilder.append("','");
725 namespaceBuilder.append(location);
726 namespaceBuilder.append("');");
727
728 String namespaceStr = namespaceBuilder.toString();
729
730 return namespaceStr;
731 }
732
733
734
735
736
737
738
739
740
741
742
743
744
745 private static Set getBodyScriptInfos(Map requestMap) {
746 Set set = (Set) requestMap.get(BODY_SCRIPT_INFOS_ATTRIBUTE_NAME);
747
748 if (set == null) {
749 set = new TreeSet();
750 requestMap.put(BODY_SCRIPT_INFOS_ATTRIBUTE_NAME, set);
751 }
752
753 return set;
754 }
755
756
757
758
759
760
761
762
763
764
765 private static void writeInlineScript(FacesContext facesContext, UIComponent component, String script) throws IOException {
766 ResponseWriter writer = facesContext.getResponseWriter();
767 writer.startElement(HTML.SCRIPT_ELEM, component);
768 writer.writeAttribute(HTML.TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
769 writer.write(script);
770 writer.endElement(HTML.SCRIPT_ELEM);
771 }
772
773 }