Project

Profile

Help

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

he / latest9.9 / hec / Saxon.C.API / XdmValue.cpp @ 5401a5ae

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 = 1;
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 NULL;
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, NULL);
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
void XdmValue::setProcessor(SaxonProcessor *p) { 
59
                proc = p;
60
        }
61

    
62

    
63
int XdmValue::size() {
64
        return xdmSize;
65
}
66

    
67
XdmValue::XdmValue(jobject val) {
68
        XdmItem * value = new XdmItem(val);
69
        values.resize(0);//TODO memory issue might occur here
70
        values.push_back(value);
71
        xdmSize++;
72
        jValues = NULL;
73
        valueType = NULL;
74
}
75

    
76

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

    
85
        if(!xvfMID){
86

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

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

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

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

    
115

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

    
131

    
132
XdmValue::~XdmValue() {
133
        bool safeToClear = false;
134
    if(getRefCount()<1){
135
        //proc->env->ReleaseObject
136
        for (size_t i = 0; i < values.size(); i++) {
137
                if (values[i] != NULL && 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 != NULL) { delete valueType; }
148
        if (jValues && proc != NULL) {
149
                SaxonProcessor::sxn_environ->env->DeleteLocalRef(jValues);  //TODO check - this maybe should be DeleteGlobalRef  - check where it is created
150
        }
151
        xdmSize = 0;
152
        if(!toStringValue.empty()) {
153
                toStringValue.clear();
154
        }
155
    }
156

    
157
}
158

    
159
void XdmValue::addXdmItem(XdmItem* val) {
160
        if (val != NULL) {
161
                values.push_back(val);
162
                xdmSize++;
163
                jValues = NULL; //TODO clear jni array from memory if needed
164
        }
165
}
166

    
167

    
168
void XdmValue::addUnderlyingValue(jobject val) {
169
        XdmItem * valuei = new XdmItem(val);
170
        valuei->setProcessor(proc);
171
        values.push_back(valuei);
172
        xdmSize++;
173
        jValues = NULL; //TODO clear jni array from memory if needed
174

    
175
}
176

    
177
void XdmValue::incrementRefCount() {
178
                refCount++;
179
                //std::cerr<<"refCount-inc-xdmVal="<<refCount<<" ob ref="<<(this)<<std::endl;
180
        }
181

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

    
188

    
189

    
190

    
191
XdmItem * XdmValue::getHead() {
192
        if (values.size() > 0) {
193
                return values[0];
194
        }
195
        else {
196
                return NULL;
197
        }
198
}
199

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

    
212
                for (i = 0; i < count; i++) {
213
                        env->SetObjectArrayElement(jValues, i, values[i]->getUnderlyingValue());
214
                }
215
        } 
216
        return (jobject)jValues;
217
}
218

    
219
void XdmValue::releaseXdmValue() {
220

    
221

    
222

    
223
}
224

    
225
XdmItem * XdmValue::itemAt(int n) {
226
        if (n >= 0 && (unsigned int)n < values.size()) {
227
                return values[n];
228
        }
229
        return NULL;
230
}
231

    
232
/**
233
* Get the type of the object
234
*/
235
XDM_TYPE XdmValue::getType() {
236
        return XDM_VALUE;
237
}
238

    
239

    
240

    
241

    
(32-32/52)