Project

Profile

Help

Support #4847 ยป Xslt30Processor.cpp

O'Neil Delpratt, 2020-12-03 11:27

 
1
// Xslt30Processor.cpp : Defines the exported functions for the DLL application.
2
//
3

    
4
#include "Xslt30Processor.h"
5
#include "XdmValue.h"
6
#include "XdmItem.h"
7
#include "XdmNode.h"
8
#include "XdmAtomicValue.h"
9
#ifdef DEBUG
10
#include <typeinfo> //used for testing only
11
#endif
12

    
13
Xslt30Processor::Xslt30Processor() {
14

    
15
    SaxonProcessor *p = new SaxonProcessor(false);
16
    Xslt30Processor(p, "");
17

    
18
}
19

    
20
Xslt30Processor::Xslt30Processor(SaxonProcessor * p, std::string curr) {
21

    
22
    proc = p;
23

    
24
    /*
25
     * Look for class.
26
     */
27
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
28
                            "net/sf/saxon/option/cpp/Xslt30Processor");
29

    
30
    cppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
31
                                  "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
32

    
33
#ifdef DEBUG
34
    jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
35
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
36

    
37
#endif
38
    nodeCreated = false;
39
    tunnel = false;
40
    proc->exception = NULL;
41
    selection = NULL;
42
    selectionV=NULL;
43
    outputfile1 = "";
44
    if(!(proc->cwd.empty()) && curr.empty()){
45
        cwdXT = proc->cwd;
46
    } else if(!curr.empty()){
47
        cwdXT = curr;
48
    }
49

    
50
}
51

    
52
Xslt30Processor::~Xslt30Processor(){
53
    clearProperties();
54
    clearParameters();
55
    if(selectionV != NULL) {
56
        selectionV->decrementRefCount();
57
        if(selectionV->getRefCount() == 0) {
58
            delete selectionV;
59
        }
60
    }
61

    
62
}
63

    
64
bool Xslt30Processor::exceptionOccurred() {
65
    return proc->exceptionOccurred();
66
}
67

    
68
const char * Xslt30Processor::getErrorCode(int i) {
69
    if(proc->exception == NULL) {return NULL;}
70
    return proc->exception->getErrorCode(i);
71
}
72

    
73

    
74

    
75
void Xslt30Processor::setGlobalContextItem(XdmItem * value){
76
    if(value != NULL){
77
        value->incrementRefCount();
78
        parameters["node"] = value;
79
    }
80
}
81

    
82
void Xslt30Processor::setGlobalContextFromFile(const char * ifile) {
83
    if(ifile != NULL) {
84
        setProperty("s", ifile);
85
    }
86
}
87

    
88
void Xslt30Processor::setInitialMatchSelection(XdmValue * _selection){
89
    if(_selection != NULL) {
90
        _selection->incrementRefCount();
91
        selectionV = _selection;
92
        selection = _selection->getUnderlyingValue();
93
    }
94
}
95

    
96

    
97
void Xslt30Processor::setInitialMatchSelectionAsFile(const char * filename){
98
    if(filename != NULL) {
99
        selection = SaxonProcessor::sxn_environ->env->NewStringUTF(filename);
100
    }
101
}
102

    
103
void Xslt30Processor::setOutputFile(const char * ofile) {
104
    outputfile1 = std::string(ofile);
105
    setProperty("o", ofile);
106
}
107

    
108
void Xslt30Processor::setParameter(const char* name, XdmValue * value, bool _static) {
109
    if(value != NULL && name != NULL){
110
        value->incrementRefCount();
111
        if (_static) {
112
            parameters["sparam:"+std::string(name)] = value;
113

    
114
        } else {
115
            parameters["param:"+std::string(name)] = value;
116
        }
117
    }
118
}
119

    
120
void Xslt30Processor::setInitialTemplateParameters(std::map<std::string,XdmValue*> _itparameters, bool _tunnel){
121
    for(std::map<std::string, XdmValue*>::iterator itr = _itparameters.begin(); itr != _itparameters.end(); itr++){
122
        parameters["itparam:"+std::string(itr->first)] = itr->second;
123
    }
124
    tunnel = _tunnel;
125
    if(tunnel) {
126
        setProperty("tunnel", "true");
127
    }
128
}
129

    
130
XdmValue* Xslt30Processor::getParameter(const char* name) {
131
    std::map<std::string, XdmValue*>::iterator it;
132
    it = parameters.find("param:"+std::string(name));
133
    if (it != parameters.end())
134
        return it->second;
135
    else {
136
        it = parameters.find("sparam:"+std::string(name));
137
        if (it != parameters.end())
138
            return it->second;
139
    }
140
    return NULL;
141
}
142

    
143
bool Xslt30Processor::removeParameter(const char* name) {
144
    return (bool)(parameters.erase("param:"+std::string(name)));
145
}
146

    
147
void Xslt30Processor::setJustInTimeCompilation(bool jit){
148
    static jmethodID jitmID =
149
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
150
                                                                      "setJustInTimeCompilation",
151
                                                                      "(Z)V");
152
    if (!jitmID) {
153
        std::cerr << "Error: "<<getDllname() << ".setJustInTimeCompilation"
154
                  << " not found\n" << std::endl;
155
        return;
156
    } else {
157
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, jitmID, jit);
158

    
159
        proc->checkAndCreateException(cppClass);
160
    }
