001: /* Copyright (C) 2003 Finalist IT Group
002: *
003: * This file is part of JAG - the Java J2EE Application Generator
004: *
005: * JAG is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: * JAG is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: * You should have received a copy of the GNU General Public License
014: * along with JAG; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
016: */
017:
018: package com.finalist.jag.taglib.util;
019:
020: import com.finalist.jag.*;
021: import com.finalist.jag.skelet.*;
022:
023: import java.util.*;
024:
025: /**
026: * Class RequestUtil
027: *
028: *
029: * @author Wendel D. de Witte
030: * @version %I%, %G%
031: */
032: public class RequestUtil {
033:
034: /**
035: * Method lookupString
036: *
037: *
038: * @param pageContext
039: * @param name
040: * @param property
041: *
042: * @return
043: *
044: */
045: public static String lookupString(PageContext pageContext,
046: String name, String property) {
047: Object object = pageContext.getAttribute(name);
048:
049: if (object == null) {
050: object = pageContext.getSessionContext().getAttribute(name);
051: }
052:
053: String returnValue = null;
054:
055: if ((object != null) && (object instanceof ModuleData)) {
056: ModuleData moduleData = (ModuleData) object;
057: if (moduleData.isValueCollection()) {
058: Collection col = (Collection) moduleData.getValue();
059: Iterator iterator = col.iterator();
060:
061: while (iterator.hasNext()) {
062: moduleData = (ModuleData) iterator.next();
063: if (moduleData != null
064: && moduleData.getName().equals(property)
065: && moduleData.isValueString()) {
066: returnValue = (String) moduleData.getValue();
067: break;
068: }
069: }
070: }
071: } else if ((object != null) && (object instanceof String)) {
072: returnValue = (String) object;
073: } else {
074: String colProperty = "";
075: String colName = "";
076: StringTokenizer tokens = new StringTokenizer(name, ".");
077: while (tokens.hasMoreTokens()) {
078: String token = tokens.nextToken();
079: if (!tokens.hasMoreTokens()) {
080: colProperty = token;
081: } else {
082: if (colName.length() > 0)
083: colName += ".";
084: colName += token;
085: }
086: }
087: Collection collection = lookupCollection(pageContext,
088: colName, colProperty);
089: if (collection != null) {
090: Iterator iterator = collection.iterator();
091: if (iterator.hasNext()) {
092: object = iterator.next();
093: String hashcode = "" + object.hashCode();
094: pageContext.setAttribute(hashcode, object);
095: returnValue = lookupString(pageContext, hashcode,
096: property);
097: pageContext.removeAttribute(hashcode);
098: }
099: }
100: }
101: return returnValue;
102: }
103:
104: /**
105: * Method lookupCollection
106: *
107: *
108: * @param pageContext
109: * @param name
110: * @param property
111: *
112: * @return
113: *
114: */
115: public static Collection lookupCollection(PageContext pageContext,
116: String name, String property) {
117: SessionContext session = pageContext.getSessionContext();
118: Object object = pageContext.getAttribute(name);
119:
120: if (object == null) {
121: object = session.getAttribute(name);
122: }
123:
124: Collection returnValue = null;
125:
126: if ((object != null) && (object instanceof Collection)) {
127: returnValue = (Collection) object;
128: } else {
129: Collection dataModules = null;
130: ArrayList tmpList = new ArrayList();
131:
132: if ((object != null) && (object instanceof ModuleData)) {
133: ModuleData dataModule = (ModuleData) object;
134:
135: if (dataModule.isValueCollection()) {
136: dataModules = (Collection) dataModule.getValue();
137: }
138: } else {
139: SkeletDataObj skelet = session.getSkelet();
140: StringTokenizer tokens = new StringTokenizer(name, ".");
141:
142: dataModules = (Collection) skelet.getValue();
143:
144: if (name.indexOf(skelet.getName()) == 0) {
145: tokens.nextToken();
146: }
147:
148: while (tokens.hasMoreTokens()) {
149: String label = tokens.nextToken();
150: Iterator iterator = dataModules.iterator();
151:
152: while (iterator.hasNext()) {
153: ModuleData moduleData = (ModuleData) iterator
154: .next();
155:
156: if (moduleData.isValueCollection()
157: && moduleData.getName().equals(label)) {
158: if (tokens.hasMoreTokens()) {
159: dataModules = (Collection) moduleData
160: .getValue();
161: break;
162: }
163: }
164: }
165: dataModules = null;
166: break;
167: }
168: }
169:
170: if (dataModules != null) {
171: Iterator iterator = dataModules.iterator();
172: while ((property != null) && iterator.hasNext()) {
173: ModuleData moduleData = (ModuleData) iterator
174: .next();
175:
176: if (moduleData != null
177: && moduleData.isValueCollection()
178: && moduleData.getName().equals(property)) {
179: tmpList.add(moduleData);
180: }
181: }
182: returnValue = tmpList;
183: }
184: }
185: return returnValue;
186: }
187: }
|