Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / XsltExecutable.cpp @ 5401a5ae

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

    
4
#include "XsltExecutable.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

    
14
XsltExecutable::XsltExecutable(std::string curr, jobject executableObject) {
15

    
16

    
17

    
18
        /*
19
         * Look for class.
20
         */
21
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
22
                        "net/sf/saxon/option/cpp/Xslt30Processor");
23
                        
24
#ifdef DEBUG
25
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
26
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
27

    
28
#endif
29
        tunnel = false;
30
        selection = NULL;
31
        selectionV=NULL;
32

    
33
    cwdXE = curr;
34
}
35

    
36
     XsltExecutable::~XsltExecutable(){
37
        clearProperties();
38
        clearParameters();
39
        if(selectionV != NULL) {
40
          selectionV->decrementRefCount();
41
          if(selectionV->getRefCount() == 0) {
42
                delete selectionV;
43
          }
44
        }
45
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(executableObject);
46

    
47
     }
48

    
49

    
50
XsltExecutable::XsltExecutable(const XsltExecutable &other) {
51

    
52
        /*
53
         * Look for class.
54
         */
55
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
56
                        "net/sf/saxon/option/cpp/Xslt30Processor");
57
                        
58
        executableObject = other.executableObject;
59
        selectionV = other.selectionV;
60
        if(selectionV != NULL) {
61
            setInitialMatchSelection(other.selectionV);
62
        } else {
63
            selection = other.selection;
64
        }
65
        tunnel = other.tunnel;
66

    
67
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
68
    while(paramIter != other.parameters.end())
69
    {
70

    
71
       XdmValue * valuei = paramIter->second;
72
       if(valuei == NULL) {
73
                     cerr<<"Error in XsltExecutable copy constructor"<<endl;
74
       } else {
75
            parameters[paramIter->first] = new XdmValue(*(valuei));
76
       }
77
       paramIter++;
78
    }
79

    
80
        std::map<std::string, std::string>::const_iterator propIter = other.properties.begin();
81
        while(propIter != other.properties.end())
82
    {
83
        properties[propIter->first] = propIter->second;
84
        propIter++;
85
    }
86

    
87
}
88

    
89

    
90
bool XsltExecutable::exceptionOccurred() {
91
        return proc->exceptionOccurred();   //TODO change to static call
92
}
93

    
94
const char * XsltExecutable::getErrorCode(int i) {
95
 if(exception == NULL) {return NULL;}
96
 return exception->getErrorCode(i);
97
 }
98

    
99

    
100

    
101
void XsltExecutable::setGlobalContextItem(XdmItem * value){
102
    if(value != NULL){
103
      value->incrementRefCount();
104
      parameters["node"] = value;
105
    }
106
}
107

    
108
void XsltExecutable::setGlobalContextFromFile(const char * ifile) {
109
        if(ifile != NULL) {
110
                setProperty("s", ifile);
111
        }
112
}
113

    
114
void XsltExecutable::setInitialMatchSelection(XdmValue * _selection){
115
     if(_selection != NULL) {
116
      _selection->incrementRefCount();
117
      selectionV = _selection;
118
      selection = _selection->getUnderlyingValue();
119
    }
120
}
121

    
122

    
123
void XsltExecutable::setInitialMatchSelectionAsFile(const char * filename){
124
    if(filename != NULL) {
125
      selection = SaxonProcessor::sxn_environ->env->NewStringUTF(filename);
126
    }
127
}
128

    
129
void XsltExecutable::setOutputFile(const char * ofile) {
130
        setProperty("o", ofile);
131
}
132

    
133
void XsltExecutable::setBaseOutputURI(const char * baseURI) {
134
        if(baseURI != NULL) {
135
              setProperty("baseoutput", baseURI);
136
        }
137
}
138

    
139

    
140
void XsltExecutable::setParameter(const char* name, XdmValue * value, bool _static) {
141
        if(value != NULL && name != NULL){
142
                value->incrementRefCount();
143
                int s = parameter.size();
144
                std::String skey = (_static ? "sparam:"+std::string(name) : "param:"+std::string(name));
145
                parameters[skey] = value;
146
                if(s == parameter.size()) {
147
            std::map<std::string, XdmValue*>::iterator it;
148
            it = parameters.find(skey);
149
            if (it != parameters.end()) {
150
                XdmValue * valuei = it->second;
151
                valuei->decrementRefCount();
152
                if(valuei != NULL && valuei->getRefCount() < 1){
153
                    delete value;
154
                }
155
                parameters.erase(skey);
156
                parameters[skey] = value;
157
            }
158
                }
159
         }
160
}
161

    
162
    void XsltExecutable::setInitialTemplateParameters(std::map<std::string,XdmValue*> _itparameters, bool _tunnel){
163
        for(std::map<std::string, XdmValue*>::iterator itr = _itparameters.begin(); itr != _itparameters.end(); itr++){
164
                parameters["itparam:"+std::string(itr->first)] = itr->second;
165
        }
166
        tunnel = _tunnel;
167
        if(tunnel) {
168
                setProperty("tunnel", "true");
169
            }
170
   }
