Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / SchemaValidator.cpp @ 6304fb8b

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
                        node->setProcessor(proc);
65
                        return node;
66
                }
67
                exception = proc->checkAndCreateException(cppClass);
68
        }
69
        return nullptr;
70
}
71

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

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

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

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

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

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

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

    
161

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

    
166

    
167

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

    
171
 }
172

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
411
        exception = proc->checkAndCreateException(cppClass);
412

    
413
}
414
        return nullptr;
415
}
416

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

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

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

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

    
439
const char* SchemaValidator::checkException() {
440
        return proc->checkException(cppV);
441
}
442

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

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

    
456
        
457
}
458

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

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

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

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

    
495
void SchemaValidator::clearProperties() {
496
        properties.clear();
497
}
498

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

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

    
(14-14/49)