Project

Profile

Help

Download (24.8 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 55b80284

1 72bf04c6 Norman Walsh
// Xslt30Processor.cpp : Defines the exported functions for the DLL application.
2
//
3
4
#include "Xslt30Processor.h"
5
#include "XdmValue.h"
6
#include "XdmItem.h"
7
#include "XdmNode.h"
8
#include "XdmAtomicValue.h"
9
#include "XdmFunctionItem.h"
10 4ee4355d O'Neil Delpratt
#include "XdmMap.h"
11
#include "XdmArray.h"
12 ead48a5d O'Neil Delpratt
#include "XsltExecutable.h"
13 72bf04c6 Norman Walsh
#ifdef DEBUG
14
#include <typeinfo> //used for testing only
15
#endif
16
17 55b80284 O'Neil Delpratt
#ifdef MEM_DEBUG
18
#define new new(__FILE__, __LINE__)
19
#endif
20
21 72bf04c6 Norman Walsh
Xslt30Processor::Xslt30Processor() {
22
23
        SaxonProcessor *p = new SaxonProcessor(false);
24
        Xslt30Processor(p, "");
25
26
}
27
28
Xslt30Processor::Xslt30Processor(SaxonProcessor * p, std::string curr) {
29
30
        proc = p;
31 00082f6f O'Neil Delpratt
    tunnel = false;
32
    jitCompilation = false;
33
    exception = nullptr;
34 72bf04c6 Norman Walsh
35
        /*
36
         * Look for class.
37
         */
38
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
39
                        "net/sf/saxon/option/cpp/Xslt30Processor");
40
41
        cppXT = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
42
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
43
44
#ifdef DEBUG
45
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
46
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
47
    
48
#endif
49 00082f6f O'Neil Delpratt
50
        if(cppXT == nullptr) {
51 4ee4355d O'Neil Delpratt
        createException();
52 00082f6f O'Neil Delpratt
        }
53 72bf04c6 Norman Walsh
54
        if(!(proc->cwd.empty()) && curr.empty()){
55
                cwdXT = proc->cwd;
56
        } else if(!curr.empty()){
57
                cwdXT = curr;
58
        }
59
}
60
61
    Xslt30Processor::~Xslt30Processor(){
62
            clearParameters();
63
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(cppXT);
64
            cwdXT.erase();
65 55b80284 O'Neil Delpratt
            exceptionClear();
66 72bf04c6 Norman Walsh
    }
67
68
69
Xslt30Processor::Xslt30Processor(const Xslt30Processor &other) {
70
        proc = other.proc;
71
        cppClass = other.cppClass;
72
        cppXT = other.cppXT;
73
    cwdXT = other.cwdXT;
74
        tunnel = other.tunnel;
75
76
        std::map<std::string, XdmValue*>::const_iterator paramIter = other.parameters.begin();
77
    while(paramIter != other.parameters.end())
78
    {
79
80
       XdmValue * valuei = paramIter->second;
81 ead48a5d O'Neil Delpratt
       if(valuei == nullptr) {
82 72bf04c6 Norman Walsh
                     //std::cerr<<"Error in Xslt30Processor copy constructor"<<std::endl;
83
       } else {
84
            parameters[paramIter->first] = new XdmValue(*(valuei));
85
       }
86
       paramIter++;
87
    }
88
89
        jitCompilation = other.jitCompilation;
90
91
}
92
93
94
95
96
bool Xslt30Processor::exceptionOccurred() {
97 00082f6f O'Neil Delpratt
        return proc->exceptionOccurred() || exception != nullptr;
98 72bf04c6 Norman Walsh
}
99
100
const char * Xslt30Processor::getErrorCode() {
101 ead48a5d O'Neil Delpratt
 if(exception == nullptr) {return nullptr;}
102 72bf04c6 Norman Walsh
 return exception->getErrorCode();
103
 }
