Project

Profile

Help

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

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

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 <string>
7

    
8
using namespace std;
9

    
10
int exists(const char *fname)
11
{
12
    FILE *file;
13
   file = fopen(fname, "r");
14
    if (file)
15
    {
16
        fclose(file);
17
        return 1;
18
    }
19
    return 0;
20
}
21

    
22

    
23
/*
24
* Test1: 
25
*/
26
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc){
27
 cout<<endl<<"Test testXQuery1:"<<endl;
28
        queryProc->clearProperties();
29
        queryProc->clearParameters(true);
30
   queryProc->setProperty("s", "cat.xml");
31

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

    
34
    const char * result = queryProc->runQueryToString();
35
    if(result != NULL){
36
            cout<<"Result :"<<result<<endl;
37
    } else {
38

    
39
        queryProc->checkException();
40
        
41
    }
42

    
43
return;
44
        queryProc->setcwd(".");
45
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
46

    
47
                if (exists("catOutput.xml")) {
48
                    cout<<"The file $filename exists"<<endl;;
49
                } else {
50
                    cout<<"The file $filename does not exist"<<endl;
51
                    processor->exceptionClear();
52
                }
53
}
54

    
55
void testxQueryError(XQueryProcessor * queryProc){
56
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
57
        queryProc->clearProperties();
58
        queryProc->clearParameters(true);
59
   queryProc->setProperty("s", "cat.xml");
60

    
61
    queryProc->setProperty("qs", "<out>{count(/out/person)}</out>");
62
queryProc->setcwd(".");
63
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
64

    
65
                if (queryProc->exceptionOccurred()) {
66
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
67
                        for(int i=0;i<queryProc->exceptionCount();i++){
68
                                const char * message = queryProc->getErrorMessage(i);
69
                                if(message != NULL) {
70
                                        cout<<"Error Message = "<<message<<endl;                
71
                                }                
72
                        }
73
                
74
                }
75
}
76

    
77
void testxQueryError2(SaxonProcessor * processor, XQueryProcessor * queryProc){
78
 cout<<endl<<"Test testXQueryError-test2:"<<endl;
79
        queryProc->clearProperties();
80
        queryProc->clearParameters(true);
81
   queryProc->setProperty("s", "cat.xml");
82

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

    
85
    const char * result = queryProc->runQueryToString();
86
   if(result != NULL){
87
            cout<<"Result :"<<result<<endl;
88
    } else {
89
        const char * message = queryProc->getErrorMessage(0);
90
        if(message != NULL) {
91
                cout<<"Error Message="<<message<<" Line number= "<<processor->getException()->getLineNumber(0)<<endl;
92
        } else {
93
                cout<<"Error Message - NULL check"<<endl;
94
        }
95
        
96
    }
97

    
98
}
99

    
100

    
101
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc) {
102
 cout<<endl<<"Test testXQuery2:"<<endl;
103
        queryProc->clearProperties();
104
        queryProc->clearParameters(true);        
105
        queryProc->declareNamespace("", "http://one.uri/");
106
         XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
107
        queryProc->setContextItem((XdmItem *)input);
108
        queryProc->setQueryContent("/foo");
109

    
110
        XdmValue * value = queryProc->runQueryToValue();
111

    
112
        if(value->size() == 1) {
113
                cout<<"Test1: Result is ok size is "<<value->size()<<endl;
114
        } else {
115
                cout<<"Test 1: Result failure"<<endl;
116
        }
117

    
118
}
119

    
120
// Test that the XQuery compiler can compile two queries without interference
121
void testReusability(SaxonProcessor * processor, XQueryProcessor * queryProc){
122
        cout<<endl<<"Test test XQuery reusability:"<<endl;
123
        XQueryProcessor * queryProc2 = processor->newXQueryProcessor();
124

    
125
        queryProc->clearProperties();
126
        queryProc->clearParameters(true);
127

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

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

    
135
        queryProc->setContextItem((XdmItem *)input);
136

    
137
        XdmAtomicValue * value1 = processor->makeBooleanValue(true);
138
          queryProc->setParameter("p",(XdmValue *)value1);
139
        XdmValue * val = queryProc->runQueryToValue();
140

    
141
        if(val != NULL && ((XdmItem*)val->itemAt(0))->isAtomic()){
142
                cout<<"Test1: Result is atomic"<<endl;
143
                XdmAtomicValue* atomic = (XdmAtomicValue *)val->itemAt(0);
144
                bool result1 = atomic->getBooleanValue();
145
                cout<<"Test2: Result value="<<(result1 == true ? "true" : "false")<<endl;
146
cout<<"Test5: PrimitiveTypeName of  atomic="<<atomic->getPrimitiveTypeName()<<endl;
147
        }
148

    
149
        queryProc2->setContextItem((XdmItem *)input);
150

    
151
        XdmAtomicValue * value2 = processor->makeLongValue(6);
152
cout<<"Test5: PrimitiveTypeName of  value2="<<value2->getPrimitiveTypeName()<<endl;
153
        queryProc2->setParameter("p",(XdmValue *)value2);
154

    
155
        XdmValue * val2 = queryProc2->runQueryToValue();
156

    
157

    
158

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

    
161
        if(val2 != NULL && ((XdmItem*)val2->itemAt(0))->isAtomic()){
162
                cout<<"Test3: Result is atomic"<<endl;
163
                XdmAtomicValue* atomic2 = (XdmAtomicValue *)(val2->itemAt(0));
164
                long result2 = atomic2->getLongValue();
165
                cout<<"Test4: Result value="<<result2<<endl;
166
                cout<<"Test5: PrimitiveTypeName of  atomic2="<<atomic2->getPrimitiveTypeName()<<endl;
167
        }
168

    
169
if (queryProc->exceptionOccurred()) {
170
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
171
                        for(int i=0;i<queryProc->exceptionCount();i++){
172
                                const char * message = queryProc->getErrorMessage(i);
173
                                if(message != NULL) {
174
                                        cout<<"Error Message = "<<message<<endl;                
175
                                }                
176
                        }
177
                
178
                }
179
        
180
}
181

    
182

    
183
//Test requirement of license file - Test should fail
184
void testXQueryLineNumberError(XQueryProcessor * queryProc){
185
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
186
        queryProc->clearProperties();
187
        queryProc->clearParameters(true);
188
   queryProc->setProperty("s", "cat.xml");
189

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

    
192
    const char * result = queryProc->runQueryToString();
193
    if(result != NULL){
194
            cout<<"Result :"<<result<<endl;
195
    } else {
196
        cout<<"Some faiure"<<endl;
197
        if (queryProc->exceptionOccurred()) {
198
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
199
                        for(int i=0;i<queryProc->exceptionCount();i++){
200
                                const char * message = queryProc->getErrorMessage(i);
201
                                if(message != NULL) {
202
                                        cout<<"Error Message = "<<message<<endl;                
203
                                }                
204
                        }
205
                
206
                }
207
        
208
    }
209

    
210
}
211

    
212
//Test requirement of license file - Test should succeed
213
void testXQueryLineNumber(){
214
  SaxonProcessor * processor = new SaxonProcessor(true);
215
  processor->setConfigurationProperty("l", "on");
216
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
217
 cout<<endl<<"testXQueryLineNumber:"<<endl;
218
        
219
   queryProc->setProperty("s", "cat.xml");
220
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
221

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

    
224
    const char * result = queryProc->runQueryToString();
225
    if(result != NULL){
226
            cout<<"Result :"<<result<<endl;
227
    } else {
228
        cout<<"Some faiure"<<endl;
229
        if (queryProc->exceptionOccurred()) {
230
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
231
                        for(int i=0;i<queryProc->exceptionCount();i++){
232
                                const char * message = queryProc->getErrorMessage(i);
233
                                if(message != NULL) {
234
                                        cout<<"Error Message = "<<message<<endl;                
235
                                }                
236
                        }
237
                
238
                }
239
        
240
    }
241
        delete processor;
242

    
243
}
244

    
245
int main()
246
{
247

    
248
    SaxonProcessor * processor = new SaxonProcessor(false);
249
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
250
    XQueryProcessor * query = processor->newXQueryProcessor();
251
    
252

    
253
    testxQuery1(processor, query);
254
    testxQueryError(query);
255
    testxQueryError2(processor, query);
256
    testDefaultNamespace(processor, query);
257
    testReusability(processor, query);
258
    testXQueryLineNumberError(query);
259
    testXQueryLineNumber();
260

    
261
    delete query;
262
     processor->release();
263
    return 0;
264
}
(22-22/25)