Project

Profile

Help

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

he / latest10 / hec / Saxon.C.API / XdmNode.cpp @ 6304fb8b

1

    
2
#include "XdmNode.h"
3

    
4

    
5
/*
6
const char * baseURI;
7
        const char * nodeName;
8
        XdmNode ** children; //caches child nodes when getChildren method is first called;
9
        int childCount;
10
        XdmNode * parent;
11
        XdmNode ** attrValues;//caches attribute nodes when getAttributeNodes method is first called;
12
        int attrCount;
13
        XDM_NODE_KIND nodeKind;
14
*/
15

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

    
18
}
19

    
20
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){}
21

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

    
38
            jobject nodeKindObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, nodeKindMID));
39
            if (!nodeKindObj) {
40

    
41
                return UNKNOWN;
42
            }
43
            jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "Lnet/sf/saxon/option/cpp/XdmUtils;");
44

    
45
            jmethodID mID2 = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,
46
                                                                                             "convertNodeKindType",
47
                                                                                             "(Lnet/sf/saxon/s9api/XdmNodeKind;)I");
48

    
49
            if (!mID2) {
50
                std::cerr << "Error: MyClassInDll." << "convertNodeKindType" << " not found\n"
51
                          << std::endl;
52
                return UNKNOWN;
53
            }
54

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

    
57
                nodeKind = static_cast<XDM_NODE_KIND>(kvalue);
58
        } 
59
        return nodeKind;
60

    
61
    }
62

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

    
99
    }
100

    
101
    XdmValue * XdmNode::getTypedValue(){
102
            if(typedValue == nullptr) {
103
                    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
104
                    jmethodID tbmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
105
                                            "getTypedValue",
106
                                            "()Lnet/sf/saxon/s9api/XdmValue;");
107
                    if (!tbmID) {
108
                            std::cerr << "Error: Saxonc." << "getTypedValue"
109
                                    << " not found\n" << std::endl;
110
                            return nullptr;
111
                    } else {
112
                            jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbmID));
113
                            if(valueObj) {
114
                                    typedValue = new XdmValue((proc == nullptr ? nullptr : proc));
115
                                    typedValue->addUnderlyingValue(valueObj);
116
                                    return typedValue;
117
                            }
118
                            return nullptr;
119
                    }
120
            } else {
121
                    return typedValue;
122
            }
123

    
124

    
125
    }
126

    
127
    const char * XdmNode::getStringValue(){
128
                   return XdmItem::getStringValue();
129
    }
130

    
131
    const char * XdmNode::toString(){
132
               if(stringValue.empty()) {
133
                        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
134
                        jmethodID strbMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
135
                                                "toString",
136
                                                "()Ljava/lang/String;");
137
                        if (!strbMID) {
138
                                std::cerr << "Error: Saxonc." << "toString"
139
                                        << " not found\n" << std::endl;
140
                                return nullptr;
141
                        } else {
142
                                jstring result = (jstring) (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, strbMID));
143
                                if(result) {
144
                       const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
145
                       stringValue = str;
146
                                  return str;
147
                }
148
                   return nullptr;
149
                        }
150
                } else {
151
                        return stringValue.c_str();
152
                }
153
    }
154

    
155
    
156
    const char* XdmNode::getBaseUri(){
157

    
158
        if(baseURI != nullptr) {
159
                return baseURI;
160
        }
161

    
162
        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
163
        jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
164
                                        "getBaseURI",
165
                                        "()Ljava/net/URI;");
166
        if (!bmID) {
167
                std::cerr << "Error: MyClassInDll." << "getBaseURI"
168
                                << " not found\n" << std::endl;
169
                return nullptr;
170
        } else {
171
                jobject nodeURIObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
172
                if(!nodeURIObj){
173
                        return nullptr;
174
                } else {
175
                        jclass URIClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/net/URI");
176
                        jmethodID strMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(URIClass,
177
                                        "toString",
178
                                        "()Ljava/lang/String;");
179
                        if(strMID){
180
                                jstring result = (jstring)(
181
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(nodeURIObj, strMID));
182
                                baseURI = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
183
                                        nullptr);
184
                        
185
                                return baseURI;
186
                        }        
187
                }