171

    
172
XdmValue* XsltExecutable::getParameter(const char* name) {
173
        std::map<std::string, XdmValue*>::iterator it;
174
        it = parameters.find("param:"+std::string(name));
175
        if (it != parameters.end())
176
          return it->second;
177
        else {
178
          it = parameters.find("sparam:"+std::string(name));
179
        if (it != parameters.end())
180
          return it->second;
181
          }
182
        return NULL;
183
}
184

    
185
bool XsltExecutable::removeParameter(const char* name) {
186
        return (bool)(parameters.erase("param:"+std::string(name)));
187
}
188

    
189

    
190
void XsltExecutable::setResultAsRawValue(bool option) {
191
        if(option) {
192
                setProperty("outvalue", "yes");
193
        }
194
 }
195

    
196
void XsltExecutable::setProperty(const char* name, const char* value) {
197
        if(name != NULL) {
198
            int s = properties.size();
199
                std:string skey = std::string(name);
200
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == NULL ? "" : value))));
201

    
202
                if(s == properties.size()) {
203
            std::map<std::string, std::string>::iterator it;
204
            it = properties.find(skey);
205
            if (it != properties.end()) {
206
                properties.erase(skey);
207
                properties[skey] = std::string((value == NULL ? "" : value));
208
            }
209
                }
210
        }
211
}
212

    
213
const char* XsltExecutable::getProperty(const char* name) {
214
        std::map<std::string, std::string>::iterator it;
215
        it = properties.find(std::string(name));
216
        if (it != properties.end())
217
          return it->second.c_str();
218
        return NULL;
219
}
220

    
221
void XsltExecutable::clearParameters(bool delValues) {
222
        if(delValues){
223
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
224

    
225
                        XdmValue * value = itr->second;
226
                        value->decrementRefCount();
227
#ifdef DEBUG
228
                        std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
229
#endif
230
                        if(value != NULL && value->getRefCount() < 1){
231
                                delete value;
232
                        }
233
                }
234

    
235
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(selection);
236
                selection = NULL;
237
        } else {
238
for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
239

    
240
                        XdmValue * value = itr->second;
241
                        value->decrementRefCount();
242

    
243
                }
244

    
245
        selection = NULL;
246
        }
247
        parameters.clear();
248

    
249

    
250
}
251

    
252
void XsltExecutable::clearProperties() {
253
        properties.clear();
254

    
255
}
256

    
257

    
258

    
259
std::map<std::string,XdmValue*>& XsltExecutable::getParameters(){
260
        std::map<std::string,XdmValue*>& ptr = parameters;
261
        return ptr;
262
}
263

    
264
std::map<std::string,std::string>& XsltExecutable::getProperties(){
265
        std::map<std::string,std::string> &ptr = properties;
266
        return ptr;
267
}
268

    
269
void XsltExecutable::exceptionClear(){
270
 if(exception != NULL) {
271
         delete exception;
272
         exception = NULL;
273
        SaxonProcessor::sxn_environ->env->ExceptionClear();
274
 }
275

    
276
 }
277

    
278
   void XsltExecutable::setcwd(const char* dir){
279
    if (dir!= NULL) {
280
        cwdXE = std::string(dir);
281
    }
282
   }
