SaxonC  11.6
Saxon Processor library for C/C++, PHP and Python
SaxonProcessor.h
1 // Copyright (c) 2022 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 
12 #if defined __linux__ || defined __APPLE__
13 
14 #include <stdlib.h>
15 #include <string>
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 //#define MEM_DEBUG //remove
27 #define CVERSION "11.5"
28 #define CVERSION_API_NO 1150
29 
30 
31 
32 
33 #include <string>
34 #include <iostream>
35 #include <sstream>
36 #include <map>
37 #include <vector>
38 #include <stdexcept> // std::logic_error
39 
40 #include "SaxonCGlue.h"
41 #include "SaxonCXPath.h"
42 #include "Xslt30Processor.h"
43 #include "XsltExecutable.h"
44 #include "DocumentBuilder.h"
45 #include "XQueryProcessor.h"
46 #include "XPathProcessor.h"
47 #include "SchemaValidator.h"
48 #include "SaxonApiException.h"
49 //#include "com_saxonica_functions_extfn_PhpCall.h"
50 //#include "com_saxonica_functions_extfn_PhpCall_PhpFunctionCall.h"
51 //#define MEM_DEBUG
52 #if defined MEM_DEBUG
53 
54 
55 #include <algorithm>
56 #include <cstdlib>
57 #include <new>
58 
59 static std::vector<void*> myAlloc;
60 
61 void* newImpl(std::size_t sz,char const* file, int line);
62 
63 void* operator new(std::size_t sz,char const* file, int line);
64 
65 void* operator new [](std::size_t sz,char const* file, int line);
66 
67 void operator delete(void* ptr) noexcept;
68 
69 void operator delete(void*, std::size_t) noexcept;
70 
71 #endif
72 
73 
74 class Xslt30Processor;
75 
76 class XQueryProcessor;
77 
78 class XPathProcessor;
79 
80 class SchemaValidator;
81 
82 class XdmValue;
83 
84 class XdmNode;
85 
86 class XdmItem;
87 
88 class XdmAtomicValue;
89 
90 class XdmFunctionItem;
91 
92 class XdmArray;
93 
94 class XdmMap;
95 
96 class XsltExecutable;
97 
98 class DocumentBuilder;
99 
100 class SaxonApiException;
101 
102 
104 
105 typedef struct {
106  jobjectArray stringArray;
107  jobjectArray objectArray;
109 } JParameters;
110 
111 
112 
113 //==========================================
114 
115 
117 
120 
121  friend class DocumentBuilder;
122 
123  friend class Xslt30Processor;
124 
125  friend class XsltExecutable;
126 
127  friend class XQueryProcessor;
128 
129  friend class SchemaValidator;
130 
131  friend class XPathProcessor;
132 
133  friend class XdmValue;
134 
135  friend class XdmAtomicValue;
136 
137  friend class XdmFunctionItem;
138 
139  friend class XdmNode;
140 
141  friend class XdmMap;
142 
143  friend class XdmArray;
144 
145 public:
146 
148 
152  SaxonProcessor();
153 
155 
159  SaxonProcessor(const char *configFile);
160 
161 
163 
167  SaxonProcessor(bool l);
168 
170 
174  SaxonProcessor &operator=(const SaxonProcessor &other);
175 
176 
178 
182  SaxonProcessor(const SaxonProcessor &other);
183 
185  ~SaxonProcessor();
186 
188 
192  const char * getErrorMessage();
193 
195 
201 
202 
204 
209 
210 
212 
218 
219 
221 
227 
229 
235 
237 
244  XdmAtomicValue *makeStringValue(std::string str);
245 
247 
254  XdmAtomicValue *makeStringValue(const char *str);
255 
257 
264  static XdmAtomicValue *makeStringValue2(std::string str);
265 
273 
274 
276 
281  XdmAtomicValue *makeDoubleValue(double d);
282 
284 
290  XdmAtomicValue *makeFloatValue(float f);
291 
293 
299  XdmAtomicValue *makeLongValue(long l);
300 
302 
310 
312 
317  XdmAtomicValue *makeQNameValue(const char *str);
318 
320 
327  XdmAtomicValue *makeAtomicValue(const char *type, const char *value);
328 
330 
336  XdmArray *makeArray(const char **input, int length);
337 
338 
340 
346  XdmArray *makeArray(short *input, int length);
347 
348 
350 
356  XdmArray *makeArray(int *input, int length);
357 
359 
365  XdmArray *makeArray(long *input, int length);
366 
373  XdmArray *makeArray(bool *input, int length);
374 
375 
377 
383  XdmArray *makeArray(XdmValue ** values, int length);
384 
386 
391  XdmMap *makeMap(std::map<XdmAtomicValue *, XdmValue *> dataMap);
392 
394 
399  static XdmMap *makeMap2(std::map<std::string, XdmValue *> dataMap);
400 
402 
409  XdmMap *makeMap3(XdmAtomicValue ** keys, XdmValue ** values, int len);
410 
411 
413 
417  const char *getStringValue(XdmItem *item);
418 
420 
426  XdmNode *parseXmlFromString(const char *source, SchemaValidator * validator = nullptr);
427 
429 
435  XdmNode *parseXmlFromFile(const char *source, SchemaValidator * validator = nullptr);
436 
438 
444  XdmNode *parseXmlFromUri(const char *source, SchemaValidator * validator = nullptr);
445 
447 
452  int getNodeKind(jobject obj);
453 
455 
459  bool isSchemaAwareProcessor();
460 
461 
463 
467  bool exceptionOccurred();
468 
470  void exceptionClear();
471 
472 
475  return (new XdmValue*[len]);
476  }
477 
480  return (new XdmAtomicValue*[len]);
481  }
482 
484 
490  static SaxonApiException *checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject);
491 
493  static void release();
494 
496  static void attachCurrentThread();
497 
499  static void detachCurrentThread();
500 
501 
503  void setcwd(const char *cwd);
504 
506 
510  const char *getcwd();
511 
512 
514 
518  const char * getResourcesDirectory();
519 
520 
522  void setResourcesDirectory(const char* dir);
523 
527  void setCatalog(const char *catalogFile, bool isTracing);
528 
529 
531 
539  void setConfigurationProperty(const char *name, const char *value);
540 
543 
544 
546 
550  const char *version();
551 
553 
559  void addNativeMethod(char *name, char *signature, void *fnPtr) {
560 
561  JNINativeMethod method;
562  method.name = name;
563  method.signature = signature;
564  method.fnPtr = fnPtr;
565 
566  nativeMethodVect.push_back(method);
567 
568 
569  }
570 
572 
579  bool registerCPPFunction(char *libName, JNINativeMethod *gMethods = nullptr) {
580  if (libName != nullptr) {
581  setConfigurationProperty("extc", libName);
582 
583  }
584 
585  if (gMethods == nullptr && nativeMethodVect.size() == 0) {
586  return false;
587  } else {
588  if (gMethods == nullptr) {
589  //copy vector to gMethods
590  gMethods = new JNINativeMethod[nativeMethodVect.size()];
591  }
592  return registerNativeMethods(sxn_environ->env, "com/saxonica/functions/extfn/CppCall$PhpFunctionCall",
593  gMethods, nativeMethodVect.size());
594 
595 
596  }
597  return false;
598  }
599 
601 
608  static bool registerNativeMethods(JNIEnv *env, const char *className,
609  JNINativeMethod *gMethods, int numMethods) {
610  jclass clazz;
611  clazz = env->FindClass(className);
612  if (clazz == nullptr) {
613  std::cerr << "Native registration unable to find class " << className << std::endl;
614  return false;
615  }
616 
617  if (env->RegisterNatives(clazz, gMethods, numMethods) < 0) {
618  // std::cerr<<"RegisterNatives failed for "<< className<<std::endl;
619  return false;
620  }
621  return true;
622  }
623 
625 
630  SaxonApiException *checkAndCreateException(jclass cppClass);
631 
632 
633 
634 
635  static int jvmCreatedCPP;
637  std::string cwd;
638  jobject proc;
644  static void getInfo();
645 
646  /*static JavaVM *jvm;*/
647 
648 protected:
649 
650 
651  jclass xdmAtomicClass;
652  jclass versionClass;
653  jclass procClass;
654  jclass saxonCAPIClass;
655  std::string cwdV;
656  //std::string resources_dir; /*!< current Saxon resources directory */
657  std::string versionStr;
658  std::map<std::string, XdmValue *> parameters;
659  std::map<std::string, std::string> configProperties;
660  bool licensei;
663  JNINativeMethod *nativeMethods;
664  std::vector<JNINativeMethod> nativeMethodVect;
668 private:
669 
670  void createException(const char * message=nullptr);
671 
672  void initialize(bool l);
673 
674  void applyConfigurationProperties();
675 
676  // SaxonC method for internal use
677  static JParameters
678  createParameterJArray(std::map<std::string, XdmValue *> parameters, std::map<std::string, std::string> properties, int additions = 0);
679 
680  static JParameters createParameterJArray2(std::map<std::string, XdmValue *> parameters);
681 
682  static jobjectArray createJArray(XdmValue **values, int length);
683 
684  static const char *checkException();
685 };
686 
687 
688 //===============================================================================================
689 
690 
691 
692 #endif /* SAXON_PROCESSOR_H */
Definition: DocumentBuilder.h:33
Definition: SaxonApiException.h:19
The SaxonProcessor class acts as a factory for generating XQuery, XPath, Schema and XSLT compilers.
Definition: SaxonProcessor.h:119
void setcwd(const char *cwd)
Set the current working directory.
Definition: SaxonProcessor.cpp:755
const char * getResourcesDirectory()
Get saxon resources directory.
Definition: SaxonProcessor.cpp:766
XdmNode * parseXmlFromFile(const char *source, SchemaValidator *validator=nullptr)
Parse a source document file and return it as an XdmNode.
Definition: SaxonProcessor.cpp:885
XdmMap * makeMap3(XdmAtomicValue **keys, XdmValue **values, int len)
Make an XdmMap from an array of keys and values.
Definition: SaxonProcessor.cpp:1506
static void detachCurrentThread()
Detach JVM from the current thread.
Definition: SaxonProcessor.cpp:323
SaxonApiException * checkAndCreateException(jclass cppClass)
Check for exception thrown in the underlying JNI. Then create the SaxonApiException class which the u...
Definition: SaxonProcessor.cpp:94
XdmAtomicValue ** createXdmAtomicValueArray(int len)
Utility method for working with SaxonC on Python.
Definition: SaxonProcessor.h:479
std::string cwdV
Definition: SaxonProcessor.h:655
jclass versionClass
Definition: SaxonProcessor.h:652
static void release()
Clean up and destroy Java VM to release memory used. Method to be called at the end of the program.
Definition: SaxonProcessor.cpp:950
XdmAtomicValue * makeAtomicValue(const char *type, const char *value)
Create an Xdm Atomic value from string representation.
Definition: SaxonProcessor.cpp:1098
XdmNode * parseXmlFromUri(const char *source, SchemaValidator *validator=nullptr)
Parse a source document available by URI and return it as an XdmNode.
Definition: SaxonProcessor.cpp:910
static XdmAtomicValue * makeStringValue2(std::string str)
Factory method. Unlike the constructor, this avoids creating a new StringValue in the case.
Definition: SaxonProcessor.cpp:983
const char * getErrorMessage()
Get any error message thrown by the processor.
Definition: SaxonProcessor.cpp:106
void setResourcesDirectory(const char *dir)
set saxon resources directory
Definition: SaxonProcessor.cpp:771
void addNativeMethod(char *name, char *signature, void *fnPtr)
Add a native method.
Definition: SaxonProcessor.h:559
static sxnc_environment * sxn_environ
Definition: SaxonProcessor.h:636
static XdmMap * makeMap2(std::map< std::string, XdmValue * > dataMap)
Make an XdmMap whose argument is a map from the standard template library.
Definition: SaxonProcessor.cpp:1443
void clearConfigurationProperties()
Clear configuration properties specific to the processor in use.
Definition: SaxonProcessor.cpp:945
jclass xdmAtomicClass
Definition: SaxonProcessor.h:651
void exceptionClear()
Clears any exception that is currently being thrown. If no exception is currently being thrown,...
Definition: SaxonProcessor.cpp:116
XdmAtomicValue * makeBooleanValue(bool b)
Factory method: makes a XdmAtomicValue representing a boolean Value.
Definition: SaxonProcessor.cpp:1077
int getNodeKind(jobject obj)
Get the kind of node.
Definition: SaxonProcessor.cpp:849
SaxonProcessor()
A default constructor.
Definition: SaxonProcessor.cpp:247
const char * getStringValue(XdmItem *item)
Get the string representation of the XdmValue.
Definition: SaxonProcessor.cpp:1106
std::map< std::string, XdmValue * > parameters
Definition: SaxonProcessor.h:658
XdmAtomicValue * makeQNameValue(const char *str)
Create an QName Xdm value from string representation in clark notation.
Definition: SaxonProcessor.cpp:1090
SchemaValidator * newSchemaValidator()
Create a SchemaValidator which can be used to validate instance documents against the schema held by ...
Definition: SaxonProcessor.cpp:713
const char * getcwd()
Get the current working directory.
Definition: SaxonProcessor.cpp:761
XdmAtomicValue * makeDoubleValue(double d)
Factory method. make an XdmAtomicValue from a primitive double value.
Definition: SaxonProcessor.cpp:1038
XdmValue ** createXdmValueArray(int len)
Utility method for working with SaxonC on Python.
Definition: SaxonProcessor.h:474
XdmAtomicValue * makeIntegerValue(int i)
Definition: SaxonProcessor.cpp:1021
bool exceptionOccurred()
Checks for thrown exceptions.
Definition: SaxonProcessor.cpp:81
jclass procClass
Definition: SaxonProcessor.h:653
void setConfigurationProperty(const char *name, const char *value)
Set a configuration property specific to the processor in use.
Definition: SaxonProcessor.cpp:938
static bool registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *gMethods, int numMethods)
Register several native methods for one class.
Definition: SaxonProcessor.h:608
bool registerCPPFunction(char *libName, JNINativeMethod *gMethods=nullptr)
Register several native methods for one class.
Definition: SaxonProcessor.h:579
void setCatalog(const char *catalogFile, bool isTracing)
Definition: SaxonProcessor.cpp:783
JNINativeMethod * nativeMethods
Definition: SaxonProcessor.h:663
XQueryProcessor * newXQueryProcessor()
Create an XQueryProcessor.
Definition: SaxonProcessor.cpp:703
XdmNode * parseXmlFromString(const char *source, SchemaValidator *validator=nullptr)
Parse a lexical representation of the source document and return it as an XdmNode.
Definition: SaxonProcessor.cpp:813
const char * version()
Get the Saxon version.
Definition: SaxonProcessor.cpp:724
static void getInfo()
Xslt30Processor * newXslt30Processor()
Create an Xslt30Processor.
Definition: SaxonProcessor.cpp:698
std::string cwd
Definition: SaxonProcessor.h:637
static void attachCurrentThread()
Attaches a current thread to the a Java VM.
Definition: SaxonProcessor.cpp:307
std::string versionStr
Definition: SaxonProcessor.h:657
XdmAtomicValue * makeLongValue(long l)
Factory method: makes either an Int64Value or a BigIntegerValue depending on the value supplied.
Definition: SaxonProcessor.cpp:1064
bool licensei
Definition: SaxonProcessor.h:660
~SaxonProcessor()
Destructor method. At the end of the program call the release() method to clear the JVM.
Definition: SaxonProcessor.cpp:383
std::map< std::string, std::string > configProperties
Definition: SaxonProcessor.h:659
jclass saxonCAPIClass
Definition: SaxonProcessor.h:654
bool isSchemaAwareProcessor()
Test whether this processor is schema-aware.
Definition: SaxonProcessor.cpp:397
jobject proc
Definition: SaxonProcessor.h:638
SaxonProcessor & operator=(const SaxonProcessor &other)
The copy assignment= operator.
Definition: SaxonProcessor.cpp:632
static SaxonApiException * checkForExceptionCPP(JNIEnv *env, jclass callingClass, jobject callingObject)
Checks for pending exceptions and creates a SaxonApiException object, which handles one or more local...
Definition: SaxonProcessor.cpp:148
SaxonApiException * exception
Definition: SaxonProcessor.h:665
DocumentBuilder * newDocumentBuilder()
Create a DocumentBuilder.
Definition: SaxonProcessor.cpp:664
std::vector< JNINativeMethod > nativeMethodVect
Definition: SaxonProcessor.h:664
XdmMap * makeMap(std::map< XdmAtomicValue *, XdmValue * > dataMap)
Make an XdmMap whose argument is a map from the standard template library.
Definition: SaxonProcessor.cpp:1389
XdmArray * makeArray(const char **input, int length)
Make an XdmArray whose members are from string representation.
Definition: SaxonProcessor.cpp:1347
static int jvmCreatedCPP
Definition: SaxonProcessor.h:635
XPathProcessor * newXPathProcessor()
Create an XPathProcessor. An XPathProcessor is used to compile XPath expressions.
Definition: SaxonProcessor.cpp:708
XdmAtomicValue * makeStringValue(std::string str)
Factory method. Unlike the constructor, this avoids creating a new StringValue in the case.
Definition: SaxonProcessor.cpp:1003
XdmAtomicValue * makeFloatValue(float f)
Factory method. make an XdmAtomicValue from a primitive float value.
Definition: SaxonProcessor.cpp:1051
Definition: SchemaValidator.h:24
Definition: XPathProcessor.h:27
Definition: XQueryProcessor.h:26
Definition: XdmArray.h:25
XdmValue ** values()
Definition: XdmArray.cpp:225
Definition: XdmAtomicValue.h:26
Definition: XdmFunctionItem.h:23
Definition: XdmItem.h:29
jobject value
Definition: XdmItem.h:163
Definition: XdmMap.h:29
Definition: XdmNode.h:31
Definition: XdmValue.h:50
void initialize()
initialize this XdmValue with default values
Definition: XdmValue.h:193
Definition: Xslt30Processor.h:26
Definition: XsltExecutable.h:30
A struct object to represent parameters.
Definition: SaxonProcessor.h:105
jobjectArray stringArray
Definition: SaxonProcessor.h:106
jobjectArray objectArray
Definition: SaxonProcessor.h:107
Definition: SaxonCGlue.h:80