Saxon/C  1.1.2
Saxon Processor library for C/C++/PHP
 All Classes Functions Variables
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.1.2"
25 #include <string>
26 #include <iostream>
27 #include <sstream>
28 #include <map>
29 #include <vector>
30 #include <stdexcept> // std::logic_error
31 
32 #include "SaxonCGlue.h"
33 #include "SaxonCXPath.h"
34 #include "XsltProcessor.h"
35 #include "XQueryProcessor.h"
36 #include "XPathProcessor.h"
37 #include "SchemaValidator.h"
38 //#include "com_saxonica_functions_extfn_PhpCall.h"
39 //#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.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;
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 
74 
75 public:
76 
81  exceptions = std::vector<MyException>(0);
82  }
83 
89  exceptions = ex.exceptions;
90  }
91 
97  SaxonApiException(const char * ec, const char * exM){
98  exceptions = std::vector<MyException>(0);
99  MyException newEx;
100  if(ec != NULL){
101  newEx.errorCode = std::string(ec);
102  } else {
103  newEx.errorCode ="Unknown";
104  }
105  if(exM != NULL){
106  newEx.errorMessage = std::string(exM);
107  } else {
108  newEx.errorMessage="Unkown";
109  }
110  newEx.isType = false;
111  newEx.isStatic = false;
112  newEx.isGlobal = false;
113  newEx.linenumber = 0;
114  exceptions.push_back(newEx);
115  }
116 
126  SaxonApiException(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
127  exceptions = std::vector<MyException>(20);
128  MyException newEx;
129  if(ec != NULL){
130  newEx.errorCode = std::string(ec);
131  } else {
132  newEx.errorCode ="ERROR1";
133  }
134  if(exM != NULL){
135  newEx.errorMessage = std::string(exM);
136  } else {
137  newEx.errorMessage="ERROR2";
138  }
139  newEx.isType = typeErr;
140  newEx.isStatic = stat;
141  newEx.isGlobal = glob;
142  newEx.linenumber = l;
143  exceptions.push_back(newEx);
144  }
145 
155  void add(const char * ec, const char * exM, bool typeErr, bool stat, bool glob, int l){
156  MyException newEx;
157  if(ec != NULL){
158  newEx.errorCode = std::string(ec);
159  } else {
160  newEx.errorCode ="ERROR1";
161  }
162  if(exM != NULL){
163  newEx.errorMessage = std::string(exM);
164  } else {
165  newEx.errorMessage="ERROR2";
166  }
167  newEx.isType = typeErr;
168  newEx.isStatic = stat;
169  newEx.isGlobal = glob;
170  newEx.linenumber = l;
171  exceptions.push_back(newEx);
172  }
173 
174 
179  clear();
180  }
181 
187  const char * getErrorCode(int i){
188  if((size_t)i <= exceptions.size()){
189  return exceptions[i].errorCode.c_str();
190  }
191  return NULL;
192  }
193 
194 
195  int getLineNumber(int i){
196  if((size_t)i <= exceptions.size()){
197  return exceptions[i].linenumber;
198  }
199  return 0;
200  }
201 
202  bool isGlobalError(int i){
203  if((size_t)i <= exceptions.size()){
204  return exceptions[i].isGlobal;
205  }
206  return false;
207  }
208 
209  bool isStaticError(int i){
210  if((size_t)i <= exceptions.size()){
211  return exceptions[i].isStatic;
212  }
213  return false;
214  }
215 
216  bool isTypeError(int i){
217  if((size_t) i <= exceptions.size()){
218  return exceptions[i].isType;
219  }
220  return false;
221  }
222 
223  void clear(){
224  for(size_t i =0; i< exceptions.size();i++) {
225  exceptions[i].errorCode.clear();
226  exceptions[i].errorMessage.clear();
227  }
228  exceptions.clear();
229  }
230 
231  int count(){
232  return (int)exceptions.size();
233  }
234 
241  const char * getErrorMessage(int i){
242  if((size_t)i <= exceptions.size()){
243  return exceptions[i].errorMessage.c_str();
244  }
245  return NULL;
246  }
247 
248  const char * getErrorMessages(){
249  std::string result;
250  for(size_t i = 0;i<exceptions.size();i++) {
251  result += getErrorMessage(i);
252  }
253  if(result.empty()) { return NULL;}
254  return result.c_str();
255  }
256 
263  if((size_t)i <= exceptions.size()){
264  return exceptions[i];
265  }
266  throw 0;
267  }
268 
269 
270 
271 private:
272  std::vector<MyException> exceptions;
273 };
274 
275 
276 
277 
278 
279 
280 
281 
282 //==========================================
283 
284 
285 
291 friend class XsltProcessor;
292 friend class XQueryProcessor;
293 friend class SchemaValidator;
294 friend class XPathProcessor;
295 friend class XdmValue;
296 friend class XdmAtomicValue;
297 public:
298 
300 
304  SaxonProcessor();
305 
307 
311  SaxonProcessor(const char * configFile);
312 
313 
315 
319  SaxonProcessor(bool l);
320 
321  SaxonProcessor& operator=( const SaxonProcessor& other );
322 
327  ~SaxonProcessor();
328 
335 
342 
343 
350 
358 
359 
367  XdmAtomicValue * makeStringValue(std::string str);
368 
376  XdmAtomicValue * makeStringValue(const char * str);
377 
385 
386 
393  XdmAtomicValue * makeDoubleValue(double d);
394 
402 
409  XdmAtomicValue * makeLongValue(long l);
410 
419 
425  XdmAtomicValue * makeQNameValue(std::string str);
426 
434  XdmAtomicValue * makeAtomicValue(std::string type, std::string value);
435 
440  const char * getStringValue(XdmItem * item);
441 
445  XdmNode * parseXmlFromString(const char* source);
446 
450  XdmNode * parseXmlFromFile(const char* source);
451 
455  XdmNode * parseXmlFromUri(const char* source);
456 
457  int getNodeKind(jobject);
458 
459  bool isSchemaAware();
460 
461 
466  bool exceptionOccurred();
467 
472  void exceptionClear(bool clearCPPException=true);
473 
481  SaxonApiException * checkForExceptionCPP(JNIEnv* env, jclass callingClass, jobject callingObject);
482 
483  SaxonApiException * getException();
484 
485  /*
486  * Clean up and destroy Java VM to release memory used.
487  */
488  static void release();
489 
493  void setcwd(const char* cwd);
494 
495 
499  void setResourcesDirectory(const char* dir);
500 
501 
505  const char * getResourcesDirectory();
506 
514  void setConfigurationProperty(const char * name, const char * value);
515 
520 
521 
526  const char * version();
527 
528 /*
529  * Add a native method.
530  * @param name of the native method
531  * @param signature of the native method
532  * @param fnPtr Pointer to the native method
533  */
534 void addNativeMethod(char *name, char* signature, void * fnPtr){
535 
536  JNINativeMethod method;
537  method.name = name;
538  method.signature = signature;
539  method.fnPtr = fnPtr;
540 
541  nativeMethodVect.push_back(method);
542 
543 
544 
545 }
546 
547 /*
548  * Register several native methods for one class.
549  * @param libName name of the library which contains the function(s). Loads the library
550  * @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 .
551  * @return bool success of registered native method
552  */
553 bool registerCPPFunction(char * libName, JNINativeMethod * gMethods=NULL){
554  if(libName != NULL) {
555  setConfigurationProperty("extc", libName);
556 
557  }
558 
559  if(gMethods == NULL && nativeMethodVect.size()==0) {
560  return false;
561  } else {
562  if(gMethods == NULL) {
563  //copy vector to gMethods
564  gMethods = new JNINativeMethod[nativeMethodVect.size()];
565  }
566  return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
567  gMethods, sizeof(gMethods) / sizeof(gMethods[0]));
568 
569 
570  }
571  return false;
572 }
573 
574 /*
575  * Register several native methods for one class.
576  * @return bool success of registered native method
577  */
578 static bool registerNativeMethods(JNIEnv* env, const char* className,
579  JNINativeMethod* gMethods, int numMethods)
580 {
581  jclass clazz;
582  clazz = env->FindClass(className);
583  if (clazz == NULL) {
584  // std::cerr<<"Native registration unable to find class "<< className<<std::endl;
585  return false;
586  }
587  if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
588  // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
589  return false;
590  }
591  return true;
592 }
593 
594  /* TODO: Remove use of this method.*/
595  const char* checkException(jobject cpp);
596 
597  /* Internal use*/
598  void checkAndCreateException(jclass cppClass);
599 
600 
601 
602 // XPathEngine
603 // XQueryEngine
604 // SchemaManager
605 
606  // static JNIEnv *env;
607  static int jvmCreatedCPP;
608  static sxnc_environment * sxn_environ;
609  static int refCount;
610  std::string cwd;
611  jobject proc;
613  /*static JavaVM *jvm;*/
614 
615 protected:
616  jclass xdmAtomicClass;
617  jclass versionClass;
618  jclass procClass;
619  jclass saxonCAPIClass;
620  std::string cwdV;
621  //std::string resources_dir; /*!< current Saxon resources directory */
622  char * versionStr;
623  std::map<std::string,XdmValue*> parameters;
624  std::map<std::string,std::string> configProperties;
625  bool licensei;
626  bool closed;
629  JNINativeMethod * nativeMethods;
630  std::vector<JNINativeMethod> nativeMethodVect;
632 private:
633 
634  void applyConfigurationProperties();
635 };
636 
637 //===============================================================================================
638 
639 #endif /* SAXON_PROCESSOR_H */
XdmAtomicValue * makeFloatValue(float)
Definition: SaxonProcessor.cpp:564
void exceptionClear(bool clearCPPException=true)
Definition: SaxonProcessor.cpp:71
XdmAtomicValue * makeBooleanValue(bool b)
Definition: SaxonProcessor.cpp:582
MyException getException(int i)
Definition: SaxonProcessor.h:262
std::vector< JNINativeMethod > nativeMethodVect
Definition: SaxonProcessor.h:630
Definition: XQueryProcessor.h:26
void setcwd(const char *cwd)
Definition: SaxonProcessor.cpp:376
std::map< std::string, XdmValue * > parameters
Definition: SaxonProcessor.h:623
XdmAtomicValue * makeDoubleValue(double d)
Definition: SaxonProcessor.cpp:555
SaxonApiException * checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject)
Definition: SaxonProcessor.cpp:88
std::string cwd
Definition: SaxonProcessor.h:610
SaxonApiException(const char *ec, const char *exM, bool typeErr, bool stat, bool glob, int l)
Definition: SaxonProcessor.h:126
Definition: XdmAtomicValue.h:21
Definition: SaxonCGlue.h:80
std::map< std::string, std::string > configProperties
Definition: SaxonProcessor.h:624
const char * getStringValue(XdmItem *item)
Definition: SaxonProcessor.cpp:605
Definition: XdmNode.h:21
void clearConfigurationProperties()
Definition: SaxonProcessor.cpp:499
SaxonApiException()
Definition: SaxonProcessor.h:80
~SaxonProcessor()
Definition: SaxonProcessor.cpp:273
SaxonApiException(const SaxonApiException &ex)
Definition: SaxonProcessor.h:88
jobject proc
Definition: SaxonProcessor.h:611
XdmAtomicValue * makeLongValue(long l)
Definition: SaxonProcessor.cpp:573
Definition: SaxonProcessor.h:73
Definition: SchemaValidator.h:26
XPathProcessor * newXPathProcessor()
Definition: SaxonProcessor.cpp:340
const char * getErrorMessage(int i)
Definition: SaxonProcessor.h:241
Definition: XdmValue.h:44
const char * getResourcesDirectory()
Definition: SaxonProcessor.cpp:385
XdmNode * parseXmlFromUri(const char *source)
Definition: SaxonProcessor.cpp:468
SaxonApiException(const char *ec, const char *exM)
Definition: SaxonProcessor.h:97
Definition: XsltProcessor.h:24
XdmNode * parseXmlFromFile(const char *source)
Definition: SaxonProcessor.cpp:446
SchemaValidator * newSchemaValidator()
Definition: SaxonProcessor.cpp:345
Definition: SaxonProcessor.h:290
XdmAtomicValue * makeIntegerValue(int i)
Definition: SaxonProcessor.cpp:544
Definition: XdmItem.h:15
const char * version()
Definition: SaxonProcessor.cpp:357
XdmAtomicValue * makeAtomicValue(std::string type, std::string value)
Definition: SaxonProcessor.cpp:598
void setConfigurationProperty(const char *name, const char *value)
Definition: SaxonProcessor.cpp:493
XdmAtomicValue * makeStringValue(std::string str)
Definition: SaxonProcessor.cpp:526
Definition: SaxonProcessor.h:58
XdmNode * parseXmlFromString(const char *source)
Definition: SaxonProcessor.cpp:390
const char * getErrorCode(int i)
Definition: SaxonProcessor.h:187
void setResourcesDirectory(const char *dir)
Definition: SaxonProcessor.cpp:380
SaxonApiException * exception
Definition: SaxonProcessor.h:627
Definition: XPathProcessor.h:26
bool licensei
Definition: SaxonProcessor.h:625
bool exceptionOccurred()
Definition: SaxonProcessor.cpp:35
XsltProcessor * newXsltProcessor()
Definition: SaxonProcessor.cpp:330
~SaxonApiException()
Definition: SaxonProcessor.h:178
SaxonProcessor()
A default constructor.
Definition: SaxonProcessor.cpp:82
XQueryProcessor * newXQueryProcessor()
Definition: SaxonProcessor.cpp:335
XdmAtomicValue * makeQNameValue(std::string str)
Definition: SaxonProcessor.cpp:591
std::string cwdV
Definition: SaxonProcessor.h:620
void add(const char *ec, const char *exM, bool typeErr, bool stat, bool glob, int l)
Definition: SaxonProcessor.h:155