Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C#

Visual Studio 2010 Add-in that Adds Diff Tools, Web Project Reporting and Some Subversion Support

Rate me:
Please Sign up or sign in to vote.
4.56/5 (13 votes)
12 May 2010CPOL4 min read 162.9K   2.7K   98  
With this add-in, you get new tools and commands that boost your productivity while developing, and some helpful reports especially for web projects - Version 2.2
/// ReportXSLT.cs: Visual Studio Add-in to test xslt transformations.
/// Created by Matthias Hertel, see http://www.mathertel.de
/// This work is licensed under a Creative Commons Attribution 2.0 Germany License.
/// see http://creativecommons.org/licenses/by/2.0/de/

using Extensibility;
using EnvDTE;

using System;
using System.Collections;
using System.IO;
using System.Text;
using System.Web;

using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace WebReports8 {

  /// <summary>Summary description for ReportXSLT.</summary>
  public class ReportXSLT : Report {

    /// <summary>Create a ReportXSLT object.</summary>
    public ReportXSLT() {
    } // ReportXSLT

    /// <summary>Number of arguments this report needs to run.</summary>
    public override int Arguments {
      get { return (2); }
    }

    /// <summary>Do the analysis by reading 2 files, compare them and render html to the output.</summary>
    /// <param name="applicationObject">reference to the VS.NET</param>
    /// <param name="output">ouput stream</param>
    /// <param name="args">List of all arguments passed to this command.</param>
    public override void Render(_DTE applicationObject, StringWriter output, params string[] args) {
      StringBuilder html;
      string xsltFileName = args[0];
      string dataFileName = args[1];
      XslCompiledTransform xTrans;
      XPathDocument xDoc;

      // swap filenames
      if ((! xsltFileName.ToLower().EndsWith(".xslt")) && (! xsltFileName.ToLower().EndsWith(".xsl"))) {
        dataFileName = args[0];
        xsltFileName = args[1];
      } // if

      // load the data (and maybe throw an exception)
      try {
        xDoc = new XPathDocument(dataFileName);
      } catch (XmlException ex) {
        // set a usable HelpLink
        ex.HelpLink = String.Format("dte:file?file={0}&line={1}&pos={2}",
          System.Web.HttpUtility.UrlEncode(dataFileName),
          ex.LineNumber, ex.LinePosition);
        throw; //re-throw this message
      } // try

      // load the xslt-file (and maybe throw an exception)
      try {
        xTrans = new XslCompiledTransform();
        xTrans.Load(xsltFileName, XsltSettings.Default, new XmlUrlResolver());
      } catch (XmlException ex) {
        // set a usable HelpLink
        ex.HelpLink = String.Format("dte:file?file={0}&line={1}&pos={2}",
          System.Web.HttpUtility.UrlEncode(xsltFileName),
          ex.LineNumber, ex.LinePosition);
        throw; //re-throw this message
      } // try

      StringBuilder outBuffer = new StringBuilder();
      StringWriter outWriter = new StringWriter(outBuffer);

      // do the transformation (and maybe throw an exception)
      xTrans.Transform(dataFileName, null, outWriter);

      if ((args.Length > 2) && (args[2].ToLower() == "html")) {
        html = outBuffer;
      } else {

        html = new StringBuilder(ExtractHtmlResource("WebReports.ReportXSLT.htm"));

        // replace all placeholders
        html.Replace("[TITLE]",
          String.Format("{0} transformed by {1}", dataFileName, xsltFileName));
        html.Replace("[XSTLFILE]", System.Web.HttpUtility.HtmlEncode(xsltFileName));
        html.Replace("[XSTLFILEENC]", System.Web.HttpUtility.UrlEncode(xsltFileName));
        html.Replace("[DATAFILE]", System.Web.HttpUtility.HtmlEncode(dataFileName));
        html.Replace("[DATAFILEENC]", System.Web.HttpUtility.UrlEncode(dataFileName));

        html.Replace("[RESULT]", System.Web.HttpUtility.HtmlEncode(outBuffer.ToString()));
      } // if

      output.Write(html.ToString());
    } // Render


  } // class

} // namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Deutsche Bank AG
Germany Germany
see https://www.mathertel.de

Comments and Discussions