View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-1-6 下午12:26:19
4    */
5   package gboat2.base.core.service.test;
6   
7   import java.util.regex.Matcher;
8   import java.util.regex.Pattern;
9   
10  import junit.framework.TestCase;
11  
12  import org.apache.commons.lang3.StringEscapeUtils;
13  import org.apache.velocity.tools.generic.EscapeTool;
14  
15  
16  public class JavaScriptEscapeTest extends TestCase{
17  
18  	protected void setUp() throws Exception {
19  		super.setUp();
20  	}
21  
22  	protected void tearDown() throws Exception {
23  		super.tearDown();
24  	}
25  
26  	
27  	
28  	public void test1() {
29  		 String str = "中\"国";  
30  	        System.out.println("用escapeJava方法转义之后的字符串为:"+StringEscapeUtils.escapeJava(str));  
31  	        System.out.println("用unescapeJava方法反转义之后的字符串为:"+StringEscapeUtils.unescapeJava(StringEscapeUtils.escapeJava(str)));  
32  	          
33  	        System.out.println("用escapeHtml方法转义之后的字符串为:"+StringEscapeUtils.escapeHtml4(str));  
34  	        System.out.println("用unescapeHtml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeHtml4(StringEscapeUtils.escapeHtml4(str)));  
35  	          
36  	        System.out.println("用escapeXml方法转义之后的字符串为:"+StringEscapeUtils.escapeXml(str));  
37  	        System.out.println("用unescapeXml方法反转义之后的字符串为:"+StringEscapeUtils.unescapeXml(StringEscapeUtils.escapeXml(str)));  
38  	          
39  	        System.out.println("用escapeJavaScript方法转义之后的字符串为:"+StringEscapeUtils.escapeEcmaScript(str));  
40  	        System.out.println("用unescapeJavaScript方法反转义之后的字符串为:"+StringEscapeUtils.unescapeEcmaScript(StringEscapeUtils.escapeEcmaScript(str)));  
41  	}
42  	
43  	public void test2(){
44  		EscapeTool escape =  new EscapeTool();
45  //		String estr = "中国";
46  		String es= "{\"desc\":\"\u672A\u53D1\u5E03\",\"name\":\"NotPublished\",\"ordinal\":1}";
47  		// System.out.println(escape.html(estr));
48  		// System.out.println(escape.java(estr));
49  		 System.out.println(escape.javascript(es));
50  		 System.out.println(escape.sql(es));
51  		// System.out.println(escape.xml(estr));
52  	}
53  	
54  	
55  	
56  	public void test3(){
57  		String str="$!a.attachsJson";
58  		//String regEx="^([$!]attachsJson).*"; //表示a或f 
59  		String regEx="[^$!attachsJson]"; //表示a或f
60  		Pattern p=Pattern.compile(regEx);
61  		Matcher m=p.matcher(str);
62  		boolean result=m.find();
63  		System.out.println(result);
64  	}
65  	
66  	public void test4(){
67  //		File file = new File("D:/Workspace/gboat2.base.core/src/test/java/gboat2/base/core/service/test/test.vm");
68  //		String htmlStr = null;
69  //		try {
70  //			htmlStr = FileUtils.readFileToString(file, "UTF-8");
71  //		} catch (IOException e) {
72  //			e.printStackTrace();
73  //		}
74  //		//String regEx="^([$!]attachsJson).*"; //表示a或f 
75  //        System.out.println("用escapeHtml方法转义之后的字符串为:"+StringEscapeUtils.escapeHtml4(htmlStr));  
76  //		System.out.println(htmlStr);
77  	}
78  	
79  	
80  //	public void test4(){
81  //		Perl5Util perl = new Perl5Util();
82  //		boolean result = perl.match("/^(?!\\$\\!.*?Json).*/", "$!json");
83  //		System.out.println(result);
84  //	}
85  	
86  
87  }