Project

Profile

Help

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

he / src / main / c / Saxon.C.API / Xslt30Processor.cpp @ 4ee4355d

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