/**
 * ÀÌ ¼Ò½º´Â Spring ÇÁ·¹ÀÓ¿öÅ© ¿öÅ©ºÏ¿¡¼­ »ç¿ëÇÑ ¿¹Á¦ ¼Ò½ºÀÔ´Ï´Ù. 
 * ÀÌ ¼Ò½º´Â ¸ðµç °³¹ßÀÚµéÀÌ ÀÚÀ¯·Ó°Ô ¼öÁ¤ ¹× ¹èÆ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. 
 * ´Ü, ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î »õ·Î¿î ¾ÖÇÃ¸®ÄÉÀÌ¼ÇÀ» °³¹ßÇÒ °æ¿ì ÃâÃ³¸¦ ¸í½ÃÇØ ÁÖ½Ã¸é µË´Ï´Ù. 
 */
package net.javajigi.common.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * ÁÖ¼®À» ³Ö¾î ÁÖ¼¼¿ä. ¹è°¡ °íÆÄ¿ä.
 * 
 * @author ¹ÚÀç¼º(ÀÚ¹ÙÁö±â, javajigi@gmail.com)
 */
public class StringUtil {
    public static String stackTraceToString(Throwable ex) {
        ByteArrayOutputStream b = new ByteArrayOutputStream();
        PrintStream p = new PrintStream(b);
        ex.printStackTrace(p);
        p.close();
        String stackTrace = b.toString();
        try {
            b.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return convertHtmlBr(stackTrace);
    }
    
    public static String convertHtmlBr(String comment) {
        if (comment == null)
            return "";
        int length = comment.length();
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            String tmp = comment.substring(i, i + 1);
            if ("\r".compareTo(tmp) == 0) {
                tmp = comment.substring(++i, i + 1);
                if ("\n".compareTo(tmp) == 0)
                    buffer.append("<br>\r");
                else
                    buffer.append("\r");
            }
            buffer.append(tmp);
        }
        return buffer.toString();
    }
}
