1 17 18 package org.apache.jasper.compiler; 19 20 import java.util.*; 21 import javax.servlet.jsp.tagext.*; 22 import org.apache.jasper.JasperException; 23 24 30 class ScriptingVariabler { 31 32 private static final Integer MAX_SCOPE = new Integer (Integer.MAX_VALUE); 33 34 39 static class CustomTagCounter extends Node.Visitor { 40 41 private int count; 42 private Node.CustomTag parent; 43 44 public void visit(Node.CustomTag n) throws JasperException { 45 n.setCustomTagParent(parent); 46 Node.CustomTag tmpParent = parent; 47 parent = n; 48 visitBody(n); 49 parent = tmpParent; 50 n.setNumCount(new Integer (count++)); 51 } 52 } 53 54 58 static class ScriptingVariableVisitor extends Node.Visitor { 59 60 private ErrorDispatcher err; 61 private Hashtable scriptVars; 62 63 public ScriptingVariableVisitor(ErrorDispatcher err) { 64 this.err = err; 65 scriptVars = new Hashtable(); 66 } 67 68 public void visit(Node.CustomTag n) throws JasperException { 69 setScriptingVars(n, VariableInfo.AT_BEGIN); 70 setScriptingVars(n, VariableInfo.NESTED); 71 visitBody(n); 72 setScriptingVars(n, VariableInfo.AT_END); 73 } 74 75 private void setScriptingVars(Node.CustomTag n, int scope) 76 throws JasperException { 77 78 TagVariableInfo[] tagVarInfos = n.getTagVariableInfos(); 79 VariableInfo[] varInfos = n.getVariableInfos(); 80 if (tagVarInfos.length == 0 && varInfos.length == 0) { 81 return; 82 } 83 84 Vector vec = new Vector(); 85 86 Integer ownRange = null; 87 if (scope == VariableInfo.AT_BEGIN 88 || scope == VariableInfo.AT_END) { 89 Node.CustomTag parent = n.getCustomTagParent(); 90 if (parent == null) 91 ownRange = MAX_SCOPE; 92 else 93 ownRange = parent.getNumCount(); 94 } else { 95 ownRange = n.getNumCount(); 97 } 98 99 if (varInfos.length > 0) { 100 for (int i=0; i<varInfos.length; i++) { 101 if (varInfos[i].getScope() != scope 102 || !varInfos[i].getDeclare()) { 103 continue; 104 } 105 String varName = varInfos[i].getVarName(); 106 107 Integer currentRange = (Integer ) scriptVars.get(varName); 108 if (currentRange == null 109 || ownRange.compareTo(currentRange) > 0) { 110 scriptVars.put(varName, ownRange); 111 vec.add(varInfos[i]); 112 } 113 } 114 } else { 115 for (int i=0; i<tagVarInfos.length; i++) { 116 if (tagVarInfos[i].getScope() != scope 117 || !tagVarInfos[i].getDeclare()) { 118 continue; 119 } 120 String varName = tagVarInfos[i].getNameGiven(); 121 if (varName == null) { 122 varName = n.getTagData().getAttributeString( 123 tagVarInfos[i].getNameFromAttribute()); 124 if (varName == null) { 125 err.jspError(n, "jsp.error.scripting.variable.missing_name", 126 tagVarInfos[i].getNameFromAttribute()); 127 } 128 } 129 130 Integer currentRange = (Integer ) scriptVars.get(varName); 131 if (currentRange == null 132 || ownRange.compareTo(currentRange) > 0) { 133 scriptVars.put(varName, ownRange); 134 vec.add(tagVarInfos[i]); 135 } 136 } 137 } 138 139 n.setScriptingVars(vec, scope); 140 } 141 } 142 143 public static void set(Node.Nodes page, ErrorDispatcher err) 144 throws JasperException { 145 page.visit(new CustomTagCounter()); 146 page.visit(new ScriptingVariableVisitor(err)); 147 } 148 } 149 | Popular Tags |