Project

Profile

Help

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

he / src / main / c / samples / cppTests / testXQuery.cpp @ 306962bc

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 "../../Saxon.C.API/XdmAtomicValue.h"
6
#include "CppTestUtils.h"
7
#include <string>
8

    
9
using namespace std;
10

    
11

    
12

    
13

    
14
/*
15
* Test1: 
16
*/
17
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc){
18
 cout<<endl<<"Test testXQuery1:"<<endl;
19
        queryProc->clearProperties();
20
        queryProc->clearParameters(true);
21
   queryProc->setProperty("s", "cat.xml");
22

    
23
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
24

    
25
    const char * result = queryProc->runQueryToString();
26
    if(result != NULL){
27
            cout<<"Result :"<<result<<endl;
28
    } else {
29

    
30
        queryProc->checkException();
31
        
32
    }
33

    
34
return;
35
        queryProc->setcwd(".");
36
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
37

    
38
                if (CppTestUtils::exists("catOutput.xml")) {
39
                    cout<<"The file $filename exists"<<endl;;
40
                } else {
41
                    cout<<"The file $filename does not exist"<<endl;
42
                    processor->exceptionClear();
43
                }
44
}
45

    
46
void testxQueryError(XQueryProcessor * queryProc){
47
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
48
        queryProc->clearProperties();
49
        queryProc->clearParameters(true);
50
   queryProc->setProperty("s", "cat.xml");
51

    
52
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
53
queryProc->setcwd(".");
54
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
55

    
56
                if (queryProc->exceptionOccurred()) {
57

    
58
                    cout<<"Exception found. "<<endl;
59
                                const char * message = queryProc->getErrorMessage();
60
                                if(message != NULL) {
61
                                        cout<<"Error Message = "<<message<<endl;                
62
                                }
63
                
64
                }
65
}
66

    
67
void testxQueryError2(SaxonProcessor * processor, XQueryProcessor * queryProc){
68
 cout<<endl<<"Test testXQueryError-test2:"<<endl;
69
        queryProc->clearProperties();
70
        queryProc->clearParameters(true);
71
   queryProc->setProperty("s", "cat.xml");
72

    
73
    queryProc->setProperty("qs", "<out>{count(/out/person)}<out>");
74

    
75
    const char * result = queryProc->runQueryToString();
76
   if(result != NULL){
77
            cout<<"Result :"<<result<<endl;
78
    } else {
79
        const char * message = queryProc->getErrorMessage();
80
        if(message != NULL) {
81
                cout<<"Error Message="<<message<<" Line number= "<<queryProc->getException()->getLineNumber()<<endl;
82
        } else {
83
                cout<<"Error Message - NULL check"<<endl;
84
        }
85
        
86
    }
87

    
88
}
89

    
90

    
91
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc) {
92
 cout<<endl<<"Test testXQuery2:"<<endl;
93
        queryProc->clearProperties();
94
        queryProc->clearParameters(true);        
95
        queryProc->declareNamespace("", "http://one.uri/");
96
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
97
        queryProc->setContextItem((XdmItem *)input);
98
        queryProc->setQueryContent("/foo");
99

    
100
        XdmValue * value = queryProc->runQueryToValue();
101

    
102
        if(value->size() == 1) {
103
                cout<<"Test1: Result is ok size is "<<value->size()<<endl;
104
        } else {
105
                cout<<"Test 1: Result failure"<<endl;
106
        }
107

    
108
}
109

    
110
// Test that the XQuery compiler can compile two queries without interference
111
void testReusability(SaxonProcessor * processor, XQueryProcessor * queryProc){
112
        cout<<endl<<"Test test XQuery reusability:"<<endl;
113
        XQueryProcessor * queryProc2 = processor->newXQueryProcessor();
114

    
115
        queryProc->clearProperties();
116
        queryProc->clearParameters(true);
117

    
118
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar xmlns='http://two.uri'>12</bar></foo>");
119
        queryProc->declareNamespace("", "http://one.uri/");
120
        queryProc->setQueryContent("declare variable $p as xs:boolean external; exists(/foo) = $p");
121

    
122
        queryProc2->declareNamespace("", "http://two.uri");
123
        queryProc2->setQueryContent("declare variable $p as xs:integer external; /*/bar + $p");
124

    
125
        queryProc->setContextItem((XdmItem *)input);
126

    
127
        XdmAtomicValue * value1 = processor->makeBooleanValue(true);
128
          queryProc->setParameter("p",(XdmValue *)value1);
129
        XdmValue * val = queryProc->runQueryToValue();
130

    
131
        if(val != NULL && ((XdmItem*)val->itemAt(0))->isAtomic()){
132
                cout<<"Test1: Result is atomic"<<endl;
133
                XdmAtomicValue* atomic = (XdmAtomicValue *)val->itemAt(0);
134
                bool result1 = atomic->getBooleanValue();
135
                cout<<"Test2: Result value="<<(result1 == true ? "true" : "false")<<endl;
136
cout<<"Test5: PrimitiveTypeName of  atomic="<<atomic->getPrimitiveTypeName()<<endl;
137
        }
138

    
139
        queryProc2->setContextItem((XdmItem *)input);
140

    
141
        XdmAtomicValue * value2 = processor->makeLongValue(6);
142
cout<<"Test5: PrimitiveTypeName of  value2="<<value2->getPrimitiveTypeName()<<endl;
143
        queryProc2->setParameter("p",(XdmValue *)value2);
144

    
145
        XdmValue * val2 = queryProc2->runQueryToValue();
146

    
147

    
148

    
149
cout<<"XdmValue size="<<val2->size()<<", "<<(val2->itemAt(0))->getStringValue()<<endl;
150

    
151
        if(val2 != NULL && ((XdmItem*)val2->itemAt(0))->isAtomic()){
152
                cout<<"Test3: Result is atomic"<<endl;
153
                XdmAtomicValue* atomic2 = (XdmAtomicValue *)(val2->itemAt(0));
154
                long result2 = atomic2->getLongValue();
155
                cout<<"Test4: Result value="<<result2<<endl;
156
                cout<<"Test5: PrimitiveTypeName of  atomic2="<<atomic2->getPrimitiveTypeName()<<endl;
157
        }
158

    
159
if (queryProc->exceptionOccurred()) {
160
                                const char * message = queryProc->getErrorMessage();
161
                                if(message != NULL) {
162
                                        cout<<"Error Message = "<<message<<endl;                
163
                                }                
164

    
165
                
166
                }
167
        
168
}
169

    
170

    
171
//Test requirement of license file - Test should fail
172
void testXQueryLineNumberError(XQueryProcessor * queryProc){
173
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
174
        queryProc->clearProperties();
175
        queryProc->clearParameters(true);
176
   queryProc->setProperty("s", "cat.xml");
177

    
178
    queryProc->setProperty("qs", "saxon:line-number((//person)[1])");
179

    
180
    const char * result = queryProc->runQueryToString();
181
    if(result != NULL){
182
            cout<<"Result :"<<result<<endl;
183
    } else {
184
        cout<<"Some faiure"<<endl;
185
        if (queryProc->exceptionOccurred()) {
186
                    cout<<"Exception found. "<<endl;
187
                    const char * message = queryProc->getException()->getMessage();
188
                                if(message != NULL) {
189
                                        cout<<"Error Message = "<<message<<endl;                
190
                                }                
191

    
192
                
193
                }
194
        
195
    }
196

    
197
}
198

    
199
//Test requirement of license file - Test should succeed
200
void testXQueryLineNumber(){
201
  SaxonProcessor * processor = new SaxonProcessor(true);
202
  processor->setConfigurationProperty("l", "on");
203
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
204
 cout<<endl<<"testXQueryLineNumber:"<<endl;
205
        
206
   queryProc->setProperty("s", "cat.xml");
207
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
208

    
209
    queryProc->setProperty("qs", "saxon:line-number(doc('cat.xml')/out/person[1])"); ///out/person[1]
210

    
211
    const char * result = queryProc->runQueryToString();
212
    if(result != NULL){
213
            cout<<"Result :"<<result<<endl;
214
    } else {
215
        cout<<"Some faiure"<<endl;
216
        if (queryProc->exceptionOccurred()) {
217
                    cout<<"Exception found."<<endl;
218
                                const char * message = queryProc->getErrorMessage();
219
                                if(message != NULL) {
220
                                        cout<<"Error Message = "<<message<<endl;                
221
                                }                
222

    
223
                
224
                }
225
        
226
    }
227
        delete processor;
228

    
229
}
230

    
231
int main()
232
{
233

    
234
    SaxonProcessor * processor = new SaxonProcessor(false);
235
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
236
    XQueryProcessor * query = processor->newXQueryProcessor();
237
    
238

    
239
    testxQuery1(processor, query);
240
    testxQueryError(query);
241
    testxQueryError2(processor, query);
242
    testDefaultNamespace(processor, query);
243
    testReusability(processor, query);
244
    testXQueryLineNumberError(query);
245
    testXQueryLineNumber();
246

    
247
    delete query;
248
     processor->release();
249
    return 0;
250
}
(25-25/28)