161
}
162

    
163
void Xslt30Processor::setResultAsRawValue(bool option) {
164
    if(option) {
165
        setProperty("outvalue", "yes");
166
    }
167
}
168

    
169
void Xslt30Processor::setProperty(const char* name, const char* value) {
170
    if(name != NULL) {
171
        properties.insert(std::pair<std::string, std::string>(std::string(name), std::string((value == NULL ? "" : value))));
172
    }
173
}
174

    
175
const char* Xslt30Processor::getProperty(const char* name) {
176
    std::map<std::string, std::string>::iterator it;
177
    it = properties.find(std::string(name));
178
    if (it != properties.end())
179
        return it->second.c_str();
180
    return NULL;
181
}
182

    
183
void Xslt30Processor::clearParameters(bool delValues) {
184
    if(delValues){
185
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
186

    
187
            XdmValue * value = itr->second;
188
            value->decrementRefCount();
189
#ifdef DEBUG
190
            std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
191
#endif
192
            if(value != NULL && value->getRefCount() < 1){
193
                delete value;
194
            }
195
        }
196

    
197
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(selection);
198
        selection = NULL;
199
    } else {
200
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
201

    
202
            XdmValue * value = itr->second;
203
            value->decrementRefCount();
204

    
205
        }
206

    
207
        selection = NULL;
208
    }
209
    parameters.clear();
210

    
211

    
212
}
213

    
214
void Xslt30Processor::clearProperties() {
215
    properties.clear();
216

    
217
}
218

    
219

    
220

    
221
std::map<std::string,XdmValue*>& Xslt30Processor::getParameters(){
222
    std::map<std::string,XdmValue*>& ptr = parameters;
223
    return ptr;
224
}
225

    
226
std::map<std::string,std::string>& Xslt30Processor::getProperties(){
227
    std::map<std::string,std::string> &ptr = properties;
228
    return ptr;
229
}
230

    
231
void Xslt30Processor::exceptionClear(){
232
    if(proc->exception != NULL) {
233
        delete proc->exception;
234
        proc->exception = NULL;
235
        SaxonProcessor::sxn_environ->env->ExceptionClear();
236
    }
237

    
238
}
239

    
240
void Xslt30Processor::setcwd(const char* dir){
241
    if (dir!= NULL) {
242
        cwdXT = std::string(dir);
243
    }
244
}
245

    
246
const char* Xslt30Processor::checkException() {
247
    /*if(proc->exception == NULL) {
248
     proc->exception = proc->checkForException(environi, cpp);
249
     }
250
     return proc->exception;*/
251
    return proc->checkException(cppXT);
252
}
253

    
254
int Xslt30Processor::exceptionCount(){
255
    if(proc->exception != NULL){
256
        return proc->exception->count();
257
    }
258
    return 0;
259
}
260

    
261

    
262
void Xslt30Processor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
263
    static jmethodID cAndSNodemID =
264
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
265
                                                                      "compileFromXdmNodeAndSave",
266
                                                                      "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V");
267
    if (!cAndSNodemID) {
268
        std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
269
                  << " not found\n" << std::endl;
270

    
271
    } else {
272

    
273

    
274
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSNodemID,
275
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
276
                                                           node->getUnderlyingValue(),                             SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
277

    
278
        proc->checkAndCreateException(cppClass);
279

    
280
    }
281

    
282

    
283

    
284
}
285

    
286
void Xslt30Processor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
287
    static jmethodID cAndSStringmID =
288
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
289
                                                                      "compileFromStringAndSave",
290
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
291
    if (!cAndSStringmID) {
292
        std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
293
                  << " not found\n" << std::endl;
294

    
295
    } else {
296

    
297

    
298
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
299
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
300
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),                             SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
301

    
302
        proc->checkAndCreateException(cppClass);
303

    
304
    }
305
}
306

    
307

    
308

    
309
void Xslt30Processor::compileFromFileAndSave(const char* xslFilename, const char* filename){
310
    static jmethodID cAndFStringmID =
311
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
312
                                                                      "compileFromFileAndSave",
313
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
314
    if (!cAndFStringmID) {
315
        std::cerr << "Error: "<<getDllname() << ".compileFromFileAndSave"
316
                  << " not found\n" << std::endl;
317

    
318
    } else {
319

    
320

    
321
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
322
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
323
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
324

    
325
        proc->checkAndCreateException(cppClass);
326

    
327

    
328
    }
329
}
330

    
331
void Xslt30Processor::compileFromString(const char* stylesheetStr) {
332
    static jmethodID cStringmID =
333
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
334
                                                                      "createStylesheetFromString",
335
                                                                      "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
336
    if (!cStringmID) {
337
        std::cerr << "Error: "<<getDllname() << ".createStylesheetFromString"
338
                  << " not found\n" << std::endl;
339

    
340
    } else {
341
        JParameters comboArrays;
342
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
343
        stylesheetObject = (jobject)(
344
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
345
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
346
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr), comboArrays.stringArray, comboArrays.objectArray));
347
        if (!stylesheetObject) {
348
            proc->checkAndCreateException(cppClass);
349
        }
350
        if (comboArrays.stringArray != NULL) {
351
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
352
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
353
        }
354
    }
