5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
7 // Oleg Tkachenko (oleg@tkachenko.com)
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:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
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.
36 using System.Collections;
38 using System.Xml.Schema;
39 using System.Xml.XPath;
43 namespace Mono.Xml.Xsl
45 using QName = System.Xml.XmlQualifiedName;
47 internal enum OutputMethod {
55 internal enum StandaloneType {
61 internal class XslOutput // also usable for xsl:result-document
65 OutputMethod method = OutputMethod.Unknown;
67 Encoding encoding = System.Text.Encoding.UTF8;
68 bool omitXmlDeclaration;
69 StandaloneType standalone = StandaloneType.NONE;
72 QName [] cdataSectionElements;
75 string stylesheetVersion;
77 // for compilation only.
78 ArrayList cdSectsList = new ArrayList ();
80 public XslOutput (string uri, string stylesheetVersion)
83 this.stylesheetVersion = stylesheetVersion;
86 public OutputMethod Method { get { return method; }}
87 public QName CustomMethod { get { return customMethod; }}
89 public string Version {
90 get { return version; }
93 public Encoding Encoding {
94 get { return encoding; }
101 public bool OmitXmlDeclaration {
102 get { return omitXmlDeclaration; }
105 public StandaloneType Standalone {
106 get { return standalone; }
109 public string DoctypePublic {
110 get { return doctypePublic; }
113 public string DoctypeSystem {
114 get { return doctypeSystem; }
117 public QName [] CDataSectionElements {
119 if (cdataSectionElements == null)
120 cdataSectionElements = cdSectsList.ToArray (typeof (QName)) as QName [];
121 return cdataSectionElements;
125 public string Indent {
126 get { return indent; }
129 public string MediaType {
130 get { return mediaType; }
133 public void Fill (XPathNavigator nav)
135 if (nav.MoveToFirstAttribute ()) {
136 ProcessAttribute (nav);
137 while (nav.MoveToNextAttribute ()) {
138 ProcessAttribute (nav);
141 // move back to original position
146 private void ProcessAttribute (XPathNavigator nav)
148 // skip attributes from non-default namespace
149 if (nav.NamespaceURI != string.Empty) {
153 string value = nav.Value;
155 switch (nav.LocalName) {
156 case "cdata-section-elements":
157 if (value.Length > 0) {
158 cdSectsList.AddRange (XslNameUtil.FromListString (value, nav));
162 if (value.Length == 0) {
168 method = OutputMethod.XML;
171 omitXmlDeclaration = true;
172 method = OutputMethod.HTML;
175 omitXmlDeclaration = true;
176 method = OutputMethod.Text;
179 method = OutputMethod.Custom;
180 customMethod = XslNameUtil.FromString (value, nav);
181 if (customMethod.Namespace == String.Empty) {
182 IXmlLineInfo li = nav as IXmlLineInfo;
183 throw new XsltCompileException (new ArgumentException (
184 "Invalid output method value: '" + value + "'. It" +
185 " must be either 'xml' or 'html' or 'text' or QName."),
187 li != null ? li.LineNumber : 0,
188 li != null ? li.LinePosition : 0);
194 if (value.Length > 0) {
195 this.version = value;
199 if (value.Length > 0) {
201 this.encoding = System.Text.Encoding.GetEncoding (value);
202 } catch (ArgumentException) {
203 // MS.NET just leaves the default encoding when encoding is unknown
204 } catch (NotSupportedException) {
205 // Workaround for a bug in System.Text, it throws invalid exception
212 this.standalone = StandaloneType.YES;
215 this.standalone = StandaloneType.NO;
218 if (stylesheetVersion != "1.0")
221 IXmlLineInfo li = nav as IXmlLineInfo;
222 throw new XsltCompileException (new XsltException (
223 "'" + value + "' is an invalid value for 'standalone'" +
224 " attribute.", (Exception) null),
226 li != null ? li.LineNumber : 0,
227 li != null ? li.LinePosition : 0);
230 case "doctype-public":
231 this.doctypePublic = value;
233 case "doctype-system":
234 this.doctypeSystem = value;
237 if (value.Length > 0) {
238 this.mediaType = value;
241 case "omit-xml-declaration":
244 this.omitXmlDeclaration = true;
247 this.omitXmlDeclaration = false;
250 if (stylesheetVersion != "1.0")
253 IXmlLineInfo li = nav as IXmlLineInfo;
254 throw new XsltCompileException (new XsltException (
255 "'" + value + "' is an invalid value for 'omit-xml-declaration'" +
256 " attribute.", (Exception) null),
258 li != null ? li.LineNumber : 0,
259 li != null ? li.LinePosition : 0);
264 if (stylesheetVersion != "1.0")
272 case OutputMethod.Custom:
275 IXmlLineInfo li = nav as IXmlLineInfo;
276 throw new XsltCompileException (String.Format ("Unexpected 'indent' attribute value in 'output' element: '{0}'", value), null, nav);
282 if (stylesheetVersion != "1.0")
285 IXmlLineInfo li = nav as IXmlLineInfo;
286 throw new XsltCompileException (new XsltException (
287 "'" + nav.LocalName + "' is an invalid attribute for 'output'" +
288 " element.", (Exception) null),
290 li != null ? li.LineNumber : 0,
291 li != null ? li.LinePosition : 0);