Project

Profile

Help

Download (15.5 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 7728ae0b

1 72bf04c6 Norman Walsh
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
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 88101661 O'Neil Delpratt
11 55b80284 O'Neil Delpratt
12 72bf04c6 Norman Walsh
#if defined __linux__ || defined __APPLE__
13
14 88101661 O'Neil Delpratt
#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 72bf04c6 Norman Walsh
#else
22 88101661 O'Neil Delpratt
#include <windows.h>
23 72bf04c6 Norman Walsh
#endif
24
25
//#define DEBUG //remove
26
#define CVERSION "1.3.0"
27
#define CVERSION_API_NO 130
28 88101661 O'Neil Delpratt
29 55b80284 O'Neil Delpratt
30
31
32 72bf04c6 Norman Walsh
#include <string>
33
#include <iostream>
34 88101661 O'Neil Delpratt
#include <sstream>
35
#include <map>
36 72bf04c6 Norman Walsh
#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 79d12c83 O'Neil Delpratt
#include "XsltExecutable.h"
44 72bf04c6 Norman Walsh
#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 55b80284 O'Neil Delpratt
#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 7728ae0b O'Neil Delpratt
void operator delete(void*, std::size_t) noexcept;
69
70 55b80284 O'Neil Delpratt
#endif
71
72
73 72bf04c6 Norman Walsh
class XsltProcessor;
74 88101661 O'Neil Delpratt
75 72bf04c6 Norman Walsh
class Xslt30Processor;
76 88101661 O'Neil Delpratt
77 72bf04c6 Norman Walsh
class XQueryProcessor;
78 88101661 O'Neil Delpratt
79 72bf04c6 Norman Walsh
class XPathProcessor;
80 88101661 O'Neil Delpratt
81 72bf04c6 Norman Walsh
class SchemaValidator;
82 88101661 O'Neil Delpratt
83 72bf04c6 Norman Walsh
class XdmValue;
84 88101661 O'Neil Delpratt
85 72bf04c6 Norman Walsh
class XdmNode;
86 88101661 O'Neil Delpratt
87 72bf04c6 Norman Walsh
class XdmItem;
88 88101661 O'Neil Delpratt
89 72bf04c6 Norman Walsh
class XdmAtomicValue;
90
91
class XdmFunctionItem;
92 88101661 O'Neil Delpratt
93 72bf04c6 Norman Walsh
class XdmArray;
94 88101661 O'Neil Delpratt
95 72bf04c6 Norman Walsh
class XdmMap;
96
97 88101661 O'Neil Delpratt
class XsltExecutable;
98 72bf04c6 Norman Walsh
99 01d6fdb6 O'Neil Delpratt
class SaxonApiException;
100
101 72bf04c6 Norman Walsh
102
// The Saxon XSLT interface class
103
104
typedef struct {
105
    jobjectArray stringArray;
106
    jobjectArray objectArray;
107
108 88101661 O'Neil Delpratt
} JParameters;
109 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
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 72bf04c6 Norman Walsh
136 ead48a5d O'Neil Delpratt
    friend class XdmFunctionItem;
137
138
    friend class XdmNode;
139
140
    friend class XdmMap;
141
142
    friend class XdmArray;
143
144 72bf04c6 Norman Walsh
public:
145
146 88101661 O'Neil Delpratt
    //! A default constructor.
147 72bf04c6 Norman Walsh
    /*!
148
      * Create Saxon Processor.
149
    */
150
151
    SaxonProcessor();
152
153 88101661 O'Neil Delpratt
    //! constructor based upon a Saxon configuration file.
154 72bf04c6 Norman Walsh
    /*!
155
      * Create Saxon Processor.
156
    */
157
158 88101661 O'Neil Delpratt
    SaxonProcessor(const char *configFile);
159 72bf04c6 Norman Walsh
160
161 88101661 O'Neil Delpratt
    //! A constructor.
162 72bf04c6 Norman Walsh
    /*!
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 88101661 O'Neil Delpratt
    SaxonProcessor &operator=(const SaxonProcessor &other);
169 72bf04c6 Norman Walsh
170
171 88101661 O'Neil Delpratt
    /**
172
     * Xslt30Processor copy constructor.
173
     * @param other - Xslt30Processor
174
     */
175 72bf04c6 Norman Walsh
    SaxonProcessor(const SaxonProcessor &other);
176
177 88101661 O'Neil Delpratt
    /*!
178 72bf04c6 Norman Walsh

179 88101661 O'Neil Delpratt
       * Destructor
180
     */
181 72bf04c6 Norman Walsh
    ~SaxonProcessor();
182
183
184 8d5b64f3 O'Neil Delpratt
    const char * getErrorMessage();
185
186
187 88101661 O'Neil Delpratt
    //! Get the Processor object. Method used in Python
188
    /* SaxonProcessor * getProcessor(){
189
     return this;
190
     }*/
191
192
    /*!
193 72bf04c6 Norman Walsh

194 88101661 O'Neil Delpratt
       * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
195
       * @return a newly created XsltProcessor
196
     */
197
    XsltProcessor *newXsltProcessor();
198 72bf04c6 Norman Walsh
199 88101661 O'Neil Delpratt
    /*!
200 72bf04c6 Norman Walsh

201 88101661 O'Neil Delpratt
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
202
       * @return a newly created Xslt30Processor
203
     */
204
    Xslt30Processor *newXslt30Processor();
205 72bf04c6 Norman Walsh
206
207
    /*!
208
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
209
     *
210
     * @return a newly created XQueryProcessor
211
     */
212 88101661 O'Neil Delpratt
    XQueryProcessor *newXQueryProcessor();
213 72bf04c6 Norman Walsh
214
215
    /*!
216
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
217
     *
218
     * @return a newly created XPathProcessor
219
     */
220 88101661 O'Neil Delpratt
    XPathProcessor *newXPathProcessor();
221 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    SchemaValidator *newSchemaValidator();
229 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
     * @param value the String value. nullptr is taken as equivalent to "".
236 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
237
     */
238 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(std::string str);
239 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
     * @param value the char pointer array. nullptr is taken as equivalent to "".
245 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
246
     */
247 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(const char *str);
248 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeIntegerValue(int i);
256 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeDoubleValue(double d);
265 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeFloatValue(float);
273 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeLongValue(long l);
281 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeBooleanValue(bool b);
290 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeQNameValue(const char *str);
297 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
306 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(const char **input, int length);
313 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(short *input, int length);
321 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(int *input, int length);
329 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(long *input, int length);
336 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(bool *input, int length);
343 72bf04c6 Norman Walsh
344
345 88101661 O'Neil Delpratt
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
346
347 72bf04c6 Norman Walsh
348 88101661 O'Neil Delpratt
    /**
349
    * Get the string representation of the XdmValue.
350
    * @return char array
351
    */
352
    const char *getStringValue(XdmItem *item);
353 72bf04c6 Norman Walsh
354
    /**
355
     * Parse a lexical representation of the source document and return it as an XdmNode
356
    */
357 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromString(const char *source);
358 72bf04c6 Norman Walsh
359
    /**
360
     * Parse a source document file and return it as an XdmNode.
361
    */
362 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromFile(const char *source);
363 72bf04c6 Norman Walsh
364
    /**
365
     * Parse a source document available by URI and return it as an XdmNode.
366
    */
367 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromUri(const char *source);
368 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
394 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    void setcwd(const char *cwd);
406 72bf04c6 Norman Walsh
407
    /**
408
     * get the current working directory
409
    */
410 88101661 O'Neil Delpratt
    const char *getcwd();
411 72bf04c6 Norman Walsh
412
413
    /**
414
     * set saxon resources directory
415 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
416 72bf04c6 Norman Walsh
    */
417 03fcd3f9 O'Neil Delpratt
    void setResourcesDirectory(const char *dir);
418 88101661 O'Neil Delpratt
419 72bf04c6 Norman Walsh
    /**
420
     * set catalog to be used in Saxon
421
    */
422 88101661 O'Neil Delpratt
    void setCatalog(const char *catalogFile, bool isTracing);
423 72bf04c6 Norman Walsh
424
    /**
425
     * get saxon resources directory
426 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
427 72bf04c6 Norman Walsh
    */
428 766106fe O'Neil Delpratt
    const char * getResourcesDirectory();
429 72bf04c6 Norman Walsh
430
    /**
431
     * Set a configuration property specific to the processor in use. 
432
     * Properties specified here are common across all the processors.
433
     * Example 'l':enable line number has the value 'on' or 'off'
434
     * @param name of the property
435
     * @param value of the property
436
     */
437 88101661 O'Neil Delpratt
    void setConfigurationProperty(const char *name, const char *value);
438 72bf04c6 Norman Walsh
439
    /**
440
     * Clear configuration properties specific to the processor in use. 
441
     */
442 88101661 O'Neil Delpratt
    void clearConfigurationProperties();
443 72bf04c6 Norman Walsh
444
445
    /**
446
     * Get the Saxon version
447
     * @return char array
448
     */
449 88101661 O'Neil Delpratt
    const char *version();
450 72bf04c6 Norman Walsh
451
/*
452
     * Add a native method.
453
     * @param name of the native method
454
     * @param signature of the native method
455
     * @param fnPtr Pointer to the native method
456
 */
457 88101661 O'Neil Delpratt
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
458 72bf04c6 Norman Walsh
459 88101661 O'Neil Delpratt
        JNINativeMethod method;
460
        method.name = name;
461
        method.signature = signature;
462
        method.fnPtr = fnPtr;
463 72bf04c6 Norman Walsh
464 88101661 O'Neil Delpratt
        nativeMethodVect.push_back(method);
465 72bf04c6 Norman Walsh
466
467 88101661 O'Neil Delpratt
    }
