Merge pull request #1506 from akoeplinger/fix-paste-test
[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 using System.Xml.XPath;
36 using System.Collections;
37
38 namespace System.Web.UI.WebControls {
39
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         // attributes
44         [DefaultProperty ("DocumentSource")]
45         [Designer ("System.Web.UI.Design.WebControls.XmlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
46         [PersistChildren (true)]
47         [ControlBuilder (typeof (XmlBuilder))] 
48         public class Xml : Control {
49                 // Property set variables
50                 XmlDocument xml_document;
51                 XPathNavigator xpath_navigator;
52                 string xml_content;
53                 string xml_file;
54
55                 XslTransform xsl_transform;
56                 XsltArgumentList transform_arguments;
57                 string transform_file;
58
59                 public Xml ()
60                 {
61                 }
62
63                 [EditorBrowsable (EditorBrowsableState.Never)]
64                 [MonoTODO ("Anything else?")]
65                 public override string ClientID
66                 {
67                         get {
68                                 return base.ClientID;
69                         }
70                 }
71
72                 [MonoTODO ("Anything else?")]
73                 [EditorBrowsable (EditorBrowsableState.Never)]
74                 public override ControlCollection Controls 
75                 {
76                         get {
77                                 return base.Controls;
78                         }
79                 }
80                 
81
82                 [Browsable(false)]
83                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
84 [Obsolete ("Use the XPathNavigator property instead by creating an XPathDocument and calling CreateNavigator().")]
85                 public XmlDocument Document {
86                         get {
87                                 return xml_document;
88                         }
89
90                         set {
91                                 xml_content = null;
92                                 xml_file = null;
93                                 xml_document = value;
94                         }
95                 }
96
97                 [Browsable(false)]
98                 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
99                 public string DocumentContent {
100                         get {
101                                 return (xml_content != null)? xml_content : "";
102                         }
103
104                         set {
105                                 xml_content = value;
106                                 xml_file = null;
107                                 xml_document = null;
108                         }
109                 }
110
111 #if ONLY_1_1
112                 [Bindable (true)]
113 #endif          
114                 [DefaultValue ("")]
115                 [UrlProperty]
116                 [Editor ("System.Web.UI.Design.XmlUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
117                 [WebSysDescription ("")]
118                 [WebCategory ("Behavior")]
119                 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
120                 public string DocumentSource {
121                         get {
122                                 if (xml_file == null)
123                                         return "";
124                                 
125                                 return xml_file;
126                         }
127
128                         set {
129                                 xml_content = null;
130                                 xml_file = value;
131                                 xml_document = null;
132                         }
133                 }
134
135                 [EditorBrowsable (EditorBrowsableState.Never)]
136                 [Browsable (false)]
137                 [DefaultValue (false)]
138                 public override bool EnableTheming 
139                 {
140                         get { return false; }
141                         set { throw new NotSupportedException (); }
142                 }
143
144                 [DefaultValue ("")]
145                 [EditorBrowsable (EditorBrowsableState.Never)]
146                 [Browsable (false)]
147                 public override string SkinID
148                 {
149                         // MSDN: Always returns an empty string (""). This property is not supported.
150                         get { return String.Empty; }
151                         // MSDN: Any attempt to set the value of this property throws a NotSupportedException exception.
152                         set { throw new NotSupportedException ("SkinID is not supported on Xml control"); }
153                 }
154                 
155
156                 [Browsable (false)]
157                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158                 [WebSysDescription ("")]
159                 [WebCategory ("Behavior")]
160                 public XslTransform Transform {
161                         get {
162                                 return xsl_transform;
163                         }
164
165                         set {
166                                 transform_file = null;
167                                 xsl_transform = value;
168                         }
169                 }
170
171                 [Browsable (false)]
172                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
173                 [WebSysDescription ("")]
174                 [WebCategory ("Behavior")]
175                 public XsltArgumentList TransformArgumentList {
176                         get {
177                                 return transform_arguments;
178                         }
179
180                         set {
181                                 transform_arguments = value;
182                         }
183                 }
184
185 #if ONLY_1_1
186                 [Bindable (true)]
187 #endif          
188                 [DefaultValue ("")]
189                 [Editor ("System.Web.UI.Design.XslUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
190                 [MonoLimitation ("Absolute path to the file system is not supported; use a relative URI instead.")]
191                 public string TransformSource {
192                         get {
193                                 if (transform_file == null)
194                                         return "";
195                                 return transform_file;
196                         }
197
198                         set {
199                                 transform_file = value;
200                                 xsl_transform = null;
201                         }
202                 }
203
204                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
205                 [Browsable (false)]
206                 public XPathNavigator XPathNavigator 
207                 {
208                         get { return xpath_navigator; }
209                         set { xpath_navigator = value; }
210                 }
211
212                 [EditorBrowsable (EditorBrowsableState.Never)]
213                 public override Control FindControl (string id) 
214                 {
215                         return null;
216                 }
217
218                 [EditorBrowsable (EditorBrowsableState.Never)]
219                 public override void Focus ()
220                 {
221                         throw new NotSupportedException ();
222                 }
223
224                 [EditorBrowsable (EditorBrowsableState.Never)]
225                 public override bool HasControls ()
226                 {
227                         return false;
228                 }
229
230                 protected internal
231                 override void Render (HtmlTextWriter output)
232                 {
233                         XmlDocument xml_doc = null;
234
235                         if (xpath_navigator == null) {
236                                 if (xml_document != null)
237                                         xml_doc = xml_document;
238                                 else {
239                                         if (xml_content != null) {
240                                                 xml_doc = new XmlDocument ();
241                                                 xml_doc.LoadXml (xml_content);
242                                         }
243                                         else if (xml_file != null) {
244                                                 xml_doc = new XmlDocument ();
245                                                 xml_doc.Load (MapPathSecure (xml_file));
246                                         }
247                                         else
248                                                 return;
249                                 }
250                         }
251
252                         XslTransform t = xsl_transform;
253                         if (transform_file != null){
254                                 t = new XslTransform ();
255                                 t.Load (MapPathSecure (transform_file));
256                         }
257
258                         if (t != null){
259                                 if (xpath_navigator != null) {
260                                         t.Transform(xpath_navigator, transform_arguments, output);
261                                 }
262                                 else {
263                                         t.Transform (xml_doc, transform_arguments, output, null);
264                                 }
265                                 return;
266                         }
267                                 
268                         XmlTextWriter xmlwriter = new XmlTextWriter (output);
269                         xmlwriter.Formatting = Formatting.None;
270                         if (xpath_navigator != null) {
271                                 xmlwriter.WriteStartDocument ();
272                                 xpath_navigator.WriteSubtree (xmlwriter);
273                         }
274                         else {
275                                 xml_doc.Save (xmlwriter);
276                         }
277                 }
278
279                 protected override void AddParsedSubObject (object obj)
280                 {
281                         LiteralControl lc = obj as LiteralControl;
282                         
283                         if (lc != null){
284                                 xml_document = new XmlDocument ();
285                                 xml_document.LoadXml (lc.Text);
286                         } else {
287                                 throw new HttpException (
288                                                          String.Format ("Objects of type {0} are not supported as children of the Xml control",
289                                                                         obj.GetType ()));
290                         }
291                 }
292
293                 protected override ControlCollection CreateControlCollection ()
294                 {
295                         return new EmptyControlCollection (this);
296                 }
297
298                 [MonoTODO("Always returns null")]
299                 protected override IDictionary GetDesignModeState ()
300                 {
301                         return null;
302                 }
303         }
304 }