Project

Profile

Help

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

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

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
#endif
69
70
71 72bf04c6 Norman Walsh
class XsltProcessor;
72 88101661 O'Neil Delpratt
73 72bf04c6 Norman Walsh
class Xslt30Processor;
74 88101661 O'Neil Delpratt
75 72bf04c6 Norman Walsh
class XQueryProcessor;
76 88101661 O'Neil Delpratt
77 72bf04c6 Norman Walsh
class XPathProcessor;
78 88101661 O'Neil Delpratt
79 72bf04c6 Norman Walsh
class SchemaValidator;
80 88101661 O'Neil Delpratt
81 72bf04c6 Norman Walsh
class XdmValue;
82 88101661 O'Neil Delpratt
83 72bf04c6 Norman Walsh
class XdmNode;
84 88101661 O'Neil Delpratt
85 72bf04c6 Norman Walsh
class XdmItem;
86 88101661 O'Neil Delpratt
87 72bf04c6 Norman Walsh
class XdmAtomicValue;
88
89
class XdmFunctionItem;
90 88101661 O'Neil Delpratt
91 72bf04c6 Norman Walsh
class XdmArray;
92 88101661 O'Neil Delpratt
93 72bf04c6 Norman Walsh
class XdmMap;
94
95 88101661 O'Neil Delpratt
class XsltExecutable;
96 72bf04c6 Norman Walsh
97
98
// The Saxon XSLT interface class
99
100
typedef struct {
101
    jobjectArray stringArray;
102
    jobjectArray objectArray;
103
104 88101661 O'Neil Delpratt
} JParameters;
105 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
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 72bf04c6 Norman Walsh
132 ead48a5d O'Neil Delpratt
    friend class XdmFunctionItem;
133
134
    friend class XdmNode;
135
136
    friend class XdmMap;
137
138
    friend class XdmArray;
139
140 72bf04c6 Norman Walsh
public:
141
142 88101661 O'Neil Delpratt
    //! A default constructor.
143 72bf04c6 Norman Walsh
    /*!
144
      * Create Saxon Processor.
145
    */
146
147
    SaxonProcessor();
148
149 88101661 O'Neil Delpratt
    //! constructor based upon a Saxon configuration file.
150 72bf04c6 Norman Walsh
    /*!
151
      * Create Saxon Processor.
152
    */
153
154 88101661 O'Neil Delpratt
    SaxonProcessor(const char *configFile);
155 72bf04c6 Norman Walsh
156
157 88101661 O'Neil Delpratt
    //! A constructor.
158 72bf04c6 Norman Walsh
    /*!
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 88101661 O'Neil Delpratt
    SaxonProcessor &operator=(const SaxonProcessor &other);
165 72bf04c6 Norman Walsh
166
167 88101661 O'Neil Delpratt
    /**
168
     * Xslt30Processor copy constructor.
169
     * @param other - Xslt30Processor
170
     */
171 72bf04c6 Norman Walsh
    SaxonProcessor(const SaxonProcessor &other);
172
173 88101661 O'Neil Delpratt
    /*!
174 72bf04c6 Norman Walsh

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

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

197 88101661 O'Neil Delpratt
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
198
       * @return a newly created Xslt30Processor
199
     */
200
    Xslt30Processor *newXslt30Processor();
201 72bf04c6 Norman Walsh
202
203
    /*!
204
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
205
     *
206
     * @return a newly created XQueryProcessor
207
     */
208 88101661 O'Neil Delpratt
    XQueryProcessor *newXQueryProcessor();
209 72bf04c6 Norman Walsh
210
211
    /*!
212
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
213
     *
214
     * @return a newly created XPathProcessor
215
     */
216 88101661 O'Neil Delpratt
    XPathProcessor *newXPathProcessor();
217 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    SchemaValidator *newSchemaValidator();
225 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
     * @param value the String value. nullptr is taken as equivalent to "".
232 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
233
     */
234 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(std::string str);
235 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
     * @param value the char pointer array. nullptr is taken as equivalent to "".
241 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
242
     */
243 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(const char *str);
244 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeIntegerValue(int i);
252 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeDoubleValue(double d);
261 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeFloatValue(float);
269 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeLongValue(long l);
277 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeBooleanValue(bool b);
286 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeQNameValue(const char *str);
293 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
302 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(const char **input, int length);
309 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(short *input, int length);
317 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(int *input, int length);
325 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(long *input, int length);
332 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    XdmArray *makeArray(bool *input, int length);
339 72bf04c6 Norman Walsh
340
341 88101661 O'Neil Delpratt
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
342
343 72bf04c6 Norman Walsh
344 88101661 O'Neil Delpratt
    /**
345
    * Get the string representation of the XdmValue.
346
    * @return char array
347
    */
348
    const char *getStringValue(XdmItem *item);
