Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 6af44631

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
void operator delete(void*, std::size_t) noexcept;
69

    
70
#endif
71

    
72

    
73
class XsltProcessor;
74

    
75
class Xslt30Processor;
76

    
77
class XQueryProcessor;
78

    
79
class XPathProcessor;
80

    
81
class SchemaValidator;
82

    
83
class XdmValue;
84

    
85
class XdmNode;
86

    
87
class XdmItem;
88

    
89
class XdmAtomicValue;
90

    
91
class XdmFunctionItem;
92

    
93
class XdmArray;
94

    
95
class XdmMap;
96

    
97
class XsltExecutable;
98

    
99
class SaxonApiException;
100

    
101

    
102
// The Saxon XSLT interface class
103

    
104
typedef struct {
105
    jobjectArray stringArray;
106
    jobjectArray objectArray;
107

    
108
} JParameters;
109

    
110

    
111

    
112
//==========================================
113

    
114

    
115

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

    
120
    friend class XsltProcessor;
121

    
122
    friend class Xslt30Processor;
123

    
124
    friend class XsltExecutable;
125

    
126
    friend class XQueryProcessor;
127

    
128
    friend class SchemaValidator;
129

    
130
    friend class XPathProcessor;
131

    
132
    friend class XdmValue;
133

    
134
    friend class XdmAtomicValue;
135

    
136
    friend class XdmFunctionItem;
137

    
138
    friend class XdmNode;
139

    
140
    friend class XdmMap;
141

    
142
    friend class XdmArray;
143

    
144
public:
145

    
146
    //! A default constructor.
147
    /*!
148
      * Create Saxon Processor.
149
    */
150

    
151
    SaxonProcessor();
152

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

    
158
    SaxonProcessor(const char *configFile);
159

    
160

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

    
168
    SaxonProcessor &operator=(const SaxonProcessor &other);
169

    
170

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

    
177
    /*!
178

179
       * Destructor
180
     */
181
    ~SaxonProcessor();
182

    
183

    
184
    const char * getErrorMessage();
185

    
186

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

    
192
    /*!
193

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

    
199
    /*!
200

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

    
206

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

    
214

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

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

    
230

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

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

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

    
257

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

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

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

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

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

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

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

    
314

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

    
322

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

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

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

    
344

    
345
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
346

    
347

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

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

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

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

    
369
    int getNodeKind(jobject);
370

    
371
    bool isSchemaAwareProcessor();
372

    
373

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

    
380
    /**
381

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

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

    
395

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

    
401

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

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

    
412
    /**
413
     * set catalog to be used in Saxon
414
    */
415
    void setCatalog(const char *catalogFile, bool isTracing);
416

    
417
    /**
418
     * get saxon resources directory
419
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
420
    */
421
    const char * getResourcesDirectory();
422

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

    
432
    /**
433
     * Clear configuration properties specific to the processor in use. 
434
     */
435
    void clearConfigurationProperties();
436

    
437

    
438
    /**
439
     * Get the Saxon version
440
     * @return char array
441
     */
442
    const char *version();
443

    
444
/*
445
     * Add a native method.
446
     * @param name of the native method
447
     * @param signature of the native method
448
     * @param fnPtr Pointer to the native method
449
 */
450
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
451

    
452
        JNINativeMethod method;
453
        method.name = name;
454
        method.signature = signature;
455
        method.fnPtr = fnPtr;
456

    
457
        nativeMethodVect.push_back(method);
458

    
459

    
460
    }
461

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

    
473
        }
474

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

    
485

    
486
        }
487
        return false;
488
    }
489

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

    
503
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
504
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
505
            return false;
506
        }
507
        return true;
508
    }
509

    
510
    SaxonApiException *checkAndCreateException(jclass cppClass);
511

    
512

    
513

    
514

    
515

    
516
//        XPathEngine
517
//        XQueryEngine
518
//        SchemaManager
519

    
520
    // static JNIEnv *env;
521
    static int jvmCreatedCPP;
522
    static sxnc_environment *sxn_environ;
523
    std::string cwd; /*!< current working directory */
524
    jobject proc; /*!< Java Processor object */
525

    
526

    
527

    
528
    static void getInfo();
529

    
530
    /*static JavaVM *jvm;*/
531

    
532
protected:
533

    
534

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

    
547

    
548
    JNINativeMethod *nativeMethods;
549
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
550
    SaxonApiException *exception;
551

    
552

    
553
private:
554

    
555
    void createException(const char * message=nullptr);
556

    
557

    
558
    void applyConfigurationProperties();
559

    
560
    // Saxon/C method for internal use
561
    static JParameters
562
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
563

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

    
566
    static jobjectArray createJArray(XdmValue **values, int length);
567

    
568
    static const char *checkException();
569
};
570

    
571

    
572
//===============================================================================================
573

    
574

    
575

    
576
#endif /* SAXON_PROCESSOR_H */
(14-14/51)