velocity-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From isid...@setgame.com
Subject Re: Velocity Template ResourceNotFoundException
Date Tue, 25 Apr 2006 17:34:49 GMT
Since no one else is repling, i will tell you how i do it.
It is most diffently wrong by any standard, but it does work quite well
for us, and has over many different situation including

develpment in eclipse.
deployed in a war.
deployed in a unpacked war.
unit tests via ant & cruise control.

it's a bit complex, so i'll layout the basics.
I will send the files i use if you want them, but i'm pretty sure the list
server strips attachments off, so i'll do it by request.

first, we create a convience function to setup the parameters.
then when you want to parse a template. call it and pass a context aware
as a call back.

lets look at the setup. you will still need some sort of configuration,
for us, it's lumped with all are other configurations for the purpose of a
single file. but for here i'm going to hard code.
there is a natural refactoring here that creates to functions.
the very generic.
 public static Writer parse(String template, Properties props,
ContextAware process[], Writer out)

the semi generic
  public static String parse(String template, Properties props,
ContextAware[] process)

and of course the project specific. which is what you would write.
again, since we are either reading from jar / file, here's mine.

  /***********************************************************************/
  public static String parseJarFile(String template, ContextAware process)
  {
    Properties props = getProperties();
    return VelocityParser.parse(template, props, new
ContextAware[]{process, Default.INSTANCE});
  }
  /***********************************************************************/
  private static Properties getProperties()
  {
    Properties props = new Properties();
    if (Config.FORCE_ACTUAL_FILE_INSTEAD_OF_JAR)
    {
      props.put("resource.loader", "file");
      props.put("file.resource.loader.path", getPath());
      props.put("file.resource.cache", "" + true);
    }
    else
    {
      props.put("resource.loader", "class");
      props.put("class.resource.loader.description", "Velocity
ServletContext Resource Loader");
      props.put("class.resource.loader.class",
"com.spun.util.velocity.ServletContextLoader");
      props.put("class.resource.cache", "" + true);
      ServletContextLoader.registerServletContext(BasicCompanyServlet.getContext());
      props.put("class.resource.loader.path",
"/WEB-INF/resources/templates/" + ", " +"/");

    }
    props.put("runtime.introspector.uberspect",
"com.spun.util.velocity.TestableUberspect");
    props.put("velocimacro.context.localscope", "" + true);
    props.put("velocimacro.permissions.allow.inline.local.scope", "" + true);
    return props;
  }
  /***********************************************************************/
  private static String getPath()
  {
    if (path == null)
    {
      path = Config.ALTERNATIVE_TEMPLATE_DIRECTORY + ", " +
Config.TEMPLATE_DIRECTORY + ", " +
convertDirectory(Config.getAlternativeHtmlDirectory())
          + ", " + convertDirectory(Config.getHtmlDirectory());
      My_System.variable("path", path);
    }
    return path;
  }
  /***********************************************************************/

as i understand your situation, you would just want the file reader part
of this.

the last part is to write a class that can be parsed. i'll make and
example here.

class Family implement ContextAware
{
Person mom, dad, brother, sister;

String getFamilyTreeAsHtml()
{
 VelocityParser.parse("template.htm", this);
}

public void setupContext(Context context)
{
context.put("mom",mom);
context.put("dad",dad);
context.put("sister",sister);
context.put("brother",brother);
}
}


anyways, this works well for us, has the distinct advantage, of being able
to break/log/troubleshoot the different settings you are passing in. if
you want more just ask.

llewellyn






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


Mime
View raw message