355

    
356
}
357

    
358
void Xslt30Processor::compileFromXdmNode(XdmNode * node) {
359
    static jmethodID cNodemID =
360
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,"createStylesheetFromXdmNode",
361
                                                                      "(Ljava/lang/String;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
362
    if (!cNodemID) {
363
        std::cerr << "Error: "<< getDllname() << ".createStylesheetFromXdmNode"
364
                  << " not found\n" << std::endl;
365

    
366
    } else {
367
        releaseStylesheet();
368
        JParameters comboArrays;
369
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
370
        stylesheetObject = (jobject)(
371
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cNodemID,
372
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
373
                                                                   node->getUnderlyingValue(), comboArrays.stringArray, comboArrays.objectArray));
374
        if (!stylesheetObject) {
375
            proc->checkAndCreateException(cppClass);
376
            //cout << "Error in compileFromXdmNode" << endl;
377
        }
378
        if (comboArrays.stringArray != NULL) {
379
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
380
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
381
        }
382
    }
383

    
384
}
385

    
386
void Xslt30Processor::compileFromAssociatedFile(const char* source) {
387
    static jmethodID cFilemID =
388
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
389
                                                                      "createStylesheetFromAssoicatedFile",
390
                                                                      "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
391
    if (!cFilemID) {
392
        std::cerr << "Error: "<<getDllname() << ".createStylesheetFromFile"
393
                  << " not found\n" << std::endl;
394

    
395
    } else {
396
        releaseStylesheet();
397
        if(source == NULL) {
398
            std::cerr << "Error in compileFromFile method - The Stylesheet file is NULL" <<std::endl;
399
            return;
400
        }
401
        JParameters comboArrays;
402
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
403
        stylesheetObject = (jobject)(
404
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
405
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
406
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(source), comboArrays.stringArray, comboArrays.objectArray));
407
        if (!stylesheetObject) {
408
            proc->checkAndCreateException(cppClass);
409

    
410
        }
411
        if (comboArrays.stringArray != NULL) {
412
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
413
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
414
        }
415
        //SaxonProcessor::sxn_environ->env->NewGlobalRef(stylesheetObject);
416
    }
417

    
418
}
419

    
420

    
421
void Xslt30Processor::compileFromFile(const char* stylesheet) {
422
    static jmethodID cFilemID =
423
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
424
                                                                      "createStylesheetFromFile",
425
                                                                      "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
426
    if (!cFilemID) {
427
        std::cerr << "Error: "<<getDllname() << ".createStylesheetFromFile"
428
                  << " not found\n" << std::endl;
429

    
430
    } else {
431
        releaseStylesheet();
432
        if(stylesheet == NULL) {
433
            std::cerr << "Error in compileFromFile method - The Stylesheet file is NULL" <<std::endl;
434
            return;
435
        }
436
        JParameters comboArrays;
437
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
438
        stylesheetObject = (jobject)(
439
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
440
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
441
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet), comboArrays.stringArray, comboArrays.objectArray));
442
        if (!stylesheetObject) {
443
            proc->checkAndCreateException(cppClass);
444

    
445
        }
446
        if (comboArrays.stringArray != NULL) {
447
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
448
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
449
        }
450
        //SaxonProcessor::sxn_environ->env->NewGlobalRef(stylesheetObject);
451
    }
452

    
453
}
454

    
455
void Xslt30Processor::releaseStylesheet() {
456

    
457
    stylesheetObject = NULL;
458

    
459
}
460

    
461
void Xslt30Processor::applyTemplatesReturningFile(const char * stylesheetfile, const char* output_filename){
462
    if(exceptionOccurred()) {
463
        //Possible error detected in the compile phase. Processor not in a clean state.
464
        //Require clearing exception.
465
        return;
466
    }
467

    
468
    if(selection == NULL) {
469
        std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
470
        return;
471
    }
472

    
473
    if(stylesheetfile == NULL && !stylesheetObject){
474

    
475
        return;
476
    }
477

    
478
    setProperty("resources", proc->getResourcesDirectory());
479
    static jmethodID atmID =
480
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
481
                                                                      "applyTemplatesReturningFile",
482
                                                                      "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
483
    if (!atmID) {
484
        std::cerr << "Error: "<< getDllname() << "applyTemplatesAsFile" << " not found\n"
485
                  << std::endl;
486

    
487
    } else {
488
        JParameters comboArrays;
489
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
490
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, atmID,
491
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),selection,
492
                                                           (stylesheetfile != NULL ?
493
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(
494
                                                                    stylesheetfile) :
495
                                                            NULL), (output_filename != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(
496
                        output_filename) : NULL),
497
                                                           comboArrays.stringArray, comboArrays.objectArray);
498
        if (comboArrays.stringArray != NULL) {
499
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
500
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
501
        }
502
        proc->checkAndCreateException(cppClass);
503

    
504
    }
505
    return;
506

    
507
}
508

    
509
const char* Xslt30Processor::applyTemplatesReturningString(const char * stylesheetfile){
510
    if(exceptionOccurred()) {
511
        //Possible error detected in the compile phase. Processor not in a clean state.
512
        //Require clearing exception.
513
        return NULL;
514
    }
515
    if(selection == NULL) {
516
        std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
517
        return NULL;
518
    }
519
    if(stylesheetfile == NULL && !stylesheetObject){
520
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling applyTemplatesReturningString or check exceptions"<<std::endl;
521
        return NULL;
522
    }
523
    setProperty("resources", proc->getResourcesDirectory());
524
    jmethodID atsmID =
525
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
526
                                                                      "applyTemplatesReturningString",
527
                                                                      "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
528
    if (!atsmID) {
529
        std::cerr << "Error: "<<getDllname() << "applyTemplatesAsString" << " not found\n"
530
                  << std::endl;
531

    
532
    } else {
533
        JParameters comboArrays;
534
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
535

    
536
        jstring result = NULL;
537
        jobject obj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, atsmID,
538
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
539
                                                                          selection,
540
                                                                          (stylesheetfile!= NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : NULL ),
541
                                                                          comboArrays.stringArray, comboArrays.objectArray));
