Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / SchemaValidator.cpp @ 0608038f

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(environ->env,
19
                        "net/sf/saxon/option/cpp/SchemaValidatorForCpp");
20
        if ((proc->proc) == NULL) {
21
                std::cerr << "Processor is NULL" << std::endl;
22
        }
23

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

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

    
32
        proc->exception = NULL;
33
        if(!(proc->cwd.empty()) && curr.empty()){
34
                cwdV = proc->cwd;
35
        } else {
36
                cwdV = curr;
37
        }
38
        if(exceptionOccurred()) {
39
                                if(proc->exception != NULL) {
40
                                        delete proc->exception;
41
                                }
42
                                proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
43
                                
44
                             
45
                        
46
#ifdef DEBUG
47
                environ->env->ExceptionDescribe();
48
#endif
49
                proc->exceptionClear();
50
        }
51

    
52
}
53

    
54
   void SchemaValidator::setcwd(const char* dir){
55
        if(dir==NULL) {
56
                    cwdV = std::string(dir);
57
        }
58
   }
59

    
60
  void SchemaValidator::setOutputFile(const char * sourceFile){
61
        outputFile = std::string(sourceFile);
62
  }
63

    
64
  XdmNode * SchemaValidator::getValidationReport(){
65
        jmethodID mID =
66
                (jmethodID) environ->env->GetMethodID(cppClass, "getValidationReport", "()Lnet/sf/saxon/s9api/XdmNode;");
67
        if (!mID) {
68
                std::cerr << "Error: libsaxon." << "validate.getValidationReport()" << " not found\n"
69
                        << std::endl;
70
        } else {
71
                jobject result = (jobject)(
72
                        environ->env->CallObjectMethod(cppV, mID));
73
                
74
                if (result) {
75
                        XdmNode * node = new XdmNode(result);
76
                        node->setProcessor(proc);
77
                        return node;
78
                }
79
        }
80
        return NULL;
81
}
82

    
83
  void SchemaValidator::registerSchemaFromFile(const char * sourceFile){
84
        if (sourceFile == NULL) {
85
                std::cerr << "Error:: sourceFile string cannot be empty or NULL" << std::endl;
86
             return;
87
        }
88
        
89
        jmethodID mID =
90
                (jmethodID) environ->env->GetMethodID(cppClass, "registerSchema",
91
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
92
        if (!mID) {
93
                std::cerr << "Error: libsaxon." << "validate" << " not found\n"
94
                        << std::endl;
95
        } else {
96
        jobjectArray stringArray = NULL;
97
        jobjectArray objectArray = NULL;
98
        jclass objectClass = lookForClass(environ->env, "java/lang/Object");
99
        jclass stringClass = lookForClass(environ->env, "java/lang/String");
100

    
101
        int size = parameters.size() + properties.size();
102
#ifdef DEBUG
103
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
104
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
105
                std::cerr<<"size:"<<size<<std::endl;
106
#endif
107
        if (size > 0) {
108
                objectArray = environ->env->NewObjectArray((jint) size,
109
                                objectClass, 0);
110
                stringArray = environ->env->NewObjectArray((jint) size,
111
                                stringClass, 0);
112
                int i = 0;
113
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
114
                                iter != parameters.end(); ++iter, i++) {
115
                        environ->env->SetObjectArrayElement(stringArray, i,
116
                                        environ->env->NewStringUTF((iter->first).c_str()));
117
                        environ->env->SetObjectArrayElement(objectArray, i,
118
                                        (iter->second)->getUnderlyingValue(proc));
119
#ifdef DEBUG
120
                                std::string s1 = typeid(iter->second).name();
121
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
122
                                jobject xx = (iter->second)->getUnderlyingValue(proc);
123
                                if(xx == NULL) {
124
                                        std::cerr<<"value failed"<<std::endl;
125
                                } else {
126

    
127
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
128
                                }
129
                                if((iter->second)->getUnderlyingValue(proc) == NULL) {
130
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
131
                                }
132
#endif
133
                }
134
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
135
                                iter != properties.end(); ++iter, i++) {
136
                        environ->env->SetObjectArrayElement(stringArray, i,
137
                                        environ->env->NewStringUTF((iter->first).c_str()));
138
                        environ->env->SetObjectArrayElement(objectArray, i,
139
                                        environ->env->NewStringUTF((iter->second).c_str()));
140
                }
141
        }
142
        
143
                        environ->env->CallVoidMethod(cppV, mID,
144
                                        environ->env->NewStringUTF(cwdV.c_str()),
145
                                        environ->env->NewStringUTF(sourceFile), stringArray, objectArray);
146

    
147
        if (size > 0) {
148
                environ->env->DeleteLocalRef(stringArray);
149
                environ->env->DeleteLocalRef(objectArray);
150
        }
151

    
152
}
153
                if(exceptionOccurred()) {
154
                                if(proc->exception != NULL) {
155
                                        delete proc->exception;
156
                                }
157
                                proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
158
                                
159
                             
160
                        
161
#ifdef DEBUG
162
                environ->env->ExceptionDescribe();
163
#endif
164
                proc->exceptionClear();
165
        }
