Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 55b80284

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

    
98
// The Saxon XSLT interface class
99

    
100
typedef struct {
101
    jobjectArray stringArray;
102
    jobjectArray objectArray;
103

    
104
} JParameters;
105

    
106

    
107

    
108
//==========================================
109

    
110

    
111

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

    
116
    friend class XsltProcessor;
117

    
118
    friend class Xslt30Processor;
119

    
120
    friend class XsltExecutable;
121

    
122
    friend class XQueryProcessor;
123

    
124
    friend class SchemaValidator;
125

    
126
    friend class XPathProcessor;
127

    
128
    friend class XdmValue;
129

    
130
    friend class XdmAtomicValue;
131

    
132
    friend class XdmFunctionItem;
133

    
134
    friend class XdmNode;
135

    
136
    friend class XdmMap;
137

    
138
    friend class XdmArray;
139

    
140
public:
141

    
142
    //! A default constructor.
143
    /*!
144
      * Create Saxon Processor.
145
    */
146

    
147
    SaxonProcessor();
148

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

    
154
    SaxonProcessor(const char *configFile);
155

    
156

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

    
164
    SaxonProcessor &operator=(const SaxonProcessor &other);
165

    
166

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

    
173
    /*!
174

175
       * Destructor
176
     */
177
    ~SaxonProcessor();
178

    
179

    
180
    const char * getErrorMessage();
181

    
182

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

    
188
    /*!
189

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

    
195
    /*!
196

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

    
202

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

    
210

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

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

    
226

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

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

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

    
253

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

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

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

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

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

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

    
303
    /**
304
        * Make an XdmArray whose members are from string representation
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(const char **input, int length);
309

    
310

    
311
    /**
312
        * Make an XdmArray whose members are xs:short 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(short *input, int length);
317

    
318

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

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

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

    
340

    
341
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
342

    
343

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

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

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

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

    
365
    int getNodeKind(jobject);
366

    
367
    bool isSchemaAwareProcessor();
368

    
369

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

    
376
    /**
377

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

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

    
391

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

    
397

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

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

    
408

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

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

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

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

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

    
440

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

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

    
455
        JNINativeMethod method;
456
        method.name = name;
457
        method.signature = signature;
458
        method.fnPtr = fnPtr;
459

    
460
        nativeMethodVect.push_back(method);
461

    
462

    
463
    }
464

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

    
476
        }
477

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

    
488

    
489
        }
490
        return false;
491
    }
492

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

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

    
513
    SaxonApiException *checkAndCreateException(jclass cppClass);
514

    
515

    
516

    
517

    
518

    
519
//        XPathEngine
520
//        XQueryEngine
521
//        SchemaManager
522

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

    
529

    
530

    
531
    static void getInfo();
532

    
533
    /*static JavaVM *jvm;*/
534

    
535
protected:
536

    
537

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

    
550

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

    
555

    
556
private:
557

    
558
    void createException(const char * message=nullptr);
559

    
560

    
561
    void applyConfigurationProperties();
562

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

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

    
569
    static jobjectArray createJArray(XdmValue **values, int length);
570

    
571
    static const char *checkException();
572
};
573

    
574

    
575
//===============================================================================================
576

    
577

    
578

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