Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / XsltProcessor.cpp @ 5401a5ae

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
        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
                     //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
 if(exception == NULL) {
93
    return NULL;
94
 }
95
 return exception->getErrorCode();
96
}
97

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

    
105
void XsltProcessor::setSourceFromFile(const char * ifile) {
106
        if(ifile != NULL) {
107
                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
        if(baseURI != NULL) {
117
              setProperty("baseoutput", baseURI);
118
        }
119
}
120

    
121
void XsltProcessor::setParameter(const char* name, XdmValue * value) {
122
        if(value != NULL && name != NULL){
123
                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
                if(valuei != NULL && valuei->getRefCount() < 1){
134
                    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
        return NULL;
149
}
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
        if(name != NULL) {
161
            int s = properties.size();
162
                std::string skey = std::string(name);
163
                properties.insert(std::pair<std::string, std::string>(skey, std::string((value == NULL ? "" : value))));
164

    
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
                properties[skey] = std::string((value == NULL ? "" : value));
171
            }
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
        return NULL;
182
}
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
                        if(value != NULL && value->getRefCount() < 1){                
194
                                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
 if(exception != NULL) {
228
         delete exception;
229
         exception = NULL;
230
        SaxonProcessor::sxn_environ->env->ExceptionClear();
231
 }
232
  
233
 }
234

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

    
241
const char* XsltProcessor::checkException() {
242
        /*if(proc->exception == NULL) {
243
         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
        stylesheetObject = NULL;
402
        
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
                return NULL;        
414
        }
415

    
416
        if(sourcefile == NULL && stylesheetfile == NULL && !stylesheetObject){
417
        
418
                return NULL;
419
        }
420

    
421
        setProperty("resources", proc->getResourcesDirectory());
422
        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
                jobjectArray stringArray = NULL;
432
                jobjectArray objectArray = NULL;
433
                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
                                                (sourcefile != NULL ?
467
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(sourcefile) :
468
                                                                NULL),
469
                                                (stylesheetfile != NULL ?
470
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
471
                                                                                stylesheetfile) :
472
                                                                NULL), stringArray, objectArray));
473
                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
                        node->setProcessor(proc);
480
                        XdmValue * value = new XdmValue();
481
                        value->addXdmItem(node);
482
                        return value;
483
                }else {
484
        
485
                        exception = proc->checkAndCreateException(cppClass);
486
                           
487
                     }
488
        }
489
        return NULL;
490

    
491
}
492

    
493

    
494
void XsltProcessor::transformFileToFile(const char* source,
495
                const char* stylesheet, const char* outputfile) {
496

    
497
        if(exceptionOccurred()) {
498
                //Possible error detected in the compile phase. Processor not in a clean state.
499
                //Require clearing exception.
500
                return;        
501
        }
502
        if(source == NULL && outputfile == NULL && !stylesheetObject){
503
                
504
                return;
505
        }
506
        setProperty("resources", proc->getResourcesDirectory());
507
        jmethodID mID =
508
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
509
                                        "transformToFile",
510
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
511
        if (!mID) {
512
                std::cerr << "Error: "<<getDllname() << "transformToFile" << " not found\n"
513
                                << std::endl;
514

    
515
        } else {
516
                jobjectArray stringArray = NULL;
517
                jobjectArray objectArray = NULL;
518
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
519
                                "java/lang/Object");
520
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
521
                                "java/lang/String");
522

    
523
                int size = parameters.size() + properties.size();
524
#ifdef DEBUG
525
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
526
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
527
                std::cerr<<"size:"<<size<<std::endl;
528
#endif
529
                if (size > 0) {
530
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
531
                                        objectClass, 0);
532
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
533
                                        stringClass, 0);
534
                
535
                        int i = 0;
536
                        for (std::map<std::string, XdmValue*>::iterator iter =
537
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
538

    
539
#ifdef DEBUG
540
                                std::cerr<<"map 1"<<std::endl;
541
                                std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
542
#endif
543
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
544
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
545
                                                                (iter->first).c_str()));
546
#ifdef DEBUG
547
                                std::string s1 = typeid(iter->second).name();
548
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
549
                                jobject xx = (iter->second)->getUnderlyingValue();
550
                                if(xx == NULL) {
551
                                        std::cerr<<"value failed"<<std::endl;
552
                                } else {
553

    
554
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
555
                                }
556
                                if((iter->second)->getUnderlyingValue() == NULL) {
557
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
558
                                }
559
#endif
560
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
561
                                                (iter->second)->getUnderlyingValue());
562

    
563
                        }
564

    
565
                        for (std::map<std::string, std::string>::iterator iter =
566
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
567
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
568
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
569
                                                                (iter->first).c_str()));
570
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
571
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
572
                                                                (iter->second).c_str()));
573
                        }
574
                }
575
                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
576
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
577
                                                                (source != NULL ?
578
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
579
                                                                                                source) :
580
                                                                                NULL),
581
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),NULL,
582
                                                                stringArray, objectArray);
583
                if (size > 0) {
584
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
585
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
586
                }
587
                
588
        }
589

    
590
        proc->checkAndCreateException(cppClass);
591
}
592

    
593
void XsltProcessor::setupXslMessage(bool show, const char* filename) {
594
    if(show) {
595
        if(filename == NULL) {
596
            setProperty("m", "on");
597
        } else {
598
            setProperty("m", filename);
599
        }
600
    } else {
601
        setProperty("m", "off");
602
    }
603
}
604

    
605

    
606
const char * XsltProcessor::transformFileToString(const char* source,
607
                const char* stylesheet) {
608

    
609
        if(exceptionOccurred()) {
610
                //Possible error detected in the compile phase. Processor not in a clean state.
611
                //Require clearing exception.
612
                return NULL;        
613
        }
614
        if(source == NULL && stylesheet == NULL && !stylesheetObject){
615
                std::cerr<< "Error: The most recent StylesheetObject failed. Please check exceptions"<<std::endl;
616
                return NULL;
617
        }
618
        setProperty("resources", proc->getResourcesDirectory());
619
        jmethodID mID =
620
                        (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass,
621
                                        "transformToString",
622
                                        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
623
        if (!mID) {
624
                std::cerr << "Error: "<<getDllname() << "transformFileToString" << " not found\n"
625
                                << std::endl;
626

    
627
        } else {
628
                jobjectArray stringArray = NULL;
629
                jobjectArray objectArray = NULL;
630
                jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env,
631
                                "java/lang/Object");
632
                jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env,
633
                                "java/lang/String");
634

    
635
                int size = parameters.size() + properties.size();
636
#ifdef DEBUG
637
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
638
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
639
#endif
640
                if (size > 0) {
641
                        objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
642
                                        objectClass, 0);
643
                        stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
644
                                        stringClass, 0);
645
                        int i = 0;
646
                        for (std::map<std::string, XdmValue*>::iterator iter =
647
                                        parameters.begin(); iter != parameters.end(); ++iter, i++) {
648

    
649
#ifdef DEBUG
650
                                std::cerr<<"map 1"<<std::endl;
651
                                std::cerr<<"iter->first"<<(iter->first).c_str()<<std::endl;
652
#endif
653
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
654
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
655
                                                                (iter->first).c_str()));
656
#ifdef DEBUG
657
                                std::string s1 = typeid(iter->second).name();
658
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
659

    
660

    
661
                                jobject xx = (iter->second)->getUnderlyingValue();
662

    
663
                                if(xx == NULL) {
664
                                        std::cerr<<"value failed"<<std::endl;
665
                                } else {
666

    
667
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
668
                                }
669
                                if((iter->second)->getUnderlyingValue() == NULL) {
670
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
671
                                }
672
#endif
673

    
674
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
675
                                                (iter->second)->getUnderlyingValue());
676

    
677
                        }
678

    
679
                        for (std::map<std::string, std::string>::iterator iter =
680
                                        properties.begin(); iter != properties.end(); ++iter, i++) {
681
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
682
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
683
                                                                (iter->first).c_str()));
684
                                SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
685
                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
686
                                                                (iter->second).c_str()));
687
                        }
688
                }
689

    
690
        jstring result = NULL;
691
        jobject obj =
692
                                (
693
                                                SaxonProcessor::sxn_environ->env->CallObjectMethod(cppXT, mID,
694
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(cwdXT.c_str()),
695
                                                                (source != NULL ?
696
                                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(
697
                                                                                                source) :
698
                                                                                NULL),
699
                                                                SaxonProcessor::sxn_environ->env->NewStringUTF(stylesheet),
700
                                                                stringArray, objectArray));
701
                if(obj) {
702
                        result = (jstring)obj;
703
                }                
704
                if (size > 0) {
705
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
706
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
707
                }
708
                if (result) {
709
                        const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
710
                                        NULL);
711
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(obj);
712
                        return str;
713
                } else  {
714
                        exception = proc->checkAndCreateException(cppClass);
715
                           
716
                     }
717
        }
718
        return NULL;
719
}
720

    
721

    
722
   const char * XsltProcessor::transformToString(){
723
        if(!stylesheetObject){
724
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToString or check exceptions"<<std::endl;
725
                return NULL;
726
        }
727
        return transformFileToString(NULL, NULL);
728
   }
729

    
730

    
731
    XdmValue * XsltProcessor::transformToValue(){
732
        if(!stylesheetObject){
733
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToValue or check exceptions"<<std::endl;
734
                return NULL;
735
        }
736
        return transformFileToValue(NULL, NULL);
737
   }
738

    
739
    void XsltProcessor::transformToFile(){
740
        if(!stylesheetObject){
741
                std::cerr<< "Error: No styleheet found. Please compile stylsheet before calling transformToFile or check exceptions"<<std::endl;
742
                return;
743
        }
744
        transformFileToFile(NULL, NULL, NULL);
745
   }
746

    
747
const char * XsltProcessor::getErrorMessage(){
748
         if(exception == NULL) {
749
             return NULL;
750
         }
751
         return exception->getErrorMessage();
752
 }
753

    
(38-38/52)