Project

Profile

Help

Revision 5d25f10d

Added by O'Neil Delpratt 5 months ago

Saxon/C development - XPath tests

View differences:

src/main/c/Saxon.C.API/XPathProcessor.cpp
28 28
		std::cerr << "Processor is nullptr" << std::endl;
29 29
	}
30 30

  
31
	cppXP = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
31
    jobject tempcppXP = createSaxonProcessor2(SaxonProcessor::sxn_environ->env, cppClass,
32 32
			"(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
33 33

  
34 34

  
35
    if(tempcppXP) {
36
        cppXP = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXP);
37
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(tempcppXP);
38

  
39
    } else {
40
        createException("Error: Failed to create the XPathProcessor internal object");
41

  
42
    }
43

  
44

  
35 45

  
36 46
#ifdef DEBUG
37 47
	jmethodID debugMID = SaxonProcessor::sxn_environ->env->GetStaticMethodID(cppClass, "setDebugMode", "(Z)V");
......
47 57

  
48 58
}
49 59

  
60

  
61
XPathProcessor::~XPathProcessor(){
62
    clearProperties();
63
    clearParameters();
64

  
65
    SaxonProcessor::sxn_environ->env->DeleteGlobalRef(cppXP);
66
    exceptionClear();
67
    //delete contextItem;
68
}
69

  
50 70
XdmValue * XPathProcessor::evaluate(const char * xpathStr) {
51 71
	if (xpathStr == nullptr) {
52 72
		std::cerr << "Error:: XPath string cannot be empty or nullptr" << std::endl;
......
241 261
}
242 262

  
243 263
void XPathProcessor::setContextItem(XdmItem * item) {
244
	contextItem = item;
245 264
    	if(item != nullptr){
246 265
     	 parameters["node"] = (XdmValue *)item;
247 266
    	}
......
429 448
        for (std::map<std::string, XdmValue *>::iterator itr = parameters.begin(); itr != parameters.end(); itr++) {
430 449
            XdmValue *value = itr->second;
431 450
            if (value != nullptr) {
432
                value->decrementRefCount();
433 451
#ifdef DEBUG
434 452
                std::cerr<<"XPathProc.clearParameter() - XdmValue refCount="<<value->getRefCount()<<std::endl;
435 453
#endif
436
                if (value->getRefCount() < 1) {
437 454
                    delete value;
438
                }
439 455
            }
440 456
        }
441 457
    }
......
463 479
}
464 480

  
465 481
void XPathProcessor::exceptionClear(){
466
	if(exception != nullptr) {
467
		delete proc->exception;
468
		exception = nullptr;
469
	}
482
    if(exception != nullptr) {
483
        delete exception;
484
        exception = nullptr;
485
        SaxonProcessor::sxn_environ->env->ExceptionClear();
486
    }
470 487

  
471
   SaxonProcessor::sxn_environ->env->ExceptionClear();
472
 
473 488
}
474 489

  
475 490
const char * XPathProcessor::getErrorCode() {
......
487 502

  
488 503
}
489 504

  
490
    bool XPathProcessor::exceptionOccurred(){
491
	return proc->exceptionOccurred();
492
    }
505
bool XPathProcessor::exceptionOccurred() {
506
    return proc->exceptionOccurred() || exception != nullptr;
507
}
493 508

  
494 509

  
495 510

  
496
    const char* XPathProcessor::checkException(){
497
	return proc->checkException();
511
void XPathProcessor::createException(const char * message) {
512
    if(exception != nullptr) {
513
        delete exception;
514
        exception = nullptr;
498 515
    }
516
    if(message == nullptr) {
517
        exception = proc->checkAndCreateException(cppClass);
518
    } else {
519
        exception = new SaxonApiException(message);
520
    }
521

  
522
}
499 523

  
src/main/c/Saxon.C.API/XPathProcessor.h
33 33
    */
34 34
    XPathProcessor();
35 35

  
36
    ~XPathProcessor(){
37
	clearProperties();
38
	clearParameters(false);
39
	//delete contextItem;
40
    }
36
    ~XPathProcessor();
41 37

  
42 38
    //! Constructor with the SaxonProcessor supplied.
43 39
    /*!
......
227 223
    */
228 224
    const char * getErrorCode();
229 225

  
230
     //! Check for exception thrown.
231
	/**
232
	* @return char*. Returns the exception message if thrown otherwise return nullptr
233
	*/
234
    const char* checkException();
235 226

  
236 227
    //! Get the SaxonApiException object created when we have an error
237 228
    /**
......
241 232
     */
242 233
    SaxonApiException * getException();
243 234

  
244

  
245 235
private:
236

  
237
    void createException(const char * message=nullptr);
238

  
246 239
	SaxonProcessor * proc;
247
	XdmItem * contextItem;
248 240
        std::string cwdXP; /*!< current working directory */
249 241
	jclass  cppClass;
250 242
	jobject cppXP;
src/main/c/Saxon.C.API/Xslt30Processor.cpp
42 42
			"(Lnet/sf/saxon/s9api/Processor;)V", proc->proc);
