Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SchemaValidator.cpp @ 4ee4355d

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

    
6
//#define DEBUG
7

    
8
SchemaValidator::SchemaValidator() {
9
        SaxonProcessor *p = new SaxonProcessor(true);
10
        SchemaValidator(p, "");
11
}
12

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

    
24
        cppV = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
25
                        "(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
26

    
27
#ifdef DEBUG
28
        jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
29
        SaxonProcessor::sxn_environ->env->CallStaticVoidMethod(cppClass, debugMID, (jboolean)true);
30
#endif    
31

    
32
        
33
        if(!(proc->cwd.empty()) && curr.empty()){
34
                cwdV = proc->cwd;
35
        } else {
36
                cwdV = curr;
37
        }
38
        exception = proc->checkAndCreateException(cppClass);
39

    
40
}
41

    
42
   void SchemaValidator::setcwd(const char* dir){
43
        if(dir==nullptr) {
44
                    cwdV = std::string(dir);
45
        }
46
   }
47

    
48
  void SchemaValidator::setOutputFile(const char * sourceFile){
49
        outputFile = std::string(sourceFile);
50
  }
51

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

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

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

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

    
135
        if (size > 0) {
136
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
137
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
138
        }
139

    
140
}
141
        exception = proc->checkAndCreateException(cppClass);
142
                     
143
        
144
 }
145

    
146
 void SchemaValidator::exportSchema(const char * fileName) {
147
      if (fileName == nullptr) {
148
                      std::cerr << "Error:: fileName string cannot be empty or nullptr" << std::endl;
149
                   return;
150
              }
151

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

    
160

    
161
                              SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
162
                                              SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
163
                                              SaxonProcessor::sxn_environ->env->NewStringUTF(fileName));
164

    
165

    
166

    
167
      }
168
      exception = proc->checkAndCreateException(cppClass);
169

    
170
 }
171

    
172
  void SchemaValidator::registerSchemaFromString(const char * sourceStr){
173

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

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

    
192
        int size = parameters.size() + properties.size();
193
#ifdef DEBUG
194
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
195
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
196
                std::cerr<<"size:"<<size<<std::endl;
197
#endif
198

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

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

    
235
        
236
                        SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
237
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()),
238
                                        (sourceStr != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourceStr) : nullptr), nullptr, stringArray, objectArray);
239

    
240
        if (size > 0) {
241
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(stringArray);
242
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(objectArray);
243
        }
244

    
245
    }
246
        exception = proc->checkAndCreateException(cppClass);
247
    
248
}
249

    
250
  void SchemaValidator::validate(const char * sourceFile){
251
        /*if (sourceFile == nullptr) {
252
                std::cerr << "Error:: sourceFile string cannot be empty or nullptr" << std::endl;
253
             return;
254
        }*/
255

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

    
263
} else {
264
        jobjectArray stringArray = nullptr;
265
        jobjectArray objectArray = nullptr;
266
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
267
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
268

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

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

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

    
320
                }
321
        }
322
        
323
                        SaxonProcessor::sxn_environ->env->CallVoidMethod(cppV, mID,
324
                                        SaxonProcessor::sxn_environ->env->NewStringUTF(cwdV.c_str()), 
325
                                        (sourceFile != nullptr ? SaxonProcessor::sxn_environ->env->NewStringUTF(sourceFile) : nullptr), (outputFile.empty() ? nullptr : outputFile.c_str() ), stringArray, objectArray);
326

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

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

    
349
} else {
350
        jobjectArray stringArray = nullptr;
351
        jobjectArray objectArray = nullptr;
352
        jclass objectClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/Object");
353
        jclass stringClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/lang/String");
354

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

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

    
409
        exception = proc->checkAndCreateException(cppClass);
410

    
411
}
412
        return nullptr;
413
}
414

    
415
void SchemaValidator::exceptionClear(){
416
 if(exception != nullptr) {
417
         delete exception;
418
         exception = nullptr;
419
 }
420
   SaxonProcessor::sxn_environ->env->ExceptionClear();
421
 }
422

    
423
/*const char * SchemaValidator::getErrorCode(int i) {
424
        if(proc->exception == nullptr) {return nullptr;}
425
        return proc->exception->getErrorCode(i);
426
}
427

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

    
433
bool SchemaValidator::exceptionOccurred() {
434
        return proc->exceptionOccurred() || exception != nullptr;
435
}
436

    
437
const char* SchemaValidator::checkException() {
438
        return proc->checkException();
439
}
440

    
441
/*int SchemaValidator::exceptionCount(){
442
 if(proc->exception != nullptr){
443
 return proc->exception->count();
444
 }
445
 return 0;
446
 }   */
447

    
448
void SchemaValidator::setSourceNode(XdmNode * value) {
449
    if(value != nullptr){
450
      value->incrementRefCount();
451
      parameters["node"] = (XdmValue *)value;
452
    }
453

    
454
        
455
}
456

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

    
464
bool SchemaValidator::removeParameter(const char * name) {
465
        return (bool)(parameters.erase("param:"+std::string(name)));
466
}
467

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

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

    
493
void SchemaValidator::clearProperties() {
494
        properties.clear();
495
}
496

    
497
std::map<std::string,XdmValue*>& SchemaValidator::getParameters(){
498
        std::map<std::string,XdmValue*>& ptr = parameters;
499
        return ptr;
500
}
501

    
502
std::map<std::string,std::string>& SchemaValidator::getProperties(){
503
        std::map<std::string,std::string> &ptr = properties;
504
        return ptr;
505
}
506

    
(15-15/55)