Project

Profile

Help

Revision 01d6fdb6

Added by O'Neil Delpratt 5 months ago

Saxon/C development

View differences:

src/main/c/Saxon.C.API/SaxonApiException.cpp
71 71
     * A destructor.
72 72
     */
73 73
	SaxonApiException::~SaxonApiException() noexcept {
74
	    std::cerr<<"SaxonApiException destructor called"<<std::endl;
74 75
        message.clear();
75 76
        errorCode.clear();
76 77
        systemId.clear();
src/main/c/Saxon.C.API/SaxonApiException.h
2 2
#define SAXON_API_EXCEPTION_H
3 3

  
4 4
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
5
// Copyright (c) 2020 Saxonica Limited.
5
// Copyright (c) 2021 Saxonica Limited.
6 6
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 7
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 8
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
src/main/c/Saxon.C.API/SaxonProcessor.cpp
105 105
    if(exceptionOccurred()) {
106 106
        SaxonProcessor::sxn_environ->env->ExceptionClear();
107 107
    }
108
    std::cerr<< "saxonProc exceptionClear cp0"<<std::endl;
108 109
    if (exception != nullptr) {
109 110
        std::cerr<< "saxonProc exception= "<< exception <<std::endl;
110 111
        delete exception;
......
241 242
    cwd = "";
242 243
    licensei = l;
243 244
    versionStr = nullptr;
245
    exception = nullptr;
244 246

  
245 247
    if (SaxonProcessor::jvmCreatedCPP == 0) {
246 248
        SaxonProcessor::jvmCreatedCPP = 1;
......
647 649
            return nullptr;
648 650
        }
649 651

  
652
        if(proc == nullptr) {
653
            createException("The Java SaxonProcessor object (i.e. cppXQ) is NULL - Possible exception thrown");
654
            return nullptr;
655
        }
656

  
650 657
        jstring jstr = (jstring) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(saxonCAPIClass, MID_version,
651 658
                                                                                           proc));
652 659
        const char *tempVersionStr = SaxonProcessor::sxn_environ->env->GetStringUTFChars(jstr, nullptr);
src/main/c/Saxon.C.API/SaxonProcessor.h
94 94

  
95 95
class XsltExecutable;
96 96

  
97
class SaxonApiException;
98

  
97 99

  
98 100
// The Saxon XSLT interface class
99 101

  
src/main/c/Saxon.C.API/XQueryProcessor.cpp
3 3
#include "XdmItem.h"
4 4
#include "XdmNode.h"
5 5
#include "XdmAtomicValue.h"
6
#include "XdmFunctionItem.h"
7
#include "XdmMap.h"
8
#include "XdmArray.h"
9

  
10

  
11
#ifdef MEM_DEBUG
12
#define new new(__FILE__, __LINE__)
13
#endif
6 14

  
7 15
    XQueryProcessor::XQueryProcessor() {
8 16
	SaxonProcessor *p = new SaxonProcessor(false);
9 17
	XQueryProcessor(p, "");
10 18
     }
11 19

  
20

  
21
    XQueryProcessor::~XQueryProcessor(){
22
        clearProperties();
23
        clearParameters();
24
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXQ);
25
        cwdXQ.erase();
26
        exceptionClear();
27
    }
