Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / SaxonProcessor.h @ 6304fb8b

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
public:
111

    
112
    //! A default constructor.
113
    /*!
114
      * Create Saxon Processor.
115
    */
116

    
117
    SaxonProcessor();
118

    
119
    //! constructor based upon a Saxon configuration file.
120
    /*!
121
      * Create Saxon Processor.
122
    */
123

    
124
    SaxonProcessor(const char *configFile);
125

    
126

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

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

    
136

    
137
    /**
138
     * Xslt30Processor copy constructor.
139
     * @param other - Xslt30Processor
140
     */
141
    SaxonProcessor(const SaxonProcessor &other);
142

    
143
    /*!
144

145
       * Destructor
146
     */
147
    ~SaxonProcessor();
148

    
149

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

    
155
    /*!
156

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

    
162
    /*!
163

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

    
169

    
170
    /*!
171
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
172
     *
173
     * @return a newly created XQueryProcessor
174
     */
175
    XQueryProcessor *newXQueryProcessor();
176

    
177

    
178
    /*!
179
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
180
     *
181
     * @return a newly created XPathProcessor
182
     */
183
    XPathProcessor *newXPathProcessor();
184

    
185
    /*!
186
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
187
     * SchemaManager
188
     *
189
     * @return a new SchemaValidator
190
     */
191
    SchemaValidator *newSchemaValidator();
192

    
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 String value. nullptr is taken as equivalent to "".
199
     * @return the corresponding StringValue
200
     */
201
    XdmAtomicValue *makeStringValue(std::string str);
202

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

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

    
220

    
221
    /*!
222
     * Factory method (for convenience in compiled bytecode)
223
     *
224
     * @param d the value of the double
225
     * @return a new XdmAtomicValue
226
     */
227
    XdmAtomicValue *makeDoubleValue(double d);
228

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

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

    
245
    /*!
246
     * Factory method: makes a XdmAtomicValue representing a boolean Value
247
     *
248
     * @param b true or false, to determine which boolean value is
249
     *              required
250
     * @return the XdmAtomicValue requested
251
     */
252
    XdmAtomicValue *makeBooleanValue(bool b);
253

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

    
261
    /*!
262
     * Create an Xdm Atomic value from string representation
263
     * @param type    - Local name of a type in the XML Schema namespace.
264
     * @param value - The value given in a string form.
265
     * In the case of a QName the value supplied must be in clark notation. {uri}local
266
     * @return XdmValue - value
267
    */
268
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
269

    
270
#if CVERSION_API_NO >= 123
271

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

    
279

    
280
    /**
281
        * Make an XdmArray whose members are xs:short values
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(short *input, int length);
286

    
287

    
288
    /**
289
        * Make an XdmArray whose members are xs:int 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(int *input, int length);
294

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

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

    
309

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

    
312

    
313
#endif
314

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

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

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

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

    
336
    int getNodeKind(jobject);
337

    
338
    bool isSchemaAwareProcessor();
339

    
340

    
341
    /**
342
     * Checks for thrown exceptions
343
     * @return bool - true when there is a pending exception; otherwise return false
344
    */
345
    bool exceptionOccurred();
346

    
347
    /**
348

349
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
350
    */
351
    void exceptionClear();
352

    
353
    /**
354
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
355
     * @param env
356
     * @param callingClass
357
     * @param callingObject
358
     * @return SaxonApiException
359
    */
360
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
361

    
362

    
363
    /*
364
      * Clean up and destroy Java VM to release memory used. 
365
     */
366
    static void release();
367

    
368

    
369
    /**
370
     * set the current working directory
371
    */
372
    void setcwd(const char *cwd);
373

    
374
    /**
375
     * get the current working directory
376
    */
377
    const char *getcwd();
378

    
379

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

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

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

    
397
    /**
398
     * Set a configuration property specific to the processor in use. 
399
     * Properties specified here are common across all the processors.
400
     * Example 'l':enable line number has the value 'on' or 'off'
401
     * @param name of the property
402
     * @param value of the property
403
     */
404
    void setConfigurationProperty(const char *name, const char *value);
405

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

    
411

    
412
    /**
413
     * Get the Saxon version
414
     * @return char array
415
     */
416
    const char *version();
417

    
418
/*
419
     * Add a native method.
420
     * @param name of the native method
421
     * @param signature of the native method
422
     * @param fnPtr Pointer to the native method
423
 */
424
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
425

    
426
        JNINativeMethod method;
427
        method.name = name;
428
        method.signature = signature;
429
        method.fnPtr = fnPtr;
430

    
431
        nativeMethodVect.push_back(method);
432

    
433

    
434
    }
435

    
436
/*
437
     * Register several native methods for one class.
438
     * @param libName name of the library which contains the function(s). Loads the library
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 .
440
 * @return bool success of registered native method
441
 */
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
    }
462

    
463
/*
464
 * Register several native methods for one class.
465
 * @return bool success of registered native method
466
 */
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;
481
    }
482

    
483
    SaxonApiException *checkAndCreateException(jclass cppClass);
484

    
485

    
486

    
487
//        XPathEngine
488
//        XQueryEngine
489
//        SchemaManager
490

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

    
497
    /*static JavaVM *jvm;*/
498

    
499
protected:
500

    
501

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

    
514

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

    
519

    
520
private:
521

    
522

    
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);
534
};
535

    
536
//===============================================================================================
537

    
538
#endif /* SAXON_PROCESSOR_H */
(13-13/49)