Project

Profile

Help

Download (11.4 KB) Statistics
| Branch: | Revision:

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

1 72bf04c6 Norman Walsh
2
3 901b6eca O'Neil Delpratt
#include "XdmNode.h"
4 72bf04c6 Norman Walsh
5 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
7
}
8
9 ead48a5d O'Neil Delpratt
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 72bf04c6 Norman Walsh
11
bool XdmNode::isAtomic() {
12
        return false;
13
} 
14
    
15
    XDM_NODE_KIND XdmNode::getNodeKind(){
16 ead48a5d O'Neil Delpratt
        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 72bf04c6 Norman Walsh
        } 
48
        return nodeKind;
49
50
    }
51
52
    const char * XdmNode::getNodeName(){
53
        
54 ead48a5d O'Neil Delpratt
        if(nodeName != nullptr) {
55 72bf04c6 Norman Walsh
                return nodeName;
56
        }
57
        XDM_NODE_KIND kind = getNodeKind();
58
         jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
59
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getNodeName",
60
                                        "(Lnet/sf/saxon/s9api/XdmNode;)Ljava/lang/String;");
61
        switch (kind) {
62
            case DOCUMENT:
63
            case TEXT:
64
            case COMMENT:
65 ead48a5d O'Neil Delpratt
                return nullptr;
66 72bf04c6 Norman Walsh
            case PROCESSING_INSTRUCTION:
67
            case NAMESPACE:
68
            case ELEMENT:
69
            case ATTRIBUTE:
70
               
71
                if (!xmID) {
72
                        std::cerr << "Error: MyClassInDll." << "getNodeName"<< " not found\n" << std::endl;
73 ead48a5d O'Neil Delpratt
                        return nullptr;
74 72bf04c6 Norman Walsh
                } else {
75 ead48a5d O'Neil Delpratt
                        jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, value));
76 72bf04c6 Norman Walsh
                        if(!result) {
77 ead48a5d O'Neil Delpratt
                                return nullptr;
78 72bf04c6 Norman Walsh
                        } else {
79 ead48a5d O'Neil Delpratt
                                nodeName = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
80 72bf04c6 Norman Walsh
                                return nodeName;
81
                        } 
82
                }
83
            default:
84 ead48a5d O'Neil Delpratt
                return nullptr;
85 72bf04c6 Norman Walsh
        }
86
        
87
88
    }
89
90
    XdmValue * XdmNode::getTypedValue(){
91 ead48a5d O'Neil Delpratt
            if(typedValue == nullptr) {
92 72bf04c6 Norman Walsh
                    jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
93
                    jmethodID tbmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
94
                                            "getTypedValue",
95
                                            "()Lnet/sf/saxon/s9api/XdmValue;");
96
                    if (!tbmID) {
97
                            std::cerr << "Error: Saxonc." << "getTypedValue"
98
                                    << " not found\n" << std::endl;
99 ead48a5d O'Neil Delpratt
                            return nullptr;
100 72bf04c6 Norman Walsh
                    } else {
101 ead48a5d O'Neil Delpratt
                            jobject valueObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, tbmID));
102 72bf04c6 Norman Walsh
                            if(valueObj) {
103 901b6eca O'Neil Delpratt
                                    typedValue = new XdmValue();
104 72bf04c6 Norman Walsh
                                    typedValue->addUnderlyingValue(valueObj);
105
                                    return typedValue;
106
                            }
107 ead48a5d O'Neil Delpratt
                            return nullptr;
108 72bf04c6 Norman Walsh
                    }
109
            } else {
110
                    return typedValue;
111
            }
112
113
114
    }
115
116
    const char * XdmNode::getStringValue(){
117
                   return XdmItem::getStringValue();
118
    }
119
120
    const char * XdmNode::toString(){
121
               if(stringValue.empty()) {
122
                        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
123
                        jmethodID strbMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
124
                                                "toString",
125
                                                "()Ljava/lang/String;");
126
                        if (!strbMID) {
127
                                std::cerr << "Error: Saxonc." << "toString"
128
                                        << " not found\n" << std::endl;
129 ead48a5d O'Neil Delpratt
                                return nullptr;
130 72bf04c6 Norman Walsh
                        } else {
131 ead48a5d O'Neil Delpratt
                                jstring result = (jstring) (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, strbMID));
132 72bf04c6 Norman Walsh
                                if(result) {
133 ead48a5d O'Neil Delpratt
                       const char * str = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
134 72bf04c6 Norman Walsh
                       stringValue = str;
135
                                  return str;
136
                }
137 ead48a5d O'Neil Delpratt
                   return nullptr;
138 72bf04c6 Norman Walsh
                        }
