Project

Profile

Help

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

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

1 72bf04c6 Norman Walsh
2
//
3
4
#include "XdmFunctionItem.h"
5
6 55b80284 O'Neil Delpratt
#ifdef MEM_DEBUG
7
#define new new(__FILE__, __LINE__)
8
#endif
9
10 72bf04c6 Norman Walsh
    XdmFunctionItem::XdmFunctionItem():XdmItem(), arity(-1){}
11
12 ead48a5d O'Neil Delpratt
13 72bf04c6 Norman Walsh
    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 ead48a5d O'Neil Delpratt
                             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getFunctionName",
27 72bf04c6 Norman Walsh
                                                     "(Lnet/sf/saxon/s9api/XdmFunctionItem;)Ljava/lang/String;");
28 ead48a5d O'Neil Delpratt
                             if (!xmID) {
29 72bf04c6 Norman Walsh
                                     std::cerr << "Error: SaxonC." << "getFunctionName"
30
                                             << " not found\n" << std::endl;
31 ead48a5d O'Neil Delpratt
                                     return nullptr;
32 72bf04c6 Norman Walsh
                             } else {
33 0992020d O'Neil Delpratt
                                     jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, value));
34 72bf04c6 Norman Walsh
                                     if(result) {
35 ead48a5d O'Neil Delpratt
                                                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
36 72bf04c6 Norman Walsh
37
                                            SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
38
                                                fname = std::string(stri);
39
                                                return stri;
40
                        }
41 8d5b64f3 O'Neil Delpratt
                        return nullptr;
42 72bf04c6 Norman Walsh
                             }
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 ead48a5d O'Neil Delpratt
                             static jmethodID bfmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmFunctionClass,
54 72bf04c6 Norman Walsh
                                                     "getArity",
55
                                                     "()I");
56 ead48a5d O'Neil Delpratt
                             if (!bfmID) {
57 72bf04c6 Norman Walsh
                                     std::cerr << "Error: SaxonC." << "getArity"
58
                                             << " not found\n" << std::endl;
59 8d5b64f3 O'Neil Delpratt
                                     return 0;
60 72bf04c6 Norman Walsh
                             } else {
61 ead48a5d O'Neil Delpratt
                                     jint result = (jint)(SaxonProcessor::sxn_environ->env->CallIntMethod(value, bfmID));
62 72bf04c6 Norman Walsh
                                     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 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
        }
76
             jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
77 ead48a5d O'Neil Delpratt
             static jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getSystemFunction",
78 72bf04c6 Norman Walsh
             "(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 ead48a5d O'Neil Delpratt
                                                 return nullptr;
82 72bf04c6 Norman Walsh
                                         } else {
83 ead48a5d O'Neil Delpratt
                                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, processor->proc, SaxonProcessor::sxn_environ->env->NewStringUTF(name), arity));
84 72bf04c6 Norman Walsh
                                                 if(result) {
85 ead48a5d O'Neil Delpratt
                                                     auto functionItem = new XdmFunctionItem(result);
86 72bf04c6 Norman Walsh
                                                     return functionItem;
87
88
                                                 } else {
89 ead48a5d O'Neil Delpratt
                                                     return nullptr;
90 72bf04c6 Norman Walsh
91
                                                 }
92
93
                                         }
94
95
96
97
    }
98
99
    XdmValue * XdmFunctionItem::call(XdmValue ** arguments, int argument_length) {
100 ead48a5d O'Neil Delpratt
          if(argument_length > 0 && arguments == nullptr) {
101
                      std::cerr << "Error in XdmFunctionItem.call.  nullptr arguments found." << std::endl;
102
                      return nullptr;
103 72bf04c6 Norman Walsh
          }
104 901b6eca O'Neil Delpratt
105 72bf04c6 Norman Walsh
                       jclass xdmFunctionClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
106 ead48a5d O'Neil Delpratt
                       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 72bf04c6 Norman Walsh
                                 std::cerr << "Error: SaxonC." << "getSystemFunction" << " not found\n" << std::endl;
110 ead48a5d O'Neil Delpratt
                                 return nullptr;
111 72bf04c6 Norman Walsh
                       } else {
112
113
                                 jobjectArray argumentJArray = SaxonProcessor::createJArray(arguments, argument_length);
114 501beb26 O'Neil Delpratt
                                 jobject result = (jobject)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmFunctionClass, xffmID, cpp_processor->proc, argumentJArray));
115 ead48a5d O'Neil Delpratt
                                 if(argumentJArray != nullptr) {
116 72bf04c6 Norman Walsh
                                            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 ead48a5d O'Neil Delpratt
                                        XdmValue * value = nullptr;
125
                                        XdmItem * xdmItem = nullptr;
126 72bf04c6 Norman Walsh
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 501beb26 O'Neil Delpratt
                                             return nullptr;
149 72bf04c6 Norman Walsh
150
                                 }
151
152
                       }
153
154
    }
155