Project

Profile

Help

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

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

1
#include "XdmMap.h"
2
#include "XdmAtomicValue.h"
3
#include <map>
4

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

    
9

    
10
XdmMap::XdmMap(): XdmFunctionItem() {
11

    
12
}
13

    
14
XdmMap::XdmMap(const XdmMap &d) {
15
      arity = d.arity;
16
      data = d.data; //TODO check if copy works
17
    }
18

    
19
XdmMap::XdmMap(jobject obj): XdmFunctionItem(obj) {}
20

    
21

    
22

    
23

    
24
    int XdmMap::mapSize(){
25
        return  data.size();
26
    }
27

    
28
    XdmValue * XdmMap::get(XdmAtomicValue* key) {
29
    jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
30
    static jmethodID tbmapID = nullptr;
31

    
32
    if(tbmapID == nullptr) {
33
    tbmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
34
                                            "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Lnet/sf/saxon/s9api/XdmValue;");
35

    
36
    }
37
    if (!tbmapID) {
38
                            std::cerr << "Error: Saxonc.XdmMap." << "get"
39
                                    << " not found\n" << std::endl;
40
                            return nullptr;
41
    } else {
42
                            jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbmapID, ((XdmItem *)key)->getUnderlyingValue()));
43
                            if(valueObj) {
44
                                    XdmValue * newValue = new XdmValue();
45
                                    newValue->addUnderlyingValue(valueObj);
46
                                    return newValue;
47
                            }
48
                            return nullptr;
49
    }
50

    
51
    }
52

    
53
    XdmValue * XdmMap::get(const char * key) {
54
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
55
        static jmethodID tbsmapID = nullptr;
56

    
57
        if(tbsmapID == nullptr) {
58
        tbsmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
59
                                                "(Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;");
60

    
61
        }
62
        if (!tbsmapID) {
63
                                std::cerr << "Error: Saxonc.XdmMap." << "get"
64
                                        << " not found\n" << std::endl;
65
                                return nullptr;
66
        } else {
67
                jobject strObj = getJavaStringValue(SaxonProcessor::sxn_environ, key);
68
                        jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbsmapID, strObj));
69
                        if(valueObj) {
70
                                XdmValue * newValue = new XdmValue();
71
                                       newValue->addUnderlyingValue(valueObj);
72
                                       return newValue;
73
                               }
74
                               return nullptr;
75
        }
76

    
77

    
78
    }
79

    
80
    XdmValue * XdmMap::get(int key) {
81
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
82
        static jmethodID tbimapID = nullptr;
83

    
84
        if(tbimapID == nullptr) {
85
        tbimapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
86
                                                "(I)Lnet/sf/saxon/s9api/XdmValue;");
87

    
88
        }
89
        if (!tbimapID) {
90
                                std::cerr << "Error: Saxonc.XdmMap." << "get"
91
                                        << " not found\n" << std::endl;
92
                                return nullptr;
93
        } else {
94
                        jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbimapID, key));
95
                        if(valueObj) {
96
                                XdmValue * newValue = new XdmValue();
97
                                       newValue->addUnderlyingValue(valueObj);
98
                                       return newValue;
99
                               }
100
                               return nullptr;
101
        }
102

    
103

    
104
    }
105

    
106
    XdmValue *  XdmMap::get(double key) {
107
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
108
        static jmethodID tbdmapID = nullptr;
109

    
110
        if(tbdmapID == nullptr) {
111
            tbdmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
112
                                                "(D)Lnet/sf/saxon/s9api/XdmValue;");
113

    
114
        }
115
        if (!tbdmapID) {
116
                                std::cerr << "Error: Saxonc.XdmMap." << "get"
117
                                        << " not found\n" << std::endl;
118
                                return nullptr;
119
        } else {
120
                        jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbdmapID, key));
121
                        if(valueObj) {
122
                                XdmValue * newValue = new XdmValue();
123
                                       newValue->addUnderlyingValue(valueObj);
124
                                       return newValue;
125
                               }
126
                               return nullptr;
127
        }
128

    
129

    
130
    }
131

    
132
    XdmValue * XdmMap::get(long key) {
133
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
134
        static jmethodID tbdmapID = nullptr;
135

    
136
        if(tbdmapID == nullptr) {
137
            tbdmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
138
                                                "(J)Lnet/sf/saxon/s9api/XdmValue;");
139

    
140
        }
141
        if (!tbdmapID) {
142
                                std::cerr << "Error: Saxonc.XdmMap." << "get"
143
                                        << " not found\n" << std::endl;
144
                                return nullptr;
145
        } else {
146
                        jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbdmapID, key));
147
                        if(valueObj) {
148
                                XdmValue * newValue = new XdmValue();
149
                                       newValue->addUnderlyingValue(valueObj);
150
                                       return newValue;
151
                               }
152
                               return nullptr;
153
        }
154

    
155
    }
