Project

Profile

Help

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

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

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