Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / IfAction.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="IfAction.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 IfAction : ContainerAction {
16         internal enum ConditionType {
17             ConditionIf,
18             ConditionWhen,
19             ConditionOtherwise
20         }
21
22         private ConditionType   type;
23         private int             testKey = Compiler.InvalidQueryKey;
24
25         internal IfAction(ConditionType type) {
26             this.type = type;
27         }
28
29         internal override void Compile(Compiler compiler) {
30             CompileAttributes(compiler);
31             if (this.type != ConditionType.ConditionOtherwise) {
32                 CheckRequiredAttribute(compiler, this.testKey != Compiler.InvalidQueryKey, "test");
33             }
34
35             if (compiler.Recurse()) {
36                 CompileTemplate(compiler);
37                 compiler.ToParent();
38             }
39         }
40
41         internal override bool CompileAttribute(Compiler compiler) {
42             string name   = compiler.Input.LocalName;
43             string value  = compiler.Input.Value;
44             if (Ref.Equal(name, compiler.Atoms.Test)) {
45                 if (this.type == ConditionType.ConditionOtherwise) {
46                     return false;
47                 }
48                 this.testKey = compiler.AddBooleanQuery(value);
49             }
50             else {
51                 return false;
52             }
53
54             return true;
55         }
56
57         internal override void Execute(Processor processor, ActionFrame frame) {
58             Debug.Assert(processor != null && frame != null);
59
60             switch (frame.State) {
61             case Initialized:
62                 if (this.type == ConditionType.ConditionIf || this.type == ConditionType.ConditionWhen) {
63                     Debug.Assert(this.testKey != Compiler.InvalidQueryKey);
64                     bool value = processor.EvaluateBoolean(frame, this.testKey);
65                     if (value == false) {
66                         frame.Finished();
67                         break;
68                     }
69                 }
70
71                 processor.PushActionFrame(frame);
72                 frame.State = ProcessingChildren;
73                 break;                              // Allow children to run
74
75             case ProcessingChildren:
76                 if (this.type == ConditionType.ConditionWhen ||this.type == ConditionType.ConditionOtherwise) {
77                     Debug.Assert(frame.Container != null);
78                     frame.Exit();
79                 }
80
81                 frame.Finished();
82                 break;
83
84             default:
85                 Debug.Fail("Invalid IfAction execution state");
86                         break;
87             }
88         }
89     }
90 }