Project

Profile

Help

How to connect?
Download (14 KB) Statistics
| Branch: | Revision:

he / src / main / c / Saxon.C.API / SaxonCProcessor.c @ 4ee4355d

1
#include "SaxonCProcessor.h"
2

    
3

    
4
/*
5
 * Get the Saxon version 
6
 */
7
const char * version(sxnc_environment environi) {
8

    
9

    
10
    jmethodID MID_version;
11
    jclass  versionClass;
12
     versionClass = lookForClass(environi.env, "net/sf/saxon/Version");
13
    char methodName[] = "getProductVersion";
14
    char args[] = "()Ljava/lang/String;";
15
    MID_version = (jmethodID)(*(environi.env))->GetStaticMethodID(environi.env, versionClass, methodName, args);
16
    if (!MID_version) {
17
        printf("\nError: MyClassInDll %s() not found\n",methodName);
18
        fflush (stdout);
19
        return NULL;
20
    }
21
   jstring jstr = (jstring)((*(environi.env))->CallStaticObjectMethod(environi.env, versionClass, MID_version));
22
   const char * str = (*(environi.env))->GetStringUTFChars(environi.env, jstr, NULL);
23
  
24
    //(*(environi.env))->ReleaseStringUTFChars(environi.env, jstr,str);
25
    return str;
26
}
27

    
28

    
29
/*
30
 * Get the Saxon version 
31
 */
32
const char * getProductVariantAndVersion(sxnc_environment environi) {
33

    
34

    
35
    jmethodID MID_version;
36
    jclass  versionClass;
37
     versionClass = lookForClass(environi.env, "net/sf/saxon/Version");
38
    char methodName[] = "getProductVariantAndVersion";
39
    char args[] = "()Ljava/lang/String;";
40
    MID_version = (jmethodID)(*(environi.env))->GetStaticMethodID(environi.env, versionClass, methodName, args);
41
    if (!MID_version) {
42
        printf("\nError: SaxonCDll %s() not found\n",methodName);
43
        fflush (stdout);
44
        return NULL;
45
    }
46
   jstring jstr = (jstring)((*(environi.env))->CallStaticObjectMethod(environi.env, versionClass, MID_version));
47
   const char * str = (*(environi.env))->GetStringUTFChars(environi.env, jstr, NULL);
48
  
49
    (*(environi.env))->ReleaseStringUTFChars(environi.env, jstr,str);
50
    return str;
51
}
52

    
53
void initSaxonc(sxnc_environment ** environi, sxnc_processor ** proc, sxnc_parameter **param, sxnc_property ** prop, int cap, int propCap){
54
    
55
    *param = (sxnc_parameter *)calloc(cap, sizeof(sxnc_parameter));
56
    *prop = (sxnc_property *)calloc(propCap, sizeof(sxnc_property));
57
    * environi =  (sxnc_environment *)malloc(sizeof(sxnc_environment));
58
    *proc = (sxnc_processor *)malloc(sizeof(sxnc_processor));
59
}
60

    
61

    
62
void freeSaxonc(sxnc_environment ** environi, sxnc_processor ** proc, sxnc_parameter **param, sxnc_property ** prop){
63
        free(*environi);
64
        free(*proc);
65
        free(*param);
66
        free(*prop);
67
}
68

    
69
void xsltSaveResultToFile(sxnc_environment environi, sxnc_processor ** proc, char * cwd, char * source, char* stylesheet, char* outputfile, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen) {
70
        jclass cppClass = lookForClass(environi.env, "net/sf/saxon/option/cpp/XsltProcessor");
71
        static jmethodID xsltFileID = NULL; //cache the methodID
72
         
73
        if(!cpp) {
74
                cpp = (jobject) createSaxonProcessor (environi.env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
75
        }
76
         
77
        if(!xsltFileID) {
78
                 xsltFileID = (jmethodID)(*(environi.env))->GetMethodID (environi.env, cppClass,"transformToFile", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
79
                 if (!xsltFileID) {
80
                        printf("Error: MyClassInDll. transformToFile not found\n");
81
                        fflush (stdout);
82
                        return;
83
                    } 
84
        }
85
        
86
         jobjectArray stringArray = NULL;
87
        jobjectArray objectArray = NULL;
88
        int size = parLen + propLen+1;
89
        
90
        
91

    
92
        if(size>0) {
93
           jclass objectClass = lookForClass(environi.env, "java/lang/Object");
94
           jclass stringClass = lookForClass(environi.env, "java/lang/String");
95
           objectArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, objectClass, 0 );
96
           stringArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, stringClass, 0 );
97
        if(size >0) {
98

    
99
           if((!objectArray) || (!stringArray)) { 
100
                printf("Error: parameter and property have some inconsistencies\n");
101
                fflush (stdout);
102
                return;}
103
           int i=0;
104
           for( i =0; i< parLen; i++) {
105
                
106
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,  parameters[i].name) );
107
                
108
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)(parameters[i].value) );
109
           }
