Project

Profile

Help

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

he / src / main / c / Saxon.C.API / SaxonProcessor.h @ 4ee4355d

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 72bf04c6 Norman Walsh
#if defined __linux__ || defined __APPLE__
12
13 88101661 O'Neil Delpratt
#include <stdlib.h>
14
#include <string>
15
#include <dlfcn.h>
16
17
#define HANDLE void*
18
#define LoadLibrary(x) dlopen(x, RTLD_LAZY)
19
#define GetProcAddress(x, y) dlsym(x,y)
20 72bf04c6 Norman Walsh
#else
21 88101661 O'Neil Delpratt
#include <windows.h>
22 72bf04c6 Norman Walsh
#endif
23
24
//#define DEBUG //remove
25
#define CVERSION "1.3.0"
26
#define CVERSION_API_NO 130
27 88101661 O'Neil Delpratt
28 72bf04c6 Norman Walsh
#include <string>
29
#include <iostream>
30 88101661 O'Neil Delpratt
#include <sstream>
31
#include <map>
32 72bf04c6 Norman Walsh
#include <vector>
33
#include <stdexcept>      // std::logic_error
34
35
#include "SaxonCGlue.h"
36
#include "SaxonCXPath.h"
37
#include "XsltProcessor.h"
38
#include "Xslt30Processor.h"
39 79d12c83 O'Neil Delpratt
#include "XsltExecutable.h"
40 72bf04c6 Norman Walsh
#include "XQueryProcessor.h"
41
#include "XPathProcessor.h"
42
#include "SchemaValidator.h"
43
#include "SaxonApiException.h"
44
//#include "com_saxonica_functions_extfn_PhpCall.h"
45
//#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
46
47
class XsltProcessor;
48 88101661 O'Neil Delpratt
49 72bf04c6 Norman Walsh
class Xslt30Processor;
50 88101661 O'Neil Delpratt
51 72bf04c6 Norman Walsh
class XQueryProcessor;
52 88101661 O'Neil Delpratt
53 72bf04c6 Norman Walsh
class XPathProcessor;
54 88101661 O'Neil Delpratt
55 72bf04c6 Norman Walsh
class SchemaValidator;
56 88101661 O'Neil Delpratt
57 72bf04c6 Norman Walsh
class XdmValue;
58 88101661 O'Neil Delpratt
59 72bf04c6 Norman Walsh
class XdmNode;
60 88101661 O'Neil Delpratt
61 72bf04c6 Norman Walsh
class XdmItem;
62 88101661 O'Neil Delpratt
63 72bf04c6 Norman Walsh
class XdmAtomicValue;
64
65
class XdmFunctionItem;
66 88101661 O'Neil Delpratt
67 72bf04c6 Norman Walsh
class XdmArray;
68 88101661 O'Neil Delpratt
69 72bf04c6 Norman Walsh
class XdmMap;
70
71 88101661 O'Neil Delpratt
class XsltExecutable;
72 72bf04c6 Norman Walsh
73
74
// The Saxon XSLT interface class
75
76
typedef struct {
77
    jobjectArray stringArray;
78
    jobjectArray objectArray;
79
80 88101661 O'Neil Delpratt
} JParameters;
81 72bf04c6 Norman Walsh
82
83
84
//==========================================
85
86
87
88
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
89
 */
