kinman      2003/01/27 16:13:39

  Modified:    jasper2/src/share/org/apache/jasper/compiler Generator.java
                        TagFileProcessor.java
               jasper2/src/share/org/apache/jasper/resources
                        messages.properties messages_es.properties
                        messages_fr.properties messages_ja.properties
               jasper2/src/share/org/apache/jasper/runtime
                        JspContextWrapper.java
  Log:
  - Implements name-from-attribute and alias in variable directives in tag file
  
  Revision  Changes    Path
  1.154     +66 -19    
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.153
  retrieving revision 1.154
  diff -u -r1.153 -r1.154
  --- Generator.java    22 Jan 2003 20:54:54 -0000      1.153
  +++ Generator.java    28 Jan 2003 00:13:37 -0000      1.154
  @@ -2478,6 +2478,43 @@
            return attrValue;
        }
   
  +     /**
  +      * Generate code to create a map for the alias variables
  +      * @return the name of the map
  +      */
  +     private String generateAliasMap(Node.CustomTag n, String tagHandlerVar)
  +             throws JasperException {
  +
  +         TagInfo tagInfo = n.getTagInfo();
  +         TagVariableInfo[] tagVars = tagInfo.getTagVariableInfos();
  +         String aliasMapVar = "null";
  +
  +         boolean aliasSeen = false;
  +         for (int i=0; i<tagVars.length; i++) {
  +
  +             String nameFrom = tagVars[i].getNameFromAttribute();
  +             if (nameFrom != null) {
  +                 String aliaseName = n.getAttributeValue(nameFrom);
  +                 if (aliaseName == null) continue;
  +
  +                 if ( ! aliasSeen ) {
  +                     out.printin("java.util.HashMap ");
  +                     aliasMapVar = tagHandlerVar+"_aliasMap";
  +                     out.print(aliasMapVar);
  +                     out.println(" = new java.util.HashMap();");
  +                     aliasSeen = true;
  +                 }
  +                 out.printin(aliasMapVar);
  +                 out.print(".put(");
  +                 out.print(quote(tagVars[i].getNameGiven()));
  +                 out.print(", ");
  +                 out.print(quote(aliaseName));
  +                 out.println(");");
  +             }
  +         }
  +         return aliasMapVar;
  +     }
  +
        private void generateSetters(Node.CustomTag n,
                                     String tagHandlerVar,
                                     TagHandlerInfo handlerInfo,
  @@ -2485,10 +2522,15 @@
                    throws JasperException {
   
            // Set context
  -         out.printin(tagHandlerVar);
            if (simpleTag) {
  -             out.println(".setJspContext(pageContext);");
  +             // Generate alias map 
  +             String aliasMapVar= generateAliasMap(n, tagHandlerVar);
  +             out.printin(tagHandlerVar);
  +             out.print(".setJspContext(pageContext, ");
  +             out.print(aliasMapVar);
  +             out.println(");");
            } else {
  +             out.printin(tagHandlerVar);
                out.println(".setPageContext(pageContext);");
            }
   
  @@ -3132,7 +3174,12 @@
        * sync AT_BEGIN and AT_END scripting variables.
        */
       private void generateSetJspContext(TagInfo tagInfo) {
  -        out.printil("public void setJspContext( JspContext ctx ) {");
  +
  +     boolean nestedSeen = false;
  +     boolean atBeginSeen = false;
  +     boolean atEndSeen = false;
  +
  +        out.printil("public void setJspContext(JspContext ctx, java.util.Map 
aliasMap) {");
           out.pushIndent();
           out.printil("super.setJspContext(ctx);");
        TagVariableInfo[] tagVars = tagInfo.getTagVariableInfos();
  @@ -3144,26 +3191,26 @@
   
            switch(tagVars[i].getScope()) {
            case VariableInfo.NESTED:
  -             out.printil("if (_jspx_nested == null)");
  -             out.pushIndent();
  -             out.printil("_jspx_nested = new java.util.ArrayList();");
  -             out.popIndent();
  +             if ( ! nestedSeen ) {
  +                 out.printil("_jspx_nested = new java.util.ArrayList();");
  +                 nestedSeen = true;
  +             }
                out.printin("_jspx_nested.add(");
                break;
   
            case VariableInfo.AT_BEGIN:
  -             out.printil("if (_jspx_at_begin == null)");
  -             out.pushIndent();
  -             out.printil("_jspx_at_begin = new java.util.ArrayList();");
  -             out.popIndent();
  +             if ( ! atBeginSeen ) {
  +                 out.printil("_jspx_at_begin = new java.util.ArrayList();");
  +                 atBeginSeen = true;
  +             }
                out.printin("_jspx_at_begin.add(");
                break;
   
            case VariableInfo.AT_END:
  -             out.printil("if (_jspx_at_end == null)");
  -             out.pushIndent();
  -             out.printil("_jspx_at_end = new java.util.ArrayList();");
  -             out.popIndent();
  +             if ( ! atEndSeen ) {
  +                 out.printil("_jspx_at_end = new java.util.ArrayList();");
  +                 atEndSeen = true;
  +             }
                out.printin("_jspx_at_end.add(");
                break;
            } // switch
  @@ -3171,7 +3218,7 @@
            out.print(quote(tagVars[i].getNameGiven()));
            out.println(");");
        }
  -     out.printil("this.jspContext = new 
org.apache.jasper.runtime.JspContextWrapper(ctx, _jspx_nested, _jspx_at_begin, 
_jspx_at_end);");
  +     out.printil("this.jspContext = new 
org.apache.jasper.runtime.JspContextWrapper(ctx, _jspx_nested, _jspx_at_begin, 
_jspx_at_end, aliasMap);");
        out.popIndent();
           out.printil("}");
           out.println();
  
  
  
  1.38      +69 -12    
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagFileProcessor.java
  
  Index: TagFileProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagFileProcessor.java,v
  retrieving revision 1.37
  retrieving revision 1.38
  diff -u -r1.37 -r1.38
  --- TagFileProcessor.java     18 Dec 2002 23:18:21 -0000      1.37
  +++ TagFileProcessor.java     28 Jan 2003 00:13:37 -0000      1.38
  @@ -113,7 +113,9 @@
        };
   
        private static final JspUtil.ValidAttribute[] variableDirectiveAttrs = {
  -         new JspUtil.ValidAttribute("name-given", true),
  +         new JspUtil.ValidAttribute("name-given"),
  +         new JspUtil.ValidAttribute("name-from-attribute"),
  +         new JspUtil.ValidAttribute("alias"),
            new JspUtil.ValidAttribute("variable-class"),
            new JspUtil.ValidAttribute("scope"),
            new JspUtil.ValidAttribute("declare"),
  @@ -210,6 +212,21 @@
                                       variableDirectiveAttrs, err);
   
               String nameGiven = n.getAttributeValue("name-given");
  +            String nameFromAttribute = n.getAttributeValue("name-from-attribute");
  +         if (nameGiven == null && nameFromAttribute == null) {
  +             err.jspError("jsp.variable.either.name");
  +         }
  +
  +         if (nameGiven != null && nameFromAttribute != null) {
  +             err.jspError("jsp.variable.both.name");
  +         }
  +
  +         String alias = n.getAttributeValue("alias");
  +         if (nameFromAttribute != null && alias == null ||
  +             nameFromAttribute == null && alias != null) {
  +             err.jspError("jsp.variable.alias");
  +         }
  +
               String className = n.getAttributeValue("variable-class");
               if (className == null)
                   className = "java.lang.String";
  @@ -231,11 +248,19 @@
                   }
               }
   
  -         variableVector.addElement(new TagVariableInfo(nameGiven,
  -                                                       null,
  -                                                       className,
  -                                                       declare,
  -                                                       scope));
  +         if (nameFromAttribute != null) {
  +             // An alias has been specified.  We use nameGiven to
  +             // hold the value of alias, and nameFromAttribute to
  +             // hold what its alias
  +             nameGiven = alias;
  +         }
  +             
  +         variableVector.addElement(new TagVariableInfo(
  +                                             nameGiven,
  +                                             nameFromAttribute,
  +                                             className,
  +                                             declare,
  +                                             scope));
           }
   
        /*
  @@ -323,8 +348,13 @@
           page.visit(tagFileVisitor);
   
        /*
  -      * It is illegal to have a variable.name-given equal to an
  -      * attribute.name in the same tag file translation unit.
  +      * TODO: need to check for uniqueness of attribute name, variable
  +      * name-given, and vraibale alias.
  +      */
  +
  +     /*
  +      * It is illegal to have a variable.name-given or variable.alias equal
  +         * to an attribute.name in the same tag file translation unit.
         */
        Iterator attrsIter = tagFileVisitor.getAttributesVector().iterator();
        while (attrsIter.hasNext()) {
  @@ -332,9 +362,36 @@
            Iterator varsIter = tagFileVisitor.getVariablesVector().iterator();
            while (varsIter.hasNext()) {
                TagVariableInfo varInfo = (TagVariableInfo) varsIter.next();
  -             if (attrInfo.getName().equals(varInfo.getNameGiven())) {
  +             String attrName = attrInfo.getName();
  +             if (attrName.equals(varInfo.getNameGiven())) {
                    err.jspError("jsp.error.tagfile.var_name_given_equals_attr_name",
  -                              path, attrInfo.getName());
  +                              path, attrName);
  +             }
  +         }
  +     }
  +     /*
  +      * It is illegal to have a variable.name-given equal another
  +      * variable.alias the same tag file translation unit.
  +      */
  +     Iterator varsIter = tagFileVisitor.getVariablesVector().iterator();
  +     while (varsIter.hasNext()) {
  +         TagVariableInfo varsInfo = (TagVariableInfo) varsIter.next();
  +         if (varsInfo.getNameFromAttribute() == null) {
  +             // Only interested in aliases.
  +             continue;
  +         }
  +         String nameGiven = varsInfo.getNameGiven();
  +
  +         Iterator varsIter2 = tagFileVisitor.getVariablesVector().iterator();
  +         while (varsIter2.hasNext()) {
  +             TagVariableInfo varsInfo2 = (TagVariableInfo) varsIter2.next();
  +             if (varsInfo2.getNameFromAttribute() != null) {
  +                 // Only interest in non-aliases
  +                 continue;
  +             }
  +             if (nameGiven.equals(varsInfo2.getNameGiven())) {
  +                 err.jspError("jsp.error.tagfile.nameGiven_equals.alias",
  +                             path, nameGiven);
                }
            }
        }
  
  
  
  1.85      +5 -2      
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages.properties
  
  Index: messages.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages.properties,v
  retrieving revision 1.84
  retrieving revision 1.85
  diff -u -r1.84 -r1.85
  --- messages.properties       27 Jan 2003 18:10:47 -0000      1.84
  +++ messages.properties       28 Jan 2003 00:13:38 -0000      1.85
  @@ -313,7 +313,7 @@
   jsp.error.attribute.standard.non_rt_with_expr=The {0} attribute of the {1} standard 
action does not accept any expressions
   jsp.error.scripting.variable.missing_name=Unable to determine scripting variable 
name from attribute {0}
   jasper.error.emptybodycontent.nonempty=According to TLD, tag {0} must be empty, but 
is not
  -jsp.error.tagfile.var_name_given_equals_attr_name=In tag file {0}, the name-given 
attribute ({1}) of a variable directive equals the name attribute of an attribute 
directive
  +jsp.error.tagfile.var_name_given_equals_attr_name=In tag file {0}, the name-given 
attribute or the alias attribute ({1}) of a variable directive equals the name 
attribute of an attribute directive
   jsp.error.useBean.noSession=Illegal for useBean to use session scope when JSP page 
declares (via page directive) that it does not participate in sessions
   jsp.error.xml.encodingByteOrderUnsupported = Given byte order for encoding \"{0}\" 
is not supported.
   jsp.error.xml.encodingDeclInvalid = Invalid encoding name \"{0}\".
  @@ -356,3 +356,6 @@
   jsp.error.attribute.invalidPrefix=The attribute prefix {0} does not correspond to 
any imported tag library
   jsp.error.nested.jspattribute=a jsp:attribute standard action cannot be nested 
within another jsp:attribute standard action
   jsp.error.nested.jspbody=a jsp:body standard action cannot be nested within another 
jsp:body or jsp:attribute standard action
  +jsp.error.variable.either.name=either name-given or name-from-attribute attribute 
must be specified in a variable directive
  +jsp.error.variable.both.name=cannot specified both name-given or 
name-from-attribute attributes in a variable directive
  +jsp.error.variable.alias=both or none of the name-from-attribute and alias 
attributes can be specified in a variable directive.
  
  
  
  1.31      +4 -1      
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_es.properties
  
  Index: messages_es.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_es.properties,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- messages_es.properties    27 Jan 2003 18:10:47 -0000      1.30
  +++ messages_es.properties    28 Jan 2003 00:13:38 -0000      1.31
  @@ -260,3 +260,6 @@
   jsp.error.attributes.not.allowed=
   jsp.error.nested.jspattribute=
   jsp.error.nested.jspbody=
  +jsp.error.variable.either.name=
  +jsp.error.variable.both.name=
  +jsp.error.variable.alias=
  
  
  
  1.14      +5 -1      
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_fr.properties
  
  Index: messages_fr.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_fr.properties,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- messages_fr.properties    27 Jan 2003 18:10:47 -0000      1.13
  +++ messages_fr.properties    28 Jan 2003 00:13:38 -0000      1.14
  @@ -299,3 +299,7 @@
   jsp.error.attributes.not.allowed = {0} ne doit avoir aucun attribut
   jsp.error.nested.jspattribute=
   jsp.error.nested.jspbody=
  +jsp.error.variable.either.name=
  +jsp.error.variable.both.name=
  +jsp.error.variable.alias=
  +
  
  
  
  1.31      +5 -1      
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_ja.properties
  
  Index: messages_ja.properties
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/resources/messages_ja.properties,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- messages_ja.properties    27 Jan 2003 18:10:47 -0000      1.30
  +++ messages_ja.properties    28 Jan 2003 00:13:38 -0000      1.31
  @@ -291,3 +291,7 @@
   jsp.error.attributes.not.allowed=
   jsp.error.nested.jspattribute=
   jsp.error.nested.jspbody=
  +jsp.error.variable.either.name=
  +jsp.error.variable.both.name=
  +jsp.error.variable.alias=
  +
  
  
  
  1.11      +31 -5     
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/JspContextWrapper.java
  
  Index: JspContextWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/JspContextWrapper.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- JspContextWrapper.java    18 Dec 2002 18:46:59 -0000      1.10
  +++ JspContextWrapper.java    28 Jan 2003 00:13:39 -0000      1.11
  @@ -68,6 +68,7 @@
   import java.util.Hashtable;
   import java.util.ArrayList;
   import java.util.Iterator;
  +import java.util.Map;
   
   import javax.servlet.Servlet;
   import javax.servlet.ServletConfig;
  @@ -115,15 +116,20 @@
       // ArrayList of AT_END scripting variables
       private ArrayList atEndVars;
   
  +    private Map aliases;
  +
       private Hashtable originalNestedVars;
   
       public JspContextWrapper(JspContext jspContext, ArrayList nestedVars,
  -                          ArrayList atBeginVars, ArrayList atEndVars) {
  +                          ArrayList atBeginVars, ArrayList atEndVars,
  +                          Map aliases) {
           this.invokingJspCtxt = (PageContext) jspContext;
        this.nestedVars = nestedVars;
        this.atBeginVars = atBeginVars;
        this.atEndVars = atEndVars;
        this.pageAttributes = new Hashtable(16);
  +     this.aliases = aliases;
  +
        if (nestedVars != null) {
            this.originalNestedVars = new Hashtable(nestedVars.size());
        }
  @@ -338,7 +344,9 @@
   
        while ((iter != null) && iter.hasNext()) {
            String varName = (String) iter.next();
  -         Object obj = invokingJspCtxt.getAttribute(varName);
  +         String aliasName = findAlias(varName);
  +
  +         Object obj = invokingJspCtxt.getAttribute(aliasName);
            if (obj != null) {
                setAttribute(varName, obj);
            }
  @@ -375,6 +383,7 @@
        while ((iter != null) && iter.hasNext()) {
            String varName = (String) iter.next();
            Object obj = getAttribute(varName);
  +         varName = findAlias(varName);
            if (obj != null) {
                invokingJspCtxt.setAttribute(varName, obj);
            } else {
  @@ -392,6 +401,7 @@
            Iterator iter = nestedVars.iterator();
            while (iter.hasNext()) {
                String varName = (String) iter.next();
  +             varName = findAlias(varName);
                Object obj = invokingJspCtxt.getAttribute(varName);
                if (obj != null) {
                    originalNestedVars.put(varName, obj);
  @@ -409,6 +419,7 @@
            Iterator iter = nestedVars.iterator();
            while (iter.hasNext()) {
                String varName = (String) iter.next();
  +             varName = findAlias(varName);
                Object obj = originalNestedVars.get(varName);
                if (obj != null) {
                    invokingJspCtxt.setAttribute(varName, obj);
  @@ -417,6 +428,21 @@
                }
            }
        }
  +    }
  +
  +    /**
  +     * Find the attribute that variable is alised to.
  +     * @param varName a variable
  +     * @return if varName is an alias, then the aliased variable
  +     *         otherwise varName
  +     */
  +    private String findAlias(String varName) {
  +
  +     String alias = (String) aliases.get(varName);
  +     if (alias == null) {
  +         return varName;
  +     }
  +     return alias;
       }
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to