Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 686e76e7

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

    
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)
20
#else
21
#include <windows.h>
22
#endif
23

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

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

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

    
46
class XsltProcessor;
47

    
48
class Xslt30Processor;
49

    
50
class XQueryProcessor;
51

    
52
class XPathProcessor;
53

    
54
class SchemaValidator;
55

    
56
class XdmValue;
57

    
58
class XdmNode;
59

    
60
class XdmItem;
61

    
62
class XdmAtomicValue;
63

    
64
class XdmFunctionItem;
65

    
66
class XdmArray;
67

    
68
class XdmMap;
69

    
70
class XsltExecutable;
71

    
72

    
73
// The Saxon XSLT interface class
74

    
75
//std::mutex mtx;
76

    
77

    
78
typedef struct {
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

    
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;
109

    
110
    friend class XdmFunctionItem;
111

    
112
    friend class XdmNode;
113

    
114
    friend class XdmMap;
115

    
116
    friend class XdmArray;
117

    
118
public:
119

    
120
    //! A default constructor.
121
    /*!
122
      * Create Saxon Processor.
123
    */
124

    
125
    SaxonProcessor();
126

    
127
    //! constructor based upon a Saxon configuration file.
128
    /*!
129
      * Create Saxon Processor.
130
    */
131

    
132
    SaxonProcessor(const char *configFile);
133

    
134

    
135
    //! A constructor.
136
    /*!
137
      * Create Saxon Processor.
138
      * @param l - Flag that a license is to be used. Default is false.        
139
    */
140
    SaxonProcessor(bool l);
141

    
142
    SaxonProcessor &operator=(const SaxonProcessor &other);
143

    
144

    
145
    /**
146
     * Xslt30Processor copy constructor.
147
     * @param other - Xslt30Processor
148
     */
149
    SaxonProcessor(const SaxonProcessor &other);
150

    
151
    /*!
152

153
       * Destructor
154
     */
155
    ~SaxonProcessor();
156

    
157

    
158
    //! Get the Processor object. Method used in Python
159
    /* SaxonProcessor * getProcessor(){
160
     return this;
161
     }*/
162

    
163
    /*!
164

165
       * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
166
       * @return a newly created XsltProcessor
167
     */
168
    XsltProcessor *newXsltProcessor();
169

    
170
    /*!
171

172
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
173
       * @return a newly created Xslt30Processor
174
     */
175
    Xslt30Processor *newXslt30Processor();
176

    
177

    
178
    /*!
179
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
180
     *
181
     * @return a newly created XQueryProcessor
182
     */
183
    XQueryProcessor *newXQueryProcessor();
184

    
185

    
186
    /*!
187
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
188
     *
189
     * @return a newly created XPathProcessor
190
     */
191
    XPathProcessor *newXPathProcessor();
192

    
193
    /*!
194
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
195
     * SchemaManager
196
     *
197
     * @return a new SchemaValidator
198
     */
199
    SchemaValidator *newSchemaValidator();
200

    
201

    
202
    /*!
203
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
204
     * of a zero-length string (and potentially other strings, in future)
205
     *
206
     * @param value the String value. nullptr is taken as equivalent to "".
207
     * @return the corresponding StringValue
208
     */
209
    XdmAtomicValue *makeStringValue(std::string str);
210

    
211
    /*!
212
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
213
     * of a zero-length string (and potentially other strings, in future)
214
     *
215
     * @param value the char pointer array. nullptr is taken as equivalent to "".
216
     * @return the corresponding StringValue
217
     */
218
    XdmAtomicValue *makeStringValue(const char *str);
219

    
220
    /*!
221
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
222
     *
223
     * @param i the supplied primitive integer value
224
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
225
     */
226
    XdmAtomicValue *makeIntegerValue(int i);
227

    
228

    
229
    /*!
230
     * Factory method (for convenience in compiled bytecode)
231
     *
232
     * @param d the value of the double
233
     * @return a new XdmAtomicValue
234
     */
235
    XdmAtomicValue *makeDoubleValue(double d);
236

    
237
    /*!
238
     * Factory method (for convenience in compiled bytecode)
239
     *
240
     * @param f the value of the foat
241
     * @return a new XdmAtomicValue
242
     */
243
    XdmAtomicValue *makeFloatValue(float);
244

    
245
    /*!
246
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
247
     *
248
     * @param l the supplied primitive long value
249
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
250
     */
251
    XdmAtomicValue *makeLongValue(long l);
252

    
253
    /*!
254
     * Factory method: makes a XdmAtomicValue representing a boolean Value
255
     *
256
     * @param b true or false, to determine which boolean value is
257
     *              required
258
     * @return the XdmAtomicValue requested
259
     */
260
    XdmAtomicValue *makeBooleanValue(bool b);
261

    
262
    /**
263
     * Create an QName Xdm value from string representation in clark notation
264
     * @param str - The value given in a string form in clark notation. {uri}local
265
     * @return XdmAtomicValue - value
266
    */
267
    XdmAtomicValue *makeQNameValue(const char *str);
268

    
269
    /*!
270
     * Create an Xdm Atomic value from string representation
271
     * @param type    - Local name of a type in the XML Schema namespace.
272
     * @param value - The value given in a string form.
273
     * In the case of a QName the value supplied must be in clark notation. {uri}local
274
     * @return XdmValue - value
275
    */
276
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
277

    
278
#if CVERSION_API_NO >= 123
279

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

    
287

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

    
295

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

    
303
    /**
304
        * Make an XdmArray whose members are xs:long values
305
        * @param input the input array of booleans
306
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
307
   */
308
    XdmArray *makeArray(long *input, int length);
309

    
310
    /**
311
        * Make an XdmArray whose members are xs:boolean values
312
        * @param input the input array of booleans
313
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
314
   */
315
    XdmArray *makeArray(bool *input, int length);
316

    
317

    
318
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
319

    
320

    
321
#endif
322

    
323
    /**
324
    * Get the string representation of the XdmValue.
325
    * @return char array
326
    */
327
    const char *getStringValue(XdmItem *item);
328

    
329
    /**
330
     * Parse a lexical representation of the source document and return it as an XdmNode
331
    */
332
    XdmNode *parseXmlFromString(const char *source);
333

    
334
    /**
335
     * Parse a source document file and return it as an XdmNode.
336
    */
337
    XdmNode *parseXmlFromFile(const char *source);
338

    
339
    /**
340
     * Parse a source document available by URI and return it as an XdmNode.
341
    */
342
    XdmNode *parseXmlFromUri(const char *source);
343

    
344
    int getNodeKind(jobject);
345

    
346
    bool isSchemaAwareProcessor();
347

    
348

    
349
    /**
350
     * Checks for thrown exceptions
351
     * @return bool - true when there is a pending exception; otherwise return false
352
    */
353
    bool exceptionOccurred();
354

    
355
    /**
356

357
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
358
    */
359
    void exceptionClear();
360

    
361
    /**
362
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
363
     * @param env
364
     * @param callingClass
365
     * @param callingObject
366
     * @return SaxonApiException
367
    */
368
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
369

    
370

    
371
    /*
372
      * Clean up and destroy Java VM to release memory used. 
373
     */
374
    static void release();
375

    
376

    
377
    /**
378
     * set the current working directory
379
    */
380
    void setcwd(const char *cwd);
381

    
382
    /**
383
     * get the current working directory
384
    */
385
    const char *getcwd();
386

    
387

    
388
    /**
389
     * set saxon resources directory
390
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
391
    */
392
    void setResourcesDirectory(const char *dir){}
393

    
394
    /**
395
     * set catalog to be used in Saxon
396
    */
397
    void setCatalog(const char *catalogFile, bool isTracing);
398

    
399
    /**
400
     * get saxon resources directory
401
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
402
    */
403
    const char * getResourcesDirectory(){}
404

    
405
    /**
406
     * Set a configuration property specific to the processor in use. 
407
     * Properties specified here are common across all the processors.
408
     * Example 'l':enable line number has the value 'on' or 'off'
409
     * @param name of the property
410
     * @param value of the property
411
     */
412
    void setConfigurationProperty(const char *name, const char *value);
413

    
414
    /**
415
     * Clear configuration properties specific to the processor in use. 
416
     */
417
    void clearConfigurationProperties();
418

    
419

    
420
    /**
421
     * Get the Saxon version
422
     * @return char array
423
     */
424
    const char *version();
425

    
426
/*
427
     * Add a native method.
428
     * @param name of the native method
429
     * @param signature of the native method
430
     * @param fnPtr Pointer to the native method
431
 */
432
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
433

    
434
        JNINativeMethod method;
435
        method.name = name;
436
        method.signature = signature;
437
        method.fnPtr = fnPtr;
438

    
439
        nativeMethodVect.push_back(method);
440

    
441

    
442
    }
