Project

Profile

Help

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

he / latest9.6 / hec / Saxon.C.API / SaxonProcessor.h @ 0608038f

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
extern int jvmCreatedCPP;
51
extern sxnc_environment * environ;
52
extern int refCount;
53

    
54
// The Saxon XSLT interface class
55

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

    
69

    
70

    
71

    
72
/*! <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
73
 * <p/>
74
 */
75
class SaxonApiException {
76

    
77
public:
78

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

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

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

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

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

    
172

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

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

    
192

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

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

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

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

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

    
229
        int count(){
230
                return exceptions.size();        
231
        }
232

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

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

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

    
262

    
263

    
264

    
265

    
266

    
267

    
268

    
269

    
270

    
271
//==========================================
272

    
273

    
274

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

    
288
   //! A default constructor.
289
    /*!
290
      * Create Saxon Processor.
291
    */
292

    
293
    SaxonProcessor();
294

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

    
300
    SaxonProcessor(const char * configFile);
301

    
302

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

    
310
    SaxonProcessor& operator=( const SaxonProcessor& other );
311

    
312
   /*!
313

314
      * Destructor
315
    */
316
    ~SaxonProcessor();
317
        
318
   /*!
319

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

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

    
332

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

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

    
348

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

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

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

    
375

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

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

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

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

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

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

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

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

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

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

    
446
    int getNodeKind(jobject);
447

    
448
    bool isSchemaAware();
449
 
450

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

    
457
    /**
458

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

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

    
472
    SaxonApiException * getException();
473

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

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

    
484

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

    
491
    /**
492
     * get saxon resources directory
493
    */
494
   const char * getResourcesDirectory();
495

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

    
505
     void clearConfigurationProperties();
506

    
507

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

    
514

    
515
//        XPathEngine
516
//        XQueryEngine
517
//        SchemaManager
518

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

    
540
private:
541

    
542
        void applyConfigurationProperties();
543
};
544

    
545
//===============================================================================================
546

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