90
class SaxonProcessor {
91 88101661 O'Neil Delpratt
92
    friend class XsltProcessor;
93
94
    friend class Xslt30Processor;
95
96
    friend class XsltExecutable;
97
98
    friend class XQueryProcessor;
99
100
    friend class SchemaValidator;
101
102
    friend class XPathProcessor;
103
104
    friend class XdmValue;
105
106
    friend class XdmAtomicValue;
107 72bf04c6 Norman Walsh
108 ead48a5d O'Neil Delpratt
    friend class XdmFunctionItem;
109
110
    friend class XdmNode;
111
112
    friend class XdmMap;
113
114
    friend class XdmArray;
115
116 72bf04c6 Norman Walsh
public:
117
118 88101661 O'Neil Delpratt
    //! A default constructor.
119 72bf04c6 Norman Walsh
    /*!
120
      * Create Saxon Processor.
121
    */
122
123
    SaxonProcessor();
124
125 88101661 O'Neil Delpratt
    //! constructor based upon a Saxon configuration file.
126 72bf04c6 Norman Walsh
    /*!
127
      * Create Saxon Processor.
128
    */
129
130 88101661 O'Neil Delpratt
    SaxonProcessor(const char *configFile);
131 72bf04c6 Norman Walsh
132
133 88101661 O'Neil Delpratt
    //! A constructor.
134 72bf04c6 Norman Walsh
    /*!
135
      * Create Saxon Processor.
136
      * @param l - Flag that a license is to be used. Default is false.        
137
    */
138
    SaxonProcessor(bool l);
139
140 88101661 O'Neil Delpratt
    SaxonProcessor &operator=(const SaxonProcessor &other);
141 72bf04c6 Norman Walsh
142
143 88101661 O'Neil Delpratt
    /**
144
     * Xslt30Processor copy constructor.
145
     * @param other - Xslt30Processor
146
     */
147 72bf04c6 Norman Walsh
    SaxonProcessor(const SaxonProcessor &other);
148
149 88101661 O'Neil Delpratt
    /*!
150 72bf04c6 Norman Walsh

151 88101661 O'Neil Delpratt
       * Destructor
152
     */
153 72bf04c6 Norman Walsh
    ~SaxonProcessor();
154
155
156 8d5b64f3 O'Neil Delpratt
    const char * getErrorMessage();
157
158
159 88101661 O'Neil Delpratt
    //! Get the Processor object. Method used in Python
160
    /* SaxonProcessor * getProcessor(){
161
     return this;
162
     }*/
163
164
    /*!
165 72bf04c6 Norman Walsh

166 88101661 O'Neil Delpratt
       * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
167
       * @return a newly created XsltProcessor
168
     */
169
    XsltProcessor *newXsltProcessor();
170 72bf04c6 Norman Walsh
171 88101661 O'Neil Delpratt
    /*!
172 72bf04c6 Norman Walsh

173 88101661 O'Neil Delpratt
       * Create an Xslt30Processor. An Xslt30Processor is used to compile XSLT30 stylesheets.
174
       * @return a newly created Xslt30Processor
175
     */
176
    Xslt30Processor *newXslt30Processor();
177 72bf04c6 Norman Walsh
178
179
    /*!
180
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
181
     *
182
     * @return a newly created XQueryProcessor
183
     */
184 88101661 O'Neil Delpratt
    XQueryProcessor *newXQueryProcessor();
185 72bf04c6 Norman Walsh
186
187
    /*!
188
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
189
     *
190
     * @return a newly created XPathProcessor
191
     */
192 88101661 O'Neil Delpratt
    XPathProcessor *newXPathProcessor();
193 72bf04c6 Norman Walsh
194
    /*!
195
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
196
     * SchemaManager
197
     *
198
     * @return a new SchemaValidator
199
     */
200 88101661 O'Neil Delpratt
    SchemaValidator *newSchemaValidator();
201 72bf04c6 Norman Walsh
202
203
    /*!
204
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
205
     * of a zero-length string (and potentially other strings, in future)
206
     *
207 88101661 O'Neil Delpratt
     * @param value the String value. nullptr is taken as equivalent to "".
208 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
209
     */
210 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(std::string str);
211 72bf04c6 Norman Walsh
212
    /*!
213
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
214
     * of a zero-length string (and potentially other strings, in future)
215
     *
216 88101661 O'Neil Delpratt
     * @param value the char pointer array. nullptr is taken as equivalent to "".
217 72bf04c6 Norman Walsh
     * @return the corresponding StringValue
218
     */
219 88101661 O'Neil Delpratt
    XdmAtomicValue *makeStringValue(const char *str);
220 72bf04c6 Norman Walsh
221
    /*!
222
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
223
     *
224
     * @param i the supplied primitive integer value
225
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
226
     */
227 88101661 O'Neil Delpratt
    XdmAtomicValue *makeIntegerValue(int i);
228 72bf04c6 Norman Walsh
229
230
    /*!
231
     * Factory method (for convenience in compiled bytecode)
232
     *
233
     * @param d the value of the double
234
     * @return a new XdmAtomicValue
235
     */
236 88101661 O'Neil Delpratt
    XdmAtomicValue *makeDoubleValue(double d);
237 72bf04c6 Norman Walsh
238
    /*!
239
     * Factory method (for convenience in compiled bytecode)
240
     *
241
     * @param f the value of the foat
242
     * @return a new XdmAtomicValue
243
     */
244 88101661 O'Neil Delpratt
    XdmAtomicValue *makeFloatValue(float);
245 72bf04c6 Norman Walsh
246
    /*!
247
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
248
     *
249
     * @param l the supplied primitive long value
250
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
251
     */
252 88101661 O'Neil Delpratt
    XdmAtomicValue *makeLongValue(long l);
253 72bf04c6 Norman Walsh
254
    /*!
255
     * Factory method: makes a XdmAtomicValue representing a boolean Value
256
     *
257
     * @param b true or false, to determine which boolean value is
258
     *              required
259
     * @return the XdmAtomicValue requested
260
     */
261 88101661 O'Neil Delpratt
    XdmAtomicValue *makeBooleanValue(bool b);
262 72bf04c6 Norman Walsh
263
    /**
264
     * Create an QName Xdm value from string representation in clark notation
265
     * @param str - The value given in a string form in clark notation. {uri}local
266
     * @return XdmAtomicValue - value
267
    */
268 88101661 O'Neil Delpratt
    XdmAtomicValue *makeQNameValue(const char *str);
269 72bf04c6 Norman Walsh
270
    /*!
271
     * Create an Xdm Atomic value from string representation
272
     * @param type    - Local name of a type in the XML Schema namespace.
273
     * @param value - The value given in a string form.
274
     * In the case of a QName the value supplied must be in clark notation. {uri}local
275
     * @return XdmValue - value
276
    */
277 88101661 O'Neil Delpratt
    XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
278 72bf04c6 Norman Walsh
279
#if CVERSION_API_NO >= 123
280 88101661 O'Neil Delpratt
281 72bf04c6 Norman Walsh
    /**
282
        * Make an XdmArray whose members are from string representation
283
        * @param input the input array of booleans
284
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
285
   */
286 88101661 O'Neil Delpratt
    XdmArray *makeArray(const char **input, int length);
287 72bf04c6 Norman Walsh
288
289
    /**
290
        * Make an XdmArray whose members are xs:short values
291
        * @param input the input array of booleans
292
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
293
   */
294 88101661 O'Neil Delpratt
    XdmArray *makeArray(short *input, int length);
295 72bf04c6 Norman Walsh
296
297
    /**
298
        * Make an XdmArray whose members are xs:int values
299
        * @param input the input array of booleans
300
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
301
   */
302 88101661 O'Neil Delpratt
    XdmArray *makeArray(int *input, int length);
303 72bf04c6 Norman Walsh
304
    /**
305
        * Make an XdmArray whose members are xs:long values
306
        * @param input the input array of booleans
307
        * @return an XdmArray whose members are xs:boolean values corresponding one-to-one with the input
308
   */
309 88101661 O'Neil Delpratt
    XdmArray *makeArray(long *input, int length);
310 72bf04c6 Norman Walsh
311
    /**
312
        * Make an XdmArray whose members are xs:boolean 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(bool *input, int length);
317 72bf04c6 Norman Walsh
318
319 88101661 O'Neil Delpratt
    XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
320
321 72bf04c6 Norman Walsh
322
#endif
323
324 88101661 O'Neil Delpratt
    /**
325
    * Get the string representation of the XdmValue.
326
    * @return char array
327
    */
328
    const char *getStringValue(XdmItem *item);
329 72bf04c6 Norman Walsh
330
    /**
331
     * Parse a lexical representation of the source document and return it as an XdmNode
332
    */
333 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromString(const char *source);
334 72bf04c6 Norman Walsh
335
    /**
336
     * Parse a source document file and return it as an XdmNode.
337
    */
338 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromFile(const char *source);
339 72bf04c6 Norman Walsh
340
    /**
341
     * Parse a source document available by URI and return it as an XdmNode.
342
    */
343 88101661 O'Neil Delpratt
    XdmNode *parseXmlFromUri(const char *source);
344 72bf04c6 Norman Walsh
345
    int getNodeKind(jobject);
346
347
    bool isSchemaAwareProcessor();
348
349
350
    /**
351
     * Checks for thrown exceptions
352
     * @return bool - true when there is a pending exception; otherwise return false
353
    */
354
    bool exceptionOccurred();
355
356
    /**
357

358
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
359
    */
360
    void exceptionClear();
361
362
    /**
363
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
364
     * @param env
365
     * @param callingClass
366
     * @param callingObject
367
     * @return SaxonApiException
368
    */
369 88101661 O'Neil Delpratt
    static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
370 72bf04c6 Norman Walsh
371
372
    /*
373
      * Clean up and destroy Java VM to release memory used. 
374
     */
375
    static void release();
376
377
378
    /**
379
     * set the current working directory
380
    */
381 88101661 O'Neil Delpratt
    void setcwd(const char *cwd);
382 72bf04c6 Norman Walsh
383
    /**
384
     * get the current working directory
385
    */
386 88101661 O'Neil Delpratt
    const char *getcwd();
387 72bf04c6 Norman Walsh
388
389
    /**
390
     * set saxon resources directory
391 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
392 72bf04c6 Norman Walsh
    */
393 03fcd3f9 O'Neil Delpratt
    void setResourcesDirectory(const char *dir);
394 88101661 O'Neil Delpratt
395 72bf04c6 Norman Walsh
    /**
396
     * set catalog to be used in Saxon
397
    */
398 88101661 O'Neil Delpratt
    void setCatalog(const char *catalogFile, bool isTracing);
399 72bf04c6 Norman Walsh
400
    /**
401
     * get saxon resources directory
402 88101661 O'Neil Delpratt
     * @deprecated - It is no longer required to explicitly specifiy the resource directory
403 72bf04c6 Norman Walsh
    */
404 766106fe O'Neil Delpratt
    const char * getResourcesDirectory();
405 72bf04c6 Norman Walsh
406
    /**
407
     * Set a configuration property specific to the processor in use. 
408
     * Properties specified here are common across all the processors.
409
     * Example 'l':enable line number has the value 'on' or 'off'
410
     * @param name of the property
411
     * @param value of the property
412
     */
413 88101661 O'Neil Delpratt
    void setConfigurationProperty(const char *name, const char *value);
414 72bf04c6 Norman Walsh
415
    /**
416
     * Clear configuration properties specific to the processor in use. 
417
     */
418 88101661 O'Neil Delpratt
    void clearConfigurationProperties();
419 72bf04c6 Norman Walsh
420
421
    /**
422
     * Get the Saxon version
423
     * @return char array
424
     */
425 88101661 O'Neil Delpratt
    const char *version();
426 72bf04c6 Norman Walsh
427
/*
428
     * Add a native method.
429
     * @param name of the native method
430
     * @param signature of the native method
431
     * @param fnPtr Pointer to the native method
432
 */
433 88101661 O'Neil Delpratt
    void addNativeMethod(char *name, char *signature, void *fnPtr) {
434 72bf04c6 Norman Walsh
435 88101661 O'Neil Delpratt
        JNINativeMethod method;
436
        method.name = name;
437
        method.signature = signature;
438
        method.fnPtr = fnPtr;
439 72bf04c6 Norman Walsh
440 88101661 O'Neil Delpratt
        nativeMethodVect.push_back(method);
441 72bf04c6 Norman Walsh
442
443 88101661 O'Neil Delpratt
    }
444 72bf04c6 Norman Walsh
445
/*
446
     * Register several native methods for one class.
447
     * @param libName name of the library which contains the function(s). Loads the library
448 88101661 O'Neil Delpratt
     * @param gMethods Register native methods. Default is nullptr, also nullptr allowed in
449
     which cause assumption is made the user has added native methods using the method addNativeMethod .
450 72bf04c6 Norman Walsh
 * @return bool success of registered native method
451
 */
452 88101661 O'Neil Delpratt
    bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
453
        if (libName != nullptr) {
454
            setConfigurationProperty("extc", libName);
455
456
        }
457
458
        if (gMethods == nullptr && nativeMethodVect.size() == 0) {
459
            return false;
460
        } else {
461
            if (gMethods == nullptr) {
462
                //copy vector to gMethods
463
                gMethods = new JNINativeMethod[nativeMethodVect.size()];
464
            }
465
            return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
466
                                         gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
467
468
469
        }
470
        return false;
471
    }
