/** * 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 new {@code FileLocator} object with the properties defined in * the given map. The map must be conform to the structure generated by the * {@link #put(FileLocator, Map)} method; unexpected data can cause * {@code ClassCastException} exceptions. The map can be <b>null</b>, then * an uninitialized {@code FileLocator} is returned. * * @param map the map * @return the new {@code FileLocator} * @throws ClassCastException if the map contains invalid data */ public static FileLocator fromMap(final Map<String, ?> map) { final FileLocator.FileLocatorBuilder builder = fileLocator(); if (map != null) { builder.basePath((String) map.get(PROP_BASE_PATH)) .encoding((String) map.get(PROP_ENCODING)) .fileName((String) map.get(PROP_FILE_NAME)) .fileSystem((FileSystem) map.get(PROP_FILE_SYSTEM)) .locationStrategy( (FileLocationStrategy) map.get(PROP_STRATEGY)) .sourceURL((URL) map.get(PROP_SOURCE_URL)); } return builder.create(); }
/** * Creates application composite configuration. * * @return Application composite configuration. */ private CompositeConfiguration createApplicationConfiguration() { CompositeConfiguration cc = new CompositeConfiguration(); SystemConfiguration systemConfiguration = new SystemConfiguration(); PropertiesConfiguration propertiesConfiguration = new PropertiesConfiguration(); propertiesConfiguration.setThrowExceptionOnMissing(true); propertiesConfiguration.setListDelimiterHandler(new DefaultListDelimiterHandler(';')); propertiesConfiguration.setIncludesAllowed(false); FileLocator locator = FileLocatorUtils.fileLocator() .fileName("testlinkjavaapi.propertiesxml") .create(); propertiesConfiguration.initFileLocator(locator); cc.addConfiguration(systemConfiguration); cc.addConfiguration(propertiesConfiguration); return cc; }
@Override public URL locate(FileSystem fileSystem, FileLocator locator) { locatedUrl = baseStrategy.locate(fileSystem, locator); if (locatedUrl != null) { return locatedUrl; } locatedUrl = fallbackBasePath != null ? FileLocatorUtils.DEFAULT_LOCATION_STRATEGY.locate(fileSystem, FileLocatorUtils.fileLocator(locator).basePath(fallbackBasePath).create()) : null; if (locatedUrl == null) { notFound = true; } return locatedUrl; }
private static URL locateFile(String fileName) { FileLocationStrategy strategy = new CombinedLocationStrategy(Arrays.asList(new FileSystemLocationStrategy(), new ClasspathLocationStrategy())); FileSystem fileSystem = new DefaultFileSystem(); FileLocator locator = FileLocatorUtils.fileLocator().locationStrategy(strategy).fileName(fileName).create(); return strategy.locate(fileSystem, locator); }
/** * Injects a {@code FileLocator} pointing to the specified URL if the * current {@code FileBased} object implements the {@code FileLocatorAware} * interface. * * @param url the URL for the locator */ private void injectFileLocator(final URL url) { if (url == null) { injectNullFileLocator(); } else { if (getContent() instanceof FileLocatorAware) { final FileLocator locator = prepareNullLocatorBuilder().sourceURL(url).create(); ((FileLocatorAware) getContent()).initFileLocator(locator); } } }
/** * Returns a {@code FileLocatorBuilder} which is already initialized with * the properties of the passed in {@code FileLocator}. This builder can * be used to create a {@code FileLocator} object which shares properties * of the original locator (e.g. the {@code FileSystem} or the encoding), * but points to a different file. An example use case is as follows: * <pre> * FileLocator loc1 = ... * FileLocator loc2 = FileLocatorUtils.fileLocator(loc1) * .setFileName("anotherTest.xml") * .create(); * </pre> * @param src the source {@code FileLocator} (may be <b>null</b>) * @return an initialized builder object for defining a {@code FileLocator} */ public static FileLocator.FileLocatorBuilder fileLocator(final FileLocator src) { return new FileLocator.FileLocatorBuilder(src); }
/** * 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(); }
/** * Checks whether the associated {@code FileBased} object implements the * {@code FileLocatorAware} interface. If this is the case, a * {@code FileLocator} instance is injected which returns only <b>null</b> * values. This method is called if no file location is available (e.g. if * data is to be loaded from a stream). The encoding of the injected locator * is derived from this object. */ private void injectNullFileLocator() { if (getContent() instanceof FileLocatorAware) { final FileLocator locator = prepareNullLocatorBuilder().create(); ((FileLocatorAware) getContent()).initFileLocator(locator); } }
/** * 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); } }
/** * 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 {@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(); }
/** * Creates an uninitialized file locator. * * @return the locator */ private static FileLocator emptyFileLocator() { return FileLocatorUtils.fileLocator().create(); }
@Override protected void updateBuilder(final FileLocatorBuilder builder) { builder.fileSystem(fileSystem); } }
@Override protected void updateBuilder(final FileLocatorBuilder builder) { builder.encoding(encoding); } }
@Override protected void updateBuilder(final FileLocatorBuilder builder) { builder.locationStrategy(strategy); }
/** * Prepares a builder for a {@code FileLocator} which does not have a * defined file location. Other properties (e.g. encoding or file system) * are initialized from the {@code FileLocator} associated with this object. * * @return the initialized builder for a {@code FileLocator} */ private FileLocatorBuilder prepareNullLocatorBuilder() { return FileLocatorUtils.fileLocator(getFileLocator()).sourceURL(null) .basePath(null).fileName(null); }