Project

Profile

Help

How to connect?
Download (22.8 KB) Statistics
| Branch: | Tag: | Revision:

he / latest9.9 / hec / Saxon.C.API / XsltProcessor.cpp @ 1117bc69

1
// 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
        proc->exception = NULL;
43
        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
       if(valuei == NULL) {
64
                     cerr<<"Error in XsltProcessor copy constructor"<<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(int i) {
92
 if(proc->exception == NULL) {return NULL;}
93
 return proc->exception->getErrorCode(i);
94
 }
95

    
96
void XsltProcessor::setSourceFromXdmNode(XdmNode * value) {
97
    if(value != NULL){
98
      value->incrementRefCount();
99
      parameters["node"] = value;
100
    }
101
}
102

    
103
void XsltProcessor::setSourceFromFile(const char * ifile) {
104
        if(ifile != NULL) {
105
                setProperty("s", ifile);
106
        }
107
}
108

    
109
void XsltProcessor::setOutputFile(const char * ofile) {
110
        setProperty("o", ofile);
111
}
112

    
113
void XsltProcessor::setBaseOutputURI(const char * baseURI) {
114
        if(baseURI != NULL) {
115
              setProperty("baseoutput", baseURI);
116
        }
117
}
118

    
119
void XsltProcessor::setParameter(const char* name, XdmValue * value) {
120
        if(value != NULL && name != NULL){
121
                value->incrementRefCount();
122
                int s = parameters.size();
123
                std::String skey = "param:"+std::string(name);
124
                parameters[skey] = value;
125
                if(s == parameters.size()) {
126
            std::map<std::string, XdmValue*>::iterator it;
127
            it = parameters.find(skey);
128
            if (it != parameters.end()) {
129
                XdmValue * valuei = it->second;
130
                valuei->decrementRefCount();
131
                if(valuei != NULL && valuei->getRefCount() < 1){
132
                    delete value;
133
                }
134
                parameters.erase(skey);
135
                parameters[skey] = value;
136
            }
137
                }
138
        } 
139
}
140

    
141
XdmValue* XsltProcessor::getParameter(const char* name) {
142
        std::map<std::string, XdmValue*>::iterator it;
143
        it = parameters.find("param:"+std::string(name));
144
        if (it != parameters.end())
145
          return it->second;
146
        return NULL;
147
}
148

    
149
bool XsltProcessor::removeParameter(const char* name) {
150
        return (bool)(parameters.erase("param:"+std::string(name)));
151
}
152

    
153
void XsltProcessor::setJustInTimeCompilation(bool jit){
154
    jitCompilation = jit;
155
}
156

    
157
void XsltProcessor::setProperty(const char* name, const char* value) {        
158
        if(name != NULL) {
159
            int s = properties.size();
160
                std:string skey = std::string(name);
161
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == NULL ? "" : value))));
162

    
163
                if(s == properties.size()) {
164
            std::map<std::string, std::string>::iterator it;
165
            it = properties.find(skey);
166
            if (it != properties.end()) {
167
                properties.erase(skey);
168
                properties[skey] = std::string((value == NULL ? "" : value));
169
            }
170
                }
171
        }
172
}
173

    
174
const char* XsltProcessor::getProperty(const char* name) {
175
        std::map<std::string, std::string>::iterator it;
176
        it = properties.find(std::string(name));
177
        if (it != properties.end())
178
          return it->second.c_str();
179
        return NULL;
180
}
181

    
182
void XsltProcessor::clearParameters(bool delValues) {
183
        if(delValues){
184
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
185
                        
186
                        XdmValue * value = itr->second;
187
                        value->decrementRefCount();
188
#ifdef DEBUG
189
                        std::cout<<"clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
190
#endif
191
                        if(value != NULL && value->getRefCount() < 1){                
192
                                delete value;
193
                        }
194
                }
195
                
196
        } else {
197
for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
198
                
199
                        XdmValue * value = itr->second;
200
                        value->decrementRefCount();
201
                
202
                }
203

    
204
        }
205
        parameters.clear();
206
}
207

    
208
void XsltProcessor::clearProperties() {
209
        properties.clear();
210
}
211

    
212

    
213

    
214
std::map<std::string,XdmValue*>& XsltProcessor::getParameters(){
215
        std::map<std::string,XdmValue*>& ptr = parameters;
216
        return ptr;
217
}
218

    
219
std::map<std::string,std::string>& XsltProcessor::getProperties(){
220
        std::map<std::string,std::string> &ptr = properties;
221
        return ptr;
222
}
223

    
224
void XsltProcessor::exceptionClear(){
225
 if(proc->exception != NULL) {
226
         delete proc->exception;
227
         proc->exception = NULL;
228
        SaxonProcessor::sxn_environ->env->ExceptionClear();
229
 }
230
  
231
 }
232

    
233
   void XsltProcessor::setcwd(const char* dir){
234
    if(dir != NULL) {
235
        cwdXT = std::string(dir);
236
    }
237
   }
