Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 01d6fdb6

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

    
12
#if defined __linux__ || defined __APPLE__
13

    
14
#include <stdlib.h>
15
#include <string>
16
#include <dlfcn.h>
17

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

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

    
29

    
30

    
31

    
32
#include <string>
33
#include <iostream>
34
#include <sstream>
35
#include <map>
36
#include <vector>
37
#include <stdexcept>      // std::logic_error
38

    
39
#include "SaxonCGlue.h"
40
#include "SaxonCXPath.h"
41
#include "XsltProcessor.h"
42
#include "Xslt30Processor.h"
43
#include "XsltExecutable.h"
44
#include "XQueryProcessor.h"
45
#include "XPathProcessor.h"
46
#include "SchemaValidator.h"
47
#include "SaxonApiException.h"
48
//#include "com_saxonica_functions_extfn_PhpCall.h"
49
//#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
50

    
51
#if defined MEM_DEBUG
52

    
53

    
54
#include <algorithm>
55
#include <cstdlib>
56
#include <new>
57

    
58
static std::vector<void*> myAlloc;
59

    
60
void* newImpl(std::size_t sz,char const* file, int line);
61

    
62
void* operator new(std::size_t sz,char const* file, int line);
63

    
64
void* operator new [](std::size_t sz,char const* file, int line);
65

    
66
void operator delete(void* ptr) noexcept;
67

    
68
#endif
69

    
70

    
71
class XsltProcessor;
72

    
73
class Xslt30Processor;
74

    
75
class XQueryProcessor;
76

    
77
class XPathProcessor;
78

    
79
class SchemaValidator;
80

    
81
class XdmValue;
82

    
83
class XdmNode;
84

    
85
class XdmItem;
86

    
87
class XdmAtomicValue;
88

    
89
class XdmFunctionItem;
90

    
91
class XdmArray;
92

    
93
class XdmMap;
94

    
95
class XsltExecutable;
96

    
97
class SaxonApiException;
98

    
99

    
100
// The Saxon XSLT interface class
101

    
102
typedef struct {
103
    jobjectArray stringArray;
104
    jobjectArray objectArray;
105

    
106
} JParameters;
107

    
108

    
109

    
110
//==========================================
111

    
112

    
113

    
114
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
115
 */
