Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXPath.cpp @ d0e6f8be

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 == NULL) {
22
                 printf("result is null \n");
23
        sresult->failure++;
24
        sresult->failureList.push_back("testXPathSingle");
25

    
26
        } else {
27
                cout<<"Number of items="<<result->size()<<endl;
28
                cout<<"String Value of result="<<result->getStringValue()<<endl;
29
        }
30
        delete result;
31

    
32
        
33

    
34
}
35

    
36
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
37
void testXPathValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
38
     cout<<endl<<"Test testXPathValues:"<<endl; 
39
          xpath->clearParameters(true);
40
          xpath->clearProperties();
41
         XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person1>text3</person1></out>");
42
        if(input == NULL) {return;}
43
        XdmNode ** children = input->getChildren();
44
        XdmNode ** children0 = children[0]->getChildren();
45
        int num = children[0]->getChildCount();
46
        delete []children;
47
        cout<<"number of children: "<<num<<endl;
48
        for(int i=0; i<num;i++) {
49
                cout<<"node name:"<<children0[i]->getNodeName()<<endl;
50
        }
51
        delete [] children0;
52

    
53
        xpath->setContextItem((XdmItem *)input);
54
        XdmValue * resultValues = xpath->evaluate("//person");
55
        
56
        if(resultValues == NULL) {
57
                 printf("result is null \n");
58
        } else {
59
                cout<<"Number of items="<<resultValues->size()<<endl;
60
                for(int i =0; i< resultValues->size();i++){
61
                        XdmItem * itemi = resultValues->itemAt(i);
62
                        if(itemi == NULL) {
63
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
64
                                break;
65
                        }
66
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;                
67
                }
68
        }
69
        delete resultValues;
70
        
71

    
72
}
73

    
74
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
75
void testXPathAttrValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
76
     cout<<endl<<"Test testXPathValues1:"<<endl; 
77
          xpath->clearParameters(true);
78
          xpath->clearProperties();
79
         XdmNode * input = processor->parseXmlFromString("<out attr='valuex'><person attr1='value1' attr2='value2'>text1</person><person>text2</person><person1>text3</person1></out>");
80
        if(input == NULL) {return;}
81

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

    
84

    
85
        xpath->setContextItem((XdmItem *)input);
86
        XdmItem * result = xpath->evaluateSingle("(//person)[1]");
87
        
88
        if(result == NULL) {
89
                 printf("result is null \n");
90
        } else {
91
                XdmNode *nodeValue = (XdmNode*)result;
92
                cout<<"Attribute Count= "<<nodeValue->getAttributeCount()<<endl;
93
                const char *attrVal = nodeValue->getAttributeValue("attr1");
94
                if(attrVal != NULL) {
95
                        cout<<"Attribute value= "<<attrVal<<endl;
96
                }
97
                
98
                XdmNode ** attrNodes = nodeValue->getAttributeNodes();
99
                if(attrNodes == NULL) { return;}
100
        
101
                const char *name1 = attrNodes[0]->getNodeName();
102
                if(name1 != NULL) {
103
                        cout<<"Name of attr1= "<<name1<<endl;
104
                }
105
                XdmNode * parent = attrNodes[0]->getParent();
106
                if(parent != NULL) {
107
                        cout<<"Name of parent= "<<parent->getNodeName()<<endl;
108
                }
109
                XdmNode * parent1 = parent->getParent();
110
                if(parent1 != NULL) {
111
                        cout<<"Name of parent= "<<parent1->getNodeName()<<endl;
112
                }
113
        //TODO test if attr value is not there
114
        }
115
        
116
        
117

    
118
}
119

    
120
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
121
void testXPathOnFile(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
122
             cout<<endl<<"Test testXPath with file source:"<<endl;
123
          xpath->clearParameters(true);
124
          xpath->clearProperties(); 
125
        xpath->setContextFile("cat.xml");
126

    
127
        XdmValue * resultValues = xpath->evaluate("//person");
128
        
129
        if(resultValues == NULL) {
130
                 printf("result is null \n");
131
        } else {
132
                cout<<"Number of items="<<resultValues->size()<<endl;
133
                for(int i =0; i< resultValues->size();i++){
134
                        XdmItem * itemi = resultValues->itemAt(i);
135
                        if(itemi == NULL) {
136
                                cout<<"Item at position "<<i<<" should not be null"<<endl;
137
                                break;
138
                        }
139
                        cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;                
140
                }
141
        }
142
        delete resultValues;
143
        
144

    
145
}
146

    
147
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
148
void testXPathOnFile2(XPathProcessor * xpath, sResultCount *sresult){
149
             cout<<endl<<"Test testXPath with file source:"<<endl;
150
          xpath->clearParameters(true);
151
          xpath->clearProperties(); 
152
        xpath->setContextFile("cat.xml");
153

    
154
        XdmItem * result = xpath->evaluateSingle("//person[1]");
155
        
156
        if(result == NULL) {
157
                 printf("result is null \n");
158
        } else {
159
                cout<<"Number of items="<<result->size()<<endl;
160
                if(result->isAtomic()) {
161
                        cout<<"XdmItem is atomic - Error"<<endl;
162
                } else {
163
                        cout<<"XdmItem is not atomic"<<endl;
164
                }
165
                XdmNode *node = (XdmNode*)result;
166
                if(node->getNodeKind() == ELEMENT){
167
                   cout<<"Result is a ELEMENT"<<endl;
168
                   cout<<"Node name: "<<node->getNodeName()<<endl;
169
                } else {
170
                        cout<<"Node is of kind:"<<node->getNodeKind()<<endl;
171
                }
172
                const char * baseURI = node->getBaseUri();
173
                if(baseURI != NULL) {
174
                        cout<<"baseURI of node: "<<baseURI<<endl;
175
                }
176
        }
177
        delete result;
178
        
179

    
180
}
181

    
182
int main()
183
{
184

    
185
    SaxonProcessor * processor = new SaxonProcessor(false);
186
    XPathProcessor * xpathProc = processor->newXPathProcessor();
187
    sResultCount *sresult = new sResultCount();
188
    cout<<endl<<"Test: XPathProcessor with Saxon version="<<processor->version()<<endl; 
189
    testXPathSingle(processor, xpathProc, sresult);
190
    testXPathValues(processor, xpathProc, sresult);
191
    testXPathAttrValues(processor, xpathProc, sresult);
192
    testXPathOnFile(processor, xpathProc, sresult);
193
    testXPathOnFile2(xpathProc, sresult);
194

    
195

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

    
199
    std::list<std::string>::iterator it;
200
    std::cout << "Failed tests:" << std::endl;
201
// Make iterate point to begining and incerement it one by one till it reaches the end of list.
202
    for (it = sresult->failureList.begin(); it != sresult->failureList.end(); it++) {
203
        //Print the contents
204
        std::cout << it->c_str() << std::endl;
205

    
206
    }
207

    
208
     processor->release();
209
    return 0;
210
}
(24-24/28)