/** * Returns an uninitialized {@code FileLocatorBuilder} which can be used * for the creation of a {@code FileLocator} object. This method provides * a convenient way to create file locators using a fluent API as in the * following example: * <pre> * FileLocator locator = FileLocatorUtils.fileLocator() * .basePath(myBasePath) * .fileName("test.xml") * .create(); * </pre> * @return a builder object for defining a {@code FileLocator} */ public static FileLocator.FileLocatorBuilder fileLocator() { return fileLocator(null); }
/** * Helper method for transforming a URL into a file object. This method * handles file: and jar: URLs. * * @param url the URL to be converted * @return the resulting file or <b>null </b> */ private static File fileFromURL(final URL url) { if (JAR_PROTOCOL.equals(url.getProtocol())) { final String path = url.getPath(); try { return FileLocatorUtils.fileFromURL(new URL(path.substring(0, path.indexOf('!')))); } catch (final MalformedURLException mex) { return null; } } return FileLocatorUtils.fileFromURL(url); } }
/** * Helper method for constructing a file object from a base path and a * file name. This method is called if the base path passed to * {@code getURL()} does not seem to be a valid URL. * * @param basePath the base path * @param fileName the file name (must not be <b>null</b>) * @return the resulting file */ static File constructFile(final String basePath, final String fileName) { File file; final File absolute = new File(fileName); if (StringUtils.isEmpty(basePath) || absolute.isAbsolute()) { file = absolute; } else { file = new File(appendPath(basePath, fileName)); } return file; }
if (isFullyInitialized(locator)) final URL url = locate(locator); return (url != null) ? createFullyInitializedLocatorFromURL(locator, url) : null;
@Override public URL getURL(final String basePath, final String file) throws MalformedURLException { final File f = new File(file); if (f.isAbsolute()) // already absolute? { return FileLocatorUtils.toURL(f); } try { if (basePath == null) { return new URL(file); } final URL base = new URL(basePath); return new URL(base, file); } catch (final MalformedURLException uex) { return FileLocatorUtils.toURL(FileLocatorUtils.constructFile(basePath, file)); } }
return fileFromURL(url); return constructFile(basePath, fileName);
/** * Helper method for locating a given file. This implementation delegates to * the corresponding method in {@link FileLocatorUtils}. * * @param fs the {@code FileSystem} * @param basePath the base path * @param name the file name * @return the URL pointing to the file */ private static URL locate(final FileSystem fs, final String basePath, final String name) { final FileLocator locator = FileLocatorUtils.fileLocator().fileSystem(fs) .basePath(basePath).fileName(name).create(); return FileLocatorUtils.locate(locator); }
/** * Creates a fully initialized {@code FileLocator} based on the specified * URL. * * @param src the source {@code FileLocator} * @param url the URL * @return the fully initialized {@code FileLocator} */ private static FileLocator createFullyInitializedLocatorFromURL(final FileLocator src, final URL url) { final FileLocator.FileLocatorBuilder fileLocatorBuilder = fileLocator(src); if (src.getSourceURL() == null) { fileLocatorBuilder.sourceURL(url); } if (StringUtils.isBlank(src.getFileName())) { fileLocatorBuilder.fileName(getFileName(url)); } if (StringUtils.isBlank(src.getBasePath())) { fileLocatorBuilder.basePath(getBasePath(url)); } return fileLocatorBuilder.create(); }
/** * Creates a {@code File} object from the content of the given * {@code FileLocator} object. If the locator is not defined, result is * <b>null</b>. * * @param loc the {@code FileLocator} * @return a {@code File} object pointing to the associated file */ private static File createFile(final FileLocator loc) { if (loc.getFileName() == null && loc.getSourceURL() == null) { return null; } else if (loc.getSourceURL() != null) { return FileLocatorUtils.fileFromURL(loc.getSourceURL()); } else { return FileLocatorUtils.getFile(loc.getBasePath(), loc.getFileName()); } }
/** * {@inheritDoc} This implementation searches in the home directory for a * file described by the passed in {@code FileLocator}. If the locator * defines a base path and the {@code evaluateBasePath} property is * <b>true</b>, a sub directory of the home directory is searched. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { if (StringUtils.isNotEmpty(locator.getFileName())) { final String basePath = fetchBasePath(locator); final File file = FileLocatorUtils.constructFile(basePath, locator.getFileName()); if (file.isFile()) { return FileLocatorUtils.convertFileToURL(file); } } return null; }
/** * Tries to convert the specified file to a URL. If this causes an * exception, result is <b>null</b>. * * @param file the file to be converted * @return the resulting URL or <b>null</b> */ static URL convertFileToURL(final File file) { return convertURIToURL(file.toURI()); }
/** * {@inheritDoc} This implementation constructs a {@code File} object from * the locator's file name (if defined). If this results in an absolute file * name pointing to an existing file, the corresponding URL is returned. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { if (StringUtils.isNotEmpty(locator.getFileName())) { final File file = new File(locator.getFileName()); if (file.isAbsolute() && file.exists()) { return FileLocatorUtils.convertFileToURL(file); } } return null; } }
/** * Tries to obtain the URL of an include file using the specified (optional) * base path and file name. * * @param basePath the base path * @param fileName the file name * @return the URL of the include file or <b>null</b> if it cannot be * resolved */ private URL locateIncludeFile(final String basePath, final String fileName) { final FileLocator includeLocator = FileLocatorUtils.fileLocator(locator).sourceURL(null) .basePath(basePath).fileName(fileName).create(); return FileLocatorUtils.locate(includeLocator); } }
/** * {@inheritDoc} This implementation uses utility methods from * {@code FileLocatorUtils} to generate a {@code File} from the locator's * base path and file name. If this {@code File} exists, its URL is * returned. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { if (StringUtils.isNotEmpty(locator.getFileName())) { final File file = FileLocatorUtils.constructFile(locator.getBasePath(), locator.getFileName()); if (file.isFile()) { return FileLocatorUtils.convertFileToURL(file); } } return null; } }
/** * Creates an uninitialized file locator. * * @return the locator */ private static FileLocator emptyFileLocator() { return FileLocatorUtils.fileLocator().create(); }
@Override public InputStream getInputStream(final URL url) throws ConfigurationException { // throw an exception if the target URL is a directory final File file = FileLocatorUtils.fileFromURL(url); if (file != null && file.isDirectory()) { throw new ConfigurationException("Cannot load a configuration from a directory"); } try { return url.openStream(); } catch (final Exception e) { throw new ConfigurationException("Unable to load the configuration from the URL " + url, e); } }
/** * Determines the base path to be used for the current locate() operation. * * @param locator the {@code FileLocator} * @return the base path to be used */ private String fetchBasePath(final FileLocator locator) { if (isEvaluateBasePath() && StringUtils.isNotEmpty(locator.getBasePath())) { return FileLocatorUtils.appendPath(getHomeDirectory(), locator.getBasePath()); } return getHomeDirectory(); }
/** * Performs an update of the enclosing file handler's * {@code FileLocator} object. */ public void update() { boolean done; do { final FileLocator oldLocator = fileLocator.get(); final FileLocatorBuilder builder = FileLocatorUtils.fileLocator(oldLocator); updateBuilder(builder); done = fileLocator.compareAndSet(oldLocator, builder.create()); } while (!done); fireLocationChangedEvent(); }
final File file = FileLocatorUtils.fileFromURL(url); if (file != null)
/** * Creates a {@code FileLocator} which is a copy of the passed in one, but * has the given file name set to reference the target file. * * @param fileName the file name * @param locator the {@code FileLocator} to copy * @return the manipulated {@code FileLocator} with the file name */ private FileLocator createLocatorWithFileName(final String fileName, final FileLocator locator) { return FileLocatorUtils.fileLocator(locator).sourceURL(null) .fileName(fileName).create(); }