/**
 * Company Name : 中贸促信息技术有限责任公司	
 * Project Name:investWebsite
 * File Name:StringEscapeEditor.java
 * Package Name:ccpit.base.controller
 * Date:2016年8月16日下午1:34:26
 * Copyright (c) 2016, liyang@ccpit.org All Rights Reserved.
 *
 */
package org.ccpit.base.controller;

import java.beans.PropertyEditorSupport;

import org.springframework.web.util.HtmlUtils;
import org.springframework.web.util.JavaScriptUtils;

/**
 * ClassName: StringEscapeEditor <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年8月16日 下午1:34:26 <br/>
 *
 * @author liyang
 * @version 
 * @since JDK 1.6
 */
public class StringEscapeEditor extends PropertyEditorSupport{
	
	private boolean escapeHTML;// 编码HTML
	private boolean escapeJavaScript;// 编码javascript
	
	public StringEscapeEditor() {
		super();
		}


		public StringEscapeEditor(boolean escapeHTML, boolean escapeJavaScript) {
		super();
		this.escapeHTML = escapeHTML;
		this.escapeJavaScript = escapeJavaScript;
		}


		@Override
		public String getAsText() {
		Object value = getValue();
		return value != null ? value.toString() : "";
		}


		@Override
		public void setAsText(String text) throws IllegalArgumentException {
		if (text == null) {
		setValue(null);
		} else {
		String value = text;
		if (escapeHTML) {
		value = HtmlUtils.htmlEscape(value);
		System.out.println("value:"+value);
		}
		if (escapeJavaScript) {
		value = JavaScriptUtils.javaScriptEscape(value);
		System.out.println("value:"+value);
		}
		setValue(value);
		}
		}
}
