1 //------------------------------------------------------------------------------
2 // <copyright file="XmlILCommand.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="false">Microsoft</owner>
7 // <spec>http://webdata/xml/specs/querylowlevel.xml</spec>
8 //------------------------------------------------------------------------------
10 using System.Collections;
11 using System.Diagnostics;
13 using System.Xml.XPath;
14 using System.Xml.Xsl.Runtime;
15 using System.Runtime.Versioning;
17 namespace System.Xml.Xsl {
19 /// This is the executable command generated by the XmlILGenerator.
21 internal class XmlILCommand {
22 private ExecuteDelegate delExec;
23 private XmlQueryStaticData staticData;
28 public XmlILCommand(ExecuteDelegate delExec, XmlQueryStaticData staticData) {
29 Debug.Assert(delExec != null && staticData != null);
30 this.delExec = delExec;
31 this.staticData = staticData;
35 /// Return execute delegate.
37 public ExecuteDelegate ExecuteDelegate {
38 get { return delExec; }
42 /// Return query static data required by the runtime.
44 public XmlQueryStaticData StaticData {
45 get { return staticData; }
50 /// Default serialization options that will be used if the user does not supply an XmlWriter
51 /// at execution time.
53 public override XmlWriterSettings DefaultWriterSettings {
54 get { return this.staticData.DefaultWriterSettings; }
58 /// Default document as XPathNavigator.
60 public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
62 throw new ArgumentNullException("results");
64 if (contextDocument != null)
65 Execute(contextDocument.CreateNavigator(), dataSources, argumentList, results, false);
67 Execute(null, dataSources, argumentList, results, false);
71 /// Default document as XPathNavigator.
73 public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) {
75 throw new ArgumentNullException("results");
77 Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings));
81 /// Default document as XPathNavigator.
83 public override void Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, Stream results) {
85 throw new ArgumentNullException("results");
87 Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings));
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.
95 public void Execute(string contextDocumentUri, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
97 throw new ArgumentNullException("results");
99 Execute(contextDocumentUri, dataSources, argumentList, results, false);
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
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;
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.
123 public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) {
125 throw new ArgumentNullException("results");
127 Execute(contextDocument, dataSources, argumentList, results, false);
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.
135 public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) {
137 throw new ArgumentNullException("results");
139 Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings), true);
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.
147 public override void Execute(XmlReader contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, Stream results) {
149 throw new ArgumentNullException("results");
151 Execute(contextDocument, dataSources, argumentList, XmlWriter.Create(results, this.staticData.DefaultWriterSettings), true);
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
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;
168 /// Execute the dynamic assembly generated by the XmlILGenerator.
170 [ResourceConsumption(ResourceScope.Machine)]
171 [ResourceExposure(ResourceScope.Machine)]
172 public void Execute(object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer) {
175 if (writer is XmlAsyncCheckWriter) {
176 writer = ((XmlAsyncCheckWriter)writer).CoreWriter;
179 // Try to extract a RawWriter
180 XmlWellFormedWriter wellFormedWriter = writer as XmlWellFormedWriter;
182 if (wellFormedWriter != null &&
183 wellFormedWriter.RawWriter != null &&
184 wellFormedWriter.WriteState == WriteState.Start &&
185 wellFormedWriter.Settings.ConformanceLevel != ConformanceLevel.Document) {
187 // Extracted RawWriter from WellFormedWriter
188 Execute(defaultDocument, dataSources, argumentList, new XmlMergeSequenceWriter(wellFormedWriter.RawWriter));
191 // Wrap Writer in RawWriter
192 Execute(defaultDocument, dataSources, argumentList, new XmlMergeSequenceWriter(new XmlRawWriterWrapper(writer)));
201 /// Execute the dynamic assembly generated by the XmlILGenerator.
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);
208 // Ensure that dataSources is always non-null
209 if (dataSources == null)
210 dataSources = XmlNullResolver.Singleton;
212 this.delExec(new XmlQueryRuntime(this.staticData, defaultDocument, dataSources, argumentList, results));