/** * Create a new ClassPathResource with optional ClassLoader and Class. * Only for internal usage. * @param path relative or absolute path within the classpath * @param classLoader the class loader to load the resource with, if any * @param clazz the class to load resources with, if any */ protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) { this.path = Paths.normalize(path); this.classLoader = classLoader; this.clazz = clazz; }
private URL getCleanedUrl(URL originalUrl, String originalPath) { try { return new URL(Paths.normalize(originalPath)); } catch (MalformedURLException ex) { // Cleaned URL path cannot be converted to URL // -> take original URL. return originalUrl; } }
/** * Create a new ClassPathResource for Class usage. * The path can be relative to the given class, * or absolute within the classpath via a leading slash. * @param path relative or absolute path within the class path * @param clazz the class to load resources with * @see java.lang.Class#getResourceAsStream */ public ClassPathResource(String path, Class<?> clazz) { Args.notNull(path, "path"); this.path = Paths.normalize(path); this.clazz = clazz; }
public SimpleFileResource(File file,String classpath) { Args.notNull(file, "file"); this.file = file; this.path = Paths.normalize(file.getPath()); this.path1 = file.isDirectory() ? path + "/" : path; this.classpath = classpath; }
public SimpleFileResource(String path,String classpath) { Args.notNull(path, "path"); this.file = new File(path); this.path = Paths.normalize(path); this.path1 = file.isDirectory() && !path.endsWith("/") ? path + "/" : path; this.classpath = classpath; }
@Override public SimpleFileResource createRelative(String relativePath) { String pathToUse = Paths.normalize(Paths.applyRelative(this.path1, relativePath)); return new SimpleFileResource(pathToUse); }
/** * Create a new ClassPathResource for ClassLoader usage. * A leading slash will be removed, as the ClassLoader * resource access methods will not accept it. * @param path the absolute path within the classpath * @param classLoader the class loader to load the resource with, * or <code>null</code> for the thread context class loader * @see java.lang.ClassLoader#getResourceAsStream(String) */ public ClassPathResource(String path, ClassLoader classLoader) { Args.notNull(path, "path"); String pathToUse = Paths.normalize(path); if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } this.path = pathToUse; this.classLoader = (classLoader != null ? classLoader : Classes.getClassLoader()); }
/** * Find all resources in the file system that match the given location pattern via the Ant-style PathMatcher. */ protected Set<Resource> doFindMatchingFileSystemResources(File rootDir, String rootDirPath, String subPattern) throws IOException { if (logger.isTraceEnabled()) { logger.trace("Looking for matching resources in directory tree [" + rootDir.getPath() + "]"); } Set<File> matchingFiles = retrieveMatchingFiles(rootDir, subPattern); Set<Resource> result = new LinkedHashSet<Resource>(matchingFiles.size()); boolean isClasspath = rootDirPath.startsWith(Urls.CLASSPATH_ALL_URL_PREFIX) || rootDirPath.startsWith(Urls.CLASSPATH_ONE_URL_PREFIX); int rootDirPathLength = rootDir.getAbsolutePath().length(); String classpathRoot = isClasspath ? rootDirPath.substring(rootDirPath.indexOf(":") + 1) : null; if(null != classpathRoot && classpathRoot.startsWith("/")){ classpathRoot = classpathRoot.substring(1); } for (File file : matchingFiles) { if(isClasspath){ String classpath = classpathRoot + Paths.normalize(file.getAbsolutePath().substring(rootDirPathLength+1)); result.add(new SimpleFileResource(file, classpath)); }else{ result.add(new SimpleFileResource(file)); } } return result; }
/** * Create a new ServletContextResource. * <p>The Servlet spec requires that resource paths start with a slash, * even if many containers accept paths without leading slash too. * Consequently, the given path will be prepended with a slash if it * doesn't already start with one. * @param servletContext the ServletContext to load from * @param path the path of the resource */ public SimpleServletResource(ServletContext servletContext, String path) { // check ServletContext Args.notNull(servletContext, "servletContext"); this.servletContext = servletContext; // check path Args.notNull(path, "path"); String pathToUse = Paths.normalize(path); if (!pathToUse.startsWith("/")) { pathToUse = "/" + pathToUse; } this.path = pathToUse; }
String newUrl = quoteChar + path(Paths.normalize(Paths.applyRelative(path, urlPath))) + quoteChar; chars.replace(mark, pos, newUrl);
public static String path(String root, Resource file) { String childPath = Urls.decode(Paths.normalize(file.getFilepath())); root = Paths.prefixAndSuffixWithSlash(root); int index = childPath.indexOf(root); if(index <= 0) { throw new IllegalStateException("Invalid root '" + root + "' at '" + file.getURLString() + "'"); } String relativePath = Paths.prefixWithAndSuffixWithoutSlash(childPath.substring(index + root.length())); return Strings.removeEnd(relativePath, "/" + file.getFilename()); }