Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmValue.cpp @ 4ee4355d

1 72bf04c6 Norman Walsh
#include "XdmValue.h"
2
#include "XdmItem.h"
3
#include "XdmAtomicValue.h"
4
#include "XdmNode.h"
5
#include "XdmFunctionItem.h"
6
#include "XdmMap.h"
7
#include "XdmArray.h"
8
9
XdmValue::XdmValue(const XdmValue &other) {
10
        //SaxonProcessor *proc = other.proc; //TODO
11
        valueType = other.valueType;
12 a69dd173 O'Neil Delpratt
        refCount = 0;
13 72bf04c6 Norman Walsh
        xdmSize = other.xdmSize;
14
        jValues = other.jValues;
15
        toStringValue = other.toStringValue;
16
        values.resize(0);//TODO memory issue might occur here
17
        toStringValue = other.toStringValue;
18
        for (int i = 0; i < xdmSize; i++) {
19
                addXdmItem(other.values[i]);
20
        }
21
        
22
}
23
24
const char * XdmValue::toString() {
25
        if (toStringValue.empty()) {
26
                jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmValue");
27
                jmethodID strMID2 = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID(xdmValueClass,
28
                        "toString",
29
                        "()Ljava/lang/String;");
30
                if (!strMID2) {
31
                        std::cerr << "Error: Saxonc.XdmValue." << "toString"
32
                                << " not found\n" << std::endl;
33 ead48a5d O'Neil Delpratt
                        return nullptr;
34 72bf04c6 Norman Walsh
                }
35
                else {
36
                        for(int i=0; i<size();i++) {
37
38
                        
39
                                jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallObjectMethod(itemAt(i)->getUnderlyingValue(), strMID2));
40
                                if (result) {
41 ead48a5d O'Neil Delpratt
                                        toStringValue += SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
42 72bf04c6 Norman Walsh
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
43
                                
44
                                }
45
                        }
46
47
                        return toStringValue.c_str();                        
48
                }
49
                        
50
                
51
        }
52
        else {
53
                return toStringValue.c_str();
54
        }
55
56
}
57
58
59
int XdmValue::size() {
60
        return xdmSize;
61
}
62
63
XdmValue::XdmValue(jobject val) {
64
        XdmItem * value = new XdmItem(val);
65 a69dd173 O'Neil Delpratt
        values.resize(0);//TODO memory issue might occur here. Need to delete XdmItems
66 72bf04c6 Norman Walsh
        values.push_back(value);
67
        xdmSize++;
68 ead48a5d O'Neil Delpratt
        jValues = nullptr;
69
        valueType = nullptr;
70 72bf04c6 Norman Walsh
}
71
72
73
XdmValue::XdmValue(jobject val, bool arr){
74
        xdmSize = 0;
75
        values.resize(0);
76 ead48a5d O'Neil Delpratt
        jValues = nullptr;
77
        valueType = nullptr;
78 72bf04c6 Norman Walsh
        jclass xdmValueForcppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp");
79
        jmethodID xvfMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmValueForcppClass, "makeArrayFromXdmValue", "(Lnet/sf/saxon/s9api/XdmValue;)[Lnet/sf/saxon/s9api/XdmItem;");
80
81
        if(!xvfMID){
82
83
                std::cerr << "Error: SaxonDll." << "makeArrayFromXdmValue"
84
                                << " not found\n" << std::endl;
85
                        return ;
86
        }
87
        
88
        jobjectArray results = (jobjectArray) SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmValueForcppClass, xvfMID, val);