283

    
284
const char* XsltExecutable::checkException() {
285
        /*if(proc->exception == NULL) {
286
         proc->exception = proc->checkForException(environi, cpp);
287
         }
288
         return proc->exception;*/
289
        return SaxonProcessor::checkException(NULL);
290
}
291

    
292
/*int XsltExecutable::exceptionCount(){
293
 if(exception != NULL){
294
 return exception->count();
295
 }
296
 return 0;
297
 }      */
298

    
299

    
300
    void XsltExecutable::export(const char * filename) {
301

    
302

    
303
        static jmethodID exportmID == NULL;
304

    
305
        if(!exportmID) {
306
            exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
307
                                     "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
308
        }
309

    
310
        if(filename == NULL) {
311
            std::cerr<< "Error: Error: export file name is NULL"<<std::endl;
312
            return;
313
        }
314
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, exportmID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
315
                                     executableObject, SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
316

    
317
    }
318

    
319
    void XsltExecutable::applyTemplatesReturningFile(const char * stylesheetfile, const char* output_filename){
320

    
321
        if(selection == NULL) {
322
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
323
                       return;
324
        }
325

    
326

    
327
        setProperty("resources", proc->getResourcesDirectory());
328
        static jmethodID atmID = NULL;
329

    
330
        if(atmID == NULL) {
331
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
332
                                        "applyTemplatesReturningFile",
333
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
334

    
335
        }
336

    
337
        if (!atmID) {
338
                std::cerr << "Error: "<< getDllname() << "applyTemplatesAsFile" << " not found\n"
339
                                << std::endl;
340

    
341
        } else {
342
        JParameters comboArrays;
343
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
344
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atmID,
345
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject ,selection,
346
                                                (output_filename != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(
347
                                                                                       output_filename) : NULL),
348
                                comboArrays.stringArray, comboArrays.objectArray);
349
                if (comboArrays.stringArray != NULL) {
350
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
351
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
352
                }
353
                proc->checkAndCreateException(cppClass);
354

    
355
        }
356
        return;
357

    
358
}
359

    
360
const char* XsltExecutable::applyTemplatesReturningString(){
361

    
362
        if(selection == NULL) {
363
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
364
                       return NULL;
365
        }
366

    
367
        setProperty("resources", proc->getResourcesDirectory());
368
        jmethodID atsmID =
369
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
370
                                        "applyTemplatesReturningString",
371
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
372
        if (!atsmID) {
373
                std::cerr << "Error: "<<getDllname() << "applyTemplatesAsString" << " not found\n"
374
                                << std::endl;
375

    
376
        } else {
377
            JParameters comboArrays;
378
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
379

    
380
        jstring result = NULL;
381
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
382
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject
383
                                                                selection,
384
                                                                comboArrays.stringArray, comboArrays.objectArray));
385

    
386
                if(obj) {
387
                        result = (jstring)obj;
388
                }
389
                if (comboArrays.stringArray != NULL) {
390
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
391
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
392
                }
393
                if (result) {
394
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
395
                                        NULL);
396
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
397
                        return str;
398
                } else  {
399
                        proc->checkAndCreateException(cppClass);
400

    
401
                     }
402
        }
403
        return NULL;
404

    
405

    
406
}
407

    
408
XdmValue * XsltExecutable::applyTemplatesReturningValue(){
409

    
410
        if(selection == NULL) {
411
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
412
                       return NULL;
413
        }
414

    
415
        setProperty("resources", proc->getResourcesDirectory());
416
        static jmethodID atsmID = NULL;
417
        if (atsmID == NULL) {
418
            atsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
419
                                        "applyTemplatesReturningValue",
420
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
421
        }
422
        if (!atsmID) {
423
                std::cerr << "Error: "<<getDllname() << "applyTemplatesAsValue" << " not found\n"
424
                                << std::endl;
425

    
426
        } else {
427
            JParameters comboArrays;
428
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
429

    
430

    
431
           // jstring result = NULL;
432
            jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
433
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
434
                                                                executableObject, selection,
435
                                                                comboArrays.stringArray, comboArrays.objectArray));
436
                /*if(obj) {
437
                        result = (jobject)obj;
438
                }*/
439
                if (comboArrays.stringArray != NULL) {
440
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
441
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
442
                }
