Project

Profile

Help

How to connect?
Download (8.35 KB) Statistics
| Branch: | Revision:

he / src / test / nunit / SaxonNUnit / SaxonNUnit / SchemaValidator.cs @ 3df04590

1
using System;
2
using System.Collections.Generic;
3
using System.Collections;
4
using System.Linq;
5
using System.IO;
6
using System.Text;
7
using Saxon.Api;
8
using NUnit.Framework;
9
using System.Xml;
10
using net.sf.saxon.tree.wrapper;
11

    
12
namespace SaxonNUnit
13
{
14
    class TestSchemaValidator
15
    {
16

    
17
        [Test]
18
        public void TestSimpleValidation()
19
        {
20
            UriBuilder ub = new UriBuilder();
21
            ub.Scheme = "file";
22
            ub.Host = "";
23
            ub.Path = @"C:\work\tests\";
24
            Uri baseUri = ub.Uri;
25

    
26
            Processor saxon = new Processor(true);
27
            //saxon.SetProperty("http://saxon.sf.net/feature/streamability", "standard");
28

    
29
            SchemaManager manager = saxon.SchemaManager;
30
            manager.ErrorList = new List<StaticError>();
31
          
32
            manager.XsdVersion = "1.1";
33

    
34
            Uri schemaUri = new Uri(baseUri, "SAFTPT.xsd");
35

    
36
            try
37
            {
38
                manager.Compile(schemaUri);
39
            }
40
            catch (Exception ex)
41
            {
42
                Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors");
43
                foreach (StaticError error in manager.ErrorList)
44
                {
45
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
46
                }
47
                return;
48
            }
49

    
50
            Saxon.Api.SchemaValidator validator = manager.NewSchemaValidator();
51
            //Uri instanceUri = new Uri(baseUri, "SAFT-PT (500086826) (20160412 172334).xml");
52

    
53
            //10 gigas
54
            Stream fs = File.OpenRead(@"C:\work\tests\SAFT.xml");
55

    
56
            //300 megas
57
            //Stream fs = File.OpenRead("SAFT-PT (500086826) (20160412 172334).xml");
58

    
59
            validator.SetSource(fs, baseUri);
60
            validator.ErrorList = new List<ValidationFailure>();
61

    
62
            Console.WriteLine("\nFile Validating file.. ");
63

    
64
            try
65
            {
66
                validator.Run();
67
            }
68
            catch (Exception ex)
69
            {
70
                Console.WriteLine(ex.StackTrace);
71

    
72
                Console.WriteLine("\nInstance validation failed with " + validator.ErrorList.Count + " errors");
73
                using (StreamWriter writetext = new StreamWriter(@"C:\work\tests\validate.txt"))
74
                {
75
                    foreach (ValidationFailure error in validator.ErrorList)
76
                    {
77
                        //Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
78
                        writetext.WriteLine("At line " + error.GetLineNumber() + ": " + error.GetMessage());
79
                    }
80

    
81
                    Console.WriteLine("\n\nException: {0}", ex.Message);
82
                }
83

    
84
            }
85

    
86
            Console.WriteLine("\nFile validation terminated!");
87
            Console.ReadLine();
88
        }
89

    
90
        [Test]
91
        public void TestInvalidityHandler()
92
        {
93

    
94
            XmlReader xsd = XmlReader.Create(new StringReader("<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" elementFormDefault=\"qualified\" attributeFormDefault=\"unqualified\">\n" +
95
               "\t<xs:element name=\"request\">\n" +
96
               "\t\t<xs:complexType>\n" +
97
               "\t\t\t<xs:sequence>\n" +
98
               "\t\t\t\t<xs:element name=\"user_name\" type=\"xs:string\"/>\n" +
99
               "\t\t\t\t<xs:element name=\"password\" type=\"xs:string\"/>\n" +
100
               "\t\t\t\t<xs:element name=\"date1\" type=\"xs:date\"/>\n" +
101
               "\t\t\t\t<xs:element name=\"date2\" type=\"xs:date\"/>\n" +
102
               "\t\t\t</xs:sequence>\n" +
103
               "\t\t</xs:complexType>\n" +
104
               "\t</xs:element>\n" +
105
               "</xs:schema>"));
106

    
107
            XmlReader source_xml = XmlReader.Create(new StringReader("<?xml version='1.0'?><request><user_name>ed</user_name><password>sdsd</password><date1>a2009-01-01</date1><date2>b2009-01-01</date2></request>"));
108

    
109
            UriBuilder ub = new UriBuilder();
110
            ub.Scheme = "file";
111
            ub.Host = "";
112
            ub.Path = @"C:\work\tests\";
113
            Uri baseUri = ub.Uri;
114

    
115
            Processor saxon = new Processor(true);
116

    
117

    
118
            SchemaManager manager = saxon.SchemaManager;
119
            manager.ErrorList = new List<StaticError>();
120
            manager.XsdVersion = "1.1";
121

    
122

    
123
            try
124
            {
125
                manager.Compile(xsd);
126
            }
127
            catch (Exception ex)
128
            {
129
                Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors");
130
                foreach (StaticError error in manager.ErrorList)
131
                {
132
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
133
                }
134
                return;
135
            }
136

    
137
            SchemaValidator validator = manager.NewSchemaValidator();
138
            validator.SetInvalidityHandler(new MyInvalidaityHandler());
139

    
140
            
141

    
142
            validator.SetSource(source_xml);
143
//            validator.ErrorList = new ArrayList();
144

    
145
            Console.WriteLine("\nFile Validating file.. ");
146

    
147
            try
148
            {
149
                validator.Run();
150
            }
151
            catch (StaticError ex)
152
            {
153
                Console.WriteLine(ex.StackTrace);
154
               
155
            }
156
        }
157

    
158
        public class MyInvalidaityHandler : IInvalidityHandler
159
        {
160
            public XdmValue endReporting()
161
            {
162
                return null;
163
            }
164

    
165
            public void reportInvalidity(ValidationFailure i)
166
            {
167
                Console.WriteLine(i.GetMessage());
168
            }
169

    
170
            public void startReporting(string systemId)
171
            {
172
                
173
            }
174
        }
175

    
176
        //throws out of memory exception
177
        [Test]
178
        public void SchemaValdiatorPerf() {
179
            // Load a schema
180

    
181
            Processor processor = new Processor(true);
182
        processor.SetProperty("http://saxon.sf.net/feature/timing", "true");
183
            processor.SetProperty("http://saxon.sf.net/feature/validation-warnings", "false"); //Set to true to suppress the exception
184
            SchemaManager manager = processor.SchemaManager;
185
        manager.XsdVersion = "1.1";
186
            manager.ErrorList = new List<StaticError>();
187

    
188
            UriBuilder ub = new UriBuilder();
189
            ub.Scheme = "file";
190
            ub.Host = "";
191
            ub.Path = @"C:\work\repositories\AHSAMedicalAgreements-v0030z\";
192
            Uri baseUri = ub.Uri;
193

    
194
            
195
            Uri schemaUri = new Uri(baseUri, "AHSAMedicalAgreements-v0030z.xsd");
196

    
197
            try
198
            {
199
                manager.Compile(schemaUri);
200
            }
201
            catch (Exception e)
202
            {
203
                Console.WriteLine(e);
204
                Console.WriteLine("Schema compilation failed with " + manager.ErrorList.Count + " errors");
205
                foreach (StaticError error in manager.ErrorList)
206
                {
207
                    Console.WriteLine("At line " + error.LineNumber + ": " + error.Message);
208
                }
209
                Assert.Fail();
210
            }
211

    
212

    
213
            // Use this to validate an instance document
214

    
215
            SchemaValidator validator = manager.NewSchemaValidator();
216

    
217
            XmlReaderSettings settings = new XmlReaderSettings();
218
            settings.DtdProcessing = DtdProcessing.Ignore;
219
            String inputFileName = new Uri(baseUri, "Providers-H-20200403 MASKED.xml").ToString();
220
            XmlReader xmlReader = XmlReader.Create(inputFileName, settings);
221
            validator.SetSource(xmlReader);
222
            Console.WriteLine("Validating input file " + inputFileName);
223
            validator.ErrorList = new List<ValidationFailure>();
224
            XdmDestination psvi = new XdmDestination();
225
            validator.SetDestination(psvi);
226

    
227
            try
228
            {
229
                validator.Run();
230
                net.sf.saxon.trace.Instrumentation.report();
231
            }
232
            catch (Exception e)
233
            {
234
                net.sf.saxon.trace.Instrumentation.report();
235
                Console.WriteLine(e);
236
                Console.WriteLine("Instance validation failed with " + validator.ErrorList.Count + " errors");
237
                foreach (ValidationFailure error in validator.ErrorList)
238
                {
239
                    Console.WriteLine("At line " + error.GetLineNumber() + ": " + error.GetMessage());
240
                }
241
                Assert.Fail();
242
            }
243

    
244

    
245

    
246

    
247
        }
248
    }
249
}
(6-6/19)