Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / XsltExecutable.cpp @ 1117bc69

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
        jitCompilation = other.jitCompilation;
87

    
88
}
89

    
90

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

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

    
100

    
101

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

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

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

    
123

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

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

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

    
140

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

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

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

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

    
190
void XsltExecutable::setJustInTimeCompilation(bool jit){
191
    jitCompilation = jit;
192
}
193

    
194
void XsltExecutable::setResultAsRawValue(bool option) {
195
        if(option) {
196
                setProperty("outvalue", "yes");
197
        }
198
 }
199

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

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

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

    
225
void XsltExecutable::clearParameters(bool delValues) {
226
        if(delValues){
227
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
228

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

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

    
244
                        XdmValue * value = itr->second;
245
                        value->decrementRefCount();
246

    
247
                }
248

    
249
        selection = NULL;
250
        }
251
        parameters.clear();
252

    
253

    
254
}
255

    
256
void XsltExecutable::clearProperties() {
257
        properties.clear();
258

    
259
}
260

    
261

    
262

    
263
std::map<std::string,XdmValue*>& XsltExecutable::getParameters(){
264
        std::map<std::string,XdmValue*>& ptr = parameters;
265
        return ptr;
266
}
267

    
268
std::map<std::string,std::string>& XsltExecutable::getProperties(){
269
        std::map<std::string,std::string> &ptr = properties;
270
        return ptr;
271
}
272

    
273
void XsltExecutable::exceptionClear(){
274
 if(proc->exception != NULL) {
275
         delete proc->exception;
276
         proc->exception = NULL;
277
        SaxonProcessor::sxn_environ->env->ExceptionClear();
278
 }
279

    
280
 }
281

    
282
   void XsltExecutable::setcwd(const char* dir){
283
    if (dir!= NULL) {
284
        cwdXE = std::string(dir);
285
    }
286
   }
287

    
288
const char* XsltExecutable::checkException() {
289
        /*if(proc->exception == NULL) {
290
         proc->exception = proc->checkForException(environi, cpp);
291
         }
292
         return proc->exception;*/
293
        return proc->checkException(cppXT);
294
}
295

    
296
int XsltExecutable::exceptionCount(){
297
 if(proc->exception != NULL){
298
 return proc->exception->count();
299
 }
300
 return 0;
301
 }
302

    
303

    
304
    void XsltExecutable::export(const char * filename) {
305
        jclass cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
306
                                "net/sf/saxon/option/cpp/Xslt30Processor");
307

    
308
        static jmethodID exportmID == NULL;
309

    
310
        if(!exportmID) {
311
            exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
312
                                     "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
313
        }
314

    
315
        if(filename == NULL) {
316
            std::cerr<< "Error: Error: export file name is NULL"<<std::endl;
317
            return;
318
        }
319
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, exportmID, SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
320
                                     executableObject, SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
321

    
322
    }
323

    
324
    void XsltExecutable::applyTemplatesReturningFile(const char * stylesheetfile, const char* output_filename){
325

    
326
        if(selection == NULL) {
327
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
328
                       return;
329
        }
330

    
331

    
332
        setProperty("resources", proc->getResourcesDirectory());
333
        static jmethodID atmID = NULL;
334

    
335
        if(atmID == NULL) {
336
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
337
                                        "applyTemplatesReturningFile",
338
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
339

    
340
        }
341

    
342
        if (!atmID) {
343
                std::cerr << "Error: "<< getDllname() << "applyTemplatesAsFile" << " not found\n"
344
                                << std::endl;
345

    
346
        } else {
347
        JParameters comboArrays;
348
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
349
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, atmID,
350
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject ,selection,
351
                                                (output_filename != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(
352
                                                                                       output_filename) : NULL),
353
                                comboArrays.stringArray, comboArrays.objectArray);
354
                if (comboArrays.stringArray != NULL) {
355
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
356
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
357
                }
358
                proc->checkAndCreateException(cppClass);
359

    
360
        }
361
        return;
362

    
363
}
364

    
365
const char* XsltExecutable::applyTemplatesReturningString(){
366

    
367
        if(selection == NULL) {
368
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
369
                       return NULL;
370
        }
371

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

    
381
        } else {
382
            JParameters comboArrays;
383
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
384

    
385
        jstring result = NULL;
386
        jobject obj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, atsmID,
387
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject
388
                                                                selection,
389
                                                                comboArrays.stringArray, comboArrays.objectArray));
390

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

    
406
                     }
407
        }
408
        return NULL;
