list = xe.getTagNodes(artf.getContent());
for (TagNode tnode : list) {
System.out.println("1----------" + tnode.getSource());
}
}
@Test
public void testExists() throws Exception, TokenHtmlError
{
//exists
String str = "<#if where=typeof(mm) >typeof<#else>false#else>#if>";
Source reader = new StringSource(str);
ScriptMark scriptMarkEngine = new ScriptMarkEngine("testExists", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,null);
writer.close();
Assert.assertEquals(writer.toString(), "false");
}
@Test
public void testCut() throws Exception, TokenHtmlError
{
//演示切短,一般用在新闻标题输出
String str = "${'adsfasdlkjf;lsadf'.cut(10,'..')}";
Source reader = new StringSource(str);
ScriptMark scriptMarkEngine = new ScriptMarkEngine("testCut", reader, null);
//scriptMarkEngine.setRootDirectory("D:\\tomcat\\webapps\\ROOT\\WEB-INF\\");
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,null);
writer.close();
Assert.assertEquals(writer.toString(), "adsfasdl..");
}
@Test
public void testXmlA() throws Exception, TokenHtmlError
{
//XML自动修复,不推荐非标准写法
String x = "<#assign mya=1+23>xx<#assign myv=2+23>xx${mya}x${myv}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testXmlA", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,null);
writer.close();
Assert.assertEquals(writer.toString(), "xxxx24x25");
}
@Test
public void testAssignAttr() throws Exception, TokenHtmlError
{
String x = "<#assign myv='xjsla\\\\\\\\x'.replace('\\\\\\\\','/') />${myv} ${\"123\\\"456\\'789\".replace('\\'','/') + 'jcms/'}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testAssignAttr", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,null);
writer.close();
Assert.assertEquals(writer.toString(), "xjsla/x 123\"456/789jcms/");
}
@Test
public void testDeleteHtml() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
TBean tb = new TBean();
tb.setName("陈sda1sfds123234231");
tb.setOld(30);
tb.setMoney(4654.4656);
tb.getTb().setName("chl+d1");
map.put("one", tb);
String x = "${one.name.deleteHtml(10,\"..\")}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testAssignAttr", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "陈sda1sfds..");
}
@Test
public void testValidation() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
map.put("mycode", "522321780705491");
map.put("mycode2", "52232119780705491X");
String x = "mycode=${mycode.isCardCode()} mycode2=${mycode2.isCardCode()}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testValidation", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "mycode=true mycode2=true");
}
@Test
public void testDate() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
map.put("myd", "780705");
map.put("myd2", "2008-01-02");
map.put("myname", "中文名称");
String x = "myd=${myd.toDate().string('yyyy-MM-dd')} myd2=${myd2.toDate().string('yyyy-MM-dd')} isGoodName=${myname.isGoodName()}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testDate", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "myd=1978-07-05 myd2=2008-01-02 isGoodName=true");
}
@Test
public void testcongealType() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
map.put("congealType", 1);
String x = "| selected=\"checked\"#if> /> | ";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testcongealType", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), " | ");
}
@Test
public void testIsDate() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
//2002年11月13日 上午12时00分00秒
map.put("congealdate", "2002-11-13");
String x = "<#assign console=java.lang.System.out />${congealdate.isDate()}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testIsDate", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "true");
}
@Test
public void testChineseNumber() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
//2002年11月13日 上午12时00分00秒
map.put("mynum", "123456");
String x = "${mynum.toChineseCurrency()} ${mynum.toChineseNumber()}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testChineseNumber", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "壹拾贰万叁仟肆佰伍拾陆圆整 一十二万三千四百五十六");
}
@Test
public void testNULL() throws Exception, TokenHtmlError
{
Map map = new Hashtable();
map.put("myValue", "");
String x = "<#if where=myValue>true<#else>false#if>";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testNULL", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,map);
writer.close();
Assert.assertEquals(writer.toString(), "false");
}
@Test
public void testDateSQL() throws Exception, TokenHtmlError
{
Map valueMap = new Hashtable();
valueMap.put("date", new Date());
valueMap.put("beginDateTime", DateUtil.getStartDateTime(new Date()));
String x = "select count(*)<150 from jcms_matter where createDate>='${beginDateTime.string('yyyy-MM-dd hh:mm:ss')}' \r\n${date.string('yyyy-MM-dd hh:mm:ss')}";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testDateSQL", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,valueMap);
writer.close();
//Assert.assertEquals(writer.toString(), "false");
System.out.println(writer.toString() + "当前:" + DateUtil.toString(DateUtil.getEndDateTime(new Date()),DateUtil.FULL_ST_FORMAT) );
}
@Test
public void testJson3() throws Exception, TokenHtmlError
{
//格式转换 json 和 xml
Map map = new Hashtable();
TBean tb = new TBean();
tb.setName("陈sda1sfdscxxc");
tb.setOld(30);
tb.setMoney(4654.4656);
tb.getTb().setName("chl+d1");
map.put("one", tb);
tb = new TBean();
tb.setName("第二个");
tb.setOld(21);
tb.setMoney(2432);
tb.getTb().setName("chl+d1");
map.put("two", tb);
String[] strs = new String[]{"one","two","three"};
String str = "${json(mo)}\n${json(ms)}";
Source reader = new StringSource(str);
ScriptMark scriptMarkEngine = new ScriptMarkEngine("testJson3", reader, null);
Map bindings = new HashMap();
bindings.put("mo", map);
bindings.put("ms", strs);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,bindings);
writer.close();
}
@Test
public void testXiaoWang() throws Exception, TokenHtmlError
{
HashMap context = new HashMap();
context.put("data", Arrays.asList("0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "A", "B", "C", "D", "E", "F"));
context.put("name", "test");
context.put("border", "1px");
context.put("testString", "1");
String x = "\n" +
"
${name}
\n" +
"
\n" +
"\t\n" +
"\t\t| | \n" +
"<#list cell=data>\n" +
"\t\t${cell} | \n" +
"#list>\n" +
"\t
\n" +
"<#list row=data>\n" +
"\t\n" +
"\t\t| ${row} | \n" +
"<#list cell=data>\n" +
"\t\t${row}${cell}; | \n" +
"#list>\n" +
"\t
\n" +
"#list>\n" +
"
\n" +
"
";
Source reader = new StringSource(x);
ScriptMarkEngine scriptMarkEngine = new ScriptMarkEngine("testXiaoWang", reader, null);
Writer writer = new StringWriter();
scriptMarkEngine.process(writer,context);
writer.close();
writer = new StringWriter();
scriptMarkEngine.process(writer,context);
writer.close();
}
@Test
public void testDateFormat() throws Exception, TokenHtmlError
{
Map valueMap = new HashMap();
valueMap.put("date", StringUtil.toDate("2010-01-01 12:12:12"));
String x = "${date.string('yyyy年MM月dd日 hh:mm:ss')}";
String result = CallScriptMarkEngine(x,valueMap,"testDateFormat1");
Assert.assertEquals(result, "2010年01月01日 12:12:12");
Date date = StringUtil.toDate("2010-03-04 06:19:12");
x = "<#assign var=date>new Date('2010/03/04 06:19:12')#assign>${date.string('yyyy年MM月dd日 hh:mm:ss')}";
result = CallScriptMarkEngine(x,valueMap,"testDateFormat2");
Assert.assertEquals(result, DateUtil.toString(date,"yyyy年MM月dd日 hh:mm:ss"));
valueMap.put("date", "2010-01-01 12:12:18");
x = "${date.toDate().string('yyyy年MM月dd日 hh:mm:ss')}";
result = CallScriptMarkEngine(x,valueMap,"testDateFormat2");
Assert.assertEquals(result, "2010年01月01日 12:12:18");
}
@Test
public void testNullObject() throws Exception, TokenHtmlError
{
Map valueMap = new HashMap();
String x = "<#if where=typeof(xx)>1<#else>2#else>#if> ${typeof(x)=='undefined'}";
String result = CallScriptMarkEngine(x,valueMap,"testNullObject");
Assert.assertEquals(result, "2 true");
}
@Test
public void InputStreamSource() throws Exception, TokenHtmlError
{
Source source = new InputStreamSource(MarkTestNG.class.getResourceAsStream("infile.html"),"infile","UTF-8");
System.out.println("------------流方式读取 begin");
System.out.println(source.getSource());
System.out.println("------------流方式读取 end");
}