139
                } else {
140
                        return stringValue.c_str();
141
                }
142
    }
143
144
    
145
    const char* XdmNode::getBaseUri(){
146
147 ead48a5d O'Neil Delpratt
        if(baseURI != nullptr) {
148 72bf04c6 Norman Walsh
                return baseURI;
149
        }
150
151
        jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
152
        jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
153
                                        "getBaseURI",
154
                                        "()Ljava/net/URI;");
155
        if (!bmID) {
156
                std::cerr << "Error: MyClassInDll." << "getBaseURI"
157
                                << " not found\n" << std::endl;
158 ead48a5d O'Neil Delpratt
                return nullptr;
159 72bf04c6 Norman Walsh
        } else {
160 ead48a5d O'Neil Delpratt
                jobject nodeURIObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
161 72bf04c6 Norman Walsh
                if(!nodeURIObj){
162 ead48a5d O'Neil Delpratt
                        return nullptr;
163 72bf04c6 Norman Walsh
                } else {
164
                        jclass URIClass = lookForClass(SaxonProcessor::sxn_environ->env, "java/net/URI");
165
                        jmethodID strMID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(URIClass,
166
                                        "toString",
167
                                        "()Ljava/lang/String;");
168
                        if(strMID){
169
                                jstring result = (jstring)(
170
                                SaxonProcessor::sxn_environ->env->CallObjectMethod(nodeURIObj, strMID));
171
                                baseURI = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
172 ead48a5d O'Neil Delpratt
                                        nullptr);
173 72bf04c6 Norman Walsh
                        
174
                                return baseURI;
175
                        }        
176
                }
177
        }
178 ead48a5d O'Neil Delpratt
        return nullptr;
179 72bf04c6 Norman Walsh
    }
180
    
181
    
182
183
184
185
186
    XdmNode* XdmNode::getParent(){
187 901b6eca O'Neil Delpratt
        if(parent == nullptr) {
188 72bf04c6 Norman Walsh
                jclass xdmNodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
189
                jmethodID bmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetMethodID(xdmNodeClass,
190
                                        "getParent",
191
                                        "()Lnet/sf/saxon/s9api/XdmNode;");
192
                if (!bmID) {
193
                        std::cerr << "Error: MyClassInDll." << "getParent"
194
                                << " not found\n" << std::endl;
195 ead48a5d O'Neil Delpratt
                        return nullptr;
196 72bf04c6 Norman Walsh
                } else {
197 ead48a5d O'Neil Delpratt
                        jobject nodeObj = (SaxonProcessor::sxn_environ->env->CallObjectMethod(value, bmID));
198 72bf04c6 Norman Walsh
                        if(nodeObj) {
199 ead48a5d O'Neil Delpratt
                                parent = new XdmNode(nullptr, nodeObj, UNKNOWN);
200 72bf04c6 Norman Walsh
                                //parent->incrementRefCount();
201
                                return parent;
202
                        }
203 ead48a5d O'Neil Delpratt
                        return nullptr;
204 72bf04c6 Norman Walsh
                }
205
        } else {
206
                return parent;
207
        }
208
        
209
    }
210
    
211
    const char* XdmNode::getAttributeValue(const char *str){
212
213 ead48a5d O'Neil Delpratt
        if(str == nullptr) { return nullptr;}
214 72bf04c6 Norman Walsh
        jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
215
        jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeValue",
216
                                        "(Lnet/sf/saxon/s9api/XdmNode;Ljava/lang/String;)Ljava/lang/String;");
217
        if (!xmID) {
218
                        std::cerr << "Error: SaxonDll." << "getAttributeValue"
219
                                << " not found\n" << std::endl;
220 ead48a5d O'Neil Delpratt
                        return nullptr;
221 72bf04c6 Norman Walsh
                }
222 ead48a5d O'Neil Delpratt
        if(str == nullptr) {
223
                return nullptr;
224 72bf04c6 Norman Walsh
        }
225
        jstring eqname = SaxonProcessor::sxn_environ->env->NewStringUTF(str);
226
227 ead48a5d O'Neil Delpratt
        jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,value, eqname));
228 72bf04c6 Norman Walsh
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(eqname);
229
        //failure = checkForException(sxn_environ,  (jobject)result);//Remove code
