Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 5d25f10d

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