commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Strachan" <james_strac...@yahoo.co.uk>
Subject Re: [Jelly] custom tag implementation
Date Wed, 06 Nov 2002 16:24:22 GMT
From: "Paul Michael Reilly" <pmr@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:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message