Project

Profile

Help

Revision 4ee4355d

Added by O'Neil Delpratt 5 months ago

Saxon/C bug fixes with exception handling and other

View differences:

src/main/c/Saxon.C.API/Xslt30Processor.cpp
7 7
#include "XdmNode.h"
8 8
#include "XdmAtomicValue.h"
9 9
#include "XdmFunctionItem.h"
10
#include "XdmMap.h"
11
#include "XdmArray.h"
10 12
#include "XsltExecutable.h"
11 13
#ifdef DEBUG
12 14
#include <typeinfo> //used for testing only
......
42 44
#endif
43 45

  
44 46
	if(cppXT == nullptr) {
45
	    exception = proc->checkAndCreateException(cppClass);
47
        createException();
46 48
	}
47 49

  
48 50
	if(!(proc->cwd.empty()) && curr.empty()){
......
143 145
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
144 146
            XdmValue * value = itr->second;
145 147
            if(value != nullptr) {
146
                value->decrementRefCount();
147 148
#ifdef DEBUG
148 149
                std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
149 150
#endif
150
                if (value->getRefCount() < 1) {
151 151
                    delete value;
152
                }
152

  
153 153
            }
154 154
        }
155 155

  
156
	} else {
157
        	for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
158

  
159
			parameters.erase(itr);
160
		
161
        	}
162

  
163 156
	}
164 157
	parameters.clear();
165 158
	
......
192 185
    return exception;
193 186
}
194 187

  
188
void Xslt30Processor::createException(const char * message) {
189

  
190
    if(exception != nullptr) {
191
        delete exception;
192
        SaxonProcessor::sxn_environ->env->ExceptionClear();
193
    }
194
    if(message == nullptr) {
195
        exception = proc->checkAndCreateException(cppClass);
196
    } else {
197
        exception = new SaxonApiException(message);
198
    }
199

  
200
}
201

  
195 202

  
196 203

  
197 204
void Xslt30Processor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
......
204 211
	}
205 212
	if (!cAndSNodemID) {
206 213
		std::cerr<<"Error: "<<getDllname()<<"compileFromStringAndSave funtion not found"<<std::endl;
207

  
214
        SaxonProcessor::sxn_environ->env->ExceptionClear();
208 215
	} else {
209 216
        if(cppXT == nullptr) {
210 217
#if defined(DEBUG)
......
217 224
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
218 225
						node->getUnderlyingValue(), SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
219 226
		
220
		exception = proc->checkAndCreateException(cppClass);
227
		createException();
221 228

  
222 229
    }
223 230

  
......
233 240
					"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
234 241
	}
235 242
	if (!cAndSStringmID) {
236
		exception = new SaxonApiException("Error: compileFromStringAndSave function not found\n");
243
        createException("Error: compileFromStringAndSave function not found\n");
237 244

  
238 245
	} else {
239 246
        if(cppXT == nullptr) {
......
248 255
						SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),
249 256
						SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
250 257
		
251
		exception = proc->checkAndCreateException(cppClass);
258
		createException();
252 259

  
253 260
    }
254 261
}
......
264 271
					"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
265 272
	}
266 273
	if (!cAndFStringmID) {
267
		exception = new SaxonApiException("Error: compileFromFileAndSave function not found");
274
        createException("Error: compileFromFileAndSave function not found");
268 275

  
269 276
	} else {
270 277

  
......
278 285
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
279 286
						SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
280 287

  
281
		exception = proc->checkAndCreateException(cppClass);
288
        createException();
282 289

  
283 290

  
284 291
     }
......
293 300
	}
294 301
					
