KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > util > WebWorkUtil


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.util;
6
7 import com.opensymphony.util.TextUtils;
8 import com.opensymphony.webwork.views.jsp.ui.OgnlTool;
9 import com.opensymphony.webwork.views.util.UrlHelper;
10 import com.opensymphony.xwork.ObjectFactory;
11 import com.opensymphony.xwork.util.OgnlValueStack;
12 import org.apache.commons.logging.Log;
13 import org.apache.commons.logging.LogFactory;
14
15 import javax.servlet.RequestDispatcher JavaDoc;
16 import javax.servlet.ServletOutputStream JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.http.HttpServletResponseWrapper JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.net.URLEncoder JavaDoc;
24 import java.util.*;
25
26
27 /**
28  * WebWork base utility class, for use in Velocity and Freemarker templates
29  *
30  * @author Rickard Öberg (rickard@dreambean.com)
31  * @author Cameron Braid
32  * @version $Revision: 1.15 $
33  */

34 public class WebWorkUtil {
35     //~ Static fields/initializers /////////////////////////////////////////////
36

37     protected static final Log log = LogFactory.getLog(WebWorkUtil.class);
38
39     //~ Instance fields ////////////////////////////////////////////////////////
40

41     protected HttpServletRequest JavaDoc request;
42     protected HttpServletResponse JavaDoc response;
43     protected Map classes = new Hashtable();
44     protected OgnlTool ognl = OgnlTool.getInstance();
45     protected OgnlValueStack stack;
46
47     //~ Constructors ///////////////////////////////////////////////////////////
48

49     public WebWorkUtil(OgnlValueStack stack, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
50         this.stack = stack;
51         this.request = request;
52         this.response = response;
53     }
54
55     //~ Methods ////////////////////////////////////////////////////////////////
56

57     public Object JavaDoc bean(Object JavaDoc aName) throws Exception JavaDoc {
58         String JavaDoc name = aName.toString();
59         Class JavaDoc c = (Class JavaDoc) classes.get(name);
60
61         if (c == null) {
62             c = ClassLoaderUtils.loadClass(name, WebWorkUtil.class);
63             classes.put(name, c);
64         }
65
66         return ObjectFactory.getObjectFactory().buildBean(c);
67     }
68
69     public Object JavaDoc findString(String JavaDoc name) {
70         return stack.findValue(name, String JavaDoc.class);
71     }
72
73     public String JavaDoc include(Object JavaDoc aName) throws Exception JavaDoc {
74         return include(aName, request, response);
75     }
76
77     /**
78      * @deprecated the request and response are stored in this util class, please use include(string)
79      */

80     public String JavaDoc include(Object JavaDoc aName, HttpServletRequest JavaDoc aRequest, HttpServletResponse JavaDoc aResponse) throws Exception JavaDoc {
81         try {
82             RequestDispatcher JavaDoc dispatcher = aRequest.getRequestDispatcher(aName.toString());
83
84             if (dispatcher == null) {
85                 throw new IllegalArgumentException JavaDoc("Cannot find included file " + aName);
86             }
87
88             ResponseWrapper JavaDoc responseWrapper = new ResponseWrapper JavaDoc(aResponse);
89
90             dispatcher.include(aRequest, responseWrapper);
91
92             return responseWrapper.getData();
93         }
94         catch (Exception JavaDoc e) {
95             e.printStackTrace();
96             throw e;
97         }
98     }
99
100     public String JavaDoc textToHtml(String JavaDoc s) {
101         return TextUtils.plainTextToHtml(s);
102     }
103
104     public String JavaDoc urlEncode(String JavaDoc s) {
105         return URLEncoder.encode(s);
106     }
107
108     public String JavaDoc buildUrl(String JavaDoc url) {
109         return UrlHelper.buildUrl(url, request, response, null);
110     }
111
112     public Object JavaDoc findValue(String JavaDoc expression, String JavaDoc className) throws ClassNotFoundException JavaDoc {
113         return stack.findValue(expression, Class.forName(className));
114     }
115
116     /**
117      * the selectedList objects are matched to the list.listValue
118      * <p/>
119      * listKey and listValue are optional, and if not provided, the list item is used
120      *
121      * @param selectedList the name of the action property
122      * that contains the list of selected items
123      * or single item if its not an array or list
124      * @param list the name of the action property
125      * that contains the list of selectable items
126      * @param listKey an ognl expression that is exaluated relative to the list item
127      * to use as the key of the ListEntry
128      * @param listValue an ognl expression that is exaluated relative to the list item
129      * to use as the value of the ListEntry
130      * @return a List of ListEntry
131      */

132     public List makeSelectList(String JavaDoc selectedList, String JavaDoc list, String JavaDoc listKey, String JavaDoc listValue) {
133         List selectList = new ArrayList();
134
135         Collection selectedItems = null;
136
137         Object JavaDoc i = stack.findValue(selectedList);
138
139         if (i != null) {
140             if (i.getClass().isArray()) {
141                 selectedItems = Arrays.asList((Object JavaDoc[]) i);
142             } else if (i instanceof Collection) {
143                 selectedItems = (Collection) i;
144             } else {
145                 // treat it is a single item
146
selectedItems = new ArrayList();
147                 selectedItems.add(i);
148             }
149         }
150
151         Collection items = (Collection) stack.findValue(list);
152
153         if (items != null) {
154             for (Iterator iter = items.iterator(); iter.hasNext();) {
155                 Object JavaDoc element = (Object JavaDoc) iter.next();
156                 Object JavaDoc key = null;
157
158                 if ((listKey == null) || (listKey.length() == 0)) {
159                     key = element;
160                 } else {
161                     key = ognl.findValue(listKey, element);
162                 }
163
164                 Object JavaDoc value = null;
165
166                 if ((listValue == null) || (listValue.length() == 0)) {
167                     value = element;
168                 } else {
169                     value = ognl.findValue(listValue, element);
170                 }
171
172                 boolean isSelected = false;
173
174                 if ((value != null) && (selectedItems != null) && selectedItems.contains(value)) {
175                     isSelected = true;
176                 }
177
178                 selectList.add(new ListEntry(key, value, isSelected));
179             }
180         }
181
182         return selectList;
183     }
184
185     public String JavaDoc htmlEncode(Object JavaDoc obj) {
186         if (obj == null) {
187             return null;
188         }
189
190         return TextUtils.htmlEncode(obj.toString());
191     }
192
193     public int toInt(long aLong) {
194         return (int) aLong;
195     }
196
197     public long toLong(int anInt) {
198         return (long) anInt;
199     }
200
201     public long toLong(String JavaDoc aLong) {
202         if (aLong == null) {
203             return 0;
204         }
205
206         return Long.parseLong(aLong);
207     }
208
209     public String JavaDoc toString(long aLong) {
210         return Long.toString(aLong);
211     }
212
213     public String JavaDoc toString(int anInt) {
214         return Integer.toString(anInt);
215     }
216
217     //~ Inner Classes //////////////////////////////////////////////////////////
218

219     static class ResponseWrapper extends HttpServletResponseWrapper JavaDoc {
220         StringWriter JavaDoc strout;
221         PrintWriter JavaDoc writer;
222         ServletOutputStream JavaDoc sout;
223
224         ResponseWrapper(HttpServletResponse JavaDoc aResponse) {
225             super(aResponse);
226             strout = new StringWriter JavaDoc();
227             sout = new ServletOutputStreamWrapper(strout);
228             writer = new PrintWriter JavaDoc(strout);
229         }
230
231         public String JavaDoc getData() {
232             writer.flush();
233
234             return strout.toString();
235         }
236
237         public ServletOutputStream JavaDoc getOutputStream() {
238             return sout;
239         }
240
241         public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
242             return writer;
243         }
244     }
245
246     static class ServletOutputStreamWrapper extends ServletOutputStream JavaDoc {
247         StringWriter JavaDoc writer;
248
249         ServletOutputStreamWrapper(StringWriter JavaDoc aWriter) {
250             writer = aWriter;
251         }
252
253         public void write(int aByte) {
254             writer.write(aByte);
255         }
256     }
257 }
258
Popular Tags