package edu.harvard.iq.dataverse.util;

import edu.harvard.iq.dataverse.DataverseLocaleBean;

import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.text.MessageFormat;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class BundleUtil {

    private static final Logger logger = Logger.getLogger(BundleUtil.class.getCanonicalName());

    private static final String defaultBundleFile = "Bundle";
    private static Locale bundle_locale;

    public static String getStringFromBundle(String key) {
        return getStringFromBundle(key, null);
    }

    public static String getStringFromBundle(String key, List<String> arguments) {
        ResourceBundle bundle = getResourceBundle(defaultBundleFile );
        return getStringFromBundle(key, arguments, bundle);
    }

    public static String getStringFromBundle(String key, List<String> arguments, ResourceBundle bundle) {
        if (key == null || key.isEmpty()) {
            return null;
        }
        String stringFromBundle = null;
        try {
            stringFromBundle = bundle.getString(key);
            logger.fine("string found: " + stringFromBundle);
        } catch (MissingResourceException ex) {
            logger.warning("Could not find key \"" + key + "\" in bundle file.");
            return null;
        }
        if (arguments != null) {
            Object[] argArray = new String[arguments.size()];
            argArray = arguments.toArray(argArray);
            return MessageFormat.format(stringFromBundle, argArray);
        } else {
            return stringFromBundle;
        }
    }

    public static String getStringFromPropertyFile(String key, String propertyFileName  ) {
        ResourceBundle bundle = getResourceBundle(propertyFileName);
        return getStringFromBundle(key, null, bundle);
    }

    public static ResourceBundle getResourceBundle(String propertyFileName)
    {
        DataverseLocaleBean d = new DataverseLocaleBean();
        bundle_locale= new Locale(d.getLocaleCode());
        ResourceBundle bundle ;
        String filesRootDirectory = System.getProperty("dataverse.lang.directory");
        if (filesRootDirectory == null || filesRootDirectory.isEmpty()) {
            filesRootDirectory = "/tmp/lang";
        }

        File bundleFileDir = new File(filesRootDirectory);

        if (!bundleFileDir.exists())
        {
            bundle = ResourceBundle.getBundle(propertyFileName, bundle_locale);
        }
        else {

            URL[] urls = null;
            try {
                urls = new URL[]{bundleFileDir.toURI().toURL()};
            } catch (Exception e) {
                e.printStackTrace();
            }

            ClassLoader loader = new URLClassLoader(urls);
            bundle = ResourceBundle.getBundle(propertyFileName, bundle_locale, loader);
        }
        return bundle ;
    }
}

@@ -2080,24 +2080,55 @@
 caleBean();%0A
+        ResourceBundle bundle;%0A
         bund
@@ -2136,16 +2136,17 @@
 e_locale
+ 
 = new Lo
@@ -2174,39 +2174,8 @@
 ));%0A
-        ResourceBundle bundle ;
 %0A   
@@ -2251,24 +2251,25 @@
 irectory%22);%0A
+%0A
         if (
@@ -2334,172 +2334,8 @@
 ) %7B%0A
-            filesRootDirectory = %22/tmp/lang%22;%0A        %7D%0A%0A        File bundleFileDir = new File(filesRootDirectory);%0A%0A        if (!bundleFileDir.exists())%0A        %7B%0A
     
@@ -2411,32 +2411,39 @@
 cale);%0A        %7D
+ else %7B
 %0A        else %7B%0A
@@ -2435,23 +2435,71 @@
         
-else %7B%0A
+    File bundleFileDir  = new File(filesRootDirectory);
 %0A       
@@ -2842,32 +2842,33 @@
 der);%0A        %7D%0A
+%0A
         return b

create / refactor the methods