542

    
543
        if(obj) {
544
            result = (jstring)obj;
545
        }
546
        if (comboArrays.stringArray != NULL) {
547
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
548
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
549
        }
550
        if (result) {
551
            const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
552
                                                                                   NULL);
553
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
554
            return str;
555
        } else  {
556
            proc->checkAndCreateException(cppClass);
557

    
558
        }
559
    }
560
    return NULL;
561

    
562

    
563
}
564

    
565
XdmValue * Xslt30Processor::applyTemplatesReturningValue(const char * stylesheetfile){
566
    if(exceptionOccurred()) {
567
        //Possible error detected in the compile phase. Processor not in a clean state.
568
        //Require clearing exception.
569
        return NULL;
570
    }
571
    if(selection == NULL) {
572
        std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
573
        return NULL;
574
    }
575
    if(stylesheetfile == NULL && !stylesheetObject){
576
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling applyTemplatesReturningValue or check exceptions"<<std::endl;
577
        return NULL;
578
    }
579
    setProperty("resources", proc->getResourcesDirectory());
580
    jmethodID atsmID =
581
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
582
                                                                      "applyTemplatesReturningValue",
583
                                                                      "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
584
    if (!atsmID) {
585
        std::cerr << "Error: "<<getDllname() << "applyTemplatesAsValue" << " not found\n"
586
                  << std::endl;
587

    
588
    } else {
589
        JParameters comboArrays;
590
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
591

    
592

    
593
        // jstring result = NULL;
594
        jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, atsmID,
595
                                                                                      SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
596
                                                                                      selection,
597
                                                                                      ( stylesheetfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : NULL),
598
                                                                                      comboArrays.stringArray, comboArrays.objectArray));
599
        /*if(obj) {
600
            result = (jobject)obj;
601
        }*/
602
        if (comboArrays.stringArray != NULL) {
603
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
604
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
605
        }
606
        if (result) {
607
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
608
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
609
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
610
            XdmValue * value = NULL;
611
            XdmItem * xdmItem = NULL;
612

    
613

    
614
            if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
615
                xdmItem =  new XdmAtomicValue(result);
616
                xdmItem->setProcessor(proc);
617
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
618
                return xdmItem;
619

    
620
            } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
621
                xdmItem =  new XdmNode(result);
622
                xdmItem->setProcessor(proc);
623
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
624
                return xdmItem;
625
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
626
                std::cerr<<"Error: applyTemplateToValue: FunctionItem found. Currently not be handled"<<std::endl;
627
                return NULL;
628
            } else {
629
                value = new XdmValue(result, true);
630
                value->setProcessor(proc);
631
                for(int z=0;z<value->size();z++) {
632
                    value->itemAt(z)->setProcessor(proc);
633
                }
634
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
635
                return value;
636
            }
637
        } else  {
638
            proc->checkAndCreateException(cppClass);
639

    
640
        }
641
    }
642
    return NULL;
643

    
644
}
645

    
646

    
647

    
648
void Xslt30Processor::callFunctionReturningFile(const char * stylesheetfile, const char* functionName, XdmValue ** arguments, int argument_length, const char* outfile){
649
    if(exceptionOccurred()) {
650
        //Possible error detected in the compile phase. Processor not in a clean state.
651
        //Require clearing exception.
652
        return;
653
    }
654

    
655

    
656
    if(stylesheetfile == NULL && !stylesheetObject){
657

    
658
        return;
659
    }
660

    
661
    setProperty("resources", proc->getResourcesDirectory());
662
    static jmethodID afmID =
663
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
664
                                                                      "callFunctionReturningFile",
665
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
666
    if (!afmID) {
667
        std::cerr << "Error: "<< getDllname() << "callFunctionReturningFile" << " not found\n"
668
                  << std::endl;
669
        return;
670
    } else {
671
        JParameters comboArrays;
672
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
673

    
674
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
675

    
676
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, afmID,
677
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
678
                                                           (stylesheetfile != NULL ?
679
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(
680
                                                                    stylesheetfile) :
681
                                                            NULL),
682
                                                           (functionName != NULL ?
683
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) :
684
                                                            NULL), argumentJArray,
685
                                                           (outfile != NULL ?
686
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) :
687
                                                            NULL),
688
                                                           comboArrays.stringArray, comboArrays.objectArray);
689
        if (comboArrays.stringArray != NULL) {
690
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
691
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
692
        }
693
        proc->checkAndCreateException(cppClass);
694

    
695
    }
696
    return;
