Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SchemaValidator.cpp @ 6af44631

1
#include "SchemaValidator.h"
2
#include "XdmNode.h"
3
#include "XdmValue.h"
4
#include "XdmItem.h"
5

    
6
//#define DEBUG
7

    
8
#ifdef MEM_DEBUG
9
#define new new(__FILE__, __LINE__)
10
#endif
11

    
12
SchemaValidator::SchemaValidator() {
13
        SaxonProcessor *p = new SaxonProcessor(true);
14
        SchemaValidator(p, "");
15
}
16

    
17
SchemaValidator::SchemaValidator(SaxonProcessor* p, std::string curr){
18
        proc = p;
19
        /*
20
         * Look for class.
21
         */
22
        cppClass = lookForClass(SaxonProcessor::sxn_environ->env,
23
                        "net/sf/saxon/option/cpp/SchemaValidatorForCpp");
24
        if ((proc->proc) == nullptr) {
25
                std::cerr << "Processor is nullptr" << std::endl;
26
        }
27

    
28
        cppV = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
29
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
30

    
31
#ifdef DEBUG
32
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
33
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
34
#endif    
35

    
36
        
37
        if(!(proc->cwd.empty()) && curr.empty()){
38
                cwdV = proc->cwd;
39
        } else {
40
                cwdV = curr;
41
        }
42
        exception = proc->checkAndCreateException(cppClass);
43

    
44
}
45

    
46
   void SchemaValidator::setcwd(const char* dir){
47
        if(dir==nullptr) {
48
                    cwdV = std::string(dir);
49
        }
50
   }
51

    
52
  void SchemaValidator::setOutputFile(const char * sourceFile){
53
        outputFile = std::string(sourceFile);
54
  }
55

    
56
  XdmNode * SchemaValidator::getValidationReport(){
57
        jmethodID mID =
58
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "getValidationReport", "()Lnet/sf/saxon/s9api/XdmNode;");
59
        if (!mID) {
60
                std::cerr << "Error: libsaxon." << "validate.getValidationReport()" << " not found\n"
61
                        << std::endl;
62
        } else {
63
                jobject result = (jobject)(
64
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppV, mID));
65
                
66
                if (result) {
67
                        XdmNode * node = new XdmNode(result);
68
                        return node;
69
                }
70
                exception = proc->checkAndCreateException(cppClass);
71
        }
72
        return nullptr;
73
}
74

    
75
  void SchemaValidator::registerSchemaFromFile(const char * sourceFile){
76
        if (sourceFile == nullptr) {
77
                std::cerr << "Error:: sourceFile string cannot be empty or nullptr" << std::endl;
78
             return;
79
        }
80
        
81
        jmethodID mID =
82
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "registerSchema",
83
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
84
        if (!mID) {
85
                std::cerr << "Error: libsaxon." << "validate" << " not found\n"
86
                        << std::endl;
87
        } else {
88
        jobjectArray stringArray = nullptr;
89
        jobjectArray objectArray = nullptr;
90
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
91
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
92

    
93
        int size = parameters.size() + properties.size();
94
#ifdef DEBUG
95
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
96
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
97
                std::cerr<<"size:"<<size<<std::endl;
98
#endif
99
        if (size > 0) {
100
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
101
                                objectClass, 0);
102
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
103
                                stringClass, 0);
104
                int i = 0;
105
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
106
                                iter != parameters.end(); ++iter, i++) {
107
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
108
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
109
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
110
                                        (iter->second)->getUnderlyingValue());
111
#ifdef DEBUG
112
                                std::string s1 = typeid(iter->second).name();
113
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
114
                                jobject xx = (iter->second)->getUnderlyingValue();
115
                                if(xx == nullptr) {
116
                                        std::cerr<<"value failed"<<std::endl;
117
                                } else {
118

    
119
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
120
                                }
121
                                if((iter->second)->getUnderlyingValue() == nullptr) {
122
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
123
                                }
124
#endif
125
                }
126
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
127
                                iter != properties.end(); ++iter, i++) {
128
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
129
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
130
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
131
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
132
                }
133
        }
134
        
135
                        SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
136
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
137
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(sourceFile), stringArray, objectArray);
138

    
139
        if (size > 0) {
140
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
141
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
142
        }
143

    
144
}
145
        exception = proc->checkAndCreateException(cppClass);
146
                     
147
        
148
 }
149

    
150
 void SchemaValidator::exportSchema(const char * fileName) {
151
      if (fileName == nullptr) {
152
                      std::cerr << "Error:: fileName string cannot be empty or nullptr" << std::endl;
153
                   return;
154
              }
155

    
156
              jmethodID mID =
157
                      (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "exportSchema",
158
                                      "(Ljava/lang/String;Ljava/lang/String;)V");
159
              if (!mID) {
160
                      std::cerr << "Error: libsaxon." << "exportSchema" << " not found\n"
161
                              << std::endl;
162
              } else {
163

    
164

    
165
                              SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
166
                                              SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
167
                                              SaxonProcessor::sxn_environ->env->NewStringUTF(fileName));
168

    
169

    
170

    
171
      }
172
      exception = proc->checkAndCreateException(cppClass);
