luehe       2002/12/05 15:56:40

  Modified:    jasper2  build.xml
               jasper2/src/share/org/apache/jasper/compiler Compiler.java
                        Generator.java Node.java TagPluginManager.java
  Added:       jasper2/src/share/org/apache/jasper/tagplugins/jstl
                        ForEach.java
  Log:
  First cut at plugin implementation for JSTL's <c:forEach> action.
  
  Revision  Changes    Path
  1.18      +1 -0      jakarta-tomcat-jasper/jasper2/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-jasper/jasper2/build.xml,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- build.xml 30 Aug 2002 18:04:50 -0000      1.17
  +++ build.xml 5 Dec 2002 23:56:39 -0000       1.18
  @@ -134,6 +134,7 @@
           <include name="org/apache/jasper/compiler/**" />
           <include name="org/apache/jasper/xmlparser/**" />
           <include name="org/apache/jasper/servlet/**" />
  +        <include name="org/apache/jasper/tagplugins/**" />
           <exclude name="org/apache/jasper/Constants.class" />
           <exclude name="org/apache/jasper/JasperException.class" />
           <include name="org/apache/jasper/*.class" />
  
  
  
  1.40      +1 -1      
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java
  
  Index: Compiler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Compiler.java,v
  retrieving revision 1.39
  retrieving revision 1.40
  diff -u -r1.39 -r1.40
  --- Compiler.java     4 Dec 2002 00:48:42 -0000       1.39
  +++ Compiler.java     5 Dec 2002 23:56:39 -0000       1.40
  @@ -291,7 +291,7 @@
   
        // Optimizations by Tag Plugins
        TagPluginManager tagPluginManager = options.getTagPluginManager();
  -     tagPluginManager.apply(pageNodes);
  +     tagPluginManager.apply(pageNodes, errDispatcher);
   
        // generate servlet .java file
        Generator.generate(writer, this, pageNodes);
  
  
  
  1.138     +6 -6      
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.137
  retrieving revision 1.138
  diff -u -r1.137 -r1.138
  --- Generator.java    5 Dec 2002 17:56:43 -0000       1.137
  +++ Generator.java    5 Dec 2002 23:56:39 -0000       1.138
  @@ -1871,13 +1871,13 @@
            }
        }
   
  -     public void visit(Node.GenAttribute n) throws JasperException {
  +     public void visit(Node.AttributeGenerator n) throws JasperException {
            Node.CustomTag tag = n.getTag();
               Node.JspAttribute[] attrs = tag.getJspAttributes();
               for (int i=0; i<attrs.length; i++) {
  -             if (attrs[i].getName() == n.getName()) {
  +             if (attrs[i].getName().equals(n.getName())) {
                    out.print(evaluateAttribute(getTagHandlerInfo(tag),
  -                             attrs[i], tag, null));
  +                                             attrs[i], tag, null));
                    break;
                }
            }
  
  
  
  1.46      +10 -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.45
  retrieving revision 1.46
  diff -u -r1.45 -r1.46
  --- Node.java 5 Dec 2002 17:56:43 -0000       1.45
  +++ Node.java 5 Dec 2002 23:56:39 -0000       1.46
  @@ -1280,15 +1280,16 @@
            return n;
        }
       }
  +
       /**
  -     * Represents a attribute value of a Custom tag.  Used only by tag plugins
  -     * to indicate generated codes for the specified attribute.
  +     * Used as a placeholder for the evaluation code of a custom action
  +     * attribute (used by the tag plugin machinery only).
        */
  -    public static class GenAttribute extends Node {
  +    public static class AttributeGenerator extends Node {
        String name;    // name of the attribute
  -     CustomTag tag;  // The tag this attribute belongs
  +     CustomTag tag;  // The tag this attribute belongs to
   
  -     public GenAttribute(Mark start, String name, CustomTag tag) {
  +     public AttributeGenerator(Mark start, String name, CustomTag tag) {
            super(start, null);
            this.name = name;
            this.tag = tag;
  @@ -1794,7 +1795,7 @@
            doVisit(n);
        }
   
  -     public void visit(GenAttribute n) throws JasperException {
  +     public void visit(AttributeGenerator n) throws JasperException {
            doVisit(n);
        }
       }
  
  
  
  1.7       +32 -28    
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java
  
  Index: TagPluginManager.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/TagPluginManager.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TagPluginManager.java     5 Dec 2002 19:32:52 -0000       1.6
  +++ TagPluginManager.java     5 Dec 2002 23:56:39 -0000       1.7
  @@ -78,7 +78,9 @@
   
   public class TagPluginManager {
   
  -    static private final String plugInsXml = "tagPlugins.xml";
  +    private static final String TAG_PLUGINS_XML = "/WEB-INF/tagPlugins.xml";
  +    private static final String TAG_PLUGINS_ROOT_ELEM = "tag-plugins";
  +
       private boolean initialized = false;
       private Hashtable tagPlugins = null;
       private ServletContext ctxt;
  @@ -87,9 +89,10 @@
        this.ctxt = ctxt;
       }
   
  -    public void apply(Node.Nodes page) throws JasperException {
  +    public void apply(Node.Nodes page, ErrorDispatcher err)
  +         throws JasperException {
   
  -     init();
  +     init(err);
        if (tagPlugins == null || tagPlugins.size() == 0) {
            return;
        }
  @@ -101,31 +104,28 @@
        });
       }
    
  -    private void init() {
  +    private void init(ErrorDispatcher err) throws JasperException {
        if (initialized)
            return;
   
        initialized = true;
  -     InputStream is = ctxt.getResourceAsStream(plugInsXml);
  +     InputStream is = ctxt.getResourceAsStream(TAG_PLUGINS_XML);
        if (is == null)
            return;
   
  -     TreeNode root = null;
  -     try {
  -         root = (new ParserUtils()).parseXMLDocument(plugInsXml, is);
  -     } catch (JasperException ex) {
  -     }
  -     if (root == null)
  +     TreeNode root = (new ParserUtils()).parseXMLDocument(TAG_PLUGINS_XML,
  +                                                          is);
  +     if (root == null) {
            return;
  +     }
   
  -     TreeNode tagPluginsNode = root.findChild("tag-plugins");
  -     if (tagPluginsNode == null) {
  -         // Error
  -         return;
  +     if (!TAG_PLUGINS_ROOT_ELEM.equals(root.getName())) {
  +         err.jspError("jsp.error.plugin.wrongRootElement", TAG_PLUGINS_XML,
  +                      TAG_PLUGINS_ROOT_ELEM);
        }
   
        tagPlugins = new Hashtable();
  -     Iterator pluginList = tagPluginsNode.findChildren("tag-plugin");
  +     Iterator pluginList = root.findChildren("tag-plugin");
        while (pluginList.hasNext()) {
            TreeNode pluginNode = (TreeNode) pluginList.next();
               TreeNode tagClassNode = pluginNode.findChild("tag-class");
  @@ -145,9 +145,8 @@
            try {
                Class pluginClass = Class.forName(pluginClassStr);
                tagPlugin = (TagPlugin) pluginClass.newInstance();
  -         } catch (ClassNotFoundException e) {
  -         } catch (InstantiationException e) {
  -         } catch (IllegalAccessException e) {
  +         } catch (Exception e) {
  +             throw new JasperException(e);
            }
            if (tagPlugin == null) {
                return;
  @@ -157,8 +156,10 @@
       }
   
       /**
  -     * Invoke tag plugin if it exists.  The node n will be modified by
  -     * the plugin if that applies
  +     * Invoke tag plugin for the given custom tag, if a plugin exists for 
  +     * the custom tag's tag handler.
  +     *
  +     * The given custom tag node will be manipulated by the plugin.
        */
       private void invokePlugin(Node.CustomTag n) {
        TagPlugin tagPlugin = (TagPlugin)
  @@ -196,12 +197,15 @@
            return JspUtil.nextTemporaryVariableName();
        }
   
  -     public void generateJavaSource(String s) {
  -         curNodes.add(new Node.Scriptlet(node.getStart(), null));
  +     public void generateJavaSource(String sourceCode) {
  +         curNodes.add(new Node.Scriptlet(sourceCode, node.getStart(),
  +                                         null));
        }
   
  -     public void generateAttribute(String attribute) {
  -         curNodes.add(new Node.GenAttribute(node.getStart(), attribute, node));
  +     public void generateAttribute(String attributeName) {
  +         curNodes.add(new Node.AttributeGenerator(node.getStart(),
  +                                                  attributeName,
  +                                                  node));
        }
   
        public void dontUseTagPlugin() {
  
  
  
  1.1                  
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/tagplugins/jstl/ForEach.java
  
  Index: ForEach.java
  ===================================================================
  /*
   * $Header: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/tagplugins/jstl/ForEach.java,v
 1.1 2002/12/05 23:56:39 luehe Exp $
   * $Revision: 1.1 $
   * $Date: 2002/12/05 23:56:39 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.jasper.tagplugins.jstl;
  
  import org.apache.jasper.compiler.tagplugin.*;
  
  public class ForEach implements TagPlugin {
  
      public void doTag(TagPluginContext ctxt) {
  
        String index = null;
  
        if (ctxt.isAttributeSpecified("begin")) {
            index = ctxt.getTemporaryVariableName();
            ctxt.generateJavaSource("for (int " + index + " = ");
            ctxt.generateAttribute("begin");
            ctxt.generateJavaSource("; " + index + " <= ");
            ctxt.generateAttribute("end");
            ctxt.generateJavaSource("; " + index + "++) {");
            ctxt.generateBody();
            ctxt.generateJavaSource("}");
        } else {
            ctxt.dontUseTagPlugin();
        }
      }
  }
  
  
  

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

Reply via email to