Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XsltProcessor.cpp @ 03fcd3f9

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