Project

Profile

Help

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

he / src / main / c / samples / cppTests / testValidator.cpp @ 339c45ee

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
        val->setSourceNode(input);
37

    
38
        
39
        
40
        val->registerSchemaFromString(sch1);
41

    
42
        val->validate();
43
        if(val->exceptionOccurred()) {
44
                cout<<endl<<"Doc is not valid!"<<endl;
45
        }
46

    
47
        delete val;
48

    
49
}
50

    
51
void testValidator3(SaxonProcessor * processor, SchemaValidator * val){
52
  processor->exceptionClear();
53
  val->clearParameters(true);
54
  val->clearProperties();
55
  cout<<endl<<"Test 3: Validate Schema from string"<<endl;        
56
  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>";
57
        
58
        val->registerSchemaFromString(sch1);
59

    
60
        val->validate("family.xml");
61
        if(!val->exceptionOccurred()) {
62
                cout<<endl<<"Doc1 is OK"<<endl;
63
        } else {
64
                cout<<endl<<"Error: Doc reported as invalid!"<<endl;
65
        }
66
}
67

    
68
void testValidator4(SaxonProcessor * processor, SchemaValidator * val){
69
  processor->exceptionClear();
70
  val->clearParameters(true);
71
  val->clearProperties();
72
  cout<<endl<<"Test 4: Validate source file with schema file. i.e. family.xml and family.xsd"<<endl;        
73

    
74
        val->registerSchemaFromFile("family-ext.xsd");
75
       //val->registerSchema("family.xsd");
76
        val->registerSchemaFromFile("family.xsd");
77
        val->validate("family.xml");
78
        if(!val->exceptionOccurred()) {
79
                cout<<endl<<"Doc1 is OK"<<endl;
80
        } else {
81
        cout<<endl<<"Error: Doc reported as valid!"<<endl;
82
        }
83

    
84
}
85

    
86
void testValidator4a(SaxonProcessor * processor, SchemaValidator * val){
87
  processor->exceptionClear();
88
  val->clearParameters(true);
89
  val->clearProperties();
90
  cout<<endl<<"Test 4: Validate source file with schema file. i.e. family.xml and family.xsd to XdmNode"<<endl;        
91

    
92
        val->registerSchemaFromFile("family-ext.xsd");
93
       //val->registerSchema("family.xsd");
94
        val->registerSchemaFromFile("family.xsd");
95
        XdmNode * node = val->validateToNode("family.xml");
96
        if(node != NULL) {
97
                if(!val->exceptionOccurred()) {
98
                        cout<<endl<<"Doc1 is OK:"<<node->toString()<<endl;
99
                 } else {
100
                        cout<<endl<<"Error: Doc reported as valid!"<<endl;
101
                }
102
        } else {
103
        cout<<endl<<"Error: node is NULL"<<endl;
104
        }
105

    
106
}
107

    
108
void testValidator5(SaxonProcessor * processor, SchemaValidator * val){
109
  processor->exceptionClear();
110
  val->clearParameters(true);
111
  val->clearProperties();
112
  cout<<endl<<"Test 5: Validate Schema from string"<<endl;        
113
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
114
  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>";
115

    
116

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

    
119
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
120
        //val->setSourceNode(input);
121

    
122
        //val->setProperty("string", doc1.c_str());
123
        val->setProperty("xsdversion", "1.1");
124
        val->setParameter("node", (XdmValue *)input);
125

    
126
        val->registerSchemaFromString(sch1);
127

    
128
        val->setProperty("report-node", "true");
129
        //val->setProperty("report-file", "validation-report2.xml");        
130
        val->setProperty("verbose", "true");
131
        val->validate();
132
        XdmNode * node = val->getValidationReport(); 
133
        if(node != NULL) {
134
                cout<<endl<<node->size()<<"Validation Report"<<node->getStringValue()<<endl;
135
        } else {
136
                cout<<endl<<"Error: Validation Report is NULL - This should not be NULL. Probably no valid license file found."<<endl;
137
        }
138
}
139

    
140

    
141
void testValidator6(SaxonProcessor * processor, SchemaValidator * val){
142
  processor->exceptionClear();
143
  val->clearParameters(true);
144
  val->clearProperties();
145
  cout<<endl<<"Test 6: Validate Schema from string and export"<<endl;
146
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
147
  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>";
148

    
149

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

    
152
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
153
        //val->setSourceNode(input);
154

    
155
        //val->setProperty("string", doc1.c_str());
156
        val->setProperty("xsdversion", "1.1");
157

    
158
        val->registerSchemaFromString(sch1);
159
        val->exportSchema("exportedSchema.scm");
160
    const char * error = val->checkException();
161

    
162
    if(error != NULL) {
163
       cout<<endl<<"Error: Validation error: "<<error<<endl;
164
    }
165
}
166

    
167
int main()
168
{
169

    
170
        SaxonProcessor * processor = new SaxonProcessor(true);
171
       cout<<"Test: SchemaValidator with Saxon version="<<processor->version()<<endl<<endl;
172

    
173
        //processor->setConfigurationProperty("xsdversion", "1.1");
174
        
175
         SchemaValidator * validator = processor->newSchemaValidator();
176
        testValidator1(validator);
177
        testValidator2();
178

    
179
        testValidator3(processor, validator);
180
        processor->setConfigurationProperty("http://saxon.sf.net/feature/multipleSchemaImports", "on");
181
        SchemaValidator * validator2 = processor->newSchemaValidator();
182
        testValidator4(processor, validator2);
183
        testValidator4a(processor, validator2);
184
        processor->setConfigurationProperty("xsdversion", "1.1");
185
        SchemaValidator * validator3 = processor->newSchemaValidator();
186
        testValidator5(processor, validator3);
187
        testValidator6(processor, validator3);
188
        delete validator;
189
        delete validator2;
190
        delete validator3;
191
        processor->release();
192
        return 0;
193
}
(23-23/28)