Project

Profile

Help

Revision 6304fb8b

Added by O'Neil Delpratt 5 months ago

Saxon/C bug fixes and development work

View differences:

latest10/hec/Saxon.C.API/XsltExecutable.cpp
5 5
#include "XdmValue.h"
6 6
#include "XdmItem.h"
7 7
#include "XdmNode.h"
8
#include "XdmFunctionItem.h"
9
#include "XdmMap.h"
10
#include "XdmArray.h"
8 11
#include "XdmAtomicValue.h"
12

  
9 13
#ifdef DEBUG
10 14
#include <typeinfo> //used for testing only
11 15
#endif
12 16

  
13 17

  
14
XsltExecutable::XsltExecutable(std::string curr, jobject executableObject) {
18
XsltExecutable::XsltExecutable(jobject exObject, std::string curr) {
19

  
15 20

  
16 21

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

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

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

  
33
    tunnel = false;
34
    selection = nullptr;
35
    selectionV = nullptr;
36
    executableObject = exObject;
33 37
    cwdXE = curr;
34 38
}
35 39

  
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);
40
XsltExecutable::~XsltExecutable() {
41
    clearProperties();
42
    clearParameters();
43
    if (selectionV != nullptr) {
44
        selectionV->decrementRefCount();
45
        if (selectionV->getRefCount() == 0) {
46
            delete selectionV;
47
        }
48
    }
49
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(executableObject);
46 50

  
47
     }
51
}
48 52

  
49 53

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

  
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 {
56
    /*
57
     * Look for class.
58
     */
59
    cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
60
                            "net/sf/saxon/option/cpp/Xslt30Processor");
61

  
62
    executableObject = other.executableObject;
63
    selectionV = other.selectionV;
64
    if (selectionV != nullptr) {
65
        setInitialMatchSelection(other.selectionV);
66
    } else {
67
        selection = other.selection;
68
    }
69
    tunnel = other.tunnel;
70

  
71
    std::map<std::string, XdmValue *>::const_iterator paramIter = other.parameters.begin();
72
    while (paramIter != other.parameters.end()) {
73

  
74
        XdmValue *valuei = paramIter->second;
75
        if (valuei == nullptr) {
76
            std::cerr << "Error in XsltExecutable copy constructor" << std::endl;
77
        } else {
75 78
            parameters[paramIter->first] = new XdmValue(*(valuei));
76
       }
77
       paramIter++;
79
        }
80
        paramIter++;
78 81
    }
79 82

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

  
89 91

  
90 92
bool XsltExecutable::exceptionOccurred() {
91
	return proc->exceptionOccurred();   //TODO change to static call
93
    return SaxonProcessor::sxn_environ->env->ExceptionCheck();
92 94
}
93 95

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

  
96
const char *XsltExecutable::getErrorCode() {
97
    SaxonApiException *exception = SaxonProcessor::checkForExceptionCPP(SaxonProcessor::sxn_environ->env, cppClass,
98
                                                                        nullptr);
99
    if (exception == nullptr) { return nullptr; }
100
    return exception->getErrorCode();
101
}
99 102

  
100 103

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

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

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

  
122 125

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

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

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

  
139 142

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

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

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

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

  
189 192

  
190 193
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()) {
194
    if (option) {
195
        setProperty("outvalue", "yes");
196
    }
197
}
198

  
199
Xslt30Processor * Xslt30Processor::clone() {
200
    Xslt30Processor * proc = new Xslt30Processor(*this);
201
    return proc;
202

  
203
}
204

  
205
void XsltExecutable::setProperty(const char *name, const char *value) {
206
    if (name != nullptr) {
207
        int s = properties.size();
208
        std:
209
        std::string skey = std::string(name);
210
        properties.insert(std::pair<std::string, std::string>(skey, std::string((value == nullptr ? "" : value))));
211

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

  
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;
223
const char *XsltExecutable::getProperty(const char *name) {
224
    std::map<std::string, std::string>::iterator it;
225
    it = properties.find(std::string(name));
226
    if (it != properties.end())
227
        return it->second.c_str();
228
    return nullptr;
219 229
}
220 230

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

  
225
			XdmValue * value = itr->second;
226
			value->decrementRefCount();
235
            XdmValue *value = itr->second;
236
            value->decrementRefCount();
227 237
#ifdef DEBUG
228
			std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
238
            std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
229 239
#endif
230
			if(value != NULL && value->getRefCount() < 1){
231
	        		delete value;
232
			}
233
        	}
240
            if (value != nullptr && value->getRefCount() < 1) {
241
                delete value;
242
            }
243
        }