28

  
12 29
    XQueryProcessor::XQueryProcessor(SaxonProcessor *p, std::string curr) {
13 30
    proc = p;
14

  
15
  
31
        exception = nullptr;
16 32
     cppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XQueryEngine");
17 33

  
18 34

  
19
    cppXQ = createSaxonProcessor2 (SaxonProcessor::sxn_environ->env, cppClass, "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
35
    jobject tempcppXQ = createSaxonProcessor2 (SaxonProcessor::sxn_environ->env, cppClass, "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
36
    if(tempcppXQ) {
37
        cppXQ = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXQ);
38
    } else {
39
        createException("Error: Failed to create the Xslt30Processor internal object");
40
        std::cerr<<"Error in XQueryProcessor XXXXXXX"<<std::endl;
41
    }
20 42
    
21 43
#ifdef DEBUG
22 44
	jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
23 45
	SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
24 46
#endif
25 47

  
26
    exception = nullptr;
48

  
27 49
   // outputfile1 = "";
28 50
	if(!(proc->cwd.empty()) && curr.empty()){
29 51
		cwdXQ = proc->cwd;
......
190 212
       		for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
191 213
                XdmValue *value = itr->second;
192 214
                if (value != nullptr) {
193
                    value->decrementRefCount();
194 215
#ifdef DEBUG
195 216
                    std::cerr<<"XQueryProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
196 217
#endif
197
                    if (value->getRefCount() < 1) {
198 218
                        delete value;
199
                    }
219
                        value = nullptr;
200 220
                }
201 221
            }
202
		
203
	} else {
204

  
205
		for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
206
			XdmValue * value = itr->second;
207
			if(value != nullptr) {
208
                value->decrementRefCount();
209
            }
210
		}
211 222
	}
212

  
213 223
	parameters.clear();
214 224
    }
215 225

  
......
250 260

  
251 261
	jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToFile", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
252 262
 	if (!mID) {
253
        std::cerr<<"Error: MyClassInDll."<<"executeQueryToFile"<<" not found\n"<<std::endl;
263
        std::cerr<<"Error: "<<getDllname<<".executeQueryToFile"<<" not found\n"<<std::endl;
254 264
    } else {
265
        if(cppXQ == nullptr) {
266
#if defined(DEBUG)
267
            std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
268
#endif
269
            createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
270
            return;
271
        }
255 272
	jobjectArray stringArray = nullptr;
256 273
	jobjectArray objectArray = nullptr;
257 274

  
......
261 278
	if(size >0) {
262 279

  
263 280
	   int i=0;
264
           jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
281
	   jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
265 282
	   jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
266 283
	   objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, objectClass, 0 );
267 284
	   stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray( (jint)size, stringClass, 0 );
......
287 304
	   }
288 305
	}
289 306

  
290
	 SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), SaxonProcessor::sxn_environ->env->NewStringUTF(ofilename), stringArray, objectArray );
307
	 SaxonProcessor::sxn_environ->env->CallVoidMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), SaxonProcessor::sxn_environ->env->NewStringUTF(ofilename), stringArray, objectArray );
291 308
	  SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
292 309
	  SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
293 310

  
294
	exception = proc->checkAndCreateException(cppClass);
311
	  if(proc->exceptionOccurred()) {
312
          createException();
313
      }
295 314
	 
296 315
  }
297 316

  
......
305 324
 if (!mID) {
306 325
        std::cerr<<"Error: MyClassInDll."<<"executeQueryToValue"<<" not found\n"<<std::endl;
307 326
    } else {
327
     if(cppXQ == nullptr) {
328
#if defined(DEBUG)
329
         std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
330
#endif
331
         createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
332
         return nullptr;
333
     }
308 334
	jobjectArray stringArray = nullptr;
309 335
	jobjectArray objectArray = nullptr;
310 336

  
......
347 373
		jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
348 374
		jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
349 375
		jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
376
        jclass mapItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
377
                                           "net/sf/saxon/s9api/XdmMap");
378
        jclass arrayItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
379
                                             "net/sf/saxon/s9api/XdmArray");
350 380
		XdmValue * value = nullptr;
351 381
		XdmItem * xdmItem = nullptr;
352 382

  
......
361 391
				return xdmItem;
362 392
			
363 393
			} else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
364
                /*xdmItem =  new XdmFunctionItem(result);
365
                xdmItem->setProcessor(proc);
366
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);*/
367
                return nullptr;// xdmItem;
