Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmMap.cpp @ 03fcd3f9

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

    
5

    
6
XdmMap::XdmMap(): XdmFunctionItem() {
7

    
8
}
9

    
10
XdmMap::XdmMap(const XdmMap &d) {
11
      arity = d.arity;
12
      data = d.data; //TODO check if copy works
13
    }
14

    
15
XdmMap::XdmMap(jobject obj): XdmFunctionItem(obj) {}
16

    
17

    
18

    
19

    
20
    int XdmMap::mapSize(){
21
        return  data.size();
22
    }
23

    
24
    XdmValue * XdmMap::get(XdmAtomicValue* key) {
25
    jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
26
    static jmethodID tbmapID = nullptr;
27

    
28
    if(tbmapID == nullptr) {
29
    tbmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
30
                                            "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Lnet/sf/saxon/s9api/XdmValue;");
31

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

    
47
    }
48

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

    
53
        if(tbsmapID == nullptr) {
54
        tbsmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
55
                                                "(Ljava/lang/String;)Lnet/sf/saxon/s9api/XdmValue;");
56

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

    
73

    
74
    }
75

    
76
    XdmValue * XdmMap::get(int key) {
77
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
78
        static jmethodID tbimapID = nullptr;
79

    
80
        if(tbimapID == nullptr) {
81
        tbimapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
82
                                                "(I)Lnet/sf/saxon/s9api/XdmValue;");
83

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

    
99

    
100
    }
101

    
102
    XdmValue *  XdmMap::get(double key) {
103
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
104
        static jmethodID tbdmapID = nullptr;
105

    
106
        if(tbdmapID == nullptr) {
107
            tbdmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
108
                                                "(D)Lnet/sf/saxon/s9api/XdmValue;");
109

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

    
125

    
126
    }
127

    
128
    XdmValue * XdmMap::get(long key) {
129
        jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
130
        static jmethodID tbdmapID = nullptr;
131

    
132
        if(tbdmapID == nullptr) {
133
            tbdmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "get",
134
                                                "(J)Lnet/sf/saxon/s9api/XdmValue;");
135

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

    
151
    }
152

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

    
162
        if(putmapID == nullptr) {
163
                    putmapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "put",
164
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;Lnet/sf/saxon/s9api/XdmValue;)Lnet/sf/saxon/s9api/XdmMap;");
165

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

    
180
    }
181

    
182
    XdmMap * XdmMap::remove(XdmAtomicValue* key){
183
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
184
                static jmethodID removemapID = nullptr;
185

    
186
                if(removemapID == nullptr) {
187
                    removemapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "remove",
188
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Lnet/sf/saxon/s9api/XdmMap;");
189

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

    
204

    
205

    
206
    }
207

    
208
    std::set<XdmAtomicValue*> XdmMap::keySet(){
209
        std::set<XdmAtomicValue *> myset;
210

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

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

    
223

    
224
            jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, keySetmID,value));
225
            if(results) {
226
                       int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
227

    
228

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

    
238

    
239

    
240
         return myset;
241
    }
242

    
243
    //std::map<XdmAtomicValue*, XdmValue*> XdmMap::asMap();
244

    
245
    bool XdmMap::isEmpty() {
246
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
247
                static jmethodID isEmptyMapID = nullptr;
248

    
249
                if(isEmptyMapID == nullptr) {
250
                    isEmptyMapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "isEmpty",
251
                                                        "()Z");
252

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

    
264
    bool XdmMap::containsKey(XdmAtomicValue * key) {
265
                jclass xdmMapClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmMap");
266
                static jmethodID containsMapID = nullptr;
267

    
268
                if(containsMapID == nullptr) {
269
                    containsMapID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmMapClass, "containsKey",
270
                                                        "(Lnet/sf/saxon/s9api/XdmAtomicValue;)Z");
271

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

    
282

    
283

    
284
    }
285

    
286
    std::list<XdmValue *> XdmMap::values() {
287
         std::list<XdmValue *> mylist;
288

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

    
297

    
298
            jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,value));
299
            if(results) {
300
                       int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
301

    
302

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

    
310
                                   }
311

    
312
            }
313

    
314

    
315
         return mylist;
316
    }
(31-31/55)