Project

Profile

Help

Download (7.34 KB) Statistics
| Branch: | Revision:

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

1 72bf04c6 Norman Walsh
2
//
3
4
#include "XdmFunctionItem.h"
5
6
    XdmFunctionItem::XdmFunctionItem():XdmItem(), arity(-1){}
7
8 ead48a5d O'Neil Delpratt
9 72bf04c6 Norman Walsh
    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 ead48a5d O'Neil Delpratt
                             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getFunctionName",
23 72bf04c6 Norman Walsh
                                                     "(Lnet/sf/saxon/s9api/XdmFunctionItem;)Ljava/lang/String;");
24 ead48a5d O'Neil Delpratt
                             if (!xmID) {
25 72bf04c6 Norman Walsh
                                     std::cerr << "Error: SaxonC." << "getFunctionName"
26
                                             << " not found\n" << std::endl;
27 ead48a5d O'Neil Delpratt
                                     return nullptr;
28 72bf04c6 Norman Walsh
                             } else {
29 0992020d O'Neil Delpratt
                                     jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, value));
30 72bf04c6 Norman Walsh
                                     if(result) {
31 ead48a5d O'Neil Delpratt
                                                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
32 72bf04c6 Norman Walsh
33
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
34
                                                fname = std::string(stri);
35
                                                return stri;
36
                        }
37 8d5b64f3 O'Neil Delpratt
                        return nullptr;
38 72bf04c6 Norman Walsh
                             }
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 ead48a5d O'Neil Delpratt
                             static jmethodID bfmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmFunctionClass,
50 72bf04c6 Norman Walsh
                                                     "getArity",
51
                                                     "()I");
52 ead48a5d O'Neil Delpratt
                             if (!bfmID) {
53 72bf04c6 Norman Walsh
                                     std::cerr << "Error: SaxonC." << "getArity"
54
                                             << " not found\n" << std::endl;
55 8d5b64f3 O'Neil Delpratt
                                     return 0;
56 72bf04c6 Norman Walsh
                             } else {
57 ead48a5d O'Neil Delpratt
                                     jint result = (jint)(SaxonProcessor::sxn_environ->env->CallIntMethod(value, bfmID));
58 72bf04c6 Norman Walsh
                                     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 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
        }
72
             jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
73 ead48a5d O'Neil Delpratt
             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getSystemFunction",
74 72bf04c6 Norman Walsh
             "(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 ead48a5d O'Neil Delpratt
                                                 return nullptr;
78 72bf04c6 Norman Walsh
                                         } else {
79 ead48a5d O'Neil Delpratt
                                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, processor->proc, SaxonProcessor::sxn_environ->env->NewStringUTF(name), arity));
80 72bf04c6 Norman Walsh
                                                 if(result) {
81 ead48a5d O'Neil Delpratt
                                                     auto functionItem = new XdmFunctionItem(result);
82 72bf04c6 Norman Walsh
                                                     return functionItem;
83
84
                                                 } else {
85 ead48a5d O'Neil Delpratt
                                                     return nullptr;
86 72bf04c6 Norman Walsh
87
                                                 }
88
89
                                         }
90
91
92
93
    }
94
95
    XdmValue * XdmFunctionItem::call(XdmValue ** arguments, int argument_length) {
96 ead48a5d O'Neil Delpratt
          if(argument_length > 0 && arguments == nullptr) {
97
                      std::cerr << "Error in XdmFunctionItem.call.  nullptr arguments found." << std::endl;
98
                      return nullptr;
99 72bf04c6 Norman Walsh
          }
100 901b6eca O'Neil Delpratt
101 72bf04c6 Norman Walsh
                       jclass xdmFunctionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
102 ead48a5d O'Neil Delpratt
                       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 72bf04c6 Norman Walsh
                                 std::cerr << "Error: SaxonC." << "getSystemFunction" << " not found\n" << std::endl;
106 ead48a5d O'Neil Delpratt
                                 return nullptr;
107 72bf04c6 Norman Walsh
                       } else {
108
109
                                 jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
110 501beb26 O'Neil Delpratt
                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmFunctionClass, xffmID, cpp_processor->proc, argumentJArray));
111 ead48a5d O'Neil Delpratt
                                 if(argumentJArray != nullptr) {
112 72bf04c6 Norman Walsh
                                            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 ead48a5d O'Neil Delpratt
                                        XdmValue * value = nullptr;
121
                                        XdmItem * xdmItem = nullptr;
122 72bf04c6 Norman Walsh
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 501beb26 O'Neil Delpratt
                                             return nullptr;
145 72bf04c6 Norman Walsh
146
                                 }
147
148
                       }
149
150
    }
151