166
 }
167

    
168
  void SchemaValidator::registerSchemaFromString(const char * sourceStr){
169
        setProperty("resources", proc->getResourcesDirectory());
170
        
171
        if (sourceStr == NULL) {
172
                std::cerr << "Error:: Schema string cannot be empty or NULL" << std::endl;
173
             return;
174
        }
175
        jmethodID mID =
176
                (jmethodID) environ->env->GetMethodID(cppClass, "registerSchemaString",
177
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
178

    
179
        if (!mID) {
180
                std::cerr << "Error: libsaxon." << "registerSchemaString" << " not found\n"
181
                        << std::endl;
182
        } else {
183
        jobjectArray stringArray = NULL;
184
        jobjectArray objectArray = NULL;
185
        jclass objectClass = lookForClass(environ->env, "java/lang/Object");
186
        jclass stringClass = lookForClass(environ->env, "java/lang/String");
187

    
188
        int size = parameters.size() + properties.size();
189
#ifdef DEBUG
190
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
191
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
192
                std::cerr<<"size:"<<size<<std::endl;
193
#endif
194

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

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

    
231
        
232
                        environ->env->CallVoidMethod(cppV, mID,
233
                                        environ->env->NewStringUTF(cwdV.c_str()),
234
                                        (sourceStr != NULL ? environ->env->NewStringUTF(sourceStr) : NULL), NULL, stringArray, objectArray);
235

    
236
        if (size > 0) {
237
                environ->env->DeleteLocalRef(stringArray);
238
                environ->env->DeleteLocalRef(objectArray);
239
        }
240

    
241
}
242

    
243
                if(exceptionOccurred()) {
244

    
245
                                if(proc->exception != NULL) {
246
                                        delete proc->exception;
247
                                }
248
                                proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
249
                                
250
                             
251
#ifdef DEBUG
252
                        environ->env->ExceptionDescribe();
253
#endif
254
                        proc->exceptionClear();
255
                }
256

    
257
        
258
}
259

    
260
  void SchemaValidator::validate(const char * sourceFile){
261
        /*if (sourceFile == NULL) {
262
                std::cerr << "Error:: sourceFile string cannot be empty or NULL" << std::endl;
263
             return;
264
        }*/
265
setProperty("resources", proc->getResourcesDirectory());
266
jmethodID mID =
267
                (jmethodID) environ->env->GetMethodID(cppClass, "validate",
268
                                "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
269
if (!mID) {
270
        std::cerr << "Error: libsaxon." << "validate" << " not found\n"
271
                        << std::endl;
272

    
273
} else {
274
        jobjectArray stringArray = NULL;
275
        jobjectArray objectArray = NULL;
276
        jclass objectClass = lookForClass(environ->env, "java/lang/Object");
277
        jclass stringClass = lookForClass(environ->env, "java/lang/String");
278

    
279
        int size = parameters.size() + properties.size();
280
#ifdef DEBUG
281
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
282
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
283
                std::cerr<<"size:"<<size<<std::endl;
284
#endif
285
        if (size > 0) {
286
                objectArray = environ->env->NewObjectArray((jint) size,
287
                                objectClass, 0);
288
                stringArray = environ->env->NewObjectArray((jint) size,
289
                                stringClass, 0);
290
                int i = 0;
291
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
292
                                iter != parameters.end(); ++iter, i++) {
293
                        environ->env->SetObjectArrayElement(stringArray, i,
294
                                        environ->env->NewStringUTF((iter->first).c_str()));
295
                        environ->env->SetObjectArrayElement(objectArray, i,
296
                                        (iter->second)->getUnderlyingValue(proc));
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(proc);
302
                                if(xx == NULL) {
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(proc) == NULL) {
309
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<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
                        environ->env->SetObjectArrayElement(stringArray, i,
316
                                        environ->env->NewStringUTF((iter->first).c_str()));
317
                        environ->env->SetObjectArrayElement(objectArray, i,
318
                                        environ->env->NewStringUTF((iter->second).c_str()));
319

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

    
327
        if (size > 0) {
328
                environ->env->DeleteLocalRef(stringArray);
329
                environ->env->DeleteLocalRef(objectArray);
330
        }
331
                if(exceptionOccurred()) {
332
                        if(proc->exception != NULL) {
333
                                delete proc->exception;
334
                        }
335
                                proc->exception = proc->checkForExceptionCPP(environ->env, cppClass, NULL);
336
                                
337
                             
338

    
339

    
340
#ifdef DEBUG
341
        environ->env->ExceptionDescribe();
342
#endif
343
                        proc->exceptionClear();
344
                }
345
                }        
346
}
347
   
348
XdmNode * SchemaValidator::validateToNode(const char * sourceFile){
349
        if (sourceFile == NULL) {
350
                std::cerr << "Error:: source file string cannot be empty or NULL" << std::endl;
351
             return NULL;
352
        }
353
setProperty("resources", proc->getResourcesDirectory());
354
jmethodID mID =
355
                (jmethodID) environ->env->GetMethodID(cppClass, "validateToNode",
356
                                "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Lnet/sf/saxon/s9api/XdmNode;");
357
if (!mID) {
358
        std::cerr << "Error: libsaxon." << "validate" << " not found\n"
359
                        << std::endl;
360

    
361
} else {
362
        jobjectArray stringArray = NULL;
363
        jobjectArray objectArray = NULL;
364
        jclass objectClass = lookForClass(environ->env, "java/lang/Object");
365
        jclass stringClass = lookForClass(environ->env, "java/lang/String");
366

    
367
        int size = parameters.size() + properties.size();
368
#ifdef DEBUG
369
                std::cerr<<"Properties size: "<<properties.size()<<std::endl;
370
                std::cerr<<"Parameter size: "<<parameters.size()<<std::endl;
371
                std::cerr<<"size:"<<size<<std::endl;
372
#endif
373
        if (size > 0) {
374
                objectArray = environ->env->NewObjectArray((jint) size,
375
                                objectClass, 0);
376
                stringArray = environ->env->NewObjectArray((jint) size,
377
                                stringClass, 0);
378
                int i = 0;
379
                for (std::map<std::string, XdmValue*>::iterator iter = parameters.begin();
380
                                iter != parameters.end(); ++iter, i++) {
381
                        environ->env->SetObjectArrayElement(stringArray, i,
382
                                        environ->env->NewStringUTF((iter->first).c_str()));
383
                        environ->env->SetObjectArrayElement(objectArray, i,
384
                                        (iter->second)->getUnderlyingValue(proc));
385
#ifdef DEBUG
386
                                std::string s1 = typeid(iter->second).name();
387
                                std::cerr<<"Type of itr:"<<s1<<std::endl;
388
                                jobject xx = (iter->second)->getUnderlyingValue(proc);
389
                                if(xx == NULL) {
390
                                        std::cerr<<"value failed"<<std::endl;
391
                                } else {
392

    
393
                                        std::cerr<<"Type of value:"<<(typeid(xx).name())<<std::endl;
394
                                }
395
                                if((iter->second)->getUnderlyingValue(proc) == NULL) {
396
                                        std::cerr<<"(iter->second)->getUnderlyingValue() is NULL"<<std::endl;
397
                                }
398
#endif
399
                }
400
                for (std::map<std::string, std::string>::iterator iter = properties.begin();
401
                                iter != properties.end(); ++iter, i++) {
402
                        environ->env->SetObjectArrayElement(stringArray, i,
403
                                        environ->env->NewStringUTF((iter->first).c_str()));
404
                        environ->env->SetObjectArrayElement(objectArray, i,
405
                                        environ->env->NewStringUTF((iter->second).c_str()));
406
                }
407
        }
408
        jobject result = (jobject)(
409
                        environ->env->CallObjectMethod(cppV, mID,
410
                                        environ->env->NewStringUTF(cwdV.c_str()),
411
                                        environ->env->NewStringUTF(sourceFile), stringArray, objectArray));
412
        if (size > 0) {
413
                environ->env->DeleteLocalRef(stringArray);
414
                environ->env->DeleteLocalRef(objectArray);
415
        }
416
        if (result) {
417
                XdmNode * node = new XdmNode(result);
418
                node->setProcessor(proc);
419
                return node;
420
        }
421

    
422
}
423
#ifdef DEBUG
424
        environ->env->ExceptionDescribe();
425
#endif
426
environ->env->ExceptionClear();
427

    
428
}
429

    
430
void SchemaValidator::exceptionClear(){
431
 if(proc->exception != NULL) {
432
         delete proc->exception;
433
         proc->exception = NULL;
434
 }
435
   environ->env->ExceptionClear();
436
 }
437

    
438
const char * SchemaValidator::getErrorCode(int i) {
439
        if(proc->exception == NULL) {return NULL;}
440
        return proc->exception->getErrorCode(i);
441
}
442

    
443
const char * SchemaValidator::getErrorMessage(int i ){
444
 if(proc->exception == NULL) {return NULL;}
445
 return proc->exception->getErrorMessage(i);
446
 }
447

    
448
bool SchemaValidator::exceptionOccurred() {
449
        return proc->exceptionOccurred() || proc->exception != NULL;
450
}
451

    
452
const char* SchemaValidator::checkException() {
453
        /*if(proc->exception == NULL) {
454
         proc->exception = proc->checkForException(environ, cppClass, cpp);
455
         }
456
         return proc->exception;*/
457
        return checkForException(*(environ), cppClass, cppV);
458
}
459

    
460
int SchemaValidator::exceptionCount(){
461
 if(proc->exception != NULL){
462
 return proc->exception->count();
463
 }
464
 return 0;
465
 }
466

    
467
void SchemaValidator::setSourceNode(XdmNode * value) {
468
    if(value != NULL){
469
      value->incrementRefCount();
470
      parameters["node"] = (XdmValue *)value;
471
    }
472

    
473
        
474
}
475

    
476
void SchemaValidator::setParameter(const char * name, XdmValue*value) {
477
        if(value != NULL){
478
                value->incrementRefCount();
479
                parameters["param:"+std::string(name)] = value;
480
        }
481
}
482

    
483
bool SchemaValidator::removeParameter(const char * name) {
484
        return (bool)(parameters.erase("param:"+std::string(name)));
485
}
486

    
487
void SchemaValidator::setProperty(const char * name, const char * value) {
488
#ifdef DEBUG        
489
        if(value == NULL) {
490
                std::cerr<<"Validator setProperty is NULL"<<std::endl;
491
        }
492
#endif
493
        properties.insert(std::pair<std::string, std::string>(std::string(name), std::string((value== NULL ? "" : value))));
494
}
495

    
496
void SchemaValidator::clearParameters(bool delVal) {
497
        if(delVal){
498
                       for(std::map<std::string, XdmValue*>::iterator itr = parameters.begin(); itr != parameters.end(); itr++){
499
                        XdmValue * value = itr->second;
500
                        value->decrementRefCount();
501
#ifdef DEBUG
502
                        std::cout<<"SchemaValidator.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
503
#endif
504
                        if(value != NULL && value->getRefCount() < 1){                
505
                                delete value;
506
                        }
507
                }
508
                parameters.clear();
509
        }
510
}
511

    
512
void SchemaValidator::clearProperties() {
513
        properties.clear();
514
}
515

    
516
std::map<std::string,XdmValue*>& SchemaValidator::getParameters(){
517
        std::map<std::string,XdmValue*>& ptr = parameters;
518
        return ptr;
519
}
520

    
521
std::map<std::string,std::string>& SchemaValidator::getProperties(){
522
        std::map<std::string,std::string> &ptr = properties;
523
        return ptr;
524
}
525

    
(10-10/37)