Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmValue.cpp @ 339c45ee

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

    
10
#ifdef MEM_DEBUG
11
#define new new(__FILE__, __LINE__)
12
#endif
13

    
14

    
15
XdmValue::XdmValue(const XdmValue &other) {
16
        //SaxonProcessor *proc = other.proc; //TODO
17
        valueType = other.valueType;
18
        refCount = 0;
19
        xdmSize = other.xdmSize;
20
        jValues = other.jValues;
21
        toStringValue = other.toStringValue;
22
        values.resize(0);//TODO memory issue might occur here
23
        toStringValue = other.toStringValue;
24
        for (int i = 0; i < xdmSize; i++) {
25
                addXdmItem(other.values[i]);
26
        }
27
        
28
}
29

    
30
const char * XdmValue::toString() {
31
        if (toStringValue.empty()) {
32
                jclass xdmValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmValue");
33
                jmethodID strMID2 = (jmethodID)SaxonProcessor::sxn_environ->env->GetMethodID(xdmValueClass,
34
                        "toString",
35
                        "()Ljava/lang/String;");
36
                if (!strMID2) {
37
                        std::cerr << "Error: Saxonc.XdmValue." << "toString"
38
                                << " not found\n" << std::endl;
39
                        return nullptr;
40
                }
41
                else {
42
                        for(int i=0; i<size();i++) {
43

    
44
                        
45
                                jstring result = (jstring)(SaxonProcessor::sxn_environ->env->CallObjectMethod(itemAt(i)->getUnderlyingValue(), strMID2));
46
                                if (result) {
47
                                        toStringValue += SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
48
                                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(result);
49
                                
50
                                }
51
                        }
52

    
53
                        return toStringValue.c_str();                        
54
                }
55
                        
56
                
57
        }
58
        else {
59
                return toStringValue.c_str();
60
        }
61

    
62
}
63

    
64

    
65
int XdmValue::size() {
66
        return xdmSize;
67
}
68

    
69
XdmValue::XdmValue(jobject val) {
70
        XdmItem * value = new XdmItem(val);
71
        values.resize(0);//TODO memory issue might occur here. Need to delete XdmItems
72
        values.push_back(value);
73
        xdmSize++;
74
        jValues = nullptr;
75
        valueType = nullptr;
76
}
77

    
78

    
79
XdmValue::XdmValue(jobject val, bool arr){
80
        xdmSize = 0;
81
        values.resize(0);
82
        jValues = nullptr;
83
        valueType = nullptr;
84
        jclass xdmValueForcppClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/option/cpp/XdmValueForCpp");
85
        jmethodID xvfMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(xdmValueForcppClass, "makeArrayFromXdmValue", "(Lnet/sf/saxon/s9api/XdmValue;)[Lnet/sf/saxon/s9api/XdmItem;");
86

    
87
        if(!xvfMID){
88

    
89
                std::cerr << "Error: SaxonDll." << "makeArrayFromXdmValue"
90
                                << " not found\n" << std::endl;
91
                        return ;
92
        }
93
        
94
        jobjectArray results = (jobjectArray) SaxonProcessor::sxn_environ->env->CallStaticObjectMethod(xdmValueForcppClass, xvfMID, val);
95
        if(results){
96
        int sizex = SaxonProcessor::sxn_environ->env->GetArrayLength(results);
97
        if (sizex>0) {
98
                jclass atomicValueClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmAtomicValue");
99
                jclass nodeClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmNode");
100
                jclass functionItemClass = lookForClass(SaxonProcessor::sxn_environ->env, "net/sf/saxon/s9api/XdmFunctionItem");
101

    
102
                //XdmValue * value = new XdmValue();
103
                //value->setProcessor(proc);
104
                XdmItem * xdmItem = nullptr;
105
                for (int p=0; p < sizex; ++p) 
106
                {
107
                        jobject resulti = SaxonProcessor::sxn_environ->env->GetObjectArrayElement(results, p);
108
                        //value->addUnderlyingValue(resulti);
109

    
110
                        if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, atomicValueClass)           == JNI_TRUE) {
111
                                xdmItem = new XdmAtomicValue(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
112
                                
113

    
114
                        } else if(SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, nodeClass)           == JNI_TRUE) {
115
                                xdmItem = new XdmNode(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
116

    
117

    
118
                        } else if (SaxonProcessor::sxn_environ->env->IsInstanceOf(resulti, functionItemClass)           == JNI_TRUE) {
119
                              //Not Supported yet
120
#if CVERSION_API_NO >= 123
121
                                xdmItem = new XdmFunctionItem(SaxonProcessor::sxn_environ->env->NewGlobalRef(resulti));
122
#endif
123
                        }
124
                        //xdmItem->setProcessor(proc);
125
                        addXdmItem(xdmItem);
126
                        SaxonProcessor::sxn_environ->env->DeleteLocalRef(resulti);
127
                }
128
        }
129
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(results);
130
        }        
