package com.fp.general.helper;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.StringTokenizer;




//Referenced classes of package org.hibernate.util:
//         StringHelper, ArrayHelper

public final class PropertiesHelper
{

 private static final String PLACEHOLDER_START = "${";

 private PropertiesHelper()
 {
 }

 public static String getString(String propertyName, Properties properties, String defaultValue)
 {
     String value = extractPropertyValue(propertyName, properties);
     return value != null ? value : defaultValue;
 }

 public static String extractPropertyValue(String propertyName, Properties properties)
 {
     String value = properties.getProperty(propertyName);
     if(value == null)
     {
         return null;
     }
     value = value.trim();
     if(StringHelper.isEmpty(value))
     {
         return null;
     } else
     {
         return value;
     }
 }

 public static boolean getBoolean(String propertyName, Properties properties)
 {
     return getBoolean(propertyName, properties, false);
 }

 public static boolean getBoolean(String propertyName, Properties properties, boolean defaultValue)
 {
     String value = extractPropertyValue(propertyName, properties);
     return value != null ? Boolean.valueOf(value).booleanValue() : defaultValue;
 }

 public static int getInt(String propertyName, Properties properties, int defaultValue)
 {
     String value = extractPropertyValue(propertyName, properties);
     return value != null ? Integer.parseInt(value) : defaultValue;
 }

 public static Integer getInteger(String propertyName, Properties properties)
 {
     String value = extractPropertyValue(propertyName, properties);
     return value != null ? Integer.valueOf(value) : null;
 }

 public static Map toMap(String propertyName, String delim, Properties properties)
 {
     Map map = new HashMap();
     String value = extractPropertyValue(propertyName, properties);
     if(value != null)
     {
         for(StringTokenizer tokens = new StringTokenizer(value, delim); tokens.hasMoreTokens(); map.put(tokens.nextToken(), tokens.hasMoreElements() ? ((Object) (tokens.nextToken())) : "")) { }
     }
     return map;
 }

 public static String[] toStringArray(String propertyName, String delim, Properties properties)
 {
     return toStringArray(extractPropertyValue(propertyName, properties), delim);
 }

 public static String[] toStringArray(String stringForm, String delim)
 {
     if(stringForm != null)
     {
         return StringHelper.split(delim, stringForm);
     } else
     {
         return ArrayHelper.EMPTY_STRING_ARRAY;
     }
 }

 public static Properties maskOut(Properties props, String key)
 {
     Properties clone = (Properties)props.clone();
     if(clone.get(key) != null)
     {
         clone.setProperty(key, "****");
     }
     return clone;
 }

 public static void resolvePlaceHolders(Properties properties)
 {
     Iterator itr = properties.entrySet().iterator();
     do
     {
         if(!itr.hasNext())
         {
             break;
         }
         java.util.Map.Entry entry = (java.util.Map.Entry)itr.next();
         Object value = entry.getValue();
         if(value != null && String.class.isInstance(value))
         {
             String resolved = resolvePlaceHolder((String)value);
             if(!value.equals(resolved))
             {
                 if(resolved == null)
                 {
                     itr.remove();
                 } else
                 {
                     entry.setValue(resolved);
                 }
             }
         }
     } while(true);
 }

 public static String resolvePlaceHolder(String property)
 {
     if(property.indexOf("${") < 0)
     {
         return property;
     }
     StringBuffer buff = new StringBuffer();
     char chars[] = property.toCharArray();
     for(int pos = 0; pos < chars.length; pos++)
     {
         if(chars[pos] == '$' && chars[pos + 1] == '{')
         {
             String systemPropertyName = "";
             int x;
             for(x = pos + 2; x < chars.length && chars[x] != '}'; x++)
             {
                 systemPropertyName = (new StringBuilder()).append(systemPropertyName).append(chars[x]).toString();
                 if(x == chars.length - 1)
                 {
                     throw new IllegalArgumentException((new StringBuilder()).append("unmatched placeholder start [").append(property).append("]").toString());
                 }
             }

             String systemProperty = extractFromSystem(systemPropertyName);
             buff.append(systemProperty != null ? systemProperty : "");
             pos = x + 1;
             if(pos >= chars.length)
             {
                 break;
             }
         }
         buff.append(chars[pos]);
     }

     String rtn = buff.toString();
     return StringHelper.isEmpty(rtn) ? null : rtn;
 }


 private static String extractFromSystem(String systemPropertyName) {
       try {
             return System.getProperty( systemPropertyName );
          }
          catch( Throwable t ) {
              return null;
          }
 }
}
