Project

Profile

Help

Revision 6304fb8b

Added by O'Neil Delpratt 5 months ago

Saxon/C bug fixes and development work

View differences:

latest10/hec/Saxon.C.API/SaxonProcessor.h
7 7

  
8 8
#ifndef SAXON_PROCESSOR_H
9 9
#define SAXON_PROCESSOR_H
10
	
10

  
11 11
#if defined __linux__ || defined __APPLE__
12
        #include <stdlib.h>
13
        #include <string.h>
14
        #include <dlfcn.h>
15 12

  
16
        #define HANDLE void*
17
        #define LoadLibrary(x) dlopen(x, RTLD_LAZY)
18
        #define GetProcAddress(x,y) dlsym(x,y)
13
#include <stdlib.h>
14
#include <string>
15
#include <dlfcn.h>
16

  
17
#define HANDLE void*
18
#define LoadLibrary(x) dlopen(x, RTLD_LAZY)
19
#define GetProcAddress(x, y) dlsym(x,y)
19 20
#else
20
    #include <windows.h>
21
#include <windows.h>
21 22
#endif
22 23

  
23 24
//#define DEBUG //remove
24 25
#define CVERSION "1.3.0"
25 26
#define CVERSION_API_NO 130
27

  
26 28
#include <string>
27 29
#include <iostream>
28
#include <sstream>  
29
#include <map>	
30
#include <sstream>
31
#include <map>
30 32
#include <vector>
31 33
#include <stdexcept>      // std::logic_error
32 34

  
......
42 44
//#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
43 45

  
44 46
class XsltProcessor;
47

  
45 48
class Xslt30Processor;
49

  
46 50
class XQueryProcessor;
51

  
47 52
class XPathProcessor;
53

  
48 54
class SchemaValidator;
55

  
49 56
class XdmValue;
57

  
50 58
class XdmNode;
59

  
51 60
class XdmItem;
61

  
52 62
class XdmAtomicValue;
53 63

  
54
#if CVERSION_API_NO >= 123
55 64
class XdmFunctionItem;
65

  
56 66
class XdmArray;
67

  
57 68
class XdmMap;
58
#endif
59 69

  
70
class XsltExecutable;
60 71

  
61 72

  
62 73
// The Saxon XSLT interface class
63 74

  
64 75
//std::mutex mtx;
65
/*! <code>MyException</code>. This struct captures details of the Java exception thrown from Saxon s9api API (Java).
66
 * <p/>
67
 */
76

  
77

  
68 78
typedef struct {
69
		std::string errorCode;
70
		std::string errorMessage;
71
		int linenumber;
72
	    	bool isType;
73
	    	bool isStatic;
74
	    	bool isGlobal;
75
	}MyException;
76

  
77
typedef struct
78
{
79 79
    jobjectArray stringArray;
80 80
    jobjectArray objectArray;
81 81

  
82
}JParameters;
82
} JParameters;
83 83

  
84 84

  
85 85

  
......
90 90
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
91 91
 */
