Project

Profile

Help

Maintenance. Planio will be undergoing a scheduled maintenance this weekend. Between Saturday, July 24 at 9:00 UTC and Sunday, July 25, 22:00 UTC your account might observe occasional downtimes which may last up to several minutes in some cases.

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 03fcd3f9

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 "XsltExecutable.h"
40
#include "XQueryProcessor.h"
41
#include "XPathProcessor.h"
42
#include "SchemaValidator.h"
43
#include "SaxonApiException.h"
44
//#include "com_saxonica_functions_extfn_PhpCall.h"
45
//#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
46

    
47
class XsltProcessor;
48

    
49
class Xslt30Processor;
50

    
51
class XQueryProcessor;
52

    
53
class XPathProcessor;
54

    
55
class SchemaValidator;
56

    
57
class XdmValue;
58

    
59
class XdmNode;
60

    
61
class XdmItem;
62

    
63
class XdmAtomicValue;
64

    
65
class XdmFunctionItem;
66

    
67
class XdmArray;
68

    
69
class XdmMap;
70

    
71
class XsltExecutable;
72

    
73

    
74
// The Saxon XSLT interface class
75

    
76
typedef struct {
77
    jobjectArray stringArray;
78
    jobjectArray objectArray;
79

    
80
} JParameters;
81

    
82

    
83

    
84
//==========================================
85

    
86

    
87

    
88
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
89
 */
90
class SaxonProcessor {
91

    
92
    friend class XsltProcessor;
93

    
94
    friend class Xslt30Processor;
95

    
96
    friend class XsltExecutable;
97

    
98
    friend class XQueryProcessor;
99

    
100
    friend class SchemaValidator;
101

    
102
    friend class XPathProcessor;
103

    
104
    friend class XdmValue;
105

    
106
    friend class XdmAtomicValue;
107

    
108
    friend class XdmFunctionItem;
109

    
110
    friend class XdmNode;
111

    
112
    friend class XdmMap;
113

    
114
    friend class XdmArray;
115

    
116
public:
117

    
118
    //! A default constructor.
119
    /*!
120
      * Create Saxon Processor.
121
    */
122

    
123
    SaxonProcessor();
124

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

    
130
    SaxonProcessor(const char *configFile);
131

    
132

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

    
140
    SaxonProcessor &operator=(const SaxonProcessor &other);
141

    
142

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

    
149
    /*!
150

151
       * Destructor
152
     */
153
    ~SaxonProcessor();
154

    
155

    
156
    const char * getErrorMessage();
157

    
158

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

    
164
    /*!
165

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

    
171
    /*!
172

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

    
178

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

    
186

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

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

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

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

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

    
229

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

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

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

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

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

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

    
279
#if CVERSION_API_NO >= 123
280

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

    
288

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

    
296

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

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

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

    
318

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

    
321

    
322
#endif
323

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

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

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

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

    
345
    int getNodeKind(jobject);
346

    
347
    bool isSchemaAwareProcessor();
348

    
349

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

    
356
    /**
357

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

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

    
371

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

    
377

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

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

    
388

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

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

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

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

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

    
420

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

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

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

    
440
        nativeMethodVect.push_back(method);
441

    
442

    
443
    }
444

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

    
456
        }
457

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

    
468

    
469
        }
470
        return false;
471
    }
472

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

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

    
493
    SaxonApiException *checkAndCreateException(jclass cppClass);
494

    
495

    
496

    
497
//        XPathEngine
498
//        XQueryEngine
499
//        SchemaManager
500

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

    
507
    /*static JavaVM *jvm;*/
508

    
509
protected:
510

    
511

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

    
524

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

    
529

    
530
private:
531

    
532

    
533
    void applyConfigurationProperties();
534

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

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

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

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

    
546
//===============================================================================================
547

    
548
#endif /* SAXON_PROCESSOR_H */
(14-14/55)