package edu.harvard.iq.dataverse.util;

import edu.harvard.iq.dataverse.DataverseLocaleBean;

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) {
        DataverseLocaleBean d = new DataverseLocaleBean();
        bundle_locale= new Locale(d.getLocaleCode());
        ResourceBundle bundle = ResourceBundle.getBundle(defaultBundleFile, bundle_locale);
        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  ) {
        DataverseLocaleBean d = new DataverseLocaleBean();
        bundle_locale= new Locale(d.getLocaleCode());
        ResourceBundle bundle = ResourceBundle.getBundle(propertyFileName, bundle_locale);
        return getStringFromBundle(key, null, bundle);
    }

}

@@ -87,16 +87,90 @@
 eBean;%0A%0A
+import java.io.File;%0Aimport java.net.URL;%0Aimport java.net.URLClassLoader;%0A
 import j
@@ -779,121 +779,8 @@
 ) %7B%0A
-        DataverseLocaleBean d = new DataverseLocaleBean();%0A        bundle_locale= new Locale(d.getLocaleCode());%0A
     
@@ -799,32 +799,35 @@
 Bundle bundle = 
+get
 ResourceBundle.g
@@ -824,26 +824,16 @@
 ceBundle
-.getBundle
 (default
@@ -842,31 +842,17 @@
 ndleFile
-, bundle_locale
+ 
 );%0A     
@@ -1712,32 +1712,39 @@
        %7D%0A    %7D%0A%0A
+    /*%0A
     public stati
@@ -1769,62 +1769,68 @@
 From
-PropertyFile(String key, String propertyFileName  
+MetadataBlocks(String metadatablock_bundle, String key
 ) %7B%0A
+%0A%0A
     
@@ -1911,61 +1911,537 @@
 le= 
-new 
+d.get
 Locale(
-d.getLocaleCode());%0A        ResourceBundle
+);%0A%0A%0A        String filesRootDirectory = System.getProperty(%22dataverse.lang.directory%22);%0A        if (filesRootDirectory == null %7C%7C filesRootDirectory.isEmpty()) %7B%0A            filesRootDirectory = %22/tmp/lang%22;%0A        %7D%0A%0A        File file = new File(filesRootDirectory);%0A        URL%5B%5D urls = null;%0A        try %7B%0A            urls = new URL%5B%5D%7Bfile.toURI().toURL()%7D;%0A        %7D%0A        catch (Exception e)%0A        %7B%0A            e.printStackTrace();%0A        %7D%0A%0A        ClassLoader loader = new URLClassLoader(urls);%0A       
  bun
@@ -2475,23 +2475,27 @@
 dle(
-propertyFileNam
+metadatablock_bundl
 e, b
@@ -2506,16 +2506,24 @@
 e_locale
+, loader
 );%0A     
@@ -2572,17 +2572,1304 @@
 undle);%0A
+%0A
     %7D%0A
+    */%0A%0A    public static String getStringFromPropertyFile(String key, String propertyFileName  ) %7B%0A        ResourceBundle bundle = getResourceBundle(propertyFileName);%0A        return getStringFromBundle(key, null, bundle);%0A    %7D%0A%0A    public static ResourceBundle getResourceBundle(String propertyFileName)%0A    %7B%0A        DataverseLocaleBean d = new DataverseLocaleBean();%0A        bundle_locale= new Locale(d.getLocaleCode());%0A        ResourceBundle bundle ;%0A        String filesRootDirectory = System.getProperty(%22dataverse.lang.directory%22);%0A        if (filesRootDirectory == null %7C%7C filesRootDirectory.isEmpty()) %7B%0A            filesRootDirectory = %22/tmp/lang%22;%0A        %7D%0A%0A        File bundleFileDir = new File(filesRootDirectory);%0A%0A        if (!bundleFileDir.exists())%0A        %7B%0A            bundle = ResourceBundle.getBundle(propertyFileName, bundle_locale);%0A        %7D%0A        else %7B%0A%0A            URL%5B%5D urls = null;%0A            try %7B%0A                urls = new URL%5B%5D%7BbundleFileDir.toURI().toURL()%7D;%0A            %7D catch (Exception e) %7B%0A                e.printStackTrace();%0A            %7D%0A%0A            ClassLoader loader = new URLClassLoader(urls);%0A            bundle = ResourceBundle.getBundle(BundleUtil.defaultBundleFile, bundle_locale, loader);%0A        %7D%0A        return bundle ;%0A    %7D
 %0A%7D%0A

refactor bundleutil.java