104
105
106
void Xslt30Processor::setParameter(const char* name, XdmValue * value) {
107 ead48a5d O'Neil Delpratt
        if(value != nullptr && name != nullptr){
108 72bf04c6 Norman Walsh
                value->incrementRefCount();
109
                int s = parameters.size();
110
                std::string skey = ("sparam:"+std::string(name));
111
                parameters[skey] = value;
112
                if(s == parameters.size()) {
113
            std::map<std::string, XdmValue*>::iterator it;
114
            it = parameters.find(skey);
115
            if (it != parameters.end()) {
116
                XdmValue * valuei = it->second;
117 146cfacb O'Neil Delpratt
                if(valuei != nullptr) {
118
                    valuei->decrementRefCount();
119 55b80284 O'Neil Delpratt
                    //if (valuei->getRefCount() < 1) {
120 146cfacb O'Neil Delpratt
                        delete value;
121 55b80284 O'Neil Delpratt
                        value = nullptr;
122
                    //}
123 146cfacb O'Neil Delpratt
                    parameters.erase(skey);
124
                    parameters[skey] = value;
125
                    value->incrementRefCount();
126 72bf04c6 Norman Walsh
                }
127
            }
128
                }
129
         }
130
}
131
132
XdmValue* Xslt30Processor::getParameter(const char* name) {
133
        std::map<std::string, XdmValue*>::iterator it;
134
        it = parameters.find("sparam:"+std::string(name));
135
        if (it != parameters.end())
136
          return it->second;
137 ead48a5d O'Neil Delpratt
            return nullptr;
138 72bf04c6 Norman Walsh
}
139
140
bool Xslt30Processor::removeParameter(const char* name) {
141
        return (bool)(parameters.erase("param:"+std::string(name)));
142
}
143
144
void Xslt30Processor::setJustInTimeCompilation(bool jit){
145
    jitCompilation = jit;
146
}
147
148
149
void Xslt30Processor::clearParameters(bool delValues) {
150
        if(delValues){
151 146cfacb O'Neil Delpratt
        for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
152
            XdmValue * value = itr->second;
153
            if(value != nullptr) {
154 72bf04c6 Norman Walsh
#ifdef DEBUG
155 146cfacb O'Neil Delpratt
                std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
156 72bf04c6 Norman Walsh
#endif
157 146cfacb O'Neil Delpratt
                    delete value;
158 55b80284 O'Neil Delpratt
                    value = nullptr;
159 4ee4355d O'Neil Delpratt
160 146cfacb O'Neil Delpratt
            }
161
        }
162 72bf04c6 Norman Walsh
163
        }
164
        parameters.clear();
165
        
166
}
167
168
169
170
std::map<std::string,XdmValue*>& Xslt30Processor::getParameters(){
171
        std::map<std::string,XdmValue*>& ptr = parameters;
172
        return ptr;
173
}
174
175
176
void Xslt30Processor::exceptionClear(){
177 ead48a5d O'Neil Delpratt
 if(exception != nullptr) {
178 72bf04c6 Norman Walsh
         delete exception;
179 ead48a5d O'Neil Delpratt
         exception = nullptr;
180 72bf04c6 Norman Walsh
        SaxonProcessor::sxn_environ->env->ExceptionClear();
181
 }
182
  
183
 }
184
185
   void Xslt30Processor::setcwd(const char* dir){
186 ead48a5d O'Neil Delpratt
    if (dir!= nullptr) {
187 72bf04c6 Norman Walsh
        cwdXT = std::string(dir);
188
    }
189
   }
190
191 79d12c83 O'Neil Delpratt
SaxonApiException* Xslt30Processor::getException() {
192
    return exception;
193 72bf04c6 Norman Walsh
}
194
195 4ee4355d O'Neil Delpratt
void Xslt30Processor::createException(const char * message) {
196 55b80284 O'Neil Delpratt
    exceptionClear();
197 4ee4355d O'Neil Delpratt
    if(message == nullptr) {
198
        exception = proc->checkAndCreateException(cppClass);
199
    } else {
200
        exception = new SaxonApiException(message);
201
    }
202
203
}
204
205 72bf04c6 Norman Walsh
206
207
void Xslt30Processor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
208 ead48a5d O'Neil Delpratt
        static jmethodID cAndSNodemID = nullptr;
209 72bf04c6 Norman Walsh
210 ead48a5d O'Neil Delpratt
        if(cAndSNodemID == nullptr) {
211 72bf04c6 Norman Walsh
                        cAndSNodemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
212
                                        "compileFromXdmNodeAndSave",
213
                                        "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V");
214
        }