697

    
698

    
699

    
700

    
701
}
702

    
703
const char * Xslt30Processor::callFunctionReturningString(const char * stylesheet, const char* functionName, XdmValue ** arguments, int argument_length){
704
    if(exceptionOccurred()) {
705
        //Possible error detected in the compile phase. Processor not in a clean state.
706
        //Require clearing exception.
707
        return NULL;
708
    }
709

    
710
    if(stylesheet == NULL && !stylesheetObject){
711
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callFunctionReturningString or check exceptions"<<std::endl;
712
        return NULL;
713
    }
714
    setProperty("resources", proc->getResourcesDirectory());
715
    jmethodID afsmID =
716
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
717
                                                                      "callFunctionReturningString",
718
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
719
    if (!afsmID) {
720
        std::cerr << "Error: "<<getDllname() << "callFunctionReturningString" << " not found\n"
721
                  << std::endl;
722

    
723
    } else {
724
        JParameters comboArrays;
725
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
726
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
727

    
728
        jstring result = NULL;
729
        jobject obj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, afsmID,
730
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
731
                                                                          (stylesheet != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : NULL ),
732
                                                                          (functionName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) : NULL),
733
                                                                          argumentJArray, comboArrays.stringArray, comboArrays.objectArray));
734
        if(obj) {
735
            result = (jstring)obj;
736
        }
737
        if (comboArrays.stringArray != NULL) {
738
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
739
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
740
        }
741
        if (result) {
742
            const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
743
                                                                                   NULL);
744
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
745
            return str;
746
        } else  {
747
            proc->checkAndCreateException(cppClass);
748

    
749
        }
750
    }
751
    return NULL;
752
}
753

    
754

    
755

    
756
XdmValue * Xslt30Processor::callFunctionReturningValue(const char * stylesheet, const char* functionName, XdmValue ** arguments, int argument_length){
757
    if(exceptionOccurred()) {
758
        //Possible error detected in the compile phase. Processor not in a clean state.
759
        //Require clearing exception.
760
        return NULL;
761
    }
762

    
763
    if(stylesheet == NULL && !stylesheetObject){
764
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callFunctionReturningValue or check exceptions"<<std::endl;
765
        return NULL;
766
    }
767
    setProperty("resources", proc->getResourcesDirectory());
768
    jmethodID cfvmID =
769
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
770
                                                                      "callFunctionReturningValue",
771
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
772
    if (!cfvmID) {
773
        std::cerr << "Error: "<<getDllname() << "callFunctionReturningValue" << " not found\n"
774
                  << std::endl;
775

    
776
    } else {
777
        JParameters comboArrays;
778
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
779
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
780

    
781
        jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cfvmID,
782
                                                                                      SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
783
                                                                                      (stylesheet != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : NULL ),
784
                                                                                      (functionName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) : NULL),
785
                                                                                      argumentJArray, comboArrays.stringArray, comboArrays.objectArray));
786

    
787
        if (comboArrays.stringArray != NULL) {
788
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
789
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
790
        }
791
        if(argumentJArray != NULL) {
792
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
793
        }
794
        if (result) {
795
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
796
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
797
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
798
            XdmValue * value = NULL;
799
            XdmItem * xdmItem = NULL;
800

    
801

    
802
            if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
803
                xdmItem = new XdmAtomicValue(result);
804
                xdmItem->setProcessor(proc);
805
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
806
                return xdmItem;
807
            } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
808
                xdmItem = new XdmNode(result);
809
                xdmItem->setProcessor(proc);
810
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
811
                return xdmItem;
812

    
813
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
814
                return NULL;
815
            } else {
816
                value = new XdmValue(result, true);
817
                value->setProcessor(proc);
818
                for(int z=0;z<value->size();z++) {
819
                    value->itemAt(z)->setProcessor(proc);
820
                }
821
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
822
                return value;
823
            }
824
            value = new XdmValue();
825
            value->setProcessor(proc);
826
            xdmItem->setProcessor(proc);
827
            value->addXdmItem(xdmItem);
828
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
829
            return value;
830
        } else  {
831
            proc->checkAndCreateException(cppClass);
832

    
833
        }
834
    }
835
    return NULL;
836

    
837
}
838

    
839
void Xslt30Processor::addPackages(const char ** fileNames, int length){
840
    if(exceptionOccurred()) {
841
        //Possible error detected in the compile phase. Processor not in a clean state.
842
        //Require clearing exception.
843
        return;
844
    }
845

    
846
    if(length<1){
847

    
848
        return;
849
    }
850

    
851
    jmethodID apmID =
852
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
853
                                                                      "addPackages",
854
                                                                      "([Ljava/lang/String;)V");
855
    if (!apmID) {
856
        std::cerr << "Error: "<<getDllname() << "addPackage" << " not found\n"
857
                  << std::endl;
858

    
859
    } else {
860

    
861
        jobjectArray stringArray = NULL;
862

    
863
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
864
                                          "java/lang/String");
865

    
866

    
867
        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) length,
868
                                                                       stringClass, 0);
869

    
870
        for (int i=0; i<length; i++) {
871

    
872
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
873
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(fileNames[i]));
874
        }
875

    
876
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, apmID,
877
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), stringArray);
878

    
879
        proc->checkAndCreateException(cppClass);
880
    }
881
    return;
882

    
883
}
884

    
885

    
886

    
887
void Xslt30Processor::clearPackages(){
888
    if(exceptionOccurred()) {
889
        //Possible error detected in the compile phase. Processor not in a clean state.
890
        //Require clearing exception.
891
        return;
892
    }
893

    
894

    
895

    
896
    jmethodID cpmID =
897
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
898
                                                                      "clearPackages",
899
                                                                      "()V");
900
    if (!cpmID) {
901
        std::cerr << "Error: "<<getDllname() << "clearPackage" << " not found\n"
902
                  << std::endl;
903

    
904
    } else {
905

    
906

    
907
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cpmID);
908

    
909
        proc->checkAndCreateException(cppClass);
910
    }