295 302
	if (cStringmID== nullptr) {
296
		exception = new SaxonApiException(strcat(getDllname(), ".compileFromString funtion not found"));
303
        createException(strcat(getDllname(), ".compileFromString funtion not found"));
297 304
		return nullptr;
298 305

  
299 306
	} else {
......
310 317
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
311 318
						SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
312 319
		if (!executableObject) {
313
			exception = proc->checkAndCreateException(cppClass);
320
            createException();
314 321
			return nullptr;
315 322
		}
316 323
		if (comboArrays.stringArray != nullptr) {
......
339 346

  
340 347
	} else {
341 348
	    if(node == nullptr) {
342
	        exception = new SaxonApiException("Error in compileFromXdmNode - node argument is NULL");
349
            createException("Error in compileFromXdmNode - node argument is NULL");
343 350
	        return nullptr;
344 351
	    }
345 352
        if(cppXT == nullptr) {
......
355 362
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
356 363
						node->getUnderlyingValue(), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
357 364
		if (!executableObject) {
358
			exception = proc->checkAndCreateException(cppClass);
365
			createException();
359 366
			return nullptr;
360 367
		}
361 368
		if (comboArrays.stringArray != nullptr) {
......
380 387
	if (!cFilemID) {
381 388
		std::cerr << "Error: "<<getDllname() << ".compileFromAssociatedFile"
382 389
				<< " not found\n" << std::endl;
390
        SaxonProcessor::sxn_environ->env->ExceptionClear();
383 391
		return nullptr;
384 392

  
385 393
	} else {
386 394
		
387 395
		if(source == nullptr) {
388
			exception = new SaxonApiException("Error in compileFromFile method - The Stylesheet file is NULL");
396
            createException("Error in compileFromFile method - The Stylesheet file is NULL");
389 397
			return nullptr;
390 398
		}
391 399
        if(cppXT == nullptr) {
......
401 409
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
402 410
						SaxonProcessor::sxn_environ->env->NewStringUTF(source), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
403 411
		if (!executableObject) {
404
			exception = proc->checkAndCreateException(cppClass);
412
			createException();
405 413
     		return nullptr;
406 414
		}
407 415
		if (comboArrays.stringArray != nullptr) {
......
428 436
	if (!cFilemID) {
429 437
		std::cerr << "Error: "<<getDllname() << ".compileFromFile"
430 438
				<< " not found\n" << std::endl;
431
        std::cerr<<"cp0 -testCatalog"<<std::endl;
439
        SaxonProcessor::sxn_environ->env->ExceptionClear();
432 440
		return nullptr;
433 441

  
434 442
	} else {
435 443
        if (stylesheet == nullptr) {
436
            exception = new SaxonApiException("Error in compileFromFile method - The Stylesheet file is nullptr");
444
            createException("The Stylesheet file is NULL");
437 445
            return nullptr;
438 446
        }
439 447
        if(cppXT == nullptr) {
......
454 462
                                                                           stylesheet), jitCompilation,
455 463
                                                                   comboArrays.stringArray, comboArrays.objectArray));
456 464
        if (!executableObject) {
457
            exception = proc->checkAndCreateException(cppClass);
465
            createException();
458 466
            return nullptr;
459 467

  
460 468
        }
......
482 490
	if(exceptionOccurred()) {
483 491
		//Possible error detected in the compile phase. Processor not in a clean state.
484 492
		//Require clearing exception.
493
		SaxonProcessor::sxn_environ->env->ExceptionDescribe();
494

  
485 495
		return nullptr;
486 496
	}
487 497

  
......
500 510
	}
501 511
	if (!mtvID) {
502 512
        std::cerr<<"Error: "<<getDllname()<<".transformtoValue function not found"<<std::endl;
503

  
513
        SaxonProcessor::sxn_environ->env->ExceptionClear();
504 514
	} else {
505 515
        if(cppXT == nullptr) {
506
#if defined(DEBUG)
516
//#if defined(DEBUG)
507 517
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
508
#endif
518
//#endif
509 519
            return nullptr;
510 520
        }
511 521
	    JParameters comboArrays;
512 522
		comboArrays = SaxonProcessor::createParameterJArray2(parameters);
513

  
523
        jstring jcwd = SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str());
524
        jstring jsourcefile = (sourcefile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : nullptr);
525
        jstring jstylesheetfile = (stylesheetfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : nullptr);
514 526
		jobject result = (jobject)(
515
				SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtvID,
516
						SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
517
						(sourcefile != nullptr ?SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : nullptr),
518
						(stylesheetfile != nullptr ?
519
								SaxonProcessor::sxn_environ->env->NewStringUTF(
520
										stylesheetfile) :
521
								nullptr), comboArrays.stringArray, comboArrays.objectArray));
527
				SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtvID,jcwd, cppXT, nullptr, jsourcefile,
528
						                            jstylesheetfile, comboArrays.stringArray, comboArrays.objectArray));
522 529
		if (comboArrays.stringArray != nullptr) {
523 530
			SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
524 531
			SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
525 532
		}
533
        if (jcwd != nullptr) {
534
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jcwd);
535
        }
536
        if (jsourcefile != nullptr) {
537
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jsourcefile);
538
        }
