/** * Returns a hash code for this object. * * @return a hash code for this object */ @Override public int hashCode() { return new HashCodeBuilder().append(getFileName()) .append(getBasePath()).append(sourceURLAsString()) .append(getEncoding()).append(getFileSystem()) .append(getLocationStrategy()).toHashCode(); }
/** * Returns a string representation of this object. This string contains the * values of all properties. * * @return a string for this object */ @Override public String toString() { return new ToStringBuilder(this).append("fileName", getFileName()) .append("basePath", getBasePath()) .append("sourceURL", sourceURLAsString()) .append("encoding", getEncoding()) .append("fileSystem", getFileSystem()) .append("locationStrategy", getLocationStrategy()).toString(); }
/** * {@inheritDoc} This implementation delegates to the {@code FileSystem}. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { return fileSystem.locateFromURL(locator.getBasePath(), locator.getFileName()); } }
/** * 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(); }
/** * 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; }
/** * 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; }
/** * {@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; } }
/** * Compares this object with another one. Two instances of * {@code FileLocatorImpl} are considered equal if all of their properties * are equal. * * @param obj the object to compare to * @return a flag whether these objects are equal */ @Override public boolean equals(final Object obj) { if (this == obj) { return true; } if (!(obj instanceof FileLocator)) { return false; } final FileLocator c = (FileLocator) obj; return new EqualsBuilder().append(getFileName(), c.getFileName()) .append(getBasePath(), c.getBasePath()) .append(sourceURLAsString(), c.sourceURLAsString()) .append(getEncoding(), c.getEncoding()) .append(getFileSystem(), c.getFileSystem()) .append(getLocationStrategy(), c.getLocationStrategy()) .isEquals(); }
/** * 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(); } }
/** * 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(); }
/** * Internal helper method for saving data to the given file name. * * @param fileName the path to the target file * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs during the save * operation */ private void save(final String fileName, final FileLocator locator) throws ConfigurationException { URL url; try { url = FileLocatorUtils.obtainFileSystem(locator).getURL( locator.getBasePath(), fileName); } catch (final MalformedURLException e) { throw new ConfigurationException(e); } if (url == null) { throw new ConfigurationException( "Cannot locate configuration source " + fileName); } save(url, locator); }
/** * 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()); } }
URL url = locateIncludeFile(locator.getBasePath(), fileName); if (url == null)