409

    
410

    
411
}
412

    
413
XdmValue * XsltExecutable::applyTemplatesReturningValue(){
414

    
415
        if(selection == NULL) {
416
           std::cerr<< "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."<<std::endl;
417
                       return NULL;
418
        }
419

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

    
431
        } else {
432
            JParameters comboArrays;
433
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
434

    
435

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

    
455

    
456
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
457
                                xdmItem =  new XdmAtomicValue(result);
458
                                xdmItem->setProcessor(proc);
459
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
460
                                return xdmItem;
461

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

    
484
                     }
485
        }
486
        return NULL;
487

    
488
}
489

    
490

    
491

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

    
494
                setProperty("resources", proc->getResourcesDirectory());
495
                static jmethodID afmID = NULL;
496

    
497
                if(afmID == NULL) {
498
                                afmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
499
                                                "callFunctionReturningFile",
500
                                                "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
501
                }
502

    
503
                if (!afmID) {
504
                        std::cerr << "Error: "<< getDllname() << "callFunctionReturningFile" << " not found\n"
505
                                        << std::endl;
506
                 return;
507
                } else {
508
                JParameters comboArrays;
509
                        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
510

    
511
                        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
512

    
513
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, afmID,
514
                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
515
                                                        executableObject,
516
                                                        (functionName != NULL ?
517
                                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) :
518
                                                                        NULL), argumentJArray,
519
                                                                        (outfile != NULL ?
520
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) :
521
                                                                     NULL),
522
                                                                        comboArrays.stringArray, comboArrays.objectArray);
523
                        if (comboArrays.stringArray != NULL) {
524
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
525
                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
526
                        }
527
                        if(argumentJArray != NULL) {
528
                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
529
                        }
530
                        proc->checkAndCreateException(cppClass);
531

    
532
                }
533
                return;
534

    
535

    
536

    
537

    
538
    }
539

    
540
    const char * XsltExecutable::callFunctionReturningString(const char* functionName, XdmValue ** arguments, int argument_length){
541

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

    
551
            } else {
552
                JParameters comboArrays;
553
                    comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
554
            jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
555

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

    
576
                         }
577
            }
578
            return NULL;
579
    }
580

    
581

    
582

    
583
    XdmValue * XsltExecutable::callFunctionReturningValue(const char* functionName, XdmValue ** arguments, int argument_length){
584

    
585
                  setProperty("resources", proc->getResourcesDirectory());
586
                  static jmethodID cfvmID = NULL;
587
                  if(cfvmID == NULL) {
588
                      cfvmId = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
589
                                                  "callFunctionReturningValue",
590
                                                  "(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;");
591
                  }
592
                  if (!cfvmID) {
593
                          std::cerr << "Error: "<<getDllname() << "callFunctionReturningValue" << " not found\n"
594
                                          << std::endl;
595

    
596
                  } else {
597
                      JParameters comboArrays;
598
                          comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
599
                jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
600

    
601
                      jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cfvmID,
602
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),executableObject,
603
                                        (functionName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(functionName) : NULL),
604
                                                                          argumentJArray, comboArrays.stringArray, comboArrays.objectArray));
605

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

    
620

    
621
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
622
                                            xdmItem = new XdmAtomicValue(result);
623
                                            xdmItem->setProcessor(proc);
624
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
625
                                            return xdmItem;
626
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
627
                                              xdmItem = new XdmNode(result);
628
                                            xdmItem->setProcessor(proc);
629
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
630
                                            return xdmItem;
631

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

    
654
                       }
655
          }
656
          return NULL;
657

    
658
    }
659

    
660

    
661
    void XsltExecutable::callTemplateReturningFile(const char* templateName, const char* outfile){
662

    
663
        if(stylesheetfile == NULL && !stylesheetObject){
664
                std::cerr<< "Error: No stylesheet found. Please compile stylesheet before calling callFunctionReturningFile or check exceptions"<<std::endl;
665
                return;
666
        }
667

    
668
        setProperty("resources", proc->getResourcesDirectory());
669
        static jmethodID ctmID =
670
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
671
                                        "callTemplateReturningFile",
672
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
673
        if (!ctmID) {
674
                std::cerr << "Error: "<< getDllname() << "callTemplateReturningFile" << " not found\n"
675
                                << std::endl;
676

    
677
        } else {
678
        JParameters comboArrays;
679
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
680
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctmID,
681
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()),
682
                                                (stylesheetfile != NULL ?
683
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
684
                                                                                stylesheetfile) :
685
                                                                NULL),
686
                                                (templateName != NULL ?
687
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) :
688
                                                                NULL),
689
                                                                (outfile != NULL ?
690
                                                        SaxonProcessor::sxn_environ->env->NewStringUTF(outfile) :
691
                                                             NULL),
692
                                                                comboArrays.stringArray, comboArrays.objectArray);
693
                if (comboArrays.stringArray != NULL) {
694
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
695
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
696
                }
697
                proc->checkAndCreateException(cppClass);
698

    
699
        }
