CSDN博客

img XiaoQy

跟潜入式编程说拜拜,JScript脚本书写的aspTemplate类

发表于2004/7/16 23:17:00  1002人阅读

分类: Asp JScript

<%
/* ======================================================================

JScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1

NAME: Template.js

AUTHOR: yingzi , yilu
DATE  : 2004-6-14

COMMENT: 模仿PHPLIB中Template.inc文件而写的一个ASP模板处理类. 
         大家可以任意修改,使用,但是请尊重作者的劳动,留下作者的版权信息

========================================================================= */
function preg_quote(str)
{
    var reg = /([:./+*?[^/]$(){}=!<>|:)/s])/g;
    var str= str.replace(reg,"//$1")
    return str;
}

function write(str){
 Response.write("<textarea rows=6 cols=80>"+str+"</textarea>");
}
function getValByKey(array,key){
 if(typeof(array) != "object"){
  Response.Write(array+"is not a array");
  return false;
 }
 for(var i=0;i<array.length;i++){
  if(array[i][0] == key){
   return array[i][1];
  }
 }
 return false;
}

function getIndexByKey(array,key){
 if(typeof(array) != "object"){
  Response.Write(array+"is not a array");
  return false;
 }
 for(var i=0;i<array.length;i++){
  if(array[i][0] == key){
   return i;
  }
 }
 return false;
}

function aspTemplate(){
 var version = "1.0";
 var classname = "aspTemplate";
 var author = "paradis@inteway.com";
 var rootdir = "tpl/";
 var unkown = "remove";
 var debug = true;
 var haltonerror = "yes";
 var file = new Array();
 var lasterror = "";
 var left = "{";
 var right = "}";
 var varkeys =new Array();
 var varvals =new Array();
 var fso = new ActiveXObject("Scripting.FileSystemObject");
 
 this.getVersion = function(){
  return version;
 }
 
 this.get = function(varname){
  return eval(varname);
 }
 
 this.set = function(varname,value){
  eval(varname+"="+value);
 }
 
 this.setRoot = function(root){
  if(!fso.FolderExists(root)){
   this.haltMsg("目录不存在");
  }
  rootdir = root;
  return true;
 }
 
 this.setUnkown = function (strUnkown){
  unkown = strUnkown;
 }
 //参数类型
 //1.单文件模式 文件句柄 文件名
 //多文件模式
 //handle = new Array();
 //handle[0] = new Array();handle[0][0] = "h1";handle[0][1] = "f1";
 //handle[1] = new Array();handle[1][0] = "h2";handle[1][1] = "f2";
 //handle[2] = new Array();handle[2][0] = "h3";handle[2][1] = "f3";
 this.setFile = function(handle,filename){
  var index = file.length;
  if(!(typeof(handle)=="object")){
   if(typeof(filename) == "undefined"){
    this.haltMsg("没有为"+handle+"指定文件名");
   }
   file[index] = Array();
   file[index][0] = handle;
   file[index][1] = this.filename(filename);
  }else{
   for(var i=0;i<handle.length;i++){
    file[index] = new Array();
    file[index][0] = handle[i][0];
    file[index][1] = this.filename(handle[i][1]);
    index++;
   }
  }
 }
 
 //参数必须为相对于rootdir的目录,确认文件存在
 this.filename = function(filename){
  filename = Server.Mappath(rootdir+filename);
  if(!fso.FileExists(filename)){
   this.haltMsg("没有找到文件:"+filename);
  }
  return filename;
 }
 
 this.loadFile = function(handle){
  for(var i=0;i<varkeys.length;i++){
   if(varkeys[i][0]==handle){
    if(varvals[i][1].length>0){
     return true;
    }
   }
  }
  
  var handle_index = null;
  for(var i=0;i<file.length;i++){
   if(file[i][0] == handle){
    handle_index = i;
    break;
   }
  }
  
  if(handle_index == null){
   this.haltMsg(handle+"不是一个有效的文件句柄");
   return false;
  }
  
  var openfile = fso.OpenTextFile(file[handle_index][1],1);
  var str = "";
  while(!openfile.AtEndOfStream){
   str +=openfile.ReadLine()+"/n";
  }
  this.setVar(handle,str);
  return true;
 }
 
 this.setVar = function(varname,val){
  if(typeof(varname) != "object"){
   var nvar = new Array();
   nvar[0] = new Array();
   nvar[0][0] = varname;
   nvar[0][1] = val;
  }else{
   nvar = varname;
  }

  for(var i=0;i<nvar.length;i++){
   var index;
   index = getIndexByKey(varkeys,nvar[i][0]);
   
   if (index == false && index !== 0){
    index=varkeys.length;
   }
   
   varkeys[index] = new Array();
   varvals[index] = new Array();
   varkeys[index][0] = nvar[i][0];
   varkeys[index][1] = "/"+this.varName(nvar[i][0])+"/";
   varvals[index][0] = nvar[i][0];
   varvals[index][1] = nvar[i][1];
  }
  return true;
 }
 
 this.setBlock = function (parent,handle,name){
  if(!this.loadFile(parent)){
   this.haltMsg(parent+"不是有效的句柄");
  }
  var str = this.getVar(parent);
  oRegExp = new RegExp("<!--//s+BEGIN//s+"+handle+"//s+-->//s*((.|//n)*)//s*<!--//s+END//s+"+handle+"//s+-->","g");
  oRegExp.Multiline = true;
  var match = oRegExp.exec(str);
  str = str.replace(oRegExp,left+name+right);
  this.setVar(handle,match[1]);
  this.setVar(parent,str);
 }
 
 this.subst = function(handle){
  if(!this.loadFile(handle)){
   this.haltMsg(hanlde+"不是有效的句柄");
  }
  var str = this.getVar(handle);
  
  for(var i=0;i<varkeys.length;i++){
   reg = eval(varkeys[i][1]+"g");
   str = str.replace(reg,varvals[i][1]);
  }
  return str;
 }
 
 this.parse = function(target,handle,append){
  if(typeof(handle) == "undefined"){
   append = false;
  }
  if(typeof(handle) != "object"){
   var str = this.subst(handle);
   if(append && this.getVar(target)){
    this.setVar(target,this.getVar(target)+str);
   }else{
    this.setVar(target,str);
   }
  }else{
   for(var i=0;i<handle.length;i++){
    this.setVar(target,this.subst(handle[i]));
   }
  }
 }
 
 this.getVar = function (varname){
  return getValByKey(varvals,varname);
 }
 
 this.varName = function(varname){
  return preg_quote(left+varname+right);
 }
 
 this.finish = function(str){
  switch(unkown){
   case "keep":
   return str;
   case "remove":
   reg = /{[^ /t/r/n}]+}/g;
   str = str.replace(reg,"");
   return str;
   case "comment":
   reg = /{[^ /t/r/n}]+}/g;
   str = str.replace(reg,"<!-- Template handle: Variable "+RegExp.$1+" undefined -->");
   return str;
  }
 }
 
 this.p = function (varname){
  return this.finish(this.getVar(varname));
 }
 this.showall = function(){
  for (var i=0; i<varkeys.length; i++){
   write(varkeys[i][0]+"/n"+varvals[i][1]);
  }
 }
 this.haltMsg = function(msg){
  Response.Write(msg);
  Response.End();
 }
}
%>

0 0

相关博文

我的热门文章

img
取 消
img