368
			} else {
394
                xdmItem = new XdmFunctionItem(result);
395
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
396
                return xdmItem;
397
			 } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapItemClass) == JNI_TRUE) {
398
            xdmItem = new XdmMap(result);
399
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
400
        return xdmItem;
401
        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayItemClass) == JNI_TRUE) {
402
        xdmItem = new XdmArray(result);
403
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
404
            return xdmItem;
405
        } else {
369 406
				value = new XdmValue(result, true);
370 407
				return value;
371 408
			}
......
383 420

  
384 421
    const char * XQueryProcessor::executeQueryToString(const char * infilename, const char * query){
385 422

  
386
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
423
 jmethodID mID = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID (cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
387 424
 if (!mID) {
388 425
        std::cerr<<"Error: MyClassInDll."<<"executeQueryToString"<<" not found\n"<<std::endl;
389 426
    } else {
427
     if(cppXQ == nullptr) {
428
#if defined(DEBUG)
429
         std::cerr << "The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown" << std::endl;
430
#endif
431
         createException("The Java XQueryEngine object (i.e. cppXQ) is NULL - Possible exception thrown");
432
         return nullptr;
433
     }
390 434
	jobjectArray stringArray = nullptr;
391 435
	jobjectArray objectArray = nullptr;
392 436

  
......
421 465
	   }
422 466
	}
423 467

  
424
	  jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
468
     jbyteArray result = nullptr;
469
     jobject obj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXQ, mID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXQ.c_str()), stringArray, objectArray ));
425 470
	  SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
426 471
	  SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
427 472

  
428
	  if(result) {
429
             const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
430
            //return "result should be ok";            
431
	    return str;
432
	   } else {
433
		    exception = proc->checkAndCreateException(cppClass);
473
     if (obj) {
474
         result = (jbyteArray) obj;
475
     }
476
     if (result) {
477
         jboolean isCopy = false;
478
         jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
479
         jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
480

  
481
         const char *str = new char[num_bytes];
482
         memcpy ((void *) str, b , num_bytes );
483

  
484
         SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
485
         return str;
486
     } else {
487
		    createException();
434 488
	   		
435 489
     		}
436 490
  }
......
467 521

  
468 522

  
469 523
void XQueryProcessor::exceptionClear(){
524
    SaxonProcessor::sxn_environ->env->ExceptionClear();
470 525
	if(exception != nullptr) {
471 526
		delete exception;
472 527
		exception = nullptr;
473
		SaxonProcessor::sxn_environ->env->ExceptionClear();
528

  
474 529
	}
475 530

  
531

  
476 532
   
477 533
 
478 534
}
479 535

  
480 536
bool XQueryProcessor::exceptionOccurred(){
481
	return proc->exceptionOccurred();
482

  
537
    return proc->exceptionOccurred() || exception != nullptr;
483 538
}
484 539

  
485 540

  
......
506 561
SaxonApiException* XQueryProcessor::getException() {
507 562
    return exception;
508 563
}
564

  
565
void XQueryProcessor::createException(const char * message) {
566
    if(exception != nullptr) {
567
        delete exception;
568
        exception = nullptr;
569
    }
570
    if(message == nullptr) {
571
        exception = proc->checkAndCreateException(cppClass);
572
    } else {
573
        exception = new SaxonApiException(message);
574
    }
575

  
576
}
src/main/c/Saxon.C.API/XQueryProcessor.h
47 47
    */
48 48
    XQueryProcessor * clone();
49 49

  
50
    ~XQueryProcessor(){
51
	clearProperties();
52
	clearParameters();
53
    }
50
    ~XQueryProcessor();
54 51

  
55 52
    //!Set the initial context item for the query
56 53
    /**
......
269 266
    
270 267

  
271 268
private:
269

  
270
    void createException(const char * message=nullptr);
271

  
272 272
    std::string cwdXQ; /*!< current working directory */
273 273
	SaxonProcessor * proc;
274 274
	jclass  cppClass;
src/main/c/Saxon.C.API/Xslt30Processor.cpp
38 38
	cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
39 39
			"net/sf/saxon/option/cpp/Xslt30Processor");
