1 //------------------------------------------------------------------------------
2 // <copyright file="CopyAttributesAction.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 sealed class CopyAttributesAction : Action {
16         private const int BeginEvent    = 2;
17         private const int TextEvent     = 3;
18         private const int EndEvent      = 4;
19         private const int Advance       = 5;
20 
21         private static CopyAttributesAction s_Action = new CopyAttributesAction();
22 
GetAction()23         internal static CopyAttributesAction GetAction() {
24             Debug.Assert(s_Action != null);
25             return s_Action;
26         }
27 
Execute(Processor processor, ActionFrame frame)28         internal override void Execute(Processor processor, ActionFrame frame) {
29             Debug.Assert(processor != null && frame != null);
30 
31             while (processor.CanContinue) {
32                 switch (frame.State) {
33                 case Initialized:
34                     if (!frame.Node.HasAttributes || frame.Node.MoveToFirstAttribute() == false) {
35                         frame.Finished();
36                         break;
37                     }
38 
39                     frame.State   = BeginEvent;
40                     goto case BeginEvent;
41 
42                 case BeginEvent:
43                     Debug.Assert(frame.State == BeginEvent);
44                     Debug.Assert(frame.Node.NodeType == XPathNodeType.Attribute);
45 
46                     if (SendBeginEvent(processor, frame.Node) == false) {
47                         // This one wasn't output
48                         break;
49                     }
50                     frame.State = TextEvent;
51                     continue;
52 
53                 case TextEvent:
54                     Debug.Assert(frame.State == TextEvent);
55                     Debug.Assert(frame.Node.NodeType == XPathNodeType.Attribute);
56 
57                     if (SendTextEvent(processor, frame.Node) == false) {
58                         // This one wasn't output
59                         break;
60                     }
61                     frame.State = EndEvent;
62                     continue;
63 
64                 case EndEvent:
65                     Debug.Assert(frame.State == EndEvent);
66                     Debug.Assert(frame.Node.NodeType == XPathNodeType.Attribute);
67 
68                     if (SendEndEvent(processor, frame.Node) == false) {
69                         // This one wasn't output
70                         break;
71                     }
72                     frame.State = Advance;
73                     continue;
74 
75                 case Advance:
76                     Debug.Assert(frame.State == Advance);
77                     Debug.Assert(frame.Node.NodeType == XPathNodeType.Attribute);
78 
79                     if (frame.Node.MoveToNextAttribute()) {
80                         frame.State = BeginEvent;
81                         continue;
82                     }
83                     else {
84                         frame.Node.MoveToParent();
85                         frame.Finished();
86                         break;
87                     }
88                 }
89                 break;
90             }// while (processor.CanContinue)
91         }
92 
SendBeginEvent(Processor processor, XPathNavigator node)93         private static bool SendBeginEvent(Processor processor, XPathNavigator node) {
94             Debug.Assert(node.NodeType == XPathNodeType.Attribute);
95             return processor.BeginEvent(XPathNodeType.Attribute, node.Prefix, node.LocalName, node.NamespaceURI, false);
96         }
97 
SendTextEvent(Processor processor, XPathNavigator node)98         private static bool SendTextEvent(Processor processor, XPathNavigator node) {
99             Debug.Assert(node.NodeType == XPathNodeType.Attribute);
100             return processor.TextEvent(node.Value);
101         }
102 
SendEndEvent(Processor processor, XPathNavigator node)103         private static bool SendEndEvent(Processor processor, XPathNavigator node) {
104             Debug.Assert(node.NodeType == XPathNodeType.Attribute);
105             return processor.EndEvent(XPathNodeType.Attribute);
106         }
107     }
108 }
109