Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XmlILCommand.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XmlILCommand.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="false">Microsoft</owner>
7 // <spec>http://webdata/xml/specs/querylowlevel.xml</spec>
8 //------------------------------------------------------------------------------
9
10 using System.Collections;
11 using System.Diagnostics;
12 using System.IO;
13 using System.Xml.XPath;
14 using System.Xml.Xsl.Runtime;
15 using System.Runtime.Versioning;
16
17 namespace System.Xml.Xsl {
18     /// <summary>
19     /// This is the executable command generated by the XmlILGenerator.
20     /// </summary>
21     internal class XmlILCommand {
22         private ExecuteDelegate delExec;
23         private XmlQueryStaticData staticData;
24
25         /// <summary>
26         /// Constructor.
27         /// </summary>
28         public XmlILCommand(ExecuteDelegate delExec, XmlQueryStaticData staticData) {
29             Debug.Assert(delExec != null && staticData != null);
30             this.delExec = delExec;
31             this.staticData = staticData;
32         }
33
34         /// <summary>
35         /// Return execute delegate.
36         /// </summary>
37         public ExecuteDelegate ExecuteDelegate {
38             get { return delExec; }
39         }
40
41         /// <summary>
42         /// Return query static data required by the runtime.
43         /// </summary>
44         public XmlQueryStaticData StaticData {
45             get { return staticData; }
46         }
47
48     #if false
49         /// <summary>
50         /// Default serialization options that will be used if the user does not supply an XmlWriter
51         /// at execution time.
52         /// </summary>
53         public override XmlWriterSettings DefaultWriterSettings {
54             get { return this.staticData.DefaultWriterSettings; }
55         }
56
57         /// <summary>
58         /// Default document as XPathNavigator.
59         /// </summary>
60         public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
61             if (results == null)
62                 throw new ArgumentNullException("results");
63
64             if (contextDocument != null)
65                 Execute(contextDocument.CreateNavigator(), dataSources, argumentList, results, false);
66             else
67                 Execute(null, dataSources, argumentList, results, false);
68         }
69
70         /// <summary>
71         /// Default document as XPathNavigator.
72         /// </summary>
73         public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) {
74             if (results == null)
75                 throw new ArgumentNullException("results");
76
77             Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings));
78         }
79
80         /// <summary>
81         /// Default document as XPathNavigator.
82         /// </summary>
83         public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, Stream results) {
84             if (results == null)
85                 throw new ArgumentNullException("results");
86
87             Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings));
88         }
89
90         /// <summary>
91         /// Executes the query by accessing datasources via the XmlResolver and using run-time parameters
92         /// as provided by the XsltArgumentList. The default document is mapped into the XmlResolver with the
93         /// provided name. The results are output to the provided XmlWriter.
94         /// </summary>
95         public void Execute(string contextDocumentUri, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
96             if (results == null)
97                 throw new ArgumentNullException("results");
98
99             Execute(contextDocumentUri, dataSources, argumentList, results, false);
100         }
101     #endif
102
103         /// <summary>
104         /// Executes the query by accessing datasources via the XmlResolver and using
105         /// run-time parameters as provided by the XsltArgumentList. The default document
106         /// is mapped into the XmlResolver with the provided name. The results are returned
107         /// as an IList.
108         /// </summary>
109         [ResourceConsumption(ResourceScope.Machine)]
110         [ResourceExposure(ResourceScope.Machine)]
111         public IList Evaluate(string contextDocumentUri, XmlResolver dataSources, XsltArgumentList argumentList) {
112             XmlCachedSequenceWriter seqwrt = new XmlCachedSequenceWriter();
113             Execute(contextDocumentUri, dataSources, argumentList, seqwrt);
114             return seqwrt.ResultSequence;
115         }
116
117     #if false
118         /// <summary>
119         /// Executes the query by accessing datasources via the XmlResolver and using run-time parameters
120         /// as provided by the XsltArgumentList. The default document is mapped into the XmlResolver with the
121         /// provided name. The results are output to the provided XmlWriter.
122         /// </summary>
123         public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
124             if (results == null)
125                 throw new ArgumentNullException("results");
126
127             Execute(contextDocument, dataSources, argumentList, results, false);
128         }
129
130         /// <summary>
131         /// Executes the query by accessing datasources via the XmlResolver and using run-time parameters
132         /// as provided by the XsltArgumentList. The default document is mapped into the XmlResolver with the
133         /// provided name. The results are output to the provided TextWriter.
134         /// </summary>
135         public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) {
136             if (results == null)
137                 throw new ArgumentNullException("results");
138
139             Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings), true);
140         }
141
142         /// <summary>
143         /// Executes the query by accessing datasources via the XmlResolver and using run-time parameters
144         /// as provided by the XsltArgumentList. The default document is mapped into the XmlResolver with the
145         /// provided name. The results are output to the provided Stream.
146         /// </summary>
147         public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, Stream results) {
148             if (results == null)
149                 throw new ArgumentNullException("results");
150
151             Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings), true);
152         }
153
154         /// <summary>
155         /// Executes the query by accessing datasources via the XmlResolver and using
156         /// run-time parameters as provided by the XsltArgumentList. The default document
157         /// is mapped into the XmlResolver with the provided name. The results are returned
158         /// as an IList.
159         /// </summary>
160         public override IList Evaluate(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList) {
161             XmlCachedSequenceWriter seqwrt = new XmlCachedSequenceWriter();
162             Execute(contextDocument, dataSources, argumentList, seqwrt);
163             return seqwrt.ResultSequence;
164         }
165     #endif
166
167         /// <summary>
168         /// Execute the dynamic assembly generated by the XmlILGenerator.
169         /// </summary>
170         [ResourceConsumption(ResourceScope.Machine)]
171         [ResourceExposure(ResourceScope.Machine)]
172         public void Execute(object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer) {
173             try {
174
175                 if (writer is XmlAsyncCheckWriter) {
176                     writer = ((XmlAsyncCheckWriter)writer).CoreWriter;
177                 }
178
179                 // Try to extract a RawWriter
180                 XmlWellFormedWriter wellFormedWriter = writer as XmlWellFormedWriter;
181
182                 if (wellFormedWriter != null &&
183                     wellFormedWriter.RawWriter != null &&
184                     wellFormedWriter.WriteState == WriteState.Start &&
185                     wellFormedWriter.Settings.ConformanceLevel != ConformanceLevel.Document) {
186
187                     // Extracted RawWriter from WellFormedWriter
188                     Execute(defaultDocument, dataSources, argumentList, new XmlMergeSequenceWriter(wellFormedWriter.RawWriter));
189                 }
190                 else {
191                     // Wrap Writer in RawWriter
192                     Execute(defaultDocument, dataSources, argumentList, new XmlMergeSequenceWriter(new XmlRawWriterWrapper(writer)));
193                 }
194             }
195             finally {
196                 writer.Flush();
197             }
198         }
199
200         /// <summary>
201         /// Execute the dynamic assembly generated by the XmlILGenerator.
202         /// </summary>
203         [ResourceConsumption(ResourceScope.Machine)]
204         [ResourceExposure(ResourceScope.Machine)]
205         private void Execute(object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results) {
206             Debug.Assert(results != null);
207
208             // Ensure that dataSources is always non-null
209             if (dataSources == null)
210                 dataSources = XmlNullResolver.Singleton;
211
212             this.delExec(new XmlQueryRuntime(this.staticData, defaultDocument, dataSources, argumentList, results));
213         }
214     }
215 }