Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXPath.cpp @ 5d25f10d

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

    
8
using namespace std;
9

    
10

    
11

    
12
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
13
void testXPathSingle(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
14

    
15
    cout<<endl<<"Test testXPathSingle:"<<endl;
16
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
17

    
18
        xpath->setContextItem((XdmItem *)input);
19
        XdmItem * result = xpath->evaluateSingle("//person[1]");
20
        
21
        if(result == nullptr) {
22
                 printf("result is null \n");
23
        SaxonApiException * exception = xpath->getException();
24
        if(exception != nullptr) {
25
            const char *message = xpath->getErrorMessage();
26
            cout << "Error Message = " << message << endl;
27
            xpath->exceptionClear();
28
        }
29
        sresult->failure++;
30
        sresult->failureList.push_back("testXPathSingle");
31

    
32
        } else {
33
                cout<<"Number of items="<<result->size()<<endl;
34
                cout<<"String Value of result="<<result->getStringValue()<<endl;
35
                sresult->success++;
36
        }
37
        delete result;
38

    
39
        
40

    
41
}
42

    
43
// Test case on the evaluate method in XPathProcessor. Here we test that we have more than one XdmItem.
44
void testXPathValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
45
     cout<<endl<<"Test testXPathValues:"<<endl; 
46
          xpath->clearParameters();
47
          xpath->clearProperties();
48
         XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person1>text3</person1></out>");
49
        if(input == nullptr) {
50
        SaxonApiException * exception = xpath->getException();
51
        if(exception != nullptr) {
52
            const char *message = xpath->getErrorMessage();
53
            cout << "Error Message = " << message << endl;
54
            xpath->exceptionClear();
55
        }
56
        sresult->failure++;
57
        sresult->failureList.push_back("testXPathValues");
58

    
59
            return;
60

    
61
        }
62

    
63
        XdmNode ** children = input->getChildren();
64
        XdmNode ** children0 = children[0]->getChildren();
65
        int num = children[0]->getChildCount();
66

    
67
        cout<<"number of children: "<<num<<endl;
68
        for(int i=0; i<num;i++) {
69
                cout<<"node name:"<<children0[i]->getNodeName()<<endl;
70
        }
71

    
72
    delete []children;
73
        delete [] children0;
74

    
75
        xpath->setContextItem((XdmItem *)input);
76
        XdmValue * resultValues = xpath->evaluate("//person");
77
        
78
        if(resultValues == nullptr) {
79
                 printf("result is null \n");
80
        SaxonApiException * exception = xpath->getException();
81
        if(exception != nullptr) {
82
            const char *message = xpath->getErrorMessage();
83
            cout << "Error Message = " << message << endl;
84
            xpath->exceptionClear();
85
        }
86
        sresult->failure++;
87
        sresult->failureList.push_back("testXPathValues");
88

    
89
        } else {
90
                cout<<"Number of items="<<resultValues->size()<<endl;
91
                bool nullFound = false;
92
                for(int i =0; i< resultValues->size();i++){
93
                        XdmItem * itemi = resultValues->itemAt(i);
94
                        if(itemi == nullptr) {
95
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
96
                                nullFound = true;
97
                                break;
98
                        }
99
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;                
100
                }
101
        delete resultValues;
102
                if(nullFound) {
103
            sresult->success++;
104
        } else {
105
                    sresult->failure++;
106
                    sresult->failureList.push_back("testXPathValues");
107
                }
108
        }
109
    delete input;
110
    xpath->clearParameters();
111
    xpath->clearProperties();
112
        
113

    
114
}
115

    
116
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
117
void testXPathAttrValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
118
     cout<<endl<<"Test testXPathValues1:"<<endl; 
119
          xpath->clearParameters();
120
          xpath->clearProperties();
121
         XdmNode * input = processor->parseXmlFromString("<out attr='valuex'><person attr1='value1' attr2='value2'>text1</person><person>text2</person><person1>text3</person1></out>");
122
        if(input == nullptr) {
123
        SaxonApiException * exception = xpath->getException();
124
        if(exception != nullptr) {
125
            const char *message = xpath->getErrorMessage();
126
            cout << "Error Message = " << message << endl;
127
            xpath->exceptionClear();
128
        }
129
        sresult->failure++;
130
        sresult->failureList.push_back("testXPathAttrValues");
131

    
132
            return;
133

    
134
        }
135

    
136
        //cout<<"Name of attr1= "<<input->getAttributeValue("attr")<<endl;
137

    
138

    
139
        xpath->setContextItem((XdmItem *)input);
140
        XdmItem * result = xpath->evaluateSingle("(//person)[1]");
141
        
