Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmValue.cpp @ 00082f6f

1
#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
        refCount = 0;
13
        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
                        return nullptr;
34
                }
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
                                        toStringValue += SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
42
                                        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
        values.resize(0);//TODO memory issue might occur here. Need to delete XdmItems
66
        values.push_back(value);
67
        xdmSize++;
68
        jValues = nullptr;
69
        valueType = nullptr;
70
}
71

    
72

    
73
XdmValue::XdmValue(jobject val, bool arr){
74
        xdmSize = 0;
75
        values.resize(0);
76
        jValues = nullptr;
77
        valueType = nullptr;
78
        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
                XdmItem * xdmItem = nullptr;
99
                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
        bool safeToClear = false;
130
        std::cout<<"xdmValue destructor size= "<<values.size()<<" ref="<<getRefCount()<<std::endl;
131
    if(getRefCount()<1){
132
        std::cout<<"xdmValue destructor refCount="<<getRefCount()<<" values.size="<<values.size()<<std::endl;
133
        //proc->env->ReleaseObject
134
        for (size_t i = 0; i < values.size(); i++) {
135
            values[i]->decrementRefCount();
136
        std::cout<<"xdmItem (XdmValue destructor) refCount="<<values[i]->getRefCount()<<std::endl;
137
                if (values[i] != nullptr && values[i]->getRefCount() < 1) {
138
                        delete values[i];
139
                        safeToClear = true;
140
                } else {
141
                        safeToClear = false;
142
                }
143
        }
144
        if(safeToClear) {
145
                values.clear();
146
        }
147
        if (valueType != nullptr) { delete valueType; }
148
        if (jValues) {
149
        std::cout<<"xdmValue jni"<<getRefCount()<<std::endl;
150
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(jValues);
151
        }
152
        xdmSize = 0;
153

    
154
    }
155

    
156
    if(!toStringValue.empty()) {
157
        toStringValue.clear();
158
    }
159
    std::cout<<"end - xdmValue destructor refCount="<<getRefCount()<<std::endl;
160

    
161
}
162

    
163
void XdmValue::addXdmItem(XdmItem* val) {
164
        if (val != nullptr) {
165
                values.push_back(val);
166
        val->incrementRefCount();
167
                xdmSize++;
168
        if (jValues) {
169
            std::cout<<"xdmValue jni"<<getRefCount()<<std::endl;
170
            SaxonProcessor::sxn_environ->env->DeleteLocalRef(jValues);  //TODO check - this maybe should be DeleteGlobalRef  - check where it is created
171
            jValues = nullptr;
172
        }
173

    
174
        }
175
}
176

    
177

    
178
void XdmValue::addUnderlyingValue(jobject val) {
179
        XdmItem * valuei = new XdmItem(val);
180
        valuei->incrementRefCount();
181
        values.push_back(valuei);
182
        xdmSize++;
183
        jValues = nullptr; //TODO clear jni array from memory if needed
184

    
185
}
186

    
187
void XdmValue::incrementRefCount() {
188
                refCount++;
189
                std::cerr<<"refCount-inc-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
190
        }
191

    
192
void XdmValue::decrementRefCount() {
193
                if (refCount > 0)
194
                        refCount--;
195
                std::cerr<<"refCount-dec-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
196
        }
197

    
198

    
199

    
200

    
201
XdmItem * XdmValue::getHead() {
202
        if (values.size() > 0) {
203
                return values[0];
204
        }
205
        else {
206
                return nullptr;
207
        }
208
}
209

    
210
jobject XdmValue::getUnderlyingValue() {
211
        if (jValues == nullptr) {
212
                int i;
213
                JNIEnv *env = SaxonProcessor::sxn_environ->env;
214
                int count = values.size();
215
                if (count == 0) {
216
                        return nullptr;
217
                }
218
                jclass objectClass = lookForClass(env,
219
                        "net/sf/saxon/s9api/XdmItem");
220
                jobjectArray jValuesi = (jobjectArray)env->NewObjectArray((jint)count, objectClass, 0);
221

    
222

    
223
                for (i = 0; i < count; i++) {
224
                        env->SetObjectArrayElement(jValuesi, i, values[i]->getUnderlyingValue());
225
                }
226
        jValues = (jobjectArray)SaxonProcessor::sxn_environ->env->NewGlobalRef(jValuesi);
227
        } 
228
        return (jobject)jValues;
229
}
230

    
231
void XdmValue::releaseXdmValue() {
232

    
233

    
234

    
235
}
236

    
237
XdmItem * XdmValue::itemAt(int n) {
238
        if (n >= 0 && (unsigned int)n < values.size()) {
239
                return values[n];
240
        }
241
        return nullptr;
242
}
243

    
244
/**
245
* Get the type of the object
246
*/
247
XDM_TYPE XdmValue::getType() {
248
        return XDM_VALUE;
249
}
250

    
251

    
252

    
253

    
(35-35/55)