Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmNode.cpp @ 306962bc

1

    
2

    
3
#include "XdmNode.h"
4

    
5
XdmNode::XdmNode(jobject obj): XdmItem(obj), baseURI(nullptr), nodeName(nullptr), children(nullptr), childCount(-1), parent(nullptr), typedValue(nullptr), attrValues(nullptr), attrCount(-1), nodeKind(UNKNOWN){
6

    
7
}
8

    
9
XdmNode::XdmNode(XdmNode * p, jobject obj, XDM_NODE_KIND kind): XdmItem(obj), baseURI(nullptr), nodeName(nullptr), children(nullptr),  childCount(-1), parent(p), typedValue(nullptr), attrValues(nullptr),  attrCount(-1), nodeKind(kind){}
10

    
11
bool XdmNode::isAtomic() {
12
        return false;
13
} 
14
    
15
    XDM_NODE_KIND XdmNode::getNodeKind(){
16
        if(nodeKind == UNKNOWN) {
17
            jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/s9api/XdmNode;");
18
            static jmethodID nodeKindMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
19
                                                                                                     "getNodeKind",
20
                                                                                                     "()Lnet/sf/saxon/s9api/XdmNodeKind;");
21
            if (!nodeKindMID) {
22
                std::cerr << "Error: MyClassInDll." << "getNodeKind" << " not found\n"
23
                          << std::endl;
24
                return UNKNOWN;
25
            }
26

    
27
            jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, nodeKindMID));
28
            if (!nodeKindObj) {
29

    
30
                return UNKNOWN;
31
            }
32
            jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
33

    
34
            jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
35
                                                                                             "convertNodeKindType",
36
                                                                                             "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
37

    
38
            if (!mID2) {
39
                std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
40
                          << std::endl;
41
                return UNKNOWN;
42
            }
43

    
44
            int kvalue = (int) (SaxonProcessor::sxn_environ->env->CallStaticIntMethod(xdmUtilsClass, mID2, nodeKindObj));
45

    
46
                nodeKind = static_cast<XDM_NODE_KIND>(kvalue);
47
        } 
48
        return nodeKind;
49

    
50
    }
51

    
52
    XdmNode::~XdmNode() {
53
        delete baseURI;
54
        delete nodeName;
55
        if(children != nullptr) {
56
            for (int p=0; p < childCount; ++p) {
57
                delete children[p];
58

    
59
            }
60
            delete [] children;
61
        }
62

    
63
        if(attrCount > 0) {
64
            for (int p=0; p < attrCount; ++p) {
65
                delete attrValues[p];
66

    
67
            }
68
            delete [] attrValues;
69
        }
70

    
71

    
72
    }
73

    
74
    const char * XdmNode::getNodeName(){
75
        
76
        if(nodeName != nullptr) {
77
                return nodeName;
78
        }
79
        XDM_NODE_KIND kind = getNodeKind();
80
         jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
81
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getNodeName",
82
                                        "(Lnet/sf/saxon/s9api/XdmNode;)Ljava/lang/String;");
83
        switch (kind) {
84
            case DOCUMENT:
85
            case TEXT:
86
            case COMMENT:
87
                return nullptr;
88
            case PROCESSING_INSTRUCTION:
89
            case NAMESPACE:
90
            case ELEMENT:
91
            case ATTRIBUTE:
92
               
93
                if (!xmID) {
94
                        std::cerr << "Error: MyClassInDll." << "getNodeName"<< " not found\n" << std::endl;
95
                        return nullptr;
96
                } else {
97
                        jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, value));
98
                        if(!result) {
99
                                return nullptr;
100
                        } else {
101
                                nodeName = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
102
                                return nodeName;
103
                        } 
104
                }
105
            default:
106
                return nullptr;
107
        }
108
        
109

    
110
    }
111

    
112
    XdmValue * XdmNode::getTypedValue(){
113
            if(typedValue == nullptr) {
114
                    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
115
                    jmethodID tbmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
116
                                            "getTypedValue",
117
                                            "()Lnet/sf/saxon/s9api/XdmValue;");
118
                    if (!tbmID) {
119
                            std::cerr << "Error: Saxonc." << "getTypedValue"
120
                                    << " not found\n" << std::endl;
121
                            return nullptr;
122
                    } else {
123
                            jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbmID));
124
                            if(valueObj) {
125
                                    typedValue = new XdmValue();
126
                                    typedValue->addUnderlyingValue(valueObj);
127
                                    return typedValue;
128
                            }
129
                            return nullptr;
130
                    }
131
            } else {
132
                    return typedValue;
133
            }
134

    
135

    
136
    }
137

    
138
    const char * XdmNode::getStringValue(){
139
                   return XdmItem::getStringValue();
140
    }
141

    
142
    const char * XdmNode::toString(){
143
               if(stringValue.empty()) {
144
                        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
145
                        jmethodID strbMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
146
                                                "toString",
147
                                                "()Ljava/lang/String;");
148
                        if (!strbMID) {
149
                                std::cerr << "Error: Saxonc." << "toString"
150
                                        << " not found\n" << std::endl;
151
                                return nullptr;
152
                        } else {
153
                                jstring result = (jstring) (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, strbMID));
154
                                if(result) {
155
                       const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
156
                       stringValue = str;
157
                                  return str;
158
                }
159
                   return nullptr;
160
                        }
161
                } else {
162
                        return stringValue.c_str();
163
                }
164
    }
165

    
166
    
