Source Code Cross Referenced for StrutsUtil.java in  » Web-Framework » struts-2.0.11 » org » apache » struts2 » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Framework » struts 2.0.11 » org.apache.struts2.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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