[stringtemplate-interest] proper stringtemplate loading from a webapp

Dave Rafkind dave.rafkind at gmail.com
Mon Feb 4 08:48:59 PST 2008


Just for the record, here is my solution, it wasn't that hard.



  public static class CustomStringTemplateGroup extends
StringTemplateGroup {

    private ServletContext context;
    private String templateBase;

    public CustomStringTemplateGroup(ServletContext ctx, String base) {
      super("templates", null, DefaultTemplateLexer.class);
      context = ctx;
      templateBase = base;
    }

    @Override
    protected StringTemplate
loadTemplateFromBeneathRootDirOrCLASSPATH(String arg0) {
      URL target = null;
      StringTemplate template = null;
      BufferedReader br = null;
      try {
        /////
	// this is the important line
        /////
        target = context.getResource(templateBase + "/" + arg0);

        InputStream in = target.openStream();
        InputStreamReader isr = getInputStreamReader(in);
        br = new BufferedReader(isr);
        template = loadTemplate(name, br);
        br.close();
        br = null;
      } catch (MalformedURLException ex) {
        error("Bad URL for " + target, ex);
      } catch (IOException ioex) {
        error("Trouble loading " + target, ioex);
        if (br != null) {
          try {
            br.close();
          } catch (IOException ioe2) {
            error("Cannot close template connection: " + target);
          }
        }
      }

      return template;
    }
  }

Dave Rafkind wrote:
> Well, I was hoping to load resources that aren't in the WEB-INF/classes
> directory, sort of replacing .JSP pages with .st pages, so I'm trying to
> avoid class-path based loading.
> 
> Could I subclass StringTemplateGroup and just override the
> loadTemplateFromBeneathRootDirOrCLASSPATH() function (which looks like
> it does the actual work)? Or would that make things too complicated?
> 
> Terence Parr wrote:
>> Hi. I think you can just use null as root dir.  A comment i see in code
>> says: "If there is no root directory, try to load the template from the
>> classpath."
>>
>> Try that...
>>
>> Ter
>> On Jan 31, 2008, at 7:23 AM, Dave Rafkind wrote:
>>
>>> Hi list, I apologize if this question has been answered before, I'm
>>> having trouble getting good list search results out of google. But
>>> anyways:
>>>
>>> The normal StringTemplateGroup constructor looks like:
>>>
>>> StringTemplateGroup stg = new StringTemplateGroup(name,hard_coded_path);
>>>
>>> I'd like to avoid hardcoding that path in my web application, and
>>> instead create a custom template loader to get at the files the "right"
>>> way which would be something like (from in a servlet):
>>>
>>>  context = getServletContext();
>>>  URL templateRoot = context.getResource("/templates");
>>>  StringTemplateGroup stg =
>>>    new StringTemplateGroup(name, templateRoot);
>>>
>>> which would use templateRoot.getUrlConnection() to get the
>>> stringtemplate files instead of a File() object.
>>>
>>> How would I go about doing such a thing?
>>>
>>>
>>> Thanks,
>>> Dave
>>> _______________________________________________
>>> stringtemplate-interest mailing list
>>> stringtemplate-interest at antlr.org
>>> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
> 



More information about the stringtemplate-interest mailing list