230
        if(result) {
231
                const char * stri = SaxonProcessor::sxn_environ->env->GetStringUTFChars(result,
232 ead48a5d O'Neil Delpratt
                                        nullptr);
233 72bf04c6 Norman Walsh
                
234
                //SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
235
236
                return stri;
237
        } else {
238
239 ead48a5d O'Neil Delpratt
                return nullptr;
240 72bf04c6 Norman Walsh
        }
241
242
    }
243
244
    XdmNode** XdmNode::getAttributeNodes(){
245 ead48a5d O'Neil Delpratt
        if(attrValues == nullptr) {
246 72bf04c6 Norman Walsh
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
247
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeNodes",
248
                                        "(Lnet/sf/saxon/s9api/XdmNode;)[Lnet/sf/saxon/s9api/XdmNode;");
249
                jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, 
250 ead48a5d O'Neil Delpratt
                value));
251
                if(results == nullptr) {
252
                        return nullptr;
253 72bf04c6 Norman Walsh
                }
254
                int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
255
                attrCount = sizex;
256
                if(sizex>0) {        
257
                        attrValues =  new XdmNode*[sizex];
258 ead48a5d O'Neil Delpratt
                        XdmNode * tempNode =nullptr;
259 72bf04c6 Norman Walsh
                        for (int p=0; p < sizex; ++p){
260
                                jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
261
                                tempNode = new XdmNode(this, resulti, ATTRIBUTE);
262
                                this->incrementRefCount();
263
                                attrValues[p] = tempNode;
264
                        }
265
                }
266
        } 
267
        return attrValues;
268
    }
269
270
    int XdmNode::getAttributeCount(){
271 901b6eca O'Neil Delpratt
        if(attrCount == -1) {
272 72bf04c6 Norman Walsh
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
273
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getAttributeCount",
274
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
275
                
276
                if (!xmID) {
277
                        std::cerr << "Error: SaxonDll." << "getAttributeCount"
278
                                << " not found\n" << std::endl;
279
                        return 0;
280
                }
281
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
282 ead48a5d O'Neil Delpratt
                value));
283 72bf04c6 Norman Walsh
284
                attrCount =(int)result;
285
        }
286
        return attrCount;
287
    }
288
289
    int XdmNode::getChildCount(){
290 901b6eca O'Neil Delpratt
        if(childCount == -1) {
291 72bf04c6 Norman Walsh
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
292
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getChildCount",
293
                                        "(Lnet/sf/saxon/s9api/XdmNode;)I");
294
                
295
                if (!xmID) {
296
                        std::cerr << "Error: SaxonDll." << "getchildCount"
297
                                << " not found\n" << std::endl;
298
                        return 0;
299
                }
300
                jint result = (jlong)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID,
301 ead48a5d O'Neil Delpratt
                value));
302 72bf04c6 Norman Walsh
303
                childCount =(int)result;
304
        }
305
        return childCount;
306
    }
307
    
308
    XdmNode** XdmNode::getChildren(){
309
310 901b6eca O'Neil Delpratt
        if(children == nullptr) {
311 72bf04c6 Norman Walsh
                jclass xdmUtilsClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmUtils");
312
                jmethodID xmID = (jmethodID) SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmUtilsClass,"getChildren",
313
                                        "(Lnet/sf/saxon/s9api/XdmNode;)[Lnet/sf/saxon/s9api/XdmNode;");
314
                
315
                if (!xmID) {
316
                        std::cerr << "Error: SaxonDll." << "getchildren"
317
                                << " not found\n" << std::endl;
318 ead48a5d O'Neil Delpratt
                        return nullptr;
319 72bf04c6 Norman Walsh
                }
320
                jobjectArray results = (jobjectArray)(SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmUtilsClass, xmID, 
321 ead48a5d O'Neil Delpratt
                value));
322 72bf04c6 Norman Walsh
                int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
323
                childCount = sizex;        
324
                children =  new XdmNode*[sizex];
325 ead48a5d O'Neil Delpratt
                XdmNode * tempNode = nullptr;
326 72bf04c6 Norman Walsh
                for (int p=0; p < sizex; ++p){
327
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
328
                        tempNode = new XdmNode(this, resulti, UNKNOWN);
329
                        //tempNode->incrementRefCount();
330
                        children[p] = tempNode;
331
                }
332
        }
333
        return children;
334
335
    }
336