Project

Profile

Help

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

he / src / main / c / samples / cppTests / testValidator.cpp @ af4371f2

1
#include "../../Saxon.C.API/SaxonProcessor.h"
2
#include "../../Saxon.C.API/XdmValue.h"
3
#include "../../Saxon.C.API/XdmNode.h"
4
#include "CppTestUtils.h"
5

    
6
#include <string>
7

    
8
using namespace std;
9

    
10
void testValidator1(SchemaValidator * val){
11
        cout<<endl<<"Test Validate Schema from string"<<endl;        
12
 string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
13
 const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
14

    
15

    
16
        string doc1 = "<Family xmlns='http://myexample/family'><Parent>John</Parent><Child>Alice</Child></Family>";
17
        
18
        val->registerSchemaFromString(sch1);
19

    
20
}
21

    
22
void testValidator2(){
23
  SaxonProcessor * processor = new SaxonProcessor(true);
24
  processor->setConfigurationProperty("xsdversion", "1.1");
25
  SchemaValidator * val = processor->newSchemaValidator();
26

    
27
  cout<<endl<<"Test 2: Validate Schema from string"<<endl;        
28
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
29
  const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
30

    
31

    
32
        string doc1 = "<Family xmlns='http://myexample/family'><Parent>John</Parent><Child>Alice</Child></Family>";
33

    
34
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
35

    
36
         if(input == nullptr) {
37
             if(val->exceptionOccurred()) {
38
                 
39

    
40
             }
41
             val->exceptionClear();
42
             return;
43

    
44
         }
45

    
46
        val->setSourceNode(input);
47

    
48
        
49
        
50
        val->registerSchemaFromString(sch1);
51

    
52
        val->validate();
53
        if(val->exceptionOccurred()) {
54
                cout<<endl<<"Doc is not valid!"<<endl;
55
        }
56

    
57
        delete val;
58

    
59
}
60

    
61
void testValidator3(SaxonProcessor * processor, SchemaValidator * val){
62
  processor->exceptionClear();
63
  val->clearParameters(true);
64
  val->clearProperties();
65
  cout<<endl<<"Test 3: Validate Schema from string"<<endl;        
66
  const char* sch1 = "<?xml version='1.0' encoding='UTF-8'?><schema targetNamespace='http://myexample/family' xmlns:fam='http://myexample/family' xmlns='http://www.w3.org/2001/XMLSchema'><element name='FamilyMember' type='string' /><element name='Parent' type='string' substitutionGroup='fam:FamilyMember'/><element name='Child' type='string' substitutionGroup='fam:FamilyMember'/><element name='Family'><complexType><sequence><element ref='fam:FamilyMember' maxOccurs='unbounded'/></sequence></complexType></element>  </schema>";
67
        
68
        val->registerSchemaFromString(sch1);
69

    
70
        val->validate("family.xml");
71
        if(!val->exceptionOccurred()) {
72
                cout<<endl<<"Doc1 is OK"<<endl;
73
        } else {
74
                cout<<endl<<"Error: Doc reported as invalid!"<<endl;
75
        }
76
}
77

    
78
void testValidator4(SaxonProcessor * processor, SchemaValidator * val){
79
  processor->exceptionClear();
80
  val->clearParameters(true);
81
  val->clearProperties();
82
  cout<<endl<<"Test 4: Validate source file with schema file. i.e. family.xml and family.xsd"<<endl;        
83

    
84
        val->registerSchemaFromFile("family-ext.xsd");
85
       //val->registerSchema("family.xsd");
86
        val->registerSchemaFromFile("family.xsd");
87
        val->validate("family.xml");
88
        if(!val->exceptionOccurred()) {
89
                cout<<endl<<"Doc1 is OK"<<endl;
90
        } else {
91
        cout<<endl<<"Error: Doc reported as valid!"<<endl;
92
        }
93

    
94
}
95

    
96
void testValidator4a(SaxonProcessor * processor, SchemaValidator * val){
97
  processor->exceptionClear();
98
  val->clearParameters(true);
99
  val->clearProperties();
100
  cout<<endl<<"Test 4: Validate source file with schema file. i.e. family.xml and family.xsd to XdmNode"<<endl;        
101

    
102
        val->registerSchemaFromFile("family-ext.xsd");
103
       //val->registerSchema("family.xsd");
104
        val->registerSchemaFromFile("family.xsd");
105
        XdmNode * node = val->validateToNode("family.xml");
106
        if(node != NULL) {
107
                if(!val->exceptionOccurred()) {
108
                        cout<<endl<<"Doc1 is OK:"<<node->toString()<<endl;
109
                 } else {
110
                        cout<<endl<<"Error: Doc reported as valid!"<<endl;
111
                }
112
        } else {
113
        cout<<endl<<"Error: node is NULL"<<endl;
114
        }
115

    
116
}
117

    
118
void testValidator5(SaxonProcessor * processor, SchemaValidator * val){
119
  processor->exceptionClear();
120
  val->clearParameters(true);
121
  val->clearProperties();
122
  cout<<endl<<"Test 5: Validate Schema from string"<<endl;        
123
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
124
  const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
125

    
126

    
127
        string doc1 = "<request xmlns='http://myexample/family'><Parent>John</Parent><Child1>Alice</Child1></request>";
128

    
129
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
130
        //val->setSourceNode(input);
131

    
132
        //val->setProperty("string", doc1.c_str());
133
        val->setProperty("xsdversion", "1.1");
134
        val->setParameter("node", (XdmValue *)input);
135

    
136
        val->registerSchemaFromString(sch1);
137

    
138
        val->setProperty("report-node", "true");
139
        //val->setProperty("report-file", "validation-report2.xml");        
140
        val->setProperty("verbose", "true");
141
        val->validate();
142
        XdmNode * node = val->getValidationReport(); 
143
        if(node != NULL) {
144
                cout<<endl<<node->size()<<"Validation Report"<<node->getStringValue()<<endl;
145
        } else {
146
                cout<<endl<<"Error: Validation Report is NULL - This should not be NULL. Probably no valid license file found."<<endl;
147
        }
148
}
149

    
150

    
151
void testValidator6(SaxonProcessor * processor, SchemaValidator * val){
152
  processor->exceptionClear();
153
  val->clearParameters(true);
154
  val->clearProperties();
155
  cout<<endl<<"Test 6: Validate Schema from string and export"<<endl;
156
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
157
  const char* sch1 = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified' attributeFormDefault='unqualified'><xs:element name='request'><xs:complexType><xs:sequence><xs:element name='a' type='xs:string'/><xs:element name='b' type='xs:string'/></xs:sequence><xs:assert test='count(child::node()) = 3'/></xs:complexType></xs:element></xs:schema>";
158

    
159

    
160
        string doc1 = "<request xmlns='http://myexample/family'><Parent>John</Parent><Child1>Alice</Child1></request>";
161

    
162
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
163
        //val->setSourceNode(input);
164

    
165
        //val->setProperty("string", doc1.c_str());
166
        val->setProperty("xsdversion", "1.1");
167

    
168
        val->registerSchemaFromString(sch1);
169
        val->exportSchema("exportedSchema.scm");
170
    const char * error = val->checkException();
171

    
172
    if(error != NULL) {
173
       cout<<endl<<"Error: Validation error: "<<error<<endl;
174
    }
175
}
176

    
177
int main()
178
{
179

    
180
        SaxonProcessor * processor = new SaxonProcessor(true);
181
       cout<<"Test: SchemaValidator with Saxon version="<<processor->version()<<endl<<endl;
182

    
183
        //processor->setConfigurationProperty("xsdversion", "1.1");
184
        
185
         SchemaValidator * validator = processor->newSchemaValidator();
186
        testValidator1(validator);
187
        testValidator2();
188

    
189
        testValidator3(processor, validator);
190
        processor->setConfigurationProperty("http://saxon.sf.net/feature/multipleSchemaImports", "on");
191
        SchemaValidator * validator2 = processor->newSchemaValidator();
192
        testValidator4(processor, validator2);
193
        testValidator4a(processor, validator2);
194
        processor->setConfigurationProperty("xsdversion", "1.1");
195
        SchemaValidator * validator3 = processor->newSchemaValidator();
196
        testValidator5(processor, validator3);
197
        testValidator6(processor, validator3);
198
        delete validator;
199
        delete validator2;
200
        delete validator3;
201
        processor->release();
202
        return 0;
203
}
(23-23/28)