5 // Ben Maurer (bmaurer@users.sourceforge.net)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
9 // (C) 2003 Atsushi Enomoto
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Collections.Specialized;
37 using System.Xml.Schema;
38 using System.Xml.XPath;
42 using Mono.Xml.Xsl.Operations;
44 using QName = System.Xml.XmlQualifiedName;
46 namespace Mono.Xml.Xsl {
48 internal class XslStylesheet {
49 public const string XsltNamespace = "http://www.w3.org/1999/XSL/Transform";
50 public const string MSXsltNamespace = "urn:schemas-microsoft-com:xslt";
54 XslStylesheet importer;
56 ArrayList imports = new ArrayList ();
58 Hashtable spaceControls = new Hashtable ();
59 // [string stylesheet-prefix]=>string result-prefix
60 NameValueCollection namespaceAliases = new NameValueCollection ();
62 Hashtable parameters = new Hashtable ();
64 Hashtable keys = new Hashtable();
66 XslTemplateTable templates;
68 // stylesheet attributes
70 XmlQualifiedName [] extensionElementPrefixes;
71 XmlQualifiedName [] excludeResultPrefixes;
72 ArrayList stylesheetNamespaces = new ArrayList ();
74 // below are newly introduced in XSLT 2.0
76 // xsl:import-schema should be interpreted into it.
77 XmlSchemaCollection schemas = new XmlSchemaCollection ();
78 // [QName]=>XslCharacterMap
79 Hashtable characterMap = new Hashtable ();
80 // [QName]=>XslDateFormat
81 Hashtable dateFormats = new Hashtable ();
82 // [QName]=>XslFunction
83 Hashtable functions = new Hashtable ();
84 // [QName]=>XslSortKey
85 Hashtable sortKeys = new Hashtable ();
87 public string BaseUri {
88 get { return c.Input.BaseURI; }
91 public XmlQualifiedName [] ExtensionElementPrefixes {
92 get { return extensionElementPrefixes; }
95 public XmlQualifiedName [] ExcludeResultPrefixes {
96 get { return excludeResultPrefixes; }
99 public ArrayList StylesheetNamespaces {
100 get { return stylesheetNamespaces; }
103 public ArrayList Imports {
104 get { return imports; }
107 public Hashtable SpaceControls {
108 get { return spaceControls; }
111 public NameValueCollection NamespaceAliases {
112 get { return namespaceAliases; }
115 public Hashtable Parameters {
116 get { return parameters; }
119 public XPathNavigator StyleDocument {
120 get { return c.Input; }
123 public XslTemplateTable Templates {
124 get { return templates; }
127 public Hashtable Keys {
131 public string Version {
132 get { return version; }
135 public XslStylesheet (Compiler c)
138 c.PushStylesheet (this);
140 templates = new XslTemplateTable (this);
142 // move to root element
143 while (c.Input.NodeType != XPathNodeType.Element)
144 if (!c.Input.MoveToNext ())
145 throw new XsltCompileException ("Stylesheet root element must be either \"stylesheet\" or \"transform\" or any literal element.", null, c.Input);
147 if (c.Input.NamespaceURI != XsltNamespace) {
148 if (c.Input.GetAttribute ("version", XsltNamespace) == null)
149 throw new XsltCompileException ("Mandatory global attribute version is missing.", null, c.Input);
150 // then it is simplified stylesheet.
151 Templates.Add (new XslTemplate (c));
153 if (c.Input.LocalName != "stylesheet" &&
154 c.Input.LocalName != "transform")
155 throw new XsltCompileException ("Stylesheet root element must be either \"stylesheet\" or \"transform\" or any literal element.", null, c.Input);
157 version = c.Input.GetAttribute ("version", "");
159 throw new XsltCompileException ("Mandatory attribute version is missing.", null, c.Input);
161 extensionElementPrefixes = c.ParseQNameListAttribute ("extension-element-prefixes");
162 excludeResultPrefixes = c.ParseQNameListAttribute ("exclude-result-prefixes");
163 if (c.Input.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
165 if (c.Input.Value == XsltNamespace)
167 this.stylesheetNamespaces.Insert (0, new QName (c.Input.Name, c.Input.Value));
168 } while (c.Input.MoveToNextNamespace (XPathNamespaceScope.Local));
169 c.Input.MoveToParent ();
171 ProcessTopLevelElements ();
177 public XslKey FindKey (QName name)
179 XslKey key = Keys [name] as XslKey;
182 for (int i = Imports.Count - 1; i >= 0; i--) {
183 key = ((XslStylesheet) Imports [i]).FindKey (name);
190 bool countedSpaceControlExistence;
191 bool cachedHasSpaceControls;
192 public bool HasSpaceControls {
194 if (!countedSpaceControlExistence) {
195 countedSpaceControlExistence = true;
196 if (this.spaceControls.Count > 0)
197 cachedHasSpaceControls = true;
198 else if (imports.Count == 0)
199 cachedHasSpaceControls = false;
201 for (int i = 0; i < imports.Count; i++)
202 if (((XslStylesheet) imports [i]).spaceControls.Count > 0)
203 countedSpaceControlExistence = true;
204 cachedHasSpaceControls = false;
207 return cachedHasSpaceControls;
211 public bool GetPreserveWhitespace (string localName, string ns)
213 if (!HasSpaceControls)
216 XmlQualifiedName qname = new XmlQualifiedName (localName, ns);
217 object o = spaceControls [qname];
220 for (int i = 0; i < imports.Count; i++) {
221 o = ((XslStylesheet) imports [i]).SpaceControls [qname];
228 qname = new XmlQualifiedName ("*", ns);
229 o = spaceControls [qname];
231 for (int i = 0; i < imports.Count; i++) {
232 o = ((XslStylesheet) imports [i]).SpaceControls [qname];
240 qname = new XmlQualifiedName ("*", String.Empty);
241 o = spaceControls [qname];
243 for (int i = 0; i < imports.Count; i++) {
244 o = ((XslStylesheet) imports [i]).SpaceControls [qname];
252 XmlSpace space = (XmlSpace) o;
253 switch ((XmlSpace) o) {
254 case XmlSpace.Preserve:
256 case XmlSpace.Default:
263 bool countedNamespaceAliases;
264 bool cachedHasNamespaceAliases;
265 public bool HasNamespaceAliases {
267 if (!countedNamespaceAliases) {
268 countedNamespaceAliases = true;
269 if (namespaceAliases.Count > 0)
270 cachedHasNamespaceAliases = true;
271 else if (imports.Count == 0)
272 cachedHasNamespaceAliases = false;
274 for (int i = 0; i < imports.Count; i++)
275 if (((XslStylesheet) imports [i]).namespaceAliases.Count > 0)
276 countedNamespaceAliases = true;
277 cachedHasNamespaceAliases = false;
280 return cachedHasNamespaceAliases;
284 public string GetActualPrefix (string prefix)
286 if (!HasNamespaceAliases)
289 string result = namespaceAliases [prefix];
290 if (result == null) {
291 for (int i = 0; i < imports.Count; i++) {
292 result = ((XslStylesheet) imports [i]).namespaceAliases [prefix];
298 return result != null ? result : prefix;
301 private XslStylesheet (Compiler c, XslStylesheet importer) : this (c)
303 this.importer = importer;
306 private void HandleInclude (string href)
308 c.PushInputDocument (href);
309 ProcessTopLevelElements ();
310 c.PopInputDocument ();
313 private void HandleImport (string href)
315 c.PushInputDocument (href);
316 imports.Add (new XslStylesheet (c, this));
317 c.PopInputDocument ();
320 private void HandleTopLevelElement ()
322 XPathNavigator n = c.Input;
323 switch (n.NamespaceURI)
330 HandleInclude (c.GetAttribute ("href"));
333 HandleImport (c.GetAttribute ("href"));
335 case "preserve-space":
336 AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Preserve, n);
340 AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Default, n);
343 case "namespace-alias":
344 namespaceAliases.Add ((string) c.GetAttribute ("stylesheet-prefix", ""), (string) c.GetAttribute ("result-prefix", ""));
347 case "attribute-set":
348 c.AddAttributeSet (new XslAttributeSet (c));
352 keys.Add (c.ParseQNameAttribute ("name"), new XslKey (c));
359 case "decimal-format":
360 c.CompileDecimalFormat ();
364 templates.Add (new XslTemplate (c));
367 c.AddGlobalVariable (new XslGlobalVariable (c));
370 c.AddGlobalVariable (new XslGlobalParam (c));
373 if (version == "1.0")
374 throw new XsltCompileException ("Unrecognized top level element.", null, c.Input);
378 case MSXsltNamespace:
382 c.ScriptManager.AddScript (c);
389 private void ProcessTopLevelElements ()
391 if (c.Input.MoveToFirstChild ()) {
393 if (c.Input.NodeType == XPathNodeType.Element) {
394 Debug.EnterNavigator (c);
395 this.HandleTopLevelElement();
396 Debug.ExitNavigator (c);
398 } while (c.Input.MoveToNext ());
400 c.Input.MoveToParent ();
404 private void AddSpaceControls (QName [] names, XmlSpace result, XPathNavigator styleElem)
406 // XSLT 3.4 - This implementation recovers from errors.
407 foreach (QName name in names)
408 spaceControls [name] = result;
411 public string PrefixInEffect (string prefix, ArrayList additionalExcluded)
413 if (additionalExcluded != null && additionalExcluded.Contains (prefix == String.Empty ? "#default" : prefix))
415 if (prefix == "#default")
416 prefix = String.Empty;
418 if (ExcludeResultPrefixes != null) {
419 bool exclude = false;
420 foreach (XmlQualifiedName exc in ExcludeResultPrefixes)
421 if (exc.Name == "#default" && prefix == String.Empty || exc.Name == prefix) {
429 if (ExtensionElementPrefixes != null) {
430 bool exclude = false;
431 foreach (XmlQualifiedName exc in ExtensionElementPrefixes)
432 if (exc.Name == "#default" && prefix == String.Empty || exc.Name == prefix) {
440 return GetActualPrefix (prefix);
445 internal enum XslDefaultValidation