@Override public String path() { return Paths.convert(file.getPath()); }
/** * Convert to file to resource path. * * @param base directory location * @param file relative file reference * @return relative path used for Resource lookup */ public static String convert(File base, File file) { if (base == null) { if (file.isAbsolute()) { throw new IllegalArgumentException( "Unable to determine relative path as file was absolute"); } else { return convert(file.getPath()); } } if (file == null) { return Paths.BASE; } URI baseURI = base.toURI(); URI fileURI = file.toURI(); if (fileURI.toString().startsWith(baseURI.toString())) { URI relativize = baseURI.relativize(fileURI); return relativize.getPath(); } else { return convert(file.getPath()); } }
/** * Performs file lookup. * * @param location The name of the resource to lookup, can be absolute or relative. * @return The file handle representing the resource, or null if the resource could not be * found. * @throws IOException In the event of an I/O error. */ public File find(String location) throws IOException { Resource resource = get(Paths.convert(location)); return Resources.find(resource); }
/** * Performs a directory lookup, creating the file if it does not exist. * * @param location The location of the directory to find or create. * @return The file handle. * @throws IOException If any i/o errors occur. */ public File findOrCreateDirectory(String location) throws IOException { Resource directory = get(Paths.convert(location)); return directory.dir(); // will create directory as needed }
/** * Creates a new file. * * <p>Calls through to {@link #createFile(File, String)} * * @param location Location of file to create, either absolute or relative. * @return The file handle of the created file. * @throws IOException In the event of an I/O error. */ public File createFile(String location) throws IOException { Resource resource = get(Paths.convert(location)); return Resources.createNewFile(resource); }
/** * Creates a new directory. * * <p>Relative paths are created relative to {@link #baseDirectory}. If {@link #baseDirectory} * is not set, an IOException is thrown. * * <p>If <code>location</code> already exists as a file, an IOException is thrown. * * @param location Location of directory to create, either absolute or relative. * @return The file handle of the created directory. */ public File createDirectory(String location) throws IOException { Resource directory = get(Paths.convert(location)); return Resources.createNewDirectory(directory); }
/** * Performs a resource lookup, optionally specifying the containing directory. * * @param parentFile The containing directory, optionally null. * @param location The name of the resource to lookup, can be absolute or relative. * @return The file handle representing the resource, or null if the resource could not be * found. * @throws IOException In the event of an I/O error. */ public File find(File parentFile, String location) throws IOException { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest( "Looking up resource " + location + " with parent " + (parentFile != null ? parentFile.getPath() : "null")); } Resource resource = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.find(resource); }
/** * Used to look up a resource based on user provided url. * * <p>Supports * * <ul> * <li>Actual URL to external resource using http or ftp protocol - will return null * <li>Resource URL - will support resources from resource store * <li>File URL - will support absolute file references * <li>File URL - will support relative file references - this is deprecated, use resource: * instead * <li>Fake URLs - sde://user:pass@server:port - will return null. * </ul> * * @param baseDirectory base directory for resource: or relative file: paths * @param url the url * @return corresponding Resource */ public static Resource fromURL(Resource baseDirectory, URL url) { if (url.getProtocol().equalsIgnoreCase("resource")) { return baseDirectory.get(Paths.convert(url.getPath())); } else if (url.getProtocol().equalsIgnoreCase("file")) { return Files.asResource(URLs.urlToFile(url)); } else { return null; } }
/** * Performs a directory lookup, creating the file if it does not exist. * * @param parentFile The containing directory, possibly null. * @param location The components of the path that make up the location of the directory to find * or create. */ public File findOrCreateDirectory(File parentFile, String... location) throws IOException { Resource directory = get(Paths.convert(getBaseDirectory(), parentFile, location)); return directory.dir(); // will create directory as needed }
/** * Performs a directory lookup, creating the file if it does not exist. * * @param parentFile The containing directory, may be null. * @param location The location of the directory to find or create. * @return The file handle. * @throws IOException If any i/o errors occur. */ public File findOrCreateDirectory(File parentFile, String location) throws IOException { Resource directory = get(Paths.convert(getBaseDirectory(), parentFile, location)); return directory.dir(); // will create directory as needed }
/** * Spring Resource is made relative to Data Directory if path is relative. * * @param resource Spring resource * @param store the Resource Store * @return Spring resource relative to Data Directory * @throws IOException */ public static org.springframework.core.io.Resource relative( org.springframework.core.io.Resource resource, ResourceStore store) throws IOException { File f = resource.getFile(); if (f != null) { if (!f.isAbsolute()) { // make relative to data directory -- or create file from resource store Resource res = store.get(Paths.convert(f.getPath())); return new SpringResourceAdaptor(res); } else { return new SpringResourceAdaptor(Files.asResource(f)); } } else { return resource; } }
@Test public void convert1() { File folder = new File("folder"); File file1 = new File("file1"); File file2 = new File(folder, "file2"); assertEquals("folder", Paths.convert(folder.getPath())); assertEquals("folder/file2", Paths.convert(file2.getPath())); assertEquals("file1", Paths.convert(file1.getPath())); }
/** * Creates a new file. * * <p>Calls through to {@link #createFile(File, String)} * * @param location Location of file to create, either absolute or relative. * @param parentFile The containing directory for the file. * @return The file handle of the created file. * @throws IOException In the event of an I/O error. */ public File createFile(File parentFile, String... location) throws IOException { Resource resource = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.createNewFile(resource); }
/** * Copies a resource located on the classpath to a specified path. * * <p>The <tt>resource</tt> is obtained from teh context class loader of the current thread. * When the <tt>to</tt> parameter is specified as a relative path it is considered to be * relative to {@link #getBaseDirectory()}. * * @param classpathResource The resource to copy. * @param location The destination to copy to. */ public void copyFromClassPath(String classpathResource, String location) throws IOException { Resource resource = get(Paths.convert(location)); copyFromClassPath(classpathResource, resource.file()); }
/** * Creates a new directory specifying components of the location, and the containing directory. * * <p>Calls through to {@link #createDirectory(String)} * * @param parentFile The containing directory, possibly null. * @param location The components of the path that make up the location of the directory to * create * @return newly created directory */ public File createDirectory(File parentFile, String... location) throws IOException { Resource directory = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.createNewDirectory(directory); }
/** * Creates a new directory, optionally specifying a containing directory. * * <p>Relative paths are created relative to {@link #baseDirectory}. If {@link #baseDirectory} * is not set, an IOException is thrown. * * <p>If <code>location</code> already exists as a file, an IOException is thrown. * * @param parentFile The containing directory, may be null. * @param location Location of directory to create, either absolute or relative. * @return The file handle of the created directory. */ public File createDirectory(File parentFile, String location) throws IOException { Resource directory = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.createNewDirectory(directory); }
@Test public void convert2() { File home = new File(System.getProperty("user.home")); File directory = new File(home, "directory"); File folder = new File(directory, "folder"); File file1 = new File(directory, "file1"); File file2 = new File(folder, "file2"); File relative = new File(new File(".."), "file1"); assertEquals("folder", Paths.convert(directory, folder)); assertEquals("folder/file2", Paths.convert(directory, file2)); assertEquals("file1", Paths.convert(directory, file1)); String relativePath = relative.getPath(); assertEquals("file1", Paths.convert(directory, folder, relativePath)); } }
/** * Creates a new file. * * <p>Relative paths are created relative to {@link #baseDirectory}. * * <p>If {@link #baseDirectory} is not set, an IOException is thrown. * * <p>If <code>location</code> already exists as a directory, an IOException is thrown. * * @param location Location of file to create, either absolute or relative. * @param parentFile The containing directory for the file. * @return The file handle of the created file. * @throws IOException In the event of an I/O error. */ public File createFile(File parentFile, String location) throws IOException { Resource resource = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.createNewFile(resource); }
/** * Performs a resource lookup, optionally specifying a containing directory. * * <pre> * Example: * File f = resourceLoader.find( "data", "shapefiles", "foo.shp" ); * </pre> * * @param parentFile The parent directory, may be null. * @param location The components of the path of the resource to lookup. * @return The file handle representing the resource, or null if the resource could not be * found. * @throws IOException Any I/O errors that occur. */ public File find(File parentFile, String... location) throws IOException { Resource resource = get(Paths.convert(getBaseDirectory(), parentFile, location)); return Resources.find(resource); }