Xml.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // System.Web.UI.WebControls.Xml.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. // (C) Gaurav Vaish (2002)
  11. // (C) 2003 Andreas Nahr
  12. //
  13. using System;
  14. using System.ComponentModel;
  15. using System.ComponentModel.Design;
  16. using System.IO;
  17. using System.Xml;
  18. using System.Xml.Xsl;
  19. using System.Xml.XPath;
  20. using System.Web;
  21. using System.Web.UI;
  22. namespace System.Web.UI.WebControls
  23. {
  24. [DefaultProperty("DocumentSource")]
  25. [PersistChildren(false)]
  26. // TODO add control builder
  27. //[ControlBuilder ()]
  28. [Designer ("System.Web.UI.Design.WebControls.XmlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  29. public class Xml : Control
  30. {
  31. private XmlDocument document;
  32. private string documentContent;
  33. private string documentSource;
  34. private XslTransform transform;
  35. private XsltArgumentList transformArgumentList;
  36. private string transformSource;
  37. private XPathDocument xpathDoc;
  38. private static XslTransform defaultTransform;
  39. static Xml()
  40. {
  41. XmlTextReader reader = new XmlTextReader(new StringReader("<xsl:stylesheet version='1.0' " +
  42. "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
  43. "<xsl:template match=\"*\">" +
  44. "<xsl:copy-of select=\".\"/>" +
  45. "</xsl:template>" +
  46. "</xsl:stylesheet>"));
  47. defaultTransform = new XslTransform();
  48. #if NET_1_0
  49. defaultTransform.Load (reader);
  50. #else
  51. defaultTransform.Load (reader, null, null);
  52. #endif
  53. }
  54. public Xml(): base()
  55. {
  56. }
  57. [MonoTODO("security")]
  58. private void LoadXmlDoc ()
  59. {
  60. if (documentContent != null && documentContent.Length > 0) {
  61. document = new XmlDocument();
  62. document.LoadXml (documentContent);
  63. return;
  64. }
  65. if (documentSource != null && documentSource.Length != 0) {
  66. document = new XmlDocument();
  67. document.Load (documentSource);
  68. }
  69. }
  70. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  71. [WebSysDescription ("This is the XML document that is used for the XML Webcontrol.")]
  72. public XmlDocument Document
  73. {
  74. get
  75. {
  76. if(document == null)
  77. LoadXmlDoc();
  78. return document;
  79. }
  80. set
  81. {
  82. documentSource = null;
  83. documentContent = null;
  84. xpathDoc = null;
  85. document = value;
  86. }
  87. }
  88. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  89. [WebSysDescription ("The XML content that is transformed for the XML Webcontrol.")]
  90. public string DocumentContent
  91. {
  92. get
  93. {
  94. return String.Empty;
  95. }
  96. set
  97. {
  98. document = null;
  99. xpathDoc = null;
  100. documentContent = value;
  101. }
  102. }
  103. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  104. [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  105. [WebSysDescription ("The URL or the source of the XML content that is transformed for the XML Webcontrol.")]
  106. public string DocumentSource
  107. {
  108. get
  109. {
  110. if(documentSource != null)
  111. return documentSource;
  112. return String.Empty;
  113. }
  114. set
  115. {
  116. document = null;
  117. documentContent = null;
  118. xpathDoc = null;
  119. documentSource = value;
  120. }
  121. }
  122. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  123. [WebSysDescription ("The XSL transform that is applied to this XML Webcontrol.")]
  124. public XslTransform Transform
  125. {
  126. get
  127. {
  128. return transform;
  129. }
  130. set
  131. {
  132. transformSource = null;
  133. transform = value;
  134. }
  135. }
  136. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  137. [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  138. [WebSysDescription ("An URL specifying the source that is used for the XSL transformation.")]
  139. public string TransformSource
  140. {
  141. get
  142. {
  143. if(transformSource != null)
  144. return transformSource;
  145. return String.Empty;
  146. }
  147. set
  148. {
  149. transform = null;
  150. transformSource = value;
  151. }
  152. }
  153. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  154. [WebSysDescription ("Arguments that are used by the XSL Transform.")]
  155. public XsltArgumentList TransformArgumentList
  156. {
  157. get
  158. {
  159. return transformArgumentList;
  160. }
  161. set
  162. {
  163. transformArgumentList = value;
  164. }
  165. }
  166. protected override void AddParsedSubObject(object obj)
  167. {
  168. if(obj is LiteralControl)
  169. {
  170. DocumentContent = ((LiteralControl)obj).Text;
  171. return;
  172. }
  173. throw new HttpException (HttpRuntime.FormatResourceString (
  174. "Cannot_Have_Children_of_Type",
  175. "Xml",
  176. GetType().Name));
  177. }
  178. [MonoTODO("security")]
  179. private void LoadXpathDoc ()
  180. {
  181. if(documentContent != null && documentContent.Length > 0) {
  182. xpathDoc = new XPathDocument (new StringReader (documentContent));
  183. return;
  184. }
  185. if (documentSource != null && documentSource.Length != 0) {
  186. xpathDoc = new XPathDocument (MapPathSecure (documentSource));
  187. return;
  188. }
  189. }
  190. [MonoTODO("security")]
  191. private void LoadTransform ()
  192. {
  193. if (transform != null)
  194. return;
  195. if (transformSource != null && transformSource.Length != 0) {
  196. transform = new XslTransform ();
  197. transform.Load (MapPathSecure (transformSource));
  198. }
  199. }
  200. protected override void Render(HtmlTextWriter output)
  201. {
  202. if(document == null)
  203. {
  204. LoadXpathDoc();
  205. }
  206. LoadTransform();
  207. if(document == null && xpathDoc == null)
  208. {
  209. return;
  210. }
  211. if(transform == null)
  212. {
  213. transform = defaultTransform;
  214. }
  215. if(document != null)
  216. {
  217. Transform.Transform(document, transformArgumentList, output);
  218. return;
  219. }
  220. Transform.Transform(xpathDoc, transformArgumentList, output);
  221. }
  222. }
  223. }