All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.soulgalore.velocity.PlainFileResourceLoader Maven / Gradle / Ivy

The newest version!
package com.soulgalore.velocity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import org.apache.commons.collections.ExtendedProperties;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.loader.ResourceLoader;

public class PlainFileResourceLoader extends ResourceLoader {

  public static final String TEMPLATE_HOME_PROPERTY = "com.soulgalore.velocity.templates.home";

  @Override
  public void init(ExtendedProperties configuration) {

  }

  @Override
  public InputStream getResourceStream(String source) throws ResourceNotFoundException {
    try {
      if (source.startsWith(File.separator))
        return new FileInputStream(source);
      else {
        String fullPath = System.getProperty(TEMPLATE_HOME_PROPERTY, ".") + File.separator + source;
        return new FileInputStream(fullPath);
      }
    } catch (FileNotFoundException e) {
      // Hack for working on Windows using GitBash
      try {
        return new FileInputStream(source);
      } catch (FileNotFoundException e1) {
        if (!source.contains("VM_global_library.vm"))
          System.err.println("PlainFileResourceLoader: Could not find "
              + (new File(source)).getAbsolutePath());

        throw new ResourceNotFoundException("Could not find "
            + (new File(source)).getAbsolutePath());
      }

    }
  }

  @Override
  public boolean isSourceModified(Resource resource) {
    return resource.isSourceModified();
  }

  @Override
  public long getLastModified(Resource resource) {
    return resource.getLastModified();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy