KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > lang > jstl > ImplicitObjects


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18
19 package org.apache.taglibs.standard.lang.jstl;
20
21
22
23 import java.util.ArrayList JavaDoc;
24
25 import java.util.Collections JavaDoc;
26
27 import java.util.Date JavaDoc;
28
29 import java.util.Enumeration JavaDoc;
30
31 import java.util.HashMap JavaDoc;
32
33 import java.util.List JavaDoc;
34
35 import java.util.Map JavaDoc;
36
37 import javax.servlet.ServletContext JavaDoc;
38
39 import javax.servlet.http.Cookie JavaDoc;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45
46
47 /**
48
49  *
50
51  * <p>This class is used to generate the implicit Map and List objects
52
53  * that wrap various elements of the PageContext. It also returns the
54
55  * correct implicit object for a given implicit object name.
56
57  *
58
59  * @author Nathan Abramson - Art Technology Group
60
61  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: horwat $
62
63  **/

64
65
66
67 public class ImplicitObjects
68
69 {
70
71   //-------------------------------------
72

73   // Constants
74

75   //-------------------------------------
76

77
78
79   static final String JavaDoc sAttributeName =
80
81     "org.apache.taglibs.standard.ImplicitObjects";
82
83
84
85   //-------------------------------------
86

87   // Member variables
88

89   //-------------------------------------
90

91
92
93   PageContext JavaDoc mContext;
94
95   Map JavaDoc mPage;
96
97   Map JavaDoc mRequest;
98
99   Map JavaDoc mSession;
100
101   Map JavaDoc mApplication;
102
103   Map JavaDoc mParam;
104
105   Map JavaDoc mParams;
106
107   Map JavaDoc mHeader;
108
109   Map JavaDoc mHeaders;
110
111   Map JavaDoc mInitParam;
112
113   Map JavaDoc mCookie;
114
115
116
117   //-------------------------------------
118

119   /**
120
121    *
122
123    * Constructor
124
125    **/

126
127   public ImplicitObjects (PageContext JavaDoc pContext)
128
129   {
130
131     mContext = pContext;
132
133   }
134
135
136
137   //-------------------------------------
138

139   /**
140
141    *
142
143    * Finds the ImplicitObjects associated with the PageContext,
144
145    * creating it if it doesn't yet exist.
146
147    **/

148
149   public static ImplicitObjects getImplicitObjects (PageContext JavaDoc pContext)
150
151   {
152
153     ImplicitObjects objs =
154
155       (ImplicitObjects)
156
157       pContext.getAttribute (sAttributeName,
158
159                  PageContext.PAGE_SCOPE);
160
161     if (objs == null) {
162
163       objs = new ImplicitObjects (pContext);
164
165       pContext.setAttribute (sAttributeName,
166
167                  objs,
168
169                  PageContext.PAGE_SCOPE);
170
171     }
172
173     return objs;
174
175   }
176
177
178
179   //-------------------------------------
180

181   /**
182
183    *
184
185    * Returns the Map that "wraps" page-scoped attributes
186
187    **/

188
189   public Map JavaDoc getPageScopeMap ()
190
191   {
192
193     if (mPage == null) {
194
195       mPage = createPageScopeMap (mContext);
196
197     }
198
199     return mPage;
200
201   }
202
203
204
205   //-------------------------------------
206

207   /**
208
209    *
210
211    * Returns the Map that "wraps" request-scoped attributes
212
213    **/

214
215   public Map JavaDoc getRequestScopeMap ()
216
217   {
218
219     if (mRequest == null) {
220
221       mRequest = createRequestScopeMap (mContext);
222
223     }
224
225     return mRequest;
226
227   }
228
229
230
231   //-------------------------------------
232

233   /**
234
235    *
236
237    * Returns the Map that "wraps" session-scoped attributes
238
239    **/

240
241   public Map JavaDoc getSessionScopeMap ()
242
243   {
244
245     if (mSession == null) {
246
247       mSession = createSessionScopeMap (mContext);
248
249     }
250
251     return mSession;
252
253   }
254
255
256
257   //-------------------------------------
258

259   /**
260
261    *
262
263    * Returns the Map that "wraps" application-scoped attributes
264
265    **/

266
267   public Map JavaDoc getApplicationScopeMap ()
268
269   {
270
271     if (mApplication == null) {
272
273       mApplication = createApplicationScopeMap (mContext);
274
275     }
276
277     return mApplication;
278
279   }
280
281
282
283   //-------------------------------------
284

285   /**
286
287    *
288
289    * Returns the Map that maps parameter name to a single parameter
290
291    * values.
292
293    **/

294
295   public Map JavaDoc getParamMap ()
296
297   {
298
299     if (mParam == null) {
300
301       mParam = createParamMap (mContext);
302
303     }
304
305     return mParam;
306
307   }
308
309
310
311   //-------------------------------------
312

313   /**
314
315    *
316
317    * Returns the Map that maps parameter name to an array of parameter
318
319    * values.
320
321    **/

322
323   public Map JavaDoc getParamsMap ()
324
325   {
326
327     if (mParams == null) {
328
329       mParams = createParamsMap (mContext);
330
331     }
332
333     return mParams;
334
335   }
336
337
338
339   //-------------------------------------
340

341   /**
342
343    *
344
345    * Returns the Map that maps header name to a single header
346
347    * values.
348
349    **/

350
351   public Map JavaDoc getHeaderMap ()
352
353   {
354
355     if (mHeader == null) {
356
357       mHeader = createHeaderMap (mContext);
358
359     }
360
361     return mHeader;
362
363   }
364
365
366
367   //-------------------------------------
368

369   /**
370
371    *
372
373    * Returns the Map that maps header name to an array of header
374
375    * values.
376
377    **/

378
379   public Map JavaDoc getHeadersMap ()
380
381   {
382
383     if (mHeaders == null) {
384
385       mHeaders = createHeadersMap (mContext);
386
387     }
388
389     return mHeaders;
390
391   }
392
393
394
395   //-------------------------------------
396

397   /**
398
399    *
400
401    * Returns the Map that maps init parameter name to a single init
402
403    * parameter values.
404
405    **/

406
407   public Map JavaDoc getInitParamMap ()
408
409   {
410
411     if (mInitParam == null) {
412
413       mInitParam = createInitParamMap (mContext);
414
415     }
416
417     return mInitParam;
418
419   }
420
421
422
423   //-------------------------------------
424

425   /**
426
427    *
428
429    * Returns the Map that maps cookie name to the first matching
430
431    * Cookie in request.getCookies().
432
433    **/

434
435   public Map JavaDoc getCookieMap ()
436
437   {
438
439     if (mCookie == null) {
440
441       mCookie = createCookieMap (mContext);
442
443     }
444
445     return mCookie;
446
447   }
448
449
450
451   //-------------------------------------
452

453   // Methods for generating wrapper maps
454

455   //-------------------------------------
456

457   /**
458
459    *
460
461    * Creates the Map that "wraps" page-scoped attributes
462
463    **/

464
465   public static Map JavaDoc createPageScopeMap (PageContext JavaDoc pContext)
466
467   {
468
469     final PageContext JavaDoc context = pContext;
470
471     return new EnumeratedMap ()
472
473       {
474
475     public Enumeration JavaDoc enumerateKeys ()
476
477     {
478
479       return context.getAttributeNamesInScope
480
481         (PageContext.PAGE_SCOPE);
482
483     }
484
485
486
487     public Object JavaDoc getValue (Object JavaDoc pKey)
488
489     {
490
491       if (pKey instanceof String JavaDoc) {
492
493         return context.getAttribute
494
495           ((String JavaDoc) pKey,
496
497            PageContext.PAGE_SCOPE);
498
499       }
500
501       else {
502
503         return null;
504
505       }
506
507     }
508
509
510
511     public boolean isMutable ()
512
513     {
514
515       return true;
516
517     }
518
519       };
520
521   }
522
523
524
525   //-------------------------------------
526

527   /**
528
529    *
530
531    * Creates the Map that "wraps" request-scoped attributes
532
533    **/

534
535   public static Map JavaDoc createRequestScopeMap (PageContext JavaDoc pContext)
536
537   {
538
539     final PageContext JavaDoc context = pContext;
540
541     return new EnumeratedMap ()
542
543       {
544
545     public Enumeration JavaDoc enumerateKeys ()
546
547     {
548
549       return context.getAttributeNamesInScope
550
551         (PageContext.REQUEST_SCOPE);
552
553     }
554
555
556
557     public Object JavaDoc getValue (Object JavaDoc pKey)
558
559     {
560
561       if (pKey instanceof String JavaDoc) {
562
563         return context.getAttribute
564
565           ((String JavaDoc) pKey,
566
567            PageContext.REQUEST_SCOPE);
568
569       }
570
571       else {
572
573         return null;
574
575       }
576
577     }
578
579
580
581     public boolean isMutable ()
582
583     {
584
585       return true;
586
587     }
588
589       };
590
591   }
592
593
594
595   //-------------------------------------
596

597   /**
598
599    *
600
601    * Creates the Map that "wraps" session-scoped attributes
602
603    **/

604
605   public static Map JavaDoc createSessionScopeMap (PageContext JavaDoc pContext)
606
607   {
608
609     final PageContext JavaDoc context = pContext;
610
611     return new EnumeratedMap ()
612
613       {
614
615     public Enumeration JavaDoc enumerateKeys ()
616
617     {
618
619       return context.getAttributeNamesInScope
620
621         (PageContext.SESSION_SCOPE);
622
623     }
624
625
626
627     public Object JavaDoc getValue (Object JavaDoc pKey)
628
629     {
630
631       if (pKey instanceof String JavaDoc) {
632
633         return context.getAttribute
634
635           ((String JavaDoc) pKey,
636
637            PageContext.SESSION_SCOPE);
638
639       }
640
641       else {
642
643         return null;
644
645       }
646
647     }
648
649
650
651     public boolean isMutable ()
652
653     {
654
655       return true;
656
657     }
658
659       };
660
661   }
662
663
664
665   //-------------------------------------
666

667   /**
668
669    *
670
671    * Creates the Map that "wraps" application-scoped attributes
672
673    **/

674
675   public static Map JavaDoc createApplicationScopeMap (PageContext JavaDoc pContext)
676
677   {
678
679     final PageContext JavaDoc context = pContext;
680
681     return new EnumeratedMap ()
682
683       {
684
685     public Enumeration JavaDoc enumerateKeys ()
686
687     {
688
689       return context.getAttributeNamesInScope
690
691         (PageContext.APPLICATION_SCOPE);
692
693     }
694
695
696
697     public Object JavaDoc getValue (Object JavaDoc pKey)
698
699     {
700
701       if (pKey instanceof String JavaDoc) {
702
703         return context.getAttribute
704
705           ((String JavaDoc) pKey,
706
707            PageContext.APPLICATION_SCOPE);
708
709       }
710
711       else {
712
713         return null;
714
715       }
716
717     }
718
719
720
721     public boolean isMutable ()
722
723     {
724
725       return true;
726
727     }
728
729       };
730
731   }
732
733
734
735   //-------------------------------------
736

737   /**
738
739    *
740
741    * Creates the Map that maps parameter name to single parameter
742
743    * value.
744
745    **/

746
747   public static Map JavaDoc createParamMap (PageContext JavaDoc pContext)
748
749   {
750
751     final HttpServletRequest JavaDoc request =
752
753       (HttpServletRequest JavaDoc) pContext.getRequest ();
754
755     return new EnumeratedMap ()
756
757       {
758
759     public Enumeration JavaDoc enumerateKeys ()
760
761     {
762
763       return request.getParameterNames ();
764
765     }
766
767
768
769     public Object JavaDoc getValue (Object JavaDoc pKey)
770
771     {
772
773       if (pKey instanceof String JavaDoc) {
774
775         return request.getParameter ((String JavaDoc) pKey);
776
777       }
778
779       else {
780
781         return null;
782
783       }
784
785     }
786
787
788
789     public boolean isMutable ()
790
791     {
792
793       return false;
794
795     }
796
797       };
798
799   }
800
801
802
803   //-------------------------------------
804

805   /**
806
807    *
808
809    * Creates the Map that maps parameter name to an array of parameter
810
811    * values.
812
813    **/

814
815   public static Map JavaDoc createParamsMap (PageContext JavaDoc pContext)
816
817   {
818
819     final HttpServletRequest JavaDoc request =
820
821       (HttpServletRequest JavaDoc) pContext.getRequest ();
822
823     return new EnumeratedMap ()
824
825       {
826
827     public Enumeration JavaDoc enumerateKeys ()
828
829     {
830
831       return request.getParameterNames ();
832
833     }
834
835
836
837     public Object JavaDoc getValue (Object JavaDoc pKey)
838
839     {
840
841       if (pKey instanceof String JavaDoc) {
842
843         return request.getParameterValues ((String JavaDoc) pKey);
844
845       }
846
847       else {
848
849         return null;
850
851       }
852
853     }
854
855
856
857     public boolean isMutable ()
858
859     {
860
861       return false;
862
863     }
864
865       };
866
867   }
868
869
870
871   //-------------------------------------
872

873   /**
874
875    *
876
877    * Creates the Map that maps header name to single header
878
879    * value.
880
881    **/

882
883   public static Map JavaDoc createHeaderMap (PageContext JavaDoc pContext)
884
885   {
886
887     final HttpServletRequest JavaDoc request =
888
889       (HttpServletRequest JavaDoc) pContext.getRequest ();
890
891     return new EnumeratedMap ()
892
893       {
894
895     public Enumeration JavaDoc enumerateKeys ()
896
897     {
898
899       return request.getHeaderNames ();
900
901     }
902
903
904
905     public Object JavaDoc getValue (Object JavaDoc pKey)
906
907     {
908
909       if (pKey instanceof String JavaDoc) {
910
911         return request.getHeader ((String JavaDoc) pKey);
912
913       }
914
915       else {
916
917         return null;
918
919       }
920
921     }
922
923
924
925     public boolean isMutable ()
926
927     {
928
929       return false;
930
931     }
932
933       };
934
935   }
936
937
938
939   //-------------------------------------
940

941   /**
942
943    *
944
945    * Creates the Map that maps header name to an array of header
946
947    * values.
948
949    **/

950
951   public static Map JavaDoc createHeadersMap (PageContext JavaDoc pContext)
952
953   {
954
955     final HttpServletRequest JavaDoc request =
956
957       (HttpServletRequest JavaDoc) pContext.getRequest ();
958
959     return new EnumeratedMap ()
960
961       {
962
963     public Enumeration JavaDoc enumerateKeys ()
964
965     {
966
967       return request.getHeaderNames ();
968
969     }
970
971
972
973     public Object JavaDoc getValue (Object JavaDoc pKey)
974
975     {
976
977       if (pKey instanceof String JavaDoc) {
978
979         // Drain the header enumeration
980

981         List JavaDoc l = new ArrayList JavaDoc ();
982
983         Enumeration JavaDoc enum_ = request.getHeaders ((String JavaDoc) pKey);
984
985         if (enum_ != null) {
986
987           while (enum_.hasMoreElements ()) {
988
989         l.add (enum_.nextElement ());
990
991           }
992
993         }
994
995         String JavaDoc [] ret = (String JavaDoc []) l.toArray (new String JavaDoc [l.size ()]);
996
997         return ret;
998
999       }
1000
1001      else {
1002
1003        return null;
1004
1005      }
1006
1007    }
1008
1009
1010
1011    public boolean isMutable ()
1012
1013    {
1014
1015      return false;
1016
1017    }
1018
1019      };
1020
1021  }
1022
1023
1024
1025  //-------------------------------------
1026

1027  /**
1028
1029   *
1030
1031   * Creates the Map that maps init parameter name to single init
1032
1033   * parameter value.
1034
1035   **/

1036
1037  public static Map JavaDoc createInitParamMap (PageContext JavaDoc pContext)
1038
1039  {
1040
1041    final ServletContext JavaDoc context = pContext.getServletContext ();
1042
1043    return new EnumeratedMap ()
1044
1045      {
1046
1047    public Enumeration JavaDoc enumerateKeys ()
1048
1049    {
1050
1051      return context.getInitParameterNames ();
1052
1053    }
1054
1055
1056
1057    public Object JavaDoc getValue (Object JavaDoc pKey)
1058
1059    {
1060
1061      if (pKey instanceof String JavaDoc) {
1062
1063        return context.getInitParameter ((String JavaDoc) pKey);
1064
1065      }
1066
1067      else {
1068
1069        return null;
1070
1071      }
1072
1073    }
1074
1075
1076
1077    public boolean isMutable ()
1078
1079    {
1080
1081      return false;
1082
1083    }
1084
1085      };
1086
1087  }
1088
1089
1090
1091  //-------------------------------------
1092

1093  /**
1094
1095   *
1096
1097   * Creates the Map that maps cookie name to the first matching
1098
1099   * Cookie in request.getCookies().
1100
1101   **/

1102
1103  public static Map JavaDoc createCookieMap (PageContext JavaDoc pContext)
1104
1105  {
1106
1107    // Read all the cookies and construct the entire map
1108

1109    HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) pContext.getRequest ();
1110
1111    Cookie JavaDoc [] cookies = request.getCookies ();
1112
1113    Map JavaDoc ret = new HashMap JavaDoc ();
1114
1115    for (int i = 0; cookies != null && i < cookies.length; i++) {
1116
1117      Cookie JavaDoc cookie = cookies [i];
1118
1119      if (cookie != null) {
1120
1121    String JavaDoc name = cookie.getName ();
1122
1123    if (!ret.containsKey (name)) {
1124
1125      ret.put (name, cookie);
1126
1127    }
1128
1129      }
1130
1131    }
1132
1133    return ret;
1134
1135  }
1136
1137
1138
1139  //-------------------------------------
1140

1141}
1142
1143
Popular Tags