Project

Profile

Help

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

he / latest9.9 / hec / samples / cppTests / testValidator.cpp @ 5401a5ae

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

    
6
using namespace std;
7

    
8
void testValidator1(SchemaValidator * val){
9
        cout<<endl<<"Test Validate Schema from string"<<endl;        
10
 string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
11
 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>";
12

    
13

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

    
18
}
19

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

    
25
  cout<<endl<<"Test 2: Validate Schema from string"<<endl;        
26
  string invalid_xml = "<?xml version='1.0'?><request><a/><!--comment--></request>";
27
  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>";
28

    
29

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

    
32
         XdmNode * input = processor->parseXmlFromString(doc1.c_str());
33

    
34
        val->setSourceNode(input);
35

    
36
        
37
        
38
        val->registerSchemaFromString(sch1);
39

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

    
45
        delete val;
46

    
47
}
48

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

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

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

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

    
82
}
83

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

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

    
104
}
105

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

    
114

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

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

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

    
124
        val->registerSchemaFromString(sch1);
125

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

    
138

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

    
147

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

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

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

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

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

    
165
int main()
166
{
167

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

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

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