43 43
	if(tempcppXT) {
44 44
        cppXT = SaxonProcessor::sxn_environ->env->NewGlobalRef(tempcppXT);
45
        SaxonProcessor::sxn_environ->env->DeleteLocalRef(tempcppXT);
46

  
45 47
    } else {
46 48
	    createException("Error: Failed to create the Xslt30Processor internal object");
47 49

  
src/main/c/samples/cppTests/testXPath.cpp
12 12
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
13 13
void testXPathSingle(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
14 14

  
15
       cout<<endl<<"Test testXPathSingle:"<<endl; 
16
      XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
15
    cout<<endl<<"Test testXPathSingle:"<<endl;
16
    XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person>text3</person></out>");
17 17

  
18 18
	xpath->setContextItem((XdmItem *)input);
19 19
	XdmItem * result = xpath->evaluateSingle("//person[1]");
20 20
	
21
	if(result == NULL) {
21
	if(result == nullptr) {
22 22
		 printf("result is null \n");
23
        SaxonApiException * exception = xpath->getException();
24
        if(exception != nullptr) {
25
            const char *message = xpath->getErrorMessage();
26
            cout << "Error Message = " << message << endl;
27
            xpath->exceptionClear();
28
        }
23 29
        sresult->failure++;
24 30
        sresult->failureList.push_back("testXPathSingle");
25 31

  
26 32
	} else {
27 33
		cout<<"Number of items="<<result->size()<<endl;
28 34
		cout<<"String Value of result="<<result->getStringValue()<<endl;
35
		sresult->success++;
29 36
	}
30 37
	delete result;
31 38

  
......
33 40

  
34 41
}
35 42

  
36
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
43
// Test case on the evaluate method in XPathProcessor. Here we test that we have more than one XdmItem.
37 44
void testXPathValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
38 45
     cout<<endl<<"Test testXPathValues:"<<endl; 
39
  	xpath->clearParameters(true);
46
  	xpath->clearParameters();
40 47
  	xpath->clearProperties();
41 48
	 XdmNode * input = processor->parseXmlFromString("<out><person>text1</person><person>text2</person><person1>text3</person1></out>");
42
	if(input == NULL) {return;}
49
	if(input == nullptr) {
50
        SaxonApiException * exception = xpath->getException();
51
        if(exception != nullptr) {
52
            const char *message = xpath->getErrorMessage();
53
            cout << "Error Message = " << message << endl;
54
            xpath->exceptionClear();
55
        }
56
        sresult->failure++;
57
        sresult->failureList.push_back("testXPathValues");
58

  
59
	    return;
60

  
61
	}
62

  
43 63
	XdmNode ** children = input->getChildren();
44 64
	XdmNode ** children0 = children[0]->getChildren();
45 65
	int num = children[0]->getChildCount();
46
	delete []children;
66

  
47 67
	cout<<"number of children: "<<num<<endl;
48 68
	for(int i=0; i<num;i++) {
49 69
		cout<<"node name:"<<children0[i]->getNodeName()<<endl;
50 70
	}
71

  
72
    delete []children;
51 73
	delete [] children0;
52 74

  
53 75
	xpath->setContextItem((XdmItem *)input);
54 76
	XdmValue * resultValues = xpath->evaluate("//person");
55 77
	
56
	if(resultValues == NULL) {
78
	if(resultValues == nullptr) {
57 79
		 printf("result is null \n");
80
        SaxonApiException * exception = xpath->getException();
81
        if(exception != nullptr) {
82
            const char *message = xpath->getErrorMessage();
83
            cout << "Error Message = " << message << endl;
84
            xpath->exceptionClear();
85
        }
86
        sresult->failure++;
87
        sresult->failureList.push_back("testXPathValues");
88

  
58 89
	} else {
59 90
		cout<<"Number of items="<<resultValues->size()<<endl;
91
		bool nullFound = false;
60 92
		for(int i =0; i< resultValues->size();i++){
61 93
			XdmItem * itemi = resultValues->itemAt(i);
62
			if(itemi == NULL) {
94
			if(itemi == nullptr) {
63 95
				cout<<"Item at position "<<i<<" should not be null"<<endl;
96
				nullFound = true;
64 97
				break;
65 98
			}
66 99
			cout<<"Item at "<<i<<" ="<<itemi->getStringValue()<<endl;		
67 100
		}
101
        delete resultValues;
102
		if(nullFound) {
103
            sresult->success++;
104
        } else {
105
		    sresult->failure++;
106
		    sresult->failureList.push_back("testXPathValues");
107
		}
68 108
	}
69
	delete resultValues;
109
    delete input;
110
    xpath->clearParameters();
111
    xpath->clearProperties();
70 112
	
71 113

  
72 114
}
......
74 116
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
75 117
void testXPathAttrValues(SaxonProcessor * processor, XPathProcessor * xpath, sResultCount *sresult){
76 118
     cout<<endl<<"Test testXPathValues1:"<<endl; 
77
  	xpath->clearParameters(true);
119
  	xpath->clearParameters();
78 120
  	xpath->clearProperties();
79 121
	 XdmNode * input = processor->parseXmlFromString("<out attr='valuex'><person attr1='value1' attr2='value2'>text1</person><person>text2</person><person1>text3</person1></out>");
80
	if(input == NULL) {return;}
122
	if(input == nullptr) {
123
        SaxonApiException * exception = xpath->getException();
124
        if(exception != nullptr) {
125
            const char *message = xpath->getErrorMessage();
126
            cout << "Error Message = " << message << endl;
127
            xpath->exceptionClear();
128
        }
129
        sresult->failure++;
130
        sresult->failureList.push_back("testXPathAttrValues");
131

  
132
	    return;
133

  
134
	}
81 135

  
82 136
	//cout<<"Name of attr1= "<<input->getAttributeValue("attr")<<endl;
83 137

  
......
85 139
	xpath->setContextItem((XdmItem *)input);
86 140
	XdmItem * result = xpath->evaluateSingle("(//person)[1]");
87 141
	
88
	if(result == NULL) {
142
	if(result == nullptr) {
89 143
		 printf("result is null \n");
90 144
	} else {
91 145
		XdmNode *nodeValue = (XdmNode*)result;
92 146
		cout<<"Attribute Count= "<<nodeValue->getAttributeCount()<<endl;
93 147
		const char *attrVal = nodeValue->getAttributeValue("attr1");
94
		if(attrVal != NULL) {
148
		if(attrVal != nullptr) {
95 149
			cout<<"Attribute value= "<<attrVal<<endl;
96 150
		}
97 151
		
98 152
		XdmNode ** attrNodes = nodeValue->getAttributeNodes();
99
		if(attrNodes == NULL) { return;}
153
		if(attrNodes == nullptr) { return;}
100 154
	
101 155
		const char *name1 = attrNodes[0]->getNodeName();
102
		if(name1 != NULL) {
156
		if(name1 != nullptr) {
103 157
			cout<<"Name of attr1= "<<name1<<endl;
104 158
		}
105 159
		XdmNode * parent = attrNodes[0]->getParent();
106
		if(parent != NULL) {
160
		if(parent != nullptr) {
107 161
			cout<<"Name of parent= "<<parent->getNodeName()<<endl;
108 162
		}
109 163
		XdmNode * parent1 = parent->getParent();
110
		if(parent1 != NULL) {
164
		if(parent1 != nullptr) {
111 165
			cout<<"Name of parent= "<<parent1->getNodeName()<<endl;
112 166
		}
167
		sresult->success++;
113 168
	//TODO test if attr value is not there
114 169
	}
115 170
	
......
126 181

  
127 182
	XdmValue * resultValues = xpath->evaluate("//person");
128 183
	
129
	if(resultValues == NULL) {
184
	if(resultValues == nullptr) {
130 185
		 printf("result is null \n");
186
        SaxonApiException * exception = xpath->getException();
187
        if(exception != nullptr) {
188
            const char *message = xpath->getErrorMessage();
189
            cout << "Error Message = " << message << endl;
190
            xpath->exceptionClear();
191
        }
192
        sresult->failure++;
193
        sresult->failureList.push_back("testXPathOnFile");
131 194
	} else {
132 195
		cout<<"Number of items="<<resultValues->size()<<endl;
133 196
		for(int i =0; i< resultValues->size();i++){
134 197
			XdmItem * itemi = resultValues->itemAt(i);
135
			if(itemi == NULL) {
198
			if(itemi == nullptr) {
136 199
				cout<<"Item at position "<<i<<" should not be null"<<endl;
137 200
				break;
138 201
			}
......
147 210
// Test case on the evaluate method in XPathProcessor. Here we test that we have morethan one XdmItem.
148 211
void testXPathOnFile2(XPathProcessor * xpath, sResultCount *sresult){
149 212
    	 cout<<endl<<"Test testXPath with file source:"<<endl;
150
  	xpath->clearParameters(true);
213
  	xpath->clearParameters();
151 214
  	xpath->clearProperties(); 
152 215
	xpath->setContextFile("cat.xml");
153 216

  
154 217
	XdmItem * result = xpath->evaluateSingle("//person[1]");
155 218
	
156
	if(result == NULL) {
219
	if(result == nullptr) {
157 220
		 printf("result is null \n");
221
        SaxonApiException * exception = xpath->getException();
222
        if(exception != nullptr) {
223
            const char *message = xpath->getErrorMessage();
224
            cout << "Error Message = " << message << endl;
225
            xpath->exceptionClear();
226
        }
227
        sresult->failure++;
228
        sresult->failureList.push_back("testXPathOnFile2");
229
        return;
158 230
	} else {
159 231
		cout<<"Number of items="<<result->size()<<endl;
160 232
		if(result->isAtomic()) {
......
170 242
			cout<<"Node is of kind:"<<node->getNodeKind()<<endl;
171 243
		}
172 244
		const char * baseURI = node->getBaseUri();
173
		if(baseURI != NULL) {
245
		if(baseURI != nullptr) {
174 246
			cout<<"baseURI of node: "<<baseURI<<endl;
175 247
		}
248
		sresult->success++;
176 249
	}
177 250
	delete result;
178
	
179

  
180 251
}
181 252

  
182 253
int main()
......
205 276

  
206 277
    }
207 278

  
279
    delete xpathProc;
280
    delete processor;
281

  
208 282
     processor->release();
283

  
284

  
285
#ifdef MEM_DEBUG
286
    SaxonProcessor::getInfo();
287
#endif
209 288
    return 0;
210 289
}
src/main/c/samples/cppTests/testXSLT30.cpp
644 644

  
645 645
    XdmValue *rootValue = executable->callTemplateReturningValue("go");
646 646
    if (rootValue== nullptr) {
647
        cout << "Result is null ====== FAIL ====== " << endl;
647
        if (executable->exceptionOccurred()) {
648
            SaxonApiException *exception = executable->getException();
649
            cerr << "Error: " << exception->getMessage() << endl;
650
            delete exception;
651
        }
648 652
        sresult->failure++;
649 653
        sresult->failureList.push_back("testXdmNodeOutput-0.0");
650 654
        return;
......
1361 1365

  
1362 1366

  
1363 1367
    if (value== nullptr) {
1364

  
1368
        if (executable->exceptionOccurred()) {
1369
            SaxonApiException *exception = executable->getException();
1370
            cerr << "Error: " << exception->getMessage() << endl;
1371
            delete exception;
1372
        }
1365 1373
        sresult->failure++;
1366 1374
        sresult->failureList.push_back("testResolveUri");
1367 1375
    } else {

Also available in: Unified diff