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