Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / SaxonProcessor.h @ 4e3fba42

1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright (c) 2016 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
#include <jni.h>
11

    
12
        
13
#if defined __linux__ || defined __APPLE__
14
        #include <stdlib.h>
15
        #include <string.h>
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

    
27
#include <string>
28
#include <iostream>
29
#include <sstream>  
30
#include <map>        
31
#include <vector>
32
#include <stdexcept>      // std::logic_error
33

    
34
#include "SaxonCGlue.h"
35
#include "SaxonCXPath.h"
36
#include "XsltProcessor.h"
37
#include "XQueryProcessor.h"
38
#include "XPathProcessor.h"
39
#include "SchemaValidator.h"
40

    
41
class XsltProcessor;
42
class XQueryProcessor;
43
class XPathProcessor;
44
class SchemaValidator;
45
class XdmValue;
46
class XdmNode;
47
class XdmItem;
48
class XdmAtomicValue;
49

    
50

    
51

    
52
// The Saxon XSLT interface class
53

    
54
//std::mutex mtx;
55
/*! <code>MyException</code>. This struct captures details of the Java exception thrown from Saxon s9api API (Java).
56
 * <p/>
57
 */
58
typedef struct {
59
                std::string errorCode;
60
                std::string errorMessage;
61
                int linenumber;
62
                    bool isType;
63
                    bool isStatic;
64
                    bool isGlobal;
65
        }MyException;
66

    
67

    
68

    
69

    
70
/*! <code>SaxonApiException</code>. An exception thrown by the Saxon s9api API (Java). This is always a C++ wrapper for some other underlying exception in Java
71
 * <p/>
72
 */
