Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XsltApiV2.cs « XslCompiledTransformApi « Xslt « tests « System.Private.Xml « src - github.com/mono/corefx.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14d16ecbf28178786bc349034f24face9a8c32c2 (plain)
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Xunit;
using Xunit.Abstractions;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.XmlDiff;
using System.Xml.XPath;
using System.Xml.Xsl;
using XmlCoreTest.Common;
using OLEDB.Test.ModuleCore;

namespace System.Xml.Tests
{
    public enum OutputType
    {
        URI, Stream, Writer, TextWriter
    }

    public enum XslInputType
    {
        Reader, URI, Navigator
    }

    public enum XmlInputType
    {
        Reader, URI, Navigator
    }

    public enum NavType
    {
        XmlDocument, DataDocument, XPathDocument, Unknown
    }

    ////////////////////////////////////////////////////////////////
    // Module declaration for LTM usage
    //
    ////////////////////////////////////////////////////////////////
    //[TestModule(Name = "XsltApiV2", Desc = "XslCompiledTransform API Tests", Pri = 1)]
    public class XslCompiledTransformModule : CTestModule
    {
        public override int Init(object objParam)
        {
            int ret = base.Init(objParam);

            // These steps use to be part of javascript files and these were copied and executed as test setup step.
            // I belive this to be a much better way of accomplishing the same task.
            // Logic from CreateApiTestFiles.js
            string sourceFile = Path.Combine(FilePathUtil.GetTestDataPath(), @"XsltApiV2\xmlResolver_document_function.xml");
            string targetFile = @"c:\temp\xmlResolver_document_function.xml";
            if (!Directory.Exists(@"c:\temp"))
                Directory.CreateDirectory(@"c:\temp");
            if (!File.Exists(targetFile))
                File.Copy(sourceFile, targetFile, true);

            // Logic from CreateStrasse.js
            using (XmlWriter writer = XmlWriter.Create("Stra\u00DFe.xml"))
            {
                writer.WriteComment(" ");
            }

            return ret;
        }
    }

    ////////////////////////////////////////////////////////////////
    // Base class for test cases
    //
    ////////////////////////////////////////////////////////////////
    public class XsltApiTestCaseBase2 //: CTestCase
    {
        // Generic data for all derived test cases
        public String szXslNS = "http://www.w3.org/1999/XSL/Transform";

        public String szDefaultNS = "urn:my-object";
        public String szEmpty = "";
        public String szInvalid = "*?%(){}[]&!@#$";
        public String szLongString = "ThisIsAVeryLongStringToBeStoredAsAVariableToDetermineHowLargeThisBufferForAVariableNameCanBeAndStillFunctionAsExpected";
        public String szLongNS = "http://www.microsoft.com/this/is/a/very/long/namespace/uri/to/do/the/api/testing/for/xslt/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/0123456789/";
        public String[] szWhiteSpace = { "  ", "\n", "\t", "\r", "\t\n  \r\t" };
        public String szSimple = "myArg";

        // Variables from init string
        private string _strPath;                           // Path of the data files

        private string _httpPath;                          // Http Path of the data files
        private XslInputType _nInputXsl;                         // reader, url, or navigator
        private XmlInputType _nInputXml = XmlInputType.Reader;   // reader, url, or navigator
        private bool _fTrace;                            // Should we write out the results of the transform?
        private OutputType _nOutput;                           // Type of XSL Transform to do
        private NavType _navType;                           // Data document type
        private ReaderType _readerType;                        // Reader type

        // Other global variables
        protected string _strOutFile = "out.xml";        // File to create when using write transforms

        protected XslCompiledTransform xslt;                           // Main XslCompiledTransform instance
        protected XmlReader xrXSLT;                         // for READER transforms
        protected XsltArgumentList m_xsltArg;                      // For XsltArgumentList tests
        public object retObj;

        protected bool _isInProc;                          // Is the current test run in proc or /Host None?
        protected string _standardTests;

        private ITestOutputHelper _output;
        public XsltApiTestCaseBase2(ITestOutputHelper output)
        {
            _output = output;
            this.Init(null);
        }

        public XslInputType MyXslInputType()
        {
            return _nInputXsl;
        }