215
        if (!cAndSNodemID) {
216 306962bc O'Neil Delpratt
                std::cerr<<"Error: "<<getDllname()<<"compileFromStringAndSave function not found"<<std::endl;
217 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
218 72bf04c6 Norman Walsh
        } else {
219 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
220
#if defined(DEBUG)
221
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
222
#endif
223
            return;
224
        }
225 72bf04c6 Norman Walsh
                
226
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSNodemID,
227
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
228
                                                node->getUnderlyingValue(), SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
229
                
230 4ee4355d O'Neil Delpratt
                createException();
231 72bf04c6 Norman Walsh
232
    }
233
234
235
236
}
237
238
    void Xslt30Processor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
239 ead48a5d O'Neil Delpratt
        static jmethodID cAndSStringmID = nullptr;
240
        if(cAndSStringmID == nullptr) {
241 72bf04c6 Norman Walsh
           cAndSStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
242
                                        "compileFromStringAndSave",
243
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
244
        }
245
        if (!cAndSStringmID) {
246 4ee4355d O'Neil Delpratt
        createException("Error: compileFromStringAndSave function not found\n");
247 72bf04c6 Norman Walsh
248
        } else {
249 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
250
#if defined(DEBUG)
251
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
252
#endif
253
            return;
254
        }
255 72bf04c6 Norman Walsh
                
256
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
257
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
258
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),
259
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
260
                
261 4ee4355d O'Neil Delpratt
                createException();
262 72bf04c6 Norman Walsh
263
    }
264
}
265
266
267
268
    void Xslt30Processor::compileFromFileAndSave(const char* xslFilename, const char* filename){
269 ead48a5d O'Neil Delpratt
        static jmethodID cAndFStringmID =  nullptr;
270 72bf04c6 Norman Walsh
271 ead48a5d O'Neil Delpratt
        if (cAndFStringmID == nullptr) {
272 72bf04c6 Norman Walsh
            cAndFStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
273
                                        "compileFromFileAndSave",
274
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
275
        }
276
        if (!cAndFStringmID) {
277 4ee4355d O'Neil Delpratt
        createException("Error: compileFromFileAndSave function not found");
278 72bf04c6 Norman Walsh
279
        } else {
280
281 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
282
#if defined(DEBUG)
283
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
284
#endif
285
            return;
286
        }
287 72bf04c6 Norman Walsh
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
288
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
289
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
290
291 4ee4355d O'Neil Delpratt
        createException();
292 72bf04c6 Norman Walsh
293
294
     }
295
}
296
297
XsltExecutable * Xslt30Processor::compileFromString(const char* stylesheetStr) {
298 ead48a5d O'Neil Delpratt
        static jmethodID cStringmID = nullptr;
299
        if (cStringmID == nullptr) {
300 72bf04c6 Norman Walsh
                        cStringmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
301
                                        "compileFromString",
302 e05bbe34 O'Neil Delpratt
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
303 72bf04c6 Norman Walsh
        }
304
                                        
305 e05bbe34 O'Neil Delpratt
        if (cStringmID== nullptr) {
306 4ee4355d O'Neil Delpratt
        createException(strcat(getDllname(), ".compileFromString funtion not found"));
307 ead48a5d O'Neil Delpratt
                return nullptr;
308 72bf04c6 Norman Walsh
309
        } else {
310 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
311
#if defined(DEBUG)
312
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
313
#endif
314
            return nullptr;
315
        }
316 72bf04c6 Norman Walsh
                JParameters comboArrays;
317
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
318
                jobject executableObject = (jobject)(
319
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
320
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
321
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
322
                if (!executableObject) {
323 4ee4355d O'Neil Delpratt
            createException();
324 ead48a5d O'Neil Delpratt
                        return nullptr;
325 72bf04c6 Norman Walsh
                }
326 ead48a5d O'Neil Delpratt
                if (comboArrays.stringArray != nullptr) {
327 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
328
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
329
                }
330
331 ead48a5d O'Neil Delpratt
                jobject obj = SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject);
332
        XsltExecutable * executable = new XsltExecutable(obj, cwdXT);
333 72bf04c6 Norman Walsh
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
334
                return executable;
335
        }
