10 Replies Latest reply on Mar 30, 2004 9:21 AM by Viet

    Get templates from repository???

    Maarten Volders Newbie

      I can use templates on my modules using html files on my disc, but is it possible to get html files from the repository to use as templates???

      Now i just do:
      Document doc = repository.loadTemplate("/template.html");

      repository.addTemplate("main", (org.dom4j.Element) doc.selectSingleNode("/node/loop[@name='main']/node"));

      main = repository.createTemplate("main");
      DelegateContext ctx = new DelegateContext();
      main.render(ctx, page.getBodyWriter());

      So, is it possible to get the html template from the database repository?????

      HELP :)

        • 1. Re: Get templates from repository???
          Viet Master

          yes there is an HTMLLoader in the HTML module, you have to create a repository with that loader that will get the data in the HTML module

          • 2. Re: Get templates from repository???
            Maarten Volders Newbie

            ok, thx, but still a question, so now I have the following code in my module:

            HtmlLoader htmlLoader = new HtmlLoader(server);
            TemplateLoader templateLoader = new TemplateLoader(htmlLoader);
            Document doc = templateLoader.loadTemplate("/template.html");
            templateLoader.addTemplate("main", (org.dom4j.Element) doc.selectSingleNode("/node/loop[@name='main']/node"));
            main = templateLoader.createTemplate("main");

            I have created a html file called /template.html using mu HTML module on the admin page, I can access it using the url, but he complains (exceptions) that he can't find the resource "/template.html" ???

            Any idea what I'm doing wrong ...

            • 3. Re: Get templates from repository???
              Maarten Volders Newbie

              Additional information on the previous post, what I'm actually getting on the server is a runtimeexception in MBean operation getResource -> cause = nullpointerexception

              Is the nullpointer being throwed because he can't find teh resource or ...
              When creating the HtmlLoader i place the variable server, into the constructor, the server variable that i'm getting from your parent class, is this the way it must be or ...

              Thx

              • 5. Re: Get templates from repository???
                Maarten Volders Newbie

                owke, so what I'm doing was ok, but I'm still not getting how the name should be formatted
                from the example you gave, i can understand that the template uses a name like html:blabla

                bbut wthat is the blabla? This are perhaps stupid questions but I'm not getting it :)

                • 6. Re: Get templates from repository???
                  Viet Master

                  in the loader you should use name like : /a/b/c
                  that should map to the names in the HTML module which have
                  the same structure.

                  with classloader loaders it's different, you need a base first.
                  if the base is empty, then the first char is removed from the string
                  because classloaders load resource with name like a/b/c
                  but it there is a base like a/b the name is left as is because a/b + /c = a/b/c

                  • 7. Re: Get templates from repository???
                    Maarten Volders Newbie

                    that's exactly what i'm doing i have:
                    - in my code:
                    Document doc = repository.loadTemplate("/template");
                    - in my html module:
                    /template
                    -> tested it by just surfing to localhost:8080/template, it works

                    So I'm REALLY NOT getting what is going wrong, the code is exactly the same, I(m working with a compiled source from the start page from nukes, that the latest from cvs, perhaps that is a problem???

                    getting frustrated :(
                    ;)

                    • 8. Re: Get templates from repository???
                      Viet Master

                      it may be a security issue, now when someone access a resource
                      it will be checked if it can or not.

                      try to put the followoing rule in the permissions :
                      .*:: with level READ

                      the problem is that the loader as not identity associed, so I don't know if it will work.

                      Otherwise you can try to recompile the HtmlLoader so it use getResourceNoCheck instead of getResource :

                       public InputStream load(String name)
                       {
                       InputStream in = null;
                       try
                       {
                       Resource res = (Resource)server.invoke(HtmlModule.OBJECT_NAME, "getResourceNoCheck", new Object[]{name}, new String[]{String.class.getName()});
                       if (res != null)
                       {
                       ByteArrayOutputStream baos = new ByteArrayOutputStream();
                       res.writeTo(baos);
                       in = new ByteArrayInputStream(baos.toByteArray());
                       }
                       }
                       catch(Exception e)
                       {
                       log.error("Cannot load resource " + name + " from the html module", e);
                       }
                       return in;
                       }
                      


                      • 9. Re: Get templates from repository???
                        Maarten Volders Newbie

                        WWHHHOOOHOOOOOOOOOOOOOOOOOOOOOO
                        well I guess the above expression of joy indicates that it works. Adjusting the permision didn't work, hacking the HtmlLoader did work fine.

                        But is it safe to do this?

                        Just a last question, would you recommend its better to get the template just from a file on the disc or from the database, because all the examples worked with just hard drive files. So I was wondering if there was a reason for that?

                        Grtz, you made my day ;)

                        • 10. Re: Get templates from repository???
                          Viet Master

                          yes it is because you are doing the request within the server itself and it is not exposed to outside of the server.

                          the reason of template in the database is an easier customization for the webmaster which can modify template from the web html.