Saxon/C  1.2.1
Saxon Processor library for C/C++, PHP and Python
SaxonProcessor.h
1 // Copyright (c) 2019 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.
7 
8 #ifndef SAXON_PROCESSOR_H
9 #define SAXON_PROCESSOR_H
10 
11 #if defined __linux__ || defined __APPLE__
12  #include <stdlib.h>
13  #include <string.h>
14  #include <dlfcn.h>
15 
16  #define HANDLE void*
17  #define LoadLibrary(x) dlopen(x, RTLD_LAZY)
18  #define GetProcAddress(x,y) dlsym(x,y)
19 #else
20  #include <windows.h>
21 #endif
22 
23 //#define DEBUG //remove
24 #define CVERSION "1.2.1"
25 #define CVERSION_API_NO 121
26 #include <string>
27 #include <iostream>
28 #include <sstream>
29 #include <map>
30 #include <vector>
31 #include <stdexcept> // std::logic_error
32 
33 #include "SaxonCGlue.h"
34 #include "SaxonCXPath.h"
35 #include "XsltProcessor.h"
36 #include "Xslt30Processor.h"
37 #include "XQueryProcessor.h"
38 #include "XPathProcessor.h"
39 #include "SchemaValidator.h"
40 //#include "com_saxonica_functions_extfn_PhpCall.h"
41 //#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
42 
43 class XsltProcessor;
44 class Xslt30Processor;
45 class XQueryProcessor;
46 class XPathProcessor;
47 class SchemaValidator;
48 class XdmValue;
49 class XdmNode;
50 class XdmItem;
51 class XdmAtomicValue;
52 
53 
54 
55 // The Saxon XSLT interface class
56 
57 //std::mutex mtx;
61 typedef struct {
62  std::string errorCode;
63  std::string errorMessage;
64  int linenumber;
65  bool isType;
66  bool isStatic;
67  bool isGlobal;
68  }MyException;
69 
70 typedef struct
71 {
72  jobjectArray stringArray;
73  jobjectArray objectArray;
74 
76 
77 
82 
83 public:
84 
89  exceptions = std::vector<MyException>(0);
90  }
91 
97  exceptions = ex.exceptions;
98  }
99 
105  SaxonApiException(const char * ec, const char * exM){
106  exceptions = std::vector<MyException>(0);
107  MyException newEx;
108  if(ec != NULL){
109  newEx.errorCode = std::string(ec);
110  } else {
111  newEx.errorCode ="Unknown";
112  }
113  if(exM != NULL){
114  newEx.errorMessage = std::string(exM);
115  } else {
116  newEx.errorMessage="Unkown";
117  }
118  newEx.isType = false;
119  newEx.isStatic = false;
120  newEx.isGlobal = false;
121  newEx.linenumber = 0;
122  exceptions.push_back(newEx);
123  }
124 
134  SaxonApiException(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
135  exceptions = std::vector<MyException>(20);
136  MyException newEx;
137  if(ec != NULL){
138  newEx.errorCode = std::string(ec);
139  } else {
140  newEx.errorCode ="ERROR1";
141  }
142  if(exM != NULL){
143  newEx.errorMessage = std::string(exM);
144  } else {
145  newEx.errorMessage="ERROR2";
146  }
147  newEx.isType = typeErr;
148  newEx.isStatic = stat;
149  newEx.isGlobal = glob;
150  newEx.linenumber = l;
151  exceptions.push_back(newEx);
152  }
153 
163  void add(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
164  MyException newEx;
165  if(ec != NULL){
166  newEx.errorCode = std::string(ec);
167  } else {
168  newEx.errorCode ="ERROR1";
169  }
170  if(exM != NULL){
171  newEx.errorMessage = std::string(exM);
172  } else {
173  newEx.errorMessage="ERROR2";
174  }
175  newEx.isType = typeErr;
176  newEx.isStatic = stat;
177  newEx.isGlobal = glob;
178  newEx.linenumber = l;
179  exceptions.push_back(newEx);
180  }
181 
182 
187  clear();
188  }
189 
195  const char * getErrorCode(int i){
196  if((size_t)i <= exceptions.size()){
197  return exceptions[i].errorCode.c_str();
198  }
199  return NULL;
200  }
201 
202 
203  int getLineNumber(int i){
204  if((size_t)i <= exceptions.size()){
205  return exceptions[i].linenumber;
206  }
207  return 0;
208  }
209 
210  bool isGlobalError(int i){
211  if((size_t)i <= exceptions.size()){
212  return exceptions[i].isGlobal;
213  }
214  return false;
215  }
216 
217  bool isStaticError(int i){
218  if((size_t)i <= exceptions.size()){
219  return exceptions[i].isStatic;
220  }
221  return false;
222  }
223 
224  bool isTypeError(int i){
225  if((size_t) i <= exceptions.size()){
226  return exceptions[i].isType;
227  }
228  return false;
229  }
230 
231  void clear(){
232  for(size_t i =0; i< exceptions.size();i++) {
233  exceptions[i].errorCode.clear();
234  exceptions[i].errorMessage.clear();
235  }
236  exceptions.clear();
237  }
238 
239  int count(){
240  return (int)exceptions.size();
241  }
242 
249  const char * getErrorMessage(int i){
250  if((size_t)i <= exceptions.size()){
251  return exceptions[i].errorMessage.c_str();
252  }
253  return NULL;
254  }
255 
256  const char * getErrorMessages(){
257  std::string result;
258  for(size_t i = 0;i<exceptions.size();i++) {
259  result += getErrorMessage(i);
260  }
261  if(result.empty()) { return NULL;}
262  return result.c_str();
263  }
264 
271  if((size_t)i <= exceptions.size()){
272  return exceptions[i];
273  }
274  throw 0;
275  }
276 
277 
278 
279 private:
280  std::vector<MyException> exceptions;
281 };
282 
283 
284 
285 
286 
287 
288 
289 
290 //==========================================
291 
292 
293 
297 friend class XsltProcessor;
298 friend class Xslt30Processor;
299 friend class XQueryProcessor;
300 friend class SchemaValidator;
301 friend class XPathProcessor;
302 friend class XdmValue;
303 friend class XdmAtomicValue;
304 public:
305 
307 
311  SaxonProcessor();
312 
314 
318  SaxonProcessor(const char * configFile);
319 
320 
322 
326  SaxonProcessor(bool l);
327 
328  SaxonProcessor& operator=( const SaxonProcessor& other );
329 
334  ~SaxonProcessor();
335 
336 
338  /* SaxonProcessor * getProcessor(){
339  return this;
340  }*/
341 
348 
355 
356 
363 
364 
371 
379 
380 
388  XdmAtomicValue * makeStringValue(std::string str);
389 
397  XdmAtomicValue * makeStringValue(const char * str);
398 
406 
407 
414  XdmAtomicValue * makeDoubleValue(double d);
415 
423 
430  XdmAtomicValue * makeLongValue(long l);
431 
440 
446  XdmAtomicValue * makeQNameValue(const char * str);
447 
455  XdmAtomicValue * makeAtomicValue(const char * type, const char * value);
456 
461  const char * getStringValue(XdmItem * item);
462 
466  XdmNode * parseXmlFromString(const char* source);
467 
471  XdmNode * parseXmlFromFile(const char* source);
472 
476  XdmNode * parseXmlFromUri(const char* source);
477 
478  int getNodeKind(jobject);
479 
480  bool isSchemaAwareProcessor();
481 
482 
483 
488  bool exceptionOccurred();
489 
494  void exceptionClear(bool clearCPPException=true);
495 
503  SaxonApiException * checkForExceptionCPP(JNIEnv* env, jclass callingClass, jobject callingObject);
504 
505  SaxonApiException * getException();
506 
507  /*
508  * Clean up and destroy Java VM to release memory used.
509  */
510  static void release();
511 
512 
516  void setcwd(const char* cwd);
517 
521  const char* getcwd();
522 
523 
527  void setResourcesDirectory(const char* dir);
528 
532  void setCatalog(const char* catalogFile, bool isTracing);
533 
537  const char * getResourcesDirectory();
538 
546  void setConfigurationProperty(const char * name, const char * value);
547 
552 
553 
558  const char * version();
559 
560 /*
561  * Add a native method.
562  * @param name of the native method
563  * @param signature of the native method
564  * @param fnPtr Pointer to the native method
565  */
566 void addNativeMethod(char *name, char* signature, void * fnPtr){
567 
568  JNINativeMethod method;
569  method.name = name;
570  method.signature = signature;
571  method.fnPtr = fnPtr;
572 
573  nativeMethodVect.push_back(method);
574 
575 
576 
577 }
578 
579 /*
580  * Register several native methods for one class.
581  * @param libName name of the library which contains the function(s). Loads the library
582  * @param gMethods Register native methods. Default is NULL, also NULL allowed in which cause assumption is made the user has added native methods using the method addNativeMethod .
583  * @return bool success of registered native method
584  */
585 bool registerCPPFunction(char * libName, JNINativeMethod * gMethods=NULL){
586  if(libName != NULL) {
587  setConfigurationProperty("extc", libName);
588 
589  }
590 
591  if(gMethods == NULL && nativeMethodVect.size()==0) {
592  return false;
593  } else {
594  if(gMethods == NULL) {
595  //copy vector to gMethods
596  gMethods = new JNINativeMethod[nativeMethodVect.size()];
597  }
598  return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
599  gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
600 
601 
602  }
603  return false;
604 }
605 
606 /*
607  * Register several native methods for one class.
608  * @return bool success of registered native method
609  */
610 static bool registerNativeMethods(JNIEnv* env, const char* className,
611  JNINativeMethod* gMethods, int numMethods)
612 {
613  jclass clazz;
614  clazz = env->FindClass(className);
615  if (clazz == NULL) {
616  std::cerr<<"Native registration unable to find class "<< className<<std::endl;
617  return false;
618  }
619 
620  if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
621  // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
622  return false;
623  }
624  return true;
625 }
626 
627 
628  /* TODO: Remove use of this method.*/
629  const char* checkException(jobject cpp);
630 
631  /* Internal use*/
632  void checkAndCreateException(jclass cppClass);
633 
634 
635 
636 // XPathEngine
637 // XQueryEngine
638 // SchemaManager
639 
640  // static JNIEnv *env;
641  static int jvmCreatedCPP;
642  static sxnc_environment * sxn_environ;
643  static int refCount;
644  std::string cwd;
645  jobject proc;
647  /*static JavaVM *jvm;*/
648 
649 protected:
650 
651 
652 
653  jclass xdmAtomicClass;
654  jclass versionClass;
655  jclass procClass;
656  jclass saxonCAPIClass;
657  std::string cwdV;
658  //std::string resources_dir; /*!< current Saxon resources directory */
659  char * versionStr;
660  std::map<std::string,XdmValue*> parameters;
661  std::map<std::string,std::string> configProperties;
662  bool licensei;
663  bool closed;
666  JNINativeMethod * nativeMethods;
667  std::vector<JNINativeMethod> nativeMethodVect;
671 private:
672 
673 
674 
675  void applyConfigurationProperties();
676  // Saxon/C method for internal use
677  static JParameters createParameterJArray(std::map<std::string,XdmValue*> parameters, std::map<std::string,std::string> properties);
678  static jobjectArray createJArray(XdmValue ** values, int length);
679 };
680 
681 //===============================================================================================
682 
683 #endif /* SAXON_PROCESSOR_H */
XdmAtomicValue * makeFloatValue(float)
Definition: SaxonProcessor.cpp:740
void exceptionClear(bool clearCPPException=true)
Definition: SaxonProcessor.cpp:65
XdmAtomicValue * makeBooleanValue(bool b)
Definition: SaxonProcessor.cpp:758
MyException getException(int i)
Definition: SaxonProcessor.h:270
std::vector< JNINativeMethod > nativeMethodVect
Definition: SaxonProcessor.h:667
Definition: XQueryProcessor.h:26
void setcwd(const char *cwd)
Definition: SaxonProcessor.cpp:502
std::map< std::string, XdmValue * > parameters
Definition: SaxonProcessor.h:660
XdmAtomicValue * makeDoubleValue(double d)
Definition: SaxonProcessor.cpp:731
SaxonApiException * checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject)
Definition: SaxonProcessor.cpp:83
std::string cwd
Definition: SaxonProcessor.h:644
SaxonApiException(const char *ec, const char *exM, bool typeErr, bool stat, bool glob, int l)
Definition: SaxonProcessor.h:134
Definition: XdmAtomicValue.h:20
Definition: SaxonCGlue.h:80
Definition: Xslt30Processor.h:24
std::map< std::string, std::string > configProperties
Definition: SaxonProcessor.h:661
const char * getStringValue(XdmItem *item)
Definition: SaxonProcessor.cpp:781
Definition: XdmNode.h:21
void clearConfigurationProperties()
Definition: SaxonProcessor.cpp:675
SaxonApiException()
Definition: SaxonProcessor.h:88
void setCatalog(const char *catalogFile, bool isTracing)
Definition: SaxonProcessor.cpp:522
~SaxonProcessor()
Definition: SaxonProcessor.cpp:268
SaxonApiException(const SaxonApiException &ex)
Definition: SaxonProcessor.h:96
const char * getcwd()
Definition: SaxonProcessor.cpp:506
jobject proc
Definition: SaxonProcessor.h:645
XdmAtomicValue * makeLongValue(long l)
Definition: SaxonProcessor.cpp:749
Definition: SaxonProcessor.h:81
Definition: SchemaValidator.h:26
XPathProcessor * newXPathProcessor()
Definition: SaxonProcessor.cpp:466
const char * getErrorMessage(int i)
Definition: SaxonProcessor.h:249
Definition: XdmValue.h:46
const char * getResourcesDirectory()
Definition: SaxonProcessor.cpp:559
XdmNode * parseXmlFromUri(const char *source)
Definition: SaxonProcessor.cpp:644
SaxonApiException(const char *ec, const char *exM)
Definition: SaxonProcessor.h:105
Definition: XsltProcessor.h:24
XdmNode * parseXmlFromFile(const char *source)
Definition: SaxonProcessor.cpp:622
XdmAtomicValue * makeAtomicValue(const char *type, const char *value)
Definition: SaxonProcessor.cpp:774
SchemaValidator * newSchemaValidator()
Definition: SaxonProcessor.cpp:471
Definition: SaxonProcessor.h:296
Xslt30Processor * newXslt30Processor()
Definition: SaxonProcessor.cpp:456
XdmAtomicValue * makeIntegerValue(int i)
Definition: SaxonProcessor.cpp:720
Definition: XdmItem.h:15
const char * version()
Definition: SaxonProcessor.cpp:483
void setConfigurationProperty(const char *name, const char *value)
Definition: SaxonProcessor.cpp:669
XdmAtomicValue * makeStringValue(std::string str)
Definition: SaxonProcessor.cpp:711
Definition: SaxonProcessor.h:61
XdmNode * parseXmlFromString(const char *source)
Definition: SaxonProcessor.cpp:564
const char * getErrorCode(int i)
Definition: SaxonProcessor.h:195
Definition: SaxonProcessor.h:70
void setResourcesDirectory(const char *dir)
Definition: SaxonProcessor.cpp:510
SaxonApiException * exception
Definition: SaxonProcessor.h:664
Definition: XPathProcessor.h:26
bool licensei
Definition: SaxonProcessor.h:662
bool exceptionOccurred()
Definition: SaxonProcessor.cpp:29
XdmAtomicValue * makeQNameValue(const char *str)
Definition: SaxonProcessor.cpp:767
XsltProcessor * newXsltProcessor()
Get the Processor object. Method used in Python.
Definition: SaxonProcessor.cpp:451
~SaxonApiException()
Definition: SaxonProcessor.h:186
SaxonProcessor()
A default constructor.
Definition: SaxonProcessor.cpp:76
XQueryProcessor * newXQueryProcessor()
Definition: SaxonProcessor.cpp:461
std::string cwdV
Definition: SaxonProcessor.h:657
void add(const char *ec, const char *exM, bool typeErr, bool stat, bool glob, int l)
Definition: SaxonProcessor.h:163