2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslStylesheet.cs
index ce81646bda0f313c12841cd259429e84075b81e9..6cf9dedda90129e071d32b4a4cda992c41b0ebe7 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,25 +45,24 @@ 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::script";
+               public const string MSXsltNamespace = "urn:schemas-microsoft-com:xslt";
                
                Compiler c;
 
-               XslStylesheet importer;
+//             XslStylesheet importer;
                // Top-level elements
                ArrayList imports = new ArrayList ();
                // [QName]=>XmlSpace
                Hashtable spaceControls = new Hashtable ();
                // [string stylesheet-prefix]=>string result-prefix
-               Hashtable namespaceAliases = new Hashtable ();
+               NameValueCollection namespaceAliases = new NameValueCollection ();
                // [QName]=>XmlSpace
                Hashtable parameters = new Hashtable ();
                // [QName]=>XslKey
                Hashtable keys = new Hashtable();
 
-               MSXslScriptManager msScripts = new MSXslScriptManager ();
                XslTemplateTable templates;
 
                // stylesheet attributes
@@ -52,22 +71,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; }
                }
@@ -92,7 +95,7 @@ namespace Mono.Xml.Xsl {
                        get { return spaceControls; }
                }
 
-               public Hashtable NamespaceAliases {
+               public NameValueCollection NamespaceAliases {
                        get { return namespaceAliases; }
                }
 
@@ -100,10 +103,6 @@ namespace Mono.Xml.Xsl {
                        get { return parameters; }
                }
 
-               public MSXslScriptManager ScriptManager{
-                       get { return msScripts; }
-               }
-
                public XPathNavigator StyleDocument {
                        get { return c.Input; }
                }
@@ -116,17 +115,36 @@ namespace Mono.Xml.Xsl {
                        get { return keys; }
                }
 
+               public string Version {
+                       get { return version; }
+               }
+
                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) == null)
