Project

Profile

Help

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

he / src / main / c / samples / cppTests / cppExtensionFunction.cpp @ 0c4104cf

1

    
2

    
3

    
4
#include "cppExtensionFunction.h"
5

    
6
/*
7
 * #include <sstream>
8
#include "../../Saxon.C.API/SaxonProcessor.h"
9
#include "../../Saxon.C.API/XdmValue.h"
10
#include "../../Saxon.C.API/XdmItem.h"
11
#include "../../Saxon.C.API/XdmNode.h"
12
#include "../../Saxon.C.API/XdmAtomicValue.h"
13
#include <string> */
14

    
15
// TODO: write test case for checking parameters which are null
16

    
17

    
18
//using namespace std;
19

    
20

    
21
/*
22
* Test transform to String. Source and stylesheet supplied as arguments
23
*/
24
jobject JNICALL cppExtensionFunction::cppNativeCall
25
  (jstring funcName, jobjectArray arguments, jobjectArray argTypes){
26

    
27

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

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

    
65
                jstring argType = (jstring)senv->GetObjectArrayElement(argTypes, i);
66
                jobject argObj = senv->GetObjectArrayElement(arguments, i);
67

    
68
                const char * str = senv->GetStringUTFChars(argType,NULL);
69
                
70
                std::cerr<<str<<std::endl;
71
         
72
                const char * stri = NULL;
73
                long lnumber = 0;
74
                float fnumber = 0;
75
                bool bvalue = false;
76
                double dnumber = 0;
77

    
78
                std::map<std::string, saxonTypeEnum>::iterator it = typeMap.find(str);
79
                if (it != typeMap.end()){
80
                        switch (it->second)
81
                        {
82
                                case enumNode:
83
                                        if(!nprocessor->proc){
84
                                                nprocessor->proc = (jobject)SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmValueForCppClass, procForNodeMID, argObj);
85
                                        }
86
                                        params[i] = new XdmNode(argObj);
87

    
88
                                        break;
89
                                case enumString:
90
                                        stri = senv->GetStringUTFChars((jstring)argObj, NULL);
91
                                        if(stri !=NULL){}
92
                                        break;
93
                                case enumInteger:
94
                                        params[i] = new XdmAtomicValue(argObj, "");
95
                                        sresult->xdmvalue = argObj; 
96
                                        lnumber = getLongValue(SaxonProcessor::sxn_environ, *sresult, 0);
97
                                        if(lnumber == 0){}
98
                                                                                
99
                                        break;
100
                                case enumDouble:
101
                                        sresult->xdmvalue = argObj; 
102
                                        dnumber = getDoubleValue(SaxonProcessor::sxn_environ, *sresult, 0);
103
                                        if(dnumber == 0){}
104
                                        break;
105
                                case enumFloat:
106
                                        sresult->xdmvalue = argObj; 
107
                                        fnumber = getFloatValue(SaxonProcessor::sxn_environ, *sresult, 0);
108
                                        if(fnumber == 0){}                                
109
                                        break;
110
                                case enumBool:
111
                                        sresult->xdmvalue = argObj; 
112
                                        bvalue = getBooleanValue(SaxonProcessor::sxn_environ, *sresult);
113
                                        if(bvalue){}                                                
114
                                        break;
115
                                case enumArrXdmValue:
116
                                        //TODO - not currently supported
117
                                        argLength--;
118
                                        break;
119
                        }
120
                        senv->ReleaseStringUTFChars(argType, str);
121

    
122
                }
123

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

    
130
string cppExtensionFunction::nativeExtensionMethod(char * param1, int number){
131

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

    
142

    
(13-13/28)