2003-09-20 Ben Maurer <bmaurer@users.sourceforge.net>
[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.UTF8;
48                 bool omitXmlDeclaration;
49                 StandaloneType standalone = StandaloneType.NONE;
50                 string doctypePublic;
51                 string doctypeSystem;
52                 QName [] cdataSectionElements;
53                 bool 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 bool 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                         // cdata-section-elements
142                 // FILL IN
143                 //      att = nav.GetAttribute ("cdata-section-elements", "");
144                 //      if (att != null)
145                 //              cdSectsList.AddRange (XslNameUtil.ParseQNames (att, nav));
146
147                         att = nav.GetAttribute ("method", "");
148
149                         if (att != String.Empty) {
150                                 switch (att) {
151                                         case "xml":
152                                                 method = OutputMethod.XML;
153                                                 break;
154                                         case "html":
155                                                 method = OutputMethod.HTML;
156                                                 break;
157                                         case "text":
158                                                 method = OutputMethod.Text;
159                                                 break;
160                                         default:
161                                                 method = OutputMethod.Custom;
162                                                 customMethod = XslNameUtil.FromString (att, nav);
163                                                 if (customMethod.Namespace == String.Empty)
164                                                         //TODO: how to get current line number and position?
165                                                         throw new XsltCompileException(new ArgumentException("Invalid output method value: '" + att + 
166                                                         "'. It must be either 'xml' or 'html' or 'text' or QName."), nav.BaseURI, 1, 1);
167                                                 break;
168                                 }
169                         }
170
171                         att = nav.GetAttribute ("version", "");
172                         if (att != String.Empty)
173                                 this.version = att;
174
175                         att = nav.GetAttribute ("encoding", "");
176                         if (att != String.Empty)
177                                 this.encoding = System.Text.Encoding.GetEncoding (att);
178
179                         att = nav.GetAttribute ("standalone", "");
180                         if (att != String.Empty)
181                                 //TODO: Should we validate values?                
182                                 this.standalone = att == "yes" ? StandaloneType.YES : StandaloneType.NO;
183
184
185                         att = nav.GetAttribute ("doctype-public", "");
186                         if (att != String.Empty)
187                                 this.doctypePublic = att;
188
189                         att = nav.GetAttribute ("doctype-system", "");
190                         if (att != String.Empty)
191                                 this.doctypeSystem = att;
192
193                         att = nav.GetAttribute ("media-type", "");
194                         if (att != String.Empty)
195                                 this.mediaType = att;
196
197                         att = nav.GetAttribute ("omit-xml-declaration", "");
198                         if (att != String.Empty)
199                                 this.omitXmlDeclaration = att == "yes";
200
201                         att = nav.GetAttribute ("indent", "");
202                         if (att != String.Empty)
203                                 this.indent = att == "yes";
204                 }
205         }
206
207 }