TemplateControl.jvm.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. private static string hashTableMutex = "lock"; //used to sync access ResourceHash property
  61. private byte [] GetResourceBytes (Type type)
  62. {
  63. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_BYTES");
  64. if (table == null) {
  65. return null;
  66. }
  67. return (byte []) table [type];
  68. }
  69. private void SetResourceBytes (Type type, byte [] bytes)
  70. {
  71. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_BYTES");
  72. if (table == null) {
  73. table = new Hashtable ();
  74. AppDomain.CurrentDomain.SetData ("TemplateControl.RES_BYTES", table);
  75. }
  76. table [type] = bytes;
  77. return;
  78. }
  79. private Hashtable ResourceHash
  80. {
  81. get
  82. {
  83. Hashtable table = (Hashtable) AppDomain.CurrentDomain.GetData ("TemplateControl.RES_STRING");
  84. if (table == null) {
  85. table = new Hashtable ();
  86. AppDomain.CurrentDomain.SetData ("TemplateControl.RES_STRING", table);
  87. }
  88. return table;
  89. }
  90. }
  91. private string CachedString (string filename, int offset, int size)
  92. {
  93. string key = filename + offset + size;
  94. lock (hashTableMutex) {
  95. string strObj = (string) ResourceHash [key];
  96. if (strObj == null) {
  97. char [] tmp = System.Text.Encoding.UTF8.GetChars (GetResourceBytes (this.GetType ()), offset, size);
  98. strObj = new string (tmp);
  99. ResourceHash.Add (key, strObj);
  100. }
  101. return strObj;
  102. }
  103. }
  104. public virtual string TemplateSourceDirectory_Private
  105. {
  106. get { return null; }
  107. }
  108. #region Constructor
  109. protected TemplateControl ()
  110. {
  111. Construct ();
  112. }
  113. #endregion
  114. #region Properties
  115. [EditorBrowsable (EditorBrowsableState.Never)]
  116. protected virtual int AutoHandlers
  117. {
  118. get { return 0; }
  119. set { }
  120. }
  121. [EditorBrowsable (EditorBrowsableState.Never)]
  122. protected virtual bool SupportAutoEvents
  123. {
  124. get { return true; }
  125. }
  126. #endregion
  127. #region Methods
  128. protected virtual void Construct ()
  129. {
  130. }
  131. [MonoTODO]
  132. protected LiteralControl CreateResourceBasedLiteralControl (int offset,
  133. int size,
  134. bool fAsciiOnly)
  135. {
  136. string str = CachedString (this.GetType ().FullName, offset, size);
  137. return new LiteralControl (str);
  138. }
  139. internal void WireupAutomaticEvents ()
  140. {
  141. if (!SupportAutoEvents || !AutoEventWireup)
  142. return;
  143. Type type = GetType ();
  144. foreach (string methodName in methodNames) {
  145. MethodInfo method = type.GetMethod (methodName, bflags);
  146. if (method == null)
  147. continue;
  148. #if ONLY_1_1
  149. if (method.DeclaringType != type) {
  150. if (!method.IsPublic && !method.IsFamilyOrAssembly &&
  151. !method.IsFamilyAndAssembly && !method.IsFamily)
  152. continue;
  153. }
  154. #endif
  155. if (method.ReturnType != typeof (void))
  156. continue;
  157. ParameterInfo [] parms = method.GetParameters ();
  158. int length = parms.Length;
  159. bool noParams = (length == 0);
  160. if (!noParams && (length != 2 ||
  161. parms [0].ParameterType != typeof (object) ||
  162. parms [1].ParameterType != typeof (EventArgs)))
  163. continue;
  164. int pos = methodName.IndexOf ("_");
  165. string eventName = methodName.Substring (pos + 1);
  166. EventInfo evt = type.GetEvent (eventName);
  167. if (evt == null) {
  168. /* This should never happen */
  169. continue;
  170. }
  171. if (noParams) {
  172. NoParamsInvoker npi = new NoParamsInvoker (this, methodName);
  173. evt.AddEventHandler (this, npi.FakeDelegate);
  174. }
  175. else {
  176. evt.AddEventHandler (this, Delegate.CreateDelegate (
  177. typeof (EventHandler), this, methodName));
  178. }
  179. }
  180. }
  181. [EditorBrowsable (EditorBrowsableState.Never)]
  182. protected virtual void FrameworkInitialize ()
  183. {
  184. }
  185. Type GetTypeFromControlPath (string virtualPath)
  186. {
  187. if (virtualPath == null)
  188. throw new ArgumentNullException ("virtualPath");
  189. string vpath = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  190. return PageMapper.GetObjectType (vpath);
  191. }
  192. public Control LoadControl (string virtualPath)
  193. {
  194. object control = Activator.CreateInstance (GetTypeFromControlPath (virtualPath));
  195. if (control is UserControl)
  196. ((UserControl) control).InitializeAsUserControl (Page);
  197. return (Control) control;
  198. }
  199. public ITemplate LoadTemplate (string virtualPath)
  200. {
  201. Type t = GetTypeFromControlPath (virtualPath);
  202. return new SimpleTemplate (t);
  203. }
  204. protected virtual void OnAbortTransaction (EventArgs e)
  205. {
  206. EventHandler eh = Events [abortTransaction] as EventHandler;
  207. if (eh != null)
  208. eh (this, e);
  209. }
  210. protected virtual void OnCommitTransaction (EventArgs e)
  211. {
  212. EventHandler eh = Events [commitTransaction] as EventHandler;
  213. if (eh != null)
  214. eh (this, e);
  215. }
  216. protected virtual void OnError (EventArgs e)
  217. {
  218. EventHandler eh = Events [error] as EventHandler;
  219. if (eh != null)
  220. eh (this, e);
  221. }
  222. [MonoNotSupported ("Not supported")]
  223. public Control ParseControl (string content)
  224. {
  225. throw new NotSupportedException ();
  226. }
  227. [MonoLimitation ("Always returns false")]
  228. public virtual bool TestDeviceFilter (string filterName)
  229. {
  230. return false;
  231. }
  232. [MonoTODO]
  233. [EditorBrowsable (EditorBrowsableState.Never)]
  234. public static object ReadStringResource (Type t)
  235. {
  236. return t;
  237. }
  238. #if NET_2_0
  239. [MonoTODO ("is this correct?")]
  240. public Object ReadStringResource ()
  241. {
  242. return this.GetType ();
  243. }
  244. #endif
  245. [MonoTODO]
  246. [EditorBrowsable (EditorBrowsableState.Never)]
  247. protected void SetStringResourcePointer (object stringResourcePointer,
  248. int maxResourceOffset)
  249. {
  250. if (GetResourceBytes (this.GetType ()) != null)
  251. return;
  252. java.lang.Class c = vmw.common.TypeUtils.ToClass (stringResourcePointer);
  253. java.lang.ClassLoader contextClassLoader = c.getClassLoader ();
  254. //TODO:move this code to page mapper
  255. string assemblyName = PageMapper.GetAssemblyResource (this.AppRelativeVirtualPath);
  256. java.io.InputStream inputStream = contextClassLoader.getResourceAsStream (assemblyName);
  257. System.IO.Stream strim = null;
  258. if (inputStream == null) {
  259. string descPath = String.Join ("/", new string [] { "assemblies", this.GetType ().Assembly.GetName ().Name, assemblyName });
  260. try {
  261. strim = new StreamReader (HttpContext.Current.Request.MapPath ("/" + descPath)).BaseStream;
  262. }
  263. catch (Exception ex) {
  264. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName, ex);
  265. }
  266. if (strim == null)
  267. throw new System.IO.IOException ("couldn't open resource file:" + assemblyName);
  268. }
  269. try {
  270. if (strim == null)
  271. strim = (System.IO.Stream) vmw.common.IOUtils.getStream (inputStream);
  272. int capacity = (int) strim.Length;
  273. byte [] resourceBytes = new byte [capacity];
  274. strim.Read (resourceBytes, 0, capacity);
  275. SetResourceBytes (this.GetType (), resourceBytes);
  276. }
  277. catch (Exception e) {
  278. throw new HttpException ("problem with dll.ghres file", e);
  279. }
  280. finally {
  281. if (strim != null)
  282. strim.Close ();
  283. if (inputStream != null)
  284. inputStream.close ();
  285. }
  286. }
  287. [MonoTODO]
  288. [EditorBrowsable (EditorBrowsableState.Never)]
  289. protected void WriteUTF8ResourceString (HtmlTextWriter output, int offset,
  290. int size, bool fAsciiOnly)
  291. {
  292. string str = CachedString (this.GetType ().FullName, offset, size);
  293. output.Write (str);
  294. }
  295. #endregion
  296. #region Events
  297. [WebSysDescription ("Raised when the user aborts a transaction.")]
  298. public event EventHandler AbortTransaction
  299. {
  300. add { Events.AddHandler (abortTransaction, value); }
  301. remove { Events.RemoveHandler (abortTransaction, value); }
  302. }
  303. [WebSysDescription ("Raised when the user initiates a transaction.")]
  304. public event EventHandler CommitTransaction
  305. {
  306. add { Events.AddHandler (commitTransaction, value); }
  307. remove { Events.RemoveHandler (commitTransaction, value); }
  308. }
  309. [WebSysDescription ("Raised when an exception occurs that cannot be handled.")]
  310. public event EventHandler Error
  311. {
  312. add { Events.AddHandler (error, value); }
  313. remove { Events.RemoveHandler (error, value); }
  314. }
  315. #endregion
  316. class SimpleTemplate : ITemplate
  317. {
  318. Type type;
  319. public SimpleTemplate (Type type)
  320. {
  321. this.type = type;
  322. }
  323. public void InstantiateIn (Control control)
  324. {
  325. Control template = Activator.CreateInstance (type) as Control;
  326. template.SetBindingContainer (false);
  327. control.Controls.Add (template);
  328. }
  329. }
  330. #if NET_2_0
  331. string _appRelativeVirtualPath = null;
  332. public string AppRelativeVirtualPath
  333. {
  334. get { return _appRelativeVirtualPath; }
  335. set
  336. {
  337. if (value == null)
  338. throw new ArgumentNullException ("value");
  339. if (!UrlUtils.IsRooted (value) && !(value.Length > 0 && value [0] == '~'))
  340. throw new ArgumentException ("The path that is set is not rooted");
  341. _appRelativeVirtualPath = value;
  342. int lastSlash = _appRelativeVirtualPath.LastIndexOf ('/');
  343. AppRelativeTemplateSourceDirectory = (lastSlash > 0) ? _appRelativeVirtualPath.Substring (0, lastSlash + 1) : "~/";
  344. }
  345. }
  346. protected internal object Eval (string expression)
  347. {
  348. return DataBinder.Eval (Page.GetDataItem (), expression);
  349. }
  350. protected internal string Eval (string expression, string format)
  351. {
  352. return DataBinder.Eval (Page.GetDataItem (), expression, format);
  353. }
  354. protected internal object XPath (string xpathexpression)
  355. {
  356. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression);
  357. }
  358. protected internal object XPath (string xpathexpression, IXmlNamespaceResolver resolver)
  359. {
  360. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, null, resolver);
  361. }
  362. protected internal string XPath (string xpathexpression, string format)
  363. {
  364. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format);
  365. }
  366. protected internal string XPath (string xpathexpression, string format, IXmlNamespaceResolver resolver)
  367. {
  368. return XPathBinder.Eval (Page.GetDataItem (), xpathexpression, format, resolver);
  369. }
  370. protected internal IEnumerable XPathSelect (string xpathexpression)
  371. {
  372. return XPathBinder.Select (Page.GetDataItem (), xpathexpression);
  373. }
  374. protected internal IEnumerable XPathSelect (string xpathexpression, IXmlNamespaceResolver resolver)
  375. {
  376. return XPathBinder.Select (Page.GetDataItem (), xpathexpression, resolver);
  377. }
  378. protected object GetGlobalResourceObject (string className, string resourceKey)
  379. {
  380. return HttpContext.GetGlobalResourceObject (className, resourceKey);
  381. }
  382. [MonoTODO ("Not implemented")]
  383. protected object GetGlobalResourceObject (string className, string resourceKey, Type objType, string propName)
  384. {
  385. // FIXME: not sure how to implement that one yet
  386. throw new NotSupportedException ();
  387. }
  388. protected Object GetLocalResourceObject (string resourceKey)
  389. {
  390. return HttpContext.GetLocalResourceObject (Context.Request.Path, resourceKey);
  391. }
  392. [MonoTODO ("Not implemented")]
  393. protected Object GetLocalResourceObject (string resourceKey, Type objType, string propName)
  394. {
  395. // FIXME: not sure how to implement that one yet
  396. throw new NotSupportedException ();
  397. }
  398. #endif
  399. }
  400. }