142
        if(result == nullptr) {
143
                 printf("result is null \n");
144
        } else {
145
                XdmNode *nodeValue = (XdmNode*)result;
146
                cout<<"Attribute Count= "<<nodeValue->getAttributeCount()<<endl;
147
                const char *attrVal = nodeValue->getAttributeValue("attr1");
148
                if(attrVal != nullptr) {
149
                        cout<<"Attribute value= "<<attrVal<<endl;
150
                }
151
                
152
                XdmNode ** attrNodes = nodeValue->getAttributeNodes();
153
                if(attrNodes == nullptr) { return;}
154
        
155
                const char *name1 = attrNodes[0]->getNodeName();
156
                if(name1 != nullptr) {
157
                        cout<<"Name of attr1= "<<name1<<endl;
158
                }
159
                XdmNode * parent = attrNodes[0]->getParent();
160
                if(parent != nullptr) {
161
                        cout<<"Name of parent= "<<parent->getNodeName()<<endl;
162
                }
163
                XdmNode * parent1 = parent->getParent();
164
                if(parent1 != nullptr) {
165
                        cout<<"Name of parent= "<<parent1->getNodeName()<<endl;
166
                }
167
                sresult->success++;
168
        //TODO test if attr value is not there
169
        }
170
        
171
        
172

    
173
}
174

    
175
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
176
void testXPathOnFile(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
177
             cout<<endl<<"Test testXPath with file source:"<<endl;
178
          xpath->clearParameters(true);
179
          xpath->clearProperties(); 
180
        xpath->setContextFile("cat.xml");
181

    
182
        XdmValue * resultValues = xpath->evaluate("//person");
183
        
184
        if(resultValues == nullptr) {
185
                 printf("result is null \n");
186
        SaxonApiException * exception = xpath->getException();
187
        if(exception != nullptr) {
188
            const char *message = xpath->getErrorMessage();
189
            cout << "Error Message = " << message << endl;
190
            xpath->exceptionClear();
191
        }
192
        sresult->failure++;
193
        sresult->failureList.push_back("testXPathOnFile");
194
        } else {
195
                cout<<"Number of items="<<resultValues->size()<<endl;
196
                for(int i =0; i< resultValues->size();i++){
197
                        XdmItem * itemi = resultValues->itemAt(i);
198
                        if(itemi == nullptr) {
199
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
200
                                break;
201
                        }
202
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;                
203
                }
204
        }
205
        delete resultValues;
206
        
207

    
208
}
209

    
210
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
211
void testXPathOnFile2(XPathProcessor * xpath, sResultCount *sresult){
212
             cout<<endl<<"Test testXPath with file source:"<<endl;
213
          xpath->clearParameters();
214
          xpath->clearProperties(); 
215
        xpath->setContextFile("cat.xml");
216

    
217
        XdmItem * result = xpath->evaluateSingle("//person[1]");
218
        
219
        if(result == nullptr) {
220
                 printf("result is null \n");
221
        SaxonApiException * exception = xpath->getException();
222
        if(exception != nullptr) {
223
            const char *message = xpath->getErrorMessage();
224
            cout << "Error Message = " << message << endl;
225
            xpath->exceptionClear();
226
        }
227
        sresult->failure++;
228
        sresult->failureList.push_back("testXPathOnFile2");
229
        return;
230
        } else {
231
                cout<<"Number of items="<<result->size()<<endl;
232
                if(result->isAtomic()) {
233
                        cout<<"XdmItem is atomic - Error"<<endl;
234
                } else {
235
                        cout<<"XdmItem is not atomic"<<endl;
236
                }
237
                XdmNode *node = (XdmNode*)result;
238
                if(node->getNodeKind() == ELEMENT){
239
                   cout<<"Result is a ELEMENT"<<endl;
240
                   cout<<"Node name: "<<node->getNodeName()<<endl;
241
                } else {
242
                        cout<<"Node is of kind:"<<node->getNodeKind()<<endl;
243
                }
244
                const char * baseURI = node->getBaseUri();
245
                if(baseURI != nullptr) {
246
                        cout<<"baseURI of node: "<<baseURI<<endl;
247
                }
248
                sresult->success++;
249
        }
250
        delete result;
251
}
252

    
253
int main()
254
{
255

    
256
    SaxonProcessor * processor = new SaxonProcessor(false);
257
    XPathProcessor * xpathProc = processor->newXPathProcessor();
258
    sResultCount *sresult = new sResultCount();
259
    cout<<endl<<"Test: XPathProcessor with Saxon version="<<processor->version()<<endl; 
260
    testXPathSingle(processor, xpathProc, sresult);
261
    testXPathValues(processor, xpathProc, sresult);
262
    testXPathAttrValues(processor, xpathProc, sresult);
263
    testXPathOnFile(processor, xpathProc, sresult);
264
    testXPathOnFile2(xpathProc, sresult);
265

    
266

    
267
    std::cout << "\nTest Results - Number of tests= " << (sresult->success + sresult->failure) << ", Successes = "
268
              << sresult->success << ",  Failures= " << sresult->failure << std::endl;
269

    
270
    std::list<std::string>::iterator it;
271
    std::cout << "Failed tests:" << std::endl;
272
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
273
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
274
        //Print the contents
275
        std::cout << it->c_str() << std::endl;
276

    
277
    }
278

    
279
    delete xpathProc;
280
    delete processor;
281

    
282
     processor->release();
283

    
284

    
285
#ifdef MEM_DEBUG
286
    SaxonProcessor::getInfo();
287
#endif
288
    return 0;
289
}
(24-24/28)