700
        return;
701

    
702

    
703
    }
704

    
705

    
706

    
707

    
708
    const char* XsltExecutable::callTemplateReturningString(const char * stylesheet, const char* templateName){
709

    
710
        setProperty("resources", proc->getResourcesDirectory());
711
        jmethodID ctsmID =
712
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
713
                                        "callTemplateReturningString",
714
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
715
        if (!ctsmID) {
716
                std::cerr << "Error: "<<getDllname() << "callTemplateReturningString" << " not found\n"
717
                                << std::endl;
718

    
719
        } else {
720
            JParameters comboArrays;
721
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
722

    
723

    
724
        jstring result = NULL;
725
        jobject obj =(jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctsmID,
726
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject,
727
                                                                (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
728
                                                                comboArrays.stringArray, comboArrays.objectArray));
729
                if(obj) {
730
                        result = (jstring)obj;
731
                }
732
                if (comboArrays.stringArray != NULL) {
733
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
734
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
735
                }
736
                if (result) {
737
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
738
                                        NULL);
739
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
740
                        return str;
741
                } else  {
742
                        proc->checkAndCreateException(cppClass);
743

    
744
                     }
745
        }
746
        return NULL;
747

    
748

    
749
    }
750

    
751
    XdmValue* XsltExecutable::callTemplateReturningValue(const char* templateName){
752

    
753
                  setProperty("resources", proc->getResourcesDirectory());
754
                  static jmethodID ctsmID = NULL;
755
                  if (ctsmID == NULL) {
756
                      ctsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
757
                                                  "callTemplateReturningValue",
758
                                                  "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
759
                  }
760
                  if (!ctsmID) {
761
                          std::cerr << "Error: "<<getDllname() << "callTemplateReturningValue" << " not found\n"
762
                                          << std::endl;
763

    
764
                  } else {
765
                      JParameters comboArrays;
766
                          comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
767

    
768

    
769
                      jstring result = NULL;
770
                      jobject obj = (jobject)(SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, ctsmID,
771
                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), executableObject,
772
                                        (templateName != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(templateName) : NULL),
773
                                                                          comboArrays.stringArray, comboArrays.objectArray));
774
                          if(obj) {
775
                                  result = (jstring)obj;
776
                          }
777
                          if (comboArrays.stringArray != NULL) {
778
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
779
                                  SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
780
                          }
781
                  if (result) {
782
                          jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
783
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
784
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
785
                          XdmValue * value = NULL;
786

    
787
                          XdmItem * xdmItem = NULL;
788
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
789
                                          xdmItem =  new XdmAtomicValue(result);
790
                                            xdmItem->setProcessor(proc);
791
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
792
                                            return xdmItem;
793

    
794
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
795
                                          xdmItem = new XdmNode(result);
796
                                            xdmItem->setProcessor(proc);
797
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
798
                                            return xdmItem;
799

    
800
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
801
                        xdmItem =  new XdmFunctionItem(result);
802
                        xdmItem->setProcessor(proc);
803
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
804
                        return xdmItem;
805
                                  } else {
806
                                        value = new XdmValue(result, true);
807
                                        value->setProcessor(proc);
808
                                        for(int z=0;z<value->size();z++) {
809
                                                value->itemAt(z)->setProcessor(proc);
810
                                        }
811
                                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
812
                                        return value;
813
                                }
814
                            value = new XdmValue();
815
                            value->setProcessor(proc);
816
                          value->addUnderlyingValue(result);
817
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
818
                          return value;
819
                 } else  {
820
                          proc->checkAndCreateException(cppClass);
821
                       }
822
          }
823
        return NULL;
824
    }
825

    
826

    
827

    
828

    
829
XdmValue * XsltExecutable::transformFileToValue(const char* sourcefile) {
830

    
831
        if(sourcefile == NULL){
832

    
833
                return NULL;
834
        }
835

    
836
        setProperty("resources", proc->getResourcesDirectory());
837
        static jmethodID tfvMID = NULL;
838
        
839
        if(tfvMID == NULL) {
840
                        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
841
                                        "transformToValue",
842
                                        "(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;");
843
        }
844
        if (!tfvMID) {
845
                std::cerr << "Error: "<< getDllname() << ".transformtoValue" << " not found\n"
846
                                << std::endl;
847

    
848
        } else {
849
            JParameters comboArrays;
850
                comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
851

    
852
                jobject result = (jobject)(
853
                                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tfvMID,
854
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject,
855
                                                (sourcefile != NULL ?SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : NULL),
856
                                                                 comboArrays.stringArray, comboArrays.objectArray));
857
                if (comboArrays.stringArray != NULL) {
858
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
859
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
860
                }
