Xml.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. using System;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.IO;
  37. using System.Xml;
  38. using System.Xml.Xsl;
  39. using System.Xml.XPath;
  40. using System.Web;
  41. using System.Web.UI;
  42. namespace System.Web.UI.WebControls
  43. {
  44. [DefaultProperty("DocumentSource")]
  45. [PersistChildren(false)]
  46. [ControlBuilder (typeof (XmlBuilder))]
  47. [Designer ("System.Web.UI.Design.WebControls.XmlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  48. public class Xml : Control
  49. {
  50. static XslTransform defaultTransform;
  51. XmlDocument document;
  52. string documentContent;
  53. string documentSource;
  54. XslTransform transform;
  55. XsltArgumentList transformArgumentList;
  56. string transformSource;
  57. XPathDocument xpathDoc;
  58. static Xml ()
  59. {
  60. XmlTextReader reader = new XmlTextReader (new StringReader("<xsl:stylesheet version='1.0' " +
  61. "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
  62. "<xsl:template match=\"*\">" +
  63. "<xsl:copy-of select=\".\"/>" +
  64. "</xsl:template>" +
  65. "</xsl:stylesheet>"));
  66. defaultTransform = new XslTransform();
  67. #if NET_1_0
  68. defaultTransform.Load (reader);
  69. #else
  70. defaultTransform.Load (reader, null, null);
  71. #endif
  72. }
  73. public Xml ()
  74. {
  75. }
  76. [MonoTODO("security")]
  77. private void LoadXmlDoc ()
  78. {
  79. if (documentContent != null && documentContent.Length > 0) {
  80. document = new XmlDocument ();
  81. document.LoadXml (documentContent);
  82. return;
  83. }
  84. if (documentSource != null && documentSource.Length != 0) {
  85. document = new XmlDocument ();
  86. document.Load (documentSource);
  87. }
  88. }
  89. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  90. [WebSysDescription ("This is the XML document that is used for the XML Webcontrol.")]
  91. public XmlDocument Document {
  92. get {
  93. if (document == null)
  94. LoadXmlDoc ();
  95. return document;
  96. }
  97. set {
  98. documentSource = null;
  99. documentContent = null;
  100. xpathDoc = null;
  101. document = value;
  102. }
  103. }
  104. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  105. [WebSysDescription ("The XML content that is transformed for the XML Webcontrol.")]
  106. public string DocumentContent {
  107. get {
  108. return String.Empty;
  109. }
  110. set {
  111. document = null;
  112. xpathDoc = null;
  113. documentContent = value;
  114. }
  115. }
  116. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  117. [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  118. [WebSysDescription ("The URL or the source of the XML content that is transformed for the XML Webcontrol.")]
  119. public string DocumentSource {
  120. get {
  121. if (documentSource != null)
  122. return documentSource;
  123. return String.Empty;
  124. }
  125. set {
  126. document = null;
  127. documentContent = null;
  128. xpathDoc = null;
  129. documentSource = value;
  130. }
  131. }
  132. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  133. [WebSysDescription ("The XSL transform that is applied to this XML Webcontrol.")]
  134. public XslTransform Transform {
  135. get {
  136. return transform;
  137. }
  138. set {
  139. transformSource = null;
  140. transform = value;
  141. }
  142. }
  143. [DefaultValue (""), Bindable (true), WebCategory ("Behavior")]
  144. [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
  145. [WebSysDescription ("An URL specifying the source that is used for the XSL transformation.")]
  146. public string TransformSource {
  147. get {
  148. if (transformSource != null)
  149. return transformSource;
  150. return String.Empty;
  151. }
  152. set {
  153. transform = null;
  154. transformSource = value;
  155. }
  156. }
  157. [Browsable (false), DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  158. [WebSysDescription ("Arguments that are used by the XSL Transform.")]
  159. public XsltArgumentList TransformArgumentList {
  160. get { return transformArgumentList; }
  161. set { transformArgumentList = value; }
  162. }
  163. protected override void AddParsedSubObject (object obj)
  164. {
  165. if (obj is LiteralControl) {
  166. DocumentContent = ((LiteralControl) obj).Text;
  167. return;
  168. }
  169. throw new HttpException (HttpRuntime.FormatResourceString (
  170. "Cannot_Have_Children_of_Type",
  171. "Xml",
  172. GetType().Name));
  173. }
  174. private void LoadXpathDoc ()
  175. {
  176. if (documentContent != null && documentContent.Length > 0) {
  177. xpathDoc = new XPathDocument (new StringReader (MapPathSecure (documentContent)));
  178. return;
  179. }
  180. if (documentSource != null && documentSource.Length != 0) {
  181. xpathDoc = new XPathDocument (MapPathSecure (documentSource));
  182. return;
  183. }
  184. }
  185. private void LoadTransform ()
  186. {
  187. if (transform != null)
  188. return;
  189. if (transformSource != null && transformSource.Length != 0) {
  190. transform = new XslTransform ();
  191. transform.Load (MapPathSecure (transformSource));
  192. }
  193. }
  194. protected override void Render(HtmlTextWriter output)
  195. {
  196. if (document == null) {
  197. LoadXpathDoc();
  198. }
  199. LoadTransform();
  200. if (document == null && xpathDoc == null) {
  201. return;
  202. }
  203. if (transform == null) {
  204. transform = defaultTransform;
  205. }
  206. if (document != null) {
  207. #if NET_1_0
  208. Transform.Transform(document, transformArgumentList, output);
  209. #else
  210. Transform.Transform(document, transformArgumentList, output, null);
  211. #endif
  212. return;
  213. }
  214. #if NET_1_0
  215. Transform.Transform(xpathDoc, transformArgumentList, output);
  216. #else
  217. Transform.Transform(xpathDoc, transformArgumentList, output, null);
  218. #endif
  219. }
  220. }
  221. }