234 244

  
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++){
245
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(selection);
246
        selection = nullptr;
247
    } else {
248
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
239 249

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

  
243
        	}
253
        }
244 254

  
245
	selection = NULL;
246
	}
247
	parameters.clear();
255
        selection = nullptr;
256
    }
257
    parameters.clear();
248 258

  
249 259

  
250 260
}
251 261

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

  
255 265
}
256 266

  
257 267

  
268
std::map<std::string, XdmValue *> &XsltExecutable::getParameters() {
269
    std::map<std::string, XdmValue *> &ptr = parameters;
270
    return ptr;
271
}
258 272

  
259
std::map<std::string,XdmValue*>& XsltExecutable::getParameters(){
260
	std::map<std::string,XdmValue*>& ptr = parameters;
261
	return ptr;
273
std::map<std::string, std::string> &XsltExecutable::getProperties() {
274
    std::map<std::string, std::string> &ptr = properties;
275
    return ptr;
262 276
}
263 277

  
264
std::map<std::string,std::string>& XsltExecutable::getProperties(){
265
	std::map<std::string,std::string> &ptr = properties;
266
	return ptr;
278
void XsltExecutable::exceptionClear() {
279
    SaxonProcessor::sxn_environ->env->ExceptionClear();
267 280
}
268 281

  
269
void XsltExecutable::exceptionClear(){
270
 if(exception != NULL) {
271
 	delete exception;
272
 	exception = NULL;
273
	SaxonProcessor::sxn_environ->env->ExceptionClear();
274
 }
282
void XsltExecutable::setcwd(const char *dir) {
283
    if (dir != nullptr) {
284
        cwdXE = std::string(dir);
285
    }
286
}
275 287

  
276
 }
277 288

  
278
   void XsltExecutable::setcwd(const char* dir){
279
    if (dir!= NULL) {
280
        cwdXE = std::string(dir);
289
void XsltExecutable::exportStylesheet(const char *filename) {
290

  
291

  
292
    static jmethodID exportmID = nullptr;
293

  
294
    if (!exportmID) {
295
        exportmID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "save",
296
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;)V");
297
    }
298

  
299
    if (filename == nullptr) {
300
        std::cerr << "Error: Error: export file name is nullptr" << std::endl;
301
        return;
302
    }
303
    SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, exportmID,
304
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(
305
                                                                   cwdXE.c_str()),
306
                                                           executableObject,
307
                                                           SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
308

  
309
}
310

  
311
void XsltExecutable::applyTemplatesReturningFile(const char *output_filename) {
312

  
313
    if (selection == nullptr) {
314
        std::cerr
315
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
316
                << std::endl;
317
        return;
318
    }
319

  
320
    static jmethodID atmID = nullptr;
321

  
322
    if (atmID == nullptr) {
323
        (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
324
                                                                        "applyTemplatesReturningFile",
325
                                                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
326

  
327
    }
328

  
329
    if (!atmID) {
330
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsFile" << " not found\n"
331
                  << std::endl;
332

  
333
    } else {
334
        JParameters comboArrays;
335
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
336
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atmID,
337
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
338
                                                                         cwdXE.c_str()), executableObject, selection,
339
                                                                 (output_filename != nullptr
340
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
341
                                                                                 output_filename) : nullptr),
342
                                                                 comboArrays.stringArray, comboArrays.objectArray);
343
        if (comboArrays.stringArray != nullptr) {
344
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
345
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
346
        }
347

  
281 348
    }
282
   }
349
    return;
283 350

  
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 351
}
291 352

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

  
355
    if (selection == nullptr) {
356
        std::cerr
357
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
358
                << std::endl;
359
        return nullptr;
360
    }
