XsltTestUtils.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. using System.Xml;
  2. using System.Xml.Xsl;
  3. using System.Xml.XPath;
  4. using System.Collections;
  5. using System.IO;
  6. namespace MonoTests.oasis_xslt {
  7. class EnvOptions {
  8. static readonly bool useDomStyle;
  9. static readonly bool useDomInstance;
  10. static readonly string outputDir;
  11. static readonly bool whitespaceStyle;
  12. static readonly bool whitespaceInstance;
  13. static readonly bool inverseResults;
  14. public static bool UseDomStyle {
  15. get {return useDomStyle;}
  16. }
  17. public static bool UseDomInstance {
  18. get {return useDomInstance;}
  19. }
  20. public static string OutputDir {
  21. get {return outputDir;}
  22. }
  23. public static bool WhitespaceStyle {
  24. get {return whitespaceStyle;}
  25. }
  26. public static bool WhitespaceInstance {
  27. get {return whitespaceInstance;}
  28. }
  29. public static bool InverseResults {
  30. get {return inverseResults;}
  31. }
  32. static EnvOptions () {
  33. IDictionary env = System.Environment.GetEnvironmentVariables();
  34. if (env.Contains ("XSLTTEST_DOM")) {
  35. useDomStyle = true;
  36. useDomInstance = true;
  37. }
  38. if (env.Contains ("XSLTTEST_DOMXSL"))
  39. useDomStyle = true;
  40. if (env.Contains ("XSLTTEST_DOMINSTANCE"))
  41. useDomInstance = true;
  42. if (env.Contains ("XSLTTEST_WS")) {
  43. whitespaceStyle = true;
  44. whitespaceInstance = true;
  45. }
  46. if (env.Contains ("XSLTTEST_WSXSL"))
  47. whitespaceStyle = true;
  48. if (env.Contains ("XSLTTEST_WSSRC"))
  49. whitespaceInstance = true;
  50. if (env.Contains ("XSLTTEST_INVERSE_RESULTS"))
  51. inverseResults = true;
  52. if (useDomStyle || useDomInstance)
  53. outputDir = "domresults";
  54. else
  55. outputDir = "results";
  56. }
  57. }
  58. class Helpers
  59. {
  60. public static void ReadStrings (ArrayList array, string filename)
  61. {
  62. if (!File.Exists (filename))
  63. return;
  64. using (StreamReader reader = new StreamReader (filename)) {
  65. foreach (string s_ in reader.ReadToEnd ().Split ("\n".ToCharArray ())) {
  66. string s = s_.Trim ();
  67. if (s.Length > 0)
  68. array.Add (s);
  69. }
  70. }
  71. }
  72. }
  73. class CatalogTestCase
  74. {
  75. string _stylesheet;
  76. string _srcxml;
  77. string _outfile;
  78. XmlElement _testCase;
  79. string _outputDir;
  80. public CatalogTestCase (string outputDir, XmlElement testCase)
  81. {
  82. _testCase = testCase;
  83. _outputDir = outputDir;
  84. }
  85. public bool Process ()
  86. {
  87. string relPath = GetRelPath ();
  88. string path = Path.Combine (Path.Combine ("testsuite", "TESTS"), relPath);
  89. string outputPath = Path.Combine (_outputDir, relPath);
  90. if (!Directory.Exists (outputPath))
  91. Directory.CreateDirectory (outputPath);
  92. //FIXME: this ignores negative tests. Read README if you want to fix it
  93. XmlNode scenario = _testCase.SelectSingleNode ("scenario[@operation='standard']");
  94. if (scenario == null)
  95. return false;
  96. ProcessScenario (path, outputPath, scenario);
  97. return true;
  98. }
  99. string GetRelPath ()
  100. {
  101. string filePath = _testCase.SelectSingleNode ("file-path").InnerText;
  102. string submitter = _testCase.SelectSingleNode ("./parent::test-catalog/@submitter").InnerText;
  103. if (submitter == "Lotus")
  104. return Path.Combine ("Xalan_Conformance_Tests", filePath);
  105. else if (submitter == "Microsoft")
  106. return Path.Combine ("MSFT_Conformance_Tests", filePath);
  107. else
  108. throw new System.Exception ("unknown submitter in the catalog");
  109. }
  110. void ProcessScenario (string path, string outputPath, XmlNode scenario)
  111. {
  112. string stylesheetBase = scenario.SelectSingleNode ("input-file[@role='principal-stylesheet']").InnerText;
  113. _stylesheet = Path.Combine (path, stylesheetBase);
  114. if (!File.Exists (_stylesheet)) {
  115. using (StreamWriter wr = new StreamWriter ("missing.lst", true))
  116. wr.WriteLine (_stylesheet);
  117. }
  118. _srcxml = Path.Combine (path, scenario.SelectSingleNode ("input-file[@role='principal-data']").InnerText);
  119. XmlNode outputNode = scenario.SelectSingleNode ("output-file[@role='principal']");
  120. if (outputNode != null)
  121. _outfile = Path.Combine (outputPath, outputNode.InnerText);
  122. else
  123. _outfile = null;
  124. }
  125. public string StyleSheet {
  126. get {return _stylesheet;}
  127. }
  128. public string SrcXml {
  129. get {return _srcxml;}
  130. }
  131. public string OutFile {
  132. get {return _outfile;}
  133. }
  134. }
  135. class SingleTestTransform
  136. {
  137. CatalogTestCase _testCase;
  138. public SingleTestTransform (CatalogTestCase testCase)
  139. {
  140. _testCase = testCase;
  141. }
  142. string _result;
  143. public string Result {
  144. get {return _result;}
  145. }
  146. System.Exception _exception;
  147. public System.Exception Exception {
  148. get {return _exception;}
  149. }
  150. public bool Succeeded {
  151. get {return this.Exception == null;}
  152. }
  153. public CatalogTestCase TestCase {
  154. get {return _testCase;}
  155. }
  156. XslTransform LoadTransform ()
  157. {
  158. XslTransform trans = new XslTransform ();
  159. if (EnvOptions.UseDomStyle) {
  160. XmlDocument styledoc = new XmlDocument ();
  161. if (EnvOptions.WhitespaceStyle)
  162. styledoc.PreserveWhitespace = true;
  163. styledoc.Load (_testCase.StyleSheet);
  164. trans.Load (styledoc, null, null);
  165. } else
  166. trans.Load (new XPathDocument (
  167. _testCase.StyleSheet,
  168. EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
  169. XmlSpace.Default),
  170. null, null);
  171. return trans;
  172. }
  173. IXPathNavigable LoadInput ()
  174. {
  175. XmlTextReader xtr=null;
  176. try {
  177. xtr = new XmlTextReader (_testCase.SrcXml);
  178. XmlValidatingReader xvr = new XmlValidatingReader (xtr);
  179. xvr.ValidationType = ValidationType.None;
  180. IXPathNavigable input = null;
  181. if (EnvOptions.UseDomInstance) {
  182. XmlDocument dom = new XmlDocument ();
  183. if (EnvOptions.WhitespaceInstance)
  184. dom.PreserveWhitespace = true;
  185. dom.Load (xvr);
  186. input = dom;
  187. } else {
  188. input = new XPathDocument (xvr,
  189. EnvOptions.WhitespaceStyle ? XmlSpace.Preserve :
  190. XmlSpace.Default);
  191. }
  192. return input;
  193. }
  194. finally {
  195. if (xtr!=null)
  196. xtr.Close ();
  197. }
  198. }
  199. public void RunTest ()
  200. {
  201. try {
  202. XslTransform trans = LoadTransform ();
  203. IXPathNavigable input = LoadInput ();
  204. using (StringWriter sw = new StringWriter ()) {
  205. trans.Transform (input, null, sw, null);
  206. _result = sw.ToString ();
  207. }
  208. }
  209. catch (System.Exception e) {
  210. _exception = e;
  211. }
  212. }
  213. }
  214. }