443
        if (result) {
444
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
445
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
446
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
447
                XdmValue * value = NULL;
448
                XdmItem * xdmItem = NULL;
449

    
450

    
451
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
452
                                xdmItem =  new XdmAtomicValue(result);
453
                                xdmItem->setProcessor(proc);
454
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
455
                                return xdmItem;
456

    
457
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
458
                                xdmItem =  new XdmNode(result);
459
                                xdmItem->setProcessor(proc);
460
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
461
                                return xdmItem;
462
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
463
                xdmItem =  new XdmFunctionItem(result);
464
                xdmItem->setProcessor(proc);
465
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
466
                return xdmItem;
467
                        } else {
468
                                value = new XdmValue(result, true);
469
                                value->setProcessor(proc);
470
                                for(int z=0;z<value->size();z++) {
471
                                        value->itemAt(z)->setProcessor(proc);
472
                                }
473
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
474
                                return value;
475
                        }
476
                } else  {
477
                        proc->checkAndCreateException(cppClass);
478

    
479
                     }
480
        }
481
        return NULL;
482

    
483
}
484

    
485

    
486

    
487
    void XsltExecutable::callFunctionReturningFile(const char* functionName, XdmValue ** arguments, int argument_length, const char* outfile){        
488

    
489
                setProperty("resources", proc->getResourcesDirectory());
490
                static jmethodID afmID = NULL;
491

    
492
                if(afmID == NULL) {
493
                                afmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
494
                                                "callFunctionReturningFile",
495
                                                "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
496
                }
497

    
498
                if (!afmID) {
499
                        std::cerr << "Error: "<< getDllname() << "callFunctionReturningFile" << " not found\n"
500
                                        << std::endl;
501
                 return;
502
                } else {
503
                JParameters comboArrays;
504
                        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
505

    
506
                        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
507

    
508
                        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afmID,
509
                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
510
                                                        executableObject,
511
                                                        (functionName != NULL ?
512
                                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) :
513
                                                                        NULL), argumentJArray,
514
                                                                        (outfile != NULL ?
515
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) :
516
                                                                     NULL),
517
                                                                        comboArrays.stringArray, comboArrays.objectArray);
518
                        if (comboArrays.stringArray != NULL) {
519
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
520
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
521
                        }
522
                        if(argumentJArray != NULL) {
523
                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
524
                        }
525
                        proc->checkAndCreateException(cppClass);
526

    
527
                }
528
                return;
529

    
530

    
531

    
532

    
533
    }
534

    
535
    const char * XsltExecutable::callFunctionReturningString(const char* functionName, XdmValue ** arguments, int argument_length){
536

    
537
            setProperty("resources", proc->getResourcesDirectory());
538
            static jmethodID afsmID =
539
                            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
540
                                            "callFunctionReturningString",
541
                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
542
            if (!afsmID) {
543
                    std::cerr << "Error: "<<getDllname() << "callFunctionReturningString" << " not found\n"
544
                                    << std::endl;
545

    
546
            } else {
547
                JParameters comboArrays;
548
                    comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
549
            jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
550

    
551
            jstring result = NULL;
552
            jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afsmID,
553
                                                                    SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),  executableObject
554
                                                                    (functionName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) : NULL),
555
                                                                    argumentJArray, comboArrays.stringArray, comboArrays.objectArray));
556
                    if(obj) {
557
                            result = (jstring)obj;
558
                    }
559
                    if (comboArrays.stringArray != NULL) {
560
                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
561
                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
562
                    }
563
                    if (result) {
564
                            const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
565
                                            NULL);
566
                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
567
                            return str;
568
                    } else  {
569
                            proc->checkAndCreateException(cppClass);
570

    
571
                         }
572
            }
573
            return NULL;
574
    }
575

    
576

    
577

    
578
    XdmValue * XsltExecutable::callFunctionReturningValue(const char* functionName, XdmValue ** arguments, int argument_length){
579

    
580
                  setProperty("resources", proc->getResourcesDirectory());
581
                  static jmethodID cfvmID = NULL;
582
                  if(cfvmID == NULL) {
583
                      cfvmId = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
584
                                                  "callFunctionReturningValue",
585
                                                  "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
586
                  }
587
                  if (!cfvmID) {
588
                          std::cerr << "Error: "<<getDllname() << "callFunctionReturningValue" << " not found\n"
589
                                          << std::endl;
590

    
591
                  } else {
592
                      JParameters comboArrays;
593
                          comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
594
                jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
595

    
596
                      jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, cfvmID,
597
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),executableObject,
598
                                        (functionName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) : NULL),