336
337
}
338
339
XsltExecutable * Xslt30Processor::compileFromXdmNode(XdmNode * node) {
340 ead48a5d O'Neil Delpratt
        static jmethodID cNodemID = nullptr;
341
    if(cNodemID == nullptr) {
342 72bf04c6 Norman Walsh
                        cNodemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,"compileFromXdmNode",
343 e05bbe34 O'Neil Delpratt
                        "(Ljava/lang/String;Ljava/lang/Object;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
344 72bf04c6 Norman Walsh
        }
345
        if (!cNodemID) {
346
                std::cerr << "Error: "<< getDllname() << ".compileFromXdmNode"
347
                                << " not found\n" << std::endl;
348 ead48a5d O'Neil Delpratt
                return nullptr;
349 72bf04c6 Norman Walsh
350
        } else {
351 00082f6f O'Neil Delpratt
            if(node == nullptr) {
352 4ee4355d O'Neil Delpratt
            createException("Error in compileFromXdmNode - node argument is NULL");
353 00082f6f O'Neil Delpratt
                return nullptr;
354
            }
355
        if(cppXT == nullptr) {
356
#if defined(DEBUG)
357
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
358
#endif
359
            return nullptr;
360
        }
361 72bf04c6 Norman Walsh
                JParameters comboArrays;
362
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
363
                jobject executableObject = (jobject)(
364
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cNodemID,
365
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
366
                                                node->getUnderlyingValue(), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
367
                if (!executableObject) {
368 4ee4355d O'Neil Delpratt
                        createException();
369 ead48a5d O'Neil Delpratt
                        return nullptr;
370 72bf04c6 Norman Walsh
                }
371 ead48a5d O'Neil Delpratt
                if (comboArrays.stringArray != nullptr) {
372 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
373
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
374
                }
375
376
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
377
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
378
                return executable;
379
        }
380
381
}
382
383
XsltExecutable * Xslt30Processor::compileFromAssociatedFile(const char* source) {
384 ead48a5d O'Neil Delpratt
        static jmethodID cFilemID = nullptr;
385
    if(cFilemID == nullptr) {
386 72bf04c6 Norman Walsh
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
387
                                        "compileFromAssociatedFile",
388
                                        "(Ljava/lang/String;Ljava/lang/String;Z;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
389
        }
390
        if (!cFilemID) {
391
                std::cerr << "Error: "<<getDllname() << ".compileFromAssociatedFile"
392
                                << " not found\n" << std::endl;
393 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
394 ead48a5d O'Neil Delpratt
                return nullptr;
395 72bf04c6 Norman Walsh
396
        } else {
397
                
398 ead48a5d O'Neil Delpratt
                if(source == nullptr) {
399 4ee4355d O'Neil Delpratt
            createException("Error in compileFromFile method - The Stylesheet file is NULL");
400 79d12c83 O'Neil Delpratt
                        return nullptr;
401 72bf04c6 Norman Walsh
                }
402 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
403
#if defined(DEBUG)
404
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
405
#endif
406
            return nullptr;
407
        }
408 72bf04c6 Norman Walsh
                JParameters comboArrays;
409
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
410
                jobject executableObject = (jobject)(
411
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
412
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
413
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(source), jitCompilation, comboArrays.stringArray, comboArrays.objectArray));
414
                if (!executableObject) {
415 4ee4355d O'Neil Delpratt
                        createException();
416 ead48a5d O'Neil Delpratt
                     return nullptr;
417 72bf04c6 Norman Walsh
                }
418 ead48a5d O'Neil Delpratt
                if (comboArrays.stringArray != nullptr) {
419 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
420
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
421
                }
422
423
        XsltExecutable * executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject));
424
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
425
                return executable;
426
        }
427
428
}
429
430
431
XsltExecutable * Xslt30Processor::compileFromFile(const char* stylesheet) {
432 ead48a5d O'Neil Delpratt
        static jmethodID cFilemID = nullptr;
433
        if(cFilemID == nullptr) {
434 72bf04c6 Norman Walsh
            cFilemID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
435
                                        "compileFromFile",
436 e05bbe34 O'Neil Delpratt
                                        "(Ljava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XsltExecutable;");
437 72bf04c6 Norman Walsh
        }
