按字节截取字符串的jsp自定义标签

按字节截取字符串的jsp自定义标签

 

tld 定义如下:

	
    
      sliceByte
        ...
        empty
        
           baseStr
           true
           true
        
	    
           byteCount
           true
           true
        
        
           escapeXml
           false
           false
        
        
           addEllipsis
           false
           false
        
        
           variable
           false
           false
        
    

 

 

java代码实现如下:

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * 按字节截取字符串标签 (非按字符截取)
 * 
 * @author chengkai
 * 
 */
public class SliceByte extends SimpleTagSupport {

	/**
	 * 原始字符串
	 */
	private String baseStr;
	/**
	 * 截取数量
	 */
	private long byteCount;

	/**
	 * 是否转义
	 */
	private boolean escapeXml;

	/**
	 * 是否在进行截取后添加...
	 */
	private boolean addEllipsis;

	/**
	 * 将截取后的结果保存进指定变量
	 */
	private String variable;

	public String getBaseStr() {
		return baseStr;
	}

	public void setBaseStr(String baseStr) {
		this.baseStr = baseStr;
	}

	public long getByteCount() {
		return byteCount;
	}

	public void setByteCount(long byteCount) {
		this.byteCount = byteCount;
	}

	public boolean isEscapeXml() {
		return escapeXml;
	}

	public void setEscapeXml(boolean escapeXml) {
		this.escapeXml = escapeXml;
	}

	public boolean isAddEllipsis() {
		return addEllipsis;
	}

	public void setAddEllipsis(boolean addEllipsis) {
		this.addEllipsis = addEllipsis;
	}

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = this.getJspContext().getOut();
		String resultStr = this.substringByBytes(this.baseStr, 0, byteCount);
		resultStr = resultStr == null ? "" : resultStr;

		if (addEllipsis && baseStr != null && baseStr.length() != resultStr.length()) {
			resultStr += "...";
		}

		resultStr = escapeXml ? this.html(resultStr) : resultStr;
		if (this.variable != null) {
			this.getJspContext().setAttribute(variable, resultStr);
		} else {
			out.print(resultStr);
		}
	}

	/**
	 * 根据字节截取字符串
	 * 
	 * @param baseString
	 *            原始字符串
	 * @param offset
	 *            字节偏移索引 (包含)
	 * @param count
	 *            要截取的字节数
	 * @return 返回截取后的字符串
	 */
	public String substringByBytes(String baseString, long offset, long count) {
		if (baseString == null) {
			return null;
		}
		offset = offset < 0 ? 0 : offset;
		count = count < 0 ? 0 : count;
		char[] chr = baseString.toCharArray();
		StringBuffer resultStr = new StringBuffer();
		for (int index = 0, bytecnt = 0, bytenum = 0; index < chr.length; index++) {
			int currentBC = String.valueOf(chr[index]).getBytes().length;
			bytecnt += currentBC;
			if (bytecnt >= offset) {
				bytenum += currentBC;
				if (bytenum <= count) {
					resultStr.append(chr[index]);
				} else {
					break;
				}
			}
		}
		return resultStr.toString();
	}

	private String html(String content) {
		return StringEscapeUtils.escapeXml(content);
	}

}
 

你可能感兴趣的:(按字节截取字符串的jsp自定义标签)