Project

Profile

Help

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

he / latest9.9 / hec / samples / cppTests / cppExtensionFunction.cpp @ 5401a5ae

1
#include <sstream>
2

    
3
#include "../../Saxon.C.API/SaxonProcessor.h"
4
#include "../../Saxon.C.API/XdmValue.h"
5
#include "../../Saxon.C.API/XdmItem.h"
6
#include "../../Saxon.C.API/XdmNode.h"
7
#include "../../Saxon.C.API/XdmAtomicValue.h"
8
#include <string>
9

    
10
// TODO: write test case for checking parameters which are null
11

    
12

    
13
using namespace std;
14

    
15

    
16
/*
17
* Test transform to String. Source and stylesheet supplied as arguments
18
*/
19
jobject JNICALL cppNativeCall
20
  (jstring funcName, jobjectArray arguments, jobjectArray argTypes){
21

    
22

    
23
        JNIEnv *senv = SaxonProcessor::sxn_environ->env;
24
        
25
        if(funcName) {
26
                
27
        }
28
          std::cerr<<"Testing CPP extension function"<<std::endl;
29
        jint argLength = 0;
30
        //XdmItem ** params[] = NULL;
31
        if(senv == NULL) {
32
                std::cerr<<"senv is null"<<std::endl;
33
        }
34
        if(arguments != NULL && argTypes) {
35
                argLength  = senv->GetArrayLength(arguments);
36
                        
37
        //params = new XdmItem[argLength];
38
        }
39
        std::cerr<<"argLength"<<argLength<<std::endl;
40
        if(argLength>2){
41
                return NULL;
42
        }
43
        XdmItem ** params = new XdmItem*[argLength];
44

    
45
        std::map<std::string, saxonTypeEnum> typeMap;
46
        typeMap["node"] = enumNode;
47
        typeMap["string"] = enumString;
48
        typeMap["integer"] = enumInteger;
49
        typeMap["double"] = enumDouble;
50
        typeMap["float"] = enumFloat;
51
        typeMap["boolean"] = enumBool;
52
        typeMap["[xdmvalue"] = enumArrXdmValue;
53
        sxnc_value* sresult = (sxnc_value *)malloc(sizeof(sxnc_value));
54
        SaxonProcessor * nprocessor = new SaxonProcessor(true); //processor object created for XdmNode
55
        jclass xdmValueForCppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp");
56
        jmethodID procForNodeMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmValueForCppClass, "getProcessor", "(Ljava/lang/Object;)Lnet/sf/saxon/s9api/Processor;");
57
        
58
        for(int i=0; i<argLength;i++){
59

    
60
                jstring argType = (jstring)senv->GetObjectArrayElement(argTypes, i);
61
                jobject argObj = senv->GetObjectArrayElement(arguments, i);
62

    
63
                const char * str = senv->GetStringUTFChars(argType,NULL);
64
                
65
                std::cerr<<str<<std::endl;
66
         
67
                const char * stri = NULL;
68
                long lnumber = 0;
69
                float fnumber = 0;
70
                bool bvalue = false;
71
                double dnumber = 0;
72

    
73
                std::map<std::string, saxonTypeEnum>::iterator it = typeMap.find(str);
74
                if (it != typeMap.end()){
75
                        switch (it->second)
76
                        {
77
                                case enumNode:
78
                                        if(!nprocessor->proc){
79
                                                nprocessor->proc = (jobject)SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmValueForCppClass, procForNodeMID, argObj);
80
                                        }
81
                                        params[i] = new XdmNode(argObj);
82
                                        params[i]->setProcessor(nprocessor);
83
                                        
84
                                        
85
                                        break;
86
                                case enumString:
87
                                        stri = senv->GetStringUTFChars((jstring)argObj, NULL);
88
                                        if(stri !=NULL){}
89
                                        break;
90
                                case enumInteger:
91
                                        params[i] = new XdmAtomicValue(argObj, "");
92
                                        sresult->xdmvalue = argObj; 
93
                                        lnumber = getLongValue(SaxonProcessor::sxn_environ, *sresult, 0);
94
                                        if(lnumber == 0){}
95
                                                                                
96
                                        break;
97
                                case enumDouble:
98
                                        sresult->xdmvalue = argObj; 
99
                                        dnumber = getDoubleValue(SaxonProcessor::sxn_environ, *sresult, 0);
100
                                        if(dnumber == 0){}
101
                                        break;
102
                                case enumFloat:
103
                                        sresult->xdmvalue = argObj; 
104
                                        fnumber = getFloatValue(SaxonProcessor::sxn_environ, *sresult, 0);
105
                                        if(fnumber == 0){}                                
106
                                        break;
107
                                case enumBool:
108
                                        sresult->xdmvalue = argObj; 
109
                                        bvalue = getBooleanValue(SaxonProcessor::sxn_environ, *sresult);
110
                                        if(bvalue){}                                                
111
                                        break;
112
                                case enumArrXdmValue:
113
                                        //TODO - not currently supported
114
                                        argLength--;
115
                                        break;
116
                        }
117
                        senv->ReleaseStringUTFChars(argType, str);
118

    
119
                }
120

    
121
                senv->DeleteLocalRef(argType);
122
                senv->DeleteLocalRef(argObj);
123
}
124
        return SaxonProcessor::sxn_environ->env->NewStringUTF("result from extension!!");
125
}
126

    
127
string nativeExtensionMethod(char * param1, int number){
128

    
129
        if(param1!=NULL) {
130
                //do nothing
131
        }
132
        if(number == 0) {
133
                //do nothing
134
        }
135
        string result = "native-extension " ;//+ param1;
136
        return result;
137
}
138

    
139

    
(11-11/25)