/** * {@inheritDoc} This implementation just returns the URL stored in the * given {@code FileLocator}. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { return locator.getSourceURL(); } }
/** * Returns the location of the associated file as a URL. If a URL is set, * it is directly returned. Otherwise, an attempt to locate the referenced * file is made. * * @return a URL to the associated file; can be <b>null</b> if the location * is unspecified */ public URL getURL() { final FileLocator locator = getFileLocator(); return (locator.getSourceURL() != null) ? locator.getSourceURL() : FileLocatorUtils.locate(locator); }
/** * Checks whether the specified {@code FileLocator} contains enough * information to locate a file. This is the case if a file name or a URL is * defined. If the passed in {@code FileLocator} is <b>null</b>, result is * <b>false</b>. * * @param locator the {@code FileLocator} to check * @return a flag whether a file location is defined by this * {@code FileLocator} */ public static boolean isLocationDefined(final FileLocator locator) { return (locator != null) && (locator.getFileName() != null || locator.getSourceURL() != null); }
/** * Return the base path. If no base path is defined, but a URL, the base * path is derived from there. * * @return the base path */ public String getBasePath() { final FileLocator locator = getFileLocator(); if (locator.getBasePath() != null) { return locator.getBasePath(); } if (locator.getSourceURL() != null) { return FileLocatorUtils.getBasePath(locator.getSourceURL()); } return null; }
/** * 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()); } }
/** * Return the name of the file. If only a URL is defined, the file name * is derived from there. * * @return the file name */ public String getFileName() { final FileLocator locator = getFileLocator(); if (locator.getFileName() != null) { return locator.getFileName(); } if (locator.getSourceURL() != null) { return FileLocatorUtils.getFileName(locator.getSourceURL()); } return null; }
/** * Stores the specified {@code FileLocator} in the given map. With the * {@link #fromMap(Map)} method a new {@code FileLocator} with the same * properties as the original one can be created. * * @param locator the {@code FileLocator} to be stored * @param map the map in which to store the {@code FileLocator} (must not be * <b>null</b>) * @throws IllegalArgumentException if the map is <b>null</b> */ public static void put(final FileLocator locator, final Map<String, Object> map) { if (map == null) { throw new IllegalArgumentException("Map must not be null!"); } if (locator != null) { map.put(PROP_BASE_PATH, locator.getBasePath()); map.put(PROP_ENCODING, locator.getEncoding()); map.put(PROP_FILE_NAME, locator.getFileName()); map.put(PROP_FILE_SYSTEM, locator.getFileSystem()); map.put(PROP_SOURCE_URL, locator.getSourceURL()); map.put(PROP_STRATEGY, locator.getLocationStrategy()); } }
/** * Returns a flag whether all components of the given {@code FileLocator} * describing the referenced file are defined. In order to reference a file, * it is not necessary that all components are filled in (for instance, the * URL alone is sufficient). For some use cases however, it might be of * interest to have different methods for accessing the referenced file. * Also, depending on the filled out properties, there is a subtle * difference how the file is accessed: If only the file name is set (and * optionally the base path), each time the file is accessed a * {@code locate()} operation has to be performed to uniquely identify the * file. If however the URL is determined once based on the other components * and stored in a fully defined {@code FileLocator}, it can be used * directly to identify the file. If the passed in {@code FileLocator} is * <b>null</b>, result is <b>false</b>. * * @param locator the {@code FileLocator} to be checked (may be <b>null</b>) * @return a flag whether all components describing the referenced file are * initialized */ public static boolean isFullyInitialized(final FileLocator locator) { if (locator == null) { return false; } return locator.getBasePath() != null && locator.getFileName() != null && locator.getSourceURL() != null; }
/** * Internal helper method for saving data to the internal location stored * for this object. * * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs during the save * operation */ private void save(final FileLocator locator) throws ConfigurationException { if (!FileLocatorUtils.isLocationDefined(locator)) { throw new ConfigurationException("No file location has been set!"); } if (locator.getSourceURL() != null) { save(locator.getSourceURL(), locator); } else { save(locator.getFileName(), locator); } }
/** * Initializes the properties of this builder from the passed in locator * object. * * @param src the source {@code FileLocator} */ private void initBuilder(final FileLocator src) { basePath = src.getBasePath(); fileName = src.getFileName(); sourceURL = src.getSourceURL(); encoding = src.getEncoding(); fileSystem = src.getFileSystem(); locationStrategy = src.getLocationStrategy(); } }
final URL sourceURL = locator.getSourceURL(); if (sourceURL != null)
/** * Returns the full path to the associated file. The return value is a valid * {@code File} path only if this location is based on a file on the local * disk. If the file was loaded from a packed archive, the returned value is * the string form of the URL from which the file was loaded. * * @return the full path to the associated file */ public String getPath() { final FileLocator locator = getFileLocator(); final File file = createFile(locator); return FileLocatorUtils.obtainFileSystem(locator).getPath(file, locator.getSourceURL(), locator.getBasePath(), locator.getFileName()); }
/** * 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(); }
if (url == null) final URL baseURL = locator.getSourceURL(); if (baseURL != null)