package de.tu_dresden.diplom.richter_mirko_mat2628335.common.util;

import org.apache.log4j.Logger;

import java.util.Set;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 */
public class ToStringUtility {

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

   public static String mapToString(String mapName, int indentation, Map map){
      StringBuffer result = new StringBuffer(indentTab(indentation));
      result.append("<map:").append(mapName).append(">\n");
      Set keys = map.keySet();
      for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
         Object key = (Object) iterator.next();
         result.append(indentTab(indentation+1)).append("<map:entry>\n");
         result.append(indentTab(indentation+2)).append("<map:key>\n");
         result.append(key);
         result.append("\n").append(indentTab(indentation+2)).append("</map:key>\n");
         result.append(indentTab(indentation+2)).append("<map:value>\n");
         result.append(map.get(key));
         result.append("\n").append(indentTab(indentation+2)).append("</map:value>");
         result.append("\n").append(indentTab(indentation+1)).append("</map:entry>");
      }
      result.append("\n").append(indentTab(indentation)).append("</map:contexts>");
      return result.toString();
   }

   private static String indentTab(int indent){
      StringBuffer result = new StringBuffer("");
      if(indent > 0){
         for(int i = 0; i<indent; i++){
            result.append("\t");
         }
      }
      return result.toString();
   }
}