443

    
444
/*
445
     * Register several native methods for one class.
446
     * @param libName name of the library which contains the function(s). Loads the library
447
     * @param gMethods Register native methods. Default is nullptr, also nullptr allowed in
448
     which cause assumption is made the user has added native methods using the method addNativeMethod .
449
 * @return bool success of registered native method
450
 */
451
    bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
452
        if (libName != nullptr) {
453
            setConfigurationProperty("extc", libName);
454

    
455
        }
456

    
457
        if (gMethods == nullptr && nativeMethodVect.size() == 0) {
458
            return false;
459
        } else {
460
            if (gMethods == nullptr) {
461
                //copy vector to gMethods
462
                gMethods = new JNINativeMethod[nativeMethodVect.size()];
463
            }
464
            return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
465
                                         gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
466

    
467

    
468
        }
469
        return false;
470
    }
471

    
472
/*
473
 * Register several native methods for one class.
474
 * @return bool success of registered native method
475
 */
476
    static bool registerNativeMethods(JNIEnv *env, const char *className,
477
                                      JNINativeMethod *gMethods, int numMethods) {
478
        jclass clazz;
479
        clazz = env->FindClass(className);
480
        if (clazz == nullptr) {
481
            std::cerr << "Native registration unable to find class " << className << std::endl;
482
            return false;
483
        }
484

    
485
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
486
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
487
            return false;
488
        }
