Source Code Cross Referenced for ELFunctionMapper.java in  » Sevlet-Container » apache-tomcat-6.0.14 » org » apache » jasper » compiler » 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 » Sevlet Container » apache tomcat 6.0.14 » org.apache.jasper.compiler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:
018:        package org.apache.jasper.compiler;
019:
020:        import java.util.*;
021:        import javax.servlet.jsp.tagext.FunctionInfo;
022:        import org.apache.jasper.JasperException;
023:
024:        /**
025:         * This class generates functions mappers for the EL expressions in the page.
026:         * Instead of a global mapper, a mapper is used for ecah call to EL
027:         * evaluator, thus avoiding the prefix overlapping and redefinition
028:         * issues.
029:         *
030:         * @author Kin-man Chung
031:         */
032:
033:        public class ELFunctionMapper {
034:            private int currFunc = 0;
035:            StringBuffer ds; // Contains codes to initialize the functions mappers.
036:            StringBuffer ss; // Contains declarations of the functions mappers.
037:
038:            /**
039:             * Creates the functions mappers for all EL expressions in the JSP page.
040:             *
041:             * @param compiler Current compiler, mainly for accessing error dispatcher.
042:             * @param page The current compilation unit.
043:             */
044:            public static void map(Compiler compiler, Node.Nodes page)
045:                    throws JasperException {
046:
047:                ELFunctionMapper map = new ELFunctionMapper();
048:                map.ds = new StringBuffer();
049:                map.ss = new StringBuffer();
050:
051:                page.visit(map.new ELFunctionVisitor());
052:
053:                // Append the declarations to the root node
054:                String ds = map.ds.toString();
055:                if (ds.length() > 0) {
056:                    Node root = page.getRoot();
057:                    new Node.Declaration(map.ss.toString(), null, root);
058:                    new Node.Declaration("static {\n" + ds + "}\n", null, root);
059:                }
060:            }
061:
062:            /**
063:             * A visitor for the page.  The places where EL is allowed are scanned
064:             * for functions, and if found functions mappers are created.
065:             */
066:            class ELFunctionVisitor extends Node.Visitor {
067:
068:                /**
069:                 * Use a global name map to facilitate reuse of function maps.
070:                 * The key used is prefix:function:uri.
071:                 */
072:                private HashMap<String, String> gMap = new HashMap<String, String>();
073:
074:                public void visit(Node.ParamAction n) throws JasperException {
075:                    doMap(n.getValue());
076:                    visitBody(n);
077:                }
078:
079:                public void visit(Node.IncludeAction n) throws JasperException {
080:                    doMap(n.getPage());
081:                    visitBody(n);
082:                }
083:
084:                public void visit(Node.ForwardAction n) throws JasperException {
085:                    doMap(n.getPage());
086:                    visitBody(n);
087:                }
088:
089:                public void visit(Node.SetProperty n) throws JasperException {
090:                    doMap(n.getValue());
091:                    visitBody(n);
092:                }
093:
094:                public void visit(Node.UseBean n) throws JasperException {
095:                    doMap(n.getBeanName());
096:                    visitBody(n);
097:                }
098:
099:                public void visit(Node.PlugIn n) throws JasperException {
100:                    doMap(n.getHeight());
101:                    doMap(n.getWidth());
102:                    visitBody(n);
103:                }
104:
105:                public void visit(Node.JspElement n) throws JasperException {
106:
107:                    Node.JspAttribute[] attrs = n.getJspAttributes();
108:                    for (int i = 0; attrs != null && i < attrs.length; i++) {
109:                        doMap(attrs[i]);
110:                    }
111:                    doMap(n.getNameAttribute());
112:                    visitBody(n);
113:                }
114:
115:                public void visit(Node.UninterpretedTag n)
116:                        throws JasperException {
117:
118:                    Node.JspAttribute[] attrs = n.getJspAttributes();
119:                    for (int i = 0; attrs != null && i < attrs.length; i++) {
120:                        doMap(attrs[i]);
121:                    }
122:                    visitBody(n);
123:                }
124:
125:                public void visit(Node.CustomTag n) throws JasperException {
126:                    Node.JspAttribute[] attrs = n.getJspAttributes();
127:                    for (int i = 0; attrs != null && i < attrs.length; i++) {
128:                        doMap(attrs[i]);
129:                    }
130:                    visitBody(n);
131:                }
132:
133:                public void visit(Node.ELExpression n) throws JasperException {
134:                    doMap(n.getEL());
135:                }
136:
137:                private void doMap(Node.JspAttribute attr)
138:                        throws JasperException {
139:                    if (attr != null) {
140:                        doMap(attr.getEL());
141:                    }
142:                }
143:
144:                /**
145:                 * Creates function mappers, if needed, from ELNodes
146:                 */
147:                private void doMap(ELNode.Nodes el) throws JasperException {
148:
149:                    // Only care about functions in ELNode's
150:                    class Fvisitor extends ELNode.Visitor {
151:                        ArrayList<ELNode.Function> funcs = new ArrayList<ELNode.Function>();
152:                        HashMap<String, String> keyMap = new HashMap<String, String>();
153:
154:                        public void visit(ELNode.Function n)
155:                                throws JasperException {
156:                            String key = n.getPrefix() + ":" + n.getName();
157:                            if (!keyMap.containsKey(key)) {
158:                                keyMap.put(key, "");
159:                                funcs.add(n);
160:                            }
161:                        }
162:                    }
163:
164:                    if (el == null) {
165:                        return;
166:                    }
167:
168:                    // First locate all unique functions in this EL
169:                    Fvisitor fv = new Fvisitor();
170:                    el.visit(fv);
171:                    ArrayList functions = fv.funcs;
172:
173:                    if (functions.size() == 0) {
174:                        return;
175:                    }
176:
177:                    // Reuse a previous map if possible
178:                    String decName = matchMap(functions);
179:                    if (decName != null) {
180:                        el.setMapName(decName);
181:                        return;
182:                    }
183:
184:                    // Generate declaration for the map statically
185:                    decName = getMapName();
186:                    ss
187:                            .append("static private org.apache.jasper.runtime.ProtectedFunctionMapper "
188:                                    + decName + ";\n");
189:
190:                    ds.append("  " + decName + "= ");
191:                    ds
192:                            .append("org.apache.jasper.runtime.ProtectedFunctionMapper");
193:
194:                    // Special case if there is only one function in the map
195:                    String funcMethod = null;
196:                    if (functions.size() == 1) {
197:                        funcMethod = ".getMapForFunction";
198:                    } else {
199:                        ds.append(".getInstance();\n");
200:                        funcMethod = "  " + decName + ".mapFunction";
201:                    }
202:
203:                    // Setup arguments for either getMapForFunction or mapFunction
204:                    for (int i = 0; i < functions.size(); i++) {
205:                        ELNode.Function f = (ELNode.Function) functions.get(i);
206:                        FunctionInfo funcInfo = f.getFunctionInfo();
207:                        String key = f.getPrefix() + ":" + f.getName();
208:                        ds.append(funcMethod + "(\"" + key + "\", "
209:                                + funcInfo.getFunctionClass() + ".class, "
210:                                + '\"' + f.getMethodName() + "\", "
211:                                + "new Class[] {");
212:                        String params[] = f.getParameters();
213:                        for (int k = 0; k < params.length; k++) {
214:                            if (k != 0) {
215:                                ds.append(", ");
216:                            }
217:                            int iArray = params[k].indexOf('[');
218:                            if (iArray < 0) {
219:                                ds.append(params[k] + ".class");
220:                            } else {
221:                                String baseType = params[k]
222:                                        .substring(0, iArray);
223:                                ds
224:                                        .append("java.lang.reflect.Array.newInstance(");
225:                                ds.append(baseType);
226:                                ds.append(".class,");
227:
228:                                // Count the number of array dimension
229:                                int aCount = 0;
230:                                for (int jj = iArray; jj < params[k].length(); jj++) {
231:                                    if (params[k].charAt(jj) == '[') {
232:                                        aCount++;
233:                                    }
234:                                }
235:                                if (aCount == 1) {
236:                                    ds.append("0).getClass()");
237:                                } else {
238:                                    ds.append("new int[" + aCount
239:                                            + "]).getClass()");
240:                                }
241:                            }
242:                        }
243:                        ds.append("});\n");
244:                        // Put the current name in the global function map
245:                        gMap.put(f.getPrefix() + ':' + f.getName() + ':'
246:                                + f.getUri(), decName);
247:                    }
248:                    el.setMapName(decName);
249:                }
250:
251:                /**
252:                 * Find the name of the function mapper for an EL.  Reuse a
253:                 * previously generated one if possible.
254:                 * @param functions An ArrayList of ELNode.Function instances that
255:                 *                  represents the functions in an EL
256:                 * @return A previous generated function mapper name that can be used
257:                 *         by this EL; null if none found.
258:                 */
259:                private String matchMap(ArrayList functions) {
260:
261:                    String mapName = null;
262:                    for (int i = 0; i < functions.size(); i++) {
263:                        ELNode.Function f = (ELNode.Function) functions.get(i);
264:                        String temName = (String) gMap.get(f.getPrefix() + ':'
265:                                + f.getName() + ':' + f.getUri());
266:                        if (temName == null) {
267:                            return null;
268:                        }
269:                        if (mapName == null) {
270:                            mapName = temName;
271:                        } else if (!temName.equals(mapName)) {
272:                            // If not all in the previous match, then no match.
273:                            return null;
274:                        }
275:                    }
276:                    return mapName;
277:                }
278:
279:                /*
280:                 * @return An unique name for a function mapper.
281:                 */
282:                private String getMapName() {
283:                    return "_jspx_fnmap_" + currFunc++;
284:                }
285:            }
286:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.