Project

Profile

Help

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

he / latest9.6 / hec / samples / cppTests / testXQuery.cpp @ a65b315b

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
    if (file = fopen(fname, "r"))
14
    {
15
        fclose(file);
16
        return 1;
17
    }
18
    return 0;
19
}
20

    
21

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

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

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

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

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

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

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

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

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

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

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

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

    
97
}
98

    
99

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

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

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

    
117
}
118

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

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

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

    
133
        queryProc->setContextItem((XdmItem *)input);
134

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

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

    
147
        queryProc2->setContextItem((XdmItem *)input);
148

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

    
153
        XdmValue * val2 = queryProc2->runQueryToValue();
154

    
155

    
156

    
157
cout<<"XdmValue size="<<val2->size()<<", "<<(val2->itemAt(0))->getStringValue(processor)<<endl;
158

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

    
168

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

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

    
178
    const char * result = queryProc->runQueryToString();
179
    if(result != NULL){
180
            cout<<"Result :"<<result<<endl;
181
    } else {
182
        cout<<"Some faiure"<<endl;
183
        if (queryProc->exceptionOccurred()) {
184
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
185
                        for(int i=0;i<queryProc->exceptionCount();i++){
186
                                const char * message = queryProc->getErrorMessage(i);
187
                                if(message != NULL) {
188
                                        cout<<"Error Message = "<<message<<endl;                
189
                                }                
190
                        }
191
                
192
                }
193
        
194
    }
195

    
196
}
197

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

    
207

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

    
210
    const char * result = queryProc->runQueryToString();
211
    if(result != NULL){
212
            cout<<"Result :"<<result<<endl;
213
    } else {
214
        cout<<"Some faiure"<<endl;
215
        if (queryProc->exceptionOccurred()) {
216
                    cout<<"Exception found. Count="<<queryProc->exceptionCount()<<endl;
217
                        for(int i=0;i<queryProc->exceptionCount();i++){
218
                                const char * message = queryProc->getErrorMessage(i);
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(processor, query);
241
    testxQueryError2(processor, query);
242
    testDefaultNamespace(processor, query);
243
    testReusability(processor, query);
244
    testXQueryLineNumberError(processor, query);
245
    testXQueryLineNumber();
246

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