+                                       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", "");
+                               if (version == null)
+                                       throw new XsltCompileException ("Mandatory attribute version is missing.", null, c.Input);
+
                                extensionElementPrefixes = c.ParseQNameListAttribute ("extension-element-prefixes");
                                excludeResultPrefixes = c.ParseQNameListAttribute ("exclude-result-prefixes");
                                if (c.Input.MoveToFirstNamespace (XPathNamespaceScope.Local)) {
@@ -156,9 +174,119 @@ namespace Mono.Xml.Xsl {
                        return null;
                }
 
+               bool countedSpaceControlExistence;
+               bool cachedHasSpaceControls;
+               public bool HasSpaceControls {
+                       get {
+                               if (!countedSpaceControlExistence) {
+                                       countedSpaceControlExistence = true;
+                                       if (this.spaceControls.Count > 0)
+                                               cachedHasSpaceControls = true;
+                                       else if (imports.Count == 0)
+                                               cachedHasSpaceControls = false;
+                                       else {
+                                               for (int i = 0; i < imports.Count; i++)
+                                                       if (((XslStylesheet) imports [i]).spaceControls.Count > 0)
+                                                               countedSpaceControlExistence = true;
+                                               cachedHasSpaceControls = false;
+                                       }
+                               }
+                               return cachedHasSpaceControls;
+                       }
+               }
+
+               public bool GetPreserveWhitespace (string localName, string ns)
+               {
+                       if (!HasSpaceControls)
+                               return true;
+
+                       XmlQualifiedName qname = new XmlQualifiedName (localName, ns);
+                       object o = spaceControls [qname];
+                       if (o == null) {
+
+                               for (int i = 0; i < imports.Count; i++) {
+                                       o = ((XslStylesheet) imports [i]).SpaceControls [qname];
+                                       if (o != null)
+                                               break;
+                               }
+                       }
+
+                       if (o == null) {
+                               qname = new XmlQualifiedName ("*", ns);
+                               o = spaceControls [qname];
+                               if (o == null) {
+                                       for (int i = 0; i < imports.Count; i++) {
+                                               o = ((XslStylesheet) imports [i]).SpaceControls [qname];
+                                               if (o != null)
+                                                       break;
+                                       }
+                               }
+                       }
+
+                       if (o == null) {
+                               qname = new XmlQualifiedName ("*", String.Empty);
+                               o = spaceControls [qname];
+                               if (o == null) {
+                                       for (int i = 0; i < imports.Count; i++) {
+                                               o = ((XslStylesheet) imports [i]).SpaceControls [qname];
+                                               if (o != null)
+                                                       break;
+                                       }
+                               }
+                       }
+
+                       if (o != null) {
+                               switch ((XmlSpace) o) {
+                               case XmlSpace.Preserve:
+                                       return true;
+                               case XmlSpace.Default:
+                                       return false;
+                               }
+                       }
+                       return true;
+               }
+
+               bool countedNamespaceAliases;
+               bool cachedHasNamespaceAliases;
+               public bool HasNamespaceAliases {
+                       get {
+                               if (!countedNamespaceAliases) {
+                                       countedNamespaceAliases = true;
+                                       if (namespaceAliases.Count > 0)
+                                               cachedHasNamespaceAliases = true;
+                                       else if (imports.Count == 0)
+                                               cachedHasNamespaceAliases = false;
+                                       else {
+                                               for (int i = 0; i < imports.Count; i++)
+                                                       if (((XslStylesheet) imports [i]).namespaceAliases.Count > 0)
+                                                               countedNamespaceAliases = true;
+                                               cachedHasNamespaceAliases = false;
+                                       }
+                               }
+                               return cachedHasNamespaceAliases;
+                       }
+               }
+
+               public string GetActualPrefix (string prefix)
+               {
+                       if (!HasNamespaceAliases)
+                               return prefix;
+
+                       string result = namespaceAliases [prefix];
+                       if (result == null) {
+                               for (int i = 0; i < imports.Count; i++) {
+                                       result = ((XslStylesheet) imports [i]).namespaceAliases [prefix];
+                                       if (result != null)
+                                               break;
+                               }
+                       }
+
+                       return result != null ? result : prefix;
+               }
+
                private XslStylesheet (Compiler c, XslStylesheet importer) : this (c)
                {
-                       this.importer = importer;
+//                     this.importer = importer;
                }
                
                private void HandleInclude (string href)
@@ -199,7 +327,7 @@ namespace Mono.Xml.Xsl {
                                        break;
                                
                                case "namespace-alias":
-                                       namespaceAliases.Add (c.GetAttribute ("stylesheet-prefix", ""), c.GetAttribute ("result-prefix", ""));
+                                       namespaceAliases.Add ((string) c.GetAttribute ("stylesheet-prefix", ""), (string) c.GetAttribute ("result-prefix", ""));
                                        break;
                                
                                case "attribute-set":
@@ -227,13 +355,17 @@ namespace Mono.Xml.Xsl {
                                case "param":
                                        c.AddGlobalVariable (new XslGlobalParam (c));
                                        break;
+                               default:
+                                       if (version == "1.0")
+                                               throw new XsltCompileException ("Unrecognized top level element.", null, c.Input);
+                                       break;
                                }
                                break;
                        case MSXsltNamespace:
                                switch (n.LocalName)
                                {
                                case "script":
-                                       msScripts.AddScript (n);
+                                       c.ScriptManager.AddScript (c);
                                        break;
                                }
                                break;
@@ -257,8 +389,9 @@ namespace Mono.Xml.Xsl {
 
                private void AddSpaceControls (QName [] names, XmlSpace result, XPathNavigator styleElem)
                {
+                       // XSLT 3.4 - This implementation recovers from errors.
                        foreach (QName name in names)
-                               spaceControls.Add (name, result);
+                               spaceControls [name] = result;
                }
 
                public string PrefixInEffect (string prefix, ArrayList additionalExcluded)
@@ -290,16 +423,12 @@ namespace Mono.Xml.Xsl {
                                        return null;
                        }
 
-                       string alias = NamespaceAliases [prefix] as string;
-                       if (alias != null)
-                               return alias;
-
-                       return prefix;
+                       return GetActualPrefix (prefix);
                }
        }
 
        
-       public enum XslDefaultValidation
+       internal enum XslDefaultValidation
        {
                Strict,
                Lax,