KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > taglib > util > RequestUtil


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.taglib.util;
19
20 import com.finalist.jag.*;
21 import com.finalist.jag.skelet.*;
22
23 import java.util.*;
24
25
26 /**
27  * Class RequestUtil
28  *
29  *
30  * @author Wendel D. de Witte
31  * @version %I%, %G%
32  */

33 public class RequestUtil {
34
35     /**
36      * Method lookupString
37      *
38      *
39      * @param pageContext
40      * @param name
41      * @param property
42      *
43      * @return
44      *
45      */

46     public static String JavaDoc lookupString(PageContext pageContext, String JavaDoc name,
47                                       String JavaDoc property)
48     {
49         Object JavaDoc object = pageContext.getAttribute(name);
50
51         if (object == null) {
52             object = pageContext.getSessionContext().getAttribute(name);
53         }
54
55         String JavaDoc returnValue = null;
56
57         if ((object != null) && (object instanceof ModuleData))
58         {
59             ModuleData moduleData = (ModuleData) object;
60             if (moduleData.isValueCollection())
61             {
62                 Collection col = (Collection) moduleData.getValue();
63                 Iterator iterator = col.iterator();
64
65                 while (iterator.hasNext())
66                 {
67                     moduleData = (ModuleData) iterator.next();
68                     if (moduleData != null && moduleData.getName().equals(property)
69                             && moduleData.isValueString())
70                     {
71                         returnValue = (String JavaDoc) moduleData.getValue();
72                         break;
73                     }
74                 }
75             }
76         }
77         else if ((object != null) && (object instanceof String JavaDoc))
78         {
79             returnValue = (String JavaDoc) object;
80         }
81         else
82         {
83             String JavaDoc colProperty = "";
84             String JavaDoc colName = "";
85             StringTokenizer tokens = new StringTokenizer(name, ".");
86             while(tokens.hasMoreTokens())
87             {
88                 String JavaDoc token = tokens.nextToken();
89                 if(!tokens.hasMoreTokens())
90                 {
91                     colProperty = token;
92                 }
93                 else
94                 {
95                     if(colName.length() > 0) colName += ".";
96                     colName += token;
97                 }
98             }
99             Collection collection = lookupCollection(pageContext,colName,colProperty);
100             if(collection != null)
101             {
102                 Iterator iterator = collection.iterator();
103                 if(iterator.hasNext())
104                 {
105                     object = iterator.next();
106                     String JavaDoc hashcode = ""+object.hashCode();
107                     pageContext.setAttribute(hashcode, object);
108                     returnValue = lookupString(pageContext, hashcode, property);
109                     pageContext.removeAttribute(hashcode);
110                 }
111             }
112         }
113         return returnValue;
114     }
115
116     /**
117      * Method lookupCollection
118      *
119      *
120      * @param pageContext
121      * @param name
122      * @param property
123      *
124      * @return
125      *
126      */

127     public static Collection lookupCollection(PageContext pageContext,
128                                               String JavaDoc name, String JavaDoc property)
129     {
130         SessionContext session = pageContext.getSessionContext();
131         Object JavaDoc object = pageContext.getAttribute(name);
132
133         if (object == null) {
134             object = session.getAttribute(name);
135         }
136
137         Collection returnValue = null;
138
139         if ((object != null) && (object instanceof Collection))
140         {
141             returnValue = (Collection) object;
142         }
143         else
144         {
145             Collection dataModules = null;
146             ArrayList tmpList = new ArrayList();
147
148             if ((object != null) && (object instanceof ModuleData))
149             {
150                 ModuleData dataModule = (ModuleData) object;
151
152                 if (dataModule.isValueCollection()) {
153                     dataModules = (Collection) dataModule.getValue();
154                 }
155             }
156             else
157             {
158                 SkeletDataObj skelet = session.getSkelet();
159                 StringTokenizer tokens = new StringTokenizer(name, ".");
160
161                 dataModules = (Collection) skelet.getValue();
162
163                 if (name.indexOf(skelet.getName()) == 0) {
164                     tokens.nextToken();
165                 }
166
167                 while (tokens.hasMoreTokens())
168                 {
169                     String JavaDoc label = tokens.nextToken();
170                     Iterator iterator = dataModules.iterator();
171
172                     while (iterator.hasNext())
173                     {
174                         ModuleData moduleData = (ModuleData) iterator.next();
175
176                         if (moduleData.isValueCollection()
177                                 && moduleData.getName().equals(label))
178                         {
179                             if (tokens.hasMoreTokens())
180                             {
181                                 dataModules =(Collection) moduleData.getValue();
182                                 break;
183                             }
184                         }
185                     }
186                     dataModules = null;
187                     break;
188                 }
189             }
190
191             if (dataModules != null)
192             {
193                 Iterator iterator = dataModules.iterator();
194                 while ((property != null) && iterator.hasNext())
195                 {
196                     ModuleData moduleData = (ModuleData) iterator.next();
197
198                     if (moduleData != null && moduleData.isValueCollection()
199                             && moduleData.getName().equals(property)) {
200                         tmpList.add(moduleData);
201                     }
202                 }
203                 returnValue = tmpList;
204             }
205         }
206         return returnValue;
207     }
208 }
209
Popular Tags