Xml.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: Xml
  4. *
  5. * Author: Gaurav Vaish, Gonzalo Paniagua Javier
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 95%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. * (c) 2002 Ximian, Inc. (http://www.ximian.com)
  13. */
  14. using System;
  15. using System.ComponentModel;
  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. public class Xml : Control
  27. {
  28. private XmlDocument document;
  29. private string documentContent;
  30. private string documentSource;
  31. private XslTransform transform;
  32. private XsltArgumentList transformArgumentList;
  33. private string transformSource;
  34. private XPathDocument xpathDoc;
  35. private static XslTransform defaultTransform;
  36. static Xml()
  37. {
  38. XmlTextReader reader = new XmlTextReader(new StringReader("<xsl:stylesheet version='1.0' " +
  39. "xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
  40. "<xsl:template match=\"\">" +
  41. "<xsl:copy-of select=\".\"/>" +
  42. "</xsl:template>" +
  43. "</xsl:stylesheet>"));
  44. defaultTransform = new XslTransform();
  45. defaultTransform.Load(reader);
  46. }
  47. public Xml(): base()
  48. {
  49. }
  50. [MonoTODO("security")]
  51. private void LoadXmlDoc ()
  52. {
  53. if (documentContent != null && documentContent.Length > 0) {
  54. document = new XmlDocument();
  55. document.LoadXml (documentContent);
  56. return;
  57. }
  58. if (documentSource != null && documentSource.Length != 0) {
  59. document = new XmlDocument();
  60. document.Load (documentSource);
  61. }
  62. }
  63. public XmlDocument Document
  64. {
  65. get
  66. {
  67. if(document == null)
  68. LoadXmlDoc();
  69. return document;
  70. }
  71. set
  72. {
  73. documentSource = null;
  74. documentContent = null;
  75. xpathDoc = null;
  76. document = value;
  77. }
  78. }
  79. public string DocumentContent
  80. {
  81. get
  82. {
  83. return String.Empty;
  84. }
  85. set
  86. {
  87. document = null;
  88. xpathDoc = null;
  89. documentContent = value;
  90. }
  91. }
  92. public string DocumentSource
  93. {
  94. get
  95. {
  96. if(documentSource != null)
  97. return documentSource;
  98. return String.Empty;
  99. }
  100. set
  101. {
  102. document = null;
  103. documentContent = null;
  104. xpathDoc = null;
  105. documentSource = value;
  106. }
  107. }
  108. public XslTransform Transform
  109. {
  110. get
  111. {
  112. return transform;
  113. }
  114. set
  115. {
  116. transformSource = null;
  117. transform = value;
  118. }
  119. }
  120. public string TransformSource
  121. {
  122. get
  123. {
  124. if(transformSource != null)
  125. return transformSource;
  126. return String.Empty;
  127. }
  128. set
  129. {
  130. transform = null;
  131. transformSource = value;
  132. }
  133. }
  134. public XsltArgumentList TransformArgumentList
  135. {
  136. get
  137. {
  138. return transformArgumentList;
  139. }
  140. set
  141. {
  142. transformArgumentList = value;
  143. }
  144. }
  145. protected override void AddParsedSubObject(object obj)
  146. {
  147. if(obj is LiteralControl)
  148. {
  149. DocumentContent = ((LiteralControl)obj).Text;
  150. return;
  151. }
  152. throw new HttpException (HttpRuntime.FormatResourceString (
  153. "Cannot_Have_Children_of_Type",
  154. "Xml",
  155. GetType().Name));
  156. }
  157. [MonoTODO("security")]
  158. private void LoadXpathDoc ()
  159. {
  160. if(documentContent != null && documentContent.Length > 0) {
  161. xpathDoc = new XPathDocument (new StringReader (documentContent));
  162. return;
  163. }
  164. if (documentSource != null && documentSource.Length != 0) {
  165. xpathDoc = new XPathDocument (MapPathSecure (documentSource));
  166. return;
  167. }
  168. }
  169. [MonoTODO("security")]
  170. private void LoadTransform ()
  171. {
  172. if (transform != null)
  173. return;
  174. if (transformSource != null && transformSource.Length != 0) {
  175. transform = new XslTransform ();
  176. transform.Load (transformSource);
  177. }
  178. }
  179. protected override void Render(HtmlTextWriter output)
  180. {
  181. if(document == null)
  182. {
  183. LoadXpathDoc();
  184. }
  185. LoadTransform();
  186. if(document == null || xpathDoc == null)
  187. {
  188. return;
  189. }
  190. if(transform == null)
  191. {
  192. transform = defaultTransform;
  193. }
  194. if(document != null)
  195. {
  196. Transform.Transform(document, transformArgumentList, output);
  197. return;
  198. }
  199. Transform.Transform(xpathDoc, transformArgumentList, output);
  200. }
  201. }
  202. }