Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / CopyNodesetAction.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CopyNodeSetAction.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">[....]</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 sealed class CopyNodeSetAction : Action {
16         private const int BeginEvent    = 2;
17         private const int Contents      = 3;
18         private const int Namespaces    = 4;
19         private const int Attributes    = 5;
20         private const int Subtree       = 6;
21         private const int EndEvent      = 7;
22
23         private static CopyNodeSetAction s_Action = new CopyNodeSetAction();
24
25         internal static CopyNodeSetAction GetAction() {
26             Debug.Assert(s_Action != null);
27             return s_Action;
28         }
29
30         internal override void Execute(Processor processor, ActionFrame frame) {
31             Debug.Assert(processor != null && frame != null);
32             while (processor.CanContinue) {
33                 switch (frame.State) {
34                 case Initialized:
35                     if (frame.NextNode(processor)) {
36                         frame.State = BeginEvent;
37                         goto case BeginEvent;
38                     }
39                     else {
40                         frame.Finished();
41                         break;
42                     }
43                 case BeginEvent:
44                     Debug.Assert(frame.State == BeginEvent);
45
46                     if (SendBeginEvent(processor, frame.Node) == false) {
47                         // This one wasn't output
48                         break;
49                     }
50                     frame.State = Contents;
51                     continue;
52
53                 case Contents:
54                     Debug.Assert(frame.State == Contents);
55                     XPathNodeType nodeType = frame.Node.NodeType;
56
57                     if (nodeType == XPathNodeType.Element || nodeType == XPathNodeType.Root) {
58                         processor.PushActionFrame(CopyNamespacesAction.GetAction(), frame.NodeSet);
59                         frame.State = Namespaces;
60                         break;
61                     }
62
63                     if (SendTextEvent(processor, frame.Node) == false) {
64                         // This one wasn't output
65                         break;
66                     }
67                     frame.State = EndEvent;
68                     continue;
69
70                 case Namespaces:
71                     processor.PushActionFrame(CopyAttributesAction.GetAction(), frame.NodeSet);
72                     frame.State = Attributes;
73                     break;
74
75                 case Attributes:
76                     if (frame.Node.HasChildren) {
77                         processor.PushActionFrame(GetAction(), frame.Node.SelectChildren(XPathNodeType.All));
78                         frame.State = Subtree;
79                         break;
80                     }
81                     frame.State = EndEvent;
82                     goto case EndEvent;
83
84                 case Subtree:
85                     //frame.Node.MoveToParent();
86                     frame.State = EndEvent;
87                     continue;
88
89                 case EndEvent:
90                     Debug.Assert(frame.State == EndEvent);
91
92                     if (SendEndEvent(processor, frame.Node) == false) {
93                         // This one wasn't output
94                         break;
95                     }
96
97                     frame.State = Initialized;
98                     continue;
99                 }
100
101                 break;
102             }
103         }
104
105         private static bool SendBeginEvent(Processor processor, XPathNavigator node) {
106             return processor.CopyBeginEvent(node, node.IsEmptyElement);
107         }
108
109         private static bool SendTextEvent(Processor processor, XPathNavigator node) {
110             return processor.CopyTextEvent(node);
111         }
112
113         private static bool SendEndEvent(Processor processor, XPathNavigator node) {
114             return processor.CopyEndEvent(node);
115         }
116     }
117 }