private void doMap()

in src/main/java/org/apache/sling/scripting/jsp/jasper/compiler/ELFunctionMapper.java [147:244]


        private void doMap(ELNode.Nodes el) 
                throws JasperException {

            // Only care about functions in ELNode's
            class Fvisitor extends ELNode.Visitor {
                ArrayList<ELNode.Function> funcs =
                    new ArrayList<ELNode.Function>();
                HashMap<String, String> keyMap = new HashMap<String, String>();
                public void visit(ELNode.Function n) throws JasperException {
                    String key = n.getPrefix() + ":" + n.getName();
                    if (! keyMap.containsKey(key)) {
                        keyMap.put(key,"");
                        funcs.add(n);
                    }
                }
            }

            if (el == null) {
                return;
            }

            // First locate all unique functions in this EL
            Fvisitor fv = new Fvisitor();
            el.visit(fv);
            ArrayList functions = fv.funcs;

            if (functions.size() == 0) {
                return;
            }

            // Reuse a previous map if possible
            String decName = matchMap(functions);
            if (decName != null) {
                el.setMapName(decName);
                return;
            }
        
            // Generate declaration for the map statically
            decName = getMapName();
            ss.append("static private org.apache.sling.scripting.jsp.jasper.runtime.ProtectedFunctionMapper " + decName + ";\n");

            ds.append("  " + decName + "= ");
            ds.append("org.apache.sling.scripting.jsp.jasper.runtime.ProtectedFunctionMapper");

            // Special case if there is only one function in the map
            String funcMethod = null;
            if (functions.size() == 1) {
                funcMethod = ".getMapForFunction";
            } else {
                ds.append(".getInstance();\n");
                funcMethod = "  " + decName + ".mapFunction";
            }

            // Setup arguments for either getMapForFunction or mapFunction
            for (int i = 0; i < functions.size(); i++) {
                ELNode.Function f = (ELNode.Function)functions.get(i);
                FunctionInfo funcInfo = f.getFunctionInfo();
                String key = f.getPrefix()+ ":" + f.getName();
                ds.append(funcMethod + "(\"" + key + "\", " +
                        funcInfo.getFunctionClass() + ".class, " +
                        '\"' + f.getMethodName() + "\", " +
                        "new Class[] {");
                String params[] = f.getParameters();
                for (int k = 0; k < params.length; k++) {
                    if (k != 0) {
                        ds.append(", ");
                    }
                    int iArray = params[k].indexOf('[');
                    if (iArray < 0) {
                        ds.append(params[k] + ".class");
                    }
                    else {
                        String baseType = params[k].substring(0, iArray);
                        ds.append("java.lang.reflect.Array.newInstance(");
                        ds.append(baseType);
                        ds.append(".class,");

                        // Count the number of array dimension
                        int aCount = 0;
                        for (int jj = iArray; jj < params[k].length(); jj++ ) {
                            if (params[k].charAt(jj) == '[') {
                                aCount++;
                            }
                        }
                        if (aCount == 1) {
                            ds.append("0).getClass()");
                        } else {
                            ds.append("new int[" + aCount + "]).getClass()");
                        }
                    }
                }
                ds.append("});\n");
                // Put the current name in the global function map
                gMap.put(f.getPrefix() + ':' + f.getName() + ':' + f.getUri(),
                         decName);
            }
            el.setMapName(decName);
        }