472 72bf04c6 Norman Walsh
473
/*
474
 * Register several native methods for one class.
475
 * @return bool success of registered native method
476
 */
477 88101661 O'Neil Delpratt
    static bool registerNativeMethods(JNIEnv *env, const char *className,
478
                                      JNINativeMethod *gMethods, int numMethods) {
479
        jclass clazz;
480
        clazz = env->FindClass(className);
481
        if (clazz == nullptr) {
482
            std::cerr << "Native registration unable to find class " << className << std::endl;
483
            return false;
484
        }
485
486
        if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
487
            // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
488
            return false;
489
        }
490
        return true;
491 72bf04c6 Norman Walsh
    }
492
493 88101661 O'Neil Delpratt
    SaxonApiException *checkAndCreateException(jclass cppClass);
494 72bf04c6 Norman Walsh
495
496
497
//        XPathEngine
498
//        XQueryEngine
499
//        SchemaManager
500
501 88101661 O'Neil Delpratt
    // static JNIEnv *env;
502 72bf04c6 Norman Walsh
    static int jvmCreatedCPP;
503 88101661 O'Neil Delpratt
    static sxnc_environment *sxn_environ;
504 72bf04c6 Norman Walsh
    std::string cwd; /*!< current working directory */
505
    jobject proc; /*!< Java Processor object */