438 00082f6f O'Neil Delpratt
439 72bf04c6 Norman Walsh
        if (!cFilemID) {
440
                std::cerr << "Error: "<<getDllname() << ".compileFromFile"
441
                                << " not found\n" << std::endl;
442 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
443 ead48a5d O'Neil Delpratt
                return nullptr;
444 72bf04c6 Norman Walsh
445
        } else {
446 e05bbe34 O'Neil Delpratt
        if (stylesheet == nullptr) {
447 4ee4355d O'Neil Delpratt
            createException("The Stylesheet file is NULL");
448 00082f6f O'Neil Delpratt
            return nullptr;
449
        }
450
        if(cppXT == nullptr) {
451
#if defined(DEBUG)
452
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
453
#endif
454
455 e05bbe34 O'Neil Delpratt
            return nullptr;
456
        }
457 00082f6f O'Neil Delpratt
458 e05bbe34 O'Neil Delpratt
        JParameters comboArrays;
459
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
460
        jobject executableObject = (jobject) (
461
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
462
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
463
                                                                           cwdXT.c_str()),
464
                                                                   SaxonProcessor::sxn_environ->env->NewStringUTF(
465
                                                                           stylesheet), jitCompilation,
466
                                                                   comboArrays.stringArray, comboArrays.objectArray));
467
        if (!executableObject) {
468 4ee4355d O'Neil Delpratt
            createException();
469 e05bbe34 O'Neil Delpratt
            return nullptr;
470
471
        }
472
        if (comboArrays.stringArray != nullptr) {
473
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
474
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
475
        }
476 72bf04c6 Norman Walsh
477 ead48a5d O'Neil Delpratt
        XsltExecutable *executable;
478
        executable = new XsltExecutable(SaxonProcessor::sxn_environ->env->NewGlobalRef(executableObject), cwdXT);
479 72bf04c6 Norman Walsh
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(executableObject);
480 e05bbe34 O'Neil Delpratt
481
        return executable;
482
483 72bf04c6 Norman Walsh
        }
484
485
}
486
487
488
489
490
XdmValue * Xslt30Processor::transformFileToValue(const char* sourcefile,
491
                const char* stylesheetfile) {
492
493
        if(exceptionOccurred()) {
494
                //Possible error detected in the compile phase. Processor not in a clean state.
495
                //Require clearing exception.
496 4ee4355d O'Neil Delpratt
                SaxonProcessor::sxn_environ->env->ExceptionDescribe();
497
498 ead48a5d O'Neil Delpratt
                return nullptr;
499 72bf04c6 Norman Walsh
        }
500
501 ead48a5d O'Neil Delpratt
        if(sourcefile == nullptr && stylesheetfile == nullptr){
502 72bf04c6 Norman Walsh
        
503 ead48a5d O'Neil Delpratt
                return nullptr;
504 72bf04c6 Norman Walsh
        }
505
506
507 ead48a5d O'Neil Delpratt
        static jmethodID mtvID = nullptr;
508
509
        if(mtvID == nullptr) {
510 72bf04c6 Norman Walsh
                        mtvID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
511
                                        "transformToValue",
512
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmValue;");
513
        }
514
        if (!mtvID) {
515 990eca02 O'Neil Delpratt
        std::cerr<<"Error: "<<getDllname()<<".transformtoValue function not found"<<std::endl;
516 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
517 72bf04c6 Norman Walsh
        } else {
518 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
519 4ee4355d O'Neil Delpratt
//#if defined(DEBUG)
520 306962bc O'Neil Delpratt
            createException("The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown");
521 4ee4355d O'Neil Delpratt
//#endif
522 00082f6f O'Neil Delpratt
            return nullptr;
523
        }
524 72bf04c6 Norman Walsh
            JParameters comboArrays;
525
                comboArrays = SaxonProcessor::createParameterJArray2(parameters);
526 4ee4355d O'Neil Delpratt
        jstring jcwd = SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str());
527
        jstring jsourcefile = (sourcefile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) : nullptr);
528
        jstring jstylesheetfile = (stylesheetfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetfile) : nullptr);
529 72bf04c6 Norman Walsh
                jobject result = (jobject)(
530 4ee4355d O'Neil Delpratt
                                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtvID,jcwd, cppXT, nullptr, jsourcefile,
531
                                                                            jstylesheetfile, comboArrays.stringArray, comboArrays.objectArray));
