Project

Profile

Help

Bug #5050 » SaxonClient.cpp

ofer benoliel, 2021-07-29 12:09

 
1
#include <sstream>
2
#include <stdio.h>
3
#include "SaxonProcessor.h"
4
#include "XdmValue.h"
5
#include "XdmItem.h"
6
#include "XdmNode.h"
7
#include <string>
8

    
9
//#define API_REDIS_CLIENT_IMPL
10
#ifdef __GNUC__
11
    #if __GNUC__ >= 4
12
            #define DLL_PUBLIC extern "C" __attribute__ ((visibility ("default")))
13
            #define DLL_LOCAL  __attribute__ ((visibility ("hidden")))
14
    #else
15
            #define DLL_PUBLIC extern "C"
16
            #define DLL_LOCAL
17
    #endif
18
    #define API_DLL_PREFIX_DEF DLL_PUBLIC
19
    #define WINAPI 
20
#else
21
        #define API_DLL_PREFIX_DEF 
22
#endif
23

    
24
#include "SaxonClient.h"
25

    
26
class SaxonResultContainer
27
{
28
public:
29
        SaxonResultContainer() {}
30
        virtual                        ~SaxonResultContainer() {}
31

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

    
37
SaxonProcessor * gProcessor = NULL;
38

    
39

    
40
void Destroy()
41
{
42
        if (gProcessor) {
43
                delete gProcessor;
44
                gProcessor = NULL;
45
        }
46
}
47

    
48
int Init(void*& obj, Xslt30Processor*& proc)
49
{
50
        if (!gProcessor)
51
                return -2;
52
        proc = (Xslt30Processor*&)obj;
53
        if (!proc) {
54
                proc = gProcessor->newXslt30Processor();
55
                obj = proc;
56
        }
57

    
58
        return 0;
59
}
60

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

    
73
API_DLL_PREFIX_DEF int WINAPI ClearContex(void* obj)
74
{
75
        if (!obj)
76
                return -1;
77
        Xslt30Processor* proc = (Xslt30Processor*)obj;
78
        delete proc;
79
        return 0;
80
}
81

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

    
88
        proc->setInitialMatchSelectionAsFile(xmlFile); 
89
        std::string  message;
90
        proc->checkException(message);
91
        proc->compileFromFile(xslFile); 
92
        proc->checkException(message);
93
        proc->setGlobalContextFromFile(xmlFile);
94
        proc->setOutputFile(outFile);
95
        proc->applyTemplatesReturningFile(NULL, outFile);
96
        proc->checkException(message);
97
        proc->clearParameters(true);
98
        proc->clearProperties();
99

    
100
        return 0;
101
}
102

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

    
109
        XdmNode * input = gProcessor->parseXmlFromString(xmlStr);
110
        std::string  message;
111
        proc->checkException(message);
112
        proc->compileFromFile(xslFile);
113
        proc->checkException(message);
114
        proc->setOutputFile(outFile);
115
        proc->applyTemplatesReturningFile(NULL, outFile);
116
        proc->checkException(message);
117

    
118
        proc->clearParameters(true);
119
        proc->clearProperties();
120
        return 0;
121
}
122

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

    
129
        int ret = -1;
130
        proc->setInitialMatchSelectionAsFile(xmlFile); 
131
        std::string  message;
132
        proc->checkException(message);
133
        proc->compileFromFile(xslFile); 
134
        proc->checkException(message);
135
        proc->setGlobalContextFromFile(xmlFile);
136
        const char *tempStr = proc->applyTemplatesReturningString();
137
        if (tempStr) {
138
                bufAlloc->Duplicate(tempStr, strlen(tempStr));
139
                ret = 0;
140
        }
141
        else 
142
                proc->checkException(message);
143

    
144
        if (message.length() > 0)
145
                bufAlloc->SetError(message.c_str(), message.length());
146
        proc->clearParameters(true);
147
        proc->clearProperties();
148

    
149
        return ret;
150
}
151

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

    
158
        int ret = -1;
159
        XdmNode * input = gProcessor->parseXmlFromString(xmlStr);
160
        std::string  message;
161
        proc->checkException(message);
162
        proc->compileFromFile(xslFile);
163
        proc->checkException(message);
164
        const char * tempStr = proc->transformToString((XdmNode*)input);
165
        message = proc->checkException(message);
166
        if (tempStr) {
167
                bufAlloc->Duplicate(tempStr, strlen(tempStr));
168
                ret = 0;
169
        }
170
        else 
171
                proc->checkException(message);
172

    
173
        if (message.length() > 0)
174
                bufAlloc->SetError(message.c_str(), message.length());
175
        proc->clearParameters(true);
176
        proc->clearProperties();
177
        return ret;
178
}
179

    
180
#ifndef __GNUC__
181
BOOL APIENTRY DllMain(HMODULE hModule,
182
        DWORD  ul_reason_for_call,
183
        LPVOID lpReserved
184
)
185
{
186
        switch (ul_reason_for_call)
187
        {
188
        case DLL_PROCESS_ATTACH:
189
                break;
190
        case DLL_THREAD_ATTACH:
191
        case DLL_THREAD_DETACH:
192
                break;
193
        case DLL_PROCESS_DETACH:
194
                Destroy();
195
                break;
196
        }
197
        return TRUE;
198
}
199
#endif
(2-2/2)