131
}
132

    
133

    
134
XdmValue::~XdmValue() {
135
        //bool safeToClear = false;
136
        if(values.size() > 0) {
137
        for (size_t i = 0; i < values.size(); i++) {
138
            if (values[i] != nullptr) {
139
                //values[i]->decrementRefCount();
140
                delete values[i];
141
            }
142
        }
143
        values.clear();
144
    }
145
        if (valueType != nullptr) { delete valueType; }
146
        if (jValues) {
147
                SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
148
        }
149
        xdmSize = 0;
150

    
151
    if(!toStringValue.empty()) {
152
        toStringValue.clear();
153
    }
154

    
155
}
156

    
157
void XdmValue::addXdmItem(XdmItem* val) {
158
        if (val != nullptr) {
159
                values.push_back(val);
160
        val->incrementRefCount();
161
                xdmSize++;
162
        if (jValues) {
163
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
164
            jValues = nullptr;
165
        }
166

    
167
        }
168
}
169

    
170

    
171
void XdmValue::addUnderlyingValue(jobject val) {
172
        XdmItem * valuei = new XdmItem(val);
173
        valuei->incrementRefCount();
174
        values.push_back(valuei);
175
        xdmSize++;
176
        jValues = nullptr; //TODO clear jni array from memory if needed
177

    
178
}
179

    
180
void XdmValue::incrementRefCount() {
181
                refCount++;
182
                std::cerr<<"refCount-inc-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
183
        }
184

    
185
void XdmValue::decrementRefCount() {
186
                if (refCount > 0)
187
                        refCount--;
188
                std::cerr<<"refCount-dec-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
189
        }
190

    
191

    
192

    
193

    
194
XdmItem * XdmValue::getHead() {
195
        if (values.size() > 0) {
196
                return values[0];
197
        }
198
        else {
199
                return nullptr;
200
        }
201
}
202

    
203
jobject XdmValue::getUnderlyingValue() {
204
        if (jValues == nullptr) {
205
                int i;
206
                JNIEnv *env = SaxonProcessor::sxn_environ->env;
207
                int count = values.size();
208
                if (count == 0) {
209
                        return nullptr;
210
                }
211
                jclass objectClass = lookForClass(env,
212
                        "net/sf/saxon/s9api/XdmItem");
213
                jobjectArray jValuesi = (jobjectArray)env->NewObjectArray((jint)count, objectClass, 0);
214

    
215

    
216
                for (i = 0; i < count; i++) {
217
                        env->SetObjectArrayElement(jValuesi, i, values[i]->getUnderlyingValue());
218
                }
219
        jValues = (jobjectArray)SaxonProcessor::sxn_environ->env->NewGlobalRef(jValuesi);
220
        } 
221
        return (jobject)jValues;
222
}
223

    
224
void XdmValue::releaseXdmValue() {
225
    for (size_t i = 0; i < values.size(); i++) {
226
        if (values[i] != nullptr) {
227
            delete values[i];
228
        }
229
    }
230

    
231
    if(jValues) {
232
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
233
    }
234

    
235

    
236
}
237

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

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

    
252

    
253

    
254

    
(36-36/56)