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