        public XmlInputType MyXmlInputType()
        {
            return _nInputXml;
        }

        public OutputType MyOutputType()
        {
            return _nOutput;
        }

        public NavType MyDocType()
        {
            return _navType;
        }

        public ReaderType MyReaderType()
        {
            return _readerType;
        }

        public OutputType GetOutputType(String s)
        {
            if (s.EndsWith(",URI"))
                return OutputType.URI;
            else if (s.EndsWith(",STREAM"))
                return OutputType.Stream;
            else if (s.EndsWith(",WRITER"))
                return OutputType.Writer;
            else if (s.EndsWith(",TEXTWRITER"))
                return OutputType.TextWriter;
            else return OutputType.Stream; //default
        }

        public XmlUrlResolver GetDefaultCredResolver()
        {
            XmlUrlResolver myDefaultCredResolver = new XmlUrlResolver();
            //myDefaultCredResolver.Credentials = CredentialCache.DefaultCredentials;

            return myDefaultCredResolver;
        }

        public XslInputType GetXslInputType(String s)
        {
            if (s.StartsWith("READER,"))
                return XslInputType.Reader;
            else if (s.StartsWith("URI,"))
                return XslInputType.URI;
            else if (s.StartsWith("NAVIGATOR,"))
                return XslInputType.Navigator;
            else
                return XslInputType.URI;
        }

        public NavType GetDocType(String s)
        {
            switch (s.ToUpper())
            {
                case "XPATHDOCUMENT":
                    return NavType.XPathDocument;

                case "XMLDOCUMENT":
                    return NavType.XmlDocument;

                case "DATADOCUMENT":
                    return NavType.DataDocument;

                default: // NavType Type not important, using default
                    return NavType.XPathDocument;
            }
        }

        public ReaderType GetReaderType(String s)
        {
            //XmlTextReader, XmlNodeReader, XmlValidatingReader, XsltReader

            switch (s.ToUpper())
            {
                case "XMLTEXTREADER":
                    return ReaderType.XmlTextReader;

                case "XMLNODEREADER":
                    return ReaderType.XmlNodeReader;

                case "XMLVALIDATINGREADER":
                    return ReaderType.XmlValidatingReader;

                case "XSLTREADER":
                    return ReaderType.XsltReader;

                default: // ReaderType Type not important, using default
                    return ReaderType.XmlValidatingReader;
            }
        }

        public String InitStringValue(String str)
        {
            return String.Empty;
        }