73
class SaxonApiException {
74

    
75
public:
76

    
77
    /**
78
     * A default Constructor. Create a SaxonApiException
79
     */
80
     SaxonApiException(){}
81

    
82
    /**
83
     * A Copy constructor. Create a SaxonApiException
84
     * @param ex - The exception object to copy
85
     */
86
        SaxonApiException(const SaxonApiException &ex){
87
                exceptions = ex.exceptions;
88
        }
89

    
90
    /**
91
     * A constructor. Create a SaxonApiException
92
     * @param ec - The error code of the underlying exception thrown, if known
93
     * @param exM - The error message of the underlying exception thrown, if known
94
     */
95
        SaxonApiException(const char * ec, const char * exM){
96
                MyException newEx;        
97
                if(ec != NULL){
98
                        newEx.errorCode =   std::string(ec);
99
                } else {
100
                        newEx.errorCode ="Unknown";        
101
                }
102
                if(exM != NULL){
103
                        newEx.errorMessage =  std::string(exM);
104
                } else {
105
                        newEx.errorMessage="Unkown";                
106
                }
107
                newEx.isType = false;
108
                    newEx.isStatic = false;
109
                    newEx.isGlobal = false;
110
                newEx.linenumber = 0;
111
                exceptions.push_back(newEx);
112
        }
113

    
114
    /**
115
     * A constructor. Create a SaxonApiException
116
     * @param ec - The error code of the underlying exception thrown, if known
117
     * @param exM - The error message of the underlying exception thrown, if known
118
     * @param typeErr - Flag indicating if the error is a type error
119
     * @param stat - Flag indicating a static error
120
     * @param glob - Flag for if the error is global
121
     * @param l - Line number information of where the error occurred
122
     */
123
        SaxonApiException(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
124
                MyException newEx;
125
                if(ec != NULL){
126
                        newEx.errorCode =   std::string(ec);
127
                } else {
128
                        newEx.errorCode ="ERROR1";        
129
                }
130
                if(exM != NULL){
131
                        newEx.errorMessage =  std::string(exM);
132
                } else {
133
                        newEx.errorMessage="ERROR2";                
134
                }
135
                newEx.isType = typeErr;
136
                    newEx.isStatic = stat;
137
                    newEx.isGlobal = glob;
138
                newEx.linenumber = l;
139
                exceptions.push_back(newEx);
140
        }
141

    
142
    /**
143
     * Creates a SaxonApiException and adds it to a vector of exceptions
144
     * @param ec - The error code of the underlying exception thrown, if known
145
     * @param exM - The error message of the underlying exception thrown, if known
146
     * @param typeErr - Flag indicating if the error is a type error
147
     * @param stat - Flag indicating a static error
148
     * @param glob - Flag for if the error is global
149
     * @param l - Line number information of where the error occurred
150
     */
151
        void add(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
152
                MyException newEx;
153
                if(ec != NULL){
154
                        newEx.errorCode =   std::string(ec);
155
                } else {
156
                        newEx.errorCode ="ERROR1";        
157
                }
158
                if(exM != NULL){
159
                        newEx.errorMessage =  std::string(exM);
160
                } else {
161
                        newEx.errorMessage="ERROR2";                
162
                }
163
                newEx.isType = typeErr;
164
                    newEx.isStatic = stat;
165
                    newEx.isGlobal = glob;
166
                newEx.linenumber = l;
167
                exceptions.push_back(newEx);
168
        }
169

    
170

    
171
    /**
172
     * A destructor.
173
     */
174
        ~SaxonApiException(){ 
175
          exceptions.clear();
176
        }
177

    
178
    /**
179
     * Get the error code associated with the ith exception in the vector, if there is one
180
     * @param i - ith exception in the vector
181
     * @return the associated error code, or null if no error code is available
182
     */
183
        const char * getErrorCode(int i){
184
                if(i <= exceptions.size()){
185
                        return exceptions[i].errorCode.c_str();
186
                }
187
                return NULL;
188
        }
189

    
190

    
191
        int getLineNumber(int i){
192
                if(i <= exceptions.size()){
193
                        return exceptions[i].linenumber;        
194
                }
195
                return 0;
196
        }
197

    
198
        bool isGlobalError(int i){
199
                if(i <= exceptions.size()){
200
                        return exceptions[i].isGlobal;
201
                }
202
                return false;
203
        }
204

    
205
        bool isStaticError(int i){
206
                if(i <= exceptions.size()){
207
                        return exceptions[i].isStatic;
208
                }
209
                return false;
210
        }
211

    
212
        bool isTypeError(int i){
213
                if(i <= exceptions.size()){
214
                        return exceptions[i].isType;
215
                }
216
                return NULL;
217
        }
218

    
219
        void clear(){
220
          for(int i =0; i< exceptions.size();i++) {
221
                exceptions[i].errorCode.clear();
222
                exceptions[i].errorMessage.clear();        
223
          }
224
          exceptions.clear();
225
        }
226

    
227
        int count(){
228
                return exceptions.size();        
229
        }
230

    
231
    /**
232
     * Returns the detail message string of the ith throwable, if there is one
233
     * @param i - ith exception in the vector
234
     * @return the detail message string of this <tt>Throwable</tt> instance
235
     *         (which may be <tt>null</tt>).
236
     */
237
        const char * getErrorMessage(int i){
238
                if(i <= exceptions.size()){
239
                        return exceptions[i].errorMessage.c_str();
240
                }
241
                return NULL;
242
        }
243

    
244
    /**
245
     * Returns the ith Exception added, if there is one
246
     * @param i - ith exception in the vector
247
     * @return MyException
248
     */
249
        MyException getException(int i){
250
                if(i <= exceptions.size()){
251
                        return exceptions[i];        
252
                }
253
                throw 0;
254
        }
255

    
256
private:
257
        std::vector<MyException> exceptions; /*!< Capture exceptions in a std:vector */
258
};
259

    
260

    
261

    
262

    
263

    
264

    
265

    
266

    
267

    
268

    
269
//==========================================
270

    
271

    
272

    
273
/*! An <code>SaxonProcessor</code> acts as a factory for generating XQuery, XPath, Schema and XSLT compilers
274
 * In this alpha release only the XSLT compiler is available
275
 * <p/>
276
 */