110
            (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,"resources"));
111
           (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, getResourceDirectory())) );
112
           i++;
113
            int j=0;
114
             for(; j< propLen; j++, i++) {
115
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, properties[j].name));
116
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, properties[j].value)) );
117
           }
118
           
119
        }
120
        }
121

    
122
      (*(environi.env))->CallVoidMethod(environi.env, cpp, xsltFileID, (cwd== NULL ? (*(environi.env))->NewStringUTF(environi.env, "") : (*(environi.env))->NewStringUTF(environi.env, cwd)),(*(environi.env))->NewStringUTF(environi.env, source), (*(environi.env))->NewStringUTF(environi.env, stylesheet), (*(environi.env))->NewStringUTF(environi.env, outputfile), stringArray, objectArray );
123
        if(size>0) {    
124
          (*(environi.env))->DeleteLocalRef(environi.env, objectArray);
125
          (*(environi.env))->DeleteLocalRef(environi.env, stringArray);
126
        }
127
#ifdef DEBUG
128
        checkForException( *(environi.env), cpp);
129
#endif
130
  
131
}
132

    
133
const char * xsltApplyStylesheet(sxnc_environment environi, sxnc_processor ** proc, char * cwd, const char * source, const char* stylesheet, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen) {
134
        static jmethodID mID = NULL; //cache the methodID
135

    
136
        jclass cppClass = lookForClass(environi.env, "net/sf/saxon/option/cpp/XsltProcessor");
137

    
138
        if(!cpp) {
139
                cpp = (jobject) createSaxonProcessor (environi.env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
140
        }
141
#ifdef DEBUG
142
        jmethodID debugMID = (*(environi.env))->GetStaticMethodID(environi.env, cppClass, "setDebugMode", "(Z)V");
143
        (*(environi.env))->CallStaticVoidMethod(environi.env, cppClass, debugMID, (jboolean)true);
144
#endif
145
        if(mID == NULL) {
146
                 mID = (jmethodID)(*(environi.env))->GetMethodID (environi.env, cppClass,"transformToString", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
147
                 if (!mID) {
148
                        printf("Error: MyClassInDll. xsltApplyStylesheet not found\n");
149
                        fflush (stdout);
150
                        return 0;
151
                  } 
152
        }
153

    
154
         jobjectArray stringArray = NULL;
155
        jobjectArray objectArray = NULL;
156
        int size = parLen + propLen+1; //We add one here for the resources-dir property
157

    
158
        if(size >0) {
159
           jclass objectClass = lookForClass(environi.env, "java/lang/Object");
160
           jclass stringClass = lookForClass(environi.env, "java/lang/String");
161
           objectArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, objectClass, 0 );
162
           stringArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, stringClass, 0 );
163
           if((!objectArray) || (!stringArray)) { return NULL;}
164
           int i=0;
165
           for(i =0; i< parLen; i++) {
166
                
167
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, parameters[i].name) );
168
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)(parameters[i].value) );
169
        
170
           }
171

    
172
           (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,"resources"));
173
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, getResourceDirectory())) );
174
            i++;
175
            int j=0;                
176
             for(; j<propLen; j++, i++) {
177
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, properties[j].name));
178
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, properties[j].value)) );
179
           }
180
          
181
        }
182

    
183
      jstring result = (jstring)((*(environi.env))->CallObjectMethod(environi.env, cpp, mID, (cwd== NULL ? (*(environi.env))->NewStringUTF(environi.env, "") : (*(environi.env))->NewStringUTF(environi.env, cwd)), (*(environi.env))->NewStringUTF(environi.env, source), (*(environi.env))->NewStringUTF(environi.env, stylesheet), stringArray, objectArray ));
184

    
185
        
186
      if(result) {
187
        const char * str = (*(environi.env))->GetStringUTFChars(environi.env, result, NULL);
188
        return str;
189
     }
190

    
191
    checkForException( *(environi.env), cpp);
192
    return 0;
