TemplatesUriLocator implements UriLocator { @Override public InputStream locate(String uri) throws IOException { StringBuilder javascript; if (uri.startsWith(URI_PREFIX_HEADER)) { javascript = getHeader(); } else if (uri.startsWith(URI_PREFIX_FOOTER)) { javascript = getFooter(); } else { javascript = new StringBuilder(); final String realPath; final String path = uri.substring(URI_PREFIX.length()); final ServletContext servletContext = Context.get().getServletContext(); if (servletContext != null) { realPath = servletContext.getRealPath(path); } else { realPath = path; } if (realPath == null) { return new ByteArrayInputStream(javascript.toString().getBytes("UTF-8")); } Path folderToVisit = Paths.get(realPath); if (Files.exists(folderToVisit)) { Files.walkFileTree(folderToVisit, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!Files.isDirectory(file) && file.getFileName().toString().endsWith("html")) { String fileFolder = file.getParent().toUri().toString(); String relativeFileFolderPath = fileFolder.substring(fileFolder.indexOf(path)); addFileToTemplateCache(javascript, "../.." + relativeFileFolderPath + file.getFileName(), file.toFile()); } return FileVisitResult.CONTINUE; } }); } } return new ByteArrayInputStream(javascript.toString().getBytes("UTF-8")); }  @Override InputStream locate(String uri); @Override boolean accept(String uri); static final String URI_PREFIX; static final String URI_PREFIX_HEADER; static final String URI_PREFIX_FOOTER; static final String URI_LOCATOR_ID; }
@Test public void testLocate() throws Exception { Context.set(Context.standaloneContext()); final TemplatesUriLocator locator = new TemplatesUriLocator(); URL path = TemplatesUriLocatorTest.class.getResource("/partials/"); File file = new File(path.toURI()); final Resource resource = new Resource(); resource.setUri(TemplatesUriLocator.URI_PREFIX + file.getPath()); assertTrue(locator.accept(resource.getUri())); final String actual = IOUtils.toString(locator.locate(resource.getUri())); String expected = String.format("$templateCache.put('%s/template.html', '<html>\\n<div ng-class=\"test | \\'bold\\'\"></div>\\n</html>\\n');", "../.." + file.getPath().replace('\\', '/')); assertEquals(expected, actual); }