277
class SaxonProcessor {
278
friend  class XsltProcessor;
279
friend  class XQueryProcessor;
280
friend class SchemaValidator;
281
friend class XPathProcessor;
282
friend class XdmValue;
283
friend class XdmAtomicValue;
284
public:
285

    
286
   //! A default constructor.
287
    /*!
288
      * Create Saxon Processor.
289
    */
290

    
291
    SaxonProcessor();
292

    
293
   //! constructor based upon a Saxon configuration file.
294
    /*!
295
      * Create Saxon Processor.
296
    */
297

    
298
    SaxonProcessor(const char * configFile);
299

    
300

    
301
   //! A constructor.
302
    /*!
303
      * Create Saxon Processor.
304
      * @param l - Flag that a license is to be used. Default is false.        
305
    */
306
    SaxonProcessor(bool l);
307

    
308
    SaxonProcessor& operator=( const SaxonProcessor& other );
309

    
310
   /*!
311

312
      * Destructor
313
    */
314
    ~SaxonProcessor();
315
        
316
   /*!
317

318
      * Create an XsltProcessor. An XsltProcessor is used to compile XSLT stylesheets.
319
      * @return a newly created XsltProcessor        
320
    */        
321
    XsltProcessor * newXsltProcessor();
322

    
323
    /*!
324
     * Create an XQueryProcessor. An XQueryProcessor is used to compile XQuery queries.
325
     *
326
     * @return a newly created XQueryProcessor
327
     */
328
    XQueryProcessor * newXQueryProcessor();
329

    
330

    
331
    /*!
332
     * Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
333
     *
334
     * @return a newly created XPathProcessor
335
     */
336
    XPathProcessor * newXPathProcessor();
337

    
338
    /*!
339
     * Create a SchemaValidator which can be used to validate instance documents against the schema held by this
340
     * SchemaManager
341
     *
342
     * @return a new SchemaValidator
343
     */
344
    SchemaValidator * newSchemaValidator();
345

    
346

    
347
    /*!
348
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
349
     * of a zero-length string (and potentially other strings, in future)
350
     *
351
     * @param value the String value. Null is taken as equivalent to "".
352
     * @return the corresponding StringValue
353
     */
354
    XdmAtomicValue * makeStringValue(std::string str);
355

    
356
    /*!
357
     * Factory method. Unlike the constructor, this avoids creating a new StringValue in the case
358
     * of a zero-length string (and potentially other strings, in future)
359
     *
360
     * @param value the char pointer array. Null is taken as equivalent to "".
361
     * @return the corresponding StringValue
362
     */
363
    XdmAtomicValue * makeStringValue(const char * str);
364

    
365
    /*!
366
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
367
     *
368
     * @param i the supplied primitive integer value
369
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
370
     */
371
    XdmAtomicValue * makeIntegerValue(int i);
372

    
373

    
374
    /*!
375
     * Factory method (for convenience in compiled bytecode)
376
     *
377
     * @param d the value of the double
378
     * @return a new XdmAtomicValue
379
     */
380
    XdmAtomicValue * makeDoubleValue(double d);
381

    
382
    /*!
383
     * Factory method (for convenience in compiled bytecode)
384
     *
385
     * @param f the value of the foat
386
     * @return a new XdmAtomicValue
387
     */
388
    XdmAtomicValue * makeFloatValue(float);
389

    
390
    /*!
391
     * Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied
392
     *
393
     * @param l the supplied primitive long value
394
     * @return the value as a XdmAtomicValue which is a BigIntegerValue or Int64Value as appropriate
395
     */
396
    XdmAtomicValue * makeLongValue(long l);
397

    
398
    /*!
399
     * Factory method: makes a XdmAtomicValue representing a boolean Value
400
     *
401
     * @param b true or false, to determine which boolean value is
402
     *              required
403
     * @return the XdmAtomicValue requested
404
     */
405
    XdmAtomicValue * makeBooleanValue(bool b);
406

    
407
    /**
408
     * Create an QName Xdm value from string representation in clark notation
409
     * @param valueStr - The value given in a string form in clark notation. {uri}local
410
     * @return XdmAtomicValue - value
411
    */
412
    XdmAtomicValue * makeQNameValue(std::string str);
413

    
414
    /*!
415
     * Create an Xdm Atomic value from string representation
416
     * @param type    - Local name of a type in the XML Schema namespace.
417
     * @param value - The value given in a string form.
418
     * In the case of a QName the value supplied must be in clark notation. {uri}local
419
     * @return XdmValue - value
420
    */
421
    XdmAtomicValue * makeAtomicValue(std::string type, std::string value);
422

    
423
     /**
424
     * Get the string representation of the XdmValue.
425
     * @return char array
426
     */
427
    const char * getStringValue(XdmItem * item);
428

    
429
    /**
430
     * Parse a lexical representation of the source document and return it as an XdmNode
431
    */
432
    XdmNode * parseXmlFromString(const char* source);
433

    
434
    /**
435
     * Parse a source document file and return it as an XdmNode.
436
    */
437
    XdmNode * parseXmlFromFile(const char* source);
438

    
439
    /**
440
     * Parse a source document available by URI and return it as an XdmNode.
441
    */
442
    XdmNode * parseXmlFromUri(const char* source);
443

    
444
    int getNodeKind(jobject);
445

    
446
    bool isSchemaAware();
447
 
448

    
449
    /**
450
     * Checks for pending exceptions without creating a local reference to the exception object
451
     * @return bool - true when there is a pending exception; otherwise return false
452
    */
453
    bool exceptionOccurred();
454

    
455
    /**
456

457
     * Clears any exception that is currently being thrown. If no exception is currently being thrown, this routine has no effect.
458
    */
459
    void exceptionClear();
460

    
461
    /**
462
     * Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local exceptions objects
463
     * @param env
464
     * @param callingClass
465
     * @param callingObject
466
     * @return SaxonApiException
467
    */
468
    SaxonApiException * checkForExceptionCPP(JNIEnv* env, jclass callingClass,  jobject callingObject);
469

    
470
    SaxonApiException * getException();
471

    
472
    /*
473
      * Clean up and destroy Java VM to release memory used. 
474
     */
475
    static void release();
476

    
477
    /**
478
     * set the current working directory
479
    */
480
   void setcwd(const char* cwd);
481

    
482

    
483
    /**
484
     * set saxon resources directory
485
    */
486
   void setResourcesDirectory(const char* dir);
487
        
488

    
489
    /**
490
     * get saxon resources directory
491
    */
492
   const char * getResourcesDirectory();
493

    
494
    /**
495
     * Set a configuration property specific to the processor in use. 
496
     * Properties specified here are common across all the processors.
497
     * Example 'l':enable line number has the value 'on' or 'off'
498
     * @param name of the property
499
     * @param value of the property
500
     */
501
    void setConfigurationProperty(const char * name, const char * value);
502

    
503
     void clearConfigurationProperties();
504

    
505

    
506
    /**
507
     * Get the Saxon version
508
     * @return char array
509
     */
510
    const char * version();
511

    
512

    
513

    
514
//        XPathEngine
515
//        XQueryEngine
516
//        SchemaManager
517

    
518
   // static JNIEnv *env;
519
    static int jvmCreatedCPP;
520
    static sxnc_environment * sxn_environ;
521
    static int refCount;
522
    std::string cwd; /*!< current working directory */
523
    jobject proc; /*!< Java Processor object */
524
    
525
    /*static JavaVM *jvm;*/
526
    
527
protected:
528
        jclass xdmAtomicClass;
529
        jclass  versionClass;
530
        jclass  procClass;
531
        jclass  saxonCAPIClass;
532
        std::string cwdV; /*!< current working directory */
533
        //std::string resources_dir; /*!< current Saxon resources directory */
534
        const char * versionStr;
535
        std::map<std::string,XdmValue*> parameters; /*!< map of parameters used for the transformation as (string, value) pairs */
536
        std::map<std::string,std::string> configProperties; /*!< map of properties used for the transformation as (string, string) pairs */         
537
        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  */
538
        bool closed;
539
        SaxonApiException* exception; /*!< Pointer to any potential exception thrown */
540

    
541
private:
542

    
543
        void applyConfigurationProperties();
544
};
545

    
546
//===============================================================================================
547

    
548
#endif /* SAXON_PROCESSOR_H */
(9-9/37)