        public void Init(object objParam)
        {
            // Get input and transform type from attribute
            _nInputXsl = GetXslInputType(String.Empty);
            _nOutput = GetOutputType(String.Empty);

            // Get parameter info from runtime variables passed to LTM
            _fTrace = false;
            _navType = GetDocType(InitStringValue("doctype"));
            _readerType = GetReaderType(InitStringValue("readertype"));

            //This is a temporary fix to restore the baselines. Refer to Test bug #
            _strPath = Path.Combine(@"TestFiles\", FilePathUtil.GetTestDataPath(), @"XsltApiV2\");
            _httpPath = FilePathUtil.GetHttpTestDataPath() + @"/XsltApiV2/";
            _standardTests = Path.Combine(@"TestFiles\", FilePathUtil.GetHttpStandardPath() + @"/xslt10/Current/");

            return;
        }

        public String FullFilePath(String szFile)
        {
            if (szFile == null || szFile == String.Empty)
                return szFile;
            if (szFile.Length > 5)
            {
                if (szFile.Substring(0, 5) != "http:")
                    szFile = _strPath + szFile;
            }
            return szFile;
        }

        public String FullHttpPath(String szFile)
        {
            if (szFile == null || szFile == String.Empty)
                return szFile;
            szFile = _httpPath + szFile;
            return szFile;
        }

        protected string _expectedErrorCode;

        // --------------------------------------------------------------------------------------------------------------
        //  SetExpectedError
        //  -------------------------------------------------------------------------------------------------------------
        public void SetExpectedError(string errorCode)
        {
            _expectedErrorCode = errorCode;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  CheckExpectedError
        //  -------------------------------------------------------------------------------------------------------------
        public void CheckExpectedError(Exception ex, string assembly)
        {
            CExceptionHandler handler = new CExceptionHandler(_strPath + "exceptions.xml", assembly, _output);
            bool result = handler.VerifyException(ex);
            if (handler.res != _expectedErrorCode)
            {
                _output.WriteLine("Expected Exception : {0}", _expectedErrorCode);
                _output.WriteLine("Actual Exception : {0}", handler.res);
                Assert.True(false);
            }
            if (!result)
            {
                Assert.True(false);
            }
            return;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  CheckExpectedError
        //  -------------------------------------------------------------------------------------------------------------
        public void CheckExpectedError(Exception ex, string assembly, string res, string[] strParams)
        {
            CExceptionHandler handler = new CExceptionHandler(_strPath + "exceptions.xml", assembly, _output);
            if (!handler.VerifyException(ex, res, strParams))
            {
                Assert.True(false);
            }
            return;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  LoadXML
        //  -------------------------------------------------------------------------------------------------------------
        public IXPathNavigable LoadXML(String strFileLocation, NavType _navType)
        {
            switch (_navType)
            {
                case NavType.XmlDocument:
                    XmlDocument xmlDocument = new XmlDocument();
                    xmlDocument.Load(strFileLocation);
                    return (IXPathNavigable)xmlDocument;

                case NavType.XPathDocument:
                    XPathDocument xPathDocument = new XPathDocument(strFileLocation);
                    return (IXPathNavigable)xPathDocument;

                default:
                    return null;
            }
        }

        // --------------------------------------------------------------------------------------------------------------
        //  LoadXSL
        //  -------------------------------------------------------------------------------------------------------------
        public int LoadXSL(String _strXslFile)
        {
            return LoadXSL(_strXslFile, _nInputXsl, new XmlUrlResolver());
        }

        public int LoadXSL(String _strXslFile, XmlResolver xr)
        {
            return LoadXSL(_strXslFile, _nInputXsl, xr);
        }

        public int LoadXSL(String _strXslFile, XslInputType xslInputType, XmlResolver xr)
        {
            _strXslFile = FullFilePath(_strXslFile);
            xslt = new XslCompiledTransform();
            XmlReaderSettings xrs = null;

            switch (xslInputType)
            {
                case XslInputType.URI:
                /*                  _output.WriteLine("Loading style sheet as URI {0}", _strXslFile);
                                    xslt.Load(_strXslFile, XsltSettings.TrustedXslt, xr);
                                    break;
                 */
                case XslInputType.Reader:
                    switch (_readerType)
                    {
                        case ReaderType.XmlTextReader:
                            XmlTextReader trTemp = new XmlTextReader(_strXslFile);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlTextReader " + _strXslFile);
                                xslt.Load(trTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (trTemp != null)
                                    trTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlNodeReader:
                            XmlDocument docTemp = new XmlDocument();
                            docTemp.Load(_strXslFile);
                            XmlNodeReader nrTemp = new XmlNodeReader(docTemp);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlNodeReader " + _strXslFile);
                                xslt.Load(nrTemp);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (nrTemp != null)
                                    nrTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlValidatingReader:
                        default:
                            xrs = new XmlReaderSettings();
#pragma warning disable 0618
                            xrs.ProhibitDtd = false;
#pragma warning restore 0618
                            XmlReader xvr = XmlReader.Create(_strXslFile, xrs);

                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlValidatingReader " + _strXslFile);
                                xslt.Load(xvr, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (xvr != null)
                                    xvr.Dispose();
                            }
                            break;
                    }
                    break;

                case XslInputType.Navigator:
                    xrs = new XmlReaderSettings();
                    xrs.ValidationType = ValidationType.None;
#pragma warning disable 0618
                    xrs.ProhibitDtd = false;
#pragma warning restore 0618
                    XmlReader xrLoad = XmlReader.Create(_strXslFile, xrs);

                    XPathDocument xdTemp = new XPathDocument(xrLoad, XmlSpace.Preserve);
                    xrLoad.Dispose();
                    _output.WriteLine("Loading style sheet as Navigator " + _strXslFile);
                    xslt.Load(xdTemp, XsltSettings.TrustedXslt, xr);
                    break;
            }
            return 1;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  LoadXSL_Resolver
        //  -------------------------------------------------------------------------------------------------------------
        public int LoadXSL_Resolver(String _strXslFile, XmlResolver xr)
        {
            _strXslFile = FullFilePath(_strXslFile);
            xslt = new XslCompiledTransform();
            XmlReaderSettings xrs = null;
            switch (_nInputXsl)
            {
                case XslInputType.URI:
                    _output.WriteLine("Loading style sheet as URI " + _strXslFile);
                    xslt.Load(_strXslFile, XsltSettings.TrustedXslt, xr);
                    break;

                case XslInputType.Reader:
                    switch (_readerType)
                    {
                        case ReaderType.XmlTextReader:
                            XmlTextReader trTemp = new XmlTextReader(_strXslFile);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlTextReader " + _strXslFile);
                                xslt.Load(trTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (trTemp != null)
                                    trTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlNodeReader:
                            XmlDocument docTemp = new XmlDocument();
                            docTemp.Load(_strXslFile);
                            XmlNodeReader nrTemp = new XmlNodeReader(docTemp);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlNodeReader " + _strXslFile);
                                xslt.Load(nrTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (nrTemp != null)
                                    nrTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlValidatingReader:
                        default:
                            xrs = new XmlReaderSettings();
#pragma warning disable 0618
                            xrs.ProhibitDtd = false;
#pragma warning restore 0618
                            XmlReader vrTemp = XmlReader.Create(_strXslFile, xrs);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlValidatingReader " + _strXslFile);
                                xslt.Load(vrTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (vrTemp != null)
                                    vrTemp.Dispose();
                            }
                            break;
                    }
                    break;

                case XslInputType.Navigator:
                    xrs = new XmlReaderSettings();
#pragma warning disable 0618
                    xrs.ProhibitDtd = false;
#pragma warning restore 0618
                    XmlReader xrLoad = XmlReader.Create(_strXslFile, xrs);

                    XPathDocument xdTemp = new XPathDocument(xrLoad, XmlSpace.Preserve);
                    xrLoad.Dispose();
                    _output.WriteLine("Loading style sheet as Navigator " + _strXslFile);
                    xslt.Load(xdTemp, XsltSettings.TrustedXslt, xr);
                    break;
            }
            return 1;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  LoadXSL_Resolver_Evidence
        //  -------------------------------------------------------------------------------------------------------------
        /*public int LoadXSL_Resolver_Evidence(String _strXslFile, XmlResolver xr, Evidence e)
        {
            _strXslFile = FullFilePath(_strXslFile);
            xslt = new XslCompiledTransform();

            switch (_nInputXsl)
            {
                case XslInputType.Reader:
                    switch (_readerType)
                    {
                        case ReaderType.XmlTextReader:
                            XmlReader trTemp = XmlReader.Create(_strXslFile);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlTextReader " + _strXslFile);
                                //xslt.Load(trTemp, xr, e); //Evidence is not supported on V2 XSLT Load
                                xslt.Load(trTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (trTemp != null)
                                    trTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlNodeReader:
                            XmlDocument docTemp = new XmlDocument();
                            docTemp.Load(_strXslFile);
                            XmlNodeReader nrTemp = new XmlNodeReader(docTemp);
                            try
                            {
                                _output.WriteLine("Loading style sheet as XmlNodeReader " + _strXslFile);
                                //xslt.Load(nrTemp, xr, e); Evidence is not supported in V2 XSLT Load
                                xslt.Load(nrTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (nrTemp != null)
                                    nrTemp.Dispose();
                            }
                            break;

                        case ReaderType.XmlValidatingReader:
                        default:
                            XmlReaderSettings xrs = new XmlReaderSettings();
#pragma warning disable 0618
                            xrs.ProhibitDtd = false;
#pragma warning restore 0618
                            XmlReader vrTemp = null;
                            try
                            {
                                vrTemp = XmlReader.Create(_strXslFile, xrs);
                                _output.WriteLine("Loading style sheet as XmlValidatingReader " + _strXslFile);
                                //xslt.Load(vrTemp, xr, e); Evidence is not supported in V2 XSLT Load
                                xslt.Load(vrTemp, XsltSettings.TrustedXslt, xr);
                            }
                            catch (Exception ex)
                            {
                                throw (ex);
                            }
                            finally
                            {
                                if (vrTemp != null)
                                    vrTemp.Dispose();
                            }
                            break;
                    }
                    break;

                case XslInputType.Navigator:
                    XmlReader xrLoad = XmlReader.Create(_strXslFile);
                    XPathDocument xdTemp = new XPathDocument(xrLoad, XmlSpace.Preserve);
                    xrLoad.Dispose();
                    _output.WriteLine("Loading style sheet as Navigator " + _strXslFile);
                    xslt.Load(xdTemp.CreateNavigator(), XsltSettings.TrustedXslt, xr);
                    break;
            }
            return 1;
        }*/

        //VerifyResult
        public void VerifyResult(string expectedValue)
        {
            XmlDiff.XmlDiff xmldiff = new XmlDiff.XmlDiff();
            xmldiff.Option = XmlDiffOption.InfosetComparison | XmlDiffOption.IgnoreEmptyElement;

            StreamReader sr = new StreamReader(new FileStream("out.xml", FileMode.Open, FileAccess.Read));
            string actualValue = sr.ReadToEnd();
            sr.Dispose();

            //Output the expected and actual values
            _output.WriteLine("Expected : " + expectedValue);
            _output.WriteLine("Actual : " + actualValue);

            //Load into XmlTextReaders
            XmlTextReader tr1 = new XmlTextReader("out.xml");
            XmlTextReader tr2 = new XmlTextReader(new StringReader(expectedValue));

            bool bResult = xmldiff.Compare(tr1, tr2);

            //Close the readers
            tr1.Dispose();
            tr2.Dispose();

            if (bResult)
                return;
            else
                Assert.True(false);
        }

        //VerifyResult which compares 2 arguments using XmlDiff.
        public void VerifyResult(string baseline, string outputFile)
        {
            bool bResult = false;
            FileStream fsExpected;

            baseline = FullFilePath(baseline);

            XmlDiff.XmlDiff diff = new XmlDiff.XmlDiff();
            diff.Option = XmlDiffOption.IgnoreEmptyElement | XmlDiffOption.IgnoreAttributeOrder | XmlDiffOption.InfosetComparison | XmlDiffOption.IgnoreWhitespace;
            XmlParserContext context = new XmlParserContext(new NameTable(), null, "", XmlSpace.None);

            fsExpected = new FileStream(baseline, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream fsActual = new FileStream(outputFile, FileMode.Open, FileAccess.Read, FileShare.Read);

            _output.WriteLine("Verifying o/p with baseline result {0}...", baseline);
            try
            {
                bResult = diff.Compare(new XmlTextReader(fsActual, XmlNodeType.Element, context), new XmlTextReader(fsExpected, XmlNodeType.Element, context));
            }
            catch (Exception e)
            {
                // TO DO: Write exception msgs in ignore tags
                _output.WriteLine(e.ToString());
            }
            finally
            {
                fsExpected.Dispose();
                fsActual.Dispose();
            }
            if (!bResult)
            {
                // Write out the actual and expected o/p
                _output.WriteLine("Expected o/p: ");
                using (StreamReader sr = new StreamReader(new FileStream(baseline, FileMode.Open, FileAccess.Read)))
                {
                    string baseLine = sr.ReadToEnd();
                    _output.WriteLine(baseLine);
                }
                _output.WriteLine("Actual o/p: ");
                using (StreamReader sr = new StreamReader(new FileStream(outputFile, FileMode.Open, FileAccess.Read)))
                {
                    string output = sr.ReadToEnd();
                    _output.WriteLine(output);
                }

                using (StreamWriter sw = new StreamWriter(new FileStream("diff.xml", FileMode.Open, FileAccess.Read)))
                {
                    sw.WriteLine("<?xml-stylesheet href='diff.xsl' type='text/xsl'?>");
                    sw.WriteLine(diff.ToXml());
                }
            }

            if (bResult)
                return;
            else
            {
                _output.WriteLine("**** Baseline mis-matched ****");
                Assert.True(false);
            }
        }

        // --------------------------------------------------------------------------------------------------------------
        //  Transform
        //  -------------------------------------------------------------------------------------------------------------

        public int Transform(String szXmlFile)
        {
            // Default value of errorCase is false
            return (Transform(szXmlFile, false));
        }

        public int Transform(String szXmlFile, bool errorCase)
        {
            szXmlFile = FullFilePath(szXmlFile);

            _output.WriteLine("Loading XML " + szXmlFile);
            IXPathNavigable xd = LoadXML(szXmlFile, _navType);

            _output.WriteLine("Executing transform");
            xrXSLT = null;
            Stream strmTemp = null;
            switch (_nOutput)
            {
                case OutputType.Stream:
                    try
                    {
                        strmTemp = new FileStream(_strOutFile, FileMode.Create, FileAccess.ReadWrite);
                        xslt.Transform(xd, null, strmTemp);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (strmTemp != null)
                            strmTemp.Dispose();
                    }
                    break;

                case OutputType.Writer:
                    XmlWriterSettings xws = new XmlWriterSettings();
                    xws.Encoding = Encoding.UTF8;
                    XmlWriter xw = null;
                    try
                    {
                        xw = XmlWriter.Create(_strOutFile, xws);
                        xw.WriteStartDocument();
                        xslt.Transform(xd, null, xw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (xw != null)
                            xw.Dispose();
                    }
                    break;

                case OutputType.TextWriter:
                    TextWriter tw = null;
                    try
                    {
                        tw = new StreamWriter(new FileStream(_strOutFile, FileMode.Create, FileAccess.Write), Encoding.UTF8);
                        xslt.Transform(xd, null, tw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (tw != null)
                            tw.Dispose();
                    }
                    break;
            }
            return 1;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  Transform_ArgList
        //  -------------------------------------------------------------------------------------------------------------

        public int Transform_ArgList(String szXmlFile)
        {
            // Default value of errorCase is false
            return (Transform_ArgList(szXmlFile, false));
        }

        public int Transform_ArgList(String szXmlFile, bool errorCase)
        {
            szXmlFile = FullFilePath(szXmlFile);

            _output.WriteLine("Loading XML " + szXmlFile);
            IXPathNavigable xd = LoadXML(szXmlFile, _navType);

            _output.WriteLine("Executing transform");
            xrXSLT = null;
            Stream strmTemp = null;
            switch (_nOutput)
            {
                case OutputType.Stream:
                    try
                    {
                        strmTemp = new FileStream(_strOutFile, FileMode.Create, FileAccess.ReadWrite);
                        xslt.Transform(xd, m_xsltArg, strmTemp);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (strmTemp != null)
                            strmTemp.Dispose();
                    }
                    break;

                case OutputType.Writer:
                    XmlWriter xw = null;
                    try
                    {
                        xw = new XmlTextWriter(_strOutFile, Encoding.UTF8);
                        xw.WriteStartDocument();
                        xslt.Transform(xd, m_xsltArg, xw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (xw != null)
                            xw.Dispose();
                    }
                    break;

                case OutputType.TextWriter:
                    TextWriter tw = null;
                    try
                    {
                        tw = new StreamWriter(new FileStream(_strOutFile, FileMode.Create, FileAccess.Write), Encoding.UTF8);
                        xslt.Transform(xd, m_xsltArg, tw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (tw != null)
                        {
                            tw.Dispose();
                        }
                    }
                    break;
            }
            return 1;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  TransformResolver
        //  -------------------------------------------------------------------------------------------------------------

        public int TransformResolver(String szXmlFile, XmlResolver xr)
        {
            // Default value of errorCase is false
            return (TransformResolver(szXmlFile, xr, false));
        }

        public int TransformResolver(String szXmlFile, XmlResolver xr, bool errorCase)
        {
            szXmlFile = FullFilePath(szXmlFile);

            _output.WriteLine("Loading XML " + szXmlFile);
            IXPathNavigable xd = LoadXML(szXmlFile, _navType);

            _output.WriteLine("Executing transform");
            xrXSLT = null;
            Stream strmTemp = null;

            switch (_nOutput)
            {
                case OutputType.Stream:
                    try
                    {
                        strmTemp = new FileStream(_strOutFile, FileMode.Create, FileAccess.ReadWrite);
                        xslt.Transform(xd, null, strmTemp);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (strmTemp != null)
                            strmTemp.Dispose();
                    }
                    break;

                case OutputType.Writer:
                    XmlWriter xw = null;
                    try
                    {
                        xw = new XmlTextWriter(_strOutFile, Encoding.UTF8);
                        xw.WriteStartDocument();
                        xslt.Transform(xd, null, xw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (xw != null)
                            xw.Dispose();
                    }
                    break;

                case OutputType.TextWriter:
                    TextWriter tw = null;
                    try
                    {
                        tw = new StreamWriter(new FileStream(_strOutFile, FileMode.Create, FileAccess.Write), Encoding.UTF8);
                        xslt.Transform(xd, null, tw);
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (tw != null)
                            tw.Dispose();
                    }
                    break;
            }
            return 1;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  CheckResult
        //  -------------------------------------------------------------------------------------------------------------
        public int CheckResult(double szExpResult)
        {
            double checksumActual;
            CXsltChecksum check = new CXsltChecksum(_fTrace, _output);

            if (_nOutput == OutputType.URI)
                checksumActual = check.Calc(xrXSLT);
            else
                checksumActual = check.Calc(_strOutFile);

            if (szExpResult != checksumActual || _fTrace)
            {
                _output.WriteLine("XML: {0}", check.Xml);
                _output.WriteLine("Actual checksum: {0}, Expected: {1}", checksumActual, szExpResult);
            }
            if (szExpResult != checksumActual)
                return 0;

            return 1;
        }

        public int CheckResult(string expResult)
        {
            double actChecksum, expChecksum;
            CXsltChecksum check = new CXsltChecksum(_fTrace, _output);

            // Let's make sure we use the same checksum calculating function for
            // actual and expected so we know we are comparing apples to apples.
            if (_nOutput == OutputType.URI)
            {
                expChecksum = check.Calc(XmlReader.Create(new StringReader(expResult)));
                actChecksum = check.Calc(xrXSLT);
            }
            else
            {
                using (StreamWriter sw = new StreamWriter(new FileStream(Path.Combine(Directory.GetCurrentDirectory(), "expectdchecksum.xml"), FileMode.Create, FileAccess.Write)))
                {
                    sw.Write(expResult);
                }
                expChecksum = check.Calc("expectdchecksum.xml");
                actChecksum = check.Calc(_strOutFile);
            }

            if (expChecksum != actChecksum || _fTrace)
            {
                _output.WriteLine("Act Xml: {0}", check.Xml);
                _output.WriteLine("Exp Xml: {0}", expResult);
                _output.WriteLine("Actual checksum: {0}, Expected: {1}", actChecksum, expChecksum);
            }

            if (expChecksum != actChecksum)
                return 0;

            return 1;
        }
    }

    internal class CExceptionHandler
    {
        private XPathDocument _doc;
        private XPathNavigator _nav;
        public string msg;
        public string res;
        private ExceptionVerifier _exVer;

        private ITestOutputHelper _output;

        public CExceptionHandler(string strXmlFile, string ns, ITestOutputHelper output)
        {
            _exVer = new ExceptionVerifier(ns, ExceptionVerificationFlags.IgnoreMultipleDots, _output);

            _doc = new XPathDocument(strXmlFile);
            _nav = ((IXPathNavigable)_doc).CreateNavigator();

            _output = output;
        }

        // --------------------------------------------------------------------------------------------------------------
        //  VerifyException
        //  -------------------------------------------------------------------------------------------------------------
        public bool VerifyException(Exception ex)
        {
            Type _type = ex.GetType();
            res = String.Empty;//(string)_type.InvokeMember("res", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, ex, null);
            msg = (string)_nav.Evaluate("string(/exceptions/exception [@res = '" + res + "']/@message)");
            try
            {
                _exVer.IsExceptionOk(ex, res);
                return true;
            }
            catch (Exception exp)
            {
                _output.WriteLine(exp.Message);
                return false;
            }
        }

        public bool VerifyException(Exception ex, string res, string[] strParams)
        {
            //msg = (string)nav.Evaluate("string(/exceptions/exception [@res = '"+res+"']/@message)");
            try
            {
                _exVer.IsExceptionOk(ex, res, strParams);
                return true;
            }
            catch (Exception exp)
            {
                _output.WriteLine(exp.Message);
                return false;
            }
        }
    }
}