Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmFunctionItem.cpp @ 9e416749

1

    
2
//
3

    
4
#include "XdmFunctionItem.h"
5

    
6
    XdmFunctionItem::XdmFunctionItem():XdmItem(), arity(-1){}
7

    
8

    
9
    XdmFunctionItem::XdmFunctionItem(const XdmFunctionItem &aVal): XdmItem(aVal){
10
        arity = aVal.arity;
11
    }
12

    
13
   
14
        
15

    
16
    XdmFunctionItem::XdmFunctionItem(jobject obj):XdmItem(obj), arity(-1){
17
    }
18

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

    
33
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
34
                                                fname = std::string(stri);
35
                                                return stri;
36
                        }
37
                        return nullptr;
38
                             }
39

    
40
          } else {
41
            return fname.c_str();
42
          }
43

    
44
    }
45

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

    
61
          } else {
62
            return arity;
63
          }
64

    
65
    }
66

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

    
84
                                                 } else {
85
                                                     return nullptr;
86

    
87
                                                 }
88

    
89
                                         }
90

    
91

    
92

    
93
    }
94

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

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

    
109
                                 jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
110
                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmFunctionClass, xffmID, cpp_processor->proc, argumentJArray));
111
                                 if(argumentJArray != nullptr) {
112
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(argumentJArray);
113

    
114

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

    
123

    
124
                                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
125
                                                xdmItem =  new XdmAtomicValue(result);
126
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
127
                                                return xdmItem;
128

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

    
143
                                 } else {
144
                                             return nullptr;
145

    
146
                                 }
147

    
148
                       }
149

    
150
    }
151

    
152

    
(27-27/55)