Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmFunctionItem.cpp @ 7728ae0b

1

    
2
//
3

    
4
#include "XdmFunctionItem.h"
5

    
6
#ifdef MEM_DEBUG
7
#define new new(__FILE__, __LINE__)
8
#endif
9

    
10
    XdmFunctionItem::XdmFunctionItem():XdmItem(), arity(-1){}
11

    
12

    
13
    XdmFunctionItem::XdmFunctionItem(const XdmFunctionItem &aVal): XdmItem(aVal){
14
        arity = aVal.arity;
15
    }
16

    
17
   
18
        
19

    
20
    XdmFunctionItem::XdmFunctionItem(jobject obj):XdmItem(obj), arity(-1){
21
    }
22

    
23
    const char* XdmFunctionItem::getName(){
24
          if(fname.empty()) {
25
             jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
26
                             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getFunctionName",
27
                                                     "(Lnet/sf/saxon/s9api/XdmFunctionItem;)Ljava/lang/String;");
28
                             if (!xmID) {
29
                                     std::cerr << "Error: SaxonC." << "getFunctionName"
30
                                             << " not found\n" << std::endl;
31
                                     return nullptr;
32
                             } else {
33
                                     jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, value));
34
                                     if(result) {
35
                                                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
36

    
37
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
38
                                                fname = std::string(stri);
39
                                                return stri;
40
                        }
41
                        return nullptr;
42
                             }
43

    
44
          } else {
45
            return fname.c_str();
46
          }
47

    
48
    }
49

    
50
    int XdmFunctionItem::getArity(){
51
          if(arity == -1) {
52
             jclass xdmFunctionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
53
                             static jmethodID bfmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmFunctionClass,
54
                                                     "getArity",
55
                                                     "()I");
56
                             if (!bfmID) {
57
                                     std::cerr << "Error: SaxonC." << "getArity"
58
                                             << " not found\n" << std::endl;
59
                                     return 0;
60
                             } else {
61
                                     jint result = (jint)(SaxonProcessor::sxn_environ->env->CallIntMethod(value, bfmID));
62
                                     return (int)result;
63
                             }
64

    
65
          } else {
66
            return arity;
67
          }
68

    
69
    }
70

    
71
    XdmFunctionItem * XdmFunctionItem::getSystemFunction(SaxonProcessor * processor, const char * name, int arity){
72
        if(processor == nullptr || name == nullptr) {
73
            std::cerr << "Error in getSystemFunction. Please make sure processor and name are not nullptr." << std::endl;
74
             return nullptr;
75
        }
76
             jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
77
             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getSystemFunction",
78
             "(Lnet/sf/saxon/s9api/Procesor;Ljava/lang/String;I)Lnet/sf/saxon/s9api/XdmFunctionItem");
79
             if (!xmID) {
80
                       std::cerr << "Error: SaxonC." << "getSystemFunction" << " not found\n" << std::endl;
81
                                                 return nullptr;
82
                                         } else {
83
                                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, processor->proc, SaxonProcessor::sxn_environ->env->NewStringUTF(name), arity));
84
                                                 if(result) {
85
                                                     auto functionItem = new XdmFunctionItem(result);
86
                                                     return functionItem;
87

    
88
                                                 } else {
89
                                                     return nullptr;
90

    
91
                                                 }
92

    
93
                                         }
94

    
95

    
96

    
97
    }
98

    
99
    XdmValue * XdmFunctionItem::call(XdmValue ** arguments, int argument_length) {
100
          if(argument_length > 0 && arguments == nullptr) {
101
                      std::cerr << "Error in XdmFunctionItem.call.  nullptr arguments found." << std::endl;
102
                      return nullptr;
103
          }
104

    
105
                       jclass xdmFunctionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
106
                       static jmethodID xffmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmFunctionClass,"call",
107
                       "(Lnet/sf/saxon/s9api/Processor;[Lnet/sf/saxon/s9api/XdmValue)Lnet/sf/saxon/s9api/XdmValue");
108
                       if (!xffmID) {
109
                                 std::cerr << "Error: SaxonC." << "getSystemFunction" << " not found\n" << std::endl;
110
                                 return nullptr;
111
                       } else {
112

    
113
                                 jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
114
                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmFunctionClass, xffmID, cpp_processor->proc, argumentJArray));
115
                                 if(argumentJArray != nullptr) {
116
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
117

    
118

    
119
                                 }
120
                                 if(result) {
121
                                        jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
122
                                        jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
123
                                        jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
124
                                        XdmValue * value = nullptr;
125
                                        XdmItem * xdmItem = nullptr;
126

    
127

    
128
                                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
129
                                                xdmItem =  new XdmAtomicValue(result);
130
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
131
                                                return xdmItem;
132

    
133
                                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, nodeClass)           == JNI_TRUE) {
134
                                                xdmItem =  new XdmNode(result);
135
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
136
                                                return xdmItem;
137
                                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
138
                                                xdmItem =  new XdmFunctionItem(result);
139
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
140
                                                return xdmItem;
141
                                        } else {
142
                                                value = new XdmValue(result, true);
143
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
144
                                                return value;
145
                                        }
146

    
147
                                 } else {
148
                                             return nullptr;
149

    
150
                                 }
151

    
152
                       }
153

    
154
    }
155

    
156

    
(28-28/56)