需要把wordFilter去掉,这个类用来把java对象转换为lua的table和字符串。
用于动态生成lua脚本的时候使用
可以弄到el函数里面,著名原作者,而且请注意,对象循环引用我没处理!!!!!
java Lua encoder
import java.io.IOException;
import java.sql.Time;
import java.util.Collection;
import java.util.Date;
import net.sf.cglib.beans.BeanMap;
import vlan.core.web.WordFilter;
import vlan.util.DateUtils;
/**
* lua的编码器,编码成lua脚本
* @author zuoge85
*
*/
public class LuaEncoder {
public static final String encodeObject(Object o){
StringBuilder sb=new StringBuilder();
try {
encodeObject(sb,o);
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
@SuppressWarnings("unchecked")
public static final void encodeObject(Appendable sb,Object o) throws IOException{
if(o==null){
sb.append("nil");
}else if(o instanceof Integer){
sb.append(String.valueOf(o));
}else if(o instanceof CharSequence){
sb.append('L');
sb.append('\"');
encode(sb,(CharSequence)o);
sb.append('\"');
}else if(o instanceof Byte){
sb.append(String.valueOf(o));
}else if(o instanceof Short){
sb.append(String.valueOf(o));
}else if(o instanceof Float){
sb.append(String.valueOf(o));
}else if(o instanceof Boolean){
sb.append(String.valueOf(o));
}else if(o instanceof java.sql.Date){
sb.append('L');
sb.append('\"');
encode(sb, DateUtils.format((java.sql.Date)o));
sb.append('\"');
}else if(o instanceof Time){
sb.append('L');
sb.append('\"');
encode(sb, DateUtils.format((Time)o));
sb.append('\"');
}else if(o instanceof Date){
sb.append('L');
sb.append('\"');
encode(sb, DateUtils.format((Date)o));
sb.append('\"');
}else if(o instanceof Table){
Table e=(Table)o;
e.toString(sb);
}else if(o instanceof Collection){
sb.append('{');
boolean isd=false;
for(Object item:(Collection)o){
if(isd){
sb.append(',');
}else{
isd=true;
}
encodeObject(sb,item);
}
sb.append('}');
}else if(o.getClass()==Object.class){
sb.append("{}");
}else if(o instanceof Number){//google的list实现了这个接口的貌似,因此放最后
/**
* 太长了的数字当成字符串处理
*/
sb.append('\"');
encode(sb, String.valueOf(o));
sb.append('\"');
}else{
//反射对象
BeanMap m=BeanMap.create(o);
sb.append('{');
boolean isd=false;
for(Object key:m.keySet()){
if(isd){
sb.append(',');
}else{
isd=true;
}
Object value=m.get(key);
sb.append((String)key);
sb.append('=');
encodeObject(sb,value);
}
sb.append('}');
}
}
public static final String encode(String str){
StringBuilder sb=new StringBuilder();
try {
encode(sb,str);
} catch (IOException e) {
throw new RuntimeException(e);
}
return sb.toString();
}
public static final void encode(Appendable out,CharSequence str) throws IOException {
if (str == null) {
return ;
}
if(wordFilter!=null){
str=wordFilter.out(str);
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
if (ch < 32) {
switch (ch) {
case '\b' :
out.append('\\');
out.append('b');
break;
case '\n' :
out.append('\\');
out.append('n');
break;
case '\t' :
out.append('\\');
out.append('t');
break;
case '\f' :
out.append('\\');
out.append('f');
break;
case '\r' :
out.append('\\');
out.append('r');
break;
default :
out.append(ch);
break;
}
} else {
switch (ch) {
case '\'' :
out.append('\\');
out.append('\'');
break;
case '"' :
out.append('\\');
out.append('"');
break;
case '\\' :
out.append('\\');
out.append('\\');
break;
case '/' :
out.append('\\');
out.append('/');
break;
default :
out.append(ch);
break;
}
}
}
}
private static WordFilter wordFilter;
/**
* 过滤,注意框架会自动设置这个属性,如果ioc工程能获取WordFilter类型的实例
* @param wordFilter
*/
public static void setWordFilter(WordFilter wordFilter) {
LuaEncoder.wordFilter = wordFilter;
}
}