911
    return;
912

    
913

    
914

    
915

    
916
}
917

    
918
void Xslt30Processor::callTemplateReturningFile(const char * stylesheetfile, const char* templateName, const char* outfile){
919
    if(exceptionOccurred()) {
920
        //Possible error detected in the compile phase. Processor not in a clean state.
921
        //Require clearing exception.
922
        return;
923
    }
924

    
925

    
926
    if(stylesheetfile == NULL && !stylesheetObject){
927
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callFunctionReturningFile or check exceptions"<<std::endl;
928
        return;
929
    }
930

    
931
    setProperty("resources", proc->getResourcesDirectory());
932
    static jmethodID ctmID =
933
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
934
                                                                      "callTemplateReturningFile",
935
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
936
    if (!ctmID) {
937
        std::cerr << "Error: "<< getDllname() << "callTemplateReturningFile" << " not found\n"
938
                  << std::endl;
939

    
940
    } else {
941
        JParameters comboArrays;
942
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
943
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctmID,
944
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
945
                                                           (stylesheetfile != NULL ?
946
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(
947
                                                                    stylesheetfile) :
948
                                                            NULL),
949
                                                           (templateName != NULL ?
950
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) :
951
                                                            NULL),
952
                                                           (outfile != NULL ?
953
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) :
954
                                                            NULL),
955
                                                           comboArrays.stringArray, comboArrays.objectArray);
956
        if (comboArrays.stringArray != NULL) {
957
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
958
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
959
        }
960
        proc->checkAndCreateException(cppClass);
961

    
962
    }
963
    return;
964

    
965

    
966
}
967

    
968

    
969

    
970

    
971
const char* Xslt30Processor::callTemplateReturningString(const char * stylesheet, const char* templateName){
972
    if(exceptionOccurred()) {
973
        //Possible error detected in the compile phase. Processor not in a clean state.
974
        //Require clearing exception.
975
        return NULL;
976
    }
977

    
978
    if(stylesheet == NULL && !stylesheetObject){
979
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callTemplateReturningString or check exceptions"<<std::endl;
980
        return NULL;
981
    }
982
    setProperty("resources", proc->getResourcesDirectory());
983
    jmethodID ctsmID =
984
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
985
                                                                      "callTemplateReturningString",
986
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
987
    if (!ctsmID) {
988
        std::cerr << "Error: "<<getDllname() << "callTemplateReturningString" << " not found\n"
989
                  << std::endl;
990

    
991
    } else {
992
        JParameters comboArrays;
993
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
994

    
995

    
996
        jstring result = NULL;
997
        jobject obj =(jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctsmID,
998
                                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
999
                                                                                  (stylesheet != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : NULL ),
1000
                                                                                  (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
1001
                                                                                  comboArrays.stringArray, comboArrays.objectArray));
1002
        if(obj) {
1003
            result = (jstring)obj;
1004
        }
1005
        if (comboArrays.stringArray != NULL) {
1006
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1007
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1008
        }
1009
        if (result) {
1010
            const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
1011
                                                                                   NULL);
1012
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
1013
            return str;
1014
        } else  {
1015
            proc->checkAndCreateException(cppClass);
1016

    
1017
        }
1018
    }
1019
    return NULL;
1020

    
1021

    
1022
}
1023

    
1024
XdmValue* Xslt30Processor::callTemplateReturningValue(const char * stylesheet, const char* templateName){
1025
    if(exceptionOccurred()) {
1026
        //Possible error detected in the compile phase. Processor not in a clean state.
1027
        //Require clearing exception.
1028
        return NULL;
1029
    }
1030
    if(stylesheet == NULL && !stylesheetObject){
1031
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callTemplateReturningValue or check exceptions"<<std::endl;
1032
        return NULL;
1033
    }
1034
    setProperty("resources", proc->getResourcesDirectory());
1035
    jmethodID ctsmID =
1036
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
1037
                                                                      "callTemplateReturningValue",
1038
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
1039
    if (!ctsmID) {
1040
        std::cerr << "Error: "<<getDllname() << "callTemplateReturningValue" << " not found\n"
1041
                  << std::endl;
1042

    
1043
    } else {
1044
        JParameters comboArrays;
1045
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
1046

    
1047

    
1048
        jstring result = NULL;
1049
        jobject obj = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctsmID,
1050
                                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
1051
                                                                                   (stylesheet != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : NULL ),
1052
                                                                                   (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
1053
                                                                                   comboArrays.stringArray, comboArrays.objectArray));
1054
        if(obj) {
1055
            result = (jstring)obj;
1056
        }
1057
        if (comboArrays.stringArray != NULL) {
1058
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1059
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1060
        }
1061
        if (result) {
1062
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
1063
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
1064
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
1065
            XdmValue * value = NULL;
1066

    
1067
            XdmItem * xdmItem = NULL;
1068
            if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
1069
                xdmItem =  new XdmAtomicValue(result);
1070
                xdmItem->setProcessor(proc);
1071
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
1072
                return xdmItem;
1073

    
1074
            } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
1075
                xdmItem = new XdmNode(result);
1076
                xdmItem->setProcessor(proc);
1077
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
1078
                return xdmItem;
1079

    
1080
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
1081
                std::cerr<<"Error: callTemplateReturningValue: FunctionItem found. Currently not be handled"<<std::endl;
1082
                return NULL;
1083
            } else {
1084
                value = new XdmValue(result, true);
1085
                value->setProcessor(proc);
1086
                for(int z=0;z<value->size();z++) {
1087
                    value->itemAt(z)->setProcessor(proc);
1088
                }
1089
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
1090
                return value;
1091
            }
