Re: Unable to find an example of implementation of ITemplateSourceDelegate interface
Here the correct code: public class TemplateSourceDelegate implements ITemplateSourceDelegate { /** * Method called by Tapestry when it is unable to find the template of a component or a page */ public ComponentTemplate findTemplate(IRequestCycle cycle, IComponent component, Locale locale) { Resource res = component.getSpecification().getSpecificationLocation(); String componentAttributeName = "jwcid"; // code that retrives the template in a db: char[] templateData = "Test".toCharArray(); ITemplateParserDelegate delegate = new DefaultParserDelegate(component, componentAttributeName, cycle, new ComponentSpecificationResolverImpl()); TemplateToken[] tokens; TemplateParser parser = new TemplateParser(); parser.setFactory(new TemplateTokenFactory()); try { // code that throws no more a NullPointerException: tokens = parser.parse(templateData, delegate, res); } catch (TemplateParseException ex) { throw new ApplicationRuntimeException("Error in TemplateSourceDelegate", ex); } return new ComponentTemplate(templateData, tokens); } } 2006/12/11, Tapestry User List <[EMAIL PROTECTED]>: Hello, It seems nobody has ever been able to implement the ITemplateSourceDelegate interface. Does it mean that it is simply not possible ? I tried to implement this interface but as others people, I got a NullPointerException in the method TemplateParser.addTextToken(). Here my code: public class TemplateSourceDelegate implements ITemplateSourceDelegate { /** * Method called by Tapestry when it is unable to find the template of a component or a page */ public ComponentTemplate findTemplate(IRequestCycle cycle, IComponent component, Locale locale) { Resource res = component.getSpecification().getSpecificationLocation(); String componentAttributeName = "jwcid"; // code that retrives the template in a db: char[] templateData = "Test".toCharArray(); ITemplateParserDelegate delegate = new DefaultParserDelegate(component, componentAttributeName, cycle, new ComponentSpecificationResolverImpl()); TemplateToken[] tokens; ITemplateParser parser = new TemplateParser(); try { // code that throws a NullPointerException: tokens = parser.parse(templateData, delegate, res); } catch (TemplateParseException ex) { throw new ApplicationRuntimeException("Error in TemplateSourceDelegate", ex); } return new ComponentTemplate(templateData, tokens); } } D.
Unable to find an example of implementation of ITemplateSourceDelegate interface
Hello, It seems nobody has ever been able to implement the ITemplateSourceDelegate interface. Does it mean that it is simply not possible ? I tried to implement this interface but as others people, I got a NullPointerException in the method TemplateParser.addTextToken(). Here my code: public class TemplateSourceDelegate implements ITemplateSourceDelegate { /** * Method called by Tapestry when it is unable to find the template of a component or a page */ public ComponentTemplate findTemplate(IRequestCycle cycle, IComponent component, Locale locale) { Resource res = component.getSpecification ().getSpecificationLocation(); String componentAttributeName = "jwcid"; // code that retrives the template in a db: char[] templateData = "Test".toCharArray(); ITemplateParserDelegate delegate = new DefaultParserDelegate(component, componentAttributeName, cycle, new ComponentSpecificationResolverImpl()); TemplateToken[] tokens; ITemplateParser parser = new TemplateParser(); try { // code that throws a NullPointerException: tokens = parser.parse(templateData, delegate, res); } catch (TemplateParseException ex) { throw new ApplicationRuntimeException("Error in TemplateSourceDelegate", ex); } return new ComponentTemplate(templateData, tokens); } } D.
Re: ITemplateSourceDelegate
I recently did the exact same thing and just saw your implementation... Actually, I got a slightly better solution ;) After finding the page class, you have to create a 'virtual' page spec. The trick is to set its specificationLocation to be in the classpath, right in the package of the class you've found... Something like this: String fullPath = pageClass.getName().replace('.', '/'); ClasspathResource virtualPage = new ClasspathResource(new DefaultClassResolver(), fullPath + ".page"); and then spec.setSpecificationLocation(virtualPage); No need to create that asset Pablo Ruggia wrote: > Thank you very much, you save me many hours of work. > I've modified your class so it try to find templates in the packages you > define in "org.apache.tapestry.page-class-packages" configuration, and > look > it into classpath, so it has not have to be only in WEB-INF/classes. > Here is the code and the hivemind configuration i've used to make it > work: > > > > service-id="tapestry.page.SpecificationResolverDelegate"> > > > value="infrastructure:classFinder"/> > > > > > > package ar.com.example; > > import org.apache.hivemind.Location; > import org.apache.hivemind.Resource; > import org.apache.hivemind.impl.LocationImpl; > import org.apache.tapestry.INamespace; > import org.apache.tapestry.IRequestCycle; > import org.apache.tapestry.resolver.ISpecificationResolverDelegate; > import org.apache.tapestry.services.ClassFinder; > import org.apache.tapestry.spec.AssetSpecification; > import org.apache.tapestry.spec.ComponentSpecification; > import org.apache.tapestry.spec.IComponentSpecification; > import org.apache.tapestry.util.DescribedLocation; > > public class SpecificationResolverDelegate implements >ISpecificationResolverDelegate { > >private ClassFinder clazzFinder; > >public ClassFinder getClazzFinder() { >return clazzFinder; >} > >public void setClazzFinder(ClassFinder clazzFinder) { >this.clazzFinder = clazzFinder; >} > >public IComponentSpecification findPageSpecification(IRequestCycle > cycle, >INamespace namespace, String simplePageName) { > >//first I try to find the page class >String packages = namespace >.getPropertyValue("org.apache.tapestry.page-class-packages > "); > >String className = simplePageName.replace('/', '.'); >Class pageClass = getClazzFinder().findClass(packages, className); > >if (pageClass == null) >return null; > >//very good, I've found the page class, so I have to create the > specification >IComponentSpecification spec = new ComponentSpecification(); >Resource namespaceResource = namespace.getSpecificationLocation(); >Resource componentResource = namespaceResource >.getRelativeResource(simplePageName + ".page"); >Location location = new LocationImpl(componentResource); >spec.setLocation(location); >spec.setSpecificationLocation(componentResource); >spec.setComponentClassName(pageClass.getName()); > >//add the $template asset telling the framework where to look for > the template >AssetSpecification aspec = new AssetSpecification(); >aspec.setPath("classpath:/" >+ pageClass.getPackage().getName().replace(".", "/") + "/" >+ className + ".html"); >aspec.setLocation(new DescribedLocation( >spec.getSpecificationLocation(), "")); >spec.addAsset("$template", aspec); > >//return the specification >return spec; > >} > >public IComponentSpecification findComponentSpecification( >IRequestCycle cycle, INamespace namespace, String type) { >return null; >} > > } > > > > > On 10/27/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: >> >> > Has someone a working example of an ITemplateSourceDelegate >> > or or a PageSpecificationResolverImpl ? I can't find any. >> > I only want to have a .class for each page and in the same >> > package the html template. All .page configuration will be >> > set using annotations. >> > Thanks !! >> > >> >> Here is ISpecificationResolverDelegate which I use in my project. It >> allows >> me to >> 1. get rid of *.page files. Ionly need MyPage.html and MyPage.class &
Re: ITemplateSourceDelegate
Thank you very much, you save me many hours of work. I've modified your class so it try to find templates in the packages you define in "org.apache.tapestry.page-class-packages" configuration, and look it into classpath, so it has not have to be only in WEB-INF/classes. Here is the code and the hivemind configuration i've used to make it work: package ar.com.example; import org.apache.hivemind.Location; import org.apache.hivemind.Resource; import org.apache.hivemind.impl.LocationImpl; import org.apache.tapestry.INamespace; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.resolver.ISpecificationResolverDelegate; import org.apache.tapestry.services.ClassFinder; import org.apache.tapestry.spec.AssetSpecification; import org.apache.tapestry.spec.ComponentSpecification; import org.apache.tapestry.spec.IComponentSpecification; import org.apache.tapestry.util.DescribedLocation; public class SpecificationResolverDelegate implements ISpecificationResolverDelegate { private ClassFinder clazzFinder; public ClassFinder getClazzFinder() { return clazzFinder; } public void setClazzFinder(ClassFinder clazzFinder) { this.clazzFinder = clazzFinder; } public IComponentSpecification findPageSpecification(IRequestCycle cycle, INamespace namespace, String simplePageName) { //first I try to find the page class String packages = namespace .getPropertyValue("org.apache.tapestry.page-class-packages "); String className = simplePageName.replace('/', '.'); Class pageClass = getClazzFinder().findClass(packages, className); if (pageClass == null) return null; //very good, I've found the page class, so I have to create the specification IComponentSpecification spec = new ComponentSpecification(); Resource namespaceResource = namespace.getSpecificationLocation(); Resource componentResource = namespaceResource .getRelativeResource(simplePageName + ".page"); Location location = new LocationImpl(componentResource); spec.setLocation(location); spec.setSpecificationLocation(componentResource); spec.setComponentClassName(pageClass.getName()); //add the $template asset telling the framework where to look for the template AssetSpecification aspec = new AssetSpecification(); aspec.setPath("classpath:/" + pageClass.getPackage().getName().replace(".", "/") + "/" + className + ".html"); aspec.setLocation(new DescribedLocation( spec.getSpecificationLocation(), "")); spec.addAsset("$template", aspec); //return the specification return spec; } public IComponentSpecification findComponentSpecification( IRequestCycle cycle, INamespace namespace, String type) { return null; } } On 10/27/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > Has someone a working example of an ITemplateSourceDelegate > or or a PageSpecificationResolverImpl ? I can't find any. > I only want to have a .class for each page and in the same > package the html template. All .page configuration will be > set using annotations. > Thanks !! > Here is ISpecificationResolverDelegate which I use in my project. It allows me to 1. get rid of *.page files. Ionly need MyPage.html and MyPage.class 2. store both MyPage.html and MyPage.class in the same location ( in my case it's WEB_INF\classes\com\Mycompany\page\ ) 3. use getPage( "com.mycompany.MyPage" ) 4. get rid of in my .application file Hope this helps. import org.apache.hivemind.Resource; import org.apache.hivemind.impl.LocationImpl; import org.apache.tapestry.INamespace; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.resolver.ISpecificationResolverDelegate; import org.apache.tapestry.spec.ComponentSpecification; import org.apache.tapestry.spec.IComponentSpecification; public class SpecificationResolverDelegate implements ISpecificationResolverDelegate { public IComponentSpecification findPageSpecification(IRequestCycle cycle, INamespace namespace, String simplePageName){ final Resource namespaceLocation = namespace.getSpecificationLocation(); final Resource templateResource = namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace ( '.', '/' ) + ".html" ); if ( templateResource.getResourceURL() != null ){ final Resource pageResource = namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace ( '.', '/' ) + &quo
RE: ITemplateSourceDelegate
> Has someone a working example of an ITemplateSourceDelegate > or or a PageSpecificationResolverImpl ? I can't find any. > I only want to have a .class for each page and in the same > package the html template. All .page configuration will be > set using annotations. > Thanks !! > Here is ISpecificationResolverDelegate which I use in my project. It allows me to 1. get rid of *.page files. Ionly need MyPage.html and MyPage.class 2. store both MyPage.html and MyPage.class in the same location ( in my case it's WEB_INF\classes\com\Mycompany\page\ ) 3. use getPage( "com.mycompany.MyPage" ) 4. get rid of in my .application file Hope this helps. import org.apache.hivemind.Resource; import org.apache.hivemind.impl.LocationImpl; import org.apache.tapestry.INamespace; import org.apache.tapestry.IRequestCycle; import org.apache.tapestry.resolver.ISpecificationResolverDelegate; import org.apache.tapestry.spec.ComponentSpecification; import org.apache.tapestry.spec.IComponentSpecification; public class SpecificationResolverDelegate implements ISpecificationResolverDelegate { public IComponentSpecification findPageSpecification(IRequestCycle cycle, INamespace namespace, String simplePageName){ final Resource namespaceLocation = namespace.getSpecificationLocation(); final Resource templateResource = namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace( '.', '/' ) + ".html" ); if ( templateResource.getResourceURL() != null ){ final Resource pageResource = namespaceLocation.getRelativeResource( "classes/" + simplePageName.replace( '.', '/' ) + ".page" ); final IComponentSpecification specification = new ComponentSpecification(); specification.setComponentClassName( simplePageName ); specification.setPageSpecification( false ); specification.setSpecificationLocation( pageResource ); specification.setLocation( new LocationImpl( templateResource ) ); return specification; } return null; } public IComponentSpecification findComponentSpecification( IRequestCycle cycle, INamespace namespace, String type){ return null; } } - To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
ITemplateSourceDelegate
Has someone a working example of an ITemplateSourceDelegate or or a PageSpecificationResolverImpl ? I can't find any. I only want to have a .class for each page and in the same package the html template. All .page configuration will be set using annotations. Thanks !!