92 92
class SaxonProcessor {
93
friend class XsltProcessor;
94
friend class Xslt30Processor;
95
friend class XQueryProcessor;
96
friend class SchemaValidator;
97
friend class XPathProcessor;
98
friend class XdmValue;
99
friend class XdmAtomicValue;
93

  
94
    friend class XsltProcessor;
95

  
96
    friend class Xslt30Processor;
97

  
98
    friend class XsltExecutable;
99

  
100
    friend class XQueryProcessor;
101

  
102
    friend class SchemaValidator;
103

  
104
    friend class XPathProcessor;
105

  
106
    friend class XdmValue;
107

  
108
    friend class XdmAtomicValue;
100 109

  
101 110
public:
102 111

  
103
   //! A default constructor.
112
    //! A default constructor.
104 113
    /*!
105 114
      * Create Saxon Processor.
106 115
    */
107 116

  
108 117
    SaxonProcessor();
109 118

  
110
   //! constructor based upon a Saxon configuration file.
119
    //! constructor based upon a Saxon configuration file.
111 120
    /*!
112 121
      * Create Saxon Processor.
113 122
    */
114 123

  
115
    SaxonProcessor(const char * configFile);
124
    SaxonProcessor(const char *configFile);
116 125

  
117 126

  
118
   //! A constructor.
127
    //! A constructor.
119 128
    /*!
120 129
      * Create Saxon Processor.
121 130
      * @param l - Flag that a license is to be used. Default is false.	
122 131
    */
123 132
    SaxonProcessor(bool l);
124 133

  
125
    SaxonProcessor& operator=( const SaxonProcessor& other );
134
    SaxonProcessor &operator=(const SaxonProcessor &other);
126 135

  
127 136

  
128
	/**
129
	 * Xslt30Processor copy constructor.
130
	 * @param other - Xslt30Processor
131
	 */
137
    /**
138
     * Xslt30Processor copy constructor.
139
     * @param other - Xslt30Processor
140
     */
132 141
    SaxonProcessor(const SaxonProcessor &other);
133 142

  
134
   /*!
143
    /*!
135 144

  
136
      * Destructor
137
    */
145
       * Destructor
146
     */
138 147
    ~SaxonProcessor();
139 148

  
140 149

  
141
   //! Get the Processor object. Method used in Python
142
   /* SaxonProcessor * getProcessor(){
143
	return this;
144
    }*/
145
	
146
   /*!
150
    //! Get the Processor object. Method used in Python
151
    /* SaxonProcessor * getProcessor(){
152
     return this;
153
     }*/
154

  
155
    /*!
147 156

  
148
      * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
149
      * @return a newly created XsltProcessor	
150
    */	
151
    XsltProcessor * newXsltProcessor();
157
       * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
158
       * @return a newly created XsltProcessor
159
     */
160
    XsltProcessor *newXsltProcessor();
152 161

  
153
   /*!
162
    /*!
154 163

  
155
      * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
156
      * @return a newly created Xslt30Processor	
157
    */	
158
    Xslt30Processor * newXslt30Processor();
164
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
165
       * @return a newly created Xslt30Processor
166
     */
167
    Xslt30Processor *newXslt30Processor();
159 168

  
160 169

  
161 170
    /*!
......
163 172
     *
164 173
     * @return a newly created XQueryProcessor
165 174
     */
166
    XQueryProcessor * newXQueryProcessor();
175
    XQueryProcessor *newXQueryProcessor();
167 176

  
168 177

  
169 178
    /*!
......
171 180
     *
172 181
     * @return a newly created XPathProcessor
173 182
     */
174
    XPathProcessor * newXPathProcessor();
183
    XPathProcessor *newXPathProcessor();
175 184

  
176 185
    /*!
177 186
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
......
179 188
     *
180 189
     * @return a new SchemaValidator
181 190
     */
182
    SchemaValidator * newSchemaValidator();
191
    SchemaValidator *newSchemaValidator();
183 192

  
184 193

  
185 194
    /*!
186 195
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
187 196
     * of a zero-length string (and potentially other strings, in future)
188 197
     *
189
     * @param value the String value. Null is taken as equivalent to "".
198
     * @param value the String value. nullptr is taken as equivalent to "".
190 199
     * @return the corresponding StringValue
191 200
     */
192
    XdmAtomicValue * makeStringValue(std::string str);
201
    XdmAtomicValue *makeStringValue(std::string str);
193 202

  
194 203
    /*!
195 204
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
196 205
     * of a zero-length string (and potentially other strings, in future)
197 206
     *
198
     * @param value the char pointer array. Null is taken as equivalent to "".
207
     * @param value the char pointer array. nullptr is taken as equivalent to "".
199 208
     * @return the corresponding StringValue
200 209
     */
201
    XdmAtomicValue * makeStringValue(const char * str);
210
    XdmAtomicValue *makeStringValue(const char *str);
202 211

  
203 212
    /*!
204 213
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
......
206 215
     * @param i the supplied primitive integer value
207 216
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
208 217
     */
209
    XdmAtomicValue * makeIntegerValue(int i);
218
    XdmAtomicValue *makeIntegerValue(int i);
210 219

  
211 220

  
212 221
    /*!
......
215 224
     * @param d the value of the double
216 225
     * @return a new XdmAtomicValue
217 226
     */
218
    XdmAtomicValue * makeDoubleValue(double d);
227
    XdmAtomicValue *makeDoubleValue(double d);
219 228

  
220 229
    /*!
221 230
     * Factory method (for convenience in compiled bytecode)
......
223 232
     * @param f the value of the foat
224 233
     * @return a new XdmAtomicValue
225 234
     */
226
    XdmAtomicValue * makeFloatValue(float);
235
    XdmAtomicValue *makeFloatValue(float);
227 236

  
228 237
    /*!
229 238
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
......
231 240
     * @param l the supplied primitive long value
232 241
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
233 242
     */
234
    XdmAtomicValue * makeLongValue(long l);
243
    XdmAtomicValue *makeLongValue(long l);
235 244

  
236 245
    /*!
237 246
     * Factory method: makes a XdmAtomicValue representing a boolean Value
......
240 249
     *              required
241 250
     * @return the XdmAtomicValue requested
242 251
     */
243
    XdmAtomicValue * makeBooleanValue(bool b);
252
    XdmAtomicValue *makeBooleanValue(bool b);
244 253

  
245 254
    /**
246 255
     * Create an QName Xdm value from string representation in clark notation
247 256
     * @param str - The value given in a string form in clark notation. {uri}local
248 257
     * @return XdmAtomicValue - value
249 258
    */
250
    XdmAtomicValue * makeQNameValue(const char * str);
259
    XdmAtomicValue *makeQNameValue(const char *str);
251 260

  
252 261
    /*!
253 262
     * Create an Xdm Atomic value from string representation
......
256 265
     * In the case of a QName the value supplied must be in clark notation. {uri}local
257 266
     * @return XdmValue - value
258 267
    */
259
    XdmAtomicValue * makeAtomicValue(const char * type, const char * value);
268
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
260 269

  
261 270
#if CVERSION_API_NO >= 123
271

  
262 272
    /**
263 273
        * Make an XdmArray whose members are from string representation
264 274
        * @param input the input array of booleans
265 275
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
266 276
   */
267
    XdmArray * makeArray(const char ** input, int length);
277
    XdmArray *makeArray(const char **input, int length);
268 278

  
269 279

  
270 280
    /**
......
272 282
        * @param input the input array of booleans
273 283
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
274 284
   */
275
    XdmArray * makeArray(short * input, int length);
276

  
285
    XdmArray *makeArray(short *input, int length);
277 286

  
278 287

  
279 288
    /**
......
281 290
        * @param input the input array of booleans
282 291
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
283 292
   */
284
    XdmArray * makeArray(int * input, int length);
293
    XdmArray *makeArray(int *input, int length);
285 294

  
286 295
    /**
287 296
        * Make an XdmArray whose members are xs:long values
288 297
        * @param input the input array of booleans
289 298
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
290 299
   */
291
    XdmArray * makeArray(long * input, int length);
300
    XdmArray *makeArray(long *input, int length);
292 301

  
293 302
    /**
294 303
        * Make an XdmArray whose members are xs:boolean values
295 304
        * @param input the input array of booleans
296 305
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
297 306
   */
298
    XdmArray * makeArray(bool * input, int length);
307
    XdmArray *makeArray(bool *input, int length);
299 308

  
300 309

  
301
    XdmMap * makeMap(std::map<XdmAtomicValue *, XdmValue*> dataMap);
302
    
310
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
311

  
303 312

  
304 313
#endif
305 314

  
306
     /**
307
     * Get the string representation of the XdmValue.
308
     * @return char array
309
     */
310
    const char * getStringValue(XdmItem * item);
315
    /**
316
    * Get the string representation of the XdmValue.
317
    * @return char array
318
    */
319
    const char *getStringValue(XdmItem *item);
311 320

  
312 321
    /**
313 322
     * Parse a lexical representation of the source document and return it as an XdmNode
314 323
    */
315
    XdmNode * parseXmlFromString(const char* source);
324
    XdmNode *parseXmlFromString(const char *source);
316 325

  
317 326
    /**
318 327
     * Parse a source document file and return it as an XdmNode.
319 328
    */
320
    XdmNode * parseXmlFromFile(const char* source);
329
    XdmNode *parseXmlFromFile(const char *source);
321 330

  
322 331
    /**
323 332
     * Parse a source document available by URI and return it as an XdmNode.
324 333
    */
325
    XdmNode * parseXmlFromUri(const char* source);
334
    XdmNode *parseXmlFromUri(const char *source);
326 335

  
327 336
    int getNodeKind(jobject);
328 337

  
329 338
    bool isSchemaAwareProcessor();
330 339

  
331
 
332 340

  
333 341
    /**
334 342
     * Checks for thrown exceptions
......
349 357
     * @param callingObject
350 358
     * @return SaxonApiException
351 359
    */
352
    SaxonApiException * checkForExceptionCPP(JNIEnv* env, jclass callingClass,  jobject callingObject);
360
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
353 361

  
354 362

  
355 363
    /*
......
361 369
    /**
362 370
     * set the current working directory
363 371
    */
364
   void setcwd(const char* cwd);
372
    void setcwd(const char *cwd);
365 373

  
366 374
    /**
367 375
     * get the current working directory
368 376
    */
369
   const char* getcwd();
377
    const char *getcwd();
370 378

  
371 379

  
372 380
    /**
373 381
     * set saxon resources directory
382
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
374 383
    */
375
   void setResourcesDirectory(const char* dir);
376
	
384
    void setResourcesDirectory(const char *dir){}
385

  
377 386
    /**
378 387
     * set catalog to be used in Saxon
379 388
    */
380
   void setCatalog(const char* catalogFile, bool isTracing);
389
    void setCatalog(const char *catalogFile, bool isTracing);
381 390

  
382 391
    /**
383 392
     * get saxon resources directory
393
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
384 394
    */
385
   const char * getResourcesDirectory();
395
    const char * getResourcesDirectory(){}
386 396

  
387 397
    /**
388 398
     * Set a configuration property specific to the processor in use. 
......
391 401
     * @param name of the property
392 402
     * @param value of the property
393 403
     */
394
    void setConfigurationProperty(const char * name, const char * value);
404
    void setConfigurationProperty(const char *name, const char *value);
395 405

  
396 406
    /**
397 407
     * Clear configuration properties specific to the processor in use. 
398 408
     */
399
     void clearConfigurationProperties();
409
    void clearConfigurationProperties();
400 410

  
401 411

  
402 412
    /**
403 413
     * Get the Saxon version
404 414
     * @return char array
405 415
     */
406
    const char * version();
416
    const char *version();
407 417

  
408 418
/*
409 419
     * Add a native method.
......
411 421
     * @param signature of the native method
412 422
     * @param fnPtr Pointer to the native method
413 423
 */
414
void addNativeMethod(char *name, char* signature, void * fnPtr){
424
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
415 425

  
416
	JNINativeMethod method;
417
	method.name = name;
418
	method.signature = signature;
419
	method.fnPtr = fnPtr;
426
        JNINativeMethod method;
427
        method.name = name;
428
        method.signature = signature;
429
        method.fnPtr = fnPtr;
420 430

  
421
	nativeMethodVect.push_back(method);
431
        nativeMethodVect.push_back(method);
422 432

  
423
	
424 433

  
425
}
434
    }