89
        if(results){
90
        int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
91
        if (sizex>0) {
92
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
93
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
94
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
95
96
                //XdmValue * value = new XdmValue();
97
                //value->setProcessor(proc);
98 ead48a5d O'Neil Delpratt
                XdmItem * xdmItem = nullptr;
99 72bf04c6 Norman Walsh
                for (int p=0; p < sizex; ++p) 
100
                {
101
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
102
                        //value->addUnderlyingValue(resulti);
103
104
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
105
                                xdmItem = new XdmAtomicValue(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
106
                                
107
108
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
109
                                xdmItem = new XdmNode(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
110
111
112
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
113
                              //Not Supported yet
114
#if CVERSION_API_NO >= 123
115
                                xdmItem = new XdmFunctionItem(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
116
#endif
117
                        }
118
                        //xdmItem->setProcessor(proc);
119
                        addXdmItem(xdmItem);
120
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(resulti);
121
                }
122
        }
123
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
124
        }        
125
}
126
127
128
XdmValue::~XdmValue() {
129 146cfacb O'Neil Delpratt
        //bool safeToClear = false;
130 85a9912e Dave Delpratt
        //std::cout<<"xdmValue destructor size= "<<values.size()<<" ref="<<getRefCount()<<" Pointer = "<<(this)<<std::endl;
131 146cfacb O'Neil Delpratt
        if(values.size() > 0) {
132
        for (size_t i = 0; i < values.size(); i++) {
133 4ee4355d O'Neil Delpratt
            if (values[i] != nullptr) {
134
                //values[i]->decrementRefCount();
135 146cfacb O'Neil Delpratt
                delete values[i];
136
            }
137
        }
138
        values.clear();
139
    }
140 ead48a5d O'Neil Delpratt
        if (valueType != nullptr) { delete valueType; }
141 901b6eca O'Neil Delpratt
        if (jValues) {
142 4ee4355d O'Neil Delpratt
                SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
143 72bf04c6 Norman Walsh
        }
144
        xdmSize = 0;
145 a69dd173 O'Neil Delpratt
146
    if(!toStringValue.empty()) {
147
        toStringValue.clear();
148 72bf04c6 Norman Walsh
    }
149
150
}
151
152
void XdmValue::addXdmItem(XdmItem* val) {
153 ead48a5d O'Neil Delpratt
        if (val != nullptr) {
154 72bf04c6 Norman Walsh
                values.push_back(val);
155 a69dd173 O'Neil Delpratt
        val->incrementRefCount();
156 72bf04c6 Norman Walsh
                xdmSize++;
157 00082f6f O'Neil Delpratt
        if (jValues) {
158 4ee4355d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
159 00082f6f O'Neil Delpratt
            jValues = nullptr;
160
        }
161
162 72bf04c6 Norman Walsh
        }
163
}
164
165
166
void XdmValue::addUnderlyingValue(jobject val) {
167
        XdmItem * valuei = new XdmItem(val);
168 a69dd173 O'Neil Delpratt
        valuei->incrementRefCount();
169 72bf04c6 Norman Walsh
        values.push_back(valuei);
170
        xdmSize++;
171 ead48a5d O'Neil Delpratt
        jValues = nullptr; //TODO clear jni array from memory if needed
172 72bf04c6 Norman Walsh
173
}
174
175
void XdmValue::incrementRefCount() {
176
                refCount++;
177 a69dd173 O'Neil Delpratt
                std::cerr<<"refCount-inc-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
178 72bf04c6 Norman Walsh
        }
179
180
void XdmValue::decrementRefCount() {
181
                if (refCount > 0)
182
                        refCount--;
183 a69dd173 O'Neil Delpratt
                std::cerr<<"refCount-dec-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
184 72bf04c6 Norman Walsh
        }
185
186
187
188
189
XdmItem * XdmValue::getHead() {
190
        if (values.size() > 0) {
191
                return values[0];
192
        }
193
        else {
194 ead48a5d O'Neil Delpratt
                return nullptr;
195 72bf04c6 Norman Walsh
        }
196
}
197
198
jobject XdmValue::getUnderlyingValue() {
199 ead48a5d O'Neil Delpratt
        if (jValues == nullptr) {
200 72bf04c6 Norman Walsh
                int i;
201
                JNIEnv *env = SaxonProcessor::sxn_environ->env;
202
                int count = values.size();
203
                if (count == 0) {
204 ead48a5d O'Neil Delpratt
                        return nullptr;
205 72bf04c6 Norman Walsh
                }
206
                jclass objectClass = lookForClass(env,
207
                        "net/sf/saxon/s9api/XdmItem");
208 00082f6f O'Neil Delpratt
                jobjectArray jValuesi = (jobjectArray)env->NewObjectArray((jint)count, objectClass, 0);
209
210 72bf04c6 Norman Walsh
211
                for (i = 0; i < count; i++) {
212 00082f6f O'Neil Delpratt
                        env->SetObjectArrayElement(jValuesi, i, values[i]->getUnderlyingValue());
213 72bf04c6 Norman Walsh
                }
214 00082f6f O'Neil Delpratt
        jValues = (jobjectArray)SaxonProcessor::sxn_environ->env->NewGlobalRef(jValuesi);
215 72bf04c6 Norman Walsh
        } 
216
        return (jobject)jValues;
217
}
218
219
void XdmValue::releaseXdmValue() {
220 4ee4355d O'Neil Delpratt
    for (size_t i = 0; i < values.size(); i++) {
221
        if (values[i] != nullptr) {
222
            delete values[i];
223
        }
224
    }
225 72bf04c6 Norman Walsh
226 4ee4355d O'Neil Delpratt
    if(jValues) {
227
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
228
    }
229 72bf04c6 Norman Walsh
230
231
}
232
233
XdmItem * XdmValue::itemAt(int n) {
234
        if (n >= 0 && (unsigned int)n < values.size()) {
235
                return values[n];
236
        }
237 ead48a5d O'Neil Delpratt
        return nullptr;
238 72bf04c6 Norman Walsh
}
239
240
/**
241
* Get the type of the object
242
*/
243
XDM_TYPE XdmValue::getType() {
244
        return XDM_VALUE;
245
}
246
247
248
249