2010-07-23 Marek Habersack <mhabersack@novell.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 //      Miguel de Icaza (miguel@novell.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.ComponentModel;
31 using System.Security.Permissions;
32 using System.Xml;
33 using System.Xml.Xsl;
34
35 #if NET_2_0
36 using System.Xml.XPath;
37 using System.Collections;
38 #endif
39
40 namespace System.Web.UI.WebControls {
41
42         // CAS
43         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
45         // attributes
46         [DefaultProperty ("DocumentSource")]
47         [Designer ("System.Web.UI.Design.WebControls.XmlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
48 #if NET_2_0
49         [PersistChildren (true)]
50 #else
51         [PersistChildren (false)]
52 #endif
53         [ControlBuilder (typeof (XmlBuilder))] 
54         public class Xml : Control {
55                 // Property set variables
56                 XmlDocument xml_document;
57 #if NET_2_0
58                 XPathNavigator xpath_navigator;
59 #endif
60                 string xml_content;
61                 string xml_file;
62
63                 XslTransform xsl_transform;
64                 XsltArgumentList transform_arguments;
65                 string transform_file;
66
67                 public Xml ()
68                 {
69                 }
70
71 #if NET_2_0
72                 [EditorBrowsable (EditorBrowsableState.Never)]
73                 [MonoTODO ("Anything else?")]
74                 public override string ClientID
75                 {
76                         get {
77                                 return base.ClientID;
78                         }
79                 }
80
81                 [MonoTODO ("Anything else?")]
82                 [EditorBrowsable (EditorBrowsableState.Never)]
83                 public override ControlCollection Controls 
84                 {
85                         get {
86                                 return base.Controls;
87                         }
88                 }
89 #endif          
90                 
91
92                 [Browsable(false)]
93                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
94 #if NET_2_0
95 [Obsolete ("Use the XPathNavigator property instead by creating an XPathDocument and calling CreateNavigator().")]
96 #endif
97                 public XmlDocument Document {
98                         get {
99                                 return xml_document;
100                         }
101
102                         set {
103                                 xml_content = null;
104                                 xml_file = null;
105                                 xml_document = value;
106                         }
107                 }
108
109                 [Browsable(false)]
110                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
111                 public string DocumentContent {
112                         get {
113 #if NET_2_0
114                                 return (xml_content != null)? xml_content : "";
115 #else
116                                 return "";
117 #endif
118                         }
119
120                         set {
121                                 xml_content = value;
122                                 xml_file = null;
123                                 xml_document = null;
124                         }
125                 }
126
127 #if ONLY_1_1
128                 [Bindable (true)]
129 #endif          
130                 [DefaultValue ("")]
131 #if NET_2_0
132                 [UrlProperty]
133                 [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
134 #else           
135                 [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
136 #endif          
137                 [WebSysDescription ("")]
138                 [WebCategory ("Behavior")]
139                 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
140                 public string DocumentSource {
141                         get {
142                                 if (xml_file == null)
143                                         return "";
144                                 
145                                 return xml_file;
146                         }
147
148                         set {
149                                 xml_content = null;
150                                 xml_file = value;
151                                 xml_document = null;
152                         }
153                 }
154
155 #if NET_2_0
156                 [EditorBrowsable (EditorBrowsableState.Never)]
157                 [Browsable (false)]
158                 [DefaultValue (false)]
159                 public override bool EnableTheming 
160                 {
161                         get { return false; }
162                         set { throw new NotSupportedException (); }
163                 }
164
165                 [DefaultValue ("")]
166                 [EditorBrowsable (EditorBrowsableState.Never)]
167                 [Browsable (false)]
168                 public override string SkinID
169                 {
170                         // MSDN: Always returns an empty string (""). This property is not supported.
171                         get { return String.Empty; }
172                         // MSDN: Any attempt to set the value of this property throws a NotSupportedException exception.
173                         set { throw new NotSupportedException ("SkinID is not supported on Xml control"); }
174                 }
175 #endif          
176                 
177
178                 [Browsable (false)]
179                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
180                 [WebSysDescription ("")]
181                 [WebCategory ("Behavior")]
182                 public XslTransform Transform {
183                         get {
184                                 return xsl_transform;
185                         }
186
187                         set {
188                                 transform_file = null;
189                                 xsl_transform = value;
190                         }
191                 }
192
193                 [Browsable (false)]
194                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
195                 [WebSysDescription ("")]
196                 [WebCategory ("Behavior")]
197                 public XsltArgumentList TransformArgumentList {
198                         get {
199                                 return transform_arguments;
200                         }
201
202                         set {
203                                 transform_arguments = value;
204                         }
205                 }
206
207 #if ONLY_1_1
208                 [Bindable (true)]
209 #endif          
210                 [DefaultValue ("")]
211 #if NET_2_0
212                 [Editor ("System.Web.UI.Design.XslUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
213 #else
214                 [Editor ("System.Web.UI.Design.XslUrlEditor, " + Consts.AssemblySystem_Design, typeof (System.Drawing.Design.UITypeEditor))]
215 #endif          
216                 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
217                 public string TransformSource {
218                         get {
219                                 if (transform_file == null)
220                                         return "";
221                                 return transform_file;
222                         }
223
224                         set {
225                                 transform_file = value;
226                                 xsl_transform = null;
227                         }
228                 }
229
230 #if NET_2_0
231                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
232                 [Browsable (false)]
233                 public XPathNavigator XPathNavigator 
234                 {
235                         get { return xpath_navigator; }
236                         set { xpath_navigator = value; }
237                 }
238
239                 [EditorBrowsable (EditorBrowsableState.Never)]
240                 public override Control FindControl (string id) 
241                 {
242                         return null;
243                 }
244
245                 [EditorBrowsable (EditorBrowsableState.Never)]
246                 public override void Focus ()
247                 {
248                         throw new NotSupportedException ();
249                 }
250
251                 [EditorBrowsable (EditorBrowsableState.Never)]
252                 public override bool HasControls ()
253                 {
254                         return false;
255                 }
256 #endif          
257
258 #if NET_2_0
259                 protected internal
260 #else           
261                 protected
262 #endif          
263                 override void Render (HtmlTextWriter output)
264                 {
265                         XmlDocument xml_doc = null;
266
267 #if NET_2_0
268                         if (xpath_navigator == null) {
269 #endif
270                                 if (xml_document != null)
271                                         xml_doc = xml_document;
272                                 else {
273                                         if (xml_content != null) {
274                                                 xml_doc = new XmlDocument ();
275                                                 xml_doc.LoadXml (xml_content);
276                                         }
277                                         else if (xml_file != null) {
278                                                 xml_doc = new XmlDocument ();
279                                                 xml_doc.Load (MapPathSecure (xml_file));
280                                         }
281                                         else
282                                                 return;
283                                 }
284 #if NET_2_0
285                         }
286 #endif
287
288                         XslTransform t = xsl_transform;
289                         if (transform_file != null){
290                                 t = new XslTransform ();
291                                 t.Load (MapPathSecure (transform_file));
292                         }
293
294                         if (t != null){
295 #if NET_2_0
296                                 if (xpath_navigator != null) {
297                                         t.Transform(xpath_navigator, transform_arguments, output);
298                                 }
299                                 else {
300 #endif
301                                         t.Transform (xml_doc, transform_arguments, output, null);
302 #if NET_2_0
303                                 }
304 #endif
305                                 return;
306                         }
307                                 
308                         XmlTextWriter xmlwriter = new XmlTextWriter (output);
309                         xmlwriter.Formatting = Formatting.None;
310 #if NET_2_0
311                         if (xpath_navigator != null) {
312                                 xmlwriter.WriteStartDocument ();
313                                 xpath_navigator.WriteSubtree (xmlwriter);
314                         }
315                         else {
316 #endif
317                                 xml_doc.Save (xmlwriter);
318 #if NET_2_0
319                         }
320 #endif
321                 }
322
323                 protected override void AddParsedSubObject (object obj)
324                 {
325                         LiteralControl lc = obj as LiteralControl;
326                         
327                         if (lc != null){
328                                 xml_document = new XmlDocument ();
329                                 xml_document.LoadXml (lc.Text);
330                         } else {
331                                 throw new HttpException (
332                                                          String.Format ("Objects of type {0} are not supported as children of the Xml control",
333                                                                         obj.GetType ()));
334                         }
335                 }
336
337 #if NET_2_0
338                 protected override ControlCollection CreateControlCollection ()
339                 {
340                         return new EmptyControlCollection (this);
341                 }
342
343                 [MonoTODO("Always returns null")]
344                 protected override IDictionary GetDesignModeState ()
345                 {
346                         return null;
347                 }
348 #endif          
349         }
350 }