org.apache.commons.configuration2.io.FileLocator Java Examples

The following examples show how to use org.apache.commons.configuration2.io.FileLocator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestFileBasedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether the location in the FileHandler is fully defined. This
 * ensures that saving writes to the expected file.
 */
@Test
public void testLocationIsFullyDefined() throws ConfigurationException
{
    final File file = createTestFile(1);
    final FileBasedConfigurationBuilder<PropertiesConfiguration> builder =
            new FileBasedConfigurationBuilder<>(
                    PropertiesConfiguration.class)
                    .configure(new FileBasedBuilderParametersImpl()
                            .setFile(file));
    builder.getConfiguration();
    final FileLocator locator = builder.getFileHandler().getFileLocator();
    assertTrue("Not fully defined: " + locator,
            FileLocatorUtils.isFullyInitialized(locator));
}
 
Example #2
Source File: SafeXMLConfiguration.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Override
public void initFileLocator(FileLocator loc) {
    super.initFileLocator(loc);
}
 
Example #3
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method for loading an included properties file. This method is
 * called by {@code load()} when an {@code include} property
 * is encountered. It tries to resolve relative file names based on the
 * current base path. If this fails, a resolution based on the location of
 * this properties file is tried.
 *
 * @param fileName the name of the file to load
 * @param optional whether or not the {@code fileName} is optional
 * @param seenStack Stack of seen include URLs
 * @throws ConfigurationException if loading fails
 */
private void loadIncludeFile(final String fileName, final boolean optional, final Deque<URL> seenStack)
        throws ConfigurationException
{
    if (locator == null)
    {
        throw new ConfigurationException("Load operation not properly "
                + "initialized! Do not call read(InputStream) directly,"
                + " but use a FileHandler to load a configuration.");
    }

    URL url = locateIncludeFile(locator.getBasePath(), fileName);
    if (url == null)
    {
        final URL baseURL = locator.getSourceURL();
        if (baseURL != null)
        {
            url = locateIncludeFile(baseURL.toString(), fileName);
        }
    }

    if (optional && url == null)
    {
        return;
    }

    if (url == null)
    {
        getIncludeListener().accept(new ConfigurationException("Cannot resolve include file " + fileName,
                new FileNotFoundException(fileName)));
    }
    else
    {
        final FileHandler fh = new FileHandler(this);
        fh.setFileLocator(locator);
        final FileLocator orgLocator = locator;
        try
        {
            try
            {
                // Check for cycles
                if (seenStack.contains(url))
                {
                    throw new ConfigurationException(
                            String.format("Cycle detected loading %s, seen stack: %s", url, seenStack));
                }
                seenStack.add(url);
                try
                {
                    fh.load(url);
                }
                finally
                {
                    seenStack.pop();
                }
            }
            catch (ConfigurationException e)
            {
                getIncludeListener().accept(e);
            }
        }
        finally
        {
            locator = orgLocator; // reset locator which is changed by load
        }
    }
}
 
Example #4
Source File: XMLConfiguration.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc} Stores the passed in locator for the upcoming IO operation.
 */
@Override
public void initFileLocator(final FileLocator loc)
{
    locator = loc;
}
 
Example #5
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * 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);
}
 
Example #6
Source File: CatalogResolver.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Locates 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);
}
 
Example #7
Source File: PropertiesConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Stores the current {@code FileLocator} for a following IO operation. The
 * {@code FileLocator} is needed to resolve include files with relative file
 * names.
 *
 * @param locator the current {@code FileLocator}
 * @since 2.0
 */
@Override
public void initFileLocator(final FileLocator locator)
{
    this.locator = locator;
}
 
Example #8
Source File: XMLPropertyListConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Stores the current file locator. This method is called before I/O
 * operations.
 *
 * @param locator the current {@code FileLocator}
 */
@Override
public void initFileLocator(final FileLocator locator)
{
    this.locator = locator;
}
 
Example #9
Source File: XMLPropertiesConfiguration.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Initializes this object with a {@code FileLocator}. The locator is
 * accessed during load and save operations.
 *
 * @param locator the associated {@code FileLocator}
 */
@Override
public void initFileLocator(final FileLocator locator)
{
    this.locator = locator;
}