kinman 2002/06/20 17:59:56 Modified: jasper2/src/share/org/apache/jasper/compiler Generator.java Node.java Log: - Fixed the last bug in nested tags costin discovered. :-) Patch supplied by Jan Luehe. Revision Changes Path 1.32 +68 -31 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java Index: Generator.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v retrieving revision 1.31 retrieving revision 1.32 diff -u -r1.31 -r1.32 --- Generator.java 20 Jun 2002 20:12:26 -0000 1.31 +++ Generator.java 21 Jun 2002 00:59:56 -0000 1.32 @@ -1471,14 +1471,21 @@ /* * Declares any NESTED scripting variables of the given custom tag, * if the given custom tag is not nested inside itself (i.e, has a - * nesting level of zero). In addition, a NESTED scripting variable is - * declared only if it has not already been declared in the same scope - * in the generated code, that is, if this custom tag's parent is + * nesting level of zero). If the custom tag does have a custom nesting + * level greater than 0, this method declares a scripting variable + * derived from the tag's "id" attribute (if present), only if its + * scope is NESTED and it does not match the "id" attribute of any of + * the enclosing tags of the same (custom) type. + * + * Additionally, a NESTED scripting variable is declared only if it + * has not already been declared in the same Java + * scope of the generated code, that is, if this custom tag's parent is * different from the parent of the custom tag that may already have * declared this variable. */ private void declareNestedScriptingVariables(Node.CustomTag n) { - if (n.getCustomNestingLevel() > 0) { + + if (n.getCustomNestingLevel() > 0 && !n.hasUnnestedIdAttribute()) { return; } @@ -1489,17 +1496,42 @@ } if (varInfos != null) { - for (int i=0; i<varInfos.length; i++) { - if ((varInfos[i].getScope() == VariableInfo.NESTED) - && varInfos[i].getDeclare()) { - String name = varInfos[i].getVarName(); - Node parent = (Node) nestedVars.get(name); - if ((parent == null) || (parent != n.getParent())) { - out.printin(varInfos[i].getClassName()); - out.print(" "); - out.print(name); - out.println(";"); - nestedVars.put(name, n.getParent()); + if (n.getCustomNestingLevel() == 0) { + // Declare *any* scripting variables with NESTED scope + for (int i=0; i<varInfos.length; i++) { + if ((varInfos[i].getScope() == VariableInfo.NESTED) + && varInfos[i].getDeclare()) { + String name = varInfos[i].getVarName(); + Node parent = (Node) nestedVars.get(name); + if (parent == null || parent != n.getParent()) { + out.printin(varInfos[i].getClassName()); + out.print(" "); + out.print(name); + out.println(";"); + nestedVars.put(name, n.getParent()); + } + } + } + } else { + /* + * Declare only scripting variable (with NESTED scope) + * derived from unnested "id" attribute + */ + String idAttr = n.getAttributeValue("id"); + for (int i=0; i<varInfos.length; i++) { + if ((varInfos[i].getScope() == VariableInfo.NESTED) + && varInfos[i].getDeclare()) { + String name = varInfos[i].getVarName(); + Node parent = (Node) nestedVars.get(name); + if ((parent == null || parent != n.getParent()) + && idAttr.equals(name)) { + out.printin(varInfos[i].getClassName()); + out.print(" "); + out.print(name); + out.println(" = null;"); + nestedVars.put(name, n.getParent()); + break; + } } } } @@ -1526,10 +1558,12 @@ } /* - * For every scripting variable exposed by this custom tag, declares - * a variable where the current value of the scripting variable may - * be saved, so it can later be restored in this custom tag's end - * element. + * This method is called as part of the custom tag's start element. + * + * If the given custom tag has a custom nesting level greater than 0, + * declare temporary variables where the current values of the tag's + * scripting variables may be saved, so these values may be restored + * in the tag's end element. */ private void declareTemporaryScriptingVariables(Node.CustomTag n) { if (n.getCustomNestingLevel() == 0) { @@ -1577,12 +1611,13 @@ } /* - * For each scripting variable of a custom tag with a nesting level - * greater than 0, save its value to a temporary variable so that the - * scripting variable can be synchronized inside the nested custom tag - * without affecting the value it had at the start element of the - * custom tag, which will be restored when the end element of the - * custom tag is reached. + * This method is called as part of the custom tag's start element. + * + * If the given custom tag has a custom nesting level greater than 0, + * save the values of all its scripting variables to temporary + * variables, so the scripting variables may be synchronized + * without affecting the values they had at the tag's start element. + * Those values will be restored when the tag's end element is reached. */ private void saveScriptingVariables(Node.CustomTag n) { if (n.getCustomNestingLevel() == 0) { @@ -1623,9 +1658,11 @@ } /* - * For each scripting variable of a custom tag with a nesting level - * greater than 0, restore its original value that was saved in the - * start element of the custom tag. + * This method is called as part of the custom tag's end element. + * + * If the given custom tag has a custom nesting level greater than 0, + * restore its scripting variables to their original values that were + * saved in the tag's start element. */ private void restoreScriptingVariables(Node.CustomTag n) { if (n.getCustomNestingLevel() == 0) { 1.15 +53 -9 jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java Index: Node.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Node.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- Node.java 13 Jun 2002 22:56:11 -0000 1.14 +++ Node.java 21 Jun 2002 00:59:56 -0000 1.15 @@ -673,7 +673,8 @@ private String tagHandlerPoolName; private TagInfo tagInfo; private VariableInfo[] varInfos; - private int nestingLevel; + private int customNestingLevel; + private boolean hasUnnestedIdAttribute; public CustomTag(Attributes attrs, Mark start, String name, String prefix, String shortName, @@ -683,7 +684,8 @@ this.prefix = prefix; this.shortName = shortName; this.tagInfo = tagInfo; - this.nestingLevel = computeCustomNestingLevel(); + this.customNestingLevel = computeCustomNestingLevel(); + this.hasUnnestedIdAttribute = determineHasUnnestedIdAttribute(); } public void accept(Visitor v) throws JasperException { @@ -789,15 +791,25 @@ } /* - * Gets this custom tag's nesting level. + * Returns true if this custom tag has an "id" attribute that does + * not match the "id" attribute of any of its enclosing parent tags of + * the same custom tag type, and false otherwise. + */ + public boolean hasUnnestedIdAttribute() { + return hasUnnestedIdAttribute; + } + + /* + * Gets this custom tag's custom nesting level, which is given as + * the number of times this custom tag is nested inside itself. */ public int getCustomNestingLevel() { - return nestingLevel; + return customNestingLevel; } /* - * Computes this custom tag's nesting level, which corresponds to the - * number of times this custom tag is nested inside itself. + * Computes this custom tag's custom nesting level, which corresponds + * to the number of times this custom tag is nested inside itself. * * Example: * @@ -829,6 +841,38 @@ p = p.parent; } return n; + } + + /* + * Checks to see if this custom tag has an "id" attribute that does + * not match the "id" attribute of any of its enclosing parent tags of + * the same custom tag type. + */ + private boolean determineHasUnnestedIdAttribute() { + boolean unnested = false; + + String id = getAttributeValue("id"); + if (id != null) { + if (customNestingLevel == 0) { + // tag not nested inside itself + unnested = true; + } else { + Node p = parent; + while (p != null) { + if ((p instanceof Node.CustomTag) + && name.equals(((Node.CustomTag) p).name) + && id.equals(p.getAttributeValue("id"))) { + break; + } + p = p.parent; + } + if (p == null) { + unnested = true; + } + } + } + + return unnested; } }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>