本文所在的实例工具地址是 :http://www.webtool.cn/tools/list/n2399997.htm
先看一下Tagus.Components的AD图
下面开始从JS代码进入整个过程进行分析。
sys.tools.getResults(..)
详细代码请使用firebug进入上面的页面查看,这里不做详细介绍了.
以上JS代码是一个封装的AJAX调用。他会直接访问我的tool.ashx页面,下面我们从tool.ashx开始讲起
public class Tool : APIFactory
此ASHX类继承自APIFactory抽象类
代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using Tagus.Components; using System.IO; using System.Xml; using System.Text; using System.Collections.Specialized; namespace Tagus.Web.Tools.SAPI{ public delegate void DoRequestEventHandler(HttpContext context, string [] param); public abstract class APIFactory : IHttpHandler { protected abstract void callBack(HttpContext context, string [] param); protected virtual string GetBigData() { System.IO.Stream instream = TagusContext.Current.Context.Request.InputStream; BinaryReader br = new BinaryReader(instream, System.Text.Encoding.UTF8); byte [] byt = br.ReadBytes(( int )instream.Length); string sXml = System.Text.Encoding.UTF8.GetString(byt); System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(sXml); XmlElement xe = xmlDoc.DocumentElement; StringBuilder sGuid = new StringBuilder(); for ( int i = 0 ; i < xe.ChildNodes.Count; i ++ ) { if (i > 0 ) sGuid.Append( " , " ); sGuid.Append( " ' " + xe.ChildNodes[i].InnerText + " ' " ); } return sGuid.ToString(); } protected virtual void DoPostRequest(NameValueCollection collection) { } protected void doRequest(HttpContext context, string rspType) { context.Response.ContentType = rspType; context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Request.ContentEncoding = System.Text.Encoding.UTF8; if (context.Request.HttpMethod.ToLower() == " post " ) { NameValueCollection collection = context.Request.Params; DoPostRequest(collection); } else if (context.Request.HttpMethod.ToLower() == " get " ) { if (context.Request[ " do " ] == null ) { context.Response.Write( " you must send params ! " ); } else { string [] s = Security.CheckParams(context.Request.QueryString[ " do " ]); for ( int i = 0 ; i < s.Length; i ++ ) { s[i] = HttpFunctions.GetRepString(s[i]); } try { if (s != null && s.Length >= 3 ) { for ( int i = 0 ; i < s.Length; i ++ ) { s[i] = s[i].Replace( " amp; " , " & " ); s[i] = s[i].Replace( " sdot; " , " , " ); } callBack(context, s); } } catch (Exception ex) { context.Response.Write(ex.Message); } } } else { } } #region IHttpHandler 成员 public bool IsReusable { get { return false ; } } public void ProcessRequest(HttpContext context) { doRequest(context, " text/plain " ); } #endregion }}
此类主要是对AJAX进行安全或基础处理。
然后回到Tool类
string s = TagusContext.Current.Provider.GetToolResults(param[ 3 ], param, null ); context.Response.Write(s);
以上语句是此类中的获取工具结果的定义
代码 public class TagusContext{ /// <summary> /// 当前TagusContext对象 /// </summary> public static TagusContext Current { get { if (HttpContext.Current == null ) return new TagusContext(); return (TagusContext)HttpContext.Current.Items[ " TagusContext " ]; } } private BaseToolProvider provider = null ; /// <summary> /// 基础工具提供者对象 /// </summary> public BaseToolProvider Provider { get { return provider; } } /// <summary> /// 构造函数 /// </summary> public TagusContext() { provider = ToolFactory.CreateToolProvider(); }}
此为系统上下文类,在类中定义了基础工具提供者对象,并且在构造函数中已经采用抽象工厂的设计模式实例化.
代码 /// <summary> /// 工具应用实现类抽象工厂 /// </summary> public class ToolFactory { public static BaseToolProvider CreateToolProvider() { return Tools.Instance; } }
这里因为还没有实际性的抽象需要,所以直接用Tools.Instance属性实例化。
Tools实现BaseToolProvider抽象类,在BaseToolProvider抽象类中 我们有这样的定义
代码 /// <summary> /// 获取工具查询结果 /// </summary> /// <param name="pid"> 工具编号 </param> /// <param name="param"> 工具所需参数数组 </param> /// <returns></returns> public string GetToolResults( string pid, string [] param, NameValueCollection collection) { BaseToolDesignerProvider dProvider = ToolDesignerProvider(pid); string s = null ; try { dProvider.Init(pid); } catch { switch (TagusContext.Current.Context.Request.HttpMethod.ToLower()) { case " get " : s = dProvider.GetResult(pid, param); break ; case " post " : s = dProvider.GetResult(pid, collection); break ; } return s; } switch (TagusContext.Current.Context.Request.HttpMethod.ToLower()) { case " get " : s = dProvider.GetResult(pid, param); break ; case " post " : s = dProvider.GetResult(pid, collection); break ; } return s; }
pid 是工具的加密参数,在本例中大家或许已经注意到ToolDesignerProvider(pid)这个方法。 我们来看看这个方法到底是干嘛的。
代码 /// <summary> /// 工具开发者接口提供类 /// </summary> /// <param name="pid"></param> /// <returns></returns> protected virtual BaseToolDesignerProvider ToolDesignerProvider( string pid) { ToolInfo t = dal.Get( int .Parse(DES.Decrypt(pid))); return (BaseToolDesignerProvider)Assembly.Load(t.AssemblyName).CreateInstance(t.ClassName, false , BindingFlags.Default, null , null , null , null ); }
很显然,在此方法中会使用到反射。 那么为什么要使用反射呢? 其实这是因为在我们的系统中拥有开发者平台。工具并不是由一个人开发的,而是很多人一起开发,大家都有自己的类库,所以在这里需要使用反射来找到目标类库,嘿嘿 如果你有兴趣的话也可以联系QQ7437280来加入我们的开发者平台。
下面是BaseToolDesignerProvider类的全部代码
代码 // 此代码由Tagus项目组开发,它将主要用于为工具开发者提供接口支持,工具开发者必须以自己的方式 // 实现此接口来支持工具初始化及返回查询结果等功能。 // Copyright 2009 四川百易年华网络服务有限公司 // 原始版本作者:王智 创建时间:2009-12-15 17:26 // 最后更新时间:2009-12-15 17:26,修改人:王智 using System; using System.Collections.Generic; using System.Text; using Tagus.DataFactory; using Tagus.Model; using System.Collections.Specialized; namespace Tagus.Components{ public abstract class BaseToolDesignerProvider { /// <summary> /// 初始化工具 /// </summary> /// <param name="pid"> 工具编号 </param> /// <returns> 返回的显示信息 </returns> public abstract string Init( string pid); /// <summary> /// 获取输入栏包含信息 /// </summary> /// <returns></returns> public abstract string GetInputSectionInfomation(); /// <summary> /// 获取工具查询结果 /// </summary> /// <param name="param"> 参数列表 </param> /// <returns> 查询结果字符串 </returns> internal string GetResult( string pid, string [] param) { ToolInfo instance = TagusContext.Current.Provider.GetTool(DES.Decrypt(pid)); return DoResult(param, instance); } /// <summary> /// 获取工具查询结果 Post /// </summary> /// <param name="pid"> 工具编号 </param> /// <param name="collection"> Form窗体变量集合 </param> /// <returns></returns> internal string GetResult( string pid, NameValueCollection collection) { ToolInfo instance = TagusContext.Current.Provider.GetTool(DES.Decrypt(pid)); return DoResultByPost(collection, instance); } /// <summary> /// 生成工具查询结果 Get方式 /// </summary> /// <param name="paras"> 参数列表 </param> /// <param name="tool"> 工具实体对象 </param> /// <returns></returns> public abstract string DoResult( string [] paras,ToolInfo tool); /// <summary> /// 生成工具查询结果 Post方式 /// </summary> /// <param name="collction"></param> /// <param name="tool"></param> /// <returns></returns> public abstract string DoResultByPost(NameValueCollection collction, ToolInfo tool); }}
BaseToolProvider会调用此抽象类中的方法获取结果,但是此为抽象类 那么需要实现此抽象类的类,请看下面代码:
代码 using System; using System.Collections.Generic; using System.Text; using Tagus.Model; namespace Tagus.Components{ public abstract class CollectToolDesignerProvider : BaseToolDesignerProvider { protected List < BaseCollectActiveXObject > _clt_Objs; public override string Init( string pid) { if (_clt_Objs == null ) { _clt_Objs = new List < BaseCollectActiveXObject > (); } return "" ; } protected void AddResources(BaseCollectActiveXObject obj) { if (_clt_Objs == null ) { _clt_Objs = new List < BaseCollectActiveXObject > (); } _clt_Objs.Add(obj); } protected virtual void DoResources(StringBuilder sb, string flag) { if (sb != null ) { foreach (BaseCollectActiveXObject obj in _clt_Objs) { if (obj.Flag == flag) { sb.Append( " sys.behavior.$doAX(' " + obj.TID + " ',' " + obj.Domain + " ',' " + obj.Port + " ',' " + obj.Path + " ',' " + obj.Method + " ',' " + obj.Params + " ',' " + obj.Flag + " ',' " + obj.RefUrl + " ',' " + obj.Referer + " ',' " + obj.CharSet + " ',' " + obj.CallBack + " '); " ); } } } } public override string GetInputSectionInfomation() { return GetInputInfomation(); } public abstract string GetInputInfomation(); public override string DoResult( string [] paras, ToolInfo tool) { return DoCollectResult(paras, tool); } public abstract string DoCollectResult( string [] paras, ToolInfo tool); public override string DoResultByPost(System.Collections.Specialized.NameValueCollection collction, ToolInfo tool) { return DoCollectResultByPost(collction, tool); } public abstract string DoCollectResultByPost(System.Collections.Specialized.NameValueCollection collction, ToolInfo tool); }}
此类为插件版本工具专用抽象类,插件版本工具必须要实现此类。此类有两个函数 AddResources DoResources yao zhuyi.
it's all.
posted on 2010-04-16 15:49 Rhina 阅读( ...) 评论( ...) 编辑 收藏
转载于:https://www.cnblogs.com/Boyona/archive/2010/04/16/1713662.html