1092
            value = new XdmValue();
1093
            value->setProcessor(proc);
1094
            xdmItem->setProcessor(proc);
1095
            value->addXdmItem(xdmItem);
1096
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
1097
            return value;
1098
        } else  {
1099
            proc->checkAndCreateException(cppClass);
1100
        }
1101
    }
1102
    return NULL;
1103
}
1104

    
1105

    
1106

    
1107

    
1108
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
1109
                                                 const char* stylesheetfile) {
1110

    
1111
    if(exceptionOccurred()) {
1112
        //Possible error detected in the compile phase. Processor not in a clean state.
1113
        //Require clearing exception.
1114
        return NULL;
1115
    }
1116

    
1117
    if(sourcefile == NULL && stylesheetfile == NULL && !stylesheetObject){
1118

    
1119
        return NULL;
1120
    }
1121

    
1122
    setProperty("resources", proc->getResourcesDirectory());
1123
    static jmethodID mID =
1124
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
1125
                                                                      "transformToValue",
1126
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
1127
    if (!mID) {
1128
        std::cerr << "Error: "<< getDllname() << ".transformtoValue" << " not found\n"
1129
                  << std::endl;
1130

    
1131
    } else {
1132
        JParameters comboArrays;
1133
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
1134

    
1135
        jobject result = (jobject)(
1136
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
1137
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
1138
                                                                   (sourcefile != NULL ?
1139
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) :
1140
                                                                    NULL),
1141
                                                                   (stylesheetfile != NULL ?
1142
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(
1143
                                                                            stylesheetfile) :
1144
                                                                    NULL), comboArrays.stringArray, comboArrays.objectArray));
1145
        if (comboArrays.stringArray != NULL) {
1146
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1147
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1148
        }
1149
        if (result) {
1150
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
1151
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
1152
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
1153
            XdmValue * value = NULL;
1154
            XdmItem * xdmItem = NULL;
1155

    
1156

    
1157
            if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
1158
                xdmItem = new XdmAtomicValue(result);
1159

    
1160

    
1161
            } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
1162
                xdmItem = new XdmNode(result);
1163

    
1164

    
1165
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
1166
                std::cerr<<"Error: TransformFileToValue: FunctionItem found. Currently not be handled"<<std::endl;
1167
                return NULL;
1168
            } else {
1169
                value = new XdmValue(result, true);
1170
                value->setProcessor(proc);
1171
                for(int z=0;z<value->size();z++) {
1172
                    value->itemAt(z)->setProcessor(proc);
1173
                }
1174
                return value;
1175
            }
1176
            value = new XdmValue();
1177
            value->setProcessor(proc);
1178
            xdmItem->setProcessor(proc);
1179
            value->addXdmItem(xdmItem);
1180

    
1181
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
1182
            return value;
1183
        }else {
1184

    
1185
            proc->checkAndCreateException(cppClass);
1186

    
1187
        }
1188
    }
1189
    return NULL;
1190

    
1191
}
1192

    
1193

    
1194
void Xslt30Processor::transformFileToFile(const char* source,
1195
                                          const char* stylesheet, const char* outputfile) {
1196

    
1197
    if(exceptionOccurred()) {
1198
        //Possible error detected in the compile phase. Processor not in a clean state.
1199
        //Require clearing exception.
1200
        return;
1201
    }
1202
    if(!stylesheetObject && stylesheet==NULL){
1203
        std::cerr<< "Error: stylesheet has not been set or created using the compile methods."<<std::endl;
1204
        return;
1205
    }
1206
    setProperty("resources", proc->getResourcesDirectory());
1207
    jmethodID mID =
1208
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
1209
                                                                      "transformToFile",
1210
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
1211
    if (!mID) {
1212
        std::cerr << "Error: "<<getDllname() << "transformToFile" << " not found\n"
1213
                  << std::endl;
1214

    
1215
    } else {
1216
        JParameters comboArrays;
1217
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
1218

    
1219
        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
1220
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
1221
                                                           (source != NULL ?
1222
                                                            SaxonProcessor::sxn_environ->env->NewStringUTF(
1223
                                                                    source) : NULL),
1224
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),                            (outputfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :NULL),
1225
                                                           comboArrays.stringArray, comboArrays.objectArray);
1226
        if (comboArrays.stringArray!= NULL) {
1227
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
1228
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
1229
        }
1230
    }
1231
    proc->checkAndCreateException(cppClass);
1232
}
1233

    
1234

    
1235

    
1236

    
1237

    
1238

    
1239
XdmValue * Xslt30Processor::getXslMessages(){
1240

    
1241
    jmethodID mID =   (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
1242
                                                                                "getXslMessages",
1243
                                                                                "()[Lnet/sf/saxon/s9api/XdmNode;");
1244
    if (!mID) {
1245
        std::cerr << "Error: "<<getDllname() << ".getXslMessages" << " not found in JNI\n"
1246
                  << std::endl;
1247

    
1248
    } else {
1249
        jobjectArray results = (jobjectArray)(
1250
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID));
1251
        int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
1252

    
1253
        if (sizex>0) {
1254
            XdmValue * value = new XdmValue();
1255

    
1256
            for (int p=0; p < sizex; ++p)
1257
            {
1258
                jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
1259
                value->addUnderlyingValue(resulti);
1260
            }
1261
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
1262
            return value;
1263
        }
1264
        proc->checkAndCreateException(cppClass);
1265
    }