599
                                                                          argumentJArray, comboArrays.stringArray, comboArrays.objectArray));
600

    
601
                          if (comboArrays.stringArray != NULL) {
602
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
603
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
604
                          }
605
                        if(argumentJArray != NULL) {
606
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
607
                        }
608
                  if (result) {
609
                          jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
610
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
611
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
612
                         XdmValue * value = NULL;
613
                          XdmItem * xdmItem = NULL;
614

    
615

    
616
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
617
                                            xdmItem = new XdmAtomicValue(result);
618
                                            xdmItem->setProcessor(proc);
619
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
620
                                            return xdmItem;
621
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
622
                                              xdmItem = new XdmNode(result);
623
                                            xdmItem->setProcessor(proc);
624
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
625
                                            return xdmItem;
626

    
627
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
628
                                           xdmItem =  new XdmFunctionItem(result);
629
                         xdmItem->setProcessor(proc);
630
                         SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
631
                         return xdmItem;
632
                                  } else {
633
                                        value = new XdmValue(result, true);
634
                                        value->setProcessor(proc);
635
                                        for(int z=0;z<value->size();z++) {
636
                                                value->itemAt(z)->setProcessor(proc);
637
                                        }
638
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
639
                                        return value;
640
                        }
641
                        value = new XdmValue();
642
                        value->setProcessor(proc);
643
            value->addUnderlyingValue(result);
644
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
645
                        return value;
646
                  } else  {
647
                          proc->checkAndCreateException(cppClass);
648

    
649
                       }
650
          }
651
          return NULL;
652

    
653
    }
654

    
655

    
656
    void XsltExecutable::callTemplateReturningFile(const char* templateName, const char* outfile){
657

    
658
        if(stylesheetfile == NULL && !stylesheetObject){
659
                std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callFunctionReturningFile or check exceptions"<<std::endl;
660
                return;
661
        }
662

    
663
        setProperty("resources", proc->getResourcesDirectory());
664
        static jmethodID ctmID =
665
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
666
                                        "callTemplateReturningFile",
667
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
668
        if (!ctmID) {
669
                std::cerr << "Error: "<< getDllname() << "callTemplateReturningFile" << " not found\n"
670
                                << std::endl;
671

    
672
        } else {
673
        JParameters comboArrays;
674
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
675
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppclass, ctmID,
676
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
677
                                                (stylesheetfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : NULL), executableObject,
678
                                                (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
679
                                                                (outfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) : NULL),
680
                                                                comboArrays.stringArray, comboArrays.objectArray);
681
                if (comboArrays.stringArray != NULL) {
682
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
683
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
684
                }
685
                proc->checkAndCreateException(cppClass);
686

    
687
        }
688
        return;
689

    
690

    
691
    }
692

    
693

    
694

    
695

    
696
    const char* XsltExecutable::callTemplateReturningString(const char * stylesheet, const char* templateName){
697

    
698
        setProperty("resources", proc->getResourcesDirectory());
699
        jmethodID ctsmID =
700
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
701
                                        "callTemplateReturningString",
702
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
703
        if (!ctsmID) {
704
                std::cerr << "Error: "<<getDllname() << "callTemplateReturningString" << " not found\n"
705
                                << std::endl;
706

    
707
        } else {
708
            JParameters comboArrays;
709
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
710

    
711

    
712
        jstring result = NULL;
713
        jobject obj =(jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
714
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject,
715
                                                                (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
716
                                                                comboArrays.stringArray, comboArrays.objectArray));
717
                if(obj) {
718
                        result = (jstring)obj;
719
                }
720
                if (comboArrays.stringArray != NULL) {
721
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
722
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
723
                }
724
                if (result) {
725
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
726
                                        NULL);
727
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
728
                        return str;
729
                } else  {
730
                        proc->checkAndCreateException(cppClass);
731

    
732
                     }
733
        }
734
        return NULL;