539
        if (jstylesheetfile != nullptr) {
540
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jstylesheetfile);
541
        }
526 542
		if (result) {
543

  
527 544
			jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
528 545
          		jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
529 546
          		jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
530
			XdmValue * value = nullptr;
547
                jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
548
                jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
549
			    XdmValue * value = nullptr;
531 550
          		XdmItem * xdmItem = nullptr;
532 551

  
533 552

  
534 553
          			if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
535
          				xdmItem = new XdmAtomicValue(SaxonProcessor::sxn_environ->env->NewGlobalRef(result));
554
          				xdmItem = new XdmAtomicValue(result);
536 555
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
537 556
                        return xdmItem;
538 557

  
539 558
          			} else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
540
          				xdmItem = new XdmNode(SaxonProcessor::sxn_environ->env->NewGlobalRef(result));
559
          				xdmItem = new XdmNode(result);
541 560
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
542 561
                        return xdmItem;
543
#if CVERSION_API_NO >= 123
562

  
544 563
          			} else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
545
                        xdmItem =  new XdmFunctionItem(SaxonProcessor::sxn_environ->env->NewGlobalRef(result));
564
                        xdmItem =  new XdmFunctionItem(result);
546 565
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
547 566
                        return xdmItem;
548
#endif
549
          			} else {
567

  
568
          			} else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapClass)           == JNI_TRUE) {
569
                        xdmItem =  new XdmMap(result);
570
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
571
                        return xdmItem;
572

  
573
                    } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayClass)           == JNI_TRUE) {
574
                        xdmItem =  new XdmArray(result);
575
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
576
                        return xdmItem;
577

  
578
                    } else {
550 579
					value = new XdmValue(result, true);
551 580
					SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
552 581
					return value;
......
557 586
          		SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
558 587
          		return value;
559 588
		}else {
560
	
561
			exception = proc->checkAndCreateException(cppClass);
562
	   		
589

  
590
			createException();
591

  
563 592
     		}
564 593
	}
565 594
	return nullptr;
......
589 618
	}
590 619
	if (!mtfID) {
591 620
        std::cerr<<"Error: "<<getDllname()<<".transformToFile function not found"<<std::endl;
592

  
621
        SaxonProcessor::sxn_environ->env->ExceptionClear();
593 622
	} else {
594 623
        if(cppXT == nullptr) {
595 624
#if defined(DEBUG)
......
610 639
			SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
611 640
		}
612 641
		}
613
		exception = proc->checkAndCreateException(cppClass);
642
		createException();
614 643
	}
615 644

  
616 645

  
......
632 661
	if(mtsID == nullptr) {
633 662
			mtsID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
634 663
					"transformToString",
635
					"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
664
					"(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
636 665
	}
637 666
	if (!mtsID) {
638 667
        std::cerr<<"Error: "<<getDllname()<<".transformFileToString not found"<<std::endl;
639

  
668
        SaxonProcessor::sxn_environ->env->ExceptionClear();
640 669
	} else {
641 670
        if(cppXT == nullptr) {
642 671
#if defined(DEBUG)
......
647 676
    JParameters comboArrays;
648 677
    comboArrays = SaxonProcessor::createParameterJArray2(parameters);
649 678

  
650
	jstring result = nullptr;
679
    jbyteArray result = nullptr;
651 680
	jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtsID,
652 681
								SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
653 682
						(source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(
......
659 688
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
660 689
    }
661 690
	if(obj) {
662
			result = (jstring)obj;
691
			result = (jbyteArray)obj;
663 692
	}
664 693

  
665 694
	if (result) {
666
			const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
667
					nullptr);
668
			SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
669
			return str;
670
		} else  {
671
			exception =proc->checkAndCreateException(cppClass);
695
        jboolean isCopy = false;
696
        jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
697
        jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
698

  
699
        const char *str = new char[num_bytes];
700
        memcpy ((void *) str, b , num_bytes );
701

  
702
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
703
        return str;
704
	} else  {
705
	    createException();
672 706
	   		
673
     		}
707
	}
674 708
	}
675 709
	return nullptr;
676 710
}

Also available in: Unified diff