2005-03-09 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslStylesheet.cs
index cfe31ad07a4a313e3ddb5e68bdfb172ca85c1b0a..0beef2e5499dde1b2a6c1b1158e94fea00a0b6f8 100644 (file)
@@ -9,8 +9,28 @@
 // (C) 2003 Atsushi Enomoto
 //
 
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
 using System;
-using System.CodeDom;
 using System.Collections;
 using System.Collections.Specialized;
 using System.Xml;
@@ -25,13 +45,10 @@ using QName = System.Xml.XmlQualifiedName;
 
 namespace Mono.Xml.Xsl {
 
-       public class XslStylesheet {
+       internal class XslStylesheet {
                public const string XsltNamespace = "http://www.w3.org/1999/XSL/Transform";
                public const string MSXsltNamespace = "urn:schemas-microsoft-com:xslt";
-               
-               Compiler c;
 
-               XslStylesheet importer;
                // Top-level elements
                ArrayList imports = new ArrayList ();
                // [QName]=>XmlSpace
@@ -51,26 +68,6 @@ namespace Mono.Xml.Xsl {
                XmlQualifiedName [] excludeResultPrefixes;
                ArrayList stylesheetNamespaces = new ArrayList ();
 
-               // below are newly introduced in XSLT 2.0
-               //  elements::
-               // xsl:import-schema should be interpreted into it.
-               XmlSchemaCollection schemas = new XmlSchemaCollection ();
-               // [QName]=>XslCharacterMap
-               Hashtable characterMap = new Hashtable ();
-               // [QName]=>XslDateFormat
-               Hashtable dateFormats = new Hashtable ();
-               // [QName]=>XslFunction
-               Hashtable functions = new Hashtable ();
-               // [QName]=>XslSortKey
-               Hashtable sortKeys = new Hashtable ();
-               //  attributes::
-               string xpathDefaultNamespace = "";
-               XslDefaultValidation defaultValidation = XslDefaultValidation.Lax;
-
-               public string BaseUri {
-                       get { return c.Input.BaseURI; }
-               }
-
                public XmlQualifiedName [] ExtensionElementPrefixes {
                        get { return extensionElementPrefixes; }
                }
@@ -99,10 +96,6 @@ namespace Mono.Xml.Xsl {
                        get { return parameters; }
                }
 
-               public XPathNavigator StyleDocument {
-                       get { return c.Input; }
-               }
-
                public XslTemplateTable Templates {
                        get { return templates; }
                }
@@ -117,17 +110,31 @@ namespace Mono.Xml.Xsl {
 
                public XslStylesheet (Compiler c)
                {
-                       this.c = c;
                        c.PushStylesheet (this);
                        
                        templates = new XslTemplateTable (this);
+
+                       // move to root element
+                       while (c.Input.NodeType != XPathNodeType.Element)
+                               if (!c.Input.MoveToNext ())
+                                       throw new XsltCompileException ("Stylesheet root element must be either \"stylesheet\" or \"transform\" or any literal element.", null, c.Input);
+
                        if (c.Input.NamespaceURI != XsltNamespace) {
+                               if (c.Input.GetAttribute ("version", XsltNamespace) == String.Empty)
+                                       throw new XsltCompileException ("Mandatory global attribute version is missing.", null, c.Input);
                                // then it is simplified stylesheet.
                                Templates.Add (new XslTemplate (c));
                        } else {
+                               if (c.Input.LocalName != "stylesheet" &&
+                                       c.Input.LocalName != "transform")
+                                       throw new XsltCompileException ("Stylesheet root element must be either \"stylesheet\" or \"transform\" or any literal element.", null, c.Input);
+
                                version = c.Input.GetAttribute ("version", "");
-                               extensionElementPrefixes = c.ParseQNameListAttribute ("extension-element-prefixes");
-                               excludeResultPrefixes = c.ParseQNameListAttribute ("exclude-result-prefixes");
+                               if (version == String.Empty)
+                                       throw new XsltCompileException ("Mandatory attribute version is missing.", null, c.Input);
+
+                               extensionElementPrefixes = ParseMappedPrefixes (c.GetAttribute ("extension-element-prefixes"), c.Input);
+                               excludeResultPrefixes = ParseMappedPrefixes (c.GetAttribute ("exclude-result-prefixes"), c.Input);
                                if (c.Input.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
                                        do {
                                                if (c.Input.Value == XsltNamespace)
@@ -136,12 +143,31 @@ namespace Mono.Xml.Xsl {
                                        } while (c.Input.MoveToNextNamespace (XPathNamespaceScope.Local));
                                        c.Input.MoveToParent ();
                                }
-                               ProcessTopLevelElements ();
+                               ProcessTopLevelElements (c);
                        }
                        
                        c.PopStylesheet ();
                }
-               
+
+               private QName [] ParseMappedPrefixes (string list, XPathNavigator nav)
+               {
+                       if (list == null)
+                               return null;
+                       ArrayList al = new ArrayList ();
+                       foreach (string entry in list.Split (XmlChar.WhitespaceChars)) {
+                               if (entry.Length == 0)
+                                       continue;
+                               if (entry == "#default")
+                                       al.Add (new QName (String.Empty, String.Empty));
+                               else {
+                                       string entryNS = nav.GetNamespace (entry);
+                                       if (entryNS != String.Empty)
+                                               al.Add (new QName (entry, entryNS));
+                               }
+                       }
+                       return (QName []) al.ToArray (typeof (QName));
+               }
+
                public XslKey FindKey (QName name)
                {
                        XslKey key = Keys [name] as XslKey;
@@ -184,8 +210,9 @@ namespace Mono.Xml.Xsl {
                        XmlQualifiedName qname = new XmlQualifiedName (localName, ns);
                        object o = spaceControls [qname];
                        if (o == null) {
-                               foreach (XslStylesheet s in imports) {
-                                       o = s.SpaceControls [qname];
+
+                               for (int i = 0; i < imports.Count; i++) {
+                                       o = ((XslStylesheet) imports [i]).SpaceControls [qname];
                                        if (o != null)
                                                break;
                                }
@@ -195,8 +222,8 @@ namespace Mono.Xml.Xsl {
                                qname = new XmlQualifiedName ("*", ns);
                                o = spaceControls [qname];
                                if (o == null) {
-                                       foreach (XslStylesheet s in imports) {
-                                               o = s.SpaceControls [qname];
+                                       for (int i = 0; i < imports.Count; i++) {
+                                               o = ((XslStylesheet) imports [i]).SpaceControls [qname];
                                                if (o != null)
                                                        break;
                                        }
@@ -207,8 +234,8 @@ namespace Mono.Xml.Xsl {
                                qname = new XmlQualifiedName ("*", String.Empty);
                                o = spaceControls [qname];
                                if (o == null) {
-                                       foreach (XslStylesheet s in imports) {
-                                               o = s.SpaceControls [qname];
+                                       for (int i = 0; i < imports.Count; i++) {
+                                               o = ((XslStylesheet) imports [i]).SpaceControls [qname];
                                                if (o != null)
                                                        break;
                                        }
@@ -216,7 +243,6 @@ namespace Mono.Xml.Xsl {
                        }
 
                        if (o != null) {
-                               XmlSpace space = (XmlSpace) o;
                                switch ((XmlSpace) o) {
                                case XmlSpace.Preserve:
                                        return true;
@@ -255,8 +281,8 @@ namespace Mono.Xml.Xsl {
 
                        string result = namespaceAliases [prefix];
                        if (result == null) {
-                               foreach (XslStylesheet s in imports) {
-                                       result = s.namespaceAliases [prefix];
+                               for (int i = 0; i < imports.Count; i++) {
+                                       result = ((XslStylesheet) imports [i]).namespaceAliases [prefix];
                                        if (result != null)
                                                break;
                                }
@@ -267,24 +293,38 @@ namespace Mono.Xml.Xsl {
 
                private XslStylesheet (Compiler c, XslStylesheet importer) : this (c)
                {
-                       this.importer = importer;
+//                     this.importer = importer;
                }
                
-               private void HandleInclude (string href)
+               private void HandleInclude (Compiler c, string href)
                {
                        c.PushInputDocument (href);
-                       ProcessTopLevelElements ();
+
+                       // move to root element
+                       while (c.Input.NodeType != XPathNodeType.Element)
+                               if (!c.Input.MoveToNext ())
+                                       throw new XsltCompileException ("Stylesheet root element must be either \"stylesheet\" or \"transform\" or any literal element.", null, c.Input);
+
+                       if (c.Input.NamespaceURI != XsltNamespace) {
+                               if (c.Input.GetAttribute ("version", XsltNamespace) == String.Empty)
+                                       throw new XsltCompileException ("Mandatory global attribute version is missing.", null, c.Input);
+                               // then it is simplified stylesheet.
+                               Templates.Add (new XslTemplate (c));
+                       }
+                       else
+                               ProcessTopLevelElements (c);
+
                        c.PopInputDocument ();
                }
                
-               private void HandleImport (string href)
+               private void HandleImport (Compiler c, string href)
                {
                        c.PushInputDocument (href);
                        imports.Add (new XslStylesheet (c, this));
                        c.PopInputDocument ();
                }
                
-               private void HandleTopLevelElement ()
+               private void HandleTopLevelElement (Compiler c)
                {
                        XPathNavigator n = c.Input;
                        switch (n.NamespaceURI)
@@ -294,10 +334,10 @@ namespace Mono.Xml.Xsl {
                                switch (n.LocalName)
                                {
                                case "include":
-                                       HandleInclude (c.GetAttribute ("href"));
+                                       HandleInclude (c, c.GetAttribute ("href"));
                                        break;
                                case "import":
-                                       HandleImport (c.GetAttribute ("href"));
+                                       HandleImport (c, c.GetAttribute ("href"));
                                        break;
                                case "preserve-space":
                                        AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Preserve, n);
@@ -306,9 +346,8 @@ namespace Mono.Xml.Xsl {
                                case "strip-space":
                                        AddSpaceControls (c.ParseQNameListAttribute ("elements"), XmlSpace.Default, n);
                                        break;
-                               
                                case "namespace-alias":
-                                       namespaceAliases.Add ((string) c.GetAttribute ("stylesheet-prefix", ""), (string) c.GetAttribute ("result-prefix", ""));
+                                       // do nothing. It is handled in prior.
                                        break;
                                
                                case "attribute-set":
@@ -316,7 +355,7 @@ namespace Mono.Xml.Xsl {
                                        break;
 
                                case "key":
-                                       keys.Add (c.ParseQNameAttribute ("name"), new XslKey (c));
+                                       keys [c.ParseQNameAttribute ("name")] = new XslKey (c);
                                        break;
                                        
                                case "output":
@@ -353,19 +392,35 @@ namespace Mono.Xml.Xsl {
                        }
                }
                
-               private void ProcessTopLevelElements ()
+               private void ProcessTopLevelElements (Compiler c)
                {
-                       if (c.Input.MoveToFirstChild ()) {
-                               do {
-                                       if (c.Input.NodeType == XPathNodeType.Element) {                                        
-                                               Debug.EnterNavigator (c);
-                                               this.HandleTopLevelElement();
-                                               Debug.ExitNavigator (c);
-                                       }
-                               } while (c.Input.MoveToNext ());
-                               
-                               c.Input.MoveToParent ();
-                       }
+                       if (!c.Input.MoveToFirstChild ())
+                               return;
+                       do {
+                               // Collect namespace aliases first.
+                               if (c.Input.NodeType != XPathNodeType.Element ||
+                                       c.Input.LocalName != "namespace-alias" ||
+                                       c.Input.NamespaceURI != XsltNamespace)
+                                       continue;
+                               string sprefix = (string) c.GetAttribute ("stylesheet-prefix", "");
+                               if (sprefix == "#default")
+                                       sprefix = String.Empty;
+                               string rprefix= (string) c.GetAttribute ("result-prefix", "");
+                               if (rprefix == "#default")
+                                       rprefix = String.Empty;
+                               namespaceAliases.Set (sprefix, rprefix);
+                       } while (c.Input.MoveToNext ());
+
+                       c.Input.MoveToFirst ();
+                       do {
+                               if (c.Input.NodeType != XPathNodeType.Element)
+                                       continue;
+                               Debug.EnterNavigator (c);
+                               this.HandleTopLevelElement (c);
+                               Debug.ExitNavigator (c);
+                       } while (c.Input.MoveToNext ());
+                       
+                       c.Input.MoveToParent ();
                }
 
                private void AddSpaceControls (QName [] names, XmlSpace result, XPathNavigator styleElem)
@@ -374,46 +429,5 @@ namespace Mono.Xml.Xsl {
                        foreach (QName name in names)
                                spaceControls [name] = result;
                }
-
-               public string PrefixInEffect (string prefix, ArrayList additionalExcluded)
-               {
-                       if (additionalExcluded != null && additionalExcluded.Contains (prefix == String.Empty ? "#default" : prefix))
-                               return null;
-                       if (prefix == "#default")
-                               prefix = String.Empty;
-
-                       if (ExcludeResultPrefixes != null) {
-                               bool exclude = false;
-                               foreach (XmlQualifiedName exc in ExcludeResultPrefixes)
-                                       if (exc.Name == "#default" && prefix == String.Empty || exc.Name == prefix) {
-                                               exclude = true;
-                                               break;
-                                       }
-                               if (exclude)
-                                       return null;
-                       }
-
-                       if (ExtensionElementPrefixes != null) {
-                               bool exclude = false;
-                               foreach (XmlQualifiedName exc in ExtensionElementPrefixes)
-                                       if (exc.Name == "#default" && prefix == String.Empty || exc.Name == prefix) {
-                                               exclude = true;
-                                               break;
-                                       }
-                               if (exclude)
-                                       return null;
-                       }
-
-                       return GetActualPrefix (prefix);
-               }
-       }
-
-       
-       public enum XslDefaultValidation
-       {
-               Strict,
-               Lax,
-               Preserve,
-               Strip
        }
 }