299 361

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

  
363
    jmethodID atsmID =
364
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
365
                                                                            "applyTemplatesReturningString",
366
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
367
    if (!atsmID) {
368
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsString" << " not found\n"
369
                  << std::endl;
302 370

  
303
        static jmethodID exportmID == NULL;
371
    } else {
372
        JParameters comboArrays;
373
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
304 374

  
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");
375
        jstring result = nullptr;
376
        jobject obj = (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, atsmID,
377
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
378
                                                                                        cwdXE.c_str()), executableObject,selection,
379
                comboArrays.stringArray, comboArrays.objectArray));
380

  
381
        if (obj) {
382
            result = (jstring) obj;
383
        }
384
        if (comboArrays.stringArray != nullptr) {
385
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
386
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
387
        }
388
        if (result) {
389
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
390
                                                                                  nullptr);
391
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
392
            return str;
308 393
        }
394
    }
395
    return nullptr;
396

  
397

  
398
}
399

  
400
XdmValue *XsltExecutable::applyTemplatesReturningValue() {
401

  
402
    if (selection == nullptr) {
403
        std::cerr
404
                << "Error: The initial match selection has not been set. Please set it using setInitialMatchSelection or setInitialMatchSelectionFile."
405
                << std::endl;
406
        return nullptr;
407
    }
408

  
309 409

  
310
        if(filename == NULL) {
311
            std::cerr<< "Error: Error: export file name is NULL"<<std::endl;
312
            return;
410
    static jmethodID atsmID = nullptr;
411
    if (atsmID == nullptr) {
412
        atsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
413
                                                                                 "applyTemplatesReturningValue",
414
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
415
    }
416
    if (!atsmID) {
417
        std::cerr << "Error: " << getDllname() << "applyTemplatesAsValue" << " not found\n"
418
                  << std::endl;
419

  
420
    } else {
421
        JParameters comboArrays;
422
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
423

  
424

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

  
316 449

  
450
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
451
                xdmItem = new XdmAtomicValue(result);
452

  
453
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
454
                return xdmItem;
455

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

  
475
}
476

  
477

  
478
void XsltExecutable::callFunctionReturningFile(const char *functionName, XdmValue **arguments, int argument_length,
479
                                               const char *outfile) {
480

  
318 481

  
319
    void XsltExecutable::applyTemplatesReturningFile(const char * stylesheetfile, const char* output_filename){
482
    static jmethodID afmID = nullptr;
320 483

  
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
	}
484
    if (afmID == nullptr) {
485
        afmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
486
                                                                                "callFunctionReturningFile",
487
                                                                                "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)V");
488
    }
489

  
490
    if (!afmID) {
491
        std::cerr << "Error: " << getDllname() << "callFunctionReturningFile" << " not found\n"
492
                  << std::endl;
493
        return;
494
    } else {
495
        JParameters comboArrays;
496
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
497

  
498
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
499

  
500
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, afmID,
501
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
502
                                                                         cwdXE.c_str()),
503
                                                                 executableObject,
504
                                                                 (functionName != nullptr ?
505
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
506
                                                                          functionName) :
507
                                                                  nullptr), argumentJArray,
508
                                                                 (outfile != nullptr ?
509
                                                                  SaxonProcessor::sxn_environ->env->NewStringUTF(
510
                                                                          outfile) :
511
                                                                  nullptr),
512
                                                                 comboArrays.stringArray, comboArrays.objectArray);
513
        if (comboArrays.stringArray != nullptr) {
514
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
515
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
516
        }
517
        if (argumentJArray != nullptr) {
518
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
519
        }