735

    
736

    
737
    }
738

    
739
    XdmValue* XsltExecutable::callTemplateReturningValue(const char* templateName){
740

    
741
                  setProperty("resources", proc->getResourcesDirectory());
742
                  static jmethodID ctsmID = NULL;
743
                  if (ctsmID == NULL) {
744
                      ctsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
745
                                                  "callTemplateReturningValue",
746
                                                  "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
747
                  }
748
                  if (!ctsmID) {
749
                          std::cerr << "Error: "<<getDllname() << "callTemplateReturningValue" << " not found\n"
750
                                          << std::endl;
751

    
752
                  } else {
753
                      JParameters comboArrays;
754
                          comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
755

    
756

    
757
                      jstring result = NULL;
758
                      jobject obj = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
759
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject,
760
                                        (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
761
                                                                          comboArrays.stringArray, comboArrays.objectArray));
762
                          if(obj) {
763
                                  result = (jstring)obj;
764
                          }
765
                          if (comboArrays.stringArray != NULL) {
766
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
767
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
768
                          }
769
                  if (result) {
770
                          jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
771
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
772
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
773
                          XdmValue * value = NULL;
774

    
775
                          XdmItem * xdmItem = NULL;
776
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
777
                                          xdmItem =  new XdmAtomicValue(result);
778
                                            xdmItem->setProcessor(proc);
779
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
780
                                            return xdmItem;
781

    
782
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
783
                                          xdmItem = new XdmNode(result);
784
                                            xdmItem->setProcessor(proc);
785
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
786
                                            return xdmItem;
787
#if CVERSION_API_NO >= 123
788
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
789
                        xdmItem =  new XdmFunctionItem(result);
790
                        xdmItem->setProcessor(proc);
791
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
792
                        return xdmItem;
793
#endif
794
                                  } else {
795
                                        value = new XdmValue(result, true);
796
                                        value->setProcessor(proc);
797
                                        for(int z=0;z<value->size();z++) {
798
                                                value->itemAt(z)->setProcessor(proc);
799
                                        }
800
                                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
801
                                        return value;
802
                                }
803
                            value = new XdmValue();
804
                            value->setProcessor(proc);
805
                          value->addUnderlyingValue(result);
806
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
807
                          return value;
808
                 } else  {
809
                          exception = proc->checkAndCreateException(cppClass);
810
                       }
811
          }
812
        return NULL;
813
    }
814

    
815

    
816

    
817

    
818
XdmValue * XsltExecutable::transformFileToValue(const char* sourcefile) {
819

    
820
        if(sourcefile == NULL){
821

    
822
                return NULL;
823
        }
824

    
825
        setProperty("resources", proc->getResourcesDirectory());
826
        static jmethodID tfvMID = NULL;
827
        
828
        if(tfvMID == NULL) {
829
                        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
830
                                        "transformToValue",
831
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
832
        }
833
        if (!tfvMID) {
834
                std::cerr << "Error: "<< getDllname() << ".transformtoValue" << " not found\n"
835
                                << std::endl;
836

    
837
        } else {
838
            JParameters comboArrays;
839
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
840

    
841
                jobject result = (jobject)(
842
                                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tfvMID,
843
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject,
844
                                                (sourcefile != NULL ?SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : NULL),
845
                                                                 comboArrays.stringArray, comboArrays.objectArray));
846
                if (comboArrays.stringArray != NULL) {
847
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
848
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
849
                }
850
                if (result) {
851
                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
852
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
853
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
854
                        XdmValue * value = NULL;
855
                          XdmItem * xdmItem = NULL;
856

    
857

    
858
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
859
                                          xdmItem = new XdmAtomicValue(result);
860
                        xdmItem->setProcessor(proc);
861
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
862
                        return xdmItem;
863

    
864
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
865
                                          xdmItem = new XdmNode(result);
866
                        xdmItem->setProcessor(proc);
867
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
868
                        return xdmItem;
869

    
870
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
871
                        xdmItem =  new XdmFunctionItem(result);
872
                        xdmItem->setProcessor(proc);
873
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
874
                        return xdmItem;
875
                                  } else {
876
                                        value = new XdmValue(result, true);
877
                                        value->setProcessor(proc);
878
                                        for(int z=0;z<value->size();z++) {
879
                                                value->itemAt(z)->setProcessor(proc);
880
                                        }
881
                                        return value;
882
                                }
883
                                value = new XdmValue();
884
                                value->setProcessor(proc);
885
                      value->addUnderlyingValue(result);
886

    
887
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
888
                          return value;
889
                }else {
890

    
891
                        exception = proc->checkAndCreateException(cppClass);
892

    
893
                     }
