1 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 ; 16 import javax.servlet.ServletOutputStream ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.http.HttpServletResponseWrapper ; 20 import java.io.IOException ; 21 import java.io.PrintWriter ; 22 import java.io.StringWriter ; 23 import java.net.URLEncoder ; 24 import java.util.*; 25 26 27 34 public class WebWorkUtil { 35 37 protected static final Log log = LogFactory.getLog(WebWorkUtil.class); 38 39 41 protected HttpServletRequest request; 42 protected HttpServletResponse response; 43 protected Map classes = new Hashtable(); 44 protected OgnlTool ognl = OgnlTool.getInstance(); 45 protected OgnlValueStack stack; 46 47 49 public WebWorkUtil(OgnlValueStack stack, HttpServletRequest request, HttpServletResponse response) { 50 this.stack = stack; 51 this.request = request; 52 this.response = response; 53 } 54 55 57 public Object bean(Object aName) throws Exception { 58 String name = aName.toString(); 59 Class c = (Class ) 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 findString(String name) { 70 return stack.findValue(name, String .class); 71 } 72 73 public String include(Object aName) throws Exception { 74 return include(aName, request, response); 75 } 76 77 80 public String include(Object aName, HttpServletRequest aRequest, HttpServletResponse aResponse) throws Exception { 81 try { 82 RequestDispatcher dispatcher = aRequest.getRequestDispatcher(aName.toString()); 83 84 if (dispatcher == null) { 85 throw new IllegalArgumentException ("Cannot find included file " + aName); 86 } 87 88 ResponseWrapper responseWrapper = new ResponseWrapper (aResponse); 89 90 dispatcher.include(aRequest, responseWrapper); 91 92 return responseWrapper.getData(); 93 } 94 catch (Exception e) { 95 e.printStackTrace(); 96 throw e; 97 } 98 } 99 100 public String textToHtml(String s) { 101 return TextUtils.plainTextToHtml(s); 102 } 103 104 public String urlEncode(String s) { 105 return URLEncoder.encode(s); 106 } 107 108 public String buildUrl(String url) { 109 return UrlHelper.buildUrl(url, request, response, null); 110 } 111 112 public Object findValue(String expression, String className) throws ClassNotFoundException { 113 return stack.findValue(expression, Class.forName(className)); 114 } 115 116 132 public List makeSelectList(String selectedList, String list, String listKey, String listValue) { 133 List selectList = new ArrayList(); 134 135 Collection selectedItems = null; 136 137 Object i = stack.findValue(selectedList); 138 139 if (i != null) { 140 if (i.getClass().isArray()) { 141 selectedItems = Arrays.asList((Object []) i); 142 } else if (i instanceof Collection) { 143 selectedItems = (Collection) i; 144 } else { 145 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 element = (Object ) iter.next(); 156 Object 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 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 htmlEncode(Object 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 aLong) { 202 if (aLong == null) { 203 return 0; 204 } 205 206 return Long.parseLong(aLong); 207 } 208 209 public String toString(long aLong) { 210 return Long.toString(aLong); 211 } 212 213 public String toString(int anInt) { 214 return Integer.toString(anInt); 215 } 216 217 219 static class ResponseWrapper extends HttpServletResponseWrapper { 220 StringWriter strout; 221 PrintWriter writer; 222 ServletOutputStream sout; 223 224 ResponseWrapper(HttpServletResponse aResponse) { 225 super(aResponse); 226 strout = new StringWriter (); 227 sout = new ServletOutputStreamWrapper(strout); 228 writer = new PrintWriter (strout); 229 } 230 231 public String getData() { 232 writer.flush(); 233 234 return strout.toString(); 235 } 236 237 public ServletOutputStream getOutputStream() { 238 return sout; 239 } 240 241 public PrintWriter getWriter() throws IOException { 242 return writer; 243 } 244 } 245 246 static class ServletOutputStreamWrapper extends ServletOutputStream { 247 StringWriter writer; 248 249 ServletOutputStreamWrapper(StringWriter aWriter) { 250 writer = aWriter; 251 } 252 253 public void write(int aByte) { 254 writer.write(aByte); 255 } 256 } 257 } 258 | Popular Tags |