TemplateControl.jvm.cs 13 KB

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