238

    
239
const char* XsltProcessor::checkException() {
240
        /*if(proc->exception == NULL) {
241
         proc->exception = proc->checkForException(environi, cpp);
242
         }
243
         return proc->exception;*/
244
        return proc->checkException(cppXT);
245
}
246

    
247
int XsltProcessor::exceptionCount(){
248
 if(proc->exception != NULL){
249
 return proc->exception->count();
250
 }
251
 return 0;
252
 }
253

    
254

    
255
    void XsltProcessor::compileFromXdmNodeAndSave(XdmNode * node, const char* filename) {
256
        static jmethodID cAndSNodemID =
257
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
258
                                        "compileFromXdmNodeAndSave",
259
                                        "(Ljava/lang/String;Ljava/lang/Object;Ljava/lang/String;)V");
260
        if (!cAndSNodemID) {
261
                std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
262
                                << " not found\n" << std::endl;
263

    
264
        } else {
265

    
266
                
267
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSNodemID,
268
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
269
                                                node->getUnderlyingValue(),                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
270
                
271
                proc->checkAndCreateException(cppClass);                
272

    
273
    }
274

    
275

    
276

    
277
}
278

    
279
    void XsltProcessor::compileFromStringAndSave(const char* stylesheetStr, const char* filename){
280
        static jmethodID cAndSStringmID =
281
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
282
                                        "compileFromStringAndSave",
283
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
284
        if (!cAndSStringmID) {
285
                std::cerr << "Error: "<<getDllname() << ".compileFromStringAndSave"
286
                                << " not found\n" << std::endl;
287

    
288
        } else {
289

    
290
                
291
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndSStringmID,
292
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
293
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr),                                                         SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
294
                
295
                proc->checkAndCreateException(cppClass);                
296

    
297
    }
298
}
299

    
300

    
301

    
302
    void XsltProcessor::compileFromFileAndSave(const char* xslFilename, const char* filename){
303
        static jmethodID cAndFStringmID =
304
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
305
                                        "compileFromFileAndSave",
306
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
307
        if (!cAndFStringmID) {
308
                std::cerr << "Error: "<<getDllname() << ".compileFromFileAndSave"
309
                                << " not found\n" << std::endl;
310

    
311
        } else {
312

    
313
                
314
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cAndFStringmID,
315
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
316
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(xslFilename),SaxonProcessor::sxn_environ->env->NewStringUTF(filename));
317
                
318
                proc->checkAndCreateException(cppClass);
319

    
320

    
321
     }
322
}
323

    
324
void XsltProcessor::compileFromString(const char* stylesheetStr) {
325
        static jmethodID cStringmID =
326
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
327
                                        "createStylesheetFromString",
328
                                        "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XsltExecutable;");
329
        if (!cStringmID) {
330
                std::cerr << "Error: "<<getDllname() << "createStylesheetFromString"
331
                                << " not found\n" << std::endl;
332

    
333
        } else {
334
                releaseStylesheet();
335
            if(jitCompilation) {
336
                setProperty("jit:", "yes");
337
            } else {
338
            setProperty("jit:", "no");
339
            }
340
                stylesheetObject = (jobject)(
341
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cStringmID,
342
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
343
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheetStr)));
344
                if (!stylesheetObject) {
345
                        proc->checkAndCreateException(cppClass);
346
                }
347
        }
348

    
349
}
350

    
351
void XsltProcessor::compileFromXdmNode(XdmNode * node) {
352
        static jmethodID cNodemID =
353
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
354
                                        "createStylesheetFromFile",
355
                                        "(Ljava/lang/String;Lnet/sf/saxon/s9api/XdmNode;)Lnet/sf/saxon/s9api/XsltExecutable;");
356
        if (!cNodemID) {
357
                std::cerr << "Error: "<< getDllname() << "createStylesheetFromXdmNode"
358
                                << " not found\n" << std::endl;
359

    
360
        } else {
361
                releaseStylesheet();
362
            if(jitCompilation) {
363
                setProperty("jit:", "yes");
364
            } else {
365
            setProperty("jit:", "no");
366
            }
367
                stylesheetObject = (jobject)(
368
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cNodemID,
369
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
370
                                                node->getUnderlyingValue()));
371
                if (!stylesheetObject) {
372
                        proc->checkAndCreateException(cppClass);
373
                        //cout << "Error in compileFromXdmNode" << endl;
374
                }
375
        }
376

    
377
}
378

    
379
void XsltProcessor::compileFromFile(const char* stylesheet) {
380
        static jmethodID cFilemID =
381
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
382
                                        "createStylesheetFromFile",
383
                                        "(Ljava/lang/String;Ljava/lang/String;)Lnet/sf/saxon/s9api/XsltExecutable;");
384
        if (!cFilemID) {
385
                std::cerr << "Error: "<<getDllname() << "createStylesheetFromFile"
386
                                << " not found\n" << std::endl;
387

    
388
        } else {
389
                releaseStylesheet();
390

    
391
                stylesheetObject = (jobject)(
392
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, cFilemID,
393
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
394
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet)));
395
                if (!stylesheetObject) {
396
                        proc->checkAndCreateException(cppClass);
397
                     
398
                }
399
                //SaxonProcessor::sxn_environ->env->NewGlobalRef(stylesheetObject);
