Project

Profile

Help

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

he / src / main / c / Saxon.C.API / XdmValue.cpp @ 55b80284

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 55b80284 O'Neil Delpratt
10
#ifdef MEM_DEBUG
11
#define new new(__FILE__, __LINE__)
12
#endif
13
14
15 72bf04c6 Norman Walsh
XdmValue::XdmValue(const XdmValue &other) {
16
        //SaxonProcessor *proc = other.proc; //TODO
17
        valueType = other.valueType;
18 a69dd173 O'Neil Delpratt
        refCount = 0;
19 72bf04c6 Norman Walsh
        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 ead48a5d O'Neil Delpratt
                        return nullptr;
40 72bf04c6 Norman Walsh
                }
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 ead48a5d O'Neil Delpratt
                                        toStringValue += SaxonProcessor::sxn_environ->env->GetStringUTFChars(result, nullptr);
48 72bf04c6 Norman Walsh
                                        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 a69dd173 O'Neil Delpratt
        values.resize(0);//TODO memory issue might occur here. Need to delete XdmItems
72 72bf04c6 Norman Walsh
        values.push_back(value);
73
        xdmSize++;
74 ead48a5d O'Neil Delpratt
        jValues = nullptr;
75
        valueType = nullptr;
76 72bf04c6 Norman Walsh
}
77
78
79
XdmValue::XdmValue(jobject val, bool arr){
80
        xdmSize = 0;
81
        values.resize(0);
82 ead48a5d O'Neil Delpratt
        jValues = nullptr;
83
        valueType = nullptr;
84 72bf04c6 Norman Walsh
        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 ead48a5d O'Neil Delpratt
                XdmItem * xdmItem = nullptr;
105 72bf04c6 Norman Walsh
                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 146cfacb O'Neil Delpratt
        //bool safeToClear = false;
136
        if(values.size() > 0) {
137
        for (size_t i = 0; i < values.size(); i++) {
138 4ee4355d O'Neil Delpratt
            if (values[i] != nullptr) {
139
                //values[i]->decrementRefCount();
140 146cfacb O'Neil Delpratt
                delete values[i];
141
            }
142
        }
143
        values.clear();
144
    }
145 ead48a5d O'Neil Delpratt
        if (valueType != nullptr) { delete valueType; }
146 901b6eca O'Neil Delpratt
        if (jValues) {
147 4ee4355d O'Neil Delpratt
                SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
148 72bf04c6 Norman Walsh
        }
149
        xdmSize = 0;
150 a69dd173 O'Neil Delpratt
151
    if(!toStringValue.empty()) {
152
        toStringValue.clear();
153 72bf04c6 Norman Walsh
    }
154
155
}
156
157
void XdmValue::addXdmItem(XdmItem* val) {
158 ead48a5d O'Neil Delpratt
        if (val != nullptr) {
159 72bf04c6 Norman Walsh
                values.push_back(val);
160 a69dd173 O'Neil Delpratt
        val->incrementRefCount();
161 72bf04c6 Norman Walsh
                xdmSize++;
162 00082f6f O'Neil Delpratt
        if (jValues) {
163 4ee4355d O'Neil Delpratt
            SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
164 00082f6f O'Neil Delpratt
            jValues = nullptr;
165
        }
166
167 72bf04c6 Norman Walsh
        }
168
}
169
170
171
void XdmValue::addUnderlyingValue(jobject val) {
172
        XdmItem * valuei = new XdmItem(val);
173 a69dd173 O'Neil Delpratt
        valuei->incrementRefCount();
174 72bf04c6 Norman Walsh
        values.push_back(valuei);
175
        xdmSize++;
176 ead48a5d O'Neil Delpratt
        jValues = nullptr; //TODO clear jni array from memory if needed
177 72bf04c6 Norman Walsh
178
}
179
180
void XdmValue::incrementRefCount() {
181
                refCount++;
182 a69dd173 O'Neil Delpratt
                std::cerr<<"refCount-inc-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
183 72bf04c6 Norman Walsh
        }
184
185
void XdmValue::decrementRefCount() {
186
                if (refCount > 0)
187
                        refCount--;
188 a69dd173 O'Neil Delpratt
                std::cerr<<"refCount-dec-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
189 72bf04c6 Norman Walsh
        }
190
191
192
193
194
XdmItem * XdmValue::getHead() {
195
        if (values.size() > 0) {
196
                return values[0];
197
        }
198
        else {
199 ead48a5d O'Neil Delpratt
                return nullptr;
200 72bf04c6 Norman Walsh
        }
201
}
202
203
jobject XdmValue::getUnderlyingValue() {
204 ead48a5d O'Neil Delpratt
        if (jValues == nullptr) {
205 72bf04c6 Norman Walsh
                int i;
206
                JNIEnv *env = SaxonProcessor::sxn_environ->env;
207
                int count = values.size();
208
                if (count == 0) {
209 ead48a5d O'Neil Delpratt
                        return nullptr;
210 72bf04c6 Norman Walsh
                }
211
                jclass objectClass = lookForClass(env,
212
                        "net/sf/saxon/s9api/XdmItem");
213 00082f6f O'Neil Delpratt
                jobjectArray jValuesi = (jobjectArray)env->NewObjectArray((jint)count, objectClass, 0);
214
215 72bf04c6 Norman Walsh
216
                for (i = 0; i < count; i++) {
217 00082f6f O'Neil Delpratt
                        env->SetObjectArrayElement(jValuesi, i, values[i]->getUnderlyingValue());
218 72bf04c6 Norman Walsh
                }
219 00082f6f O'Neil Delpratt
        jValues = (jobjectArray)SaxonProcessor::sxn_environ->env->NewGlobalRef(jValuesi);
220 72bf04c6 Norman Walsh
        } 
221
        return (jobject)jValues;
222
}
223
224
void XdmValue::releaseXdmValue() {
225 4ee4355d O'Neil Delpratt
    for (size_t i = 0; i < values.size(); i++) {
226
        if (values[i] != nullptr) {
227
            delete values[i];
228
        }
229
    }
230 72bf04c6 Norman Walsh
231 4ee4355d O'Neil Delpratt
    if(jValues) {
232
        SaxonProcessor::sxn_environ->env->DeleteGlobalRef(jValues);
233
    }
234 72bf04c6 Norman Walsh
235
236
}
237
238
XdmItem * XdmValue::itemAt(int n) {
239
        if (n >= 0 && (unsigned int)n < values.size()) {
240
                return values[n];
241
        }
242 ead48a5d O'Neil Delpratt
        return nullptr;
243 72bf04c6 Norman Walsh
}
244
245
/**
246
* Get the type of the object
247
*/
248
XDM_TYPE XdmValue::getType() {
249
        return XDM_VALUE;
250
}
251
252
253
254