173

    
174
 }
175

    
176
  void SchemaValidator::registerSchemaFromString(const char * sourceStr){
177

    
178
        
179
        if (sourceStr == nullptr) {
180
                std::cerr << "Error:: Schema string cannot be empty or nullptr" << std::endl;
181
             return;
182
        }
183
        jmethodID mID =
184
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "registerSchemaString",
185
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
186

    
187
        if (!mID) {
188
                std::cerr << "Error: libsaxon." << "registerSchemaString" << " not found\n"
189
                        << std::endl;
190
        } else {
191
        jobjectArray stringArray = nullptr;
192
        jobjectArray objectArray = nullptr;
193
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
194
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
195

    
196
        int size = parameters.size() + properties.size();
197
#ifdef DEBUG
198
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
199
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
200
                std::cerr<<"size:"<<size<<std::endl;
201
#endif
202

    
203
        if (size > 0) {
204
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
205
                                objectClass, 0);
206
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
207
                                stringClass, 0);
208
                int i = 0;
209
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
210
                                iter != parameters.end(); ++iter, i++) {
211
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
212
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
213
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
214
                                        (iter->second)->getUnderlyingValue());
215
#ifdef DEBUG
216
                                std::string s1 = typeid(iter->second).name();
217
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
218
                                jobject xx = (iter->second)->getUnderlyingValue();
219
                                if(xx == nullptr) {
220
                                        std::cerr<<"value failed"<<std::endl;
221
                                } else {
222

    
223
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
224
                                }
225
                                if((iter->second)->getUnderlyingValue() == nullptr) {
226
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
227
                                }
228
#endif
229
                }
230
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
231
                                iter != properties.end(); ++iter, i++) {
232
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
233
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
234
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
235
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
236
                }
237
        }
238

    
239
        
240
                        SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
241
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
242
                                        (sourceStr != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourceStr) : nullptr), nullptr, stringArray, objectArray);
243

    
244
        if (size > 0) {
245
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
246
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
247
        }
248

    
249
    }
250
        exception = proc->checkAndCreateException(cppClass);
251
    
252
}
253

    
254
  void SchemaValidator::validate(const char * sourceFile){
255
        /*if (sourceFile == nullptr) {
256
                std::cerr << "Error:: sourceFile string cannot be empty or nullptr" << std::endl;
257
             return;
258
        }*/
259

    
260
jmethodID mID =
261
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "validate",
262
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
263
if (!mID) {
264
        std::cerr << "Error: libsaxon." << "validate" << " not found\n"
265
                        << std::endl;
266

    
267
} else {
268
        jobjectArray stringArray = nullptr;
269
        jobjectArray objectArray = nullptr;
270
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
271
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
272

    
273
        int size = parameters.size() + properties.size();
274
        if(lax) {
275
      size++;
276
     }
277
#ifdef DEBUG
278
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
279
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
280
                std::cerr<<"size:"<<size<<std::endl;
281
#endif
282
        if (size > 0) {
283
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
284
                                objectClass, 0);
285
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
286
                                stringClass, 0);
287
                int i = 0;
288
        if(lax){
289
            SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
290
                        SaxonProcessor::sxn_environ->env->NewStringUTF("lax"));
291
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
292
                        booleanValue(SaxonProcessor::sxn_environ, lax));
293
            i++;
294
        }
295
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
296
                                iter != parameters.end(); ++iter, i++) {
297
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
298
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
299
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
300
                                        (iter->second)->getUnderlyingValue());
301

    
302
#ifdef DEBUG
303
                                std::string s1 = typeid(iter->second).name();
304
                                std::cerr<<"param-name:"<<(iter->first)<<",  "<<"Type of itr:"<<s1<<std::endl;
305
                                jobject xx = (iter->second)->getUnderlyingValue();
306
                                if(xx == nullptr) {
307
                                        std::cerr<<"value failed"<<std::endl;
308
                                } else {
309

    
310
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
311
                                }
312
                                if((iter->second)->getUnderlyingValue() == nullptr) {
313
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
314
                                }
315
#endif
316
                }
317
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
318
                                iter != properties.end(); ++iter, i++) {
319
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
320
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
321
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
322
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
323

    
324
                }
325
        }
326
        
327
                        SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
328
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()), 
329
                                        (sourceFile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourceFile) : nullptr), (outputFile.empty() ? nullptr : outputFile.c_str() ), stringArray, objectArray);
330

    
331
        if (size > 0) {
332
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
333
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
334
        }
335
        exception = proc->checkAndCreateException(cppClass);
336
                                
337
        }        
338
}
339
   
