using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
namespace TFSIntegration
{
public static class TrxConverter
{
///
///
///
///
///
public static XslCompiledTransform LoadXsl(string xsltPath)
{
var transform = new XslCompiledTransform();
var settings = new XmlReaderSettings();
settings.ProhibitDtd = false;
using (XmlReader reader = XmlReader.Create(xsltPath, settings))
{
var xsltSettings = new XsltSettings();
xsltSettings.EnableDocumentFunction = true;
transform.Load(reader, xsltSettings, new XmlUrlResolver());
}
return transform;
}
///
///
///
///
public static XslCompiledTransform LoadXslFromContent()
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream rsrc = asm.GetManifestResourceStream("TFSIntegration.TRX2HTML.xslt");
XmlTextReader reader = new XmlTextReader(rsrc);
XslCompiledTransform xsltdoc = new XslCompiledTransform();
xsltdoc.Load(reader);
return xsltdoc;
}
///
///
///
/// The input .trx file
/// The output .html file
public static void Transform(string xsltPath, string input, string output)
{
XslCompiledTransform transform = LoadXsl(xsltPath);
Transform(transform, input, output);
}
///
///
///
/// The input .trx file
/// The output .html file
public static void Transform(XslCompiledTransform transform, string input, string output)
{
var writerSettings = new XmlWriterSettings();
writerSettings.ConformanceLevel = ConformanceLevel.Auto;
using (XmlWriter writer = XmlWriter.Create(output, writerSettings))
{
var arguments = new XsltArgumentList();
//arguments.AddParam("html.stylesheet", String.Empty, "styles.css");
using (XmlReader read = CreateReader(input))
{
transform.Transform(read, arguments, writer);
}
}
}
///
///
///
///
///
public static XmlReader CreateReader(string pathToTrxFile)
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false;
return XmlReader.Create(pathToTrxFile, readerSettings);
}
///
/// Finds the name of the .trx file from the process output
///
///
///
public static string GetTrxFileName(string s)
{
string trxFilePathName = "";
int startPos = s.IndexOf("Results file:") + 15;
int endPos = s.IndexOf("Test Settings:");
if (startPos > 0 && endPos > 0)
{
trxFilePathName = s.Substring(startPos, (endPos - startPos)).Trim();
}
else
{
Logger.LogEvent(String.Format("Error: {0}", "Result File Not Found in Output " + s), EventLogEntryType.Information);
return "";
}
return trxFilePathName;
}
///
/// Produce an html summary of the TRX file and copy it to the drop location
///
///
public static string ProduceHtmlTestResult(string trxFileName)
{
try
{
//Find the Test Result Name in the console output
string xsltFile = "TRX2HTML.xslt";
string htmlFilePathName = trxFileName.Replace(".trx", ".html");
if (File.Exists(trxFileName))
TrxConverter.Transform(TrxConverter.LoadXslFromContent(), trxFileName, htmlFilePathName);
else throw new FileNotFoundException(String.Format("The .trx file {0} was not found.", trxFileName));
return htmlFilePathName;
}
catch (Exception ex)
{
Logger.LogEvent(String.Format("Error: {0}", ex.ToString()), EventLogEntryType.Error);
throw;
}
}
}
}