001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.util;
006:
007: import com.opensymphony.util.TextUtils;
008: import com.opensymphony.webwork.views.jsp.ui.OgnlTool;
009: import com.opensymphony.webwork.views.util.UrlHelper;
010: import com.opensymphony.xwork.ObjectFactory;
011: import com.opensymphony.xwork.util.OgnlValueStack;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: import javax.servlet.RequestDispatcher;
016: import javax.servlet.ServletOutputStream;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019: import javax.servlet.http.HttpServletResponseWrapper;
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023: import java.io.UnsupportedEncodingException;
024: import java.net.URLEncoder;
025: import java.util.*;
026:
027: /**
028: * WebWork base utility class, for use in Velocity and Freemarker templates
029: *
030: * @author Rickard Öberg (rickard@dreambean.com)
031: * @author Cameron Braid
032: * @version $Revision: 1995 $
033: */
034: public class WebWorkUtil {
035:
036: protected static final Log log = LogFactory
037: .getLog(WebWorkUtil.class);
038:
039: protected HttpServletRequest request;
040: protected HttpServletResponse response;
041: protected Map classes = new Hashtable();
042: protected OgnlTool ognl = OgnlTool.getInstance();
043: protected OgnlValueStack stack;
044:
045: public WebWorkUtil(OgnlValueStack stack,
046: HttpServletRequest request, HttpServletResponse response) {
047: this .stack = stack;
048: this .request = request;
049: this .response = response;
050: }
051:
052: public Object bean(Object aName) throws Exception {
053: String name = aName.toString();
054: Class c = (Class) classes.get(name);
055:
056: if (c == null) {
057: c = ClassLoaderUtils.loadClass(name, WebWorkUtil.class);
058: classes.put(name, c);
059: }
060:
061: return ObjectFactory.getObjectFactory().buildBean(c,
062: stack.getContext());
063: }
064:
065: public boolean isTrue(String expression) {
066: Boolean retVal = (Boolean) stack.findValue(expression,
067: Boolean.class);
068: if (retVal == null) {
069: return false;
070: }
071: return retVal.booleanValue();
072: }
073:
074: public Object findString(String name) {
075: return stack.findValue(name, String.class);
076: }
077:
078: public String include(Object aName) throws Exception {
079: return include(aName, request, response);
080: }
081:
082: /**
083: * @deprecated the request and response are stored in this util class, please use include(string)
084: */
085: public String include(Object aName, HttpServletRequest aRequest,
086: HttpServletResponse aResponse) throws Exception {
087: try {
088: RequestDispatcher dispatcher = aRequest
089: .getRequestDispatcher(aName.toString());
090:
091: if (dispatcher == null) {
092: throw new IllegalArgumentException(
093: "Cannot find included file " + aName);
094: }
095:
096: ResponseWrapper responseWrapper = new ResponseWrapper(
097: aResponse);
098:
099: dispatcher.include(aRequest, responseWrapper);
100:
101: return responseWrapper.getData();
102: } catch (Exception e) {
103: e.printStackTrace();
104: throw e;
105: }
106: }
107:
108: public String textToHtml(String s) {
109: return TextUtils.plainTextToHtml(s);
110: }
111:
112: public String urlEncode(String s) {
113: try {
114: return URLEncoder.encode(s, "UTF-8");
115: } catch (UnsupportedEncodingException e) {
116: return s;
117: }
118: }
119:
120: public String buildUrl(String url) {
121: return UrlHelper.buildUrl(url, request, response, null);
122: }
123:
124: public Object findValue(String expression, String className)
125: throws ClassNotFoundException {
126: return stack.findValue(expression, Class.forName(className));
127: }
128:
129: public String getText(String text) {
130: return (String) stack.findValue("getText('" + text + "')");
131: }
132:
133: /*
134: * @return the url ContextPath. An empty string if one does not exist.
135: */
136: public String getContext() {
137: return (request == null) ? "" : request.getContextPath();
138: }
139:
140: /**
141: * the selectedList objects are matched to the list.listValue
142: * <p/>
143: * listKey and listValue are optional, and if not provided, the list item is used
144: *
145: * @param selectedList the name of the action property
146: * that contains the list of selected items
147: * or single item if its not an array or list
148: * @param list the name of the action property
149: * that contains the list of selectable items
150: * @param listKey an ognl expression that is exaluated relative to the list item
151: * to use as the key of the ListEntry
152: * @param listValue an ognl expression that is exaluated relative to the list item
153: * to use as the value of the ListEntry
154: * @return a List of ListEntry
155: */
156: public List makeSelectList(String selectedList, String list,
157: String listKey, String listValue) {
158: List selectList = new ArrayList();
159:
160: Collection selectedItems = null;
161:
162: Object i = stack.findValue(selectedList);
163:
164: if (i != null) {
165: if (i.getClass().isArray()) {
166: selectedItems = Arrays.asList((Object[]) i);
167: } else if (i instanceof Collection) {
168: selectedItems = (Collection) i;
169: } else {
170: // treat it is a single item
171: selectedItems = new ArrayList();
172: selectedItems.add(i);
173: }
174: }
175:
176: Collection items = (Collection) stack.findValue(list);
177:
178: if (items != null) {
179: for (Iterator iter = items.iterator(); iter.hasNext();) {
180: Object element = (Object) iter.next();
181: Object key = null;
182:
183: if ((listKey == null) || (listKey.length() == 0)) {
184: key = element;
185: } else {
186: key = ognl.findValue(listKey, element);
187: }
188:
189: Object value = null;
190:
191: if ((listValue == null) || (listValue.length() == 0)) {
192: value = element;
193: } else {
194: value = ognl.findValue(listValue, element);
195: }
196:
197: boolean isSelected = false;
198:
199: if ((value != null) && (selectedItems != null)
200: && selectedItems.contains(value)) {
201: isSelected = true;
202: }
203:
204: selectList.add(new ListEntry(key, value, isSelected));
205: }
206: }
207:
208: return selectList;
209: }
210:
211: public String htmlEncode(Object obj) {
212: if (obj == null) {
213: return null;
214: }
215:
216: return TextUtils.htmlEncode(obj.toString());
217: }
218:
219: public int toInt(long aLong) {
220: return (int) aLong;
221: }
222:
223: public long toLong(int anInt) {
224: return (long) anInt;
225: }
226:
227: public long toLong(String aLong) {
228: if (aLong == null) {
229: return 0;
230: }
231:
232: return Long.parseLong(aLong);
233: }
234:
235: public String toString(long aLong) {
236: return Long.toString(aLong);
237: }
238:
239: public String toString(int anInt) {
240: return Integer.toString(anInt);
241: }
242:
243: static class ResponseWrapper extends HttpServletResponseWrapper {
244: StringWriter strout;
245: PrintWriter writer;
246: ServletOutputStream sout;
247:
248: ResponseWrapper(HttpServletResponse aResponse) {
249: super (aResponse);
250: strout = new StringWriter();
251: sout = new ServletOutputStreamWrapper(strout);
252: writer = new PrintWriter(strout);
253: }
254:
255: public String getData() {
256: writer.flush();
257:
258: return strout.toString();
259: }
260:
261: public ServletOutputStream getOutputStream() {
262: return sout;
263: }
264:
265: public PrintWriter getWriter() throws IOException {
266: return writer;
267: }
268: }
269:
270: static class ServletOutputStreamWrapper extends ServletOutputStream {
271: StringWriter writer;
272:
273: ServletOutputStreamWrapper(StringWriter aWriter) {
274: writer = aWriter;
275: }
276:
277: public void write(int aByte) {
278: writer.write(aByte);
279: }
280: }
281: }
|