340
XdmNode * SchemaValidator::validateToNode(const char * sourceFile){
341
        /*if (sourceFile == nullptr) {
342
                std::cerr << "Error:: source file string cannot be empty or nullptr" << std::endl;
343
             return nullptr;
344
        }*/
345

    
346
jmethodID mID =
347
                (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(cppClass, "validateToNode",
348
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmNode;");
349
if (!mID) {
350
        std::cerr << "Error: libsaxon." << "validate" << " not found\n"
351
                        << std::endl;
352

    
353
} else {
354
        jobjectArray stringArray = nullptr;
355
        jobjectArray objectArray = nullptr;
356
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
357
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
358

    
359
        int size = parameters.size() + properties.size();
360
#ifdef DEBUG
361
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
362
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
363
                std::cerr<<"size:"<<size<<std::endl;
364
#endif
365
        if (size > 0) {
366
                objectArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
367
                                objectClass, 0);
368
                stringArray = SaxonProcessor::sxn_environ->env->NewObjectArray((jint) size,
369
                                stringClass, 0);
370
                int i = 0;
371
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
372
                                iter != parameters.end(); ++iter, i++) {
373
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
374
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
375
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
376
                                        (iter->second)->getUnderlyingValue());
377
#ifdef DEBUG
378
                                std::string s1 = typeid(iter->second).name();
379
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
380
                                jobject xx = (iter->second)->getUnderlyingValue();
381
                                if(xx == nullptr) {
382
                                        std::cerr<<"value failed"<<std::endl;
383
                                } else {
384

    
385
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
386
                                }
387
                                if((iter->second)->getUnderlyingValue() == nullptr) {
388
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is nullptr"<<std::endl;
389
                                }
390
#endif
391
                }
392
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
393
                                iter != properties.end(); ++iter, i++) {
394
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(stringArray, i,
395
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->first).c_str()));
396
                        SaxonProcessor::sxn_environ->env->SetObjectArrayElement(objectArray, i,
397
                                        SaxonProcessor::sxn_environ->env->NewStringUTF((iter->second).c_str()));
398
                }
399
        }
400
        jobject result = (jobject)(
401
                        SaxonProcessor::sxn_environ->env->CallObjectMethod(cppV, mID,
402
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
403
                                        (sourceFile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourceFile) : nullptr), stringArray, objectArray));
404
        if (size > 0) {
405
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
406
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
407
        }
408
        if (result) {
409
                XdmNode * node = new XdmNode(result);
410
                return node;
411
        }
412

    
413
        exception = proc->checkAndCreateException(cppClass);
414

    
415
}
416
        return nullptr;
417
}
418

    
419
void SchemaValidator::exceptionClear(){
420
 if(exception != nullptr) {
421
         delete exception;
422
         exception = nullptr;
423
 }
424
   SaxonProcessor::sxn_environ->env->ExceptionClear();
425
 }
426

    
427
/*const char * SchemaValidator::getErrorCode(int i) {
428
        if(proc->exception == nullptr) {return nullptr;}
429
        return proc->exception->getErrorCode(i);
430
}
431

432
const char * SchemaValidator::getErrorMessage(int i ){
433
 if(proc->exception == nullptr) {return nullptr;}
434
 return proc->exception->getErrorMessage(i);
435
 } */
436

    
437
bool SchemaValidator::exceptionOccurred() {
438
        return proc->exceptionOccurred() || exception != nullptr;
439
}
440

    
441
const char* SchemaValidator::checkException() {
442
        return proc->checkException();
443
}
444

    
445
/*int SchemaValidator::exceptionCount(){
446
 if(proc->exception != nullptr){
447
 return proc->exception->count();
448
 }
449
 return 0;
450
 }   */
451

    
452
void SchemaValidator::setSourceNode(XdmNode * value) {
453
    if(value != nullptr){
454
      value->incrementRefCount();
455
      parameters["node"] = (XdmValue *)value;
456
    }
457

    
458
        
459
}
460

    
461
void SchemaValidator::setParameter(const char * name, XdmValue*value) {
462
        if(value != nullptr){
463
                value->incrementRefCount();
464
                parameters["param:"+std::string(name)] = value;
465
        }
466
}
467

    
468
bool SchemaValidator::removeParameter(const char * name) {
469
        return (bool)(parameters.erase("param:"+std::string(name)));
470
}
471

    
472
void SchemaValidator::setProperty(const char * name, const char * value) {
473
#ifdef DEBUG        
474
        if(value == nullptr) {
475
                std::cerr<<"Validator setProperty is nullptr"<<std::endl;
476
        }
477
#endif
478
        properties.insert(std::pair<std::string, std::string>(std::string(name), std::string((value== nullptr ? "" : value))));
479
}
480

    
481
void SchemaValidator::clearParameters(bool delVal) {
482
        if(delVal){
483
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
484
                        XdmValue * value = itr->second;
485
                        value->decrementRefCount();
486
#ifdef DEBUG
487
                        std::cout<<"SchemaValidator.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
488
#endif
489
                        if(value != nullptr && value->getRefCount() < 1){
490
                                delete value;
491
                        }
492
                }
493
                parameters.clear();
494
        }
495
}
496

    
497
void SchemaValidator::clearProperties() {
498
        properties.clear();
499
}
500

    
501
std::map<std::string,XdmValue*>& SchemaValidator::getParameters(){
502
        std::map<std::string,XdmValue*>& ptr = parameters;
503
        return ptr;
504
}
505

    
506
std::map<std::string,std::string>& SchemaValidator::getProperties(){
507
        std::map<std::string,std::string> &ptr = properties;
508
        return ptr;
509
}
510

    
(16-16/51)