Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / XdmFunctionItem.cpp @ 6304fb8b

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->CallStaticIntMethod(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
                             }
38

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

    
43
    }
44

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

    
60
          } else {
61
            return arity;
62
          }
63

    
64
    }
65

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

    
83
                                                 } else {
84
                                                     return nullptr;
85

    
86
                                                 }
87

    
88
                                         }
89

    
90

    
91

    
92
    }
93

    
94
    XdmValue * XdmFunctionItem::call(XdmValue ** arguments, int argument_length) {
95
          if(argument_length > 0 && arguments == nullptr) {
96
                      std::cerr << "Error in XdmFunctionItem.call.  nullptr arguments found." << std::endl;
97
                      return nullptr;
98
          }
99
          if(proc != nullptr) {
100
                       jclass xdmFunctionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
101
                       static jmethodID xffmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetObjectMethodID(xdmFunctionClass,"call",
102
                       "(Lnet/sf/saxon/s9api/Procesor;[Lnet/sf/saxon/s9api/XdmValue)Lnet/sf/saxon/s9api/XdmValue");
103
                       if (!xffmID) {
104
                                 std::cerr << "Error: SaxonC." << "getSystemFunction" << " not found\n" << std::endl;
105
                                 return nullptr;
106
                       } else {
107

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

    
113

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

    
122

    
123
                                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(result, atomicValueClass)           == JNI_TRUE) {
124
                                                xdmItem =  new XdmAtomicValue(result);
125
                                                xdmItem->setProcessor(proc);
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
                                                xdmItem->setProcessor(proc);
132
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
133
                                                return xdmItem;
134
                                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(result, functionItemClass)           == JNI_TRUE) {
135
                                                xdmItem =  new XdmFunctionItem(result);
136
                                                xdmItem->setProcessor(proc);
137
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
138
                                                return xdmItem;
139
                                        } else {
140
                                                value = new XdmValue(result, true);
141
                                                value->setProcessor(proc);
142
                                                for(int z=0;z<value->size();z++) {
143
                                                        value->itemAt(z)->setProcessor(proc);
144
                                                }
145
                                                SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
146
                                                return value;
147
                                        }
148

    
149
                                 } else {
150
                                             return nullptr
151

    
152
                                 }
153

    
154
                       }
155
      } else {
156
         std::cerr << "Error in XdmFunctionItem.call.  Processor not set on the xdmFunctionItem." << std::endl;
157
         return nullptr;
158
      }
159

    
160
    }
161

    
162

    
(23-23/49)