426 435

  
427 436
/*
428 437
     * Register several native methods for one class.
429 438
     * @param libName name of the library which contains the function(s). Loads the library
430
     * @param gMethods Register native methods. Default is NULL, also NULL allowed in which cause assumption is made the user has added native methods using the method addNativeMethod .
439
     * @param gMethods Register native methods. Default is nullptr, also nullptr allowed in which cause assumption is made the user has added native methods using the method addNativeMethod .
431 440
 * @return bool success of registered native method
432 441
 */
433
bool registerCPPFunction(char * libName, JNINativeMethod * gMethods=NULL){
434
	if(libName != NULL) {
435
		setConfigurationProperty("extc", libName);
436
			
437
	}
438

  
439
	if(gMethods == NULL && nativeMethodVect.size()==0) {
440
	return false;
441
	} else {
442
		if(gMethods == NULL) {
443
			//copy vector to gMethods
444
			gMethods = new JNINativeMethod[nativeMethodVect.size()];
445
		} 
446
		return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
447
    gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
448
	
449

  
450
	}
451
	return false;
452
}
442
    bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
443
        if (libName != nullptr) {
444
            setConfigurationProperty("extc", libName);
445

  
446
        }
447

  
448
        if (gMethods == nullptr && nativeMethodVect.size() == 0) {
449
            return false;
450
        } else {
451
            if (gMethods == nullptr) {
452
                //copy vector to gMethods
453
                gMethods = new JNINativeMethod[nativeMethodVect.size()];
454
            }
455
            return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
456
                                         gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
457

  
458

  
459
        }
