Interface PathResolver

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface PathResolver
Path resolvers are used by the template parser to load the source code for a template. Implementations are given the path string extracted from ~%%include:/path/to/resource.html%% and must return an InputStream to the source code. This enables you to implement and use a custom mechanism for loading templates. Klojang Templates provides two implementations itself: one that loads templates from the file system (used under the hood by Template.fromFile()), and another one that loads templates from the classpath (used by Template.fromResource()). You can use different path resolvers for different templates, but nested templates always inherit the PathResolver from the root template (the template that was explicitly instantiated using one of the fromXXX() methods on the Template class.
Author:
Ayco Holleman
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default boolean
    Returns whether the path specified in an included template (like ~%%include:/path/to/foo.html%%) points to an existing resource.
    Returns an InputStream to the resource denoted by the specified path.
  • Method Details

    • isValidPath

      default boolean isValidPath(String path)
      Returns whether the path specified in an included template (like ~%%include:/path/to/foo.html%%) points to an existing resource. If it is expensive to determine this (e.g. it requires a database lookup or an FTP connection), you may simply return true. Parsing will still fail graciously when the path turns out to be invalid, but it will be some time after the included template was first encountered, which may make it harder to understand what was wrong with the template code. If this method returns false, parsing will be aborted immediately. The default implementation returns true.
      Parameters:
      path - the path to verify
      Returns:
      whether it is a valid path
    • resolve

      InputStream resolve(String path) throws IOException
      Returns an InputStream to the resource denoted by the specified path.
      Parameters:
      path - the path
      Returns:
      an InputStream to the resource denoted by the specified path
      Throws:
      IOException - If an error occurred while setting up the InputStream.