325 520

  
521
    }
522
    return;
523

  
524

  
525
}
526

  
527
const char *
528
XsltExecutable::callFunctionReturningString(const char *functionName, XdmValue **arguments, int argument_length) {
529

  
530

  
531
    static jmethodID afsmID =
532
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
533
                                                                            "callFunctionReturningString",
534
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/Object;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
535
    if (!afsmID) {
536
        std::cerr << "Error: " << getDllname() << "callFunctionReturningString" << " not found\n"
537
                  << std::endl;
538

  
539
    } else {
540
        JParameters comboArrays;
541
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
542
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
543

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

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

  
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");
572
XdmValue *
573
XsltExecutable::callFunctionReturningValue(const char *functionName, XdmValue **arguments, int argument_length) {
334 574

  
335
	}
336 575

  
337
	if (!atmID) {
338
		std::cerr << "Error: "<< getDllname() << "applyTemplatesAsFile" << " not found\n"
339
				<< std::endl;
576
    static jmethodID cfvmID = nullptr;
577
    if (cfvmID == nullptr) {
578
        cfvmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
579
                                                                                 "callFunctionReturningValue",
580
                                                                                 "(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;");
581
    }
582
    if (!cfvmID) {
583
        std::cerr << "Error: " << getDllname() << "callFunctionReturningValue" << " not found\n"
584
                  << std::endl;
340 585

  
341
	} else {
586
    } else {
342 587
        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
		}
588
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
589
        jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
590

  
591
        jobject result = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, cfvmID,
592
                                                                                             SaxonProcessor::sxn_environ->env->NewStringUTF(
593
                                                                                                     cwdXE.c_str()),
594
                                                                                             executableObject,
595
                                                                                             (functionName != nullptr
596
                                                                                              ? SaxonProcessor::sxn_environ->env->NewStringUTF(
597
                                                                                                             functionName)
598
                                                                                              : nullptr),
599
                                                                                             argumentJArray,
600
                                                                                             comboArrays.stringArray,
601
                                                                                             comboArrays.objectArray));
602

  
603
        if (comboArrays.stringArray != nullptr) {
604
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
605
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
606
        }
607
        if (argumentJArray != nullptr) {
608
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
609
        }
443 610
        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);
611
            jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env,
612
                                                   "net/sf/saxon/s9api/XdmAtomicValue");
613
            jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
614
            jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env,
615
                                                    "net/sf/saxon/s9api/XdmFunctionItem");
616
            XdmValue *value = nullptr;
617
            XdmItem *xdmItem = nullptr;
618

  
619

  
620
            if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass) == JNI_TRUE) {
621
                xdmItem = new XdmAtomicValue(result);
465 622
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
466 623
                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);
624
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
625
                xdmItem = new XdmNode(result);
626
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
627
                return xdmItem;
628

  
629
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
630
                xdmItem = new XdmFunctionItem(result);
631
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
632
                return xdmItem;
633
            } else {
634
                value = new XdmValue(result, true);
635
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
636
                return value;
637
            }
638
            value = new XdmValue();
643 639
            value->addUnderlyingValue(result);
644
	        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
645
			return value;
646
          	} else  {
647
          		proc->checkAndCreateException(cppClass);
640
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
641
            return value;
642
        }
643
    }
644
    return nullptr;
645

  
646
}
647

  
648

  
649
void XsltExecutable::callTemplateReturningFile(const char *templateName, const char *outfile) {
650

  
651

  
652
    static jmethodID ctmID =
653
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
654
                                                                            "callTemplateReturningFile",
655
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
656
    if (!ctmID) {
657
        std::cerr << "Error: " << getDllname() << "callTemplateReturningFile" << " not found\n"
658
                  << std::endl;
659

  
660
    } else {
661
        JParameters comboArrays;
662
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
663
        SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctmID,
664
                                                                 SaxonProcessor::sxn_environ->env->NewStringUTF(
665
                                                                         cwdXE.c_str()),
666
                                                                 executableObject,
667
                                                                 (templateName != nullptr
668
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
669
                                                                                 templateName) : nullptr),
670
                                                                 (outfile != nullptr
671
                                                                  ? SaxonProcessor::sxn_environ->env->NewStringUTF(
672
                                                                                 outfile) : nullptr),
673
                                                                 comboArrays.stringArray, comboArrays.objectArray);
