/*
 * Copyright 2016 Datalogics Inc.
 */

package com.datalogics.pdf.hsm.samples.util;

import org.apache.commons.configuration2.Configuration;
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.io.BasePathLocationStrategy;
import org.apache.commons.configuration2.io.CombinedLocationStrategy;
import org.apache.commons.configuration2.io.FileLocationStrategy;
import org.apache.commons.configuration2.io.HomeDirectoryLocationStrategy;
import org.apache.commons.configuration2.io.ProvidedURLLocationStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * Helper class to provide Apache Configuration objects to clients.
 */
public final class ConfigurationUtils {
    /**
     * This is a utility class, and won't be instantiated.
     */
    private ConfigurationUtils() {}

    /**
     * Get a configuration object.
     *
     * @param configurationFile name of configuration file to load
     * @return a Configuration object
     * @throws ConfigurationException if configuration file could not be found
     */
    public static Configuration getConfiguration(final String configurationFile) throws ConfigurationException {
        final Parameters parameters = new Parameters();
        final FileBasedConfigurationBuilder<FileBasedConfiguration> builder = new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                                                                                                                                                                      .configure(parameters.fileBased()
                                                                                                                                                                                           .setLocationStrategy(buildDefaultLocationStrategy())
                                                                                                                                                                                           .setFileName(configurationFile));
        return builder.getConfiguration();
    }

    /**
     * Build a location strategy for finding a configuration file.
     *
     * <p>
     * This strategy searches the following locations in order:
     * <ul>
     * <li>A provided URL</li>
     * <li>The current working directory</li>
     * <li>The user's home directory</li>
     * </ul>
     *
     * @return a strategy for finding a configuration file
     */
    private static FileLocationStrategy buildDefaultLocationStrategy() {
        final List<FileLocationStrategy> strategies = new ArrayList<FileLocationStrategy>();
        strategies.add(new ProvidedURLLocationStrategy());
        strategies.add(new BasePathLocationStrategy());
        strategies.add(new HomeDirectoryLocationStrategy());
        return new CombinedLocationStrategy(strategies);
    }
}

@@ -861,20 +861,17 @@
 il.Array
-List
+s
 ;%0Aimport
@@ -1158,32 +1158,220 @@
  object.%0A     *%0A
+     * %3Cp%3E%0A     * You can specify a path to the configuration file; by default, it will search in the current working directory and%0A     * the user's home directory, in that order.%0A     *%0A
      * @param co
@@ -1800,685 +1800,167 @@
  new
- FileBasedConfigurationBuilder%3CFileBasedConfiguration%3E(PropertiesConfiguration.class)%0A                                                                                                                                                                      .configure(parameters.fileBased()%0A                                                                                                                                                                                           .setLocationStrategy(buildDefaultLocationStrategy())%0A                                                                                                                                                       
+PropertiesBuilder();%0A        builder.configure(parameters.fileBased()%0A                                    .setLocationStrategy(buildDefaultLocationStrategy())%0A
     
@@ -2580,70 +2580,22 @@
 es =
- new
  Array
+s.as
 List
-%3CFileLocationStrategy%3E();%0A        strategies.add
 (new
@@ -2616,34 +2616,33 @@
 cationStrategy()
-);
+,
 %0A        strateg
@@ -2630,39 +2630,84 @@
 (),%0A        
-strategies.add(
+                                                            
 new BasePath
@@ -2716,34 +2716,33 @@
 cationStrategy()
-);
+,
 %0A        strateg
@@ -2738,23 +2738,68 @@
     
-strategies.add(
+                                                            
 new 
@@ -2894,11 +2894,221 @@
 );%0A    %7D
+%0A%0A    private static FileBasedConfigurationBuilder%3CFileBasedConfiguration%3E newPropertiesBuilder() %7B%0A        return new FileBasedConfigurationBuilder%3CFileBasedConfiguration%3E(PropertiesConfiguration.class);%0A    %7D
 %0A%7D%0A

Refactor code to fit in our line length limit