489
        return true;
490
    }
491

    
492
    SaxonApiException *checkAndCreateException(jclass cppClass);
493

    
494

    
495

    
496
//        XPathEngine
497
//        XQueryEngine
498
//        SchemaManager
499

    
500
    // static JNIEnv *env;
501
    static int jvmCreatedCPP;
502
    static sxnc_environment *sxn_environ;
503
    std::string cwd; /*!< current working directory */
504
    jobject proc; /*!< Java Processor object */
505

    
506
    /*static JavaVM *jvm;*/
507

    
508
protected:
509

    
510

    
511
    jclass xdmAtomicClass;
512
    jclass versionClass;
513
    jclass procClass;
514
    jclass saxonCAPIClass;
515
    std::string cwdV; /*!< current working directory */
516
    //std::string resources_dir; /*!< current Saxon resources directory */
517
    char *versionStr;
518
    std::map<std::string, XdmValue *> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
519
    std::map<std::string, std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */
520
    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  */
521
    bool closed;
522

    
523

    
524
    JNINativeMethod *nativeMethods;
525
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
526
    SaxonApiException *exception;
527

    
528

    
529
private:
530

    
531

    
532
    void applyConfigurationProperties();
533

    
534
    // Saxon/C method for internal use
535
    static JParameters
536
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
537

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

    
540
    static jobjectArray createJArray(XdmValue **values, int length);
541

    
542
    static const char *checkException(jobject cpp);
543
};
544

    
545
//===============================================================================================
546

    
547
#endif /* SAXON_PROCESSOR_H */
(13-13/54)