TemplateControl.jvm.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. using System;
  25. using System.Collections;
  26. using System.ComponentModel;
  27. using System.Reflection;
  28. using System.Web;
  29. using System.IO;
  30. using System.Web.J2EE;
  31. using System.Xml;
  32. using vmw.common;
  33. using System.Web.Util;
  34. namespace System.Web.UI {
  35. public abstract class TemplateControl : Control, INamingContainer
  36. {
  37. static object abortTransaction = new object ();
  38. static object commitTransaction = new object ();
  39. static object error = new object ();
  40. static string [] methodNames = { "Page_Init",
  41. #if NET_2_0
  42. "Page_PreInit",
  43. "Page_PreLoad",
  44. "Page_LoadComplete",
  45. "Page_PreRenderComplete",
  46. "Page_SaveStateComplete",
  47. "Page_InitComplete",
  48. #endif
  49. "Page_Load",
  50. "Page_DataBind",
  51. "Page_PreRender",
  52. "Page_Disposed",
  53. "Page_Error",
  54. "Page_Unload",
  55. "Page_AbortTransaction",
  56. "Page_CommitTransaction" };
  57. const BindingFlags bflags = BindingFlags.Public |
  58. BindingFlags.NonPublic |
  59. BindingFlags.Instance;
  60. static readonly Type [] NoParams = new Type [0];
  61. private static string hashTableMutex = "lock"; //used to sync access ResourceHash property
  62. private byte [] GetResourceBytes (Type type)
  63. {
  64. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_BYTES");
  65. if (table == null) {
  66. return null;
  67. }
  68. return (byte []) table [type];
  69. }
  70. private void SetResourceBytes (Type type, byte [] bytes)
  71. {
  72. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_BYTES");
  73. if (table == null) {
  74. table = new Hashtable ();
  75. AppDomain.CurrentDomain.SetData ("TemplateControl.RES_BYTES", table);
  76. }
  77. table [type] = bytes;
  78. return;
  79. }
  80. private Hashtable ResourceHash
  81. {
  82. get
  83. {
  84. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_STRING");
  85. if (table == null) {
  86. table = new Hashtable ();
  87. AppDomain.CurrentDomain.SetData ("TemplateControl.RES_STRING", table);
  88. }
  89. return table;
  90. }
  91. }
  92. private string CachedString (string filename, int offset, int size)
  93. {
  94. string key = filename + offset + size;
  95. lock (hashTableMutex) {
  96. string strObj = (string) ResourceHash [key];
  97. if (strObj == null) {
  98. char [] tmp = System.Text.Encoding.UTF8.GetChars (GetResourceBytes (this.GetType ()), offset, size);
  99. strObj = new string (tmp);
  100. ResourceHash.Add (key, strObj);
  101. }
  102. return strObj;
  103. }
  104. }
  105. public virtual string TemplateSourceDirectory_Private
  106. {
  107. get { return null; }
  108. }
  109. #region Constructor
  110. protected TemplateControl ()
  111. {
  112. Construct ();
  113. }
  114. #endregion
  115. #region Properties
  116. [EditorBrowsable (EditorBrowsableState.Never)]
  117. protected virtual int AutoHandlers
  118. {
  119. get { return 0; }
  120. set { }
  121. }
  122. [EditorBrowsable (EditorBrowsableState.Never)]
  123. protected virtual bool SupportAutoEvents
  124. {
  125. get { return true; }
  126. }
  127. #endregion
  128. #region Methods
  129. protected virtual void Construct ()
  130. {
  131. }
  132. [MonoTODO]
  133. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  134. int size,
  135. bool fAsciiOnly)
  136. {
  137. string str = CachedString (this.GetType ().FullName, offset, size);
  138. return new LiteralControl (str);
  139. }
  140. sealed class EventMethodMap
  141. {
  142. public EventMethodMap (EventInfo Event, MethodInfo Method, bool NoParameters)
  143. {
  144. this.Event = Event;
  145. this.Method = Method;
  146. this.NoParameters = NoParameters;
  147. }
  148. public readonly EventInfo Event;
  149. public readonly MethodInfo Method;
  150. public readonly bool NoParameters;
  151. }
  152. // This hashtable cashes methods and events found in user code
  153. const string eventMethodCacheKey = "eventMethodCacheKey";
  154. static Hashtable EventMethodCache
  155. {
  156. get { return (Hashtable) AppDomain.CurrentDomain.GetData (eventMethodCacheKey); }
  157. set { AppDomain.CurrentDomain.SetData (eventMethodCacheKey, value); }
  158. }
  159. internal void WireupAutomaticEvents ()
  160. {
  161. Type cacheKey = this.GetType ();
  162. Hashtable eventMethodCache = EventMethodCache;
  163. ArrayList eventMethodList = eventMethodCache == null ? null : (ArrayList) eventMethodCache [cacheKey];
  164. if (eventMethodList == null) {
  165. eventMethodList = new ArrayList ();
  166. if (!SupportAutoEvents || !AutoEventWireup)
  167. return;
  168. Type thisType = typeof (TemplateControl);
  169. Type voidType = typeof (void);
  170. Type [] DefaultParams = new Type [] {
  171. typeof (object),
  172. typeof (EventArgs) };
  173. foreach (string methodName in methodNames) {
  174. MethodInfo method;
  175. bool noParams = false;
  176. Type type = GetType ();
  177. do {
  178. method = type.GetMethod (methodName, bflags, null, DefaultParams, null);
  179. if (method != null) {
  180. break;
  181. }
  182. type = type.BaseType;
  183. }
  184. while (type != thisType);
  185. if (method == null) {
  186. type = GetType ();
  187. do {
  188. method = type.GetMethod (methodName, bflags, null, NoParams, null);
  189. if (method != null) {
  190. noParams = true;
  191. break;
  192. }
  193. type = type.BaseType;
  194. }
  195. while (type != thisType);
  196. if (method == null)
  197. continue;
  198. }
  199. if (method.ReturnType != voidType)
  200. continue;
  201. int pos = methodName.IndexOf ("_");
  202. string eventName = methodName.Substring (pos + 1);
  203. EventInfo evt = GetType ().GetEvent (eventName);
  204. if (evt == null) {
  205. /* This should never happen */
  206. continue;
  207. }
  208. eventMethodList.Add (new EventMethodMap (evt, method, noParams));
  209. #if ONLY_1_1
  210. if (method.DeclaringType != type) {
  211. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  212. !method.IsFamilyAndAssembly && !method.IsFamily)
  213. continue;
  214. }
  215. #endif
  216. }
  217. // We copy to not lock
  218. Hashtable newEventMethodCache = eventMethodCache == null ? new Hashtable () : (Hashtable) eventMethodCache.Clone ();
  219. newEventMethodCache [cacheKey] = eventMethodList;
  220. EventMethodCache = newEventMethodCache;
  221. }
  222. foreach (EventMethodMap eventMethod in eventMethodList) {
  223. if (eventMethod.NoParameters) {
  224. NoParamsInvoker npi = new NoParamsInvoker (this, eventMethod.Method);
  225. eventMethod.Event.AddEventHandler (this, npi.FakeDelegate);
  226. }
  227. else {
  228. eventMethod.Event.AddEventHandler (this, Delegate.CreateDelegate (typeof (EventHandler), this, eventMethod.Method));
  229. }
  230. }
  231. }
  232. [EditorBrowsable (EditorBrowsableState.Never)]
  233. protected virtual void FrameworkInitialize ()
  234. {
  235. }
  236. Type GetTypeFromControlPath (string virtualPath)
  237. {
  238. if (virtualPath == null)
  239. throw new ArgumentNullException ("virtualPath");
  240. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  241. return PageMapper.GetObjectType (vpath);
  242. }
  243. public Control LoadControl (string virtualPath)
  244. {
  245. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  246. if (control is UserControl)
  247. ((UserControl) control).InitializeAsUserControl (Page);
  248. return (Control) control;
  249. }
  250. public ITemplate LoadTemplate (string virtualPath)
  251. {
  252. Type t = GetTypeFromControlPath (virtualPath);
  253. return new SimpleTemplate (t);
  254. }
  255. protected virtual void OnAbortTransaction (EventArgs e)
  256. {
  257. EventHandler eh = Events [abortTransaction] as EventHandler;
  258. if (eh != null)
  259. eh (this, e);
  260. }
  261. protected virtual void OnCommitTransaction (EventArgs e)
  262. {
  263. EventHandler eh = Events [commitTransaction] as EventHandler;
  264. if (eh != null)
  265. eh (this, e);
  266. }
  267. protected virtual void OnError (EventArgs e)
  268. {
  269. EventHandler eh = Events [error] as EventHandler;
  270. if (eh != null)
  271. eh (this, e);
  272. }
  273. [MonoNotSupported ("Not supported")]
  274. public Control ParseControl (string content)
  275. {
  276. throw new NotSupportedException ();
  277. }
  278. [MonoLimitation ("Always returns false")]
  279. public virtual bool TestDeviceFilter (string filterName)
  280. {
  281. return false;
  282. }
  283. [MonoTODO]
  284. [EditorBrowsable (EditorBrowsableState.Never)]
  285. public static object ReadStringResource (Type t)
  286. {
  287. return t;
  288. }
  289. #if NET_2_0
  290. [MonoTODO ("is this correct?")]
  291. public Object ReadStringResource ()
  292. {
  293. return this.GetType ();
  294. }
  295. #endif
  296. [MonoTODO]
  297. [EditorBrowsable (EditorBrowsableState.Never)]
  298. protected void SetStringResourcePointer (object stringResourcePointer,
  299. int maxResourceOffset)
  300. {
  301. if (GetResourceBytes (this.GetType ()) != null)
  302. return;
  303. java.lang.Class c = vmw.common.TypeUtils.ToClass (stringResourcePointer);
  304. java.lang.ClassLoader contextClassLoader = c.getClassLoader ();
  305. //TODO:move this code to page mapper
  306. string assemblyName = PageMapper.GetAssemblyResource (this.AppRelativeVirtualPath);
  307. java.io.InputStream inputStream = contextClassLoader.getResourceAsStream (assemblyName);
  308. System.IO.Stream strim = null;
  309. if (inputStream == null) {
  310. string descPath = String.Join ("/", new string [] { "assemblies", this.GetType ().Assembly.GetName ().Name, assemblyName });
  311. try {
  312. strim = new StreamReader (HttpContext.Current.Request.MapPath ("/" + descPath)).BaseStream;
  313. }
  314. catch (Exception ex) {
  315. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName, ex);
  316. }
  317. if (strim == null)
  318. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName);
  319. }
  320. try {
  321. if (strim == null)
  322. strim = (System.IO.Stream) vmw.common.IOUtils.getStream (inputStream);
  323. int capacity = (int) strim.Length;
  324. byte [] resourceBytes = new byte [capacity];
  325. strim.Read (resourceBytes, 0, capacity);
  326. SetResourceBytes (this.GetType (), resourceBytes);
  327. }
  328. catch (Exception e) {
  329. throw new HttpException ("problem with dll.ghres file", e);
  330. }
  331. finally {
  332. if (strim != null)
  333. strim.Close ();
  334. if (inputStream != null)
  335. inputStream.close ();
  336. }
  337. }
  338. [MonoTODO]
  339. [EditorBrowsable (EditorBrowsableState.Never)]
  340. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  341. int size, bool fAsciiOnly)
  342. {
  343. string str = CachedString (this.GetType ().FullName, offset, size);
  344. output.Write (str);
  345. }
  346. #endregion
  347. #region Events
  348. [WebSysDescription ("Raised when the user aborts a transaction.")]
  349. public event EventHandler AbortTransaction
  350. {
  351. add { Events.AddHandler (abortTransaction, value); }
  352. remove { Events.RemoveHandler (abortTransaction, value); }
  353. }
  354. [WebSysDescription ("Raised when the user initiates a transaction.")]
  355. public event EventHandler CommitTransaction
  356. {
  357. add { Events.AddHandler (commitTransaction, value); }
  358. remove { Events.RemoveHandler (commitTransaction, value); }
  359. }
  360. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  361. public event EventHandler Error
  362. {
  363. add { Events.AddHandler (error, value); }
  364. remove { Events.RemoveHandler (error, value); }
  365. }
  366. #endregion
  367. class SimpleTemplate : ITemplate
  368. {
  369. Type type;
  370. public SimpleTemplate (Type type)
  371. {
  372. this.type = type;
  373. }
  374. public void InstantiateIn (Control control)
  375. {
  376. Control template = Activator.CreateInstance (type) as Control;
  377. template.SetBindingContainer (false);
  378. control.Controls.Add (template);
  379. }
  380. }
  381. #if NET_2_0
  382. string _appRelativeVirtualPath = null;
  383. public string AppRelativeVirtualPath
  384. {
  385. get { return _appRelativeVirtualPath; }
  386. set
  387. {
  388. if (value == null)
  389. throw new ArgumentNullException ("value");
  390. if (!UrlUtils.IsRooted (value) && !(value.Length > 0 && value [0] == '~'))
  391. throw new ArgumentException ("The path that is set is not rooted");
  392. _appRelativeVirtualPath = value;
  393. int lastSlash = _appRelativeVirtualPath.LastIndexOf ('/');
  394. AppRelativeTemplateSourceDirectory = (lastSlash > 0) ? _appRelativeVirtualPath.Substring (0, lastSlash + 1) : "~/";
  395. }
  396. }
  397. protected internal object Eval (string expression)
  398. {
  399. return DataBinder.Eval (Page.GetDataItem (), expression);
  400. }
  401. protected internal string Eval (string expression, string format)
  402. {
  403. return DataBinder.Eval (Page.GetDataItem (), expression, format);
  404. }
  405. protected internal object XPath (string xpathexpression)
  406. {
  407. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression);
  408. }
  409. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  410. {
  411. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  412. }
  413. protected internal string XPath (string xpathexpression, string format)
  414. {
  415. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format);
  416. }
  417. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  418. {
  419. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  420. }
  421. protected internal IEnumerable XPathSelect (string xpathexpression)
  422. {
  423. return XPathBinder.Select (Page.GetDataItem (), xpathexpression);
  424. }
  425. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  426. {
  427. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  428. }
  429. protected object GetGlobalResourceObject (string className, string resourceKey)
  430. {
  431. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  432. }
  433. [MonoTODO ("Not implemented")]
  434. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  435. {
  436. // FIXME: not sure how to implement that one yet
  437. throw new NotSupportedException ();
  438. }
  439. protected Object GetLocalResourceObject (string resourceKey)
  440. {
  441. return HttpContext.GetLocalResourceObject (Context.Request.Path, resourceKey);
  442. }
  443. [MonoTODO ("Not implemented")]
  444. protected Object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  445. {
  446. // FIXME: not sure how to implement that one yet
  447. throw new NotSupportedException ();
  448. }
  449. #endif
  450. }
  451. }