400
        }
401

    
402
}
403

    
404
void XsltProcessor::releaseStylesheet() {
405

    
406
        stylesheetObject = NULL;
407
        
408
}
409

    
410

    
411

    
412
XdmValue * XsltProcessor::transformFileToValue(const char* sourcefile,
413
                const char* stylesheetfile) {
414

    
415
        if(exceptionOccurred()) {
416
                //Possible error detected in the compile phase. Processor not in a clean state.
417
                //Require clearing exception.
418
                return NULL;        
419
        }
420

    
421
        if(sourcefile == NULL && stylesheetfile == NULL && !stylesheetObject){
422
        
423
                return NULL;
424
        }
425

    
426
        setProperty("resources", proc->getResourcesDirectory());
427
        static jmethodID mID =
428
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
429
                                        "transformToNode",
430
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmNode;");
431
        if (!mID) {
432
                std::cerr << "Error: "<< getDllname() << "transformtoNode" << " not found\n"
433
                                << std::endl;
434

    
435
        } else {
436
                jobjectArray stringArray = NULL;
437
                jobjectArray objectArray = NULL;
438
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
439
                                "java/lang/Object");
440
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
441
                                "java/lang/String");
442

    
443
                int size = parameters.size() + properties.size();
444
                if (size > 0) {
445
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
446
                                        objectClass, 0);
447
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
448
                                        stringClass, 0);
449
                        int i = 0;
450
                        for (std::map<std::string, XdmValue*>::iterator iter =
451
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
452
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
453
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
454
                                                                (iter->first).c_str()));
455
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
456
                                                (iter->second)->getUnderlyingValue());
457
                        }
458
                        for (std::map<std::string, std::string>::iterator iter =
459
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
460
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
461
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
462
                                                                (iter->first).c_str()));
463
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
464
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
465
                                                                (iter->second).c_str()));
466
                        }
467
                }
468
                jobject result = (jobject)(
469
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
470
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
471
                                                (sourcefile != NULL ?
472
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) :
473
                                                                NULL),
474
                                                (stylesheetfile != NULL ?
475
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
476
                                                                                stylesheetfile) :
477
                                                                NULL), stringArray, objectArray));
478
                if (size > 0) {
479
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
480
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
481
                }
482
                if (result) {
483
                        XdmNode * node = new XdmNode(result);
484
                        node->setProcessor(proc);
485
                        XdmValue * value = new XdmValue();
486
                        value->addXdmItem(node);
487
                        return value;
488
                }else {
489
        
490
                        proc->checkAndCreateException(cppClass);
491
                           
492
                     }
493
        }
494
        return NULL;
495

    
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
        if(source == NULL && outputfile == NULL && !stylesheetObject){
508
                
509
                return;
510
        }
511
        setProperty("resources", proc->getResourcesDirectory());
512
        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
                jobjectArray stringArray = NULL;
522
                jobjectArray objectArray = NULL;
523
                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
                                if(xx == NULL) {
556
                                        std::cerr<<"value failed"<<std::endl;
557
                                } else {
558

    
559
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
560
                                }
561
                                if((iter->second)->getUnderlyingValue() == NULL) {
562
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
563
                                }
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
                                                                (source != NULL ?
583
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
584
                                                                                                source) :
585
                                                                                NULL),
586
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),NULL,
587
                                                                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=NULL) {
599
    if(show) {
600
        if(filename == NULL) {
601
            setProperty("m", "on");
602
        } else {
603
            setProperty("m", std::string(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
                return NULL;        
618
        }
619
        if(source == NULL && stylesheet == NULL && !stylesheetObject){
620
                std::cerr<< "Error: The most recent StylesheetObject failed. Please check exceptions"<<std::endl;
621
                return NULL;
622
        }
623
        setProperty("resources", proc->getResourcesDirectory());
624
        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
                jobjectArray stringArray = NULL;
634
                jobjectArray objectArray = NULL;
635
                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
                                if(xx == NULL) {
669
                                        std::cerr<<"value failed"<<std::endl;
670
                                } else {
671

    
672
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
673
                                }
674
                                if((iter->second)->getUnderlyingValue() == NULL) {
675
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
676
                                }
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
        jstring result = NULL;
696
        jobject obj =
697
                                (
698
                                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
699
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
700
                                                                (source != NULL ?
701
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
702
                                                                                                source) :
703
                                                                                NULL),
704
                                                                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
                                        NULL);
716
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
717
                        return str;
718
                } else  {
719
                        proc->checkAndCreateException(cppClass);  
720
                           
721
                     }
722
        }
723
        return NULL;
724
}
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
                return NULL;
731
        }
732
        return transformFileToString(NULL, NULL);
733
   }
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
                return NULL;
740
        }
741
        return transformFileToValue(NULL, NULL);
742
   }
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
        transformFileToFile(NULL, NULL, NULL);
750
   }
751

    
752
const char * XsltProcessor::getErrorMessage(int i ){
753
         if(proc->exception == NULL) {return NULL;}
754
                 return proc->exception->getErrorMessage(i);
755
 }
756

    
(36-36/50)