You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Paul Michael Reilly <pm...@pajato.com> on 2002/11/05 17:03:35 UTC

Re: [Jelly] custom tag implementation

Now that I've got Jelly built from source I'm ready to contribute by
helping to find bugs.  The original post in this thread dealt with
defining custom tags.  I suspected, wrongly, that using the CVS
sources would enable the following to work:

Script:
  ...
  <?xml version="1.0"?>
  <j:jelly xmlns:j="jelly:core" 
           xmlns:log="jelly:log"
           xmlns:dcc="psg.dcc.jelly.DCCTagLibrary">

    <dcc:test>
      <log:info>Testing custom jelly tags</log:info>
    </dcc:test>

    <dcc:notdefined>
      <log:info>Why does this print?</log:info>
    </dcc:notdefined>

  </j:jelly>

Tag library source:
  ...
  package psg.dcc.jelly;

  import org.apache.commons.jelly.TagLibrary;
  import psg.dcc.jelly.tags.swing.FileChooserTag;
  import psg.dcc.jelly.tags.ui.ShowPageTag;
  import psg.dcc.jelly.tags.TestTag;

  /** 
   * A Jelly custom tag library that supports DCC.
   *
   * @author <a href="mailto:pmr@pajato.com">Paul Reilly</a>
   * @version $Revision: 1.0 $
   */
  public class DCCTagLibrary extends TagLibrary {

      public DCCTagLibrary() {
          registerTag( "fileChooser", FileChooserTag.class );
          registerTag( "showPage", ShowPageTag.class );
          registerTag( "test", TestTag.class );
      }
  }

Tag source:
  ...
  package psg.dcc.jelly.tags;

  import org.apache.commons.jelly.XMLOutput;
  import org.apache.commons.jelly.TagSupport;

  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;


  /** 
   * Test custom tag creation.
   *
   * @author <a href="mailto:pmr@pajato.com">Paul Reilly</a>
   * @version $Revision: 1.0 $
   */
  public class TestTag extends TagSupport {

      /** The Log to which logging calls will be made. */
      private static final Log log = LogFactory.getLog(TestTag.class);

      public TestTag() {

          System.out.println( "Constructed the test tag." );
      }

      private XMLOutput output;

      // Tag interface
      //-------------------------------------------------------------------------                    
      public void doTag(XMLOutput output) throws Exception {

          this.output = output;

          System.out.println( "testing, testing" );
      }

  }

Results:
  ...
  resources:

  run:
  Nov 5, 2002 10:54:00 AM org.apache.commons.jelly.tags.log.InfoTag doTag
  INFO: Testing custom jelly tags
  Nov 5, 2002 10:54:00 AM org.apache.commons.jelly.tags.log.InfoTag doTag
  INFO: Why does this print?

  BUILD SUCCESSFUL
  Total time: 11 seconds


I invoke the script from a Java application.  There are two issues:

1) Why do the print statements in the tag implementation not get
   output?

2) Shouldn't the <notdefined> tag generate an undefined tag error?

-pmr

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: [Jelly] custom tag implementation

Posted by James Strachan <ja...@yahoo.co.uk>.
From: "Paul Michael Reilly" <pm...@pajato.com>
> Now that I've got Jelly built from source I'm ready to contribute by
> helping to find bugs.  The original post in this thread dealt with
> defining custom tags.  I suspected, wrongly, that using the CVS
> sources would enable the following to work:
>
> Script:
>   ...
>   <?xml version="1.0"?>
>   <j:jelly xmlns:j="jelly:core"
>            xmlns:log="jelly:log"
>            xmlns:dcc="psg.dcc.jelly.DCCTagLibrary">
>
>     <dcc:test>
>       <log:info>Testing custom jelly tags</log:info>
>     </dcc:test>
>
>     <dcc:notdefined>
>       <log:info>Why does this print?</log:info>
>     </dcc:notdefined>
>
>   </j:jelly>
>
> Tag library source:
>   ...
>   package psg.dcc.jelly;
>
>   import org.apache.commons.jelly.TagLibrary;
>   import psg.dcc.jelly.tags.swing.FileChooserTag;
>   import psg.dcc.jelly.tags.ui.ShowPageTag;
>   import psg.dcc.jelly.tags.TestTag;
>
>   /**
>    * A Jelly custom tag library that supports DCC.
>    *
>    * @author <a href="mailto:pmr@pajato.com">Paul Reilly</a>
>    * @version $Revision: 1.0 $
>    */
>   public class DCCTagLibrary extends TagLibrary {
>
>       public DCCTagLibrary() {
>           registerTag( "fileChooser", FileChooserTag.class );
>           registerTag( "showPage", ShowPageTag.class );
>           registerTag( "test", TestTag.class );
>       }
>   }
>
> Tag source:
>   ...
>   package psg.dcc.jelly.tags;
>
>   import org.apache.commons.jelly.XMLOutput;
>   import org.apache.commons.jelly.TagSupport;
>
>   import org.apache.commons.logging.Log;
>   import org.apache.commons.logging.LogFactory;
>
>
>   /**
>    * Test custom tag creation.
>    *
>    * @author <a href="mailto:pmr@pajato.com">Paul Reilly</a>
>    * @version $Revision: 1.0 $
>    */
>   public class TestTag extends TagSupport {
>
>       /** The Log to which logging calls will be made. */
>       private static final Log log = LogFactory.getLog(TestTag.class);
>
>       public TestTag() {
>
>           System.out.println( "Constructed the test tag." );
>       }
>
>       private XMLOutput output;
>
>       // Tag interface
>
//-------------------------------------------------------------------------
>       public void doTag(XMLOutput output) throws Exception {
>
>           this.output = output;
>
>           System.out.println( "testing, testing" );
>       }
>
>   }
>
> Results:
>   ...
>   resources:
>
>   run:
>   Nov 5, 2002 10:54:00 AM org.apache.commons.jelly.tags.log.InfoTag doTag
>   INFO: Testing custom jelly tags
>   Nov 5, 2002 10:54:00 AM org.apache.commons.jelly.tags.log.InfoTag doTag
>   INFO: Why does this print?
>
>   BUILD SUCCESSFUL
>   Total time: 11 seconds
>
>
> I invoke the script from a Java application.  There are two issues:
>
> 1) Why do the print statements in the tag implementation not get
>    output?

Because its not even invoking your custom tag library. Though you almost did
everything totally right. You need to prefix your namespace URI with
'jelly:' to get Jelly to automatically detect your new tag library. So just
to change your namespace definition in the Jelly script to the following
(notice the use of 'jelly:' as a prefix)...

    xmlns:dcc="jelly:psg.dcc.jelly.DCCTagLibrary"

which should then use your tag library

Another way around the problem is to create a JellyContext and register your
TagLibrary class with a specific namespace URI before running your script;
then your tag library can be bound to any namespace URI you like.



> 2) Shouldn't the <notdefined> tag generate an undefined tag error?

Default behaviour is to treat all XML elements that are not bound to a Tag
as just static XML. Though your TagLibrary could throw an exception if an
unknown tag name is used if it wished

James
-------
http://radio.weblogs.com/0112098/

__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>