2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.XML / Mono.Xml.Xsl / XslStylesheet.cs
index cfe31ad07a4a313e3ddb5e68bdfb172ca85c1b0a..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,13 +45,13 @@ 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;
+//             XslStylesheet importer;
                // Top-level elements
                ArrayList imports = new ArrayList ();
                // [QName]=>XmlSpace
@@ -51,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; }
                }
@@ -121,11 +125,26 @@ namespace Mono.Xml.Xsl {
                        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)) {
@@ -184,8 +203,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 +215,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 +227,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 +236,6 @@ namespace Mono.Xml.Xsl {
                        }
 
                        if (o != null) {
-                               XmlSpace space = (XmlSpace) o;
                                switch ((XmlSpace) o) {
                                case XmlSpace.Preserve:
                                        return true;
@@ -255,8 +274,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,7 +286,7 @@ namespace Mono.Xml.Xsl {
 
                private XslStylesheet (Compiler c, XslStylesheet importer) : this (c)
                {
-                       this.importer = importer;
+//                     this.importer = importer;
                }
                
                private void HandleInclude (string href)
@@ -409,7 +428,7 @@ namespace Mono.Xml.Xsl {
        }
 
        
-       public enum XslDefaultValidation
+       internal enum XslDefaultValidation
        {
                Strict,
                Lax,