/** * Creates a new immutable {@code FileLocatorImpl} object based on the * properties set so far for this builder. * * @return the newly created {@code FileLocator} object */ public FileLocator create() { return new FileLocator(this); }
/** * 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(); }
/** * 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(); } }
/** * 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); }
/** * 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; }
/** * Returns the encoding of the associated file. Result can be <b>null</b> if * no encoding has been set. * * @return the encoding of the associated file */ public String getEncoding() { return getFileLocator().getEncoding(); }
/** * {@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()); } }
/** * 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; }
/** * {@inheritDoc} This implementation looks up the locator's file name as a * resource on the class path. */ @Override public URL locate(final FileSystem fileSystem, final FileLocator locator) { return StringUtils.isEmpty(locator.getFileName()) ? null : FileLocatorUtils.locateFromClasspath(locator.getFileName()); } }
/** * {@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(); } }
/** * 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(); }
/** * Obtains a non-<b>null</b> {@code FileSystem} object from the passed in * {@code FileLocator}. If the passed in {@code FileLocator} has a * {@code FileSystem} object, it is returned. Otherwise, result is the * default {@code FileSystem}. * * @param locator the {@code FileLocator} (may be <b>null</b>) * @return the {@code FileSystem} to be used for this {@code FileLocator} */ static FileSystem obtainFileSystem(final FileLocator locator) { return (locator != null) ? ObjectUtils.defaultIfNull( locator.getFileSystem(), DEFAULT_FILE_SYSTEM) : DEFAULT_FILE_SYSTEM; }
/** * Obtains a non <b>null</b> {@code FileLocationStrategy} object from the * passed in {@code FileLocator}. If the {@code FileLocator} is not * <b>null</b> and has a {@code FileLocationStrategy} defined, this strategy * is returned. Otherwise, result is the default * {@code FileLocationStrategy}. * * @param locator the {@code FileLocator} * @return the {@code FileLocationStrategy} for this {@code FileLocator} */ static FileLocationStrategy obtainLocationStrategy(final FileLocator locator) { return (locator != null) ? ObjectUtils.defaultIfNull( locator.getLocationStrategy(), DEFAULT_LOCATION_STRATEGY) : DEFAULT_LOCATION_STRATEGY; }
/** * 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; }
/** * Internal helper method for loading a file from the given input stream. * * @param in the input stream * @param locator the current {@code FileLocator} * @throws ConfigurationException if an error occurs */ private void load(final InputStream in, final FileLocator locator) throws ConfigurationException { load(in, locator.getEncoding()); }
/** * {@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; } }
URL url = locateIncludeFile(locator.getBasePath(), fileName); if (url == null) final URL baseURL = locator.getSourceURL(); if (baseURL != null)
/** * {@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; } }
/** * 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); }