468 72bf04c6 Norman Walsh
469
/*
470
     * Register several native methods for one class.
471
     * @param libName name of the library which contains the function(s). Loads the library
472 88101661 O'Neil Delpratt
     * @param gMethods Register native methods. Default is nullptr, also nullptr allowed in
473
     which cause assumption is made the user has added native methods using the method addNativeMethod .
474 72bf04c6 Norman Walsh
 * @return bool success of registered native method
475
 */
476 88101661 O'Neil Delpratt
    bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
477
        if (libName != nullptr) {
478
            setConfigurationProperty("extc", libName);
479
480
        }
481
482
        if (gMethods == nullptr && nativeMethodVect.size() == 0) {
483
            return false;
484
        } else {
485
            if (gMethods == nullptr) {
486
                //copy vector to gMethods
487
                gMethods = new JNINativeMethod[nativeMethodVect.size()];
488
            }
489
            return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
490
                                         gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
491
492
493
        }
494
        return false;
495
    }
496 72bf04c6 Norman Walsh
497
/*
498
 * Register several native methods for one class.
499
 * @return bool success of registered native method
500
 */
501 88101661 O'Neil Delpratt
    static bool registerNativeMethods(JNIEnv *env, const char *className,
502
                                      JNINativeMethod *gMethods, int numMethods) {
503
        jclass clazz;
504
        clazz = env->FindClass(className);
505
        if (clazz == nullptr) {
506
            std::cerr << "Native registration unable to find class " << className << std::endl;
507
            return false;
508
        }
509
510
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
511
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
512
            return false;
513
        }