188
        }
189
        return nullptr;
190
    }
191
    
192
    
193

    
194

    
195

    
196

    
197
    XdmNode* XdmNode::getParent(){
198
        if(parent == nullptr && proc!= nullptr) {
199
                jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
200
                jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
201
                                        "getParent",
202
                                        "()Lnet/sf/saxon/s9api/XdmNode;");
203
                if (!bmID) {
204
                        std::cerr << "Error: MyClassInDll." << "getParent"
205
                                << " not found\n" << std::endl;
206
                        return nullptr;
207
                } else {
208
                        jobject nodeObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
209
                        if(nodeObj) {
210
                                parent = new XdmNode(nullptr, nodeObj, UNKNOWN);
211
                                parent->setProcessor(proc);
212
                                //parent->incrementRefCount();
213
                                return parent;
214
                        }
215
                        return nullptr;
216
                }
217
        } else {
218
                return parent;
219
        }
220
        
221
    }
222
    
223
    const char* XdmNode::getAttributeValue(const char *str){
224

    
225
        if(str == nullptr) { return nullptr;}
226
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
227
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeValue",
228
                                        "(Lnet/sf/saxon/s9api/XdmNode;Ljava/lang/String;)Ljava/lang/String;");
229
        if (!xmID) {
230
                        std::cerr << "Error: SaxonDll." << "getAttributeValue"
231
                                << " not found\n" << std::endl;
232
                        return nullptr;
233
                }
234
        if(str == nullptr) {
235
                return nullptr;
236
        }
237
        jstring eqname = SaxonProcessor::sxn_environ->env->NewStringUTF(str);
238

    
239
        jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,value, eqname));
240
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(eqname);
241
        //failure = checkForException(sxn_environ,  (jobject)result);//Remove code
242
        if(result) {
243
                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
244
                                        nullptr);
245
                
246
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
247

    
248
                return stri;
249
        } else {
250

    
251
                return nullptr;
252
        }
253

    
254
    }
255

    
256
    XdmNode** XdmNode::getAttributeNodes(){
257
        if(attrValues == nullptr) {
258
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
259
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeNodes",
260
                                        "(Lnet/sf/saxon/s9api/XdmNode;)[Lnet/sf/saxon/s9api/XdmNode;");
261
                jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, 
262
                value));
263
                if(results == nullptr) {
264
                        return nullptr;
265
                }
266
                int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
267
                attrCount = sizex;
268
                if(sizex>0) {        
269
                        attrValues =  new XdmNode*[sizex];
270
                        XdmNode * tempNode =nullptr;
271
                        for (int p=0; p < sizex; ++p){
272
                                jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
273
                                tempNode = new XdmNode(this, resulti, ATTRIBUTE);
274
                                tempNode->setProcessor(proc);
275
                                this->incrementRefCount();
276
                                attrValues[p] = tempNode;
277
                        }
278
                }
279
        } 
280
        return attrValues;
281
    }
282

    
283
    int XdmNode::getAttributeCount(){
284
        if(attrCount == -1 && proc!= nullptr) {
285
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
286
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeCount",
287
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
288
                
289
                if (!xmID) {
290
                        std::cerr << "Error: SaxonDll." << "getAttributeCount"
291
                                << " not found\n" << std::endl;
292
                        return 0;
293
                }
294
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
295
                value));
296

    
297
                attrCount =(int)result;
298
        }
299
        return attrCount;
300
    }
301

    
302
    int XdmNode::getChildCount(){
303
        if(childCount == -1 && proc!= nullptr) {
304
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
305
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getChildCount",
306
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
307
                
308
                if (!xmID) {
309
                        std::cerr << "Error: SaxonDll." << "getchildCount"
310
                                << " not found\n" << std::endl;
311
                        return 0;
312
                }
313
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
314
                value));
315

    
316
                childCount =(int)result;
317
        }
318
        return childCount;
319
    }
320
    
321
    XdmNode** XdmNode::getChildren(){
322

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

    
349
    }
350
  
(27-27/49)