116
class SaxonProcessor {
117

    
118
    friend class XsltProcessor;
119

    
120
    friend class Xslt30Processor;
121

    
122
    friend class XsltExecutable;
123

    
124
    friend class XQueryProcessor;
125

    
126
    friend class SchemaValidator;
127

    
128
    friend class XPathProcessor;
129

    
130
    friend class XdmValue;
131

    
132
    friend class XdmAtomicValue;
133

    
134
    friend class XdmFunctionItem;
135

    
136
    friend class XdmNode;
137

    
138
    friend class XdmMap;
139

    
140
    friend class XdmArray;
141

    
142
public:
143

    
144
    //! A default constructor.
145
    /*!
146
      * Create Saxon Processor.
147
    */
148

    
149
    SaxonProcessor();
150

    
151
    //! constructor based upon a Saxon configuration file.
152
    /*!
153
      * Create Saxon Processor.
154
    */
155

    
156
    SaxonProcessor(const char *configFile);
157

    
158

    
159
    //! A constructor.
160
    /*!
161
      * Create Saxon Processor.
162
      * @param l - Flag that a license is to be used. Default is false.        
163
    */
164
    SaxonProcessor(bool l);
165

    
166
    SaxonProcessor &operator=(const SaxonProcessor &other);
167

    
168

    
169
    /**
170
     * Xslt30Processor copy constructor.
171
     * @param other - Xslt30Processor
172
     */
173
    SaxonProcessor(const SaxonProcessor &other);
174

    
175
    /*!
176

177
       * Destructor
178
     */
179
    ~SaxonProcessor();
180

    
181

    
182
    const char * getErrorMessage();
183

    
184

    
185
    //! Get the Processor object. Method used in Python
186
    /* SaxonProcessor * getProcessor(){
187
     return this;
188
     }*/
189

    
190
    /*!
191

192
       * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
193
       * @return a newly created XsltProcessor
194
     */
195
    XsltProcessor *newXsltProcessor();
196

    
197
    /*!
198

199
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
200
       * @return a newly created Xslt30Processor
201
     */
202
    Xslt30Processor *newXslt30Processor();
203

    
204

    
205
    /*!
206
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
207
     *
208
     * @return a newly created XQueryProcessor
209
     */
210
    XQueryProcessor *newXQueryProcessor();
211

    
212

    
213
    /*!
214
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
215
     *
216
     * @return a newly created XPathProcessor
217
     */
218
    XPathProcessor *newXPathProcessor();
219

    
220
    /*!
221
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
222
     * SchemaManager
223
     *
224
     * @return a new SchemaValidator
225
     */
226
    SchemaValidator *newSchemaValidator();
227

    
228

    
229
    /*!
230
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
231
     * of a zero-length string (and potentially other strings, in future)
232
     *
233
     * @param value the String value. nullptr is taken as equivalent to "".
234
     * @return the corresponding StringValue
235
     */
236
    XdmAtomicValue *makeStringValue(std::string str);
237

    
238
    /*!
239
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
240
     * of a zero-length string (and potentially other strings, in future)
241
     *
242
     * @param value the char pointer array. nullptr is taken as equivalent to "".
243
     * @return the corresponding StringValue
244
     */
245
    XdmAtomicValue *makeStringValue(const char *str);
246

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

    
255

    
256
    /*!
257
     * Factory method (for convenience in compiled bytecode)
258
     *
259
     * @param d the value of the double
260
     * @return a new XdmAtomicValue
261
     */
262
    XdmAtomicValue *makeDoubleValue(double d);
263

    
264
    /*!
265
     * Factory method (for convenience in compiled bytecode)
266
     *
267
     * @param f the value of the foat
268
     * @return a new XdmAtomicValue
269
     */
270
    XdmAtomicValue *makeFloatValue(float);
271

    
272
    /*!
273
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
274
     *
275
     * @param l the supplied primitive long value
276
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
277
     */
278
    XdmAtomicValue *makeLongValue(long l);
279

    
280
    /*!
281
     * Factory method: makes a XdmAtomicValue representing a boolean Value
282
     *
283
     * @param b true or false, to determine which boolean value is
284
     *              required
285
     * @return the XdmAtomicValue requested
286
     */
287
    XdmAtomicValue *makeBooleanValue(bool b);
288

    
289
    /**
290
     * Create an QName Xdm value from string representation in clark notation
291
     * @param str - The value given in a string form in clark notation. {uri}local
292
     * @return XdmAtomicValue - value
293
    */
294
    XdmAtomicValue *makeQNameValue(const char *str);
295

    
296
    /*!
297
     * Create an Xdm Atomic value from string representation
298
     * @param type    - Local name of a type in the XML Schema namespace.
299
     * @param value - The value given in a string form.
300
     * In the case of a QName the value supplied must be in clark notation. {uri}local
301
     * @return XdmValue - value
302
    */
303
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
304

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

    
312

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

    
320

    
321
    /**
322
        * Make an XdmArray whose members are xs:int values
323
        * @param input the input array of booleans
324
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
325
   */
326
    XdmArray *makeArray(int *input, int length);
327

    
328
    /**
329
        * Make an XdmArray whose members are xs:long values
330
        * @param input the input array of booleans
331
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
332
   */
333
    XdmArray *makeArray(long *input, int length);
334

    
335
    /**
336
        * Make an XdmArray whose members are xs:boolean values
337
        * @param input the input array of booleans
338
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
339
   */
340
    XdmArray *makeArray(bool *input, int length);
341

    
342

    
343
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
344

    
345

    
346
    /**
347
    * Get the string representation of the XdmValue.
348
    * @return char array
349
    */
350
    const char *getStringValue(XdmItem *item);
351

    
352
    /**
353
     * Parse a lexical representation of the source document and return it as an XdmNode
354
    */
355
    XdmNode *parseXmlFromString(const char *source);
356

    
357
    /**
358
     * Parse a source document file and return it as an XdmNode.
359
    */
360
    XdmNode *parseXmlFromFile(const char *source);
361

    
362
    /**
363
     * Parse a source document available by URI and return it as an XdmNode.
364
    */
365
    XdmNode *parseXmlFromUri(const char *source);
366

    
367
    int getNodeKind(jobject);
368

    
369
    bool isSchemaAwareProcessor();
370

    
371

    
372
    /**
373
     * Checks for thrown exceptions
374
     * @return bool - true when there is a pending exception; otherwise return false
375
    */
376
    bool exceptionOccurred();
377

    
378
    /**
379

380
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
381
    */
382
    void exceptionClear();
383

    
384
    /**
385
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
386
     * @param env
387
     * @param callingClass
388
     * @param callingObject
389
     * @return SaxonApiException
390
    */
391
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
392

    
393

    
394
    /*
395
      * Clean up and destroy Java VM to release memory used. 
396
     */
397
    static void release();
398

    
399

    
400
    /**
401
     * set the current working directory
402
    */
403
    void setcwd(const char *cwd);
404

    
405
    /**
406
     * get the current working directory
407
    */
408
    const char *getcwd();
409

    
410

    
411
    /**
412
     * set saxon resources directory
413
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
414
    */
415
    void setResourcesDirectory(const char *dir);
416

    
417
    /**
418
     * set catalog to be used in Saxon
419
    */
420
    void setCatalog(const char *catalogFile, bool isTracing);
421

    
422
    /**
423
     * get saxon resources directory
424
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
425
    */
426
    const char * getResourcesDirectory();
427

    
428
    /**
429
     * Set a configuration property specific to the processor in use. 
430
     * Properties specified here are common across all the processors.
431
     * Example 'l':enable line number has the value 'on' or 'off'
432
     * @param name of the property
433
     * @param value of the property
434
     */
435
    void setConfigurationProperty(const char *name, const char *value);
436

    
437
    /**
438
     * Clear configuration properties specific to the processor in use. 
439
     */
440
    void clearConfigurationProperties();
441

    
442

    
443
    /**
444
     * Get the Saxon version
445
     * @return char array
446
     */
447
    const char *version();
448

    
449
/*
450
     * Add a native method.
451
     * @param name of the native method
452
     * @param signature of the native method
453
     * @param fnPtr Pointer to the native method
454
 */
455
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
456

    
457
        JNINativeMethod method;
458
        method.name = name;
459
        method.signature = signature;
460
        method.fnPtr = fnPtr;
461

    
462
        nativeMethodVect.push_back(method);
463

    
464

    
465
    }
