You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by phillip rhodes <rh...@yahoo.com> on 2004/12/08 18:58:13 UTC

Information on IDelegateSourceTemplate ?

I am trying to find more information on using this, as
I want to load a template from a database.
I found 2 references on google and none in the
tapestry docs.

Is this in a cvs only version of tapestry?  Can
someone send me a couple of code snippets of using it
(java and snipped from .application)?

Thanks very much!



---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Information on IDelegateSourceTemplate ?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Dec 8, 2004, at 12:58 PM, phillip rhodes wrote:
> I am trying to find more information on using this, as
> I want to load a template from a database.
> I found 2 references on google and none in the
> tapestry docs.
>
> Is this in a cvs only version of tapestry?  Can
> someone send me a couple of code snippets of using it
> (java and snipped from .application)?
>
> Thanks very much!

Here's the example that I've used in my presentations (at OSCON, OSCOM, 
JUG's and NFJS):

.application registers an extension:
   <extension name="org.apache.tapestry.template-source-delegate"
              class="lia.tapestry.HelpTemplateDelegate"
   />

The delegate code follows.  The ugly ParserDelegate is directly copied 
from some internal Tapestry code.  This delegate should be made 
publicly accessible to make writing a delegate easier.   My example is 
for a single file - you'll have to extrapolate it to be more 
generalized.

	Erik



public class HelpTemplateDelegate implements ITemplateSourceDelegate {
   public ComponentTemplate findTemplate(IRequestCycle cycle,
                                         IComponent component,
                                         Locale locale) {


     TemplateParser parser = new TemplateParser();
     ITemplateParserDelegate delegate = new ParserDelegate(component, 
cycle);

     TemplateToken[] tokens;

     String path = 
cycle.getEngine().getSpecification().getProperty("help.file");
     File helpFile = new File(path);
     char [] template = new char[(int)helpFile.length()];

     try {
       FileReader reader = new FileReader(helpFile);
       reader.read(template, 0, (int)helpFile.length());
       reader.close();
     } catch (IOException e) {
       throw new ApplicationRuntimeException(e);
     }

     try {
       tokens = parser.parse(template, delegate, null);
     } catch (TemplateParseException e) {
       throw new ApplicationRuntimeException(e);
     }

     return new ComponentTemplate(template, tokens);
   }

   private static class ParserDelegate implements 
ITemplateParserDelegate {
     private IComponent _component;
     private ComponentSpecificationResolver _resolver;
     private IRequestCycle _cycle;

     ParserDelegate(IComponent component, IRequestCycle cycle) {
       _component = component;
       _resolver = new ComponentSpecificationResolver(cycle);
       _cycle = cycle;
     }

     public boolean getKnownComponent(String componentId) {
       return _component.getSpecification().getComponent(componentId) != 
null;
     }

     public boolean getAllowBody(String componentId, ILocation location) 
{
       IComponent embedded = _component.getComponent(componentId);

       if (embedded == null)
         throw Tapestry.createNoSuchComponentException(_component, 
componentId, location);

       return embedded.getSpecification().getAllowBody();
     }

     public boolean getAllowBody(String libraryId, String type, 
ILocation location) {
       INamespace namespace = _component.getNamespace();

       _resolver.resolve(_cycle, namespace, libraryId, type, location);

       IComponentSpecification spec = _resolver.getSpecification();

       return spec.getAllowBody();
     }

   }

}


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org