Project

Profile

Help

Bug #5050 » SaxonClient.cpp

ofer benoliel, 2021-07-29 12:09

 
#include <sstream>
#include <stdio.h>
#include "SaxonProcessor.h"
#include "XdmValue.h"
#include "XdmItem.h"
#include "XdmNode.h"
#include <string>

//#define API_REDIS_CLIENT_IMPL
#ifdef __GNUC__
#if __GNUC__ >= 4
#define DLL_PUBLIC extern "C" __attribute__ ((visibility ("default")))
#define DLL_LOCAL __attribute__ ((visibility ("hidden")))
#else
#define DLL_PUBLIC extern "C"
#define DLL_LOCAL
#endif
#define API_DLL_PREFIX_DEF DLL_PUBLIC
#define WINAPI
#else
#define API_DLL_PREFIX_DEF
#endif

#include "SaxonClient.h"

class SaxonResultContainer
{
public:
SaxonResultContainer() {}
virtual ~SaxonResultContainer() {}

virtual int Duplicate(const char* strSrc, int size) = 0;
virtual int SetError(const char* error, int size) = 0;
};
//#include "MemCacheStringAllocator.h"

SaxonProcessor * gProcessor = NULL;


void Destroy()
{
if (gProcessor) {
delete gProcessor;
gProcessor = NULL;
}
}

int Init(void*& obj, Xslt30Processor*& proc)
{
if (!gProcessor)
return -2;
proc = (Xslt30Processor*&)obj;
if (!proc) {
proc = gProcessor->newXslt30Processor();
obj = proc;
}

return 0;
}

// This is an example of an exported function.
API_DLL_PREFIX_DEF int WINAPI InitSaxonXsltInf()
{
if (gProcessor)
return 0;
gProcessor = new SaxonProcessor(true);
//gProcessor->setConfigurationProperty("http://saxon.sf.net/feature/generateByteCode", "false");
//gProcessor->setConfigurationProperty("http://saxon.sf.net/feature/licenseFileLocation", "./saxon-license.lic");
//std::cerr << "Xslt30Processor with Saxon version=" << processor->version() << endl << endl;
return 0;
}

API_DLL_PREFIX_DEF int WINAPI ClearContex(void* obj)
{
if (!obj)
return -1;
Xslt30Processor* proc = (Xslt30Processor*)obj;
delete proc;
return 0;
}

API_DLL_PREFIX_DEF int WINAPI MergeFiles(const char* xmlFile, const char* xslFile, const char* outFile, void*& obj)
{
Xslt30Processor* proc = NULL;
if (Init(obj, proc) < 0)
return -1;

proc->setInitialMatchSelectionAsFile(xmlFile);
std::string message;
proc->checkException(message);
proc->compileFromFile(xslFile);
proc->checkException(message);
proc->setGlobalContextFromFile(xmlFile);
proc->setOutputFile(outFile);
proc->applyTemplatesReturningFile(NULL, outFile);
proc->checkException(message);
proc->clearParameters(true);
proc->clearProperties();

return 0;
}

API_DLL_PREFIX_DEF int WINAPI MergeStringToFiles(const char* xmlStr, const char* xslFile, const char* outFile, void*& obj)
{
Xslt30Processor* proc = NULL;
if (Init(obj, proc) < 0)
return -1;

XdmNode * input = gProcessor->parseXmlFromString(xmlStr);
std::string message;
proc->checkException(message);
proc->compileFromFile(xslFile);
proc->checkException(message);
proc->setOutputFile(outFile);
proc->applyTemplatesReturningFile(NULL, outFile);
proc->checkException(message);

proc->clearParameters(true);
proc->clearProperties();
return 0;
}

API_DLL_PREFIX_DEF int WINAPI MergeFilesToString(const char* xmlFile, const char* xslFile, SaxonResultContainer* bufAlloc, void*& obj)
{
Xslt30Processor* proc = NULL;
if (Init(obj, proc) < 0)
return -1;

int ret = -1;
proc->setInitialMatchSelectionAsFile(xmlFile);
std::string message;
proc->checkException(message);
proc->compileFromFile(xslFile);
proc->checkException(message);
proc->setGlobalContextFromFile(xmlFile);
const char *tempStr = proc->applyTemplatesReturningString();
if (tempStr) {
bufAlloc->Duplicate(tempStr, strlen(tempStr));
ret = 0;
}
else
proc->checkException(message);

if (message.length() > 0)
bufAlloc->SetError(message.c_str(), message.length());
proc->clearParameters(true);
proc->clearProperties();

return ret;
}

API_DLL_PREFIX_DEF int WINAPI MergeStringToString(const char* xmlStr, const char* xslFile, SaxonResultContainer* bufAlloc, void*& obj)
{
Xslt30Processor* proc = NULL;
if (Init(obj, proc) < 0)
return -1;

int ret = -1;
XdmNode * input = gProcessor->parseXmlFromString(xmlStr);
std::string message;
proc->checkException(message);
proc->compileFromFile(xslFile);
proc->checkException(message);
const char * tempStr = proc->transformToString((XdmNode*)input);
message = proc->checkException(message);
if (tempStr) {
bufAlloc->Duplicate(tempStr, strlen(tempStr));
ret = 0;
}
else
proc->checkException(message);

if (message.length() > 0)
bufAlloc->SetError(message.c_str(), message.length());
proc->clearParameters(true);
proc->clearProperties();
return ret;
}

#ifndef __GNUC__
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
Destroy();
break;
}
return TRUE;
}
#endif
(2-2/2)