TemplateControl.jvm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. internal void WireupAutomaticEvents ()
  141. {
  142. if (!SupportAutoEvents || !AutoEventWireup)
  143. return;
  144. Type thisType = typeof(TemplateControl);
  145. Type voidType = typeof (void);
  146. Type [] DefaultParams = new Type [] {
  147. typeof (object),
  148. typeof (EventArgs) };
  149. foreach (string methodName in methodNames) {
  150. MethodInfo method;
  151. bool noParams = false;
  152. Type type = GetType ();
  153. do {
  154. method = type.GetMethod (methodName, bflags, null, DefaultParams, null);
  155. if (method != null)
  156. break;
  157. type = type.BaseType;
  158. }
  159. while (type != thisType);
  160. if (method == null) {
  161. type = GetType ();
  162. do {
  163. method = type.GetMethod (methodName, bflags, null, NoParams, null);
  164. if (method != null) {
  165. noParams = true;
  166. break;
  167. }
  168. type = type.BaseType;
  169. }
  170. while (type != thisType);
  171. if (method == null)
  172. continue;
  173. }
  174. #if ONLY_1_1
  175. if (method.DeclaringType != type) {
  176. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  177. !method.IsFamilyAndAssembly && !method.IsFamily)
  178. continue;
  179. }
  180. #endif
  181. if (method.ReturnType != voidType)
  182. continue;
  183. int pos = methodName.IndexOf ("_");
  184. string eventName = methodName.Substring (pos + 1);
  185. EventInfo evt = GetType ().GetEvent (eventName);
  186. if (evt == null) {
  187. /* This should never happen */
  188. continue;
  189. }
  190. if (noParams) {
  191. NoParamsInvoker npi = new NoParamsInvoker (this, method);
  192. evt.AddEventHandler (this, npi.FakeDelegate);
  193. }
  194. else {
  195. evt.AddEventHandler (this, Delegate.CreateDelegate (
  196. typeof (EventHandler), this, method));
  197. }
  198. }
  199. }
  200. [EditorBrowsable (EditorBrowsableState.Never)]
  201. protected virtual void FrameworkInitialize ()
  202. {
  203. }
  204. Type GetTypeFromControlPath (string virtualPath)
  205. {
  206. if (virtualPath == null)
  207. throw new ArgumentNullException ("virtualPath");
  208. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  209. return PageMapper.GetObjectType (vpath);
  210. }
  211. public Control LoadControl (string virtualPath)
  212. {
  213. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  214. if (control is UserControl)
  215. ((UserControl) control).InitializeAsUserControl (Page);
  216. return (Control) control;
  217. }
  218. public ITemplate LoadTemplate (string virtualPath)
  219. {
  220. Type t = GetTypeFromControlPath (virtualPath);
  221. return new SimpleTemplate (t);
  222. }
  223. protected virtual void OnAbortTransaction (EventArgs e)
  224. {
  225. EventHandler eh = Events [abortTransaction] as EventHandler;
  226. if (eh != null)
  227. eh (this, e);
  228. }
  229. protected virtual void OnCommitTransaction (EventArgs e)
  230. {
  231. EventHandler eh = Events [commitTransaction] as EventHandler;
  232. if (eh != null)
  233. eh (this, e);
  234. }
  235. protected virtual void OnError (EventArgs e)
  236. {
  237. EventHandler eh = Events [error] as EventHandler;
  238. if (eh != null)
  239. eh (this, e);
  240. }
  241. [MonoNotSupported ("Not supported")]
  242. public Control ParseControl (string content)
  243. {
  244. throw new NotSupportedException ();
  245. }
  246. [MonoLimitation ("Always returns false")]
  247. public virtual bool TestDeviceFilter (string filterName)
  248. {
  249. return false;
  250. }
  251. [MonoTODO]
  252. [EditorBrowsable (EditorBrowsableState.Never)]
  253. public static object ReadStringResource (Type t)
  254. {
  255. return t;
  256. }
  257. #if NET_2_0
  258. [MonoTODO ("is this correct?")]
  259. public Object ReadStringResource ()
  260. {
  261. return this.GetType ();
  262. }
  263. #endif
  264. [MonoTODO]
  265. [EditorBrowsable (EditorBrowsableState.Never)]
  266. protected void SetStringResourcePointer (object stringResourcePointer,
  267. int maxResourceOffset)
  268. {
  269. if (GetResourceBytes (this.GetType ()) != null)
  270. return;
  271. java.lang.Class c = vmw.common.TypeUtils.ToClass (stringResourcePointer);
  272. java.lang.ClassLoader contextClassLoader = c.getClassLoader ();
  273. //TODO:move this code to page mapper
  274. string assemblyName = PageMapper.GetAssemblyResource (this.AppRelativeVirtualPath);
  275. java.io.InputStream inputStream = contextClassLoader.getResourceAsStream (assemblyName);
  276. System.IO.Stream strim = null;
  277. if (inputStream == null) {
  278. string descPath = String.Join ("/", new string [] { "assemblies", this.GetType ().Assembly.GetName ().Name, assemblyName });
  279. try {
  280. strim = new StreamReader (HttpContext.Current.Request.MapPath ("/" + descPath)).BaseStream;
  281. }
  282. catch (Exception ex) {
  283. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName, ex);
  284. }
  285. if (strim == null)
  286. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName);
  287. }
  288. try {
  289. if (strim == null)
  290. strim = (System.IO.Stream) vmw.common.IOUtils.getStream (inputStream);
  291. int capacity = (int) strim.Length;
  292. byte [] resourceBytes = new byte [capacity];
  293. strim.Read (resourceBytes, 0, capacity);
  294. SetResourceBytes (this.GetType (), resourceBytes);
  295. }
  296. catch (Exception e) {
  297. throw new HttpException ("problem with dll.ghres file", e);
  298. }
  299. finally {
  300. if (strim != null)
  301. strim.Close ();
  302. if (inputStream != null)
  303. inputStream.close ();
  304. }
  305. }
  306. [MonoTODO]
  307. [EditorBrowsable (EditorBrowsableState.Never)]
  308. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  309. int size, bool fAsciiOnly)
  310. {
  311. string str = CachedString (this.GetType ().FullName, offset, size);
  312. output.Write (str);
  313. }
  314. #endregion
  315. #region Events
  316. [WebSysDescription ("Raised when the user aborts a transaction.")]
  317. public event EventHandler AbortTransaction
  318. {
  319. add { Events.AddHandler (abortTransaction, value); }
  320. remove { Events.RemoveHandler (abortTransaction, value); }
  321. }
  322. [WebSysDescription ("Raised when the user initiates a transaction.")]
  323. public event EventHandler CommitTransaction
  324. {
  325. add { Events.AddHandler (commitTransaction, value); }
  326. remove { Events.RemoveHandler (commitTransaction, value); }
  327. }
  328. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  329. public event EventHandler Error
  330. {
  331. add { Events.AddHandler (error, value); }
  332. remove { Events.RemoveHandler (error, value); }
  333. }
  334. #endregion
  335. class SimpleTemplate : ITemplate
  336. {
  337. Type type;
  338. public SimpleTemplate (Type type)
  339. {
  340. this.type = type;
  341. }
  342. public void InstantiateIn (Control control)
  343. {
  344. Control template = Activator.CreateInstance (type) as Control;
  345. template.SetBindingContainer (false);
  346. control.Controls.Add (template);
  347. }
  348. }
  349. #if NET_2_0
  350. string _appRelativeVirtualPath = null;
  351. public string AppRelativeVirtualPath
  352. {
  353. get { return _appRelativeVirtualPath; }
  354. set
  355. {
  356. if (value == null)
  357. throw new ArgumentNullException ("value");
  358. if (!UrlUtils.IsRooted (value) && !(value.Length > 0 && value [0] == '~'))
  359. throw new ArgumentException ("The path that is set is not rooted");
  360. _appRelativeVirtualPath = value;
  361. int lastSlash = _appRelativeVirtualPath.LastIndexOf ('/');
  362. AppRelativeTemplateSourceDirectory = (lastSlash > 0) ? _appRelativeVirtualPath.Substring (0, lastSlash + 1) : "~/";
  363. }
  364. }
  365. protected internal object Eval (string expression)
  366. {
  367. return DataBinder.Eval (Page.GetDataItem (), expression);
  368. }
  369. protected internal string Eval (string expression, string format)
  370. {
  371. return DataBinder.Eval (Page.GetDataItem (), expression, format);
  372. }
  373. protected internal object XPath (string xpathexpression)
  374. {
  375. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression);
  376. }
  377. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  378. {
  379. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  380. }
  381. protected internal string XPath (string xpathexpression, string format)
  382. {
  383. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format);
  384. }
  385. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  386. {
  387. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  388. }
  389. protected internal IEnumerable XPathSelect (string xpathexpression)
  390. {
  391. return XPathBinder.Select (Page.GetDataItem (), xpathexpression);
  392. }
  393. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  394. {
  395. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  396. }
  397. protected object GetGlobalResourceObject (string className, string resourceKey)
  398. {
  399. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  400. }
  401. [MonoTODO ("Not implemented")]
  402. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  403. {
  404. // FIXME: not sure how to implement that one yet
  405. throw new NotSupportedException ();
  406. }
  407. protected Object GetLocalResourceObject (string resourceKey)
  408. {
  409. return HttpContext.GetLocalResourceObject (Context.Request.Path, resourceKey);
  410. }
  411. [MonoTODO ("Not implemented")]
  412. protected Object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  413. {
  414. // FIXME: not sure how to implement that one yet
  415. throw new NotSupportedException ();
  416. }
  417. #endif
  418. }
  419. }