The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl \ XslTransform

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XslTransform.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Xml.Xsl
  16. {
  17.     #if ! HIDE_XSL
  18.     using System.Reflection;
  19.     using System.Diagnostics;
  20.     using System.IO;
  21.     using System.Xml.XPath;
  22.     using System.Xml.Xsl.XsltOld;
  23.     using MS.Internal.Xml.XPath;
  24.     using MS.Internal.Xml.Cache;
  25.     using System.Collections.Generic;
  26.     using System.Xml.Xsl.XsltOld.Debugger;
  27.     using System.Security.Policy;
  28.     using System.Security.Permissions;
  29.    
  30.     [Obsolete("This class has been deprecated. Please use System.Xml.Xsl.XslCompiledTransform instead. http://go.microsoft.com/fwlink/?linkid=14202")]
  31.     public sealed class XslTransform
  32.     {
  33.         private XmlResolver _XmlResolver = new XmlUrlResolver();
  34.        
  35.         //
  36.         // Compiled stylesheet state
  37.         //
  38.         private Stylesheet _CompiledStylesheet;
  39.         private List<TheQuery> _QueryStore;
  40.         private RootAction _RootAction;
  41.        
  42.         private IXsltDebugger debugger;
  43.        
  44.         public XslTransform()
  45.         {
  46.         }
  47.        
  48.         public XmlResolver XmlResolver {
  49.             set { _XmlResolver = value; }
  50.         }
  51.        
  52.         public void Load(XmlReader stylesheet)
  53.         {
  54.             Load(stylesheet, new XmlUrlResolver());
  55.         }
  56.         public void Load(XmlReader stylesheet, XmlResolver resolver)
  57.         {
  58.             Load(new XPathDocument(stylesheet, XmlSpace.Preserve), resolver);
  59.         }
  60.        
  61.         public void Load(IXPathNavigable stylesheet)
  62.         {
  63.             Load(stylesheet, new XmlUrlResolver());
  64.         }
  65.         public void Load(IXPathNavigable stylesheet, XmlResolver resolver)
  66.         {
  67.             if (stylesheet == null) {
  68.                 throw new ArgumentNullException("stylesheet");
  69.             }
  70.             Load(stylesheet.CreateNavigator(), resolver);
  71.         }
  72.        
  73.         public void Load(XPathNavigator stylesheet)
  74.         {
  75.             if (stylesheet == null) {
  76.                 throw new ArgumentNullException("stylesheet");
  77.             }
  78.             Load(stylesheet, new XmlUrlResolver());
  79.         }
  80.         public void Load(XPathNavigator stylesheet, XmlResolver resolver)
  81.         {
  82.             if (stylesheet == null) {
  83.                 throw new ArgumentNullException("stylesheet");
  84.             }
  85.             if (resolver == null) {
  86.                 resolver = new XmlNullResolver();
  87.             }
  88.                 /*evidence:*/            Compile(stylesheet, resolver, null);
  89.         }
  90.        
  91.         public void Load(string url)
  92.         {
  93.             Load(url, new XmlUrlResolver());
  94.         }
  95.         public void Load(string url, XmlResolver resolver)
  96.         {
  97.             XmlTextReaderImpl tr = new XmlTextReaderImpl(url);
  98.             {
  99.                 tr.XmlResolver = resolver;
  100.             }
  101.             Evidence evidence = XmlSecureResolver.CreateEvidenceForUrl(tr.BaseURI);
  102.             // We should ask BaseURI before we start reading because it's changing with each node
  103.             if (resolver == null) {
  104.                 resolver = new XmlNullResolver();
  105.             }
  106.             Compile(Compiler.LoadDocument(tr).CreateNavigator(), resolver, evidence);
  107.         }
  108.        
  109.         public void Load(IXPathNavigable stylesheet, XmlResolver resolver, Evidence evidence)
  110.         {
  111.             if (stylesheet == null) {
  112.                 throw new ArgumentNullException("stylesheet");
  113.             }
  114.             Load(stylesheet.CreateNavigator(), resolver, evidence);
  115.         }
  116.         public void Load(XmlReader stylesheet, XmlResolver resolver, Evidence evidence)
  117.         {
  118.             if (stylesheet == null) {
  119.                 throw new ArgumentNullException("stylesheet");
  120.             }
  121.             Load(new XPathDocument(stylesheet, XmlSpace.Preserve), resolver, evidence);
  122.         }
  123.         public void Load(XPathNavigator stylesheet, XmlResolver resolver, Evidence evidence)
  124.         {
  125.             if (stylesheet == null) {
  126.                 throw new ArgumentNullException("stylesheet");
  127.             }
  128.             if (resolver == null) {
  129.                 resolver = new XmlNullResolver();
  130.             }
  131.             if (evidence == null) {
  132.                 evidence = new Evidence();
  133.             }
  134.             else {
  135.                 new SecurityPermission(SecurityPermissionFlag.ControlEvidence).Demand();
  136.             }
  137.             Compile(stylesheet, resolver, evidence);
  138.         }
  139.        
  140.         // ------------------------------------ Transform() ------------------------------------ //
  141.        
  142.         private void CheckCommand()
  143.         {
  144.             if (_CompiledStylesheet == null) {
  145.                 throw new InvalidOperationException(Res.GetString(Res.Xslt_NoStylesheetLoaded));
  146.             }
  147.         }
  148.        
  149.         public XmlReader Transform(XPathNavigator input, XsltArgumentList args, XmlResolver resolver)
  150.         {
  151.             CheckCommand();
  152.             Processor processor = new Processor(input, args, resolver, _CompiledStylesheet, _QueryStore, _RootAction, debugger);
  153.             return processor.StartReader();
  154.         }
  155.        
  156.         public XmlReader Transform(XPathNavigator input, XsltArgumentList args)
  157.         {
  158.             return Transform(input, args, _XmlResolver);
  159.         }
  160.        
  161.         public void Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  162.         {
  163.             CheckCommand();
  164.             Processor processor = new Processor(input, args, resolver, _CompiledStylesheet, _QueryStore, _RootAction, debugger);
  165.             processor.Execute(output);
  166.         }
  167.        
  168.         public void Transform(XPathNavigator input, XsltArgumentList args, XmlWriter output)
  169.         {
  170.             Transform(input, args, output, _XmlResolver);
  171.         }
  172.         public void Transform(XPathNavigator input, XsltArgumentList args, Stream output, XmlResolver resolver)
  173.         {
  174.             CheckCommand();
  175.             Processor processor = new Processor(input, args, resolver, _CompiledStylesheet, _QueryStore, _RootAction, debugger);
  176.             processor.Execute(output);
  177.         }
  178.        
  179.         public void Transform(XPathNavigator input, XsltArgumentList args, Stream output)
  180.         {
  181.             Transform(input, args, output, _XmlResolver);
  182.         }
  183.        
  184.         public void Transform(XPathNavigator input, XsltArgumentList args, TextWriter output, XmlResolver resolver)
  185.         {
  186.             CheckCommand();
  187.             Processor processor = new Processor(input, args, resolver, _CompiledStylesheet, _QueryStore, _RootAction, debugger);
  188.             processor.Execute(output);
  189.         }
  190.        
  191.         public void Transform(XPathNavigator input, XsltArgumentList args, TextWriter output)
  192.         {
  193.             CheckCommand();
  194.             Processor processor = new Processor(input, args, _XmlResolver, _CompiledStylesheet, _QueryStore, _RootAction, debugger);
  195.             processor.Execute(output);
  196.         }
  197.        
  198.         public XmlReader Transform(IXPathNavigable input, XsltArgumentList args, XmlResolver resolver)
  199.         {
  200.             if (input == null) {
  201.                 throw new ArgumentNullException("input");
  202.             }
  203.             return Transform(input.CreateNavigator(), args, resolver);
  204.         }
  205.        
  206.         public XmlReader Transform(IXPathNavigable input, XsltArgumentList args)
  207.         {
  208.             if (input == null) {
  209.                 throw new ArgumentNullException("input");
  210.             }
  211.             return Transform(input.CreateNavigator(), args, _XmlResolver);
  212.         }
  213.         public void Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output, XmlResolver resolver)
  214.         {
  215.             if (input == null) {
  216.                 throw new ArgumentNullException("input");
  217.             }
  218.             Transform(input.CreateNavigator(), args, output, resolver);
  219.         }
  220.        
  221.         public void Transform(IXPathNavigable input, XsltArgumentList args, TextWriter output)
  222.         {
  223.             if (input == null) {
  224.                 throw new ArgumentNullException("input");
  225.             }
  226.             Transform(input.CreateNavigator(), args, output, _XmlResolver);
  227.         }
  228.        
  229.         public void Transform(IXPathNavigable input, XsltArgumentList args, Stream output, XmlResolver resolver)
  230.         {
  231.             if (input == null) {
  232.                 throw new ArgumentNullException("input");
  233.             }
  234.             Transform(input.CreateNavigator(), args, output, resolver);
  235.         }
  236.        
  237.         public void Transform(IXPathNavigable input, XsltArgumentList args, Stream output)
  238.         {
  239.             if (input == null) {
  240.                 throw new ArgumentNullException("input");
  241.             }
  242.             Transform(input.CreateNavigator(), args, output, _XmlResolver);
  243.         }
  244.        
  245.         public void Transform(IXPathNavigable input, XsltArgumentList args, XmlWriter output, XmlResolver resolver)
  246.         {
  247.             if (input == null) {
  248.                 throw new ArgumentNullException("input");
  249.             }
  250.             Transform(input.CreateNavigator(), args, output, resolver);
  251.         }
  252.        
  253.         public void Transform(IXPathNavigable input, XsltArgumentList args, XmlWriter output)
  254.         {
  255.             if (input == null) {
  256.                 throw new ArgumentNullException("input");
  257.             }
  258.             Transform(input.CreateNavigator(), args, output, _XmlResolver);
  259.         }
  260.        
  261.         public void Transform(string inputfile, string outputfile, XmlResolver resolver)
  262.         {
  263.             FileStream fs = null;
  264.             try {
  265.                 // We should read doc before creating output file in case they are the same
  266.                 XPathDocument doc = new XPathDocument(inputfile);
  267.                 fs = new FileStream(outputfile, FileMode.Create, FileAccess.ReadWrite);
  268.                     /*args:*/                Transform(doc, null, fs, resolver);
  269.             }
  270.             finally {
  271.                 if (fs != null) {
  272.                     fs.Close();
  273.                 }
  274.             }
  275.         }
  276.        
  277.         public void Transform(string inputfile, string outputfile)
  278.         {
  279.             Transform(inputfile, outputfile, _XmlResolver);
  280.         }
  281.        
  282.         // Implementation
  283.        
  284.         private void Compile(XPathNavigator stylesheet, XmlResolver resolver, Evidence evidence)
  285.         {
  286.             Debug.Assert(stylesheet != null);
  287.             Debug.Assert(resolver != null);
  288.            
  289.             Compiler compiler = (Debugger == null) ? new Compiler() : new DbgCompiler(this.Debugger);
  290.             NavigatorInput input = new NavigatorInput(stylesheet);
  291.             compiler.Compile(input, resolver, evidence);
  292.            
  293.             Debug.Assert(compiler.CompiledStylesheet != null);
  294.             Debug.Assert(compiler.QueryStore != null);
  295.             Debug.Assert(compiler.QueryStore != null);
  296.             _CompiledStylesheet = compiler.CompiledStylesheet;
  297.             _QueryStore = compiler.QueryStore;
  298.             _RootAction = compiler.RootAction;
  299.         }
  300.        
  301.         internal IXsltDebugger Debugger {
  302.             get { return this.debugger; }
  303.         }
  304.        
  305.        
  306.         internal XslTransform(object debugger)
  307.         {
  308.             if (debugger != null) {
  309.                 this.debugger = new DebuggerAddapter(debugger);
  310.             }
  311.         }
  312.        
  313.         private class DebuggerAddapter : IXsltDebugger
  314.         {
  315.             private object unknownDebugger;
  316.             private MethodInfo getBltIn;
  317.             private MethodInfo onCompile;
  318.             private MethodInfo onExecute;
  319.             public DebuggerAddapter(object unknownDebugger)
  320.             {
  321.                 this.unknownDebugger = unknownDebugger;
  322.                 BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
  323.                 Type unknownType = unknownDebugger.GetType();
  324.                 getBltIn = unknownType.GetMethod("GetBuiltInTemplatesUri", flags);
  325.                 onCompile = unknownType.GetMethod("OnInstructionCompile", flags);
  326.                 onExecute = unknownType.GetMethod("OnInstructionExecute", flags);
  327.             }
  328.             // ------------------ IXsltDebugger ---------------
  329.             public string GetBuiltInTemplatesUri()
  330.             {
  331.                 if (getBltIn == null) {
  332.                     return null;
  333.                 }
  334.                 return (string)getBltIn.Invoke(unknownDebugger, new object[] {});
  335.             }
  336.             public void OnInstructionCompile(XPathNavigator styleSheetNavigator)
  337.             {
  338.                 if (onCompile != null) {
  339.                     onCompile.Invoke(unknownDebugger, new object[] {styleSheetNavigator});
  340.                 }
  341.             }
  342.             public void OnInstructionExecute(IXsltProcessor xsltProcessor)
  343.             {
  344.                 if (onExecute != null) {
  345.                     onExecute.Invoke(unknownDebugger, new object[] {xsltProcessor});
  346.                 }
  347.             }
  348.         }
  349.     }
  350.     #endif // ! HIDE_XSL
  351. }

Developer Fusion