156

    
157
//TODO test this method
158
    XdmMap * XdmMap::put(XdmAtomicValue* key, XdmValue * valuei) {
159
        if(key == nullptr || valuei == nullptr) {
160
            std::cerr << "Error: Saxonc.XdmMap." << "key or value is nullptr" << std::endl;
161
            return nullptr;
162
        }
163
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
164
        static jmethodID putmapID = nullptr;
165

    
166
        if(putmapID == nullptr) {
167
                    putmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "put",
168
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
169

    
170
        }
171
        if (!putmapID) {
172
            std::cerr << "Error: Saxonc.XdmMap." << "put"
173
                          << " not found\n" << std::endl;
174
            return nullptr;
175
        } else {
176
                                jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, putmapID, ((XdmItem *)key)->getUnderlyingValue(), valuei->getUnderlyingValue()));
177
                                if(valueObj) {
178
                                        XdmMap * newValue = new XdmMap(valueObj);
179
                                               return newValue;
180
                                       }
181
                                       return nullptr;
182
                }
183

    
184
    }
185

    
186
    XdmMap * XdmMap::remove(XdmAtomicValue* key){
187
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
188
                static jmethodID removemapID = nullptr;
189

    
190
                if(removemapID == nullptr) {
191
                    removemapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "remove",
192
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Lnet/sf/saxon/s9api/XdmMap;");
193

    
194
                }
195
                if (!removemapID) {
196
                                        std::cerr << "Error: Saxonc.XdmMap." << "remove"
197
                                                << " not found\n" << std::endl;
198
                                        return nullptr;
199
                } else {
200
                                jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, removemapID, ((XdmItem *)key)->getUnderlyingValue()));
201
                                if(valueObj) {
202
                                        XdmMap * newValue = new XdmMap(valueObj);
203
                                               return newValue;
204
                                       }
205
                                       return nullptr;
206
                }
207

    
208

    
209

    
210
    }
211

    
212
    std::set<XdmAtomicValue*> XdmMap::keySet(){
213
        std::set<XdmAtomicValue *> myset;
214

    
215
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
216
        static jmethodID keySetmID = nullptr;
217

    
218
        if(keySetmID == nullptr) {
219
            keySetmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getXdmMapKeys",
220
                                                                "(Lnet/sf/saxon/s9api/XdmMap)[Lnet/sf/saxon/s9api/XdmAtomicValue;");
221
            }
222
            if (!keySetmID) {
223
                std::cerr << "Error: SaxonDll." << "makeXdmMap"<< " not found\n" << std::endl;
224
                return myset;
225
            }
226

    
227

    
228
            jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, keySetmID,value));
229
            if(results) {
230
                       int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
231

    
232

    
233
                           XdmAtomicValue * tempValue = nullptr;
234
                                   for (int p=0; p < sizex; ++p){
235
                                           jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
236
                                           tempValue = new XdmAtomicValue();
237
                                           tempValue->addUnderlyingValue(resulti);
238
                                           myset.insert(tempValue);
239
                                   }
240
        }
241

    
242

    
243

    
244
         return myset;
245
    }
246

    
247
    //std::map<XdmAtomicValue*, XdmValue*> XdmMap::asMap();
248

    
249
    bool XdmMap::isEmpty() {
250
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
251
                static jmethodID isEmptyMapID = nullptr;
252

    
253
                if(isEmptyMapID == nullptr) {
254
                    isEmptyMapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "isEmpty",
255
                                                        "()Z");
256

    
257
                }
258
                if (!isEmptyMapID) {
259
                                        std::cerr << "Error: Saxonc.XdmMap." << "isEmpty"
260
                                                << " not found\n" << std::endl;
261
                                        return false;
262
                } else {
263
                                jboolean result = (SaxonProcessor::sxn_environ->env->CallBooleanMethod(value, isEmptyMapID));
264
                                return result;
265
                }
266
    }
267

    
268
    bool XdmMap::containsKey(XdmAtomicValue * key) {
269
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
270
                static jmethodID containsMapID = nullptr;
271

    
272
                if(containsMapID == nullptr) {
273
                    containsMapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "containsKey",
274
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Z");
275

    
276
                }
277
                if (!containsMapID) {
278
                                        std::cerr << "Error: Saxonc.XdmMap." << "containsKey"
279
                                                << " not found\n" << std::endl;
280
                                        return false;
281
                } else {
282
                                jboolean result = (SaxonProcessor::sxn_environ->env->CallBooleanMethod(value, containsMapID, ((XdmItem *)key)->getUnderlyingValue()));
283
                                return result;
284
                }
285

    
286

    
287

    
288
    }
289

    
290
    std::list<XdmValue *> XdmMap::values() {
291
         std::list<XdmValue *> mylist;
292

    
293
            jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
294
            jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getXdmMapValues",
295
                                                                "(Lnet/sf/saxon/s9api/XdmMap)[Lnet/sf/saxon/s9api/XdmValue;");
296
            if (!xmID) {
297
                std::cerr << "Error: SaxonDll." << "makeXdmMap"<< " not found\n" << std::endl;
298
                return mylist;
299
            }
300

    
301

    
302
            jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,value));
303
            if(results) {
304
                       int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
305

    
306

    
307
                           XdmValue * tempValue = nullptr;
308
                           for (int p=0; p < sizex; ++p){
309
                                           jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
310
                                           tempValue = new XdmValue();
311
                                           tempValue->addUnderlyingValue(resulti);
312
                                           mylist.push_back(tempValue);
313

    
314
                                   }
315

    
316
            }
317

    
318

    
319
         return mylist;
320
    }
(32-32/56)