894
        }
895
        return NULL;
896

    
897
}
898

    
899

    
900
void XsltExecutable::transformFileToFile(const char* source, const char* outputfile) {
901

    
902
        setProperty("resources", proc->getResourcesDirectory());
903
        static jmethodID tffMID = NULL;
904

    
905
        if(tffMID == NULL) {
906
                        tffMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
907
                                        "transformToFile",
908
                                        "(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/String;[Ljava/lang/Object;)V");
909
        }
910
        if (!tffMID) {
911
                std::cerr << "Error: "<<getDllname() << "transformToFile" << " not found\n"
912
                                << std::endl;
913

    
914
        } else {
915
            JParameters comboArrays;
916
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
917

    
918
                SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, tffMID,
919
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject
920
                                                                (source != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : NULL), NULL,
921
                                                                (outputfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :NULL),
922
                                                                comboArrays.stringArray, comboArrays.objectArray);
923
                if (comboArrays.stringArray!= NULL) {
924
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
925
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
926
                }
927
                }
928
                exception = proc->checkAndCreateException(cppClass);
929
        }
930

    
931

    
932

    
933

    
934

    
935
void XsltExecutable::setupXslMessage(bool show, const char *filename=NULL) {
936
    if(show) {
937
        if(filename == NULL) {
938
            setProperty("m", "on");
939
        } else {
940
            setProperty("m", std::string(filename));
941
        }
942
    } else {
943
        setProperty("m", "off");
944
    }
945
}
946

    
947

    
948
const char * XsltExecutable::transformFileToString(const char* source) {
949

    
950
        setProperty("resources", proc->getResourcesDirectory());
951
        static jmethodID tftMID = tftMID
952
        if(tftMID == NULL) {
953
            tftMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
954
                                        "transformToString",
955
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
956
        }
957
        if (!tftMID) {
958
                std::cerr << "Error: "<<getDllname() << "transformFileToString" << " not found\n"
959
                                << std::endl;
960

    
961
        } else {
962
        JParameters comboArrays;
963
    comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
964

    
965
        jstring result = NULL;
966
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tftMID,
967
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject,
968
                                                (source != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : NULL),
969
                                                                comboArrays.stringArray, comboArrays.objectArray);
970
    if (comboArrays.stringArray!= NULL) {
971
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
972
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
973
    }
974
                if(obj) {
975
                        result = (jstring)obj;
976
                }
977
                if (result) {
978
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
979
                                        NULL);
980
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
981
                        return str;
982
                } else  {
983
                        exception = proc->checkAndCreateException(cppClass);
984

    
985
                     }
986
        }
987
        return NULL;
988
}
989

    
990

    
991
   const char * XsltExecutable::transformToString(XdmNode * source){
992

    
993
        if(source != NULL){
994
                      source->incrementRefCount();
995
                      parameters["node"] = source;
996
            }
997
        return transformFileToString(NULL, NULL);
998
   }
999

    
1000

    
1001
    XdmValue * XsltExecutable::transformToValue(XdmNode * source){
1002

    
1003
        if(source != NULL){
1004
                      source->incrementRefCount();
1005
                      parameters["node"] = source;
1006
            }
1007
        return transformFileToValue(NULL, NULL);
1008
   }
1009

    
1010
    void XsltExecutable::transformToFile(XdmNode * source){
1011

    
1012
        if(source != NULL){
1013
                      source->incrementRefCount();
1014
                      parameters["node"] = source;
1015
            }
1016
        transformFileToFile(NULL, NULL, NULL);
1017
   }
1018

    
1019
const char * XsltExecutable::getErrorMessage(int i ){
1020
         if(exception == NULL) {return NULL;}
1021
         return exception->getErrorMessage(i);
1022
 }
(36-36/52)