Source Code Cross Referenced for ASTArrayAccess.java in  » Library » Apache-commons-jexl-1.1-src » org » apache » commons » jexl » parser » 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 » Library » Apache commons jexl 1.1 src » org.apache.commons.jexl.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2002-2006 The Apache Software Foundation.
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.apache.commons.jexl.parser;
018:
019:        import org.apache.commons.jexl.JexlContext;
020:        import org.apache.commons.jexl.util.Coercion;
021:        import org.apache.commons.jexl.util.Introspector;
022:        import org.apache.commons.jexl.util.introspection.Info;
023:        import org.apache.commons.jexl.util.introspection.VelPropertyGet;
024:
025:        import java.util.List;
026:        import java.util.Map;
027:        import java.lang.reflect.Array;
028:
029:        /**
030:         * Like an ASTIdentifier, but with array access allowed.
031:         * 
032:         * $foo[2]
033:         * 
034:         * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
035:         * @version $Id: ASTArrayAccess.java 398180 2006-04-29 15:40:35Z dion $
036:         */
037:        public class ASTArrayAccess extends SimpleNode {
038:            /** dummy velocity info. */
039:            private static final Info DUMMY = new Info("", 1, 1);
040:
041:            /**
042:             * Create the node given an id.
043:             * 
044:             * @param id node id.
045:             */
046:            public ASTArrayAccess(int id) {
047:                super (id);
048:            }
049:
050:            /**
051:             * Create a node with the given parser and id.
052:             * 
053:             * @param p a parser.
054:             * @param id node id.
055:             */
056:            public ASTArrayAccess(Parser p, int id) {
057:                super (p, id);
058:            }
059:
060:            /** {@inheritDoc} */
061:            public Object jjtAccept(ParserVisitor visitor, Object data) {
062:                return visitor.visit(this , data);
063:            }
064:
065:            /**
066:             * evaluate array access upon a base object.
067:             * 
068:             * foo.bar[2]
069:             * 
070:             * makes me rethink the array operator :)
071:             * @param jc the {@link JexlContext} to evaluate against.
072:             * @param obj not used.
073:             * @return the value of the array expression.
074:             * @throws Exception on any error
075:             */
076:            public Object execute(Object obj, JexlContext jc) throws Exception {
077:                ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
078:
079:                Object result = base.execute(obj, jc);
080:
081:                /*
082:                 * ignore the first child - it's our identifier
083:                 */
084:                for (int i = 1; i < jjtGetNumChildren(); i++) {
085:                    Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
086:
087:                    if (loc == null) {
088:                        return null;
089:                    }
090:
091:                    result = evaluateExpr(result, loc);
092:                }
093:
094:                return result;
095:            }
096:
097:            /** {@inheritDoc} */
098:            public Object value(JexlContext jc) throws Exception {
099:                /*
100:                 * get the base ASTIdentifier
101:                 */
102:
103:                ASTIdentifier base = (ASTIdentifier) jjtGetChild(0);
104:
105:                Object o = base.value(jc);
106:
107:                /*
108:                 * ignore the first child - it's our identifier
109:                 */
110:                for (int i = 1; i < jjtGetNumChildren(); i++) {
111:                    Object loc = ((SimpleNode) jjtGetChild(i)).value(jc);
112:
113:                    if (loc == null) {
114:                        return null;
115:                    }
116:
117:                    o = evaluateExpr(o, loc);
118:                }
119:
120:                return o;
121:            }
122:
123:            /**
124:             * Evaluate the Array expression 'loc' on the given object, o.
125:             * e.g. in 'a[2]', <code>2</code> is 'loc' and <code>a</code> is 'o'.
126:             * 
127:             * If o or loc are null, null is returned.
128:             * If o is a Map, o.get(loc) is returned.
129:             * If o is a List, o.get(loc) is returned. loc must resolve to an int value.
130:             * If o is an Array, o[loc] is returned. loc must resolve to an int value.
131:             * Otherwise loc is treated as a bean property of o.
132:             *  
133:             * @param o an object to be accessed using the array operator or '.' operator.
134:             * @param loc the index of the object to be returned.
135:             * @return the resulting value.
136:             * @throws Exception on any error.
137:             */
138:            public static Object evaluateExpr(Object o, Object loc)
139:                    throws Exception {
140:                /*
141:                 * following the JSTL EL rules
142:                 */
143:
144:                if (o == null) {
145:                    return null;
146:                }
147:
148:                if (loc == null) {
149:                    return null;
150:                }
151:
152:                if (o instanceof  Map) {
153:                    if (!((Map) o).containsKey(loc)) {
154:                        return null;
155:                    }
156:
157:                    return ((Map) o).get(loc);
158:                } else if (o instanceof  List) {
159:                    int idx = Coercion.coerceInteger(loc).intValue();
160:
161:                    try {
162:                        return ((List) o).get(idx);
163:                    } catch (IndexOutOfBoundsException iobe) {
164:                        return null;
165:                    }
166:                } else if (o.getClass().isArray()) {
167:                    int idx = Coercion.coerceInteger(loc).intValue();
168:
169:                    try {
170:                        return Array.get(o, idx);
171:                    } catch (ArrayIndexOutOfBoundsException aiobe) {
172:                        return null;
173:                    }
174:                } else {
175:                    /*
176:                     * "Otherwise (a JavaBean object)..." huh? :)
177:                     */
178:
179:                    String s = loc.toString();
180:
181:                    VelPropertyGet vg = Introspector.getUberspect()
182:                            .getPropertyGet(o, s, DUMMY);
183:
184:                    if (vg != null) {
185:                        return vg.invoke(o);
186:                    }
187:                }
188:
189:                throw new Exception(
190:                        "Unsupported object type for array [] accessor");
191:            }
192:
193:            /**
194:             * Gets the variable name piece of the expression.
195:             * @return a String of the identifer. 
196:             * @see ASTIdentifier#getIdentifierString().
197:             */
198:            public String getIdentifierString() {
199:                return ((ASTIdentifier) jjtGetChild(0)).getIdentifierString();
200:            }
201:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.