2004-01-14 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslOutput.cs
1 //
2 // XslOutput.cs
3 //
4 // Authors:
5 //      Ben Maurer (bmaurer@users.sourceforge.net)
6 //      Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 //      Oleg Tkachenko (oleg@tkachenko.com)
8 //      
9 // (C) 2003 Ben Maurer
10 // (C) 2003 Atsushi Enomoto
11 // (C) 2003 Oleg Tkachenko
12 //
13
14 using System;
15 using System.CodeDom;
16 using System.Collections;
17 using System.Xml;
18 using System.Xml.Schema;
19 using System.Xml.XPath;
20 using System.Xml.Xsl;
21 using System.Text;
22
23 namespace Mono.Xml.Xsl
24 {
25         using QName = System.Xml.XmlQualifiedName;
26
27         public enum OutputMethod {
28                 XML,
29                 HTML,
30                 Text,
31                 Custom,
32                 Unknown
33         }
34         
35         public enum StandaloneType {
36                 NONE,
37                 YES,
38                 NO
39         }
40         
41         public class XslOutput  // also usable for xsl:result-document
42         {
43                 string uri;
44                 QName customMethod;
45                 OutputMethod method = OutputMethod.Unknown; 
46                 string version;
47                 Encoding encoding = System.Text.Encoding.Unicode;
48                 bool omitXmlDeclaration;
49                 StandaloneType standalone = StandaloneType.NONE;
50                 string doctypePublic;
51                 string doctypeSystem;
52                 QName [] cdataSectionElements;
53                 string indent;
54                 string mediaType;
55                 bool escapeUriAttributes;
56                 bool includeContentType;
57                 bool normalizeUnicode;
58                 bool undeclareNamespaces;
59                 QName [] useCharacterMaps;
60
61                 // for compilation only.
62                 ArrayList cdSectsList = new ArrayList ();
63
64                 public XslOutput (string uri)
65                 {
66                         this.uri = uri;
67                 }
68
69                 public OutputMethod Method { get { return method; }}
70                 public QName CustomMethod { get { return customMethod; }}
71
72                 public string Version {
73                         get { return version; }
74                 }
75
76                 public Encoding Encoding {
77                         get { return encoding; }
78                 }
79
80                 public string Uri {
81                         get { return uri; }
82                 }
83
84                 public bool OmitXmlDeclaration {
85                         get { return omitXmlDeclaration; }
86                 }
87
88                 public StandaloneType Standalone {
89                         get { return standalone; }
90                 }
91
92                 public string DoctypePublic {
93                         get { return doctypePublic; }
94                 }
95
96                 public string DoctypeSystem {
97                         get { return doctypeSystem; }
98                 }
99
100                 public QName [] CDataSectionElements {
101                         get {
102                                 if (cdataSectionElements == null)
103                                         cdataSectionElements = cdSectsList.ToArray (typeof (QName)) as QName [];
104                                 return cdataSectionElements;
105                         }
106                 }
107
108                 public string Indent {
109                         get { return indent; }
110                 }
111
112                 public string MediaType {
113                         get { return mediaType; }
114                 }
115
116                 // Below are introduced in XSLT 2.0 (WD-20030502)
117                 public bool EscapeUriAttributes {
118                         get { return escapeUriAttributes; }
119                 }
120
121                 public bool IncludeContentType {
122                         get { return includeContentType; }
123                 }
124
125                 public bool NormalizeUnicode {
126                         get { return normalizeUnicode; }
127                 }
128
129                 public bool UndeclareNamespaces {
130                         get { return undeclareNamespaces; }
131                 }
132
133                 public QName [] UseCharacterMaps {
134                         get { return useCharacterMaps; }
135                 }
136
137                 public void Fill (XPathNavigator nav)
138                 {
139                         string att;
140                         
141                         att = nav.GetAttribute ("cdata-section-elements", "");
142                         if (att != String.Empty)
143                                 cdSectsList.AddRange (XslNameUtil.FromListString (att, nav));
144
145                         att = nav.GetAttribute ("method", "");
146
147                         if (att != String.Empty) {
148                                 switch (att) {
149                                         case "xml":
150                                                 method = OutputMethod.XML;
151                                                 break;
152                                         case "html":
153                                                 method = OutputMethod.HTML;
154                                                 break;
155                                         case "text":
156                                                 method = OutputMethod.Text;
157                                                 break;
158                                         default:
159                                                 method = OutputMethod.Custom;
160                                                 customMethod = XslNameUtil.FromString (att, nav);
161                                                 if (customMethod.Namespace == String.Empty)
162                                                         //TODO: how to get current line number and position?
163                                                         throw new XsltCompileException(new ArgumentException("Invalid output method value: '" + att + 
164                                                         "'. It must be either 'xml' or 'html' or 'text' or QName."), nav.BaseURI, 1, 1);
165                                                 break;
166                                 }
167                         }
168
169                         att = nav.GetAttribute ("version", "");
170                         if (att != String.Empty)
171                                 this.version = att;
172
173                         att = nav.GetAttribute ("encoding", "");
174                         if (att != String.Empty)
175                                 this.encoding = System.Text.Encoding.GetEncoding (att);
176
177                         att = nav.GetAttribute ("standalone", "");
178                         if (att != String.Empty)
179                                 //TODO: Should we validate values?                
180                                 this.standalone = att == "yes" ? StandaloneType.YES : StandaloneType.NO;
181
182
183                         att = nav.GetAttribute ("doctype-public", "");
184                         if (att != String.Empty)
185                                 this.doctypePublic = att;
186
187                         att = nav.GetAttribute ("doctype-system", "");
188                         if (att != String.Empty)
189                                 this.doctypeSystem = att;
190
191                         att = nav.GetAttribute ("media-type", "");
192                         if (att != String.Empty)
193                                 this.mediaType = att;
194
195                         att = nav.GetAttribute ("omit-xml-declaration", "");
196                         if (att != String.Empty)
197                                 this.omitXmlDeclaration = att == "yes";
198
199                         att = nav.GetAttribute ("indent", "");
200                         if (att != String.Empty)
201                                 this.indent = att;
202                 }
203         }
204
205 }