063d58991c4377f3ff6e67fb614fa2461855c760
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / TemplateLookupAction.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="TemplateLookupAction.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
7
8 namespace System.Xml.Xsl.XsltOld {
9     using Res = System.Xml.Utils.Res;
10     using System;
11     using System.Diagnostics;
12     using System.Xml;
13     using System.Xml.XPath;
14
15     internal class TemplateLookupAction : Action {
16         protected XmlQualifiedName mode;
17         protected Stylesheet       importsOf;
18
19         internal void Initialize(XmlQualifiedName mode, Stylesheet importsOf) {
20             this.mode      = mode;
21             this.importsOf = importsOf;
22         }
23
24         internal override void Execute(Processor processor, ActionFrame frame) {
25             Debug.Assert(processor != null && frame != null);
26             Debug.Assert(frame.State == Initialized);
27
28             Action action = null;
29
30             if (this.mode != null) {
31                 action = importsOf == null 
32                     ? processor.Stylesheet.FindTemplate(processor, frame.Node, this.mode)
33                     : importsOf.FindTemplateImports(processor, frame.Node, this.mode);
34             }
35             else {
36                 action = importsOf == null 
37                     ? processor.Stylesheet.FindTemplate(processor, frame.Node)
38                     : importsOf.FindTemplateImports(processor, frame.Node);
39             }
40
41             // Built-int template rules
42             if (action == null) {
43                 action = BuiltInTemplate(frame.Node);
44             }
45
46             // Jump
47             if (action != null) {
48                 frame.SetAction(action);
49             }
50             else {
51                 frame.Finished();
52             }
53         }
54
55         internal Action BuiltInTemplate(XPathNavigator node) {
56             Debug.Assert(node != null);
57             Action action = null;
58
59             switch (node.NodeType) {
60             //  <xsl:template match="*|/" [mode="?"]>
61             //    <xsl:apply-templates [mode="?"]/>
62             //  </xsl:template>
63             case XPathNodeType.Element:
64             case XPathNodeType.Root:
65                 action = ApplyTemplatesAction.BuiltInRule(this.mode);
66                 break;
67             //  <xsl:template match="text()|@*">
68             //    <xsl:value-of select="."/>
69             //  </xsl:template>
70             case XPathNodeType.Attribute:
71             case XPathNodeType.Whitespace:
72             case XPathNodeType.SignificantWhitespace:
73             case XPathNodeType.Text:
74                 action = ValueOfAction.BuiltInRule();
75                 break;
76             // <xsl:template match="processing-instruction()|comment()"/>
77             case XPathNodeType.ProcessingInstruction:
78             case XPathNodeType.Comment:
79                 // Empty action;
80                 break;
81             case XPathNodeType.All:
82                 // Ignore the rest
83                 break;
84             }
85
86             return action;
87         }
88     }
89
90     internal class TemplateLookupActionDbg : TemplateLookupAction {
91         internal override void Execute(Processor processor, ActionFrame frame) {
92             Debug.Assert(processor != null && frame != null);
93             Debug.Assert(frame.State == Initialized);
94             Debug.Assert(processor.Debugger != null);
95
96             Action action = null;
97
98             if (this.mode == Compiler.BuiltInMode) {
99                 // mode="*" -- use one from debuggerStack
100                 this.mode = processor.GetPrevioseMode();
101                 Debug.Assert(this.mode != Compiler.BuiltInMode);
102             }
103             processor.SetCurrentMode(this.mode);
104
105             if (this.mode != null) {
106                 action = importsOf == null 
107                     ? processor.Stylesheet.FindTemplate(processor, frame.Node, this.mode)
108                     : importsOf.FindTemplateImports(processor, frame.Node, this.mode);
109             }
110             else {
111                 action = importsOf == null 
112                     ? processor.Stylesheet.FindTemplate(processor, frame.Node)
113                     : importsOf.FindTemplateImports(processor, frame.Node);
114             }
115
116             // Built-int template rules
117             if (action == null && processor.RootAction.builtInSheet != null) {
118                 action = processor.RootAction.builtInSheet.FindTemplate(processor, frame.Node, Compiler.BuiltInMode);
119             }
120             if (action == null) {
121                 action = BuiltInTemplate(frame.Node);
122             }
123
124             // Jump
125             if (action != null) {
126                 frame.SetAction(action);
127             }
128             else {
129                 frame.Finished();
130             }
131         }
132     }
133 }