protected Pair<XMLConfiguration, URL> createXmlConfiguration(String fileName) { List<Lookup> lookups = Arrays.asList(new SystemPropertiesLookup(), new HomeLookup(), new ConfigLookup()); Parameters params = new Parameters(); FallbackBasePathLocationStrategy locationStrategy = new FallbackBasePathLocationStrategy(FileLocatorUtils.DEFAULT_LOCATION_STRATEGY, home); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class) .configure(params.xml().setDefaultLookups(lookups).setLocationStrategy(locationStrategy).setFileName(fileName) .setSchemaValidation(true).setEntityResolver(new ResourceSchemaResolver())); try { XMLConfiguration xmlConfiguration = builder.getConfiguration(); return new ImmutablePair<>(xmlConfiguration, locationStrategy.getLocatedUrl()); } catch (ConfigurationException e) { throw new ConfigException(e); } }
@Override public Reader getReader(Engine engine, String fileName, Charset charset) throws IOException { // Try to read relative to the current directory or classpath. Reader reader = SpongeUtils.getReader(fileName, charset); // Try to read in the XML configuration file directory. if (reader == null) { URL configurationFileUrl = engine.getConfigurationManager().getConfigurationFileUrl(); if (configurationFileUrl != null) { File configFile = FileLocatorUtils.fileFromURL(configurationFileUrl); if (configFile != null) { String configDir = configFile.getParent(); if (configDir != null) { reader = SpongeUtils.getReader(Paths.get(configDir, fileName).toString(), charset); } } else { logger.warn("Configuration file URL {} cannot be converted to File", configurationFileUrl); } } } // Try to read in the Sponge home directory. if (reader == null) { String home = engine.getConfigurationManager().getHome(); if (home != null) { reader = SpongeUtils.getReader(Paths.get(home, fileName).toString(), charset); } } return reader; }
@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; return locatedUrl; }
private void init() { this.setListDelimiterHandler(new DefaultListDelimiterHandler(',')); FileLocationStrategy strategy = new MyFileLocationStrategy(); String basePath = _fileName == null ? "." : new File(_fileName).getParent(); FileLocator locator = FileLocatorUtils.fileLocator() .locationStrategy(strategy) .basePath(basePath) .create(); this.initFileLocator(locator); }
/** * The file {@link URL} can be absolute or relative to the working folder. The configuration file is located using a {@link FileLocatorUtils#DEFAULT_LOCATION_STRATEGY strategy} that uses a number of techniques to determine the file location. * <p> * If no file is found, the {@link #FILENAME resource file} is used. * * @return the configuration file {@link URL}. */ public URL getFileName() { if (this.propertiesFile == null) { final List<FileLocationStrategy> subs = Arrays.asList( new ProvidedURLLocationStrategy(), new FileSystemLocationStrategy(), new ClasspathLocationStrategy()); final FileLocationStrategy strategy = new CombinedLocationStrategy(subs); FileLocator fileLocator = FileLocatorUtils.fileLocator() .basePath(this.getDefaultFile().getParent()) .fileName(this.getDefaultFile().getName()) .create(); if (!new File(FileLocatorUtils.locate(fileLocator).getFile()).exists()) { fileLocator = FileLocatorUtils.fileLocator() .fileName(this.getDefaultFile().getName()) .locationStrategy(strategy) .create(); } this.propertiesFile = FileLocatorUtils.locate(fileLocator); // Ensure that the ConfigurationBuilder is now initialised as that may force a re-initialisation of this FileLocator. this.getConfigurationBuilder(); } return this.propertiesFile; }
@Override protected XMLConfiguration create(final Map<String, Object> properties) throws ConfigurationException { final XMLConfiguration configuration = new XMLConfiguration(); configuration.initFileLocator(FileLocatorUtils.fileLocator().create()); return configuration; }