466

    
467
/*
468
     * Register several native methods for one class.
469
     * @param libName name of the library which contains the function(s). Loads the library
470
     * @param gMethods Register native methods. Default is nullptr, also nullptr allowed in
471
     which cause assumption is made the user has added native methods using the method addNativeMethod .
472
 * @return bool success of registered native method
473
 */
474
    bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
475
        if (libName != nullptr) {
476
            setConfigurationProperty("extc", libName);
477

    
478
        }
479

    
480
        if (gMethods == nullptr && nativeMethodVect.size() == 0) {
481
            return false;
482
        } else {
483
            if (gMethods == nullptr) {
484
                //copy vector to gMethods
485
                gMethods = new JNINativeMethod[nativeMethodVect.size()];
486
            }
487
            return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
488
                                         gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
489

    
490

    
491
        }
492
        return false;
493
    }
494

    
495
/*
496
 * Register several native methods for one class.
497
 * @return bool success of registered native method
498
 */
499
    static bool registerNativeMethods(JNIEnv *env, const char *className,
500
                                      JNINativeMethod *gMethods, int numMethods) {
501
        jclass clazz;
502
        clazz = env->FindClass(className);
503
        if (clazz == nullptr) {
504
            std::cerr << "Native registration unable to find class " << className << std::endl;
505
            return false;
506
        }
507

    
508
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
509
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
510
            return false;
511
        }
512
        return true;
513
    }
514

    
515
    SaxonApiException *checkAndCreateException(jclass cppClass);
516

    
517

    
518

    
519

    
520

    
521
//        XPathEngine
522
//        XQueryEngine
523
//        SchemaManager
524

    
525
    // static JNIEnv *env;
526
    static int jvmCreatedCPP;
527
    static sxnc_environment *sxn_environ;
528
    std::string cwd; /*!< current working directory */
529
    jobject proc; /*!< Java Processor object */
530

    
531

    
532

    
533
    static void getInfo();
534

    
535
    /*static JavaVM *jvm;*/
536

    
537
protected:
538

    
539

    
540
    jclass xdmAtomicClass;
541
    jclass versionClass;
542
    jclass procClass;
543
    jclass saxonCAPIClass;
544
    std::string cwdV; /*!< current working directory */
545
    //std::string resources_dir; /*!< current Saxon resources directory */
546
    char *versionStr;
547
    std::map<std::string, XdmValue *> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
548
    std::map<std::string, std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */
549
    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  */
550
    bool closed;
551

    
552

    
553
    JNINativeMethod *nativeMethods;
554
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
555
    SaxonApiException *exception;
556

    
557

    
558
private:
559

    
560
    void createException(const char * message=nullptr);
561

    
562

    
563
    void applyConfigurationProperties();
564

    
565
    // Saxon/C method for internal use
566
    static JParameters
567
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
568

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

    
571
    static jobjectArray createJArray(XdmValue **values, int length);
572

    
573
    static const char *checkException();
574
};
575

    
576

    
577
//===============================================================================================
578

    
579

    
580

    
581
#endif /* SAXON_PROCESSOR_H */
(14-14/56)