40 40

  
41
	cppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
41
	jobject tempcppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
42 42
			"(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
43
	if(tempcppXT) {
44
        cppXT = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXT);
45
    } else {
46
	    createException("Error: Failed to create the Xslt30Processor internal object");
47

  
48
	}
43 49

  
44 50
#ifdef DEBUG
45 51
	jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
......
60 66

  
61 67
    Xslt30Processor::~Xslt30Processor(){
62 68
	    clearParameters();
63
	    SaxonProcessor::sxn_environ->env->DeleteLocalRef(cppXT);
69
	    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXT);
64 70
	    cwdXT.erase();
65 71
	    exceptionClear();
66 72
    }
......
193 199
}
194 200

  
195 201
void Xslt30Processor::createException(const char * message) {
196
    exceptionClear();
202
    if(exception != nullptr) {
203
        delete exception;
204
        exception = nullptr;
205
    }
197 206
    if(message == nullptr) {
198 207
        exception = proc->checkAndCreateException(cppClass);
199 208
    } else {
......
632 641
	    JParameters comboArrays;
633 642
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
634 643

  
635
		SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtfID,
644
		SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, mtfID,
636 645
								SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
637 646
								(source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : nullptr),
638 647
								SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),	(outputfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :nullptr),
src/main/c/samples/cppTests/catOutput.xml
1
<?xml version="1.0" encoding="UTF-8"?><out>3</out>
1
<?xml version="1.0" encoding="UTF-8"?>
src/main/c/samples/cppTests/testXQuery.cpp
21 21
*/
22 22
void testxQuery1(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult){
23 23
    cout<<endl<<"Test testXQuery1:"<<endl;
24
	queryProc->clearProperties();
25
	queryProc->clearParameters(true);
24
    queryProc->clearParameters();
25
    queryProc->clearProperties();
26 26
   queryProc->setProperty("s", "cat.xml");
27 27

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

  
30 30
    const char * result = queryProc->runQueryToString();
31
    if(result != NULL){
31
    if(result != nullptr){
32 32
    	cout<<"Result :"<<result<<endl;
33
    	sresult->success++;
34
    	delete result;
33 35
    } else {
36
        sresult->failure++;
37
        sresult->failureList.push_back("testXQuery1");
34 38
        if(queryProc->exceptionOccurred()) {
35

  
36 39
            SaxonApiException *exception = queryProc->getException();
37
            cerr<< "Error: "<<exception->getMessage()<<endl;
38
            delete exception;
40
            if(exception != nullptr) {
41
                cerr << "Error: " << exception->getMessage() << endl;
42
                delete exception;
43
                exception = nullptr;
44
            }
45
            queryProc->exceptionClear();
39 46
        }
40 47

  
41 48
	
42 49
    }
43 50

  
51
    queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
44 52

  
45
	queryProc->setcwd(".");
46
     queryProc->executeQueryToFile(NULL, "catOutput.xml", NULL);
53
    if(queryProc->exceptionOccurred()) {
54
        SaxonApiException *exception = queryProc->getException();
55
        if(exception != nullptr) {
56
            cerr << "Error: " << exception->getMessage() << endl;
57
        }
58
        queryProc->exceptionClear();
59
    }
60

  
61
    if (CppTestUtils::exists("catOutput.xml")) {
62
        cout<<"The file $filename exists"<<endl;
63
        sresult->success++;
64
        remove("catOutput.xml");
65
    } else {
66
        sresult->failure++;
67
        sresult->failureList.push_back("testXQuery1");
68
        cout<<"The file $filename does not exist"<<endl;
69

  
70
    }
71
    cerr<<"testXQuery1 cp0"<<endl;
72
    queryProc->clearParameters();
73
    cerr<<"testXQuery1 cp0"<<endl;
74
    queryProc->clearProperties();
47 75

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

  
56 78
void testxQueryError(XQueryProcessor * queryProc, sResultCount *sresult){
57 79
 cout<<endl<<"Test testXQueryError-Test:"<<endl;
58
	queryProc->clearProperties();
59
	queryProc->clearParameters(true);
60
   queryProc->setProperty("s", "cat.xml");
80
    queryProc->clearParameters();
81
    queryProc->clearProperties();
82
    //queryProc->setProperty("s", "cat.xml");
61 83

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

  
86
     queryProc->executeQueryToFile(nullptr, "catOutput.xml", nullptr);
65 87

  
66 88
		if (queryProc->exceptionOccurred()) {
89
            cout << "Exception found. " << endl;
90
            SaxonApiException * exception = queryProc->getException();
91
            if(exception != nullptr) {
92
                sresult->success++;
93
                const char *message = exception->getMessage();
94
                cout << "Error Message = " << message << endl;
95
            }
96
            queryProc->exceptionClear();
97
		} else {
98

  
99
            sresult->failure++;
100
            sresult->failureList.push_back("testXQueryError");
67 101

  
68
		    cout<<"Exception found. "<<endl;
69
				const char * message = queryProc->getErrorMessage();
70
				if(message != NULL) {
71
					cout<<"Error Message = "<<message<<endl;		
72
				}
73
		
74 102
		}
103
    queryProc->clearParameters();
104
    queryProc->clearProperties();
75 105
}
76 106

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

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

  
85 115
    const char * result = queryProc->runQueryToString();
86
   if(result != NULL){
116
   if(result != nullptr){
87 117
    	cout<<"Result :"<<result<<endl;
118
    	delete result;
119
       sresult->failure++;
120
       sresult->failureList.push_back("testXQueryError2");
88 121
    } else {
89
	const char * message = queryProc->getErrorMessage();
90
	if(message != NULL) {
91
		cout<<"Error Message="<<message<<" Line number= "<<queryProc->getException()->getLineNumber()<<endl;
92
	} else {
93
		cout<<"Error Message - NULL check"<<endl;
94
	}
95
	
122

  
123
       if (queryProc->exceptionOccurred()) {
124
           SaxonApiException * exception = queryProc->getException();
125
           if(exception != nullptr) {
126
               sresult->success++;
127
               cout << "Exception found. " << endl;
128
               const char *message = queryProc->getErrorMessage();
129
               cout << "Error Message = " << message << endl;
130

  
131
           } else {
132
               cout << "Exception obj is NULL !!!!. " << endl;
133

  
134
           }
135
           queryProc->exceptionClear();
136
       } else {
137

  
138
           sresult->failure++;
139
           sresult->failureList.push_back("testXQueryError2");
140
       }
141

  
96 142
    }
143
    queryProc->clearProperties();
144
    queryProc->clearParameters();
97 145

  
98 146
}
99 147

  
100 148

  
101 149
void testDefaultNamespace(SaxonProcessor * processor, XQueryProcessor * queryProc, sResultCount *sresult) {
102
 cout<<endl<<"Test testXQuery2:"<<endl;
150
 cout<<endl<<"Test testDefaultNamespace:"<<endl;
103 151
	queryProc->clearProperties();
104
	queryProc->clearParameters(true);	
152
	queryProc->clearParameters();
105 153
	queryProc->declareNamespace("", "http://one.uri/");
106 154
	 XdmNode * input = processor->parseXmlFromString("<foo xmlns='http://one.uri/'><bar/></foo>");
155

  
156
    if(input == nullptr) {
157
        cout << "Source document is null." << endl;
158
        if(processor->exceptionOccurred()) {
159
            cerr<<processor->getErrorMessage()<<endl;
160
        }
161
        sresult->failure++;
162
        sresult->failureList.push_back("testDefaultNamespace");
163
        return;
164

  
165
    }
166

  
107 167
	queryProc->setContextItem((XdmItem *)input);
108 168
	queryProc->setQueryContent("/foo");
109 169

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

  
112
	if(value->size() == 1) {
172
	if(value != nullptr && value->size() == 1) {
173
	    sresult->success++;
113 174
		cout<<"Test1: Result is ok size is "<<value->size()<<endl;
175
        delete value;
114 176
	} else {
115
		cout<<"Test 1: Result failure"<<endl;
177
        if (queryProc->exceptionOccurred()) {
178
            sresult->failure++;
179
            sresult->failureList.push_back("testDefaultNamespace");
180
            SaxonApiException * exception = queryProc->getException();
181
            if(exception != nullptr) {
182
                cout << "Exception found. " << endl;
183
                const char *message = queryProc->getErrorMessage();
184
                cout << "Error Message = " << message << endl;
185
                queryProc->exceptionClear();
186
            }
187
        }
116 188
	}
189
    queryProc->clearProperties();
190
    queryProc->clearParameters();
191
	delete input;
117 192

  
118 193
}
119 194

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

  
125
	queryProc->clearProperties();
126
	queryProc->clearParameters(true);
200
	queryProc2->clearProperties();
201
	queryProc2->clearParameters();
127 202

  
128 203
 	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");
204

  
205
 	if(input == nullptr) {
206
        cout << "Source document is null." << endl;
207
        if(processor->exceptionOccurred()) {
208
            cerr<<processor->getErrorMessage()<<endl;
209
        }
210
 	    sresult->failure++;
211
 	    sresult->failureList.push_back("testReusability");
212
 	    return;
213

  
214
 	}
215

  
216
	queryProc2->declareNamespace("", "http://one.uri/");
217
	queryProc2->setQueryContent("declare variable $p as xs:boolean external; exists(/foo) = $p");
131 218

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

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

  
137 224
	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;
225
  	queryProc2->setParameter("p",(XdmValue *)value1);
226
	XdmValue * val = queryProc2->runQueryToValue();
227

  
228
	if(val != nullptr){
229
	    if( ((XdmItem*)val->itemAt(0))->isAtomic()) {
230
	        sresult->success++;
231
            cout << "Test1: Result is atomic" << endl;
232
            XdmAtomicValue *atomic = (XdmAtomicValue *) val->itemAt(0);
233
            bool result1 = atomic->getBooleanValue();
234
            cout << "Test2: Result value=" << (result1 == true ? "true" : "false") << endl;
235
            cout << "PrimitiveTypeName of  atomic=" << atomic->getPrimitiveTypeName() << endl;
236
        } else {
237
            sresult->failure++;
238
            sresult->failureList.push_back("testReusability");
239
	    }
240
	    delete val;
241
	} else {
242
        if (queryProc2->exceptionOccurred()) {
243
            sresult->failure++;
244
            sresult->failureList.push_back("testReusability");
245
            SaxonApiException * exception = queryProc2->getException();
246
            if(exception != nullptr) {
247
                cout << "Exception found. " << endl;
248
                const char *message = queryProc2->getErrorMessage();
249
                cout << "Error Message = " << message << endl;
250
                queryProc2->exceptionClear();
251
            }
252
        }
253

  
254
        return;
147 255
	}
148 256

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

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

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

  
157 265

  
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;
266
	if(val2 != nullptr){
267
        cout<<"XdmValue size="<<val2->size()<<", "<<(val2->itemAt(0))->getStringValue()<<endl;
268
	    if(((XdmItem*)val2->itemAt(0))->isAtomic()) {
269
            sresult->success++;
270
            cout << "Test3: Result is atomic" << endl;
271
            XdmAtomicValue *atomic2 = (XdmAtomicValue *) (val2->itemAt(0));
272
            long result2 = atomic2->getLongValue();
273
            cout << "Result value=" << result2 << endl;
274
            cout << "PrimitiveTypeName of  atomic2=" << atomic2->getPrimitiveTypeName() << endl;
275
        }
276
	    delete val2;
277
	} else {
278
        if (queryProc2->exceptionOccurred()) {
279
            sresult->failure++;
280
            sresult->failureList.push_back("testReusability-2");
281
            SaxonApiException * exception = queryProc2->getException();
282
            if(exception != nullptr) {
283
                cout << "Exception found. " << endl;
284
                const char *message = queryProc2->getErrorMessage();
285
                cout << "Error Message = " << message << endl;
286
                queryProc2->exceptionClear();
287
            }
288
        }
289
	    delete value2;
290
	    delete queryProc2;
291
	    return;
167 292
	}
168 293

  
169
if (queryProc->exceptionOccurred()) {
170
				const char * message = queryProc->getErrorMessage();
171
				if(message != NULL) {
172
					cout<<"Error Message = "<<message<<endl;		
173
				}		
174

  
175
		
176
		}
294
    if (queryProc2->exceptionOccurred()) {
295
        sresult->failure++;
296
        sresult->failureList.push_back("testReusability-3");
297
        SaxonApiException * exception = queryProc2->getException();
298
        if(exception != nullptr) {
299
            cout << "Exception found. " << endl;
300
            const char *message = queryProc2->getErrorMessage();
301
            cout << "Error Message = " << message << endl;
302
            queryProc2->exceptionClear();
303
        }
304
    }
305
    delete value2;
306
    delete input;
307
    delete queryProc2;
177 308
	
178 309
}
179 310

  
......
182 313
void testXQueryLineNumberError(XQueryProcessor * queryProc, sResultCount *sresult){
183 314
 cout<<endl<<"Test testXQueryLineNumberError:"<<endl;
184 315
	queryProc->clearProperties();
185
	queryProc->clearParameters(true);
316
	queryProc->clearParameters();
186 317
   queryProc->setProperty("s", "cat.xml");
187 318

  
188 319
    queryProc->setProperty("qs", "saxon:line-number((//person)[1])");
189 320

  
190 321
    const char * result = queryProc->runQueryToString();
191
    if(result != NULL){
322
    if(result != nullptr){
323
        sresult->success++;
192 324
    	cout<<"Result :"<<result<<endl;
325
    	delete result;
193 326
    } else {
194
	cout<<"Some faiure"<<endl;
195 327
	if (queryProc->exceptionOccurred()) {
328
        sresult->failure++;
329
        sresult->failureList.push_back("testXQueryLineNumberError");
196 330
		    cout<<"Exception found. "<<endl;
197
		    const char * message = queryProc->getException()->getMessage();
198
				if(message != NULL) {
199
					cout<<"Error Message = "<<message<<endl;		
200
				}		
201

  
331
        SaxonApiException * exception = queryProc->getException();
332
        if(exception != nullptr) {
333
            const char *message = queryProc->getErrorMessage();
334
            cout << "Error Message = " << message << endl;
335
            queryProc->exceptionClear();
336
        }
202 337
		
203 338
		}
204 339
	
205 340
    }
341
    queryProc->clearProperties();
342
    queryProc->clearParameters();
206 343

  
207 344
}
208 345

  
209 346
//Test requirement of license file - Test should succeed
210
void testXQueryLineNumber(sResultCount *sresult){
211
  SaxonProcessor * processor = new SaxonProcessor(true);
347
void testXQueryLineNumber(SaxonProcessor * processor, sResultCount *sresult){
348
  //SaxonProcessor * processor = new SaxonProcessor(true);
212 349
  processor->setConfigurationProperty("l", "on");
213 350
  XQueryProcessor * queryProc = processor->newXQueryProcessor();
214 351
 cout<<endl<<"testXQueryLineNumber:"<<endl;
215
	
216
   queryProc->setProperty("s", "cat.xml");
352

  
353

  
354
    //queryProc->setQueryBaseURI(processor->getcwd());
355

  
356
    queryProc->setProperty("s", "cat.xml");
217 357
   queryProc->declareNamespace("saxon","http://saxon.sf.net/");
218 358

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

  
221 361
    const char * result = queryProc->runQueryToString();
222
    if(result != NULL){
362
    if(result != nullptr){
223 363
    	cout<<"Result :"<<result<<endl;
364
    	sresult->success++;
224 365
    } else {
225
	cout<<"Some faiure"<<endl;
226 366
	if (queryProc->exceptionOccurred()) {
367
        sresult->failure++;
368
        sresult->failureList.push_back("testXQueryLineNumber");
227 369
		    cout<<"Exception found."<<endl;
228 370
				const char * message = queryProc->getErrorMessage();
229
				if(message != NULL) {
371
				if(message != nullptr) {
230 372
					cout<<"Error Message = "<<message<<endl;		
231
				}		
373
				}
374

  
375
				queryProc->exceptionClear();
232 376

  
233 377
		
234 378
		}
235 379
	
236 380
    }
237 381
	delete processor;
382
    delete queryProc;
238 383

  
239 384
}
240 385

  
241
int main()
386
int main(int argc, char* argv[])
242 387
{
243 388

  
389
    const char * cwd = nullptr;
390
    if(argc > 0) {
391
        cwd = argv[1];
392
    }
393

  
244 394
    SaxonProcessor * processor = new SaxonProcessor(false);
245 395
    cout<<"Test: XQueryProcessor with Saxon version="<<processor->version()<<endl<<endl;
396

  
397
    char buff[FILENAME_MAX]; //create string buffer to hold path
398
    GetCurrentDir( buff, FILENAME_MAX );
399
    cout<<"CWD = "<< buff<<endl;
400
    if(cwd != nullptr) {
401
        processor->setcwd(cwd);//"/Users/ond1/work/development/git/saxon-dev/saxondev/src/main/c/samples/cppTests"); //set to the current working directory
402
    } else {
403
        processor->setcwd(buff);
404
    }
405

  
406

  
246 407
    XQueryProcessor * query = processor->newXQueryProcessor();
247 408

  
248 409
    sResultCount *sresult = new sResultCount();
......
255 416

  
256 417
    cout<<endl<<"============================================================="<<endl<<endl;
257 418

  
258
    testxQueryError2(processor, query,sresult);
419
    testXQueryError2(processor, query,sresult);
259 420

  
421

  
422
#ifdef MEM_DEBUG
423
    SaxonProcessor::getInfo();
424
#endif
260 425
    cout<<endl<<"============================================================="<<endl<<endl;
261
    testDefaultNamespace(processor, query,sresult);
426
    /*testDefaultNamespace(processor, query,sresult);
262 427

  
263 428
    cout<<endl<<"============================================================="<<endl<<endl;
264
    testReusability(processor, query,sresult);
429
    testReusability(processor, sresult);
265 430
    cout<<endl<<"============================================================="<<endl<<endl;
266 431
    testXQueryLineNumberError(query,sresult);
267 432
    cout<<endl<<"============================================================="<<endl<<endl;
268
    testXQueryLineNumber(sresult);
269

  
270

  
433
    testXQueryLineNumber(processor, sresult);*/
434
cerr<<"testXquery cp0"<<endl;
271 435
    delete query;
436
    cerr<<"testXquery cp0"<<endl;
437

  
438
    delete processor;
439
    cerr<<"testXquery cp0"<<endl;
272 440
    processor->release();
273 441

  
442
#ifdef MEM_DEBUG
443
    SaxonProcessor::getInfo();
444
#endif
274 445

  
275 446
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;
276 447

  
......
288 459

  
289 460
    delete sresult;
290 461

  
462

  
463

  
291 464
#ifdef MEM_DEBUG
292 465
    SaxonProcessor::getInfo();
293 466
#endif
src/main/c/samples/cppTests/testXSLT30.cpp
2175 2175

  
2176 2176
    delete trans;
2177 2177
    delete trans2;
2178
    processor->release();
2179 2178
    delete processor;
2179
    processor->release();
2180 2180

  
2181 2181

  
2182 2182
    cout<<endl<<"======================== Test Results ========================"<<endl<<endl;

Also available in: Unified diff