349 72bf04c6 Norman Walsh
350
    /**
351
     * Parse a lexical representation of the source document and return it as an XdmNode
352
    */
353 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromString(const char *source);
354 72bf04c6 Norman Walsh
355
    /**
356
     * Parse a source document file and return it as an XdmNode.
357
    */
358 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromFile(const char *source);
359 72bf04c6 Norman Walsh
360
    /**
361
     * Parse a source document available by URI and return it as an XdmNode.
362
    */
363 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromUri(const char *source);
364 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
390 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    void setcwd(const char *cwd);
402 72bf04c6 Norman Walsh
403
    /**
404
     * get the current working directory
405
    */
406 88101661 O'Neil Delpratt
    const char *getcwd();
407 72bf04c6 Norman Walsh
408
409
    /**
410
     * set saxon resources directory
411 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
412 72bf04c6 Norman Walsh
    */
413 03fcd3f9 O'Neil Delpratt
    void setResourcesDirectory(const char *dir);
414 88101661 O'Neil Delpratt
415 72bf04c6 Norman Walsh
    /**
416
     * set catalog to be used in Saxon
417
    */
418 88101661 O'Neil Delpratt
    void setCatalog(const char *catalogFile, bool isTracing);
419 72bf04c6 Norman Walsh
420
    /**
421
     * get saxon resources directory
422 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
423 72bf04c6 Norman Walsh
    */
424 766106fe O'Neil Delpratt
    const char * getResourcesDirectory();
425 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    void setConfigurationProperty(const char *name, const char *value);
434 72bf04c6 Norman Walsh
435
    /**
436
     * Clear configuration properties specific to the processor in use. 
437
     */
438 88101661 O'Neil Delpratt
    void clearConfigurationProperties();
439 72bf04c6 Norman Walsh
440
441
    /**
442
     * Get the Saxon version
443
     * @return char array
444
     */
445 88101661 O'Neil Delpratt
    const char *version();
446 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
454 72bf04c6 Norman Walsh
455 88101661 O'Neil Delpratt
        JNINativeMethod method;
456
        method.name = name;
457
        method.signature = signature;
458
        method.fnPtr = fnPtr;
459 72bf04c6 Norman Walsh
460 88101661 O'Neil Delpratt
        nativeMethodVect.push_back(method);
461 72bf04c6 Norman Walsh
462
463 88101661 O'Neil Delpratt
    }
464 72bf04c6 Norman Walsh
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 88101661 O'Neil Delpratt
     * @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 72bf04c6 Norman Walsh
 * @return bool success of registered native method
471
 */
472 88101661 O'Neil Delpratt
    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 72bf04c6 Norman Walsh
493
/*
494
 * Register several native methods for one class.
495
 * @return bool success of registered native method
496
 */
497 88101661 O'Neil Delpratt
    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 72bf04c6 Norman Walsh
    }
512
513 88101661 O'Neil Delpratt
    SaxonApiException *checkAndCreateException(jclass cppClass);
514 72bf04c6 Norman Walsh
515
516
517 55b80284 O'Neil Delpratt
518
519 72bf04c6 Norman Walsh
//        XPathEngine
520
//        XQueryEngine
521
//        SchemaManager
522
523 88101661 O'Neil Delpratt
    // static JNIEnv *env;
524 72bf04c6 Norman Walsh
    static int jvmCreatedCPP;
525 88101661 O'Neil Delpratt
    static sxnc_environment *sxn_environ;
526 72bf04c6 Norman Walsh
    std::string cwd; /*!< current working directory */
527
    jobject proc; /*!< Java Processor object */
528 88101661 O'Neil Delpratt
529 55b80284 O'Neil Delpratt
530
531
    static void getInfo();
532
533 72bf04c6 Norman Walsh
    /*static JavaVM *jvm;*/
534
535 88101661 O'Neil Delpratt
protected:
536 72bf04c6 Norman Walsh
537
538 88101661 O'Neil Delpratt
    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 72bf04c6 Norman Walsh
550
551 88101661 O'Neil Delpratt
    JNINativeMethod *nativeMethods;
552
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
553
    SaxonApiException *exception;
554 72bf04c6 Norman Walsh
555
556
private:
557
558 306962bc O'Neil Delpratt
    void createException(const char * message=nullptr);
559
560 72bf04c6 Norman Walsh
561 88101661 O'Neil Delpratt
    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 4ee4355d O'Neil Delpratt
    static const char *checkException();
572 72bf04c6 Norman Walsh
};
573
574 55b80284 O'Neil Delpratt
575 72bf04c6 Norman Walsh
//===============================================================================================
576
577 55b80284 O'Neil Delpratt
578
579 72bf04c6 Norman Walsh
#endif /* SAXON_PROCESSOR_H */