1266
    return NULL;
1267

    
1268

    
1269
}
1270

    
1271
const char * Xslt30Processor::transformFileToString(const char* source,
1272
                                                    const char* stylesheet) {
1273

    
1274
    if(exceptionOccurred()) {
1275
        //Possible error detected in the compile phase. Processor not in a clean state.
1276
        //Require clearing exception.
1277
        return NULL;
1278
    }
1279
    if(source == NULL && stylesheet == NULL && !stylesheetObject){
1280
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling transformFileToString or check exceptions"<<std::endl;
1281
        return NULL;
1282
    }
1283
    setProperty("resources", proc->getResourcesDirectory());
1284
    jmethodID mID =
1285
            (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
1286
                                                                      "transformToString",
1287
                                                                      "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
1288
    if (!mID) {
1289
        std::cerr << "Error: "<<getDllname() << "transformFileToString" << " not found\n"
1290
                  << std::endl;
1291

    
1292
    } else {
1293
        jobjectArray stringArray = NULL;
1294
        jobjectArray objectArray = NULL;
1295
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
1296
                                          "java/lang/Object");
1297
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
1298
                                          "java/lang/String");
1299

    
1300
        int size = parameters.size() + properties.size();
1301
#ifdef DEBUG
1302
        std::cerr<<"Properties size: "<<properties.size()<<std::endl;
1303
        std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
1304
#endif
1305
        if (size > 0) {
1306
            objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1307
                                                                           objectClass, 0);
1308
            stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
1309
                                                                           stringClass, 0);
1310
            int i = 0;
1311
            for (std::map<std::string, XdmValue*>::iterator iter =
1312
                    parameters.begin(); iter != parameters.end(); ++iter, i++) {
1313

    
1314
#ifdef DEBUG
1315
                std::cerr<<"map 1"<<std::endl;
1316
                std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
1317
#endif
1318
                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
1319
                                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(
1320
                                                                                (iter->first).c_str()));
1321
#ifdef DEBUG
1322
                std::string s1 = typeid(iter->second).name();
1323
                std::cerr<<"Type of itr:"<<s1<<std::endl;
1324

    
1325

    
1326
                jobject xx = (iter->second)->getUnderlyingValue();
1327

    
1328
                if(xx == NULL) {
1329
                    std::cerr<<"value failed"<<std::endl;
1330
                } else {
1331

    
1332
                    std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
1333
                }
1334
                if((iter->second)->getUnderlyingValue() == NULL) {
1335
                    std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
1336
                }
1337
#endif
1338

    
1339
                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
1340
                                                                        (iter->second)->getUnderlyingValue());
1341

    
1342
            }
1343

    
1344
            for (std::map<std::string, std::string>::iterator iter =
1345
                    properties.begin(); iter != properties.end(); ++iter, i++) {
1346
                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
1347
                                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(
1348
                                                                                (iter->first).c_str()));
1349
                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
1350
                                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(
1351
                                                                                (iter->second).c_str()));
1352
            }
1353
        }
1354

    
1355
        jstring result = NULL;
1356
        jobject obj =
1357
                (
1358
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
1359
                                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
1360
                                                                           (source != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(
1361
                                                                                   source) : NULL),
1362
                                                                           (stylesheet != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : NULL),
1363
                                                                           stringArray, objectArray));
1364
        if(obj) {
1365
            result = (jstring)obj;
1366
        }
1367
        if (size > 0) {
1368
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
1369
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
1370
        }
1371
        if (result) {
1372
            const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
1373
                                                                                   NULL);
1374
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
1375
            return str;
1376
        } else  {
1377
            proc->checkAndCreateException(cppClass);
1378

    
1379
        }
1380
    }
1381
    return NULL;
1382
}
1383

    
1384

    
1385
const char * Xslt30Processor::transformToString(XdmNode * source){
1386
    if(!stylesheetObject){
1387
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling transformToString or check exceptions"<<std::endl;
1388
        return NULL;
1389
    }
1390
    if(source != NULL){
1391
        source->incrementRefCount();
1392
        parameters["node"] = source;
1393
    }
1394
    return transformFileToString(NULL, NULL);
1395
}
1396

    
1397

    
1398
XdmValue * Xslt30Processor::transformToValue(XdmNode * source){
1399
    if(!stylesheetObject){
1400
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling transformToValue or check exceptions"<<std::endl;
1401
        return NULL;
1402
    }
1403
    if(source != NULL){
1404
        source->incrementRefCount();
1405
        parameters["node"] = source;
1406
    }
1407
    return transformFileToValue(NULL, NULL);
1408
}
1409

    
1410
void Xslt30Processor::transformToFile(XdmNode * source){
1411
    if(!stylesheetObject){
1412
        std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling transformToFile or check exceptions"<<std::endl;
1413
        return;
1414
    }
1415
    if(source != NULL){
1416
        source->incrementRefCount();
1417
        parameters["node"] = source;
1418
    }
1419
    transformFileToFile(NULL, NULL, NULL);
1420
}
1421

    
1422
const char * Xslt30Processor::getErrorMessage(int i ){
1423
    if(proc->exception == NULL) {return NULL;}
1424
    return proc->exception->getErrorMessage(i);
1425
}
    (1-1/1)