514
        return true;
515 72bf04c6 Norman Walsh
    }
516
517 88101661 O'Neil Delpratt
    SaxonApiException *checkAndCreateException(jclass cppClass);
518 72bf04c6 Norman Walsh
519
520
521 55b80284 O'Neil Delpratt
522
523 72bf04c6 Norman Walsh
//        XPathEngine
524
//        XQueryEngine
525
//        SchemaManager
526
527 88101661 O'Neil Delpratt
    // static JNIEnv *env;
528 72bf04c6 Norman Walsh
    static int jvmCreatedCPP;
529 88101661 O'Neil Delpratt
    static sxnc_environment *sxn_environ;
530 72bf04c6 Norman Walsh
    std::string cwd; /*!< current working directory */
531
    jobject proc; /*!< Java Processor object */
532 88101661 O'Neil Delpratt
533 55b80284 O'Neil Delpratt
534
535
    static void getInfo();
536
537 72bf04c6 Norman Walsh
    /*static JavaVM *jvm;*/
538
539 88101661 O'Neil Delpratt
protected:
540 72bf04c6 Norman Walsh
541
542 88101661 O'Neil Delpratt
    jclass xdmAtomicClass;
543
    jclass versionClass;
544
    jclass procClass;
545
    jclass saxonCAPIClass;
546
    std::string cwdV; /*!< current working directory */
547
    //std::string resources_dir; /*!< current Saxon resources directory */
548
    char *versionStr;
549
    std::map<std::string, XdmValue *> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
550
    std::map<std::string, std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */
551
    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  */
552
    bool closed;
553 72bf04c6 Norman Walsh
554
555 88101661 O'Neil Delpratt
    JNINativeMethod *nativeMethods;
556
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
557
    SaxonApiException *exception;
558 72bf04c6 Norman Walsh
559
560
private:
561
562 306962bc O'Neil Delpratt
    void createException(const char * message=nullptr);
563
564 72bf04c6 Norman Walsh
565 88101661 O'Neil Delpratt
    void applyConfigurationProperties();
566
567
    // Saxon/C method for internal use
568
    static JParameters
569
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
570
571
    static JParameters createParameterJArray2(std::map<std::string, XdmValue *> parameters);
572
573
    static jobjectArray createJArray(XdmValue **values, int length);
574
575 4ee4355d O'Neil Delpratt
    static const char *checkException();
576 72bf04c6 Norman Walsh
};
577
578 55b80284 O'Neil Delpratt
579 72bf04c6 Norman Walsh
//===============================================================================================
580
581 55b80284 O'Neil Delpratt
582
583 72bf04c6 Norman Walsh
#endif /* SAXON_PROCESSOR_H */