**** Merged from MCS ****
[mono-project.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslOutput.cs
blob3e00e6a3c12774c043b855c04d45f8b8856f6a6e
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
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.
35 using System;
36 using System.Collections;
37 using System.Xml;
38 using System.Xml.Schema;
39 using System.Xml.XPath;
40 using System.Xml.Xsl;
41 using System.Text;
43 namespace Mono.Xml.Xsl
45 using QName = System.Xml.XmlQualifiedName;
47 internal enum OutputMethod {
48 XML,
49 HTML,
50 Text,
51 Custom,
52 Unknown
55 internal enum StandaloneType {
56 NONE,
57 YES,
61 internal class XslOutput // also usable for xsl:result-document
63 string uri;
64 QName customMethod;
65 OutputMethod method = OutputMethod.Unknown;
66 string version;
67 Encoding encoding = System.Text.Encoding.UTF8;
68 bool omitXmlDeclaration;
69 StandaloneType standalone = StandaloneType.NONE;
70 string doctypePublic;
71 string doctypeSystem;
72 QName [] cdataSectionElements;
73 string indent;
74 string mediaType;
75 bool escapeUriAttributes;
76 bool includeContentType;
77 bool normalizeUnicode;
78 bool undeclareNamespaces;
79 QName [] useCharacterMaps;
81 // for compilation only.
82 ArrayList cdSectsList = new ArrayList ();
84 public XslOutput (string uri)
86 this.uri = uri;
89 public OutputMethod Method { get { return method; }}
90 public QName CustomMethod { get { return customMethod; }}
92 public string Version {
93 get { return version; }
96 public Encoding Encoding {
97 get { return encoding; }
100 public string Uri {
101 get { return uri; }
104 public bool OmitXmlDeclaration {
105 get { return omitXmlDeclaration; }
108 public StandaloneType Standalone {
109 get { return standalone; }
112 public string DoctypePublic {
113 get { return doctypePublic; }
116 public string DoctypeSystem {
117 get { return doctypeSystem; }
120 public QName [] CDataSectionElements {
121 get {
122 if (cdataSectionElements == null)
123 cdataSectionElements = cdSectsList.ToArray (typeof (QName)) as QName [];
124 return cdataSectionElements;
128 public string Indent {
129 get { return indent; }
132 public string MediaType {
133 get { return mediaType; }
136 // Below are introduced in XSLT 2.0 (WD-20030502)
137 public bool EscapeUriAttributes {
138 get { return escapeUriAttributes; }
141 public bool IncludeContentType {
142 get { return includeContentType; }
145 public bool NormalizeUnicode {
146 get { return normalizeUnicode; }
149 public bool UndeclareNamespaces {
150 get { return undeclareNamespaces; }
153 public QName [] UseCharacterMaps {
154 get { return useCharacterMaps; }
157 public void Fill (XPathNavigator nav)
159 string att;
161 att = nav.GetAttribute ("cdata-section-elements", "");
162 if (att != String.Empty)
163 cdSectsList.AddRange (XslNameUtil.FromListString (att, nav));
165 att = nav.GetAttribute ("method", "");
167 if (att != String.Empty) {
168 switch (att) {
169 case "xml":
170 method = OutputMethod.XML;
171 break;
172 case "html":
173 method = OutputMethod.HTML;
174 break;
175 case "text":
176 method = OutputMethod.Text;
177 break;
178 default:
179 method = OutputMethod.Custom;
180 customMethod = XslNameUtil.FromString (att, nav);
181 if (customMethod.Namespace == String.Empty) {
182 IXmlLineInfo li = nav as IXmlLineInfo;
183 throw new XsltCompileException (new ArgumentException ("Invalid output method value: '" + att +
184 "'. It must be either 'xml' or 'html' or 'text' or QName."),
185 nav.BaseURI,
186 li != null ? li.LineNumber : 0,
187 li != null ? li.LinePosition : 0);
189 break;
193 att = nav.GetAttribute ("version", "");
194 if (att != String.Empty)
195 this.version = att;
197 att = nav.GetAttribute ("encoding", "");
198 if (att != String.Empty)
199 this.encoding = System.Text.Encoding.GetEncoding (att);
201 att = nav.GetAttribute ("standalone", "");
202 if (att != String.Empty)
203 //TODO: Should we validate values?
204 this.standalone = att == "yes" ? StandaloneType.YES : StandaloneType.NO;
207 att = nav.GetAttribute ("doctype-public", "");
208 if (att != String.Empty)
209 this.doctypePublic = att;
211 att = nav.GetAttribute ("doctype-system", "");
212 if (att != String.Empty)
213 this.doctypeSystem = att;
215 att = nav.GetAttribute ("media-type", "");
216 if (att != String.Empty)
217 this.mediaType = att;
219 att = nav.GetAttribute ("omit-xml-declaration", "");
220 if (att != String.Empty)
221 this.omitXmlDeclaration = att == "yes";
223 att = nav.GetAttribute ("indent", "");
224 if (att != String.Empty)
225 this.indent = att;