861
                if (result) {
862
                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
863
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
864
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
865
                        XdmValue * value = NULL;
866
                          XdmItem * xdmItem = NULL;
867

    
868

    
869
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
870
                                          xdmItem = new XdmAtomicValue(result);
871
                        xdmItem->setProcessor(proc);
872
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
873
                        return xdmItem;
874

    
875
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
876
                                          xdmItem = new XdmNode(result);
877
                        xdmItem->setProcessor(proc);
878
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
879
                        return xdmItem;
880

    
881
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
882
                        xdmItem =  new XdmFunctionItem(result);
883
                        xdmItem->setProcessor(proc);
884
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
885
                        return xdmItem;
886
                                  } else {
887
                                        value = new XdmValue(result, true);
888
                                        value->setProcessor(proc);
889
                                        for(int z=0;z<value->size();z++) {
890
                                                value->itemAt(z)->setProcessor(proc);
891
                                        }
892
                                        return value;
893
                                }
894
                                value = new XdmValue();
895
                                value->setProcessor(proc);
896
                      value->addUnderlyingValue(result);
897

    
898
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
899
                          return value;
900
                }else {
901

    
902
                        proc->checkAndCreateException(cppClass);
903

    
904
                     }
905
        }
906
        return NULL;
907

    
908
}
909

    
910

    
911
void XsltExecutable::transformFileToFile(const char* source, const char* outputfile) {
912

    
913
        setProperty("resources", proc->getResourcesDirectory());
914
        static jmethodID tffMID = NULL;
915

    
916
        if(tffMID == NULL) {
917
                        tffMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
918
                                        "transformToFile",
919
                                        "(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");
920
        }
921
        if (!tffMID) {
922
                std::cerr << "Error: "<<getDllname() << "transformToFile" << " not found\n"
923
                                << std::endl;
924

    
925
        } else {
926
            JParameters comboArrays;
927
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
928

    
929
                SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, tffMID,
930
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject
931
                                                                (source != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : NULL), NULL,
932
                                                                (outputfile != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :NULL),
933
                                                                comboArrays.stringArray, comboArrays.objectArray);
934
                if (comboArrays.stringArray!= NULL) {
935
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
936
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
937
                }
938
                }
939
                proc->checkAndCreateException(cppClass);
940
        }
941

    
942

    
943

    
944

    
945

    
946
void XsltExecutable::setupXslMessage(bool show, const char *filename=NULL) {
947
    if(show) {
948
        if(filename == NULL) {
949
            setProperty("m", "on");
950
        } else {
951
            setProperty("m", std::string(filename));
952
        }
953
    } else {
954
        setProperty("m", "off");
955
    }
956
}
957

    
958

    
959
const char * XsltExecutable::transformFileToString(const char* source) {
960

    
961
        setProperty("resources", proc->getResourcesDirectory());
962
        static jmethodID tftMID = tftMID
963
        if(tftMID == NULL) {
964
            tftMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
965
                                        "transformToString",
966
                                        "(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;");
967
        }
968
        if (!tftMID) {
969
                std::cerr << "Error: "<<getDllname() << "transformFileToString" << " not found\n"
970
                                << std::endl;
971

    
972
        } else {
973
        JParameters comboArrays;
974
    comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
975

    
976
        jstring result = NULL;
977
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, tftMID,
978
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXE.c_str()), NULL, executableObject,
979
                                                (source != NULL ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : NULL),
980
                                                                comboArrays.stringArray, comboArrays.objectArray);
981
    if (comboArrays.stringArray!= NULL) {
982
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
983
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
984
    }
985
                if(obj) {
986
                        result = (jstring)obj;
987
                }
988
                if (result) {
989
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
990
                                        NULL);
991
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
992
                        return str;
993
                } else  {
994
                        proc->checkAndCreateException(cppClass);
995

    
996
                     }
997
        }
998
        return NULL;
999
}
1000

    
1001

    
1002
   const char * XsltExecutable::transformToString(XdmNode * source){
1003

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

    
1011

    
1012
    XdmValue * XsltExecutable::transformToValue(XdmNode * source){
1013

    
1014
        if(source != NULL){
1015
                      source->incrementRefCount();
1016
                      parameters["node"] = source;
1017
            }
1018
        return transformFileToValue(NULL, NULL);
1019
   }
1020

    
1021
    void XsltExecutable::transformToFile(XdmNode * source){
1022

    
1023
        if(source != NULL){
1024
                      source->incrementRefCount();
1025
                      parameters["node"] = source;
1026
            }
1027
        transformFileToFile(NULL, NULL, NULL);
1028
   }
1029

    
1030
const char * XsltExecutable::getErrorMessage(int i ){
1031
         if(proc->exception == NULL) {return NULL;}
1032
         return proc->exception->getErrorMessage(i);
1033
 }
(34-34/50)