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;

An implementation that will recursively search (and resolve) for soy files based on provided templatesLocation path

Author(s):
Brandon Zeeb
 
 @ParametersAreNonnullByDefault
 @ThreadSafe
 
     private static final Logger logger = LoggerFactory.getLogger(ClasspathTemplateFilesResolver.class);

    
spring resource string that points to a root path, in which soy templates are located
 
     private String templatesLocation = null;
 
     private boolean recursive = true;
 
 
 
friendly
 
 
     @Override
     public void afterPropertiesSet() {
         Preconditions.checkArgument( != null"A ResourceLoader is expected to have been provided.");
     }
 
     @Override
     public Collection<URLresolve() throws IOException {
         Preconditions.checkNotNull("templatesLocation cannot be null!");
 
         return Collections.unmodifiableCollection(handleResolution());
     }
 
     @Override
     public Optional<URLresolve(final @Nullable String templateFileNamethrows IOException {
         if (templateFileName == null) {
             return Optional.absent();
         }
 
         final Collection<URLfiles = handleResolution();
         final String normalizedFileName = normalizeTemplateName(templateFileName);
 
         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(normalizedFileName);
             }
 
         }, null);
 
         return Optional.fromNullable(templateFile);
     }
 
     protected Collection<URLhandleResolution() throws IOException {
         Preconditions.checkNotNull("templatesLocation cannot be null!");
 
         if () {
             final Collection<URLfiles = resolveSoyResources();
             .debug("Debug on - resolved files: {}"files.size());
 
             return Collections.unmodifiableCollection(files);
         }
 
        // no debug
        // double check locking to ensure we don't block threads to test if cached files is null
        if ( == null) {
            synchronized (this) {
                if ( == null) {
                    final Collection<URLfiles = resolveSoyResources();
                    .debug("templates location: {}");
                    .debug("Using cache resolve, debug off, urls: {}"files.size());
                     = Collections.unmodifiableCollection(files);
                }
            }
        }
        return ;
    }
    private Collection<URLresolveSoyResources(final String templatesLocationthrows IOException {
        List<URLurls = new LinkedList<URL>();
        final String search = templateSearchStrings(templatesLocation);
        final Resource[] resources = .getResources(search);
        for (Resource r : resources) {
            urls.add(r.getURL());
        }
        return urls;
    }
    private String normalizeTemplateName(final String templateFileName) {
        String normalizedTemplateName = templateFileName;
        if (!templateFileName.endsWith(dotWithExtension())) {
            normalizedTemplateName = templateFileName + dotWithExtension();
        }
        return normalizedTemplateName;
    }
    private String templateSearchStrings(String resource) {
        if () {
            return resource + "/**/*" + dotWithExtension();
        }
        return resource + "/*" + dotWithExtension();
    }
    private String dotWithExtension() {
        return "." + ;
    }
    public void setTemplatesLocation(String templatesLocation) {
        this. = templatesLocation;
    }
    public void setRecursive(boolean recursive) {
        this. = recursive;
    }
    public void setHotReloadMode(boolean hotReloadMode) {
        this. = hotReloadMode;
    }
    public String getTemplatesLocation() {
        return ;
    }
    public boolean isRecursive() {
        return ;
    }
    public boolean isHotReloadMode() {
        return ;
    }
    public String getFilesExtension() {
        return ;
    }
    public void setFilesExtension(String filesExtension) {
        this. = filesExtension;
    }
New to GrepCode? Check out our FAQ X