460
        return false;
461
    }
453 462

  
454 463
/*
455 464
 * Register several native methods for one class.
456 465
 * @return bool success of registered native method
457 466
 */
458
static bool registerNativeMethods(JNIEnv* env, const char* className,
459
    JNINativeMethod* gMethods, int numMethods)
460
{
461
    jclass clazz;
462
    clazz = env->FindClass(className);
463
    if (clazz == NULL) {
464
        std::cerr<<"Native registration unable to find class "<< className<<std::endl;
465
        return false;
466
    }
467
	
468
    if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
469
       // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
470
        return false;
467
    static bool registerNativeMethods(JNIEnv *env, const char *className,
468
                                      JNINativeMethod *gMethods, int numMethods) {
469
        jclass clazz;
470
        clazz = env->FindClass(className);
471
        if (clazz == nullptr) {
472
            std::cerr << "Native registration unable to find class " << className << std::endl;
473
            return false;
474
        }
475

  
476
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
477
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
478
            return false;
479
        }
480
        return true;
471 481
    }
472
    return true;
473
}
474 482

  
475
	SaxonApiException * checkAndCreateException(jclass cppClass);
483
    SaxonApiException *checkAndCreateException(jclass cppClass);
476 484

  
477 485

  
478 486

  
......
480 488
//	XQueryEngine
481 489
//	SchemaManager
482 490

  
483
   // static JNIEnv *env;
