Source Code Cross Referenced for SetSupport.java in  » EJB-Server-GlassFish » appserv-jstl » org » apache » taglibs » standard » tag » common » core » 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 » EJB Server GlassFish » appserv jstl » org.apache.taglibs.standard.tag.common.core 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003:         * 
004:         * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005:         * 
006:         * Portions Copyright Apache Software Foundation.
007:         * 
008:         * The contents of this file are subject to the terms of either the GNU
009:         * General Public License Version 2 only ("GPL") or the Common Development
010:         * and Distribution License("CDDL") (collectively, the "License").  You
011:         * may not use this file except in compliance with the License. You can obtain
012:         * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013:         * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
014:         * language governing permissions and limitations under the License.
015:         * 
016:         * When distributing the software, include this License Header Notice in each
017:         * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018:         * Sun designates this particular file as subject to the "Classpath" exception
019:         * as provided by Sun in the GPL Version 2 section of the License file that
020:         * accompanied this code.  If applicable, add the following below the License
021:         * Header, with the fields enclosed by brackets [] replaced by your own
022:         * identifying information: "Portions Copyrighted [year]
023:         * [name of copyright owner]"
024:         * 
025:         * Contributor(s):
026:         * 
027:         * If you wish your version of this file to be governed by only the CDDL or
028:         * only the GPL Version 2, indicate your decision by adding "[Contributor]
029:         * elects to include this software in this distribution under the [CDDL or GPL
030:         * Version 2] license."  If you don't indicate a single choice of license, a
031:         * recipient has the option to distribute your version of this file under
032:         * either the CDDL, the GPL Version 2 or to extend the choice of license to
033:         * its licensees as provided above.  However, if you add GPL Version 2 code
034:         * and therefore, elected the GPL Version 2 license, then the option applies
035:         * only if the new code is made subject to such option by the copyright
036:         * holder.
037:         */
038:
039:        package org.apache.taglibs.standard.tag.common.core;
040:
041:        import java.beans.IntrospectionException;
042:        import java.beans.Introspector;
043:        import java.beans.PropertyDescriptor;
044:        import java.lang.reflect.InvocationTargetException;
045:        import java.lang.reflect.Method;
046:        import java.util.Map;
047:
048:        import javax.el.ExpressionFactory;
049:        import javax.el.ValueExpression;
050:        import javax.el.VariableMapper;
051:        import javax.el.ELException;
052:
053:        import javax.servlet.jsp.JspApplicationContext;
054:        import javax.servlet.jsp.JspFactory;
055:        import javax.servlet.jsp.JspException;
056:        import javax.servlet.jsp.JspTagException;
057:        import javax.servlet.jsp.PageContext;
058:        import javax.servlet.jsp.tagext.BodyTagSupport;
059:
060:        import org.apache.taglibs.standard.resources.Resources;
061:
062:        /**
063:         * <p>Support for handlers of the &lt;set&gt; tag.</p>
064:         *
065:         * @author Shawn Bayern
066:         */
067:        public class SetSupport extends BodyTagSupport {
068:
069:            //*********************************************************************
070:            // Internal state
071:
072:            protected Object value; // tag attribute
073:            protected boolean valueSpecified; // status
074:            protected Object target; // tag attribute
075:            protected String property; // tag attribute
076:            private String var; // tag attribute
077:            private int scope; // tag attribute
078:            private boolean scopeSpecified; // status
079:
080:            //*********************************************************************
081:            // Construction and initialization
082:
083:            /**
084:             * Constructs a new handler.  As with TagSupport, subclasses should
085:             * not provide other constructors and are expected to call the
086:             * superclass constructor.
087:             */
088:            public SetSupport() {
089:                super ();
090:                init();
091:            }
092:
093:            // resets local state
094:            private void init() {
095:                value = var = null;
096:                scopeSpecified = valueSpecified = false;
097:                scope = PageContext.PAGE_SCOPE;
098:            }
099:
100:            // Releases any resources we may have (or inherit)
101:            public void release() {
102:                super .release();
103:                init();
104:            }
105:
106:            //*********************************************************************
107:            // Tag logic
108:
109:            public int doEndTag() throws JspException {
110:
111:                Object result; // what we'll store in scope:var
112:
113:                // determine the value by...
114:                if (value != null) {
115:                    // ... reading our attribute
116:                    result = value;
117:                } else if (valueSpecified) {
118:                    // ... accepting an explicit null
119:                    result = null;
120:                } else {
121:                    // ... retrieving and trimming our body
122:                    if (bodyContent == null || bodyContent.getString() == null)
123:                        result = "";
124:                    else
125:                        result = bodyContent.getString().trim();
126:                }
127:
128:                // decide what to do with the result
129:                if (var != null) {
130:
131:                    /*
132:                     * Store the result, letting an IllegalArgumentException
133:                     * propagate back if the scope is invalid (e.g., if an attempt
134:                     * is made to store something in the session without any
135:                     * HttpSession existing).
136:                     */
137:                    if (result != null) {
138:                        if (result instanceof  ValueExpression) {
139:                            if (scope != PageContext.PAGE_SCOPE) {
140:                                throw new JspException(Resources
141:                                        .getMessage("SET_BAD_SCOPE_DEFERRED"));
142:                            }
143:                            VariableMapper vm = pageContext.getELContext()
144:                                    .getVariableMapper();
145:                            if (vm != null) {
146:                                vm.setVariable(var, (ValueExpression) result);
147:                            }
148:                        } else {
149:                            // Make sure to clear any previous mapping for this
150:                            // variable in the variable mapper.
151:                            if (scope == PageContext.PAGE_SCOPE) {
152:                                VariableMapper vm = pageContext.getELContext()
153:                                        .getVariableMapper();
154:                                if (vm != null) {
155:                                    vm.setVariable(var, null);
156:                                }
157:                            }
158:                            pageContext.setAttribute(var, result, scope);
159:                        }
160:                    } else {
161:                        if (scopeSpecified)
162:                            pageContext.removeAttribute(var, scope);
163:                        else
164:                            pageContext.removeAttribute(var);
165:
166:                        if (scope == PageContext.PAGE_SCOPE) {
167:                            VariableMapper vm = pageContext.getELContext()
168:                                    .getVariableMapper();
169:                            if (vm != null) {
170:                                vm.setVariable(var, null);
171:                            }
172:                        }
173:                    }
174:
175:                } else if (target != null) {
176:
177:                    // save the result to target.property
178:                    if (target instanceof  Map) {
179:                        // ... treating it as a Map entry
180:                        if (result == null)
181:                            ((Map) target).remove(property);
182:                        else
183:                            ((Map) target).put(property, result);
184:                    } else {
185:                        // ... treating it as a bean property
186:                        try {
187:                            PropertyDescriptor pd[] = Introspector.getBeanInfo(
188:                                    target.getClass()).getPropertyDescriptors();
189:                            boolean succeeded = false;
190:                            for (int i = 0; i < pd.length; i++) {
191:                                if (pd[i].getName().equals(property)) {
192:                                    Method m = pd[i].getWriteMethod();
193:                                    if (m == null) {
194:                                        throw new JspException(Resources
195:                                                .getMessage(
196:                                                        "SET_NO_SETTER_METHOD",
197:                                                        property));
198:                                    }
199:                                    if (result != null) {
200:                                        try {
201:                                            m
202:                                                    .invoke(
203:                                                            target,
204:                                                            new Object[] { convertToExpectedType(
205:                                                                    result,
206:                                                                    m
207:                                                                            .getParameterTypes()[0]) });
208:                                        } catch (javax.el.ELException ex) {
209:                                            throw new JspTagException(ex);
210:                                        }
211:                                    } else {
212:                                        m.invoke(target, new Object[] { null });
213:                                    }
214:                                    succeeded = true;
215:                                }
216:                            }
217:                            if (!succeeded) {
218:                                throw new JspTagException(Resources.getMessage(
219:                                        "SET_INVALID_PROPERTY", property));
220:                            }
221:                        } catch (IllegalAccessException ex) {
222:                            throw new JspException(ex);
223:                        } catch (IntrospectionException ex) {
224:                            throw new JspException(ex);
225:                        } catch (InvocationTargetException ex) {
226:                            throw new JspException(ex);
227:                        }
228:                    }
229:                } else {
230:                    // should't ever occur because of validation in TLV and setters
231:                    throw new JspTagException();
232:                }
233:
234:                return EVAL_PAGE;
235:            }
236:
237:            /**
238:             * Convert an object to an expected type according to the conversion
239:             * rules of the Expression Language.
240:             */
241:            private Object convertToExpectedType(final Object value,
242:                    Class expectedType) {
243:
244:                JspFactory jspFactory = JspFactory.getDefaultFactory();
245:                JspApplicationContext jspAppContext = jspFactory
246:                        .getJspApplicationContext(pageContext
247:                                .getServletContext());
248:                ExpressionFactory exprFactory = jspAppContext
249:                        .getExpressionFactory();
250:                return exprFactory.coerceToType(value, expectedType);
251:            }
252:
253:            //*********************************************************************
254:            // Accessor methods
255:
256:            // for tag attribute
257:            public void setVar(String var) {
258:                this .var = var;
259:            }
260:
261:            // for tag attribute
262:            public void setScope(String scope) {
263:                this .scope = Util.getScope(scope);
264:                this .scopeSpecified = true;
265:            }
266:        }
w_w_w_.ja_v__a___2_s__.___c__o_m__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.