193
}
194

    
195

    
196
void executeQueryToFile(sxnc_environment environi, sxnc_processor ** proc, char * cwd, char* outputfile, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
197
        static jmethodID queryFileID = NULL; //cache the methodID
198
        jclass cppClass = lookForClass(environi.env, "net/sf/saxon/option/cpp/XQueryEngine");
199
         
200
        if(!cpp) {
201
                cpp = (jobject) createSaxonProcessor (environi.env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
202
        }
203
         
204
        if(queryFileID == NULL) {
205
                queryFileID = (jmethodID)(*(environi.env))->GetMethodID (environi.env, cppClass,"executeQueryToFile", "(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)V");
206
                 if (!queryFileID) {
207
                        printf("Error: MyClassInDll. executeQueryToString not found\n");
208
                        fflush (stdout);
209
                        return;
210
                    } 
211
        }
212
        
213
         jobjectArray stringArray = NULL;
214
        jobjectArray objectArray = NULL;
215
        int size = parLen + propLen+1;
216
        
217
        
218

    
219
        if(size>0) {
220
           jclass objectClass = lookForClass(environi.env, "java/lang/Object");
221
           jclass stringClass = lookForClass(environi.env, "java/lang/String");
222
           objectArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, objectClass, 0 );
223
           stringArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, stringClass, 0 );
224
        if(size >0) {
225

    
226
           if((!objectArray) || (!stringArray)) { return;}
227
           int i=0;
228
           for( i =0; i< parLen; i++) {
229
                
230
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,  parameters[i].name) );
231
                
232
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)(parameters[i].value) );
233
           }
234
            (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,"resources"));
235
           (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, getResourceDirectory())) );
236
           i++;
237
          int j=0;
238
             for(; j<propLen; i++, j++) {
239
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, properties[j].name));
240
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, properties[j].value)) );
241
           }
242
           
243
        }
244
        }
245
      (*(environi.env))->CallVoidMethod(environi.env,cpp, queryFileID, (cwd== NULL ? (*(environi.env))->NewStringUTF(environi.env, "") : (*(environi.env))->NewStringUTF(environi.env, cwd)), (*(environi.env))->NewStringUTF(environi.env, outputfile), stringArray, objectArray );
246
          (*(environi.env))->DeleteLocalRef(environi.env, objectArray);
247
          (*(environi.env))->DeleteLocalRef(environi.env, stringArray);
248

    
249
}
250

    
251
const char * executeQueryToString(sxnc_environment environi, sxnc_processor ** proc, char * cwd, sxnc_parameter *parameters, sxnc_property * properties, int parLen, int propLen){
252
        static jmethodID queryStrID = NULL; //cache the methodID
253
        jclass cppClass = lookForClass(environi.env, "net/sf/saxon/option/cpp/XQueryEngine");
254

    
255
        if(!cpp) {
256
                cpp = (jobject) createSaxonProcessor (environi.env, cppClass, "(Z)V", NULL, (jboolean)sxn_license);
257
        }
258

    
259
        if(queryStrID == NULL) {
260
                queryStrID = (jmethodID)(*(environi.env))->GetMethodID (environi.env, cppClass,"executeQueryToString", "(Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;");
261
                if (!queryStrID) {
262
                        printf("Error: MyClassInDll. executeQueryToString not found\n");
263
                        fflush (stdout);
264
                        return 0;
265
                 } 
266
        }
267
         
268
        jobjectArray stringArray = NULL;
269
        jobjectArray objectArray = NULL;
270
        int size = parLen + propLen+1; //We add one here for the resources-dir property
271

    
272
        if(size >0) {
273
           jclass objectClass = lookForClass(environi.env, "java/lang/Object");
274
           jclass stringClass = lookForClass(environi.env, "java/lang/String");
275
           objectArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, objectClass, 0 );
276
           stringArray = (*(environi.env))->NewObjectArray(environi.env, (jint)size, stringClass, 0 );
277
           if((!objectArray) || (!stringArray)) { return NULL;}
278
           int i=0;
279
           for(i =0; i< parLen; i++) {
280
                
281
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, parameters[i].name) );
282
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)(parameters[i].value) );
283
        
284
           }
285

    
286

    
287
           (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env,"resources"));
288
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, getResourceDirectory())) );
289
            i++;
290
            int j=0;        
291
             for(; j<propLen; i++, j++) {
292
             (*(environi.env))->SetObjectArrayElement(environi.env, stringArray, i, (*(environi.env))->NewStringUTF(environi.env, properties[j].name));
293
             (*(environi.env))->SetObjectArrayElement(environi.env, objectArray, i, (jobject)((*(environi.env))->NewStringUTF(environi.env, properties[j].value)) );
294
           }
295
          
296
        }
297
      jstring result = (jstring)((*(environi.env))->CallObjectMethod(environi.env, cpp, queryStrID, (cwd== NULL ? (*(environi.env))->NewStringUTF(environi.env, "") : (*(environi.env))->NewStringUTF(environi.env, cwd)), stringArray, objectArray ));
298

    
299
      (*(environi.env))->DeleteLocalRef(environi.env, objectArray);
300
      (*(environi.env))->DeleteLocalRef(environi.env, stringArray);
301
      if(result) {
302

    
303
       const char * str = (*(environi.env))->GetStringUTFChars(environi.env, result, NULL);
304
       //return "result should be ok";       
305
        //checkForException( environi, cpp);     
306
        return str;
307
     }
308

    
309
    checkForException( *(environi.env));
310
    return 0;
311

    
312
}
313

    
314

    
315

    
316

    
317

    
318

    
319

    
(9-9/55)