/**
 * @(#)file    StringCutter.java
 * @(#)author  [개발자명]
 * @(#)version 1.0
 * @(#)date    May 16, 2007
 * @(#)since   JDK 1.4.2
 *
 * Copyright (c) www.udapsoft.co.kr, Inc.
 * All rights reserved.
 * This software is the proprietary information of Hanwha E&C, Inc.
 */
package com.udapsoft.waf.common.ui;

/**
* 문자를 잘라주는 Utility
*
* @version 1.0 2003-06-20
* @author DaeJin Lee
*/
public class StringCutter {
    public static String cut(String src, int byteLength) {

        return cut(src, byteLength, false);
    }
    
    public static String cut(String src, int byteLength, boolean isViewDesc) {

        return cut(src, byteLength, false, true);
    }

    public static String cut(String src, int byteLength, boolean isViewDesc, boolean isDot) {

        if( src == null )
            return "";

        if( byteLength <= 0 )
            return src;

        boolean isCut = false;
        int nCurLen = 0 ;
        String  strTemp = "" ;
        StringBuffer strBuf = new StringBuffer() ;

        for( int i = 0 ; i < src.length() ; i++ ) {

            strTemp = src.substring(i, i+1) ;
            byte[] byteStr = strTemp.getBytes() ;
            nCurLen += byteStr.length ;
            strBuf.append(strTemp)  ;
            
            if( nCurLen > (byteLength ) ) {

                if((src.length() != strBuf.length())&& isDot ) {
                    strBuf.append("...");
                    isCut = true;
                }
                break ;
            }
        }

        if( isViewDesc && isCut ) {

            return "<A title=\"" + src + "\" id=\"blank\">" + strBuf.toString() + "</A>";
        }
        else {

            return strBuf.toString() ;
        }
    }
}
