/***Template.class.js***/
function Template()
{
this.classname="Template";
this.debug=false;
this.file=new HashMap();
this.root="";
this.varkeys=new HashMap();
this.varvals=new HashMap();
this.unknowns="remove";
this.halt_on_error="yes";
this.last_error="";
this.fso=new ActiveXObject("Scripting.FileSystemObject");
this.set_root=_set_root;
this.set_unknowns=_set_unknowns;
this.get_var=_get_var;
this.set_file=_set_file;
this.set_var=_set_var;
this.set_block=_set_block;
this.subst=_subst;
this.parse=_parse;
this.p=_p;
this.pparse=_pparse;
this.finish=_finish;
this.loadfile=_loadfile;
this.is_dir=_is_dir;
this.file_exists=_file_exists;
this.filename=_filename;
this.varname=_varname;
this.halt=_halt;
this.haltmsg=_haltmsg;
}
/**
* 设置模板文件根目录
* @param root
*/
function _set_root(root)
{
if(!this.is_dir(root))
{
this.halt("set_root:"+root+" is not a directory.");
}
this.root=root;
}
/**
* 设定对未知模板变量的处理办法
* @param unknowns
*/
function _set_unknowns(unknowns)
{
this.unknowns=unknowns;
}
/**
* 设定模板文件
* @param handle
* @param filename
*/
function _set_file(handle,filename)
{
this.file.put(handle,this.filename(filename));
}
/**
* 设定模板变量
* @param varname
* @param value
*/
function _set_var(varname,value)
{
if(!this.varkeys.containsKey(varname))
{
this.varkeys.put(varname,this.varname(varname));
}
if(!this.varvals.containsKey(varname))
{
this.varvals.put(varname,value);
}
else
{
this.varvals.remove(varname);
this.varvals.put(varname,value);
}
//alert(varname+"=================="+value);
}
/**
* 设定块变量
* @param parent
* @param handle
* @param name
*/
function _set_block(parent,handle,name)
{
if(!this.loadfile(parent))
{
this.halt("subst:unable to load "+parent);
}
if(name=="")
{
name=handle;
}
var str=this.get_var(parent);
var re=new RegExp("<!--\\s+BEGIN " + handle + "\s+-->([\s\S.]*)<!--\s+END " + handle + "\s+-->");
//Matcher m=p.matcher(str);
//var rs=m.find();
//var t=m.group(m.groupCount());
//this.set_var(handle,t);
var arr=re.exec(str);
if(arr!=null)
this.set_var(handle,RegExp.$1);
str=str.replace(re,"{"+name+"}");
this.set_var(parent,str);
}
/**
* 进行变量替换
* @param handle
* @return
*/
function _subst(handle)
{
if(!this.loadfile(handle))
{
this.halt("subst:unable to load "+handle);
}
var str=this.get_var(handle);
var keys=this.varkeys.keySet();
//alert(keys.length);
for(var i=0;i<keys.length;i++)
{
var key=keys[i];
var re=new RegExp(this.varkeys.get(key),"g")
str=str.replace(re,this.varvals.get(key));
}
//alert(handle+"++++++++++++++++++"+str);
return str;
}
/**
* 进行变量复制
* @param target
* @param handle
* @param append
*/
function _parse(target,handle,append)
{
var str=this.subst(handle);
if(append)
{
this.set_var(target,this.get_var(target)+str);
}
else
{
this.set_var(target,str);
}
}
/**
* 返回替换后的文件
* @param varname
* @return
*/
function _p(varname)
{
return this.finish(this.get_var(varname));
}
/**
* parse()和p()的合并
* @param target
* @param handle
* @param append
* @return
*/
function _pparse(target,handle,append)
{
this.parse(target,handle,append);
document.writeln(this.p(target));
}
/**
* 加载模板文件
* @param handle
* @return
*/
function _loadfile(handle)
{
if(this.varkeys.containsKey(handle) && this.varvals.get(handle)!=null)
{
return true;
}
if(!this.file.containsKey(handle))
{
_halt("loadfile:"+handle+" is not a valid handle.");
return false;
}
var filename=this.file.get(handle);
if(!this.file_exists(filename))
{
this.halt("loadfile:while loading "+handle+","+filename+" does not exist.");
return false;
}
try
{
var fr=this.fso.OpenTextFile(filename);
var s=fr.ReadAll();
if(s=="")
{
halt("loadfile:while loading "+handle+","+filename+" is empty.");
return false;
}
this.set_var(handle,s);
}
catch(e)
{
}
return true;
}
/**
* 获取变量
* @param varname
* @return
*/
function _get_var(varname)
{
if(this.varvals.containsKey(varname))
return this.varvals.get(varname);
else
return "";
}
/**
* 判断目录
* @param path
* @return
*/
function _is_dir(path)
{
if(this.fso.FolderExists(path))
return true;
else
return false;
}
/**
* 判断文件
* @param filename
* @return
*/
function _file_exists(filename)
{
if(this.fso.FileExists(filename))
return true;
else
return false;
}
/**
* 文件名处理
* @param filename
* @return
*/
function _filename(filename)
{
if(!this.file_exists(this.root+filename))
{
this.halt("filename:file "+filename+" does not exist.");
}
return this.root+filename;
}
/**
* 变量名处理
* @param varname
* @return
*/
function _varname(varname)
{
return "{"+varname+"}";
}
/**
* 完成字符串的处理
* @param str
* @return
*/
function _finish(str)
{
var re=new RegExp("{[^ \t\r\n\}]+\}","g");
if(this.unknowns=="remove")
{
str=str.replace(re,"");
}
else if(this.unknowns=="comment")
{
str=str.replace(re,"<!-- Template Variable undefined -->");
}
else
{
;
}
return str;
}
function _halt(msg)
{
this.last_error=msg;
if(this.halt_on_error!="no")
{
_haltmsg(msg);
}
if(this.halt_on_error=="yes")
{
alert("Halted.");
//System.exit(0);
}
}
function _haltmsg(msg)
{
alert("Template Error:"+msg);
}
/**
* HashMap构造函数
*/
function HashMap()
{
this.length = 0;
this.prefix = "hashmap_prefix_20050524_";
}
/**
* 向HashMap中添加键值对
*/
HashMap.prototype.put = function (key, value)
{
&n