674
        if (comboArrays.stringArray != nullptr) {
675
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
676
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
677
        }
648 678

  
649
               	}
650
          }
651
          return NULL;
652 679

  
653 680
    }
654 681

  
682
}
683

  
655 684

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

  
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 687

  
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;
688
    jmethodID ctsmID =
689
            (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
690
                                                                            "callTemplateReturningString",
691
                                                                            "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
692
    if (!ctsmID) {
693
        std::cerr << "Error: " << getDllname() << "callTemplateReturningString" << " not found\n"
694
                  << std::endl;
671 695

  
672
	} else {
696
    } else {
697
        JParameters comboArrays;
698
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
699

  
700

  
701
        jstring result = nullptr;
702
        jobject obj = (jobject) (SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, ctsmID,
703
                                                                                          SaxonProcessor::sxn_environ->env->NewStringUTF(
704
                                                                                                  cwdXE.c_str()),
705
                                                                                          executableObject,
706
                                                                                          (templateName != nullptr
707
                                                                                           ? SaxonProcessor::sxn_environ->env->NewStringUTF(
708
                                                                                                          templateName)
709
                                                                                           : nullptr),
710
                                                                                          comboArrays.stringArray,
711
                                                                                          comboArrays.objectArray));
712
        if (obj) {
713
            result = (jstring) obj;
714
        }
715
        if (comboArrays.stringArray != nullptr) {
716
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
717
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
718
        }
719
        if (result) {
720
            const char *str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
721
                                                                                  nullptr);
722
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
723
            return str;
724
        }
725
    }
726
    return nullptr;
727

  
728

  
729
}
730

  
731
XdmValue *XsltExecutable::callTemplateReturningValue(const char *templateName) {
732

  
733

  
734
    static jmethodID ctsmID = nullptr;
735
    if (ctsmID == nullptr) {
736
        ctsmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
737
                                                                                 "callTemplateReturningValue",
738
                                                                                 "(Ljava/lang/String;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
739
    }
740
    if (!ctsmID) {
741
        std::cerr << "Error: " << getDllname() << "callTemplateReturningValue" << " not found\n"
742
                  << std::endl;
743

  
744
    } else {
745
        JParameters comboArrays;
746
        comboArrays = SaxonProcessor::createParameterJArray(parameters, properties);
747

  
748

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

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

  
781
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass) == JNI_TRUE) {
782
                xdmItem = new XdmNode(result);
783
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
784
                return xdmItem;
785
            } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass) == JNI_TRUE) {
786
                xdmItem = new XdmFunctionItem(result);
787
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
788
                return xdmItem;
789
            } else {
790
                value = new XdmValue(result, true);
791
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
792
                return value;
793
            }
794
            value = new XdmValue();
795
            value->addUnderlyingValue(result);
796
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
797
            return value;
798
        }
799
    }
800
    return nullptr;
801
}
802

  
803

  
804
XdmValue *XsltExecutable::transformFileToValue(const char *sourcefile) {
805

  
806
    if (sourcefile == nullptr) {
807

  
808
        return nullptr;
809
    }
810

  
811

  
812
    static jmethodID tfvMID = nullptr;
813

  
814
    if (tfvMID == nullptr) {
815
        tfvMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
816
                                                                                 "transformToValue",
817
                                                                                 "(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;");
818
    }
819
    if (!tfvMID) {
820
        std::cerr << "Error: " << getDllname() << ".transformtoValue" << " not found\n"
821
                  << std::endl;
822

  
823
    } else {
673 824
        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

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff