commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject cvs commit: jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define SuperTag.java ExtendTag.java DefineTagLibrary.java
Date Wed, 30 Oct 2002 19:30:47 GMT
jstrachan    2002/10/30 11:30:47

  Modified:    jelly/src/java/org/apache/commons/jelly/impl
                        DynamicTagLibrary.java
               jelly/src/test/org/apache/commons/jelly/define suite.jelly
               jelly/src/java/org/apache/commons/jelly/tags/define
                        DefineTagLibrary.java
  Added:       jelly/src/java/org/apache/commons/jelly/tags/define
                        SuperTag.java ExtendTag.java
  Log:
  Applied Tim Anderson's patch that provides extend and super tags to the dynamic tag definitions.

  This provides tag-based OO programming which is kinda wacky and cool :-). 
  This introduces the possibility of merging template based and OO based scripting mechanisms
together
  
  Revision  Changes    Path
  1.8       +33 -0     jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/DynamicTagLibrary.java
  
  Index: DynamicTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/impl/DynamicTagLibrary.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- DynamicTagLibrary.java	30 Oct 2002 19:16:24 -0000	1.7
  +++ DynamicTagLibrary.java	30 Oct 2002 19:30:46 -0000	1.8
  @@ -139,6 +139,39 @@
           templates.put(name, factory);
       }
   
  +    /**
  +     * Returns the script associated with the given tag name
  +     *
  +     * @param name The tag name
  +     * @return The script associated with <code>name</code>, or 
  +     *         <code>null</code> if the tag doesn't exist or isn't a script
  +     */
  +    public Script getDynamicTag(String name) {
  +        Object result = templates.get(name);
  +        return (result instanceof Script) ? (Script) result : null;
  +    }
  +
  +    /**
  +     * Returns the tag library instance which contains the named tag.
  +     * <p/>
  +     * If the tag is not registered within this library, the set of
  +     * parent libraries will be searched.
  +     * 
  +     * @param name The tag name
  +     * @return The tag library containing the named tag, or <code>null</code>
  +     *         if the tag is not registered.
  +     */
  +    public DynamicTagLibrary find(String name) {
  +        DynamicTagLibrary result = null;
  +        if (templates.get(name) != null) {
  +            result = this;
  +        } 
  +        else if (parent instanceof DynamicTagLibrary) {
  +            result = ((DynamicTagLibrary) parent).find(name);
  +        }
  +        return result;
  +    }
  +
       // Properties
       //-------------------------------------------------------------------------     
       public String getUri() {
  
  
  
  1.5       +63 -0     jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/define/suite.jelly
  
  Index: suite.jelly
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/test/org/apache/commons/jelly/define/suite.jelly,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- suite.jelly	23 Oct 2002 10:58:59 -0000	1.4
  +++ suite.jelly	30 Oct 2002 19:30:47 -0000	1.5
  @@ -119,5 +119,68 @@
     <test:assertEquals expected="bar" actual="${output}"/>
   
   </test:case>
  +
  +<test:case name="testExtend" xmlns="taglibExtend">
  +
  +  <define:taglib uri="taglibExtend" inherit="false">
  +     <define:tag name="foo">
  +        foo
  +     </define:tag>
  +  </define:taglib>
  +
  +  <j:set var="output"><foo/></j:set>
  +  <test:assertEquals expected="foo" actual="${output}"/>
  +
  +  <!-- extend the foo tag -->
  +  <define:taglib uri="taglibExtend" inherit="true">
  +     <define:extend name="foo">
  +        bar
  +     </define:extend>
  +  </define:taglib>
  +
  +  <j:set var="output">
  +     <foo/>
  +  </j:set>
  +  <test:assertEquals expected="bar" actual="${output}"/>
  +
  +</test:case>
  +
  +<test:case name="testSuper" xmlns="taglibSuper">
  +
  +  <define:taglib uri="taglibSuper" inherit="false">
  +     <define:tag name="foo">
  +        foo
  +     </define:tag>
  +  </define:taglib>
  +
  +  <!-- extend the foo tag -->
  +  <define:taglib uri="taglibSuper" inherit="true">
  +     <define:extend name="foo">
  +        <define:super/>
  +        bar
  +     </define:extend>
  +  </define:taglib>
  +
  +  <j:set var="output">
  +    <foo/>
  +  </j:set>
  +  <test:assertEquals expected="foobar" actual="${output}"/>
  +
  +  <!-- extend the foo tag -->
  +  <define:taglib uri="taglibSuper" inherit="true">
  +     <define:extend name="foo">
  +        bar
  +        <define:super/>
  +     </define:extend>
  +  </define:taglib>
  +
  +  <j:set var="output">
  +    <foo/>
  +  </j:set>
  +  <test:assertEquals expected="barfoobar" actual="${output}"/>
  +
  +</test:case>
  +
  +
      
   </test:suite>
  
  
  
  1.11      +7 -5      jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DefineTagLibrary.java
  
  Index: DefineTagLibrary.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/DefineTagLibrary.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefineTagLibrary.java	30 Oct 2002 19:16:29 -0000	1.10
  +++ DefineTagLibrary.java	30 Oct 2002 19:30:47 -0000	1.11
  @@ -82,5 +82,7 @@
           registerTag( "script", ScriptTag.class );
           registerTag( "invoke", InvokeTag.class );
           registerTag( "classLoader", ClassLoaderTag.class );
  +        registerTag( "extend", ExtendTag.class );
  +        registerTag( "super", SuperTag.class );
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/SuperTag.java
  
  Index: SuperTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/SuperTag.java,v
1.5 2002/05/17 15:18:12 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/05/17 15:18:12 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   * 
   * $Id: SuperTag.java,v 1.5 2002/05/17 15:18:12 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.define;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.TagSupport;
  import org.apache.commons.jelly.XMLOutput;
  
  /** 
   * &lt;super&gt; tag is used to invoke a parent tag implementation, when
   * a tag extends an existing tag
   *
   * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
   * @version $Revision: 1.0 $
   * @see ExtendTag
   */
  public class SuperTag extends TagSupport {
      
      public SuperTag() {
      }
      
      // Tag interface
      //-------------------------------------------------------------------------
      public void doTag(XMLOutput output) throws Exception {
          ExtendTag tag = (ExtendTag) findAncestorWithClass(ExtendTag.class);
          if (tag == null) {
              throw new JellyException( 
                  "<define:super> must be inside a <define:extend>");
          }
  
          tag.getSuperScript().run(getContext(), output);
      }
  }
  
  
  
  
  1.1                  jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/ExtendTag.java
  
  Index: ExtendTag.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons-sandbox/jelly/src/java/org/apache/commons/jelly/tags/define/TagTag.java,v
1.5 2002/06/25 19:12:28 jstrachan Exp $
   * $Revision: 1.5 $
   * $Date: 2002/06/25 19:12:28 $
   *
   * ====================================================================
   *
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999-2002 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", "Commons", 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 apache@apache.org.
   *
   * 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/>.
   * 
   * $Id: TagTag.java,v 1.5 2002/06/25 19:12:28 jstrachan Exp $
   */
  package org.apache.commons.jelly.tags.define;
  
  import org.apache.commons.jelly.JellyException;
  import org.apache.commons.jelly.Script;
  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.impl.DynamicTagLibrary;
  
  /** 
   * &lt;extend&gt; is used to extend a dynamic tag defined in an inherited 
   * dynamic tag library
   * <p/>
   *
   * @author <a href="mailto:tima@intalio.com">Tim Anderson</a>
   * @version $Revision: 1.5 $
   * @see SuperTag
   */
  public class ExtendTag extends DefineTagSupport {
      
      private String name;
  
      private Script superScript;
      
      public ExtendTag() {
      }
      
      // Tag interface
      //-------------------------------------------------------------------------        
           
      public void doTag(XMLOutput output) throws Exception {
          DynamicTagLibrary library = getTagLibrary();
          DynamicTagLibrary owner = library.find(getName());
          if (owner == null) {
              throw new JellyException(
                  "Cannot extend " + getName() + ": dynamic tag not defined");
          }
          if (owner == library) {
              // disallow extension of tags defined within the same tag
              // library
              throw new JellyException("Cannot extend " + getName() + 
                                       ": dynamic tag defined locally");
          }
          superScript = owner.getDynamicTag(name);
          if (superScript == null) {
              // tag doesn't define a script - disallow this for the moment.
              throw new JellyException("Cannot extend " + getName() + 
                                       ": tag is not a dynamic tag");
          }
  
          owner.registerDynamicTag(getName() , getBody());
      }    
  
      // Properties
      //-------------------------------------------------------------------------
      
      /** 
       * @return the name of the tag to create 
       */
      public String getName() {
          return name;
      }
      
      /** 
       * Sets the name of the tag to create 
       */
      public void setName(String name) {
          this.name = name;
      }    
  
      /**
       * Returns the parent implementation of this tag
       */
      public Script getSuperScript() {
          return superScript;
      }
  }
  
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message