491
    // static JNIEnv *env;
484 492
    static int jvmCreatedCPP;
485
    static sxnc_environment * sxn_environ;
493
    static sxnc_environment *sxn_environ;
486 494
    std::string cwd; /*!< current working directory */
487 495
    jobject proc; /*!< Java Processor object */
488
    
496

  
489 497
    /*static JavaVM *jvm;*/
490
    
491
protected:
492 498

  
499
protected:
493 500

  
494 501

  
495
	jclass xdmAtomicClass;
496
	jclass  versionClass;
497
	jclass  procClass;
498
	jclass  saxonCAPIClass;
499
	std::string cwdV; /*!< current working directory */
500
	//std::string resources_dir; /*!< current Saxon resources directory */
501
	char * versionStr;
502
	std::map<std::string,XdmValue*> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
503
	std::map<std::string,std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */	 
504
	bool licensei; /*!< indicates whether the Processor requires a Saxon that needs a license file (i.e. Saxon-EE) other a Saxon-HE Processor is created  */
505
	bool closed;
502
    jclass xdmAtomicClass;
503
    jclass versionClass;
504
    jclass procClass;
505
    jclass saxonCAPIClass;
506
    std::string cwdV; /*!< current working directory */
507
    //std::string resources_dir; /*!< current Saxon resources directory */
508
    char *versionStr;
509
    std::map<std::string, XdmValue *> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
510
    std::map<std::string, std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */
511
    bool licensei; /*!< indicates whether the Processor requires a Saxon that needs a license file (i.e. Saxon-EE) other a Saxon-HE Processor is created  */
512
    bool closed;
506 513

  
507 514

  
508
	JNINativeMethod * nativeMethods;
509
	std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
510
    SaxonApiException * exception;
515
    JNINativeMethod *nativeMethods;
516
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
517
    SaxonApiException *exception;
511 518

  
512 519

  
513 520
private:
514 521

  
515
    
516 522

  
517
	void applyConfigurationProperties();
518
	// Saxon/C method for internal use
519
    static JParameters createParameterJArray(std::map<std::string,XdmValue*> parameters, std::map<std::string,std::string> properties);
520
    static JParameters createParameterJArray2(std::map<std::string,XdmValue*> parameters);
521
    static jobjectArray createJArray(XdmValue ** values, int length);
522
    static 	const char* checkException(jobject cpp);
523
    void applyConfigurationProperties();
524

  
525
    // Saxon/C method for internal use
526
    static JParameters
527
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
528

  
529
    static JParameters createParameterJArray2(std::map<std::string, XdmValue *> parameters);
530

  
531
    static jobjectArray createJArray(XdmValue **values, int length);
532

  
533
    static const char *checkException(jobject cpp);
523 534
};
524 535

  
525 536
//===============================================================================================

Also available in: Unified diff