cb7d325be0eb7fc3152084dceb5c368c471fa25e
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XsltOld / CopyAction.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="CopyAction.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     using MS.Internal.Xml.XPath;
15
16     internal class CopyAction : ContainerAction {
17         // Local execution states
18         private const int CopyText          = 4;
19         private const int NamespaceCopy     = 5;
20
21         private const int ContentsCopy      = 6;
22         private const int ProcessChildren   = 7;
23         private const int ChildrenOnly      = 8;
24
25         private string useAttributeSets;
26         private bool   empty;
27
28         internal override void Compile(Compiler compiler) {
29             CompileAttributes(compiler);
30
31             if (compiler.Recurse()) {
32                 CompileTemplate(compiler);
33                 compiler.ToParent();
34             }
35             if (this.containedActions == null)
36                 this.empty = true;
37                 
38         }
39
40         internal override bool CompileAttribute(Compiler compiler) {
41             string name   = compiler.Input.LocalName;
42             string value  = compiler.Input.Value;
43             if (Ref.Equal(name, compiler.Atoms.UseAttributeSets)) {
44                 this.useAttributeSets = value;
45                 AddAction(compiler.CreateUseAttributeSetsAction());
46             }
47             else {
48                 return false;
49             }
50             return true;
51         }
52
53         internal override void Execute(Processor processor, ActionFrame frame) {
54             Debug.Assert(processor != null && frame != null);
55
56             while (processor.CanContinue) {
57                 switch (frame.State) {
58                 case Initialized:
59                     if (Processor.IsRoot(frame.Node)) {
60                         processor.PushActionFrame(frame);
61                         frame.State = ChildrenOnly;
62                         break;
63                     }
64
65                     if (processor.CopyBeginEvent(frame.Node, this.empty) == false) {
66                         // This event wasn't processed
67                         break;
68                     }
69                     frame.State = NamespaceCopy;
70                  
71                     continue;
72                 case NamespaceCopy:
73                     frame.State = ContentsCopy;
74                     if ( frame.Node.NodeType == XPathNodeType.Element ) {
75                         processor.PushActionFrame(CopyNamespacesAction.GetAction(), frame.NodeSet);
76                         break;
77                     }
78                     continue;
79                 case ContentsCopy:
80                     if (frame.Node.NodeType == XPathNodeType.Element && !this.empty) {
81                         //Debug.Assert(frame.Node.HasValue == false);
82                         processor.PushActionFrame(frame);
83                         frame.State = ProcessChildren;
84                         break;
85                     }
86                     else {
87                         if (processor.CopyTextEvent(frame.Node)) {
88                             frame.State = ProcessChildren;
89                             continue;
90                         }
91                         else {
92                             // This event wasn't processed
93                             break;
94                         }
95                     }
96
97                 case ProcessChildren:
98                     if (processor.CopyEndEvent(frame.Node)) {
99                         frame.Finished();
100                     }
101                     break;
102
103                 case ChildrenOnly:
104                     frame.Finished();
105                     break;
106
107                 default:
108                     Debug.Fail("Invalid CopyAction execution state");
109                                 break;
110                 }
111
112                 break;
113             }
114         }
115     }
116 }