Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmNode.cpp @ 6af44631

1

    
2

    
3
#include "XdmNode.h"
4

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

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

    
11
}
12

    
13
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){}
14

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

    
31
            jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, nodeKindMID));
32
            if (!nodeKindObj) {
33

    
34
                return UNKNOWN;
35
            }
36
            jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
37

    
38
            jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
39
                                                                                             "convertNodeKindType",
40
                                                                                             "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
41

    
42
            if (!mID2) {
43
                std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
44
                          << std::endl;
45
                return UNKNOWN;
46
            }
47

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

    
50
                nodeKind = static_cast<XDM_NODE_KIND>(kvalue);
51
        } 
52
        return nodeKind;
53

    
54
    }
55

    
56
    XdmNode::~XdmNode() {
57
        delete baseURI;
58
        delete nodeName;
59
        if(children != nullptr) {
60
            for (int p=0; p < childCount; ++p) {
61
                delete children[p];
62

    
63
            }
64
            delete [] children;
65
        }
66

    
67
        if(attrCount > 0) {
68
            for (int p=0; p < attrCount; ++p) {
69
                delete attrValues[p];
70

    
71
            }
72
            delete [] attrValues;
73
        }
74

    
75

    
76
    }
77

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

    
114
    }
115

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

    
139

    
140
    }
141

    
142
    const char * XdmNode::getStringValue(){
143
                   return XdmItem::getStringValue();
144
    }
145

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

    
170
    
171
    const char* XdmNode::getBaseUri(){
172

    
173
        if(baseURI != nullptr) {
174
                return baseURI;
175
        }
176

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

    
209

    
210

    
211

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

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

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

    
262
                return stri;
263
        } else {
264

    
265
                return nullptr;
266
        }
267

    
268
    }
269

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

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

    
310
                attrCount =(int)result;
311
        }
312
        return attrCount;
313
    }
314

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

    
329
                childCount =(int)result;
330
        }
331
        return childCount;
332
    }
333
    
334
    XdmNode** XdmNode::getChildren(){
335

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

    
361
    }
362
  
(34-34/51)