1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
|
//------------------------------------------------------------------------------
// <copyright file="XslCompiledTransform.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
// <spec>http://webdata/xml/specs/XslCompiledTransform.xml</spec>
//------------------------------------------------------------------------------
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Security;
using System.Security.Permissions;
using System.Xml.XPath;
using System.Xml.Xsl.Qil;
using System.Xml.Xsl.Runtime;
using System.Xml.Xsl.Xslt;
using System.Runtime.Versioning;
using System.Xml.XmlConfiguration;
namespace System.Xml.Xsl {
#if ! HIDE_XSL
//----------------------------------------------------------------------------------------------------
// Clarification on null values in this API:
// stylesheet, stylesheetUri - cannot be null
// settings - if null, XsltSettings.Default will be used
// stylesheetResolver - if null, XmlNullResolver will be used for includes/imports.
// However, if the principal stylesheet is given by its URI, that
// URI will be resolved using XmlUrlResolver (for compatibility
// with XslTransform and XmlReader).
// typeBuilder - cannot be null
// scriptAssemblyPath - can be null only if scripts are disabled
// compiledStylesheet - cannot be null
// executeMethod, queryData - cannot be null
// earlyBoundTypes - null means no script types
// documentResolver - if null, XmlNullResolver will be used
// input, inputUri - cannot be null
// arguments - null means no arguments
// results, resultsFile - cannot be null
//----------------------------------------------------------------------------------------------------
public sealed class XslCompiledTransform {
// Reader settings used when creating XmlReader from inputUri
private static readonly XmlReaderSettings ReaderSettings = null;
// Permission set that contains Reflection [MemberAccess] permissions
private static readonly PermissionSet MemberAccessPermissionSet;
// Version for GeneratedCodeAttribute
private const string Version = ThisAssembly.Version;
static XslCompiledTransform() {
MemberAccessPermissionSet = new PermissionSet(PermissionState.None);
MemberAccessPermissionSet.AddPermission(new ReflectionPermission(ReflectionPermissionFlag.MemberAccess));
ReaderSettings = new XmlReaderSettings();
}
// Options of compilation
private bool enableDebug = false;
// Results of compilation
private CompilerResults compilerResults = null;
private XmlWriterSettings outputSettings = null;
private QilExpression qil = null;
// Executable command for the compiled stylesheet
private XmlILCommand command = null;
public XslCompiledTransform() {}
public XslCompiledTransform(bool enableDebug) {
this.enableDebug = enableDebug;
}
/// <summary>
/// This function is called on every recompilation to discard all previous results
/// </summary>
private void Reset() {
this.compilerResults = null;
this.outputSettings = null;
this.qil = null;
this.command = null;
}
internal CompilerErrorCollection Errors {
get { return this.compilerResults != null ? this.compilerResults.Errors : null; }
}
/// <summary>
/// Writer settings specified in the stylesheet
/// </summary>
public XmlWriterSettings OutputSettings {
get {
return this.outputSettings;
}
}
public TempFileCollection TemporaryFiles {
[PermissionSet(SecurityAction.LinkDemand, Name="FullTrust")]
get { return this.compilerResults != null ? this.compilerResults.TempFiles : null; }
}
//------------------------------------------------
// Load methods
//------------------------------------------------
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Load(XmlReader stylesheet) {
Reset();
LoadInternal(stylesheet, XsltSettings.Default, XsltConfigSection.CreateDefaultResolver());
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Load(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) {
Reset();
LoadInternal(stylesheet, settings, stylesheetResolver);
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Load(IXPathNavigable stylesheet) {
Reset();
LoadInternal(stylesheet, XsltSettings.Default, XsltConfigSection.CreateDefaultResolver());
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Load(IXPathNavigable stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) {
Reset();
LoadInternal(stylesheet, settings, stylesheetResolver);
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Load(string stylesheetUri) {
Reset();
if (stylesheetUri == null) {
throw new ArgumentNullException("stylesheetUri");
}
LoadInternal(stylesheetUri, XsltSettings.Default, XsltConfigSection.CreateDefaultResolver());
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Load(string stylesheetUri, XsltSettings settings, XmlResolver stylesheetResolver) {
Reset();
if (stylesheetUri == null) {
throw new ArgumentNullException("stylesheetUri");
}
LoadInternal(stylesheetUri, settings, stylesheetResolver);
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
private CompilerResults LoadInternal(object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) {
if (stylesheet == null) {
throw new ArgumentNullException("stylesheet");
}
if (settings == null) {
settings = XsltSettings.Default;
}
CompileXsltToQil(stylesheet, settings, stylesheetResolver);
CompilerError error = GetFirstError();
if (error != null) {
throw new XslLoadException(error);
}
if (!settings.CheckOnly) {
CompileQilToMsil(settings);
}
return this.compilerResults;
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
private void CompileXsltToQil(object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) {
this.compilerResults = new Compiler(settings, this.enableDebug, null).Compile(stylesheet, stylesheetResolver, out this.qil);
}
/// <summary>
/// Returns the first compiler error except warnings
/// </summary>
private CompilerError GetFirstError() {
foreach (CompilerError error in compilerResults.Errors) {
if (!error.IsWarning) {
return error;
}
}
return null;
}
private void CompileQilToMsil(XsltSettings settings) {
this.command = new XmlILGenerator().Generate(this.qil, /*typeBuilder:*/null);
this.outputSettings = this.command.StaticData.DefaultWriterSettings;
this.qil = null;
}
//------------------------------------------------
// Compile stylesheet to a TypeBuilder
//------------------------------------------------
private static volatile ConstructorInfo GeneratedCodeCtor;
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public static CompilerErrorCollection CompileToType(XmlReader stylesheet, XsltSettings settings, XmlResolver stylesheetResolver, bool debug, TypeBuilder typeBuilder, string scriptAssemblyPath) {
if (stylesheet == null)
throw new ArgumentNullException("stylesheet");
if (typeBuilder == null)
throw new ArgumentNullException("typeBuilder");
if (settings == null)
settings = XsltSettings.Default;
if (settings.EnableScript && scriptAssemblyPath == null)
throw new ArgumentNullException("scriptAssemblyPath");
if (scriptAssemblyPath != null)
scriptAssemblyPath = Path.GetFullPath(scriptAssemblyPath);
QilExpression qil;
CompilerErrorCollection errors = new Compiler(settings, debug, scriptAssemblyPath).Compile(stylesheet, stylesheetResolver, out qil).Errors;
if (!errors.HasErrors) {
// Mark the type with GeneratedCodeAttribute to identify its origin
if (GeneratedCodeCtor == null)
GeneratedCodeCtor = typeof(GeneratedCodeAttribute).GetConstructor(new Type[] { typeof(string), typeof(string) });
typeBuilder.SetCustomAttribute(new CustomAttributeBuilder(GeneratedCodeCtor,
new object[] { typeof(XslCompiledTransform).FullName, Version }));
new XmlILGenerator().Generate(qil, typeBuilder);
}
return errors;
}
//------------------------------------------------
// Load compiled stylesheet from a Type
//------------------------------------------------
public void Load(Type compiledStylesheet) {
Reset();
if (compiledStylesheet == null)
throw new ArgumentNullException("compiledStylesheet");
object[] customAttrs = compiledStylesheet.GetCustomAttributes(typeof(GeneratedCodeAttribute), /*inherit:*/false);
GeneratedCodeAttribute generatedCodeAttr = customAttrs.Length > 0 ? (GeneratedCodeAttribute)customAttrs[0] : null;
// If GeneratedCodeAttribute is not there, it is not a compiled stylesheet class
if (generatedCodeAttr != null && generatedCodeAttr.Tool == typeof(XslCompiledTransform).FullName) {
if(new Version(Version).CompareTo(new Version(generatedCodeAttr.Version)) < 0) {
throw new ArgumentException(Res.GetString(Res.Xslt_IncompatibleCompiledStylesheetVersion, generatedCodeAttr.Version, Version), "compiledStylesheet");
}
FieldInfo fldData = compiledStylesheet.GetField(XmlQueryStaticData.DataFieldName, BindingFlags.Static | BindingFlags.NonPublic);
FieldInfo fldTypes = compiledStylesheet.GetField(XmlQueryStaticData.TypesFieldName, BindingFlags.Static | BindingFlags.NonPublic);
// If private fields are not there, it is not a compiled stylesheet class
if (fldData != null && fldTypes != null) {
if (System.Xml.XmlConfiguration.XsltConfigSection.EnableMemberAccessForXslCompiledTransform)
{
// Need MemberAccess reflection permission to access a private data field and create a delegate
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Assert();
}
// Retrieve query static data from the type
byte[] queryData = fldData.GetValue(/*this:*/null) as byte[];
if (queryData != null) {
MethodInfo executeMethod = compiledStylesheet.GetMethod("Execute", BindingFlags.Static | BindingFlags.NonPublic);
Type[] earlyBoundTypes = (Type[])fldTypes.GetValue(/*this:*/null);
// Load the stylesheet
Load(executeMethod, queryData, earlyBoundTypes);
return;
}
}
}
// Throw an exception if the command was not loaded
if (this.command == null)
throw new ArgumentException(Res.GetString(Res.Xslt_NotCompiledStylesheet, compiledStylesheet.FullName), "compiledStylesheet");
}
public void Load(MethodInfo executeMethod, byte[] queryData, Type[] earlyBoundTypes) {
Reset();
if (executeMethod == null)
throw new ArgumentNullException("executeMethod");
if (queryData == null)
throw new ArgumentNullException("queryData");
// earlyBoundTypes may be null
if (!System.Xml.XmlConfiguration.XsltConfigSection.EnableMemberAccessForXslCompiledTransform)
{
// make sure we have permission to create the delegate if the type is not visible.
// If the declaring type is null we cannot check for visibility.
// NOTE: a DynamicMethod will always have a DeclaringType == null. DynamicMethods will do demand on their own if skipVisibility is true.
if (executeMethod.DeclaringType != null && !executeMethod.DeclaringType.IsVisible)
{
new ReflectionPermission(ReflectionPermissionFlag.MemberAccess).Demand();
}
}
DynamicMethod dm = executeMethod as DynamicMethod;
Delegate delExec = (dm != null) ? dm.CreateDelegate(typeof(ExecuteDelegate)) : Delegate.CreateDelegate(typeof(ExecuteDelegate), executeMethod);
this.command = new XmlILCommand((ExecuteDelegate)delExec, new XmlQueryStaticData(queryData, earlyBoundTypes));
this.outputSettings = this.command.StaticData.DefaultWriterSettings;
}
//------------------------------------------------
// Transform methods which take an IXPathNavigable
//------------------------------------------------
public void Transform(IXPathNavigable input, XmlWriter results) {
CheckArguments(input, results);
Transform(input, (XsltArgumentList)null, results, XsltConfigSection.CreateDefaultResolver());
}
public void Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results) {
CheckArguments(input, results);
Transform(input, arguments, results, XsltConfigSection.CreateDefaultResolver());
}
public void Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results) {
CheckArguments(input, results);
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(input, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
public void Transform(IXPathNavigable input, XsltArgumentList arguments, Stream results) {
CheckArguments(input, results);
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(input, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
//------------------------------------------------
// Transform methods which take an XmlReader
//------------------------------------------------
public void Transform(XmlReader input, XmlWriter results) {
CheckArguments(input, results);
Transform(input, (XsltArgumentList)null, results, XsltConfigSection.CreateDefaultResolver());
}
public void Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results) {
CheckArguments(input, results);
Transform(input, arguments, results, XsltConfigSection.CreateDefaultResolver());
}
public void Transform(XmlReader input, XsltArgumentList arguments, TextWriter results) {
CheckArguments(input, results);
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(input, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
public void Transform(XmlReader input, XsltArgumentList arguments, Stream results) {
CheckArguments(input, results);
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(input, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
//------------------------------------------------
// Transform methods which take a uri
// SxS Note: Annotations should propagate to the caller to have him either check that
// the passed URIs are SxS safe or decide that they don't have to be SxS safe and
// suppress the message.
//------------------------------------------------
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Transform(string inputUri, XmlWriter results) {
CheckArguments(inputUri, results);
using (XmlReader reader = XmlReader.Create(inputUri, ReaderSettings)) {
Transform(reader, (XsltArgumentList)null, results, XsltConfigSection.CreateDefaultResolver());
}
}
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Transform(string inputUri, XsltArgumentList arguments, XmlWriter results) {
CheckArguments(inputUri, results);
using (XmlReader reader = XmlReader.Create(inputUri, ReaderSettings)) {
Transform(reader, arguments, results, XsltConfigSection.CreateDefaultResolver());
}
}
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Transform(string inputUri, XsltArgumentList arguments, TextWriter results) {
CheckArguments(inputUri, results);
using (XmlReader reader = XmlReader.Create(inputUri, ReaderSettings))
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(reader, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Transform(string inputUri, XsltArgumentList arguments, Stream results) {
CheckArguments(inputUri, results);
using (XmlReader reader = XmlReader.Create(inputUri, ReaderSettings))
using (XmlWriter writer = XmlWriter.Create(results, OutputSettings)) {
Transform(reader, arguments, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
[SuppressMessage("Microsoft.Design", "CA1054:UriParametersShouldNotBeStrings")]
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
public void Transform(string inputUri, string resultsFile) {
if (inputUri == null)
throw new ArgumentNullException("inputUri");
if (resultsFile == null)
throw new ArgumentNullException("resultsFile");
// SQLBUDT 276415: Prevent wiping out the content of the input file if the output file is the same
using (XmlReader reader = XmlReader.Create(inputUri, ReaderSettings))
using (XmlWriter writer = XmlWriter.Create(resultsFile, OutputSettings)) {
Transform(reader, (XsltArgumentList)null, writer, XsltConfigSection.CreateDefaultResolver());
writer.Close();
}
}
//------------------------------------------------
// Main Transform overloads
//------------------------------------------------
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Transform(XmlReader input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver) {
CheckArguments(input, results);
CheckCommand();
this.command.Execute((object)input, documentResolver, arguments, results);
}
// SxS: This method does not take any resource name and does not expose any resources to the caller.
// It's OK to suppress the SxS warning.
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
[ResourceExposure(ResourceScope.None)]
public void Transform(IXPathNavigable input, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver) {
CheckArguments(input, results);
CheckCommand();
this.command.Execute((object)input.CreateNavigator(), documentResolver, arguments, results);
}
//------------------------------------------------
// Helper methods
//------------------------------------------------
private static void CheckArguments(object input, object results) {
if (input == null)
throw new ArgumentNullException("input");
if (results == null)
throw new ArgumentNullException("results");
}
private static void CheckArguments(string inputUri, object results) {
if (inputUri == null)
throw new ArgumentNullException("inputUri");
if (results == null)
throw new ArgumentNullException("results");
}
private void CheckCommand() {
if (this.command == null) {
throw new InvalidOperationException(Res.GetString(Res.Xslt_NoStylesheetLoaded));
}
}
//------------------------------------------------
// Test suites entry points
//------------------------------------------------
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
private QilExpression TestCompile(object stylesheet, XsltSettings settings, XmlResolver stylesheetResolver) {
Reset();
CompileXsltToQil(stylesheet, settings, stylesheetResolver);
return qil;
}
private void TestGenerate(XsltSettings settings) {
Debug.Assert(qil != null, "You must compile to Qil first");
CompileQilToMsil(settings);
}
[ResourceConsumption(ResourceScope.Machine)]
[ResourceExposure(ResourceScope.Machine)]
private void Transform(string inputUri, XsltArgumentList arguments, XmlWriter results, XmlResolver documentResolver) {
command.Execute(inputUri, documentResolver, arguments, results);
}
internal static void PrintQil(object qil, XmlWriter xw, bool printComments, bool printTypes, bool printLineInfo) {
QilExpression qilExpr = (QilExpression)qil;
QilXmlWriter.Options options = QilXmlWriter.Options.None;
QilValidationVisitor.Validate(qilExpr);
if (printComments) options |= QilXmlWriter.Options.Annotations;
if (printTypes) options |= QilXmlWriter.Options.TypeInfo;
if (printLineInfo) options |= QilXmlWriter.Options.LineInfo;
QilXmlWriter qw = new QilXmlWriter(xw, options);
qw.ToXml(qilExpr);
xw.Flush();
}
}
#endif // ! HIDE_XSL
}
|