167
    const char* XdmNode::getBaseUri(){
168

    
169
        if(baseURI != nullptr) {
170
                return baseURI;
171
        }
172

    
173
        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
174
        jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
175
                                        "getBaseURI",
176
                                        "()Ljava/net/URI;");
177
        if (!bmID) {
178
                std::cerr << "Error: MyClassInDll." << "getBaseURI"
179
                                << " not found\n" << std::endl;
180
                return nullptr;
181
        } else {
182
                jobject nodeURIObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
183
                if(!nodeURIObj){
184
                        return nullptr;
185
                } else {
186
                        jclass URIClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/net/URI");
187
                        jmethodID strMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(URIClass,
188
                                        "toString",
189
                                        "()Ljava/lang/String;");
190
                        if(strMID){
191
                                jstring result = (jstring)(
192
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(nodeURIObj, strMID));
193
                                baseURI = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
194
                                        nullptr);
195
                        
196
                                return baseURI;
197
                        }        
198
                }
199
        }
200
        return nullptr;
201
    }
202
    
203
    
204

    
205

    
206

    
207

    
208
    XdmNode* XdmNode::getParent(){
209
        if(parent == nullptr) {
210
                jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
211
                jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
212
                                        "getParent",
213
                                        "()Lnet/sf/saxon/s9api/XdmNode;");
214
                if (!bmID) {
215
                        std::cerr << "Error: MyClassInDll." << "getParent"
216
                                << " not found\n" << std::endl;
217
                        return nullptr;
218
                } else {
219
                        jobject nodeObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
220
                        if(nodeObj) {
221
                                parent = new XdmNode(nullptr, nodeObj, UNKNOWN);
222
                                //parent->incrementRefCount();
223
                                return parent;
224
                        }
225
                        return nullptr;
226
                }
227
        } else {
228
                return parent;
229
        }
230
        
231
    }
232
    
233
    const char* XdmNode::getAttributeValue(const char *str){
234

    
235
        if(str == nullptr) { return nullptr;}
236
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
237
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeValue",
238
                                        "(Lnet/sf/saxon/s9api/XdmNode;Ljava/lang/String;)Ljava/lang/String;");
239
        if (!xmID) {
240
                        std::cerr << "Error: SaxonDll." << "getAttributeValue"
241
                                << " not found\n" << std::endl;
242
                        return nullptr;
243
                }
244
        if(str == nullptr) {
245
                return nullptr;
246
        }
247
        jstring eqname = SaxonProcessor::sxn_environ->env->NewStringUTF(str);
248

    
249
        jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,value, eqname));
250
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(eqname);
251
        //failure = checkForException(sxn_environ,  (jobject)result);//Remove code
252
        if(result) {
253
                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
254
                                        nullptr);
255
                
256
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
257

    
258
                return stri;
259
        } else {
260

    
261
                return nullptr;
262
        }
263

    
264
    }
265

    
266
    XdmNode** XdmNode::getAttributeNodes(){
267
        if(attrValues == nullptr) {
268
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
269
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeNodes",
270
                                        "(Lnet/sf/saxon/s9api/XdmNode;)[Lnet/sf/saxon/s9api/XdmNode;");
271
                jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, 
272
                value));
273
                if(results == nullptr) {
274
                        return nullptr;
275
                }
276
                int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
277
                attrCount = sizex;
278
                if(sizex>0) {        
279
                        attrValues =  new XdmNode*[sizex];
280
                        XdmNode * tempNode =nullptr;
281
                        for (int p=0; p < sizex; ++p){
282
                                jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
283
                                tempNode = new XdmNode(this, resulti, ATTRIBUTE);
284
                                this->incrementRefCount();
285
                                attrValues[p] = tempNode;
286
                        }
287
                }
288
        } 
289
        return attrValues;
290
    }
291

    
292
    int XdmNode::getAttributeCount(){
293
        if(attrCount == -1) {
294
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
295
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeCount",
296
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
297
                
298
                if (!xmID) {
299
                        std::cerr << "Error: SaxonDll." << "getAttributeCount"
300
                                << " not found\n" << std::endl;
301
                        return 0;
302
                }
303
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
304
                value));
305

    
306
                attrCount =(int)result;
307
        }
308
        return attrCount;
309
    }
310

    
311
    int XdmNode::getChildCount(){
312
        if(childCount == -1) {
313
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
314
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getChildCount",
315
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
316
                
317
                if (!xmID) {
318
                        std::cerr << "Error: SaxonDll." << "getchildCount"
319
                                << " not found\n" << std::endl;
320
                        return 0;
321
                }
322
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
323
                value));
324

    
325
                childCount =(int)result;
326
        }
327
        return childCount;
328
    }
329
    
330
    XdmNode** XdmNode::getChildren(){
331

    
332
        if(children == nullptr) {
333
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
334
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getChildren",
335
                                        "(Lnet/sf/saxon/s9api/XdmNode;)[Lnet/sf/saxon/s9api/XdmNode;");
336
                
337
                if (!xmID) {
338
                        std::cerr << "Error: SaxonDll." << "getchildren"
339
                                << " not found\n" << std::endl;
340
                        return nullptr;
341
                }
342
                jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, 
343
                value));
344
                int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
345
                childCount = sizex;        
346
                children =  new XdmNode*[sizex];
347
                XdmNode * tempNode = nullptr;
348
                for (int p=0; p < sizex; ++p){
349
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
350
                        tempNode = new XdmNode(this, resulti, UNKNOWN);
351
                        //tempNode->incrementRefCount();
352
                        children[p] = tempNode;
353
                }
354
        }
355
        return children;
356

    
357
    }
358
  
(33-33/55)