PrintUtil – A helper java class for printing Map and HttpServletRequest instances

PrintUtil is a helper java class for printing in a table format any instance of a java.util.Map or javax.servlet.http.HttpServletRequest
Because of the javax.servlet.http.HttpServletRequest the servlet api is required but of course you could remove the method for the HttpServletRequest. If HttpServletRequest contains array parameters it print’s them all in the same row.See a priview of the output

+--------------------------------------------+
|                     Map                    |
+--------------------------------------------+
| aLongerKey          : one more value again |
| key1                : a value              |
| key2                : an other value       |
| longKey             : one more value       |
| theLongerstKeyOfAll : the last value       |
+--------------------------------------------+
+--------------------------------------------------+
|                HttpServletRequest                |
+--------------------------------------------------+
| aLongerKey          : [1]                        |
| key1                : [1, 2, 3]                  |
| key2                : [alpha, beta, gama, delta] |
| longKey             : [value]                    |
| theLongerstKeyOfAll : [yes, no, no, yes, no]     |
+--------------------------------------------------+

PrintUtil.java

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Panos Bariamis
 */
public class PrintUtil {

 protected final static String FRAME_START = "+";
 protected final static String FRAME_END = "+";
 protected final static String FRAME_HBORDER = "-";
 protected final static String FRAME_VBORDER = "|";
 protected final static String SEPARATOR = "_____SEP_____";

 public static void print(Map<String, ?> map) {
  Set<String> keys = map.keySet();
  if (keys.size() > 0) {
   List<String> list = new ArrayList<String>();
   for (String key : keys) {
    list.add(" " + key + SEPARATOR + map.get(key) + " ");
   }
   printFrame("Map", list);
  }
  keys = null;
 }

 public static void print(HttpServletRequest request) {
  Enumeration<?> enumer = request.getParameterNames();
     if (enumer != null && enumer.hasMoreElements()) {
      String key = null;
      List<String> list = new ArrayList<String>();
      while (enumer.hasMoreElements()) {
          key = (String)enumer.nextElement();
          list.add(" " + key + SEPARATOR 
                        + Arrays.asList(request.getParameterValues(key)) + " ");
      }
      printFrame("HttpServletRequest", list);
     }
 }

 protected static void printFrame(String title, List<String> list) {
  Collections.sort(list);
  String[] messages = list.toArray(new String[] {});
  String[] msgKeys = new String[messages.length];
  String[] msgVals = new String[messages.length];

  int index = 0;
  for (String message : messages) {
   String[] t = message.split(SEPARATOR);
   try { 
    msgKeys[index] = t[0];
    msgVals[index++] = t[1];
   } catch (ArrayIndexOutOfBoundsException e) {}
   t = null;
  }

  int longest = findLongest(msgKeys);
  index = 0;
  for (String msgkey : msgKeys) {
   messages[index] = msgkey + compoundSpaces(longest, msgkey) 
                                + " : " + msgVals[index++];
  }

  longest = findLongest(messages);
  longest = title.length() > longest ? title.length() + 2: longest;
  String hborder = "";
  for (int i=0; i<longest; i++) { hborder += FRAME_HBORDER; }

  StringBuilder result = new StringBuilder();
  result.append(FRAME_START).append(hborder).append(FRAME_END + "\n");
  result.append(FRAME_VBORDER).append(frameTitle(title, longest))
                      .append(FRAME_VBORDER + "\n");
  result.append(FRAME_START).append(hborder).append(FRAME_END + "\n");
  for (String message : messages) {
   result.append(FRAME_VBORDER)
    .append(message)
    .append(compoundSpaces(longest, message))
    .append(FRAME_VBORDER + "\n");
  }
  result.append(FRAME_START).append(hborder).append(FRAME_END);
  System.out.println(result);

  hborder = null;
  result = null;
  msgKeys = null;
  msgVals = null;
 }

 protected static int findLongest(String[] strs) {
  int result = 0;
  for (String str : strs) {
   if (str.length() > result) { result = str.length(); }
  }
  return result;
 }

 protected static String compoundSpaces(int longest, String str) {
  String result = "";
  for (int i= str.length(); i<longest; i++) {
   result += " ";
  }
  return result;
 }

 protected static String frameTitle(String title, int longest) {
  char[] spaces = new char[longest];
  for(int i=0; i<longest; i++) {
   spaces[i] = ' ';
  }
  String temp = new String(spaces);
  int length = title.length();
  int start = longest / 2 - length / 2; 

  StringBuilder result = new StringBuilder();
  result.append(temp.substring(0, start)).append(title)
                      .append(temp.substring(start + length));

  temp = null;
  return result.toString();
 }
}

Usage

PrintUtil.print(myMap);
// Or
PrintUtil.print(myHttpServletRequest);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s