1 //------------------------------------------------------------------------------
2 // <copyright file="TemplateManager.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 namespace System
.Xml
.Xsl
.XsltOld
{
9 using Res
= System
.Xml
.Utils
.Res
;
11 using System
.Diagnostics
;
13 using System
.Xml
.XPath
;
14 using System
.Collections
;
16 internal class TemplateManager
{
17 private XmlQualifiedName mode
;
18 internal ArrayList templates
;
19 private Stylesheet stylesheet
; // Owning stylesheet
21 private class TemplateComparer
: IComparer
{
22 public int Compare(object x
, object y
) {
23 Debug
.Assert(x
!= null && x
is TemplateAction
);
24 Debug
.Assert(y
!= null && y
is TemplateAction
);
26 TemplateAction tx
= (TemplateAction
) x
;
27 TemplateAction ty
= (TemplateAction
) y
;
29 Debug
.Assert(! Double
.IsNaN(tx
.Priority
));
30 Debug
.Assert(! Double
.IsNaN(ty
.Priority
));
32 if (tx
.Priority
== ty
.Priority
) {
33 Debug
.Assert(tx
.TemplateId
!= ty
.TemplateId
|| tx
== ty
);
34 return tx
.TemplateId
- ty
.TemplateId
;
37 return tx
.Priority
> ty
.Priority
? 1 : -1;
42 private static TemplateComparer s_TemplateComparer
= new TemplateComparer();
44 internal XmlQualifiedName Mode
{
45 get { return this.mode; }
48 internal TemplateManager(Stylesheet stylesheet
, XmlQualifiedName mode
) {
50 this.stylesheet
= stylesheet
;
53 internal void AddTemplate(TemplateAction template
) {
54 Debug
.Assert(template
!= null);
56 ((object) this.mode
== (object) template
.Mode
) ||
57 (template
.Mode
== null && this.mode
.Equals(XmlQualifiedName
.Empty
)) ||
58 this.mode
.Equals(template
.Mode
)
61 if (this.templates
== null) {
62 this.templates
= new ArrayList();
65 this.templates
.Add(template
);
68 internal void ProcessTemplates() {
69 if (this.templates
!= null) {
70 this.templates
.Sort(s_TemplateComparer
);
74 internal TemplateAction
FindTemplate(Processor processor
, XPathNavigator navigator
) {
75 if (this.templates
== null) {
79 Debug
.Assert(this.templates
!= null);
80 for (int templateIndex
= this.templates
.Count
- 1; templateIndex
>= 0 ; templateIndex
--) {
81 TemplateAction action
= (TemplateAction
) this.templates
[templateIndex
];
82 int matchKey
= action
.MatchKey
;
84 if (matchKey
!= Compiler
.InvalidQueryKey
) {
85 if (processor
.Matches(navigator
, matchKey
)) {