532 ead48a5d O'Neil Delpratt
                if (comboArrays.stringArray != nullptr) {
533 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
534
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
535
                }
536 4ee4355d O'Neil Delpratt
        if (jcwd != nullptr) {
537
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jcwd);
538
        }
539
        if (jsourcefile != nullptr) {
540
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jsourcefile);
541
        }
542
        if (jstylesheetfile != nullptr) {
543
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jstylesheetfile);
544
        }
545 72bf04c6 Norman Walsh
                if (result) {
546 4ee4355d O'Neil Delpratt
547 72bf04c6 Norman Walsh
                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
548
                          jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
549
                          jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
550 4ee4355d O'Neil Delpratt
                jclass mapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
551
                jclass arrayClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmArray");
552
                            XdmValue * value = nullptr;
553 ead48a5d O'Neil Delpratt
                          XdmItem * xdmItem = nullptr;
554 72bf04c6 Norman Walsh
555
556
                                  if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
557 4ee4355d O'Neil Delpratt
                                          xdmItem = new XdmAtomicValue(result);
558 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
559
                        return xdmItem;
560
561
                                  } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
562 4ee4355d O'Neil Delpratt
                                          xdmItem = new XdmNode(result);
563 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
564
                        return xdmItem;
565 4ee4355d O'Neil Delpratt
566 72bf04c6 Norman Walsh
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
567 4ee4355d O'Neil Delpratt
                        xdmItem =  new XdmFunctionItem(result);
568 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
569
                        return xdmItem;
570 4ee4355d O'Neil Delpratt
571
                                  } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, mapClass)           == JNI_TRUE) {
572
                        xdmItem =  new XdmMap(result);
573
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
574
                        return xdmItem;
575
576
                    } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, arrayClass)           == JNI_TRUE) {
577
                        xdmItem =  new XdmArray(result);
578
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
579
                        return xdmItem;
580
581
                    } else {
582 72bf04c6 Norman Walsh
                                        value = new XdmValue(result, true);
583
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
584
                                        return value;
585
                                }
586
                                value = new XdmValue();
587
                      value->addUnderlyingValue(result);
588
589
                          SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
590
                          return value;
591
                }else {
592 4ee4355d O'Neil Delpratt
593
                        createException();
594
595 72bf04c6 Norman Walsh
                     }
596
        }
597 ead48a5d O'Neil Delpratt
        return nullptr;
598 72bf04c6 Norman Walsh
599
}
600
601
602
void Xslt30Processor::transformFileToFile(const char* source,
603
                const char* stylesheet, const char* outputfile) {
604
605
        if(exceptionOccurred()) {
606
                //Possible error detected in the compile phase. Processor not in a clean state.
607
                //Require clearing exception.
608
                return;        
609
        }
610 ead48a5d O'Neil Delpratt
        if(stylesheet==nullptr){
611 72bf04c6 Norman Walsh
                std::cerr<< "Error: stylesheet has not been set."<<std::endl;
612
                return;
613
        }
614 ead48a5d O'Neil Delpratt
        //
615
        static jmethodID mtfID = nullptr;
616 72bf04c6 Norman Walsh
617 ead48a5d O'Neil Delpratt
        if(mtfID == nullptr) {
618 72bf04c6 Norman Walsh
                mtfID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
619
                                        "transformToFile",
620
                                        "(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");
621
        }
622
        if (!mtfID) {
623 990eca02 O'Neil Delpratt
        std::cerr<<"Error: "<<getDllname()<<".transformToFile function not found"<<std::endl;
624 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
625 72bf04c6 Norman Walsh
        } else {
626 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
627
#if defined(DEBUG)
628
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
629
#endif
630
            return;
631
        }
632 72bf04c6 Norman Walsh
            JParameters comboArrays;
633
        comboArrays = SaxonProcessor::createParameterJArray2(parameters);
634
635
                SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtfID,
636 ead48a5d O'Neil Delpratt
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
637
                                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(source) : nullptr),
638
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),        (outputfile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(outputfile) :nullptr),
639 72bf04c6 Norman Walsh
                                                                comboArrays.stringArray, comboArrays.objectArray);
