Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / SaxonProcessor.h @ 5401a5ae

1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2020 Saxonica Limited.
3
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7

    
8
#ifndef SAXON_PROCESSOR_H
9
#define SAXON_PROCESSOR_H
10
        
11
#if defined __linux__ || defined __APPLE__
12
        #include <stdlib.h>
13
        #include <string.h>
14
        #include <dlfcn.h>
15

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

    
23
//#define DEBUG //remove
24
#define CVERSION "1.3.0"
25
#define CVERSION_API_NO 130
26
#include <string>
27
#include <iostream>
28
#include <sstream>  
29
#include <map>        
30
#include <vector>
31
#include <stdexcept>      // std::logic_error
32

    
33
#include "SaxonCGlue.h"
34
#include "SaxonCXPath.h"
35
#include "XsltProcessor.h"
36
#include "Xslt30Processor.h"
37
#include "XQueryProcessor.h"
38
#include "XPathProcessor.h"
39
#include "SchemaValidator.h"
40
#include "SaxonApiException.h"
41
//#include "com_saxonica_functions_extfn_PhpCall.h"
42
//#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
43

    
44
class XsltProcessor;
45
class Xslt30Processor;
46
class XQueryProcessor;
47
class XPathProcessor;
48
class SchemaValidator;
49
class XdmValue;
50
class XdmNode;
51
class XdmItem;
52
class XdmAtomicValue;
53

    
54
#if CVERSION_API_NO >= 123
55
class XdmFunctionItem;
56
class XdmArray;
57
class XdmMap;
58
#endif
59

    
60

    
61

    
62
// The Saxon XSLT interface class
63

    
64
//std::mutex mtx;
65
/*! <code>MyException</code>. This struct captures details of the Java exception thrown from Saxon s9api API (Java).
66
 * <p/>
67
 */
68
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
    jobjectArray stringArray;
80
    jobjectArray objectArray;
81

    
82
}JParameters;
83

    
84

    
85

    
86
//==========================================
87

    
88

    
89

    
90
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
91
 */
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;
100

    
101
public:
102

    
103
   //! A default constructor.
104
    /*!
105
      * Create Saxon Processor.
106
    */
107

    
108
    SaxonProcessor();
109

    
110
   //! constructor based upon a Saxon configuration file.
111
    /*!
112
      * Create Saxon Processor.
113
    */
114

    
115
    SaxonProcessor(const char * configFile);
116

    
117

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

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

    
127

    
128
        /**
129
         * Xslt30Processor copy constructor.
130
         * @param other - Xslt30Processor
131
         */
132
    SaxonProcessor(const SaxonProcessor &other);
133

    
134
   /*!
135

136
      * Destructor
137
    */
138
    ~SaxonProcessor();
139

    
140

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

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

    
153
   /*!
154

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

    
160

    
161
    /*!
162
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
163
     *
164
     * @return a newly created XQueryProcessor
165
     */
166
    XQueryProcessor * newXQueryProcessor();
167

    
168

    
169
    /*!
170
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
171
     *
172
     * @return a newly created XPathProcessor
173
     */
174
    XPathProcessor * newXPathProcessor();
175

    
176
    /*!
177
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
178
     * SchemaManager
179
     *
180
     * @return a new SchemaValidator
181
     */
182
    SchemaValidator * newSchemaValidator();
183

    
184

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

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

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

    
211

    
212
    /*!
213
     * Factory method (for convenience in compiled bytecode)
214
     *
215
     * @param d the value of the double
216
     * @return a new XdmAtomicValue
217
     */
218
    XdmAtomicValue * makeDoubleValue(double d);
219

    
220
    /*!
221
     * Factory method (for convenience in compiled bytecode)
222
     *
223
     * @param f the value of the foat
224
     * @return a new XdmAtomicValue
225
     */
226
    XdmAtomicValue * makeFloatValue(float);
227

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

    
236
    /*!
237
     * Factory method: makes a XdmAtomicValue representing a boolean Value
238
     *
239
     * @param b true or false, to determine which boolean value is
240
     *              required
241
     * @return the XdmAtomicValue requested
242
     */
243
    XdmAtomicValue * makeBooleanValue(bool b);
244

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

    
252
    /*!
253
     * Create an Xdm Atomic value from string representation
254
     * @param type    - Local name of a type in the XML Schema namespace.
255
     * @param value - The value given in a string form.
256
     * In the case of a QName the value supplied must be in clark notation. {uri}local
257
     * @return XdmValue - value
258
    */