506 88101661 O'Neil Delpratt
507 72bf04c6 Norman Walsh
    /*static JavaVM *jvm;*/
508
509 88101661 O'Neil Delpratt
protected:
510 72bf04c6 Norman Walsh
511
512 88101661 O'Neil Delpratt
    jclass xdmAtomicClass;
513
    jclass versionClass;
514
    jclass procClass;
515
    jclass saxonCAPIClass;
516
    std::string cwdV; /*!< current working directory */
517
    //std::string resources_dir; /*!< current Saxon resources directory */
518
    char *versionStr;
519
    std::map<std::string, XdmValue *> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
520
    std::map<std::string, std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */
521
    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  */
522
    bool closed;
523 72bf04c6 Norman Walsh
524
525 88101661 O'Neil Delpratt
    JNINativeMethod *nativeMethods;
526
    std::vector<JNINativeMethod> nativeMethodVect; /*!< Vector of native methods defined by user */
527
    SaxonApiException *exception;
528 72bf04c6 Norman Walsh
529
530
private:
531
532
533 88101661 O'Neil Delpratt
    void applyConfigurationProperties();
534
535
    // Saxon/C method for internal use
536
    static JParameters
537
    createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties);
538
539
    static JParameters createParameterJArray2(std::map<std::string, XdmValue *> parameters);
540
541
    static jobjectArray createJArray(XdmValue **values, int length);
542
543 4ee4355d O'Neil Delpratt
    static const char *checkException();
544 72bf04c6 Norman Walsh
};
545
546
//===============================================================================================
547
548
#endif /* SAXON_PROCESSOR_H */