640 ead48a5d O'Neil Delpratt
                if (comboArrays.stringArray!= nullptr) {
641 72bf04c6 Norman Walsh
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
642
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
643
                }
644
                }
645 4ee4355d O'Neil Delpratt
                createException();
646 72bf04c6 Norman Walsh
        }
647
648
649
const char * Xslt30Processor::transformFileToString(const char* source,
650
                const char* stylesheet) {
651
652
        if(exceptionOccurred()) {
653
                //Possible error detected in the compile phase. Processor not in a clean state.
654
                //Require clearing exception.
655 ead48a5d O'Neil Delpratt
                return nullptr;
656 72bf04c6 Norman Walsh
        }
657 ead48a5d O'Neil Delpratt
        if(source == nullptr && stylesheet == nullptr){
658
                std::cerr<< "Error: nullptr file name found in transformFiletoString."<<std::endl;
659
                return nullptr;
660 72bf04c6 Norman Walsh
        }
661
662 ead48a5d O'Neil Delpratt
        static jmethodID mtsID =  nullptr;
663
664
        if(mtsID == nullptr) {
665 72bf04c6 Norman Walsh
                        mtsID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass,
666
                                        "transformToString",
667 4ee4355d O'Neil Delpratt
                                        "(Ljava/lang/String;Lnet/sf/saxon/option/cpp/Xslt30Processor;Lnet/sf/saxon/s9api/XsltExecutable;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)[B");
668 72bf04c6 Norman Walsh
        }
669
        if (!mtsID) {
670 990eca02 O'Neil Delpratt
        std::cerr<<"Error: "<<getDllname()<<".transformFileToString not found"<<std::endl;
671 4ee4355d O'Neil Delpratt
        SaxonProcessor::sxn_environ->env->ExceptionClear();
672 72bf04c6 Norman Walsh
        } else {
673 00082f6f O'Neil Delpratt
        if(cppXT == nullptr) {
674
#if defined(DEBUG)
675
            std::cerr << "The Java Xslt30Processor object (i.e. cppXT) is NULL - Possible exception thrown" << std::endl;
676
#endif
677
            return nullptr;
678
        }
679 72bf04c6 Norman Walsh
    JParameters comboArrays;
680
    comboArrays = SaxonProcessor::createParameterJArray2(parameters);
681
682 4ee4355d O'Neil Delpratt
    jbyteArray result = nullptr;
683 72bf04c6 Norman Walsh
        jobject obj = SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(cppClass, mtsID,
684 ead48a5d O'Neil Delpratt
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()), cppXT, nullptr,
685
                                                (source != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(
686
                                                                                                source) : nullptr),
687
                                                                (stylesheet != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet) : nullptr),
688 72bf04c6 Norman Walsh
                                                                comboArrays.stringArray, comboArrays.objectArray);
689 ead48a5d O'Neil Delpratt
    if (comboArrays.stringArray!= nullptr) {
690 72bf04c6 Norman Walsh
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.stringArray);
691
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(comboArrays.objectArray);
692
    }
693
        if(obj) {
694 4ee4355d O'Neil Delpratt
                        result = (jbyteArray)obj;
695 72bf04c6 Norman Walsh
        }
696
697
        if (result) {
698 4ee4355d O'Neil Delpratt
        jboolean isCopy = false;
699
        jbyte* b = SaxonProcessor::sxn_environ->env->GetByteArrayElements(result, &isCopy);
700
        jsize num_bytes = SaxonProcessor::sxn_environ->env->GetArrayLength(result);
701
702
        const char *str = new char[num_bytes];
703
        memcpy ((void *) str, b , num_bytes );
704
705
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
706
        return str;
707
        } else  {
708
            createException();
709 72bf04c6 Norman Walsh
                           
710 4ee4355d O'Neil Delpratt
        }
711 72bf04c6 Norman Walsh
        }
712 ead48a5d O'Neil Delpratt
        return nullptr;
713 72bf04c6 Norman Walsh
}
714
715
716
717
const char * Xslt30Processor::getErrorMessage(){
718 ead48a5d O'Neil Delpratt
         if(exception == nullptr) {return nullptr;}
719
         return exception->getMessage();
720 72bf04c6 Norman Walsh
 }