259
    XdmAtomicValue * makeAtomicValue(const char * type, const char * value);
260

    
261
#if CVERSION_API_NO >= 123
262
    /**
263
        * Make an XdmArray whose members are from string representation
264
        * @param input the input array of booleans
265
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
266
   */
267
    XdmArray * makeArray(const char ** input, int length);
268

    
269

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

    
277

    
278

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

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

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

    
300

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

    
304
#endif
305

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

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

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

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

    
327
    int getNodeKind(jobject);
328

    
329
    bool isSchemaAwareProcessor();
330

    
331
 
332

    
333
    /**
334
     * Checks for thrown exceptions
335
     * @return bool - true when there is a pending exception; otherwise return false
336
    */
337
    bool exceptionOccurred();
338

    
339
    /**
340

341
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
342
    */
343
    void exceptionClear();
344

    
345
    /**
346
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
347
     * @param env
348
     * @param callingClass
349
     * @param callingObject
350
     * @return SaxonApiException
351
    */
352
    SaxonApiException * checkForExceptionCPP(JNIEnv* env, jclass callingClass,  jobject callingObject);
353

    
354

    
355
    /*
356
      * Clean up and destroy Java VM to release memory used. 
357
     */
358
    static void release();
359

    
360

    
361
    /**
362
     * set the current working directory
363
    */
364
   void setcwd(const char* cwd);
365

    
366
    /**
367
     * get the current working directory
368
    */
369
   const char* getcwd();
370

    
371

    
372
    /**
373
     * set saxon resources directory
374
    */
375
   void setResourcesDirectory(const char* dir);
376
        
377
    /**
378
     * set catalog to be used in Saxon
379
    */
380
   void setCatalog(const char* catalogFile, bool isTracing);
381

    
382
    /**
383
     * get saxon resources directory
384
    */
385
   const char * getResourcesDirectory();
386

    
387
    /**
388
     * Set a configuration property specific to the processor in use. 
389
     * Properties specified here are common across all the processors.
390
     * Example 'l':enable line number has the value 'on' or 'off'
391
     * @param name of the property
392
     * @param value of the property
393
     */
394
    void setConfigurationProperty(const char * name, const char * value);
395

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

    
401

    
402
    /**
403
     * Get the Saxon version
404
     * @return char array
405
     */
406
    const char * version();
407

    
408
/*
409
     * Add a native method.
410
     * @param name of the native method
411
     * @param signature of the native method
412
     * @param fnPtr Pointer to the native method
413
 */
414
void addNativeMethod(char *name, char* signature, void * fnPtr){
415

    
416
        JNINativeMethod method;
417
        method.name = name;
418
        method.signature = signature;
419
        method.fnPtr = fnPtr;
420

    
421
        nativeMethodVect.push_back(method);
422

    
423
        
424

    
425
}
426

    
427
/*
428
     * Register several native methods for one class.
429
     * @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 .
431
 * @return bool success of registered native method
432
 */
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
}
453

    
454
/*
455
 * Register several native methods for one class.
456
 * @return bool success of registered native method
457
 */
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;
471
    }
472
    return true;
473
}
474

    
475
        SaxonApiException * checkAndCreateException(jclass cppClass);
476

    
477

    
478

    
479
//        XPathEngine
480
//        XQueryEngine
481
//        SchemaManager
482

    
483
   // static JNIEnv *env;
484
    static int jvmCreatedCPP;
485
    static sxnc_environment * sxn_environ;
486
    static int refCount;
487
    std::string cwd; /*!< current working directory */
488
    jobject proc; /*!< Java Processor object */
489
    
490
    /*static JavaVM *jvm;*/
491
    
492
protected:
493

    
494

    
495

    
496
        jclass xdmAtomicClass;
497
        jclass  versionClass;
498
        jclass  procClass;
499
        jclass  saxonCAPIClass;
500
        std::string cwdV; /*!< current working directory */
501
        //std::string resources_dir; /*!< current Saxon resources directory */
502
        char * versionStr;
503
        std::map<std::string,XdmValue*> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
504
        std::map<std::string,std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */         
505
        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  */
506
        bool closed;
507

    
508

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

    
513

    
514
private:
515

    
516
    
517

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

    
526
//===============================================================================================
527

    
528
#endif /* SAXON_PROCESSOR_H */
(14-14/52)