The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Xslt \ Stylesheet

  1. //------------------------------------------------------------------------------
  2. // <copyright file="Stylesheet.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System.Collections.Generic;
  16. using System.Diagnostics;
  17. using System.Xml.Xsl.Qil;
  18. namespace System.Xml.Xsl.Xslt
  19. {
  20.    
  21.     internal class Stylesheet
  22.     {
  23.         private Compiler compiler;
  24.         public List<Uri> ImportHrefs = new List<Uri>();
  25.         public Stylesheet[] Imports = null;
  26.         public List<Template> Templates = new List<Template>();
  27.         public List<XslNode> GlobalVarPars = new List<XslNode>();
  28.        
  29.         // xsl:attribute-set/@name -> AttributeSet
  30.         public Dictionary<QilName, AttributeSet> AttributeSets = new Dictionary<QilName, AttributeSet>();
  31.        
  32.         // xsl:template/@mode -> list of @match'es
  33.         public Dictionary<QilName, List<TemplateMatch>> TemplateMatches = new Dictionary<QilName, List<TemplateMatch>>();
  34.        
  35.         // mode -> xsl:apply-import functions for that mode
  36.         public Dictionary<QilName, List<QilFunction>> ApplyImportsFunctions = new Dictionary<QilName, List<QilFunction>>();
  37.        
  38.         private int importPrecedence;
  39.         private int orderNumber = 0;
  40.        
  41. /*
  42.             WhitespaceRules[0] - rules with default priority  0
  43.             WhitespaceRules[1] - rules with default priority -0.25
  44.             WhitespaceRules[2] - rules with default priority -0.5
  45.         */       
  46.         public List<WhitespaceRule>[] WhitespaceRules = new List<WhitespaceRule>[3];
  47.        
  48.         public Stylesheet(Compiler compiler, int importPrecedence)
  49.         {
  50.             this.compiler = compiler;
  51.             this.importPrecedence = importPrecedence;
  52.            
  53.             WhitespaceRules[0] = new List<WhitespaceRule>();
  54.             WhitespaceRules[1] = new List<WhitespaceRule>();
  55.             WhitespaceRules[2] = new List<WhitespaceRule>();
  56.         }
  57.        
  58.         public int ImportPrecedence {
  59.             get { return importPrecedence; }
  60.         }
  61.        
  62.         public void AddWhitespaceRule(int index, WhitespaceRule rule)
  63.         {
  64.             WhitespaceRules[index].Add(rule);
  65.         }
  66.        
  67.         public bool AddVarPar(VarPar var)
  68.         {
  69.             Debug.Assert(var.NodeType == XslNodeType.Variable || var.NodeType == XslNodeType.Param);
  70.             Debug.Assert(var.Name.NamespaceUri != null, "Name must be resolved in XsltLoader");
  71.             foreach (XslNode prevVar in GlobalVarPars) {
  72.                 if (prevVar.Name.Equals(var.Name)) {
  73.                     // [ERR XT0630] It is a static error if a stylesheet contains more than one binding
  74.                     // of a global variable with the same name and same import precedence, unless it also
  75.                     // contains another binding with the same name and higher import precedence.
  76.                     return compiler.AllGlobalVarPars.ContainsKey(var.Name);
  77.                 }
  78.             }
  79.             GlobalVarPars.Add(var);
  80.             return true;
  81.         }
  82.        
  83.         public bool AddTemplate(Template template)
  84.         {
  85.             Debug.Assert(template.ImportPrecedence == 0);
  86.            
  87.             template.ImportPrecedence = this.importPrecedence;
  88.             template.OrderNumber = this.orderNumber++;
  89.            
  90.             compiler.AllTemplates.Add(template);
  91.            
  92.             if (template.Name != null) {
  93.                 Template old;
  94.                 if (!compiler.NamedTemplates.TryGetValue(template.Name, out old)) {
  95.                     compiler.NamedTemplates[template.Name] = template;
  96.                 }
  97.                 else {
  98.                     Debug.Assert(template.ImportPrecedence <= old.ImportPrecedence, "Global objects are processed in order of decreasing import precedence");
  99.                     if (old.ImportPrecedence == template.ImportPrecedence) {
  100.                         return false;
  101.                     }
  102.                 }
  103.             }
  104.            
  105.             if (template.Match != null) {
  106.                 Templates.Add(template);
  107.             }
  108.             return true;
  109.         }
  110.        
  111.         public void AddTemplateMatch(Template template, QilLoop filter)
  112.         {
  113.             List<TemplateMatch> matchesForMode;
  114.             if (!TemplateMatches.TryGetValue(template.Mode, out matchesForMode)) {
  115.                 matchesForMode = TemplateMatches[template.Mode] = new List<TemplateMatch>();
  116.             }
  117.             matchesForMode.Add(new TemplateMatch(template, filter));
  118.         }
  119.        
  120.         public void SortTemplateMatches()
  121.         {
  122.             foreach (QilName mode in TemplateMatches.Keys) {
  123.                 TemplateMatches[mode].Sort(TemplateMatch.Comparer);
  124.             }
  125.         }
  126.     }
  127. }

Developer Fusion