Project

Profile

Help

Revision 01d6fdb6

Added by O'Neil Delpratt 5 months ago

Saxon/C development

View differences:

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
}

Also available in: Unified diff