Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package pl.matisoft.soy.template;
  
  import org.slf4j.Logger;
 
 import  javax.annotation.Nullable;
 import  javax.annotation.ParametersAreNonnullByDefault;
 import  javax.annotation.concurrent.ThreadSafe;
 import java.io.File;
 import java.net.URL;
 import java.util.List;
Created with IntelliJ IDEA. User: mati Date: 20/06/2013 Time: 19:58 An implementation that will recursively search (and resolve) for soy files based on provided templatesLocation path
 
 @ParametersAreNonnullByDefault
 @ThreadSafe
 
     private static final Logger logger = LoggerFactory.getLogger(DefaultTemplateFilesResolver.class);

    
spring resource that points to a root path, in which soy templates are located
 
     private Resource templatesLocation = null;
 
     private boolean recursive = true;
 
     private boolean hotReloadMode = .;

    
a thread safe cache for resolved templates, no need to worry of ddos attack
 
    
friendly
 
 
     @Inject
     private ServletContext servletContext;
 
     public DefaultTemplateFilesResolver() {
     }
 
     @Override
     public void afterPropertiesSet() throws Exception {
         if ( == null) {
         }
     }
 
     @Override
     public Collection<URLresolve() throws IOException {
         Preconditions.checkNotNull("templatesLocation cannot be null!");
 
         if () {
             final List<URLfiles = toFiles();
             .debug("Debug on - resolved files:" + files.size());
 
             return files;
         }
 
         //no debug
         synchronized () {
             if (.isEmpty()) {
                 final List<URLfiles = toFiles();
                 .debug("templates location:" + );
                 .debug("Using cache resolve, debug off, urls:" + files.size());
                 .addAll(files);
             }
         }
 
         return ;
     }
 
     @Override
     public Optional<URLresolve(final @Nullable String templateFileNamethrows IOException {
         if (templateFileName == null) {
             return Optional.absent();
         }
 
        final Collection<URLfiles = resolve();
        final URL templateFile = Iterables.find(filesnew Predicate<URL>() {
            @Override
            public boolean apply(final URL url) {
                final String fileName = url.getFile();
                final File file = new File(fileName);
                return file.toURI().toString().endsWith(normalizeTemplateName(templateFileName));
            }
        }, null);
        return Optional.fromNullable(templateFile);
    }
    private String normalizeTemplateName(final String templateFileName) {
        String normalizedTemplateName = templateFileName;
        if (!templateFileName.endsWith(dotWithExtension())) {
            normalizedTemplateName = templateFileName + dotWithExtension();
        }
        return normalizedTemplateName;
    }
    private List<URLtoFiles(final Resource templatesLocation) {
        final List<URLtemplateFiles = Lists.newArrayList();
        try {
            File baseDirectory = templatesLocation.getFile();
            if (baseDirectory.isDirectory()) {
                templateFiles.addAll(findSoyFiles(baseDirectory));
            } else {
                throw new IllegalArgumentException("Soy template base directory:" + templatesLocation + "' is not a directory");
            }
        } catch (final IOException e) {
            throw new IllegalArgumentException("Soy template base directory '" + templatesLocation + "' does not exist"e);
        }
        return templateFiles;
    }
    protected List<URLfindSoyFiles(final File baseDirectoryfinal boolean recursivethrows MalformedURLException {
        final List<URLsoyFiles = new ArrayList<URL>();
        findSoyFiles(soyFilesbaseDirectoryrecursive);
        return soyFiles;
    }
    protected void findSoyFiles(final List<URLsoyFilesfinal File baseDirectoryfinal boolean recursivethrows MalformedURLException {
        final File[] files = baseDirectory.listFiles();
        if (files != null) {
            for (final File file : files) {
                if (file.isFile()) {
                    if (file.getName().endsWith(dotWithExtension())) {
                        soyFiles.add(file.toURI().toURL());
                    }
                } else if (file.isDirectory() && recursive) {
                    findSoyFiles(soyFilesfilerecursive);
                }
            }
        } else {
            throw new IllegalArgumentException("Unable to retrieve contents of:" + baseDirectory);
        }
    }
    private String dotWithExtension() {
        return "." + ;
    }
    public void setTemplatesLocation(Resource templatesLocation) {
        this. = templatesLocation;
    }
    public void setRecursive(boolean recursive) {
        this. = recursive;
    }
    public void setHotReloadMode(boolean hotReloadMode) {
        this. = hotReloadMode;
    }
    public Resource getTemplatesLocation() {
        return ;
    }
    public boolean isRecursive() {
        return ;
    }
    public boolean isHotReloadMode() {
        return ;
    }
    public String getFilesExtension() {
        return ;
    }
    public void setFilesExtension(String filesExtension) {
        this. = filesExtension;
    }
    @Override
    public void setServletContext(ServletContext servletContext) {
        this. = servletContext;
    }
New to GrepCode? Check out our FAQ X