1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /*
19  * $Id$
20  */
21 
22 
23 
24 /**
25  * This class tests methods for XML DOM implementation
26  * DOMException errors are tested by calls to DOMExceptionsTest from: Main, docBuilder...
27  *
28  */
29 
30 #include <stdio.h>
31 #include "DTest.h"
32 #include <xercesc/util/PlatformUtils.hpp>
33 #include <xercesc/util/XMLException.hpp>
34 #include <xercesc/util/XMLString.hpp>
35 #include <xercesc/util/BinInputStream.hpp>
36 #include <xercesc/util/regx/Match.hpp>
37 #include <xercesc/util/TransService.hpp>
38 #include <xercesc/util/OutOfMemoryException.hpp>
39 #include <xercesc/parsers/XercesDOMParser.hpp>
40 #include <xercesc/dom/DOMException.hpp>
41 #include <xercesc/dom/DOMLSException.hpp>
42 #include <xercesc/dom/DOMLSParserFilter.hpp>
43 #include <xercesc/framework/MemBufInputSource.hpp>
44 #include <xercesc/validators/common/CMStateSet.hpp>
45 
46 #define UNUSED(x) { if(x!=0){} }
47 
48 #define EXCEPTIONSTEST(operation, expectedException, resultFlag, testNum) \
49     {                                                               \
50         try                                                         \
51         {                                                           \
52             operation;                                              \
53             fprintf(stderr, "Exceptions Test # %d: no Exception thrown->\n", testNum); \
54         }                                                           \
55         catch (DOMException &e)                                 \
56         {                                                           \
57             if (e.code != expectedException) {                      \
58                 fprintf(stderr, "Exceptions Test # %d: wrong DOMException thrown->\n", \
59                     testNum);                                       \
60                 resultFlag = false;                                 \
61             }                                                       \
62         }                                                           \
63         catch (...)                                                 \
64         {                                                           \
65             fprintf(stderr, "Exceptions Test # %d: unknown exception thrown->\n",    \
66                 testNum);                                           \
67             resultFlag = false;                                     \
68         }                                                           \
69     }
70 
71 #define USERDATAHANDLERTEST(userhandler, uoperation, ukey, udata, usrc, udst, uline) \
72     if (userhandler.getCurrentType() != uoperation) {\
73         fprintf(stderr, "DOMUserDataHandler::handler's operationType does not work in line %i\n", uline); \
74         OK = false; \
75     } \
76     if (XMLString::compareString(userhandler.getCurrentKey(), ukey)) {\
77         fprintf(stderr, "DOMUserDataHandler::handler's key does not work in line %i\n", uline); \
78         OK = false; \
79     } \
80     if (userhandler.getCurrentData() != udata) {\
81         fprintf(stderr, "DOMUserDataHandler::handler's data does not work in line %i\n", uline); \
82         OK = false; \
83     } \
84     if (userhandler.getCurrentSrc() != usrc) {\
85         fprintf(stderr, "DOMUserDataHandler::handler's src does not work in line %i\n", uline); \
86         OK = false; \
87     } \
88     if (userhandler.getCurrentDst() != udst) {\
89         fprintf(stderr, "DOMUserDataHandler::handler's dst does not work in line %i\n", uline); \
90         OK = false; \
91     }
92 
93 
94 #define LOOKUPDEFAULTNSTEST(thisNode, uri, pass, line) \
95     if(thisNode->isDefaultNamespace(uri)) { \
96         if(!pass) { \
97             fprintf(stderr, "DOMNode::isDefaultNamespace returned true in line %i\n", line); \
98             OK = false; \
99         } \
100     } \
101     else { \
102         if(pass) { \
103             fprintf(stderr, "DOMNode::isDefaultNamespace returned false in line %i\n", line); \
104             OK = false; \
105         } \
106     }
107 
108 
109 #define LOOKUPNSTEST(thisNode, prefix, uri, pass, line) \
110     prefixResult = XMLString::compareString(thisNode->lookupPrefix(uri), prefix); \
111     uriResult = XMLString::compareString(thisNode->lookupNamespaceURI(prefix), uri); \
112     if(pass) { \
113         if(prefixResult != 0) { \
114         fprintf(stderr, "DOMNode::lookupNamespacePrefix does not work in line %i\n", line); \
115         OK = false; \
116         } \
117         if(uriResult != 0) { \
118         fprintf(stderr, "DOMNode::lookupNamespaceURI does not work in line %i\n", line); \
119         OK = false;\
120             } \
121         } \
122     else { \
123         if(prefixResult == 0) { \
124         fprintf(stderr, "DOMNode::lookupNamespacePrefix does not work in line %i\n", line); \
125         OK = false; \
126         } \
127         if(uriResult == 0) { \
128         fprintf(stderr, "DOMNode::lookupNamespaceURI does not work in line %i\n", line); \
129         OK = false; \
130             } \
131         } \
132 
133 int prefixResult;
134 int uriResult;
135 
136 #define COMPARETREEPOSITIONTEST(thisNode, otherNode, position, line) \
137     myposition = thisNode->compareDocumentPosition(otherNode); \
138     if ((myposition & position) == 0) {\
139         fprintf(stderr, "DOMNode::compareDocumentPosition does not work in line %i\n", line); \
140         OK = false; \
141     }
142 
143 // temp position for compareDocumentPosition
144 short myposition;
145 
146 //temp XMLCh String Buffer
147 XMLCh tempStr[4000];
148 XMLCh tempStr2[4000];
149 XMLCh tempStr3[4000];
150 XMLCh tempStr4[4000];
151 XMLCh tempStr5[4000];
152 
153 //DOMUserDataHandler
154 myUserDataHandler userhandler;
155 
156 DOMElement*                 DOMTest::testElementNode;
157 DOMAttr*                    DOMTest::testAttributeNode;
158 DOMText*                    DOMTest::testTextNode;
159 DOMCDATASection*            DOMTest::testCDATASectionNode;
160 DOMEntityReference*         DOMTest::testEntityReferenceNode;
161 DOMEntity*                  DOMTest::testEntityNode;
162 DOMProcessingInstruction*   DOMTest::testProcessingInstructionNode;
163 DOMComment*                 DOMTest::testCommentNode;
164 DOMDocument*                DOMTest::testDocumentNode;
165 DOMDocumentType*            DOMTest::testDocumentTypeNode;
166 DOMDocumentFragment*        DOMTest::testDocumentFragmentNode;
167 DOMNotation*                DOMTest::testNotationNode;
168 
169 /**
170  *
171  *
172  */
173 
DOMTest()174 DOMTest::DOMTest()
175 {
176 }
177 
178 
179 /**
180  *
181  * @return DOMDocument
182  *
183  */
createDocument()184 DOMDocument* DOMTest::createDocument() {
185     XMLCh coreStr[100];
186     XMLString::transcode("Core",coreStr,99);
187 
188     DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(coreStr);
189     return impl->createDocument();
190 }
191 
192 
193 /**
194  *
195  * @return DOMDocumentType
196  * @param name XMLCh*
197  *
198  */
createDocumentType(DOMDocument * doc,XMLCh * name)199 DOMDocumentType* DOMTest::createDocumentType(DOMDocument* doc, XMLCh* name) {
200     return doc->createDocumentType(name);    //Replace with a DOMDocumentType* creator
201 }
202 
203 
204 /**
205  *
206  * @return org.w3c.dom.DOMEntity
207  * @param doc org.w3c.dom.DOMDocument
208  * @param name XMLCh*
209  *
210  */
createEntity(DOMDocument * doc,XMLCh * name)211 DOMEntity* DOMTest::createEntity(DOMDocument* doc, XMLCh* name) {
212     return doc->createEntity(name);
213 }
214 
215 
216 
217 /**
218  *
219  * @return org.w3c.dom.DOMNotation
220  * @param doc org.w3c.dom.DOMDocument
221  * @param name XMLCh*
222  *
223  */
createNotation(DOMDocument * doc,XMLCh * name)224 DOMNotation* DOMTest::createNotation(DOMDocument* doc, XMLCh* name) {
225     return doc->createNotation(name);
226 }
227 
228 
229 /**
230  * This method builds test documents for the XML DOM implementation
231  * @param document org.w3c.dom.DOMDocument
232  * @param name document's name
233  * @param type document's type
234  *
235  */
docBuilder(DOMDocument * document,XMLCh * nameIn)236 bool DOMTest::docBuilder(DOMDocument* document, XMLCh* nameIn)
237 {
238     XMLCh* name = XMLString::replicate(nameIn);
239 
240     DOMDocument* doc = document;
241     bool OK = true;
242 
243     //name + "FirstElement"
244     XMLString::transcode("FirstElement", tempStr2, 3999);
245     XMLString::copyString(tempStr, name);
246     XMLString::catString(tempStr, tempStr2);
247 
248     DOMElement* docFirstElement = doc->createElement(tempStr);
249     doc->appendChild(docFirstElement);
250 
251     //name + "FirstElement", name + "firstElement"
252     XMLString::catString(tempStr, name);
253     XMLString::transcode("FirstElement", tempStr2, 3999);
254     XMLString::catString(tempStr, tempStr2);
255     XMLString::catString(tempStr2, name);
256     XMLString::transcode("firstElement", tempStr3, 3999);
257     XMLString::catString(tempStr2, tempStr3);
258     docFirstElement->setAttribute(tempStr, tempStr2);
259     DOMAttr* docFirstElementAttr = docFirstElement->getAttributeNode(tempStr);
260 
261     //name + "TargetProcessorChannel" + "This is " + doc->getNodeName() + "'s processing instruction");
262     XMLString::copyString(tempStr, name);
263     XMLString::transcode("TargetProcessorChannel", tempStr2, 3999);
264     XMLString::catString(tempStr, tempStr2);
265     XMLString::transcode("This is ", tempStr2, 3999);
266     XMLString::catString(tempStr2, doc->getNodeName());
267     XMLString::transcode("'s processing instruction", tempStr3, 3999);
268     XMLString::catString(tempStr2, tempStr3);
269 
270     DOMProcessingInstruction* docProcessingInstruction = doc->createProcessingInstruction(tempStr, tempStr2);
271     docFirstElement->appendChild(docProcessingInstruction);
272 
273     //name + "TestBody"
274     XMLString::copyString(tempStr, name);
275     XMLString::transcode("TestBody", tempStr2, 3999);
276     XMLString::catString(tempStr, tempStr2);
277     DOMElement* docBody = doc->createElement(tempStr);
278     docFirstElement->appendChild(docBody);
279 
280     //name + "BodyLevel21"
281     XMLString::copyString(tempStr, name);
282     XMLString::transcode("BodyLevel21", tempStr2, 3999);
283     XMLString::catString(tempStr, tempStr2);
284     DOMElement* docBodyLevel21 = doc->createElement(tempStr);
285 
286     //name + "BodyLevel22"
287     XMLString::copyString(tempStr, name);
288     XMLString::transcode("BodyLevel22", tempStr2, 3999);
289     XMLString::catString(tempStr, tempStr2);
290     DOMElement* docBodyLevel22 = doc->createElement(tempStr);
291 
292     //name + "BodyLevel23"
293     XMLString::copyString(tempStr, name);
294     XMLString::transcode("BodyLevel23", tempStr2, 3999);
295     XMLString::catString(tempStr, tempStr2);
296     DOMElement* docBodyLevel23 = doc->createElement(tempStr);
297 
298     //name + "BodyLevel24"
299     XMLString::copyString(tempStr, name);
300     XMLString::transcode("BodyLevel24", tempStr2, 3999);
301     XMLString::catString(tempStr, tempStr2);
302     DOMElement* docBodyLevel24 = doc->createElement(tempStr);
303 
304     docBody->appendChild(docBodyLevel21);
305     docBody->appendChild(docBodyLevel22);
306     docBody->appendChild(docBodyLevel23);
307     docBody->appendChild(docBodyLevel24);
308 
309     //name + "BodyLevel31"
310     XMLString::copyString(tempStr, name);
311     XMLString::transcode("BodyLevel31", tempStr2, 3999);
312     XMLString::catString(tempStr, tempStr2);
313     DOMElement* docBodyLevel31 = doc->createElement(tempStr);
314 
315     //name + "BodyLevel32"
316     XMLString::copyString(tempStr, name);
317     XMLString::transcode("BodyLevel32", tempStr2, 3999);
318     XMLString::catString(tempStr, tempStr2);
319     DOMElement* docBodyLevel32 = doc->createElement(tempStr);
320 
321     //name + "BodyLevel33"
322     XMLString::copyString(tempStr, name);
323     XMLString::transcode("BodyLevel33", tempStr2, 3999);
324     XMLString::catString(tempStr, tempStr2);
325     DOMElement* docBodyLevel33 = doc->createElement(tempStr);
326 
327     //name + "BodyLevel34"
328     XMLString::copyString(tempStr, name);
329     XMLString::transcode("BodyLevel34", tempStr2, 3999);
330     XMLString::catString(tempStr, tempStr2);
331     DOMElement* docBodyLevel34 = doc->createElement(tempStr);
332 
333     docBodyLevel21->appendChild(docBodyLevel31);
334     docBodyLevel21->appendChild(docBodyLevel32);
335     docBodyLevel22->appendChild(docBodyLevel33);
336     docBodyLevel22->appendChild(docBodyLevel34);
337 
338     //name + "BodyLevel31'sChildTextNode11"
339     XMLString::copyString(tempStr, name);
340     XMLString::transcode("BodyLevel31'sChildTextNode11", tempStr2, 3999);
341     XMLString::catString(tempStr, tempStr2);
342     DOMText* docTextNode11 = doc->createTextNode(tempStr);
343 
344     //name + "BodyLevel31'sChildTextNode12"
345     XMLString::copyString(tempStr, name);
346     XMLString::transcode("BodyLevel31'sChildTextNode12", tempStr2, 3999);
347     XMLString::catString(tempStr, tempStr2);
348     DOMText* docTextNode12 = doc->createTextNode(tempStr);
349 
350     //name + "BodyLevel31'sChildTextNode13"
351     XMLString::copyString(tempStr, name);
352     XMLString::transcode("BodyLevel31'sChildTextNode13", tempStr2, 3999);
353     XMLString::catString(tempStr, tempStr2);
354     DOMText* docTextNode13 = doc->createTextNode(tempStr);
355 
356     //name + "TextNode2"
357     XMLString::copyString(tempStr, name);
358     XMLString::transcode("TextNode2", tempStr2, 3999);
359     XMLString::catString(tempStr, tempStr2);
360     DOMText* docTextNode2 = doc->createTextNode(tempStr);
361 
362     //name + "TextNode3"
363     XMLString::copyString(tempStr, name);
364     XMLString::transcode("TextNode3", tempStr2, 3999);
365     XMLString::catString(tempStr, tempStr2);
366     DOMText* docTextNode3 = doc->createTextNode(tempStr);
367 
368     //name + "TextNode4"
369     XMLString::copyString(tempStr, name);
370     XMLString::transcode("TextNode4", tempStr2, 3999);
371     XMLString::catString(tempStr, tempStr2);
372     DOMText* docTextNode4 = doc->createTextNode(tempStr);
373 
374     docBodyLevel31->appendChild(docTextNode11);
375     docBodyLevel31->appendChild(docTextNode12);
376     docBodyLevel31->appendChild(docTextNode13);
377     docBodyLevel32->appendChild(docTextNode2);
378     docBodyLevel33->appendChild(docTextNode3);
379     docBodyLevel34->appendChild(docTextNode4);
380 
381     //"<![CDATA[<greeting>Hello, world!</greeting>]]>"
382     XMLString::transcode("<![CDATA[<greeting>Hello, world!</greeting>]]>", tempStr, 3999);
383     DOMCDATASection* docCDATASection = doc->createCDATASection(tempStr);
384     docBodyLevel23->appendChild(docCDATASection);
385 
386     //"This should be a comment of some kind "
387     XMLString::transcode("This should be a comment of some kind ", tempStr, 3999);
388     DOMComment* docComment = doc->createComment(tempStr);
389 
390     //Test compareDocumentPosition before append
391     COMPARETREEPOSITIONTEST(docFirstElementAttr, docComment, DOMNode::DOCUMENT_POSITION_DISCONNECTED, __LINE__);
392 
393     docBodyLevel23->appendChild(docComment);
394 
395     //"ourEntityNode"
396     XMLString::transcode("ourEntityNode", tempStr, 3999);
397     DOMEntityReference* docReferenceEntity = doc->createEntityReference(tempStr);
398     docBodyLevel24->appendChild(docReferenceEntity);
399 
400     DOMTest make;
401 
402     //"ourNotationNode"
403     XMLString::transcode("ourNotationNode", tempStr, 3999);
404     DOMNotation* docNotation = make.createNotation(doc, tempStr);
405     DOMNode*  abc1 = doc->getFirstChild();
406     DOMDocumentType* docType = (DOMDocumentType*) abc1;
407     DOMNode* rem = docType->getNotations()->setNamedItem(docNotation);
408     if (rem)
409         rem->release();
410 
411 
412 //***********Do some quick compareDocumentPosition tests
413 //The tree now looks like
414 //
415 // docFirstElement (has docFirstElementAttr)
416 //      |
417 //      |_ docProcessInstruction
418 //      |
419 //      |_ docBody
420 //            |
421 //            |_ docBodyLevel21
422 //            |         |
423 //            |         |_ docBodyLevel31
424 //            |         |        |
425 //            |         |        |_ docTextNode11
426 //            |         |        |
427 //            |         |        |_ docTextNode12
428 //            |         |        |
429 //            |         |        |_ docTextNode13
430 //            |         |
431 //            |         |_ docBodyLevel32
432 //            |                  |
433 //            |                  |_ docTextNode2
434 //            |
435 //            |_ docBodyLevel22
436 //            |         |
437 //            |         |_ docBodyLevel33
438 //            |         |        |
439 //            |         |        |_ docTextNode3
440 //            |         |
441 //            |         |
442 //            |         |_ docBodyLevel34
443 //            |                  |
444 //            |                  |_ docTextNode4
445 //            |
446 //            |_ docBodyLevel23
447 //            |         |
448 //            |         |_ docCDATASection
449 //            |         |
450 //            |         |_ docComment
451 //            |
452 //            |_ docBodyLevel24
453 //                      |
454 //                      |_ docReferenceEntity
455 //
456 
457     COMPARETREEPOSITIONTEST(docProcessingInstruction, docBody, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
458     COMPARETREEPOSITIONTEST(docBodyLevel24, docProcessingInstruction, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
459     COMPARETREEPOSITIONTEST(docBodyLevel23, docBodyLevel21, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
460     COMPARETREEPOSITIONTEST(docBodyLevel21, docTextNode11, DOMNode::DOCUMENT_POSITION_CONTAINED_BY, __LINE__);
461     COMPARETREEPOSITIONTEST(docCDATASection, docFirstElement, DOMNode::DOCUMENT_POSITION_CONTAINS, __LINE__);
462     COMPARETREEPOSITIONTEST(docReferenceEntity, docFirstElement, DOMNode::DOCUMENT_POSITION_CONTAINS, __LINE__);
463     COMPARETREEPOSITIONTEST(docFirstElementAttr, docFirstElement, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
464     COMPARETREEPOSITIONTEST(docFirstElementAttr, docProcessingInstruction, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
465     COMPARETREEPOSITIONTEST(docProcessingInstruction, docFirstElementAttr, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
466     COMPARETREEPOSITIONTEST(docFirstElementAttr, doc, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
467     COMPARETREEPOSITIONTEST(doc, docFirstElementAttr, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
468     COMPARETREEPOSITIONTEST(docBodyLevel21, docBodyLevel22, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
469 
470     COMPARETREEPOSITIONTEST(docNotation, docFirstElement, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
471 
472     myposition = docFirstElement->compareDocumentPosition(docFirstElement);
473     if (myposition != 0) {
474         fprintf(stderr, "DOMNode::compareDocumentPosition does not work in line %i\n", __LINE__);
475         OK = false;
476     }
477 
478     //now do some lookupNamespaceURI and lookupNamespacePrefix
479     //first lets add some attributes
480     XMLString::transcode("http://www.w3.org/2000/xmlns/", tempStr, 3999);
481     XMLString::transcode("xmlns:pre1", tempStr2, 3999);
482     XMLString::transcode("pre1URI", tempStr3, 3999);
483     XMLString::transcode("pre1", tempStr4, 3999);
484 
485     DOMAttr *attr1 = doc->createAttributeNS(tempStr, tempStr2);
486     attr1->setValue(tempStr3);
487     docFirstElement->setAttributeNodeNS(attr1);
488 
489 
490     LOOKUPNSTEST(docProcessingInstruction, tempStr4, tempStr3, true, __LINE__);
491     LOOKUPNSTEST(docBodyLevel24, tempStr4, tempStr3, true, __LINE__);
492     LOOKUPNSTEST(docBodyLevel23, tempStr4, tempStr3, true, __LINE__);
493     LOOKUPNSTEST(docBodyLevel21, tempStr4, tempStr3, true, __LINE__);
494     LOOKUPNSTEST(docBodyLevel31, tempStr4, tempStr3, true, __LINE__);
495     LOOKUPNSTEST(docBodyLevel32, tempStr4, tempStr3, true, __LINE__);
496     LOOKUPNSTEST(docCDATASection, tempStr4, tempStr3, true, __LINE__);
497     LOOKUPNSTEST(docFirstElement, tempStr4, tempStr3, true, __LINE__);
498     LOOKUPNSTEST(docReferenceEntity, tempStr4, tempStr3, true, __LINE__);
499     LOOKUPNSTEST(docFirstElementAttr, tempStr4, tempStr3, true, __LINE__);
500     LOOKUPNSTEST(doc, tempStr4, tempStr3, true, __LINE__);
501     LOOKUPNSTEST(docNotation, tempStr4, tempStr3, false, __LINE__);
502     LOOKUPNSTEST(docTextNode2, tempStr4, tempStr3, true, __LINE__);
503     LOOKUPNSTEST(docTextNode4, tempStr4, tempStr3, true, __LINE__);
504     LOOKUPNSTEST(docComment, tempStr4, tempStr3, true, __LINE__);
505 
506     XMLString::transcode("xmlns:pre2", tempStr2, 3999);
507     XMLString::transcode("pre2URI", tempStr3, 3999);
508     XMLString::transcode("pre2", tempStr4, 3999);
509 
510     DOMAttr *attr2 = doc->createAttributeNS(tempStr, tempStr2);
511     attr2->setValue(tempStr3);
512     docBodyLevel21->setAttributeNodeNS(attr2);
513 
514 
515     LOOKUPNSTEST(docProcessingInstruction, tempStr4, tempStr3, false, __LINE__);
516     LOOKUPNSTEST(docBodyLevel24, tempStr4, tempStr3, false, __LINE__);
517     LOOKUPNSTEST(docBodyLevel23, tempStr4, tempStr3, false, __LINE__);
518     LOOKUPNSTEST(docBodyLevel21, tempStr4, tempStr3, true, __LINE__);
519     LOOKUPNSTEST(docBodyLevel31, tempStr4, tempStr3, true, __LINE__);
520     LOOKUPNSTEST(docBodyLevel32, tempStr4, tempStr3, true, __LINE__);
521     LOOKUPNSTEST(docCDATASection, tempStr4, tempStr3, false, __LINE__);
522     LOOKUPNSTEST(docFirstElement, tempStr4, tempStr3, false, __LINE__);
523     LOOKUPNSTEST(docReferenceEntity, tempStr4, tempStr3, false, __LINE__);
524     LOOKUPNSTEST(docFirstElementAttr, tempStr4, tempStr3, false, __LINE__);
525     LOOKUPNSTEST(doc, tempStr4, tempStr3, false, __LINE__);
526     LOOKUPNSTEST(docNotation, tempStr4, tempStr3, false, __LINE__);
527     LOOKUPNSTEST(docComment, tempStr4, tempStr3, false, __LINE__);
528     LOOKUPNSTEST(docTextNode2, tempStr4, tempStr3, true, __LINE__);
529     LOOKUPNSTEST(docTextNode4, tempStr4, tempStr3, false, __LINE__);
530 
531 
532     XMLString::transcode("xmlns", tempStr2, 3999);
533     XMLString::transcode("default", tempStr3, 3999);
534     XMLString::transcode("", tempStr4, 3999);
535 
536 
537     DOMAttr *attr3 = doc->createAttributeNS(tempStr, tempStr2);
538     attr3->setValue(tempStr3);
539     docFirstElement->setAttributeNodeNS(attr3);
540 
541     LOOKUPNSTEST(docProcessingInstruction, 0, tempStr3, true, __LINE__);
542     LOOKUPNSTEST(docBodyLevel24, 0, tempStr3, true, __LINE__);
543     LOOKUPNSTEST(docBodyLevel23, 0, tempStr3, true, __LINE__);
544     LOOKUPNSTEST(docBodyLevel21, 0, tempStr3, true, __LINE__);
545     LOOKUPNSTEST(docBodyLevel31, 0, tempStr3, true, __LINE__);
546     LOOKUPNSTEST(docBodyLevel32, 0, tempStr3, true, __LINE__);
547     LOOKUPNSTEST(docCDATASection, 0, tempStr3, true, __LINE__);
548     LOOKUPNSTEST(docFirstElement, 0, tempStr3, true, __LINE__);
549     LOOKUPNSTEST(docReferenceEntity, 0, tempStr3, true, __LINE__);
550     LOOKUPNSTEST(docFirstElementAttr, 0, tempStr3, true, __LINE__);
551     LOOKUPNSTEST(doc, 0, tempStr3, true, __LINE__);
552     LOOKUPNSTEST(docComment, 0, tempStr3, true, __LINE__);
553     LOOKUPNSTEST(docTextNode2, 0, tempStr3, true, __LINE__);
554     LOOKUPNSTEST(docTextNode4, 0, tempStr3, true, __LINE__);
555 
556     //this has to be done separately because negative lookup is the same as default ns lookup!!!
557 
558     prefixResult = XMLString::compareString(docNotation->lookupPrefix(tempStr3), 0);
559 
560     uriResult = XMLString::compareString(docNotation->lookupNamespaceURI(0), 0);
561     if(prefixResult != 0) {
562         fprintf(stderr, "DOMNode::lookupNamespacePrefix does not work in line %i\n", __LINE__);
563         OK = false;
564     }
565     if(uriResult != 0) {
566         fprintf(stderr, "DOMNode::lookupNamespacePrefix does not work in line %i\n", __LINE__);
567         OK = false;
568     }
569 
570     XMLString::transcode("notset", tempStr3, 3999);
571 
572     LOOKUPDEFAULTNSTEST(docProcessingInstruction, tempStr3, false, __LINE__);
573     LOOKUPDEFAULTNSTEST(docBodyLevel24, tempStr3, false, __LINE__);
574     LOOKUPDEFAULTNSTEST(docBodyLevel23, tempStr3, false, __LINE__);
575     LOOKUPDEFAULTNSTEST(docBodyLevel21, tempStr3, false, __LINE__);
576     LOOKUPDEFAULTNSTEST(docBodyLevel31, tempStr3, false, __LINE__);
577     LOOKUPDEFAULTNSTEST(docBodyLevel32, tempStr3, false, __LINE__);
578     LOOKUPDEFAULTNSTEST(docCDATASection, tempStr3, false, __LINE__);
579     LOOKUPDEFAULTNSTEST(docFirstElement, tempStr3, false, __LINE__);
580     LOOKUPDEFAULTNSTEST(docReferenceEntity, tempStr3, false, __LINE__);
581     LOOKUPDEFAULTNSTEST(docFirstElementAttr, tempStr3, false, __LINE__);
582     LOOKUPDEFAULTNSTEST(doc, tempStr3, false, __LINE__);
583     LOOKUPDEFAULTNSTEST(docNotation, tempStr3, false, __LINE__);
584     LOOKUPDEFAULTNSTEST(docComment, tempStr3, false, __LINE__);
585     LOOKUPDEFAULTNSTEST(docTextNode2, tempStr3, false, __LINE__);
586     LOOKUPDEFAULTNSTEST(docTextNode4, tempStr3, false, __LINE__);
587 
588 
589     XMLString::transcode("default", tempStr3, 3999);
590 
591     LOOKUPDEFAULTNSTEST(docProcessingInstruction, tempStr3, false, __LINE__);
592     LOOKUPDEFAULTNSTEST(docBodyLevel24, tempStr3, false, __LINE__);
593     LOOKUPDEFAULTNSTEST(docBodyLevel23, tempStr3, false, __LINE__);
594     LOOKUPDEFAULTNSTEST(docBodyLevel21, tempStr3, false, __LINE__);
595     LOOKUPDEFAULTNSTEST(docBodyLevel31, tempStr3, false, __LINE__);
596     LOOKUPDEFAULTNSTEST(docBodyLevel32, tempStr3, false, __LINE__);
597     LOOKUPDEFAULTNSTEST(docCDATASection, tempStr3, false, __LINE__);
598     LOOKUPDEFAULTNSTEST(docFirstElement, tempStr3, false, __LINE__);
599     LOOKUPDEFAULTNSTEST(docReferenceEntity, tempStr3, false, __LINE__);
600     LOOKUPDEFAULTNSTEST(docFirstElementAttr, tempStr3, false, __LINE__);
601     LOOKUPDEFAULTNSTEST(doc, tempStr3, false, __LINE__);
602     LOOKUPDEFAULTNSTEST(docNotation, tempStr3, false, __LINE__);
603     LOOKUPDEFAULTNSTEST(docComment, tempStr3, false, __LINE__);
604     LOOKUPDEFAULTNSTEST(docTextNode2, tempStr3, false, __LINE__);
605     LOOKUPDEFAULTNSTEST(docTextNode4, tempStr3, false, __LINE__);
606 
607     //remove the xmlns attr
608     docFirstElement->removeAttributeNode(attr3);
609 
610     LOOKUPDEFAULTNSTEST(docProcessingInstruction, 0, true, __LINE__);
611     LOOKUPDEFAULTNSTEST(docBodyLevel24, 0, true, __LINE__);
612     LOOKUPDEFAULTNSTEST(docBodyLevel23, 0, true, __LINE__);
613     LOOKUPDEFAULTNSTEST(docBodyLevel21, 0, true, __LINE__);
614     LOOKUPDEFAULTNSTEST(docBodyLevel31, 0, true, __LINE__);
615     LOOKUPDEFAULTNSTEST(docBodyLevel32, 0, true, __LINE__);
616     LOOKUPDEFAULTNSTEST(docCDATASection, 0, true, __LINE__);
617     LOOKUPDEFAULTNSTEST(docFirstElement, 0, true, __LINE__);
618     LOOKUPDEFAULTNSTEST(docReferenceEntity, 0, true, __LINE__);
619     LOOKUPDEFAULTNSTEST(docFirstElementAttr, 0, true, __LINE__);
620     LOOKUPDEFAULTNSTEST(doc, 0, true, __LINE__);
621     LOOKUPDEFAULTNSTEST(docNotation, 0, false, __LINE__);
622     LOOKUPDEFAULTNSTEST(docComment, 0, true, __LINE__);
623     LOOKUPDEFAULTNSTEST(docTextNode2, 0, true, __LINE__);
624     LOOKUPDEFAULTNSTEST(docTextNode4, 0, true, __LINE__);
625 
626     XMLString::transcode("someSpecialURI", tempStr3, 3999);
627     XMLString::transcode("newNameForEle", tempStr4, 3999);
628     DOMElement *ele = doc->createElementNS(tempStr3, tempStr4);
629     docFirstElement->insertBefore(ele, docFirstElement->getFirstChild());
630 
631     // test for bug# 26919
632     docFirstElement->insertBefore(docFirstElement->getFirstChild(), docFirstElement->getFirstChild());
633 
634     //a test for lookup when xmlns is not set so we take the fact that there is no prefix to be confimation
635     LOOKUPDEFAULTNSTEST(ele, tempStr3, true, __LINE__);
636 
637     docFirstElement->removeAttributeNode(attr1);
638     docBodyLevel21->removeAttributeNode(attr2);
639     docFirstElement->removeChild(ele);
640 
641 //***********Following are for errorTests
642     DOMDocumentFragment* docDocFragment = doc->createDocumentFragment();
643     UNUSED(docDocFragment); // silence warning
644 
645     //name + "docTextNode3"
646     XMLString::copyString(tempStr, name);
647     XMLString::transcode("docTextNode3", tempStr2, 3999);
648     XMLString::catString(tempStr, tempStr2);
649     DOMText* docNode3 = doc->createTextNode(tempStr);
650 
651     //name + "docTextNode4"
652     XMLString::copyString(tempStr, name);
653     XMLString::transcode("docTextNode4", tempStr2, 3999);
654     XMLString::catString(tempStr, tempStr2);
655     DOMText* docNode4 = doc->createTextNode(tempStr);
656 
657     //"ourEntityNode"
658     XMLString::transcode("ourEntityNode", tempStr, 3999);
659     DOMNode*   abc2 =  doc->getDoctype()->getEntities()->getNamedItem(tempStr);  // Get the DOMEntity* node
660     DOMEntity* docEntity = (DOMEntity*) abc2;
661     UNUSED(docEntity); // silence warning
662     DOMNode*   abc3 = doc->getFirstChild(); // Get the DOMDocumentType* node
663     DOMDocumentType* docDocType = (DOMDocumentType*) abc3;
664     UNUSED(docDocType); // silence warning
665     DOMNode*   abc4 = doc->getLastChild()->getLastChild()->getLastChild()->getFirstChild();
666     DOMEntityReference* entityReferenceText = (DOMEntityReference*) abc4;
667     UNUSED(entityReferenceText); // silence warning
668 
669     //"entityReferenceText information"
670     XMLString::transcode("entityReferenceText information", tempStr, 3999);
671     DOMText* entityReferenceText2 = doc->createTextNode(tempStr);
672 //************************************************ ERROR TESTS
673     DOMTest tests;
674 
675     EXCEPTIONSTEST(document->appendChild(docBody), DOMException::HIERARCHY_REQUEST_ERR, OK,  1);
676 
677     EXCEPTIONSTEST(document->appendChild(docBody), DOMException::HIERARCHY_REQUEST_ERR, OK, 2);
678     EXCEPTIONSTEST(docNode3->appendChild(docNode4), DOMException::HIERARCHY_REQUEST_ERR, OK, 3);
679     // EXCEPTIONSTEST(doc->insertBefore(docEntity, docFirstElement), DOMException::HIERARCHY_REQUEST_ERR, OK, 4);
680     EXCEPTIONSTEST(doc->replaceChild(docCDATASection, docFirstElement), DOMException::HIERARCHY_REQUEST_ERR, OK, 5);
681 
682     //"This shouldn't work!"
683     XMLString::transcode("entityReferenceText information", tempStr, 3999);
684 
685     // The following setNodeValue tests are not invalid
686     // According to DOM spec, if the node value is defined to be null in the DOM spec, setting it has no effect.
687     // Only those node type that are supposed to have node value, exception will be raised if the node is readonly.
688     // EXCEPTIONSTEST(docFirstElement->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 6);
689     // EXCEPTIONSTEST(docReferenceEntity->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 7);
690     // EXCEPTIONSTEST(docEntity->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 8);
691     // EXCEPTIONSTEST(doc->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 9);
692     // EXCEPTIONSTEST(docDocType->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 10);
693     // EXCEPTIONSTEST(docDocFragment->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 11);
694     // EXCEPTIONSTEST(docNotation->setNodeValue(tempStr), DOMException::NO_MODIFICATION_ALLOWED_ERR, OK, 12);
695     EXCEPTIONSTEST(docReferenceEntity->appendChild(entityReferenceText2 ), DOMException::NO_MODIFICATION_ALLOWED_ERR , OK, 13);
696     EXCEPTIONSTEST(docBodyLevel32->insertBefore(docTextNode11,docBody ), DOMException::NOT_FOUND_ERR, OK, 14);
697     EXCEPTIONSTEST(docBodyLevel32->removeChild(docFirstElement), DOMException::NOT_FOUND_ERR, OK, 15);
698     EXCEPTIONSTEST(docBodyLevel32->replaceChild(docTextNode11,docFirstElement ), DOMException::NOT_FOUND_ERR, OK, 16);
699 
700     XMLString::release(&name);
701 
702     return OK;
703 
704 
705 //!! Throws a NOT_FOUND_ERR ********
706 
707      // docBodyLevel32->getAttributes()->removeNamedItem(testAttribute->getName());    16  // To test removeNamedItem
708 
709 }  //END OF DOCBUILDER
710 
711 
712 
713 /**
714  * @param document org.w3c.dom.DOMDocument
715  */
findTestNodes(DOMDocument * document)716 void DOMTest::findTestNodes(DOMDocument* document) {
717     DOMNode* node = document;
718     int nodeCount = 0;
719 
720     // Walk the tree until you find and assign all node types needed that exist.
721     while (node != 0 && nodeCount < 12)
722     {
723 
724         switch (node->getNodeType())
725     {
726         case DOMNode::ELEMENT_NODE :
727             if (testElementNode == 0) {testElementNode = (DOMElement*)node; nodeCount++;}
728             break;
729         case DOMNode::ATTRIBUTE_NODE :
730             if (testAttributeNode == 0) {testAttributeNode = (DOMAttr*)node; nodeCount++;}
731             break;
732         case DOMNode::TEXT_NODE :
733             if (testTextNode == 0) {testTextNode = (DOMText*)node; nodeCount++;}
734             break;
735         case DOMNode::CDATA_SECTION_NODE :
736             if (testCDATASectionNode == 0) {testCDATASectionNode = (DOMCDATASection*)node; nodeCount++;}
737             break;
738         case DOMNode::ENTITY_REFERENCE_NODE :
739             if (testEntityReferenceNode == 0) {testEntityReferenceNode = (DOMEntityReference*)node; nodeCount++;}
740             break;
741         case DOMNode::ENTITY_NODE :
742             if (testEntityNode == 0) {testEntityNode = (DOMEntity*)node; nodeCount++;}
743             break;
744         case DOMNode::PROCESSING_INSTRUCTION_NODE :
745             if (testProcessingInstructionNode == 0) {testProcessingInstructionNode = (DOMProcessingInstruction*)node; nodeCount++;}
746             break;
747         case DOMNode::COMMENT_NODE :
748             if (testCommentNode == 0) {testCommentNode = (DOMComment*)node; nodeCount++;}
749             break;
750         case DOMNode::DOCUMENT_TYPE_NODE :
751             if (testDocumentTypeNode == 0) {testDocumentTypeNode = (DOMDocumentType*)node; nodeCount++;}
752             break;
753         case DOMNode::DOCUMENT_FRAGMENT_NODE :
754             if (testDocumentFragmentNode == 0) {testDocumentFragmentNode = (DOMDocumentFragment*)node; nodeCount++;}
755             break;
756         case DOMNode::NOTATION_NODE :
757             if (testNotationNode == 0) {testNotationNode = (DOMNotation*)node; nodeCount++;}
758             break;
759         case DOMNode::DOCUMENT_NODE :
760             if (testDocumentNode == 0) {testDocumentNode = (DOMDocument*)node; nodeCount++;}
761             break;
762         default:
763             ;
764     }// End of switch
765 
766     }   // End of while
767 }
768 
769 
770 /**
771  * @param document org.w3c.dom.DOMDocument
772  */
findTestNodes(DOMNode * node)773 void DOMTest::findTestNodes(DOMNode* node) {
774     DOMTest test;
775     DOMNode*  kid;
776     // Walk the tree until you find and assign all node types needed that exist.
777 
778 
779     if (node->getFirstChild() != 0)
780     {
781         kid = node->getFirstChild();
782         test.findTestNodes(kid);
783     }
784 
785 
786     if (node->getNextSibling() != 0)
787     {
788         kid = node->getNextSibling();
789         test.findTestNodes(kid);
790     }
791 
792 
793     switch (node->getNodeType())
794     {
795         case DOMNode::ELEMENT_NODE :
796             if (testElementNode == 0) {testElementNode = (DOMElement*)node; }
797             break;
798         case DOMNode::ATTRIBUTE_NODE :
799             if (testAttributeNode == 0) {testAttributeNode = (DOMAttr*)node; }
800             break;
801         case DOMNode::TEXT_NODE :
802             if (testTextNode == 0) {testTextNode = (DOMText*)node; }
803             break;
804         case DOMNode::CDATA_SECTION_NODE :
805             if (testCDATASectionNode == 0) {testCDATASectionNode = (DOMCDATASection*)node; }
806             break;
807         case DOMNode::ENTITY_REFERENCE_NODE :
808             if (testEntityReferenceNode == 0) {testEntityReferenceNode = (DOMEntityReference*)node;}
809             break;
810         case DOMNode::ENTITY_NODE :
811             if (testEntityNode == 0) {testEntityNode = (DOMEntity*)node;}
812             break;
813         case DOMNode::PROCESSING_INSTRUCTION_NODE :
814             if (testProcessingInstructionNode == 0) {testProcessingInstructionNode = (DOMProcessingInstruction*)node;}
815             break;
816         case DOMNode::COMMENT_NODE :
817             if (testCommentNode == 0) {testCommentNode = (DOMComment*)node;}
818             break;
819         case DOMNode::DOCUMENT_TYPE_NODE :
820             if (testDocumentTypeNode == 0) {testDocumentTypeNode = (DOMDocumentType*)node; }
821             break;
822         case DOMNode::DOCUMENT_FRAGMENT_NODE :
823             if (testDocumentFragmentNode == 0) {testDocumentFragmentNode = (DOMDocumentFragment*)node;}
824             break;
825         case DOMNode::NOTATION_NODE :
826             if (testNotationNode == 0) {testNotationNode = (DOMNotation*)node;}
827             break;
828         case DOMNode::DOCUMENT_NODE :
829             if (testDocumentNode == 0) {testDocumentNode = (DOMDocument*)node;}
830             break;
831         default:
832             ;
833     }// End of switch
834 }//End of class
835 
836 /**
837  *
838  *
839  */
main(int,char **)840 int main(int /*argc*/, char ** /*argv*/)
841  {
842      bool OK = true;
843 
844      {
845          //  Nest entire test in an inner block.
846          //     Reference counting should recover all document
847          //     storage when this block exits.
848 
849          DOMTest test;
850          try {
851              XMLPlatformUtils::Initialize();
852          }
853          catch (const XMLException& toCatch) {
854              char *pMessage = XMLString::transcode(toCatch.getMessage());
855              fprintf(stderr, "Error during initialization! \n  %s \n", pMessage);
856              XMLString::release(&pMessage);
857              return -1;
858          }
859 
860          // for (int i=0; i< 1000; i++)
861          // {
862          // AH Revisit  //  startTime = System.currentTimeMillis();
863          //     if(!OK)
864          //     break;
865 
866          DOMDocument* d = test.createDocument();
867 
868          XMLString::transcode("testDocument1", tempStr, 3999);
869          DOMDocumentType* docDocType = test.createDocumentType(d,tempStr);
870          d->appendChild(docDocType);
871 
872          XMLString::transcode("ourEntityNode", tempStr, 3999);
873          DOMEntity* docEntity = test.createEntity( d, tempStr);
874          UNUSED(docEntity); // silence warning
875          //Build a branch for entityReference tests
876          // DOMText* entityChildText = d.createTextNode("entityChildText information"); //
877          // docEntity->appendChild(entityChildText);
878          // docDocType->getEntities()->setNamedItem(docEntity);
879 
880          XMLString::transcode("d", tempStr3, 3999);
881          OK &= test.docBuilder(d, tempStr3);
882 
883          test.findTestNodes((DOMNode*)d);
884 
885          OK &= test.testAttr(d);
886          OK &= test.testCDATASection(d);
887          OK &= test.testCharacterData(d);
888          OK &= test.testChildNodeList(d);
889          OK &= test.testComment(d);
890          OK &= test.testDeepNodeList(d);
891          OK &= test.testDocument(d);
892          OK &= test.testDocumentFragment(d);
893          OK &= test.testDocumentType(d);
894          OK &= test.testDOMImplementation(d);
895          OK &= test.testElement(d);
896 //         OK &= test.testEntity(d);  // Can not test entities;  only parser can create them.
897          OK &= test.testEntityReference(d);
898          OK &= test.testNode(d);
899          OK &= test.testNotation(d);
900          OK &= test.testPI(d);
901          OK &= test.testText(d);
902          OK &= test.testDOMerrors(d);
903          OK &= test.testXPath(d);
904          OK &= test.testRegex();
905 
906          // Null out the static object references in class DOMTest,
907          // which will recover their storage.
908          DOMTest::testElementNode = 0;
909          DOMTest::testAttributeNode = 0;
910          DOMTest::testTextNode = 0;
911          DOMTest::testCDATASectionNode = 0;
912          DOMTest::testEntityReferenceNode = 0;
913          DOMTest::testEntityNode = 0;
914          DOMTest::testProcessingInstructionNode = 0;
915          DOMTest::testCommentNode = 0;
916          DOMTest::testDocumentNode = 0;
917          DOMTest::testDocumentTypeNode = 0;
918          DOMTest::testDocumentFragmentNode = 0;
919          DOMTest::testNotationNode = 0;
920 
921         // test release of attributes
922         DOMElement* elt = d->createElement(tempStr3);
923         elt->setAttribute(tempStr3, tempStr3);
924         elt->release();
925 
926         elt = d->createElement(tempStr3);
927         DOMAttr *attr = d->createAttribute(tempStr3);
928         attr->setValue(tempStr3);
929         elt->setAttributeNode(attr);
930         elt->setIdAttributeNode(attr, true);
931 
932         DOMElement *eleTest = d->getElementById(tempStr3);
933         if(eleTest==NULL)
934         {
935             fprintf(stderr, "getElementById test failed at line %i\n", __LINE__);
936             OK = false;
937         }
938 
939         //all good until here
940         elt->release();
941         eleTest = d->getElementById(tempStr3);
942         if(eleTest!=NULL)
943         {
944             fprintf(stderr, "getElementById test failed at line %i\n", __LINE__);
945             OK = false;
946         }
947 
948         // we couldn't really test the user data handler call as the userhandler is already
949         // deleted when the release() is done, but still set it to test the code internally
950         d->setUserData(tempStr, (void*) tempStr, &userhandler);
951         d->release();
952 
953         // Test baseURI. BaseURI is set on nodes at parse time so we
954         // cannot use the docBuilder document above
955 
956         //Setup parser
957 
958         XercesDOMParser *parser = new XercesDOMParser;
959         parser->setValidationScheme(XercesDOMParser::Val_Never);
960         parser->setDoNamespaces(true);
961         parser->setDoSchema(true);
962         parser->setCreateEntityReferenceNodes(true);
963 
964         OK &= test.testBaseURI(parser);
965 
966         parser->setCreateEntityReferenceNodes(false);
967         OK &= test.testBaseURI(parser);
968 
969         parser->setDoNamespaces(false);
970         parser->setDoSchema(false);
971         OK &= test.testBaseURI(parser);
972 
973         parser->setCreateEntityReferenceNodes(true);
974         OK &= test.testBaseURI(parser);
975 
976 		OK &= test.testWholeText(parser);
977         OK &= test.testScanner(parser);
978         delete parser;
979 
980         OK &= test.testLSExceptions();
981 
982         OK &= test.testElementTraversal();
983 
984         OK &= test.testUtilFunctions();
985     }
986 
987     XMLPlatformUtils::Terminate();
988 
989     if (!OK) {
990         printf("Test Failed\n");
991         return 4;
992     }
993 
994     printf("Test Run Successfully\n");
995 
996     return 0;
997 }
998 
999 
1000 /**
1001  * This method tests DOMAttr* methods for the XML DOM implementation
1002  * @param document org.w3c.dom.DOMDocument
1003  *
1004  */
testAttr(DOMDocument * document)1005 bool DOMTest::testAttr(DOMDocument* document)
1006 {
1007     DOMNode* node;
1008     DOMAttr* attributeNode;
1009     bool T = true;
1010     bool OK = true;
1011 // For debugging*****   printf("\n          testAttr's outputs:\n\n");
1012 
1013     XMLString::transcode("testAttribute", tempStr, 3999);
1014     DOMAttr* testAttribute = document->createAttribute(tempStr);
1015 
1016     XMLString::transcode("testAttribute's value", tempStr, 3999);
1017     testAttribute->setValue(tempStr);
1018     node = document->getDocumentElement(); // node gets first element
1019 
1020     // ((DOMElement*)node)->setAttributeNode(testAttribute);
1021     // attributeNode = ((DOMElement*)node)->getAttributeNode("testAttribute");
1022     DOMElement* el = (DOMElement*)node;
1023     DOMNode* rem = el->setAttributeNode(testAttribute);
1024     if (rem)
1025         rem->release();
1026 
1027     XMLString::transcode("testAttribute", tempStr, 3999);
1028     attributeNode = el->getAttributeNode(tempStr);
1029 
1030     //Test compareDocumentPosition, the equivalent case here
1031     XMLString::transcode("dFirstElementdFirstElement", tempStr2, 3999);
1032     DOMAttr* docFirstElementAttr = el->getAttributeNode(tempStr2);
1033 
1034     COMPARETREEPOSITIONTEST(docFirstElementAttr, attributeNode, DOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC, __LINE__);
1035 
1036     // Test the name and other data
1037     if (XMLString::compareString(tempStr, attributeNode->getName()))
1038     {
1039         fprintf(stderr, "Warning!!! DOMAttr's 'getName' method failed to work properly!\n");
1040         OK = false;
1041     }
1042 
1043     XMLString::transcode("testAttribute's value", tempStr, 3999);
1044     if (XMLString::compareString(tempStr, attributeNode->getNodeValue()))
1045     {
1046         fprintf(stderr, "Warning!!! DOMAttr's 'getNodeValue' method failed to work properly!\n");
1047         OK = false;
1048     }
1049     if (! T ==attributeNode->getSpecified())
1050     {
1051         fprintf(stderr, "Warning!!! DOMAttr's 'getSpecified' method failed to work properly!\n");
1052         OK = false;
1053     }
1054 
1055     if (XMLString::compareString(tempStr, attributeNode->getValue()))
1056     {
1057         fprintf(stderr, "Warning!!! DOMAttr's 'getValue' method failed to work properly!\n");
1058         OK = false;
1059     }
1060 
1061 
1062     XMLString::transcode("Reset Value", tempStr, 3999);
1063     attributeNode->setNodeValue(tempStr);   /// LEAK!!!!!
1064     if (XMLString::compareString(tempStr, attributeNode->getNodeValue()))
1065     {
1066         fprintf(stderr, "Warning!!! DOMAttr's 'setNodeValue' method failed to work properly!\n");
1067         OK = false;
1068     }
1069 
1070     attributeNode->setValue(XMLUni::fgZeroLenString);
1071     if (XMLString::compareString(XMLUni::fgZeroLenString, attributeNode->getValue()))
1072     {
1073         fprintf(stderr, "Warning!!! DOMAttr's 'setValue' to '0' method failed to work properly!\n");
1074         OK = false;
1075     }
1076 
1077     XMLString::transcode("Another value ", tempStr, 3999);
1078     attributeNode->setValue(tempStr);
1079     if (XMLString::compareString(tempStr, attributeNode->getValue()))
1080     {
1081         fprintf(stderr, "Warning!!! DOMAttr's 'setValue' method failed to work properly!");
1082         OK = false;
1083     }
1084 
1085     node = attributeNode->cloneNode(T);
1086 
1087     // Check nodes for equality, both their name and value or lack thereof
1088     bool cloneOK = true;
1089     if (XMLString::compareString(node->getNodeName(), attributeNode->getNodeName()))
1090         cloneOK = false;
1091     if (node->getNodeValue() == 0 &&
1092         attributeNode->getNodeValue() != 0)
1093     {
1094         cloneOK = false;
1095     }
1096 
1097     if (node->getNodeValue() != 0 && attributeNode->getNodeValue() == 0)
1098     {
1099         cloneOK = false;
1100     }
1101 
1102     if (node->getNodeValue() != 0 && attributeNode->getNodeValue() != 0)
1103     {
1104         if (XMLString::compareString(node->getNodeValue(),attributeNode->getNodeValue()))
1105             cloneOK = false;
1106     }
1107 
1108 
1109 /*
1110     if (! (node->getNodeName(), attributeNode->getNodeName()) &&         // Compares node names for equality
1111           (node->getNodeValue() != 0 && attributeNode->getNodeValue() != 0)  // Checks to make sure each node has a value node
1112         ?  node->getNodeValue(), attributeNode->getNodeValue())          // If both have value nodes test those value nodes for equality
1113         : (node->getNodeValue() == 0 && attributeNode->getNodeValue() == 0)))// If one node doesn't have a value node make sure both don't
1114 */
1115     if (cloneOK == false)
1116         {
1117             fprintf(stderr, "'cloneNode' did not clone the Attribute node correctly\n");
1118             OK = false;
1119         }
1120         // Deep clone test comparison is in testNode & testDocument
1121 
1122 //************************************************* ERROR TESTS
1123     DOMTest tests;
1124 //!! Throws HIERARCHY_REQUEST_ERR ****************
1125     //  doc->getDocumentElement()->appendChild(attributeNode);
1126 
1127 //!! Throws a NOT_FOUND_ERR ********
1128     //  attribute2 = doc->createAttribute("testAttribute2");
1129     //  doc->getDocumentElement()->removeAttributeNode(attribute2);
1130 
1131 //!! Throws an INUSE_ATTRIBUTE_ERR ******
1132     //  DOMElement* element = (DOMElement*)doc->getLastChild()->getLastChild();
1133     //  element->setAttributeNode(testAttribute );// Tests setNamedItem which generates error through justSetNamedItem.
1134 
1135 
1136     // Test the user data
1137     // Test simple set and get
1138     DOMAttr* userTest = testAttribute;
1139     DOMElement*  userFirst = el;
1140     XMLCh* userSecond = tempStr2;
1141     XMLString::transcode("first", tempStr, 3999);
1142 
1143     XMLString::transcode("document", tempStr2, 3999);
1144     userTest->setUserData(tempStr2, (void*) document, 0);
1145     void* mydocument = userTest->getUserData(tempStr2);
1146     if (document != mydocument) {
1147         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1148         OK = false;
1149     }
1150 
1151     userTest->setUserData(tempStr, (void*) userFirst, 0);
1152     void* myFirst = userTest->getUserData(tempStr);
1153     if (userFirst != myFirst) {
1154         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1155         OK = false;
1156     }
1157 
1158     // Test overwrite
1159     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
1160     void* mySecond = userTest->getUserData(tempStr);
1161     if (userSecond != mySecond) {
1162         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1163         OK = false;
1164     }
1165     if (userFirst != myFirst2) {
1166         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1167         OK = false;
1168     }
1169     if (userFirst == mySecond) {
1170         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1171         OK = false;
1172     }
1173 
1174     // Test null
1175     // test non-exist key
1176     XMLString::transcode("not-exist", tempStr3, 3999);
1177     if (userTest->getUserData(tempStr3)) {
1178         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1179         OK = false;
1180     }
1181 
1182     // use a node that does not have user data set before
1183     if (userFirst->getUserData(tempStr)) {
1184         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1185         OK = false;
1186     }
1187 
1188     // Test reset
1189     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
1190     void* myNull = userTest->getUserData(tempStr);
1191     if (userSecond != mySecond2) {
1192         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1193         OK = false;
1194     }
1195     if (myNull) {
1196         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1197         OK = false;
1198     }
1199 
1200     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
1201     void* myNull2 = userTest->getUserData(tempStr2);
1202     if (mydocument != mydocument2) {
1203         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1204         OK = false;
1205     }
1206     if (myNull2) {
1207         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1208         OK = false;
1209     }
1210 
1211     //the userTest user data table should be null now
1212     if (userTest->getUserData(tempStr)) {
1213         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1214         OK = false;
1215     }
1216     if (userTest->getUserData(tempStr2)) {
1217         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1218         OK = false;
1219     }
1220 
1221     // Test DOMUserDataHandler
1222     // test clone
1223     userTest->setUserData(tempStr2, (void*) document, &userhandler);
1224     DOMNode* mycloned = userTest->cloneNode(true);
1225     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
1226 
1227     // test import
1228     document->setUserData(tempStr2, (void*) document, &userhandler);
1229     DOMNode* myimport = document->importNode(userTest,true);
1230     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
1231 
1232     // test delete
1233     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
1234     myimport->release();
1235     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
1236 
1237     // Test isSameNode
1238     if (!userTest->isSameNode(userTest)) {
1239         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1240         OK = false;
1241     }
1242     if (userTest->isSameNode(userFirst)) {
1243         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1244         OK = false;
1245     }
1246 
1247     // Test isEqualNode
1248     if (!userTest->isEqualNode(mycloned)) {
1249         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1250         OK = false;
1251     }
1252 
1253     if (!userTest->isEqualNode(userTest)) {
1254         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1255         OK = false;
1256     }
1257     if (userTest->isEqualNode(userFirst)) {
1258         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1259         OK = false;
1260     }
1261 
1262     // Test renameNode
1263     XMLString::transcode("http://nsa", tempStr4, 3999);
1264     XMLString::transcode("aa", tempStr5, 3999);
1265     XMLString::transcode("pnsa:aa", tempStr3, 3999);
1266 
1267     // create the attribute
1268     DOMAttr* renameTestAttribute = document->createAttribute(tempStr5);
1269     DOMAttr* renameTestAttributeNS = document->createAttributeNS(tempStr4, tempStr3);
1270 
1271     // create the owner element and append the attribute node
1272     DOMElement* renameTestElement = document->createElement(tempStr5);
1273     renameTestElement->setAttributeNode(renameTestAttribute);
1274     renameTestElement->setAttributeNode(renameTestAttributeNS);
1275 
1276     // set up the userdata
1277     renameTestAttribute->setUserData(tempStr5, (void*) document, &userhandler);
1278     renameTestAttributeNS->setUserData(tempStr4, (void*) document, 0);
1279 
1280     // set up the node value
1281     renameTestAttribute->setNodeValue(tempStr5);
1282     renameTestAttributeNS->setNodeValue(tempStr4);
1283 
1284     XMLString::transcode("http://nsb", tempStr, 3999);
1285     XMLString::transcode("bb", tempStr2, 3999);
1286     XMLString::transcode("pnsb:bb", tempStr3, 3999);
1287 
1288     // start the rename tests
1289     // rename the NS Attribute
1290     DOMAttr* renameTest = (DOMAttr*) document->renameNode(renameTestAttributeNS, tempStr, tempStr3);
1291     // test the name
1292     if (XMLString::compareString(tempStr, renameTest->getNamespaceURI()))
1293     {
1294         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1295         OK = false;
1296     }
1297     if (XMLString::compareString(tempStr2, renameTest->getLocalName()))
1298     {
1299         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1300         OK = false;
1301     }
1302     if (XMLString::compareString(tempStr3, renameTest->getNodeName()))
1303     {
1304         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1305         OK = false;
1306     }
1307     // test the child / nodevalue
1308     if (XMLString::compareString(tempStr4, renameTest->getNodeValue()))
1309     {
1310         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1311         OK = false;
1312     }
1313     if (XMLString::compareString(tempStr4, renameTest->getFirstChild()->getNodeValue()))
1314     {
1315         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1316         OK = false;
1317     }
1318     // test the owner element
1319     if (!renameTestElement->getAttributeNodeNS(tempStr, tempStr2)) {
1320         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1321         OK = false;
1322     }
1323     if (renameTestElement->getAttributeNodeNS(tempStr4, tempStr5)) {
1324         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1325         OK = false;
1326     }
1327     // test user data
1328     void* renamedocument = renameTest->getUserData(tempStr4);
1329     if (document != renamedocument) {
1330         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1331         OK = false;
1332     }
1333     // test isSame and isEqual
1334     if (!renameTestAttributeNS->isEqualNode(renameTest)) {
1335         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1336         OK = false;
1337     }
1338     if (!renameTestAttributeNS->isSameNode(renameTest)) {
1339         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1340         OK = false;
1341     }
1342 
1343 
1344     // rename the Attribute (null namespace)
1345     renameTest = (DOMAttr*) document->renameNode(renameTestAttribute, 0, tempStr2);
1346     // test the name
1347     if (renameTest->getNamespaceURI())
1348     {
1349         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1350         OK = false;
1351     }
1352     if (renameTest->getLocalName())
1353     {
1354         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1355         OK = false;
1356     }
1357     if (XMLString::compareString(tempStr2, renameTest->getNodeName()))
1358     {
1359         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1360         OK = false;
1361     }
1362     // test the child / nodevalue
1363     if (XMLString::compareString(tempStr5, renameTest->getNodeValue()))
1364     {
1365         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1366         OK = false;
1367     }
1368     if (XMLString::compareString(tempStr5, renameTest->getFirstChild()->getNodeValue()))
1369     {
1370         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1371         OK = false;
1372     }
1373     // test the owner element
1374     if (!renameTestElement->getAttributeNode(tempStr2)) {
1375         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1376         OK = false;
1377     }
1378     if (renameTestElement->getAttributeNode(tempStr5)) {
1379         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1380         OK = false;
1381     }
1382     // test user data
1383     renamedocument = renameTest->getUserData(tempStr5);
1384     if (document != renamedocument) {
1385         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1386         OK = false;
1387     }
1388     // test isSame and isEqual
1389     if (!renameTestAttribute->isEqualNode(renameTest)) {
1390         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1391         OK = false;
1392     }
1393     if (!renameTestAttribute->isSameNode(renameTest)) {
1394         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1395         OK = false;
1396     }
1397 
1398 
1399     // rename the Attribute (with namespace)
1400     renameTest = (DOMAttr*) document->renameNode(renameTestAttribute, tempStr, tempStr3);
1401     // test the name
1402     if (XMLString::compareString(tempStr, renameTest->getNamespaceURI()))
1403     {
1404         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1405         OK = false;
1406     }
1407     if (XMLString::compareString(tempStr2, renameTest->getLocalName()))
1408     {
1409         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1410         OK = false;
1411     }
1412     if (XMLString::compareString(tempStr3, renameTest->getNodeName()))
1413     {
1414         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1415         OK = false;
1416     }
1417     // test the child / nodevalue
1418     if (XMLString::compareString(tempStr5, renameTest->getNodeValue()))
1419     {
1420         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1421         OK = false;
1422     }
1423     if (XMLString::compareString(tempStr5, renameTest->getFirstChild()->getNodeValue()))
1424     {
1425         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1426         OK = false;
1427     }
1428     if (renameTestAttribute->getFirstChild())
1429     {
1430         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1431         OK = false;
1432     }
1433     // test the owner element
1434     if (!renameTestElement->getAttributeNodeNS(tempStr, tempStr2)) {
1435         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1436         OK = false;
1437     }
1438     if (renameTestElement->getAttributeNodeNS(0, tempStr2)) {
1439         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1440         OK = false;
1441     }
1442     if (renameTestElement->getAttributeNode(tempStr2)) {
1443         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1444         OK = false;
1445     }
1446     // test user data
1447     renamedocument = renameTest->getUserData(tempStr5);
1448     if (document != renamedocument) {
1449         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1450         OK = false;
1451     }
1452     // test userdatahandler
1453     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_RENAMED, tempStr5, document, renameTestAttribute, renameTest, __LINE__);
1454     // test isSame and isEqual
1455     // a new node is created here, so both isSame and isEqual are not compared
1456     if (renameTestAttribute->isEqualNode(renameTest)) {
1457         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1458         OK = false;
1459     }
1460     if (renameTestAttribute->isSameNode(renameTest)) {
1461         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
1462         OK = false;
1463     }
1464 
1465 
1466 
1467     //isID tests
1468 
1469     XMLString::transcode("http://nsa", tempStr4, 3999);
1470     XMLString::transcode("aa", tempStr5, 3999);
1471 
1472     DOMAttr *idAtt = document->createAttributeNS(tempStr4, tempStr5);
1473     testElementNode->setAttributeNode(idAtt);
1474 
1475 
1476     if(idAtt->isId()) {
1477         fprintf(stderr, "isID failed in line %i\n", __LINE__);
1478         OK = false;
1479     }
1480 
1481     testElementNode->setIdAttributeNode(idAtt,true);
1482 
1483     if(!idAtt->isId()) {
1484         fprintf(stderr, "isID failed in line %i\n", __LINE__);
1485         OK = false;
1486     }
1487 
1488     testElementNode->setIdAttributeNode(idAtt,false);
1489 
1490     if(idAtt->isId()) {
1491         fprintf(stderr, "isID failed in line %i\n", __LINE__);
1492         OK = false;
1493     }
1494     //clean up
1495     testElementNode->removeAttributeNode(idAtt);
1496 
1497     if (! OK)
1498         printf("\n*****The DOMAttr* method calls listed above failed, all others worked correctly.*****\n");
1499     return OK;
1500 
1501 }
1502 
1503 
1504 
1505 
1506 /**
1507  * This method tests DOMCDATASection* methods for the XML DOM implementation
1508  * @param document org.w3c.dom.DOMDocument
1509  *
1510  */
testCDATASection(DOMDocument * document)1511 bool DOMTest::testCDATASection(DOMDocument* document)
1512 {
1513     DOMNode* node;
1514     DOMNode* node2;
1515     bool T = true;
1516     bool OK = true;
1517 // For debugging*****   printf("\n          testCDATASection's outputs:\n");
1518     XMLString::transcode("dBodyLevel23", tempStr, 3999);
1519     node = document->getDocumentElement()->getElementsByTagName(tempStr)->item(0)->getFirstChild(); // node gets DOMCDATASection* node
1520 
1521     node2 = node->cloneNode(T);//*****?
1522 
1523     // Check nodes for equality, both their name and value or lack thereof
1524     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
1525           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
1526         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
1527         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
1528     {
1529         fprintf(stderr, "'cloneNode' did not clone the DOMCDATASection* node correctly\n");
1530         OK = false;
1531     }
1532     // Deep clone test comparison is in testNode & testDocument
1533 
1534 
1535     // Test the user data
1536     // Test simple set and get
1537     DOMCDATASection* userTest = (DOMCDATASection*) node;
1538     DOMCDATASection*  userFirst = (DOMCDATASection*) node2;
1539     XMLCh* userSecond = tempStr2;
1540     XMLString::transcode("first", tempStr, 3999);
1541 
1542     XMLString::transcode("document", tempStr2, 3999);
1543     userTest->setUserData(tempStr2, (void*) document, 0);
1544     void* mydocument = userTest->getUserData(tempStr2);
1545     if (document != mydocument) {
1546         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1547         OK = false;
1548     }
1549 
1550     userTest->setUserData(tempStr, (void*) userFirst, 0);
1551     void* myFirst = userTest->getUserData(tempStr);
1552     if (userFirst != myFirst) {
1553         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1554         OK = false;
1555     }
1556 
1557     // Test overwrite
1558     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
1559     void* mySecond = userTest->getUserData(tempStr);
1560     if (userSecond != mySecond) {
1561         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1562         OK = false;
1563     }
1564     if (userFirst != myFirst2) {
1565         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1566         OK = false;
1567     }
1568     if (userFirst == mySecond) {
1569         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1570         OK = false;
1571     }
1572 
1573     // Test null
1574     // test non-exist key
1575     XMLString::transcode("not-exist", tempStr3, 3999);
1576     if (userTest->getUserData(tempStr3)) {
1577         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1578         OK = false;
1579     }
1580 
1581     // use a node that does not have user data set before
1582     if (userFirst->getUserData(tempStr)) {
1583         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1584         OK = false;
1585     }
1586 
1587     // Test reset
1588     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
1589     void* myNull = userTest->getUserData(tempStr);
1590     if (userSecond != mySecond2) {
1591         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1592         OK = false;
1593     }
1594     if (myNull) {
1595         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1596         OK = false;
1597     }
1598 
1599     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
1600     void* myNull2 = userTest->getUserData(tempStr2);
1601     if (mydocument != mydocument2) {
1602         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1603         OK = false;
1604     }
1605     if (myNull2) {
1606         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1607         OK = false;
1608     }
1609 
1610     //the userTest user data table should be null now
1611     if (userTest->getUserData(tempStr)) {
1612         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1613         OK = false;
1614     }
1615     if (userTest->getUserData(tempStr2)) {
1616         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1617         OK = false;
1618     }
1619 
1620     // Test DOMUserDataHandler
1621     // test clone
1622     userTest->setUserData(tempStr2, (void*) document, &userhandler);
1623     DOMNode* mycloned = userTest->cloneNode(true);
1624     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
1625 
1626     // test import
1627     document->setUserData(tempStr2, (void*) document, &userhandler);
1628     DOMNode* myimport = document->importNode(userTest,true);
1629     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
1630 
1631     // test delete
1632     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
1633     myimport->release();
1634     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
1635 
1636     // Test isSameNode
1637     if (!userTest->isSameNode(userTest)) {
1638         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1639         OK = false;
1640     }
1641     if (userTest->isSameNode(userFirst)) {
1642         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1643         OK = false;
1644     }
1645 
1646     // Test isEqualNode
1647     if (!userTest->isEqualNode(mycloned)) {
1648         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1649         OK = false;
1650     }
1651 
1652     if (!userTest->isEqualNode(userTest)) {
1653         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1654         OK = false;
1655     }
1656     if (userTest->isEqualNode(document)) {
1657         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1658         OK = false;
1659     }
1660 
1661     // Test rename, should throw exception
1662     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 204 );
1663 
1664     if (! OK)
1665         printf("\n*****The DOMCDATASection* method calls listed above failed, all others worked correctly.*****\n");
1666     return OK;
1667 }
1668 
1669 
1670 
1671 /**
1672  * This method tests DOMCharacterData methods for the XML DOM implementation
1673  * @param document org.w3c.dom.DOMDocument
1674  *
1675  */
testCharacterData(DOMDocument * document)1676 bool DOMTest::testCharacterData(DOMDocument* document)
1677 {
1678     DOMCharacterData* charData;
1679     XMLCh resetData[3999];
1680     bool OK = true;
1681 // For debugging*****   printf("\n          testCharacterData's outputs:\n");
1682     XMLString::transcode("dBodyLevel31", tempStr, 3999);
1683     DOMNode*  abc1 = document->getDocumentElement()->getElementsByTagName(tempStr)->item(0)->getFirstChild(); // charData gets textNode11
1684 
1685     charData = (DOMCharacterData *) abc1;
1686 
1687     XMLString::transcode("dBodyLevel31'sChildTextNode11", tempStr, 3999);
1688     if (XMLString::compareString(tempStr, charData->getData()))
1689     {
1690         fprintf(stderr, "Warning!!! DOMCharacterData's 'getData' failed to work properly!\n This may corrupt other DOMCharacterData tests!!!*****\n");
1691         OK = false;
1692     }
1693 
1694     XMLString::copyString(resetData,charData->getData());
1695     //  printf("This node's original data is: " + charData->getData());
1696 
1697     XMLString::transcode(" This is new data for this node", tempStr2, 3999);
1698 
1699     XMLString::copyString(tempStr, charData->getData());
1700     XMLString::catString(tempStr, tempStr2);
1701 
1702     charData->appendData(tempStr2);
1703 
1704 
1705     if (XMLString::compareString(tempStr, charData->getData()))
1706     {
1707         fprintf(stderr, "Warning!!! DOMCharacterData's 'appendData' failed to work properly!\n");
1708         OK = false;
1709     }
1710     //  printf("This node's appended data is: " + charData->getData());
1711 
1712     XMLString::transcode("dBodyLevel", tempStr, 3999);
1713     charData->deleteData(10, 100);
1714     if (XMLString::compareString(tempStr, charData->getData()))
1715     {
1716         fprintf(stderr, "Warning!!! DOMCharacterData's 'deleteData' failed to work properly!\n");
1717         OK = false;
1718     }
1719     //  printf("This node's partially deleted data is: " + charData->getData());
1720 
1721     XMLSize_t length = 10;
1722     if (!(length == charData->getLength()))
1723     {
1724         fprintf(stderr, "Warning!!! DOMCharacterData's 'getLength' failed to work properly!\n");
1725         OK = false;
1726     }
1727     //  printf("This node's data length is: " + charData->getLength());
1728 
1729     XMLString::transcode("dBody' This is data inserted into this node'Level", tempStr, 3999);
1730     XMLString::transcode("' This is data inserted into this node'", tempStr2, 3999);
1731     charData->insertData(5, tempStr2);
1732     if (XMLString::compareString(tempStr, charData->getData()))
1733     {
1734         fprintf(stderr, "Warning!!! DOMCharacterData's 'insertData' failed to work properly!\n");
1735         OK = false;
1736     }
1737     //  printf("This node's updated with insert data is: " + charData->getData());
1738 
1739     XMLString::transcode("dBody' This is ' replacement data'ted into this node'Level", tempStr, 3999);
1740     XMLString::transcode("' replacement data'", tempStr2, 3999);
1741     charData->replaceData(15, 10, tempStr2);
1742     if (XMLString::compareString(tempStr, charData->getData()))
1743     {
1744         fprintf(stderr, "Warning!!! DOMCharacterData's 'replaceData' failed to work properly!\n");
1745         OK = false;
1746     }
1747     //  printf("This node's updated with replacement data is: " +charData->getData());
1748 
1749     XMLString::transcode("New data A123456789B123456789C123456789D123456789E123456789", tempStr, 3999);
1750     charData->setData(tempStr);
1751     if (XMLString::compareString(tempStr, charData->getData()))
1752     {
1753         fprintf(stderr, "Warning!!! DOMCharacterData's 'setData' failed to work properly!");
1754         OK = false;
1755     }
1756     //  printf("This node's new data via setData: " + charData->getData());
1757 
1758     XMLString::transcode("123456789D123456789E123456789", tempStr, 3999);
1759     if (XMLString::compareString(tempStr, charData->substringData(30, 30)))
1760     {
1761         fprintf(stderr, "Warning!!! DOMCharacterData's 'substringData' failed to work properly!\n");
1762         OK = false;
1763     }
1764     //  printf("Using subString 30,30 you get:");  charData->substringData(30,30)).print();
1765 
1766     XMLString::transcode("New data A123456789B12345", tempStr, 3999);
1767     if (XMLString::compareString(tempStr, charData->substringData(0, 25)))
1768     {
1769         fprintf(stderr, "Warning!!! DOMCharacterData's 'substringData' failed to work properly!\n");
1770         OK = false;
1771     }
1772     //  printf("Using subString 0,25 you get: ");   charData->substringData(0,25)).print();
1773 
1774 //************************************************* ERROR TESTS
1775     DOMTest tests;   // What is this for?  'tests' is never used.
1776 
1777 //!! Throws INDEX_SIZE_ERR ********************
1778     EXCEPTIONSTEST(charData->deleteData((XMLSize_t)-1, 5), DOMException::INDEX_SIZE_ERR, OK, 101 );
1779     // Test 102 is not an error because the -1 parameter is an unsigned value, and counts
1780     //   that exceed the length of the string are allowed.
1781 //    EXCEPTIONSTEST(charData->deleteData(2, -1), DOMException::INDEX_SIZE_ERR, OK, 102 );
1782     EXCEPTIONSTEST(charData->deleteData(100, 5), DOMException::INDEX_SIZE_ERR, OK,103 );
1783 
1784 //can't set negative XMLSize_t in c++ compiler
1785 
1786   //  EXCEPTIONSTEST(charData->insertData(-1, "Stuff inserted"), DOMException::INDEX_SIZE_ERR, OK, 104 );
1787     XMLString::transcode("Stuff inserted", tempStr, 3999);
1788     EXCEPTIONSTEST(charData->insertData(100, tempStr), DOMException::INDEX_SIZE_ERR, OK, 105 );
1789 
1790   //  EXCEPTIONSTEST(charData->replaceData(-1, 5, "Replacement stuff") , DOMException::INDEX_SIZE_ERR, OK, 106 );
1791     XMLString::transcode("Replacement stuff", tempStr, 3999);
1792     EXCEPTIONSTEST(charData->replaceData(100, 5 ,tempStr), DOMException::INDEX_SIZE_ERR, OK, 107 );
1793   //  EXCEPTIONSTEST(charData->replaceData(2, -1, "Replacement stuff"), DOMException::INDEX_SIZE_ERR,  OK, 108 );
1794 
1795     EXCEPTIONSTEST(charData->substringData((XMLSize_t)-1, 5), DOMException::INDEX_SIZE_ERR, OK, 109 );
1796     EXCEPTIONSTEST(charData->substringData(100, 5), DOMException::INDEX_SIZE_ERR, OK, 110 );
1797  //   EXCEPTIONSTEST(charData->substringData(2, -1), DOMException::INDEX_SIZE_ERR, OK, 111 );
1798 
1799 
1800     // Test the user data
1801     // Test simple set and get
1802     DOMCharacterData* userTest = charData;
1803     DOMDocument*  userFirst = document;
1804     XMLCh* userSecond = tempStr2;
1805     XMLString::transcode("first", tempStr, 3999);
1806 
1807     XMLString::transcode("document", tempStr2, 3999);
1808     userTest->setUserData(tempStr2, (void*) document, 0);
1809     void* mydocument = userTest->getUserData(tempStr2);
1810     if (document != mydocument) {
1811         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1812         OK = false;
1813     }
1814 
1815     userTest->setUserData(tempStr, (void*) userFirst, 0);
1816     void* myFirst = userTest->getUserData(tempStr);
1817     if (userFirst != myFirst) {
1818         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
1819         OK = false;
1820     }
1821 
1822     // Test overwrite
1823     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
1824     void* mySecond = userTest->getUserData(tempStr);
1825     if (userSecond != mySecond) {
1826         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1827         OK = false;
1828     }
1829     if (userFirst != myFirst2) {
1830         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1831         OK = false;
1832     }
1833     if (userFirst == mySecond) {
1834         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1835         OK = false;
1836     }
1837 
1838     // Test null
1839     // test non-exist key
1840     XMLString::transcode("not-exist", tempStr3, 3999);
1841     if (userTest->getUserData(tempStr3)) {
1842         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1843         OK = false;
1844     }
1845 
1846     // use a node that does not have user data set before
1847     if (userFirst->getUserData(tempStr)) {
1848         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1849         OK = false;
1850     }
1851 
1852     // Test reset
1853     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
1854     void* myNull = userTest->getUserData(tempStr);
1855     if (userSecond != mySecond2) {
1856         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1857         OK = false;
1858     }
1859     if (myNull) {
1860         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1861         OK = false;
1862     }
1863 
1864     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
1865     void* myNull2 = userTest->getUserData(tempStr2);
1866     if (mydocument != mydocument2) {
1867         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1868         OK = false;
1869     }
1870     if (myNull2) {
1871         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
1872         OK = false;
1873     }
1874 
1875     //the userTest user data table should be null now
1876     if (userTest->getUserData(tempStr)) {
1877         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1878         OK = false;
1879     }
1880     if (userTest->getUserData(tempStr2)) {
1881         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
1882         OK = false;
1883     }
1884 
1885     // Test DOMUserDataHandler
1886     // test clone
1887     userTest->setUserData(tempStr2, (void*) document, &userhandler);
1888     DOMNode* mycloned = userTest->cloneNode(true);
1889     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
1890 
1891     // test import
1892     document->setUserData(tempStr2, (void*) document, &userhandler);
1893     DOMNode* myimport = document->importNode(userTest,true);
1894     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
1895 
1896     // test delete
1897     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
1898     myimport->release();
1899     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
1900 
1901     // Test isSameNode
1902     if (!userTest->isSameNode(userTest)) {
1903         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1904         OK = false;
1905     }
1906     if (userTest->isSameNode(userFirst)) {
1907         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
1908         OK = false;
1909     }
1910 
1911     // Test isEqualNode
1912     if (!userTest->isEqualNode(mycloned)) {
1913         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1914         OK = false;
1915     }
1916 
1917     if (!userTest->isEqualNode(userTest)) {
1918         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1919         OK = false;
1920     }
1921     if (userTest->isEqualNode(userFirst)) {
1922         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
1923         OK = false;
1924     }
1925 
1926     // Test rename, should throw exception
1927     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 205 );
1928 
1929     if (!OK)
1930         printf("\n*****The DOMCharacterData method calls listed above failed, all others worked correctly.*****\n");
1931     charData->setData(resetData); // reset node to original data
1932     return OK;
1933 }
1934 
1935 
1936 
1937 
1938 /**
1939  * This method tests ChildNodeList methods for the XML DOM implementation
1940  * @param document org.w3c.dom.DOMDocument
1941  *
1942  */
testChildNodeList(DOMDocument * document)1943 bool DOMTest::testChildNodeList(DOMDocument* document)
1944 {
1945     DOMNode* node;
1946     DOMNode* node2;
1947     bool OK = true;
1948 // For debugging*****   printf("\n          testChildNodeList's outputs:\n");
1949     node = document->getDocumentElement()->getLastChild(); // node gets doc's testBody element
1950 
1951     if (!(node->getChildNodes()->getLength()== 4))
1952         OK = false;
1953     node2 = node->getChildNodes()->item(2);
1954     XMLString::transcode("dBodyLevel23", tempStr, 3999);
1955     if (XMLString::compareString(tempStr, node2->getNodeName()))
1956         OK = false;
1957 
1958     if (!OK)
1959         printf("\n*****The ChildNodeList method calls listed above failed, all others worked correctly.*****\n");
1960     return OK;
1961 }
1962 
1963 
1964 
1965 /**
1966  * This method tests DOMComment* methods for the XML DOM implementation
1967  * @param document org.w3c.dom.DOMDocument
1968  *
1969  */
testComment(DOMDocument * document)1970 bool DOMTest::testComment(DOMDocument* document)
1971 {
1972     DOMNode* node;
1973     DOMNode* node2;
1974     bool T = true;
1975     bool OK = true;
1976 // For debugging*****   printf("\n          testComment's outputs:\n");
1977     XMLString::transcode("dBodyLevel31", tempStr, 3999);
1978     node = document->getDocumentElement()->getElementsByTagName(tempStr)->item(0)->getFirstChild(); // node gets textNode11
1979     node2 = node->cloneNode(T);
1980     // Check nodes for equality, both their name and value or lack thereof
1981     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
1982           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
1983         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
1984         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
1985     {
1986         fprintf(stderr, "'cloneNode' did not clone the DOMComment* node correctly\n");
1987         OK = false;
1988     }
1989     // Deep clone test comparison is in testNode & testDocument
1990 
1991     // Test the user data
1992     // Test simple set and get
1993     DOMComment* userTest = (DOMComment*) node;
1994     DOMComment*  userFirst = (DOMComment*) node2;
1995     XMLCh* userSecond = tempStr2;
1996     XMLString::transcode("first", tempStr, 3999);
1997 
1998     XMLString::transcode("document", tempStr2, 3999);
1999     userTest->setUserData(tempStr2, (void*) document, 0);
2000     void* mydocument = userTest->getUserData(tempStr2);
2001     if (document != mydocument) {
2002         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2003         OK = false;
2004     }
2005 
2006     userTest->setUserData(tempStr, (void*) userFirst, 0);
2007     void* myFirst = userTest->getUserData(tempStr);
2008     if (userFirst != myFirst) {
2009         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2010         OK = false;
2011     }
2012 
2013     // Test overwrite
2014     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
2015     void* mySecond = userTest->getUserData(tempStr);
2016     if (userSecond != mySecond) {
2017         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2018         OK = false;
2019     }
2020     if (userFirst != myFirst2) {
2021         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2022         OK = false;
2023     }
2024     if (userFirst == mySecond) {
2025         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2026         OK = false;
2027     }
2028 
2029     // Test null
2030     // test non-exist key
2031     XMLString::transcode("not-exist", tempStr3, 3999);
2032     if (userTest->getUserData(tempStr3)) {
2033         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2034         OK = false;
2035     }
2036 
2037     // use a node that does not have user data set before
2038     if (userFirst->getUserData(tempStr)) {
2039         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2040         OK = false;
2041     }
2042 
2043     // Test reset
2044     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
2045     void* myNull = userTest->getUserData(tempStr);
2046     if (userSecond != mySecond2) {
2047         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2048         OK = false;
2049     }
2050     if (myNull) {
2051         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2052         OK = false;
2053     }
2054 
2055     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
2056     void* myNull2 = userTest->getUserData(tempStr2);
2057     if (mydocument != mydocument2) {
2058         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2059         OK = false;
2060     }
2061     if (myNull2) {
2062         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2063         OK = false;
2064     }
2065 
2066     //the userTest user data table should be null now
2067     if (userTest->getUserData(tempStr)) {
2068         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2069         OK = false;
2070     }
2071     if (userTest->getUserData(tempStr2)) {
2072         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2073         OK = false;
2074     }
2075 
2076     // Test DOMUserDataHandler
2077     // test clone
2078     userTest->setUserData(tempStr2, (void*) document, &userhandler);
2079     DOMNode* mycloned = userTest->cloneNode(true);
2080     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
2081 
2082     // test import
2083     document->setUserData(tempStr2, (void*) document, &userhandler);
2084     DOMNode* myimport = document->importNode(userTest,true);
2085     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
2086 
2087     // test delete
2088     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
2089     myimport->release();
2090     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
2091 
2092     // Test isSameNode
2093     if (!userTest->isSameNode(userTest)) {
2094         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2095         OK = false;
2096     }
2097     if (userTest->isSameNode(userFirst)) {
2098         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2099         OK = false;
2100     }
2101 
2102     // Test isEqualNode
2103     if (!userTest->isEqualNode(mycloned)) {
2104         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2105         OK = false;
2106     }
2107 
2108     if (!userTest->isEqualNode(userTest)) {
2109         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2110         OK = false;
2111     }
2112     if (userTest->isEqualNode(document)) {
2113         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2114         OK = false;
2115     }
2116 
2117     // Test rename, should throw exception
2118     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 206 );
2119 
2120     if (!OK)
2121         printf("\n*****The DOMComment* method calls listed above failed, all others worked correctly.*****\n");
2122     return OK;
2123 }
2124 
2125 
2126 
2127 /**
2128  * This method tests DeepNodeList methods for the XML DOM implementation
2129  * @param document org.w3c.dom.DOMDocument
2130  *
2131  */
testDeepNodeList(DOMDocument * document)2132 bool DOMTest::testDeepNodeList(DOMDocument* document)
2133 {
2134     DOMNode* node;
2135     DOMNode* node2;
2136     bool OK = true;
2137 // For debugging*****   printf("\n          testDeepNodeList's outputs:\n\n");
2138     node = document->getLastChild()->getLastChild(); // node gets docBody element
2139 //  DOMElement* el = (DOMElement*)node;
2140 //  DOMNodeList nl = el->getElementsByTagName("*");
2141 //  int len = nl->getLength();
2142 //  if (len != 8)
2143     XMLString::transcode("*",tempStr, 3999);
2144     if (!(8 == ((DOMElement*) node)->getElementsByTagName(tempStr)->getLength()))
2145         {
2146             printf ("Warning!!! DeepNodeList's 'getLength' failed to work properly!\n");
2147             OK = false;
2148         }
2149     node2 = ((DOMElement*) node)->getElementsByTagName(tempStr)->item(2); //This also runs through 'nextMatchingElementAfter"
2150 
2151     XMLString::transcode("dBodyLevel32", tempStr, 3999);
2152     if (XMLString::compareString(tempStr, node2->getNodeName()))
2153         {
2154             printf ("Warning!!! DeepNodeList's 'item' (or DOMElement's 'getElementsBy TagName)failed to work properly!\n");
2155             OK = false;
2156         }
2157     node2 = document->getLastChild();
2158     XMLString::transcode("dTestBody", tempStr, 3999);
2159     if (XMLString::compareString(tempStr, ((DOMElement*) node2)->getElementsByTagName(tempStr)->item(0)->getNodeName()))//This also runs through 'nextMatchingElementAfter"
2160         {
2161             printf ("Warning!!! DeepNodeList's 'item' (or DOMElement's 'getElementsBy TagName)failed to work properly!\n");
2162             OK = false;
2163         }
2164 
2165     if (!OK)
2166         printf("\n*****The DeepNodeList method calls listed above failed, all others worked correctly.*****\n");
2167     return OK;
2168 }
2169 
2170 
2171 
2172 /**
2173  * This method tests DOMDocument* methods for the XML DOM implementation
2174  * @param document org.w3c.dom.DOMDocument
2175  *
2176  *
2177  **** ALL DOMDocument* create methods are run in docBuilder except createAttribute which is in testAttribute**
2178  */
testDocument(DOMDocument * document)2179 bool DOMTest::testDocument(DOMDocument* document)
2180 {
2181     DOMTest make;
2182     DOMDocumentFragment* docFragment, *docFragment2;
2183     DOMElement* newElement;
2184     DOMNode* node, *node2;
2185 
2186     const char* elementNames[] =  {"dFirstElement", "dTestBody", "dBodyLevel21","dBodyLevel31","dBodyLevel32",
2187                    "dBodyLevel22","dBodyLevel33","dBodyLevel34","dBodyLevel23","dBodyLevel24"};
2188     const char* newElementNames[] = {"dFirstElement", "dTestBody", "dBodyLevel22","dBodyLevel33","dBodyLevel34","dBodyLevel23"};
2189 
2190 
2191     bool result;
2192     bool OK = true;
2193 // For debugging*****   printf("\n          testDocument's outputs:\n \n");
2194 
2195     XMLString::transcode("testDocument1", tempStr, 3999);
2196     DOMDocumentType* checkDocType =  make.createDocumentType(document,tempStr);
2197     DOMDocumentType* docType = document->getDoctype();
2198 
2199     if (XMLString::compareString(checkDocType->getNodeName(),docType->getNodeName() ))
2200     {
2201         fprintf(stderr, "Warning!!! DOMDocument's 'getDocType method failed!\n" );
2202         OK = false;
2203     }
2204 
2205     if (XMLString::compareString(checkDocType->getNodeValue(), docType->getNodeValue()))
2206     {
2207         fprintf(stderr, "Warning!!! DOMDocument's 'getDocType method failed!\n" );
2208         OK = false;
2209     }
2210 
2211     /*
2212     if (! (checkDocType->getNodeName(), docType->getNodeName()) &&      // Compares node names for equality
2213           (checkDocType->getNodeValue() != 0 && docType->getNodeValue() != 0)   // Checks to make sure each node has a value node
2214         ?  checkDocType->getNodeValue(), docType->getNodeValue())       // If both have value nodes test those value nodes for equality
2215         : (checkDocType->getNodeValue() == 0 && docType->getNodeValue() == 0))) // If one node doesn't have a value node make sure both don't
2216     {
2217         fprintf(stderr, "Warning!!! DOMDocument's 'getDocType method failed!\n" );
2218         OK = false;
2219     }
2220     */
2221 
2222     DOMNode*  rootElement = document->getLastChild();
2223 
2224     bool check = (rootElement->getNodeValue() && document->getDocumentElement()->getNodeValue())   // Checks to make sure each node has a value node
2225         ?  !XMLString::compareString(rootElement->getNodeValue(), document->getDocumentElement()->getNodeValue())      // If both have value nodes test those value nodes for equality
2226         : (rootElement->getNodeValue() == 0 && document->getDocumentElement()->getNodeValue() == 0);    // If one node doesn't have a value node make sure both don't
2227     if (!(!XMLString::compareString(rootElement->getNodeName(), document->getDocumentElement()->getNodeName()) &&        // Compares node names for equality
2228          check))
2229     {
2230         fprintf(stderr, "Warning!!! DOMDocument's 'getDocumentElement' method failed!\n" );
2231         OK = false;
2232     }
2233 
2234     XMLString::transcode("*", tempStr, 3999);
2235     DOMNodeList* docElements = document->getElementsByTagName(tempStr);
2236     XMLSize_t docSize = docElements->getLength();
2237     XMLSize_t i;
2238     for (i = 0; i < docSize; i++)
2239     {
2240         DOMNode*  n = (DOMNode*) docElements->item(i);
2241         XMLString::transcode(elementNames[i], tempStr, 3999);
2242         if (XMLString::compareString(tempStr, n->getNodeName()))
2243         {
2244             fprintf(stderr, "Comparison of this document's elements failed at element number %llu at line %i \n",
2245                     (unsigned long long) i, __LINE__);
2246             OK = false;
2247             break;
2248         }
2249     }
2250 
2251     // What is this supposed to be doing?
2252     //
2253     //if (document->equals(document->getImplementation()))
2254     //{
2255     //  fprintf(stderr, "Warning!!! DOMDocument's 'getImplementation' method failed!\n" );
2256     //  OK = false;
2257     //}
2258 
2259     XMLString::transcode("NewElementTestsInsertBefore", tempStr, 3999);
2260     newElement = document->createElement(tempStr);
2261     //  doc->insertBefore(newElement,0);//!! Throws a HIERARCHY_REQUEST_ERR   *******
2262     //  doc->removeChild(docElements->item(9));//!! Throws a NOT_FOUND_ERR  ********
2263 
2264     docFragment = document->createDocumentFragment();
2265     //Tests removeChild and stores removed branch for tree reconstruction
2266     docFragment->appendChild(docElements->item(1)->removeChild(docElements->item(9)));
2267     docFragment2 = document->createDocumentFragment();
2268     //Tests removeChild and stores removed branch for tree reconstruction
2269     docFragment2->appendChild(docElements->item(1)->removeChild(docElements->item(2)));
2270     docSize = docElements->getLength();
2271     for (i = 0; i < docSize; i++)
2272     {
2273         DOMNode*  n = (DOMNode*) docElements->item(i);
2274         XMLString::transcode(newElementNames[i], tempStr, 3999);
2275         if (XMLString::compareString(tempStr, n->getNodeName()))
2276         {
2277             fprintf(stderr, "Comparison of new document's elements failed at element number %llu at line %i \n",
2278                     (unsigned long long) i, __LINE__);
2279             OK = false;
2280             break;
2281         }
2282     }
2283     docElements->item(1)->insertBefore(docFragment, 0); //Reattaches removed branch to restore tree to the original
2284                                                 // AH Revist.  Note: insertBefore should be able to take
2285                                                 //   a 0 as its second parameter.
2286     docElements->item(1)->insertBefore(docFragment2, docElements->item(2)); //Reattaches removed branch to restore tree to the original
2287 
2288     //  printf(docElements->item(2)->getNodeName());
2289 
2290     docSize = docElements->getLength();
2291     for (i = 0; i < docSize; i++)
2292     {
2293         DOMNode*  n = (DOMNode*) docElements->item(i);
2294         XMLString::transcode(elementNames[i], tempStr, 3999);
2295         if (XMLString::compareString(tempStr, n->getNodeName()))
2296         {
2297             fprintf(stderr, "Comparison of restored document's elements failed at element number %llu at line %i \n",
2298                     (unsigned long long) i, __LINE__);
2299             OK = false;
2300             break;
2301         }
2302     }
2303 
2304     DOMTest tests;
2305 
2306 
2307 //  DOMDocument* z = tests.createDocument();
2308 //  tests.docBuilder(z, "z");
2309 
2310 //!! Throws WRONG_DOCUMENT_ERR **********
2311 //  EXCEPTIONSTEST(z->appendChild(
2312     //  z->appendChild(d.createComment("Test doc d comment"));// Tries to append z document with document d comment
2313     //  d->getDocumentElement()->appendChild(z.createElement("newZdocElement"));// Tries to append d document with document z DOMElement
2314     //  d->getLastChild()->getLastChild()->insertBefore(z.createElement("newZdocElement"),d->getLastChild()->getLastChild()->getFirstChild());// Tries to insert into d document with document z DOMElement
2315     //  d->replaceChild(z.createElement("newZdocElement"),d->getLastChild()->getLastChild()->getFirstChild());  // Tries to replace in d document with document z DOMElement
2316 
2317     //  doc->setNodeValue("This shouldn't work");//!! Throws a NO_MODIFICATION_ALLOWED_ERR ********
2318 
2319     node = document;
2320     node2 = document->cloneNode(true);
2321     result = treeCompare(node, node2); // Deep clone test comparison of document cloneNode
2322     if (!result)
2323     {
2324         fprintf(stderr, "Warning!!! Deep clone of the document failed!\n");
2325         OK = false;
2326     }
2327     node2->release();
2328 
2329     // Deep clone test comparison is also in testNode
2330 
2331     // Test the user data
2332     // Test simple set and get
2333     DOMDocumentFragment* userTest = docFragment;
2334     DOMDocumentFragment*  userFirst = docFragment2;
2335     XMLCh* userSecond = tempStr2;
2336     XMLString::transcode("first", tempStr, 3999);
2337 
2338     XMLString::transcode("document", tempStr2, 3999);
2339     userTest->setUserData(tempStr2, (void*) document, 0);
2340     void* mydocument = userTest->getUserData(tempStr2);
2341     if (document != mydocument) {
2342         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2343         OK = false;
2344     }
2345 
2346     userTest->setUserData(tempStr, (void*) userFirst, 0);
2347     void* myFirst = userTest->getUserData(tempStr);
2348     if (userFirst != myFirst) {
2349         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2350         OK = false;
2351     }
2352 
2353     // Test overwrite
2354     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
2355     void* mySecond = userTest->getUserData(tempStr);
2356     if (userSecond != mySecond) {
2357         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2358         OK = false;
2359     }
2360     if (userFirst != myFirst2) {
2361         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2362         OK = false;
2363     }
2364     if (userFirst == mySecond) {
2365         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2366         OK = false;
2367     }
2368 
2369     // Test null
2370     // test non-exist key
2371     XMLString::transcode("not-exist", tempStr3, 3999);
2372     if (userTest->getUserData(tempStr3)) {
2373         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2374         OK = false;
2375     }
2376 
2377     // use a node that does not have user data set before
2378     if (userFirst->getUserData(tempStr)) {
2379         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2380         OK = false;
2381     }
2382 
2383     // Test reset
2384     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
2385     void* myNull = userTest->getUserData(tempStr);
2386     if (userSecond != mySecond2) {
2387         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2388         OK = false;
2389     }
2390     if (myNull) {
2391         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2392         OK = false;
2393     }
2394 
2395     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
2396     void* myNull2 = userTest->getUserData(tempStr2);
2397     if (mydocument != mydocument2) {
2398         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2399         OK = false;
2400     }
2401     if (myNull2) {
2402         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2403         OK = false;
2404     }
2405 
2406     //the userTest user data table should be null now
2407     if (userTest->getUserData(tempStr)) {
2408         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2409         OK = false;
2410     }
2411     if (userTest->getUserData(tempStr2)) {
2412         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2413         OK = false;
2414     }
2415 
2416     // Test DOMUserDataHandler
2417     // test clone
2418     userTest->setUserData(tempStr2, (void*) document, &userhandler);
2419     DOMNode* mycloned = userTest->cloneNode(true);
2420     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
2421 
2422     // test import
2423     document->setUserData(tempStr2, (void*) document, &userhandler);
2424     DOMNode* myimport = document->importNode(userTest,true);
2425     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
2426 
2427     // test delete
2428     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
2429     myimport->release();
2430     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
2431 
2432     // Test isSameNode
2433     if (!userTest->isSameNode(userTest)) {
2434         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2435         OK = false;
2436     }
2437     if (userTest->isSameNode(userFirst)) {
2438         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2439         OK = false;
2440     }
2441 
2442     // Test isEqualNode
2443     if (!userTest->isEqualNode(mycloned)) {
2444         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2445         OK = false;
2446     }
2447 
2448     if (!userTest->isEqualNode(userTest)) {
2449         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2450         OK = false;
2451     }
2452     if (userTest->isEqualNode(document)) {
2453         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2454         OK = false;
2455     }
2456 
2457     // Test rename, should throw exception
2458     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 207 );
2459 
2460     if (!OK)
2461         printf("\n*****The DOMDocument* method calls listed above failed, all others worked correctly.*****\n");
2462     return OK;
2463 }
2464 
2465 
2466 
2467 /**
2468  * This method tests DOMDocumentFragment* methods for the XML DOM implementation
2469  * @param document org.w3c.dom.DOMDocument
2470  *
2471  *
2472  *
2473  ********This really isn't needed, only exists to throw NO_MODIFICATION_ALLOWED_ERR ********
2474  */
testDocumentFragment(DOMDocument * document)2475 bool DOMTest::testDocumentFragment(DOMDocument* document)
2476 {
2477     bool OK = true;
2478 // For debugging*****   printf("\n          testDocumentFragment's outputs:\n");
2479     DOMDocumentFragment* testDocFragment = document->createDocumentFragment();
2480 
2481     //  testDocFragment->setNodeValue("This is a document fragment!");//!! Throws a NO_MODIFICATION_ALLOWED_ERR ********
2482 
2483     // Test isSameNode
2484     DOMDocumentFragment* userTest = testDocFragment;
2485     DOMNode* mycloned = testDocFragment->cloneNode(true);
2486     DOMDocument* userFirst = document;
2487 
2488     if (!userTest->isSameNode(userTest)) {
2489         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2490         OK = false;
2491     }
2492     if (userTest->isSameNode(userFirst)) {
2493         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2494         OK = false;
2495     }
2496 
2497     // Test isEqualNode
2498     if (!userTest->isEqualNode(mycloned)) {
2499         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2500         OK = false;
2501     }
2502 
2503     if (!userTest->isEqualNode(userTest)) {
2504         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2505         OK = false;
2506     }
2507     if (userTest->isEqualNode(userFirst)) {
2508         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2509         OK = false;
2510     }
2511 
2512     // Test rename, should throw exception
2513     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 208 );
2514 
2515     if (!OK)
2516         printf("\n*****The DOMDocumentFragment* method calls listed above failed, all others worked correctly.*****\n");
2517     return OK;
2518 }
2519 
2520 
2521 
2522 /**
2523  * This method tests DOMDocumentType* methods for the XML DOM implementation
2524  * @param document org.w3c.dom.DOMDocument
2525  *
2526  */
testDocumentType(DOMDocument * document)2527 bool DOMTest::testDocumentType(DOMDocument* document)
2528 {
2529     DOMTest test;
2530     DOMDocumentType* docType, *holdDocType;
2531     DOMNamedNodeMap* docNotationMap;
2532     DOMNode* node, *node2;
2533     bool OK = true;
2534 // For debugging*****   printf("\n          testDocumentType's outputs:\n");
2535     XMLString::transcode("TestDocument", tempStr, 3999);
2536     DOMDocumentType* newDocumentType =  test.createDocumentType(document, tempStr);
2537     node = document->getFirstChild(); // node gets doc's docType node
2538     node2 = node->cloneNode(true);
2539     // Check nodes for equality, both their name and value or lack thereof
2540     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
2541           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
2542         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
2543         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
2544     {
2545         fprintf(stderr, "'cloneNode' did not clone the DOMDocumentType* node correctly\n");
2546         OK = false;
2547     }
2548      // Deep clone test comparison is in testNode & testDocument
2549 
2550     DOMNode*   abc9 = document->getFirstChild();
2551     docType = (DOMDocumentType*) abc9;
2552 
2553     XMLString::transcode("ourNotationNode", tempStr, 3999);
2554     docNotationMap = docType->getNotations();
2555     if (XMLString::compareString(tempStr, docNotationMap->item(0)->getNodeName()))
2556     {
2557         fprintf(stderr, "Warning!!! DOMDocumentType's 'getNotations' failed!\n");
2558         OK = false;
2559     }
2560     //  doc->appendChild(newDocumentTypeImpl);//!! Throws a HIERARCHY_REQUEST_ERR    *******
2561     DOMNode*  abc10 = document->removeChild(document->getFirstChild()); //Tests removeChild and stores removed branch for tree reconstruction
2562     holdDocType = (DOMDocumentType*) abc10;
2563     document->insertBefore(newDocumentType, document->getDocumentElement());
2564     //** Other aspects of insertBefore are tested in docBuilder through appendChild*
2565 
2566     DOMNode* rem = document->removeChild(document->getFirstChild()); //Removes newDocumentType for tree restoral
2567     rem->release();
2568     document->insertBefore(holdDocType, document->getFirstChild()); //Reattaches removed branch to restore tree to the original
2569 
2570     // Test the user data
2571     // Test simple set and get
2572     DOMDocumentType* userTest = docType;
2573     DOMNamedNodeMap*  userFirst = docNotationMap;
2574     XMLCh* userSecond = tempStr2;
2575     XMLString::transcode("first", tempStr, 3999);
2576 
2577     XMLString::transcode("document", tempStr2, 3999);
2578     userTest->setUserData(tempStr2, (void*) document, 0);
2579     void* mydocument = userTest->getUserData(tempStr2);
2580     if (document != mydocument) {
2581         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2582         OK = false;
2583     }
2584 
2585     userTest->setUserData(tempStr, (void*) userFirst, 0);
2586     void* myFirst = userTest->getUserData(tempStr);
2587     if (userFirst != myFirst) {
2588         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
2589         OK = false;
2590     }
2591 
2592     // Test overwrite
2593     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
2594     void* mySecond = userTest->getUserData(tempStr);
2595     if (userSecond != mySecond) {
2596         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2597         OK = false;
2598     }
2599     if (userFirst != myFirst2) {
2600         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2601         OK = false;
2602     }
2603     if (userFirst == mySecond) {
2604         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2605         OK = false;
2606     }
2607 
2608     // Test null
2609     // test non-exist key
2610     XMLString::transcode("not-exist", tempStr3, 3999);
2611     if (userTest->getUserData(tempStr3)) {
2612         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2613         OK = false;
2614     }
2615 
2616     // use a node that does not have user data set before
2617     if (node2->getUserData(tempStr)) {
2618         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2619         OK = false;
2620     }
2621 
2622     // Test reset
2623     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
2624     void* myNull = userTest->getUserData(tempStr);
2625     if (userSecond != mySecond2) {
2626         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2627         OK = false;
2628     }
2629     if (myNull) {
2630         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2631         OK = false;
2632     }
2633 
2634     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
2635     void* myNull2 = userTest->getUserData(tempStr2);
2636     if (mydocument != mydocument2) {
2637         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2638         OK = false;
2639     }
2640     if (myNull2) {
2641         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
2642         OK = false;
2643     }
2644 
2645     //the userTest user data table should be null now
2646     if (userTest->getUserData(tempStr)) {
2647         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2648         OK = false;
2649     }
2650     if (userTest->getUserData(tempStr2)) {
2651         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
2652         OK = false;
2653     }
2654 
2655     // Test DOMUserDataHandler
2656     // test clone
2657     userTest->setUserData(tempStr2, (void*) document, &userhandler);
2658     DOMNode* mycloned = userTest->cloneNode(true);
2659     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
2660 
2661     // document type cannot be imported, so no test import, rather test the exception
2662     document->setUserData(tempStr2, (void*) document, &userhandler);
2663     EXCEPTIONSTEST(document->importNode(userTest,true), DOMException::NOT_SUPPORTED_ERR, OK, 203 );
2664 
2665     // Test isSameNode
2666     if (!userTest->isSameNode(userTest)) {
2667         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2668         OK = false;
2669     }
2670     if (userTest->isSameNode(document)) {
2671         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
2672         OK = false;
2673     }
2674 
2675     // Test isEqualNode
2676     if (!userTest->isEqualNode(mycloned)) {
2677         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2678         OK = false;
2679     }
2680 
2681     if (!userTest->isEqualNode(userTest)) {
2682         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2683         OK = false;
2684     }
2685     if (userTest->isEqualNode(document)) {
2686         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
2687         OK = false;
2688     }
2689 
2690     // Test rename, should throw exception
2691     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 209 );
2692 
2693     if (!OK)
2694         printf("\n*****The DOMDocumentType* method calls listed above failed, all others worked correctly.*****\n");
2695     return OK;
2696 }
2697 
2698 
2699 
2700 /**
2701  * @param document org.w3c.dom.DOMDocument
2702  */
testDOMerrors(DOMDocument * document)2703 bool DOMTest::testDOMerrors(DOMDocument* document) {
2704     bool OK = true;
2705 
2706     DOMTest tests;
2707 
2708     EXCEPTIONSTEST(document->appendChild(testElementNode), DOMException::HIERARCHY_REQUEST_ERR, OK, 201 );
2709     EXCEPTIONSTEST(testTextNode->appendChild(testTextNode), DOMException::HIERARCHY_REQUEST_ERR, OK, 202 );
2710     return OK;
2711 }
2712 
2713 #define TEST_VALID_XPATH(xpath, expected, line)   \
2714     try \
2715     { \
2716         XMLCh xpathStr[100]; \
2717         XMLString::transcode(xpath,xpathStr,99); \
2718         DOMXPathResult* result=(DOMXPathResult*)document->evaluate(xpathStr, document->getDocumentElement(), NULL, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL); \
2719         if(result->getSnapshotLength() != expected) {  \
2720             fprintf(stderr, "DOMDocument::evaluate does not work in line %i (%llu nodes instead of %d)\n", line, \
2721                     (unsigned long long) result->getSnapshotLength(), expected); \
2722             OK = false; \
2723         }   \
2724         result->release(); \
2725     }   \
2726     catch(DOMException&) \
2727     {   \
2728         fprintf(stderr, "DOMDocument::evaluate failed at line %i\n", line); \
2729         OK = false; \
2730     }
2731 
2732 #define TEST_VALID_XPATH_NS(xpath, resolver, expected, line)   \
2733     try \
2734     { \
2735         XMLCh xpathStr[100]; \
2736         XMLString::transcode(xpath,xpathStr,99); \
2737         DOMXPathResult* result=(DOMXPathResult*)document->evaluate(xpathStr, document->getDocumentElement(), resolver, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL); \
2738         if(result->getSnapshotLength() != expected) {  \
2739             fprintf(stderr, "DOMDocument::evaluate does not work in line %i (%llu nodes instead of %d)\n", line, \
2740                     (unsigned long long) result->getSnapshotLength(), expected); \
2741             OK = false; \
2742         }   \
2743         result->release(); \
2744     }   \
2745     catch(DOMException&) \
2746     {   \
2747         fprintf(stderr, "DOMDocument::evaluate failed at line %i\n", line); \
2748         OK = false; \
2749     }
2750 
2751 #define TEST_INVALID_XPATH(xpath, line)   \
2752     try \
2753     { \
2754         XMLCh xpathStr[100]; \
2755         XMLString::transcode(xpath,xpathStr,99); \
2756         DOMXPathResult* result=(DOMXPathResult*)document->evaluate(xpathStr, document->getDocumentElement(), NULL, DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE, NULL); \
2757         fprintf(stderr, "DOMDocument::evaluate does not work in line %i (invalid XPath)\n", line);  \
2758         OK = false; \
2759         result->release(); \
2760     }   \
2761     catch(DOMException& ) \
2762     {   \
2763     }
2764 
2765 #define TEST_VALID_XPATH_SINGLE(xpath, line)   \
2766     try \
2767     { \
2768         XMLCh xpathStr[100]; \
2769         XMLString::transcode(xpath,xpathStr,99); \
2770         DOMXPathResult* result=(DOMXPathResult*)document->evaluate(xpathStr, document->getDocumentElement(), NULL, DOMXPathResult::FIRST_ORDERED_NODE_TYPE, NULL); \
2771         if(result->getNodeValue() == NULL) {  \
2772             fprintf(stderr, "DOMDocument::evaluate does not work in line %i (single node not found)\n", line);  \
2773             OK = false; \
2774         }   \
2775         result->release(); \
2776     }   \
2777     catch(DOMException& ) \
2778     {   \
2779         fprintf(stderr, "DOMDocument::evaluate failed at line %i\n", line); \
2780         OK = false; \
2781     }
2782 
2783 #include <xercesc/framework/StdOutFormatTarget.hpp>
2784 
testXPath(DOMDocument * document)2785 bool DOMTest::testXPath(DOMDocument* document) {
2786     bool OK = true;
2787 
2788 #if 0
2789             XMLCh tempLS[3] = {chLatin_L, chLatin_S, chNull};
2790             DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempLS);
2791             DOMLSSerializer   *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
2792             DOMLSOutput       *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
2793             StdOutFormatTarget myFormTarget;
2794             theOutputDesc->setByteStream(&myFormTarget);
2795             theSerializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
2796             theSerializer->write(document, theOutputDesc);
2797 
2798             theOutputDesc->release();
2799             theSerializer->release();
2800 #endif
2801 
2802     TEST_VALID_XPATH("*", 1, __LINE__);
2803     TEST_VALID_XPATH("dTestBody/dBodyLevel24", 1, __LINE__);
2804     TEST_VALID_XPATH("//dBodyLevel34", 1, __LINE__);
2805     TEST_VALID_XPATH("/*", 1, __LINE__);
2806     TEST_VALID_XPATH("/dFirstElement/dTestBody/dBodyLevel24", 1, __LINE__);
2807     TEST_INVALID_XPATH("/dFirstElement//dBodyLevel34", __LINE__);  // the "//" can only be at the beginning
2808     TEST_INVALID_XPATH("/dFirstElement/@dFirstElementdFirstElement", __LINE__);   // cannot select attributes
2809     TEST_VALID_XPATH("//*", 10, __LINE__);
2810     TEST_VALID_XPATH_SINGLE("//*", __LINE__);
2811     TEST_INVALID_XPATH("//ns:node", __LINE__);  // "ns" prefix is undefined
2812 
2813     XMLCh tempStr[100];
2814     XMLString::transcode("xmlns:ns",tempStr,99);
2815     DOMAttr* attr=document->createAttributeNS(XMLUni::fgXMLNSURIName, tempStr);
2816     attr->setNodeValue(XMLUni::fgXSAXMLScanner);
2817     document->getDocumentElement()->setAttributeNodeNS(attr);
2818     const DOMXPathNSResolver* resolver=document->createNSResolver(document->getDocumentElement());
2819     TEST_VALID_XPATH_NS("//ns:node", resolver, 0, __LINE__);
2820     document->getDocumentElement()->removeAttributeNode(attr);
2821 
2822     return OK;
2823 }
2824 
2825 /**
2826  * This method tests DOMImplementation methods for the XML DOM implementation
2827  * @param document org.w3c.dom.DOMDocument
2828  *
2829  */
testDOMImplementation(DOMDocument * document)2830 bool DOMTest::testDOMImplementation(DOMDocument* document)
2831 {
2832 
2833     DOMImplementation* implementation;
2834     bool result = false;
2835     bool OK = true;
2836 // For debugging*****   printf("\n          testDOMImplementation's outputs:\n");
2837     implementation = document->getImplementation(); //Uses getDOMImplementation to obtain implementation
2838 
2839     XMLString::transcode("XML", tempStr, 3999);
2840     XMLString::transcode("1.0", tempStr2, 3999);
2841     result = implementation->hasFeature(tempStr, tempStr2);
2842     if(!result)
2843     {
2844         fprintf(stderr, "Warning!!! DOMImplementation's 'hasFeature' that should be 'true' failed!");
2845         OK = false;
2846     }
2847 
2848     XMLString::transcode("HTML", tempStr, 3999);
2849     XMLString::transcode("4.0", tempStr2, 3999);
2850     result = implementation->hasFeature(tempStr, tempStr2);
2851     if(result)
2852     {
2853         fprintf(stderr, "Warning!!! DOMImplementation's 'hasFeature' that should be 'false' failed!");
2854         OK = false;
2855     }
2856 
2857 
2858     if (!OK)
2859         fprintf(stderr, "\n*****The DOMImplementation method calls listed above failed, all others worked correctly.*****\n");
2860     return OK;
2861 }
2862 
2863 
2864 
2865 /**
2866  * This method tests DOMElement* methods for the XML DOM implementation
2867  * @param document org.w3c.dom.DOMDocument
2868  *
2869  */
testElement(DOMDocument * document)2870 bool DOMTest::testElement(DOMDocument* document)
2871 {
2872     DOMAttr* attributeNode, *newAttributeNode;
2873     DOMElement* element, *element2;
2874     DOMNode* node, *node2;
2875 
2876     const char* elementNames[] =  {"dFirstElement", "dTestBody", "dBodyLevel21","dBodyLevel31","dBodyLevel32",
2877                    "dBodyLevel22","dBodyLevel33","dBodyLevel34","dBodyLevel23","dBodyLevel24"};
2878     const char* textCompare[] = {"dBodyLevel31'sChildTextNode11",
2879                                 "dBodyLevel31'sChildTextNode12",
2880                                 "dBodyLevel31'sChildTextNode13"};
2881 
2882     DOMNamedNodeMap* nodeMap;
2883     bool OK = true;
2884     node = document->getDocumentElement(); // node gets doc's firstElement
2885     node2 = node->cloneNode(true);
2886     // Check nodes for equality, both their name and value or lack thereof
2887     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
2888           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
2889         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
2890         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
2891     {
2892         fprintf(stderr, "'cloneNode' did not clone the DOMElement* node correctly.\n");
2893         OK = false;
2894     }
2895     // Deep clone test comparison is in testNode & testDocument
2896 
2897     element = document->getDocumentElement(); // element gets doc's firstElement
2898 
2899     XMLString::copyString(tempStr, document->getNodeValue());
2900     XMLString::transcode("'s test attribute", tempStr2, 3999);
2901     XMLString::catString(tempStr, tempStr2);
2902     if (XMLString::compareString(XMLUni::fgZeroLenString, element->getAttribute(tempStr)))
2903     {
2904         fprintf(stderr, "Warning!!! DOMElement's 'getAttribute' failed!\n");
2905         OK = false;
2906     }
2907 
2908     XMLString::copyString(tempStr, document->getNodeValue());
2909     XMLString::transcode("FirstElement", tempStr2, 3999);
2910     XMLString::catString(tempStr, tempStr2);
2911     attributeNode = element->getAttributeNode(tempStr);
2912     if(! (attributeNode == 0))
2913     {
2914         fprintf(stderr, "Warning!!! DOMElement's 'getAttributeNode' failed! It should have returned '0' here!\n");
2915         OK = false;
2916     }
2917 
2918 
2919     XMLString::transcode("AnotherFirstElementAttribute", tempStr, 3999);
2920     newAttributeNode = document->createAttribute(tempStr);
2921 
2922     XMLString::transcode("A new attribute which helps test calls in DOMElement", tempStr, 3999);
2923     newAttributeNode->setValue(tempStr);
2924     // This test is incorrect.  It assumes that there is a defined ordering of the entries
2925     //  in a nodeMap, but there is no ordering required.
2926 #ifdef TheFollowingCheckIsInvalid
2927     DOMNode* rem2 = element->setAttributeNode(newAttributeNode);
2928     if (rem2)
2929         rem2->release();
2930     nodeMap = element->getAttributes();
2931     XMLSize_t size = nodeMap->getLength();
2932     XMLSize_t k;
2933     for (k = 0; k < size; k++)
2934     {
2935         DOMNode*  n = (DOMNode) nodeMap->item(k);
2936         XMLString::transcode(attributeCompare[k], tempStr, 3999);
2937         if (XMLString::compareString(tempStr, n->getNodeName())))
2938         {
2939             fprintf(stderr, "Warning!!! Comparison of firstElement's attributes failed at line %i.\n", __LINE__);
2940             fprintf(stderr, "   This failure can be a result of DOMElement's 'setValue' and/or 'setAttributeNode' and/or 'getAttributes' failing.\n");
2941             OK = false;
2942             break;
2943         }
2944     //  printf("firstElement's attribute number " + k + " : " + n->getNodeName());
2945     }
2946 #endif
2947 
2948     nodeMap = element->getAttributes();
2949     XMLSize_t size = nodeMap->getLength();
2950     if (size != 2)
2951     {
2952         fprintf(stderr, "DOMElement* Tests Failure 001\n");
2953         OK = false;
2954     }
2955     DOMNode* rem = element->setAttributeNode(newAttributeNode);
2956     if (rem)
2957         rem->release();
2958     size = nodeMap->getLength();
2959     if (size != 3)
2960     {
2961         fprintf(stderr, "DOMElement* Tests Failure 002\n");
2962         OK = false;
2963     }
2964 
2965     // Fetch the newly added attribute node back out of from the named node map,
2966     //  and check that we are returned the same node that we put in->
2967     XMLString::transcode("AnotherFirstElementAttribute", tempStr, 3999);
2968     DOMNode*  abc12 = nodeMap->getNamedItem(tempStr);
2969     DOMAttr* fetchedAttr = (DOMAttr*) abc12;
2970     if (fetchedAttr != newAttributeNode)
2971     {
2972         fprintf(stderr, "DOMElement* Tests Failure 003\n");
2973         OK = false;
2974     }
2975 
2976     // Fetch the newly added attribute back out directly from the element itself.
2977     XMLString::transcode("AnotherFirstElementAttribute", tempStr, 3999);
2978     fetchedAttr = element->getAttributeNode(tempStr);
2979     if (fetchedAttr != newAttributeNode)
2980     {
2981         fprintf(stderr, "DOMElement* Tests Failure 004\n");
2982         OK = false;
2983     }
2984 
2985 
2986 
2987     XMLString::transcode("*",tempStr, 3999);
2988     DOMNodeList* docElements = document->getElementsByTagName(tempStr);
2989     XMLSize_t docSize = docElements->getLength();
2990     XMLSize_t i;
2991     for (i = 0; i < docSize; i++)
2992     {
2993         DOMNode*  n = docElements->item(i);
2994         XMLString::transcode(elementNames[i], tempStr, 3999);
2995         if (XMLString::compareString(tempStr, n->getNodeName()))
2996         {
2997             fprintf(stderr, "Warning!!! Comparison of DOMElement's 'getElementsByTagName' "
2998                             "and/or 'item' failed at element number %llu at line %i \n",
2999                     (unsigned long long) i, __LINE__ );
3000             fprintf(stderr, "\n");
3001             OK = false;
3002             break;
3003         }
3004     //  printf("docElement's number " + i + " is: " + n->getNodeName());
3005     }
3006     XMLString::transcode("dBodyLevel21", tempStr, 3999);
3007     DOMNode*  abc15 = document->getElementsByTagName(tempStr)->item(0); // element gets DOMElement* test BodyLevel21
3008     element = (DOMElement*) abc15;
3009 
3010     XMLString::transcode("dBodyLevel31", tempStr, 3999);
3011     DOMNode*  abc16 = document->getElementsByTagName(tempStr)->item(0); // element2 gets DOMElement* test BodyLevel31
3012     element2 = (DOMElement*) abc16;
3013     DOMNodeList* text = ((DOMNode*  &) element2)->getChildNodes();
3014     XMLSize_t textSize = text->getLength();
3015     XMLSize_t j;
3016     static bool firstTime = true;
3017     if (firstTime)
3018     {
3019         firstTime = false;      // Temporary fix.  Subsequent tests alter the doc, causing
3020                                 //   this test to fail on all but the first time through.
3021         for (j = 0; j < textSize; j++)
3022         {
3023             DOMNode*  n = text->item(j);
3024             XMLString::transcode(textCompare[j], tempStr, 3999);
3025             if (XMLString::compareString(tempStr, n->getNodeValue()))
3026             {
3027                 fprintf(stderr, "Warning!!! Comparison of original text nodes via DOMNode*  'getChildNodes' & DOMNodeList 'item'\n"
3028                         "     failed at text node: #%llu at line %i \n     ",
3029                         (unsigned long long) j, __LINE__ );
3030                 OK = false;
3031                 break;
3032             }
3033             //  printf("DOMElement* testBodyLevel31's child text node " + j + " is: " + n->getNodeValue());
3034         }
3035     }
3036 
3037     element = document->getDocumentElement(); // element gets doc's firstElement
3038     element->normalize();        // Concatenates all adjacent text nodes in this element's subtree
3039     DOMNodeList* text2 = ((DOMNode*) element2)->getChildNodes();
3040     XMLString::transcode("dBodyLevel31'sChildTextNode11dBodyLevel31'sChildTextNode12dBodyLevel31'sChildTextNode13", tempStr, 3999);
3041     DOMNode*  n = text2->item(0);
3042     if (XMLString::compareString(tempStr, n->getNodeValue()))
3043     {
3044         fprintf(stderr, "Warning!!! Comparison of concatenated text nodes created by DOMElement's 'normalize' failed!\n");
3045         OK = false;
3046     }
3047 
3048     XMLString::transcode("FirstElementLastAttribute", tempStr, 3999);
3049     XMLString::transcode("More attribute stuff for firstElement!!", tempStr2, 3999);
3050     element->setAttribute(tempStr, tempStr2);
3051 
3052     XMLString::transcode("FirstElementLastAttribute", tempStr, 3999);
3053     element->removeAttribute(tempStr);
3054     rem = element->removeAttributeNode(newAttributeNode);
3055     if (rem)
3056         rem->release();
3057 
3058     //  doc->getLastChild()->setNodeValue("This shouldn't work");//!! Throws a NO_MODIFICATION_ALLOWED_ERR***
3059 
3060     // Test the user data
3061     // Test simple set and get
3062     DOMElement* userTest = element;
3063     DOMAttr*  userFirst = newAttributeNode;
3064     XMLCh* userSecond = tempStr2;
3065     XMLString::transcode("first", tempStr, 3999);
3066 
3067     XMLString::transcode("document", tempStr2, 3999);
3068     userTest->setUserData(tempStr2, (void*) document, 0);
3069     void* mydocument = userTest->getUserData(tempStr2);
3070     if (document != mydocument) {
3071         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3072         OK = false;
3073     }
3074 
3075     userTest->setUserData(tempStr, (void*) userFirst, 0);
3076     void* myFirst = userTest->getUserData(tempStr);
3077     if (userFirst != myFirst) {
3078         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3079         OK = false;
3080     }
3081 
3082     // Test overwrite
3083     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
3084     void* mySecond = userTest->getUserData(tempStr);
3085     if (userSecond != mySecond) {
3086         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3087         OK = false;
3088     }
3089     if (userFirst != myFirst2) {
3090         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3091         OK = false;
3092     }
3093     if (userFirst == mySecond) {
3094         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3095         OK = false;
3096     }
3097 
3098     // Test null
3099     // test non-exist key
3100     XMLString::transcode("not-exist", tempStr3, 3999);
3101     if (userTest->getUserData(tempStr3)) {
3102         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3103         OK = false;
3104     }
3105 
3106     // use a node that does not have user data set before
3107     if (userFirst->getUserData(tempStr)) {
3108         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3109         OK = false;
3110     }
3111 
3112     // Test reset
3113     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
3114     void* myNull = userTest->getUserData(tempStr);
3115     if (userSecond != mySecond2) {
3116         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3117         OK = false;
3118     }
3119     if (myNull) {
3120         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3121         OK = false;
3122     }
3123 
3124     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
3125     void* myNull2 = userTest->getUserData(tempStr2);
3126     if (mydocument != mydocument2) {
3127         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3128         OK = false;
3129     }
3130     if (myNull2) {
3131         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3132         OK = false;
3133     }
3134 
3135     //the userTest user data table should be null now
3136     if (userTest->getUserData(tempStr)) {
3137         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3138         OK = false;
3139     }
3140     if (userTest->getUserData(tempStr2)) {
3141         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3142         OK = false;
3143     }
3144 
3145     // Test DOMUserDataHandler
3146     // test clone
3147     userTest->setUserData(tempStr2, (void*) document, &userhandler);
3148     DOMNode* mycloned = userTest->cloneNode(true);
3149     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
3150 
3151     // test import
3152     document->setUserData(tempStr2, (void*) document, &userhandler);
3153     DOMNode* myimport = document->importNode(userTest,true);
3154     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
3155 
3156     // test delete
3157     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
3158     myimport->release();
3159     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
3160 
3161     // Test isSameNode
3162     if (!userTest->isSameNode(userTest)) {
3163         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3164         OK = false;
3165     }
3166     if (userTest->isSameNode(userFirst)) {
3167         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3168         OK = false;
3169     }
3170 
3171     // Test isEqualNode
3172     if (!userTest->isEqualNode(mycloned)) {
3173         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3174         OK = false;
3175     }
3176 
3177     if (!userTest->isEqualNode(userTest)) {
3178         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3179         OK = false;
3180     }
3181     if (userTest->isEqualNode(userFirst)) {
3182         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3183         OK = false;
3184     }
3185 
3186     // Test renameNode
3187     XMLString::transcode("http://nsa", tempStr4, 3999);
3188     XMLString::transcode("aa", tempStr5, 3999);
3189     XMLString::transcode("pnsa:aa", tempStr3, 3999);
3190 
3191     // create the element
3192     DOMElement* renameTestElement = document->createElement(tempStr5);
3193     DOMElement* renameTestElementNS = document->createElementNS(tempStr4, tempStr3);
3194 
3195     // create the parent
3196     DOMElement* renameTestParent = document->createElement(tempStr5);
3197     renameTestParent->appendChild(renameTestElement);
3198     renameTestParent->appendChild(renameTestElementNS);
3199 
3200     // set up the userdata
3201     renameTestElement->setUserData(tempStr5, (void*) document, &userhandler);
3202     renameTestElementNS->setUserData(tempStr4, (void*) document, 0);
3203 
3204     // append a text node as child
3205     DOMText* renameTestText = document->createTextNode(tempStr5);
3206     DOMText* renameTestTextNS = document->createTextNode(tempStr4);
3207     renameTestElement->appendChild(renameTestText);
3208     renameTestElementNS->appendChild(renameTestTextNS);
3209 
3210     XMLString::transcode("http://nsb", tempStr, 3999);
3211     XMLString::transcode("bb", tempStr2, 3999);
3212     XMLString::transcode("pnsb:bb", tempStr3, 3999);
3213 
3214     // set up some attributes
3215     DOMAttr* renameTestAttribute = document->createAttribute(tempStr5);
3216     DOMAttr* renameTestAttributeNS = document->createAttributeNS(tempStr4, tempStr3);
3217     renameTestElement->setAttributeNode(renameTestAttribute);
3218     renameTestElementNS->setAttributeNodeNS(renameTestAttributeNS);
3219 
3220     //Test compareDocumentPosition first before testing rename
3221     // renameTestParent
3222     //  |
3223     //  |_ renameTestElement (has renameTestAttribute)
3224     //  |          |
3225     //  |          |_ renameTestText
3226     //  |
3227     //  |_ renameTestElementNS (has renameTestAttributeNS)
3228     //  |          |
3229     //  |          |_ renameTestTextNS
3230     //
3231     COMPARETREEPOSITIONTEST(renameTestAttribute, renameTestAttributeNS, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
3232     COMPARETREEPOSITIONTEST(renameTestAttribute, renameTestElement, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
3233     COMPARETREEPOSITIONTEST(renameTestAttribute, renameTestText, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
3234     COMPARETREEPOSITIONTEST(renameTestAttribute, renameTestTextNS, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
3235     COMPARETREEPOSITIONTEST(renameTestAttribute, renameTestParent, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
3236 
3237     COMPARETREEPOSITIONTEST(renameTestAttributeNS, renameTestAttribute, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
3238     COMPARETREEPOSITIONTEST(renameTestElement, renameTestAttributeNS, DOMNode::DOCUMENT_POSITION_FOLLOWING, __LINE__);
3239     COMPARETREEPOSITIONTEST(renameTestAttributeNS, renameTestText, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
3240     COMPARETREEPOSITIONTEST(renameTestTextNS, renameTestAttributeNS, DOMNode::DOCUMENT_POSITION_PRECEDING, __LINE__);
3241 
3242     // start the rename tests
3243     // rename the NS Element
3244     DOMElement* renameTest = (DOMElement*) document->renameNode(renameTestElementNS, tempStr, tempStr3);
3245     // test the name
3246     if (XMLString::compareString(tempStr, renameTest->getNamespaceURI()))
3247     {
3248         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3249         OK = false;
3250     }
3251     if (XMLString::compareString(tempStr2, renameTest->getLocalName()))
3252     {
3253         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3254         OK = false;
3255     }
3256     if (XMLString::compareString(tempStr3, renameTest->getNodeName()))
3257     {
3258         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3259         OK = false;
3260     }
3261     // test the child / nodevalue
3262     if (XMLString::compareString(tempStr4, renameTest->getFirstChild()->getNodeValue()))
3263     {
3264         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3265         OK = false;
3266     }
3267     // test the attribute
3268     if (!renameTest->getAttributeNodeNS(tempStr4, tempStr2))
3269     {
3270         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3271         OK = false;
3272     }
3273     // test the owner element
3274     if (renameTestParent->getElementsByTagNameNS(tempStr, tempStr2)->getLength() != 1) {
3275         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3276         OK = false;
3277     }
3278     if (renameTestParent->getElementsByTagNameNS(tempStr4, tempStr5)->getLength() != 0) {
3279         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3280         OK = false;
3281     }
3282     // test user data
3283     void* renamedocument = renameTest->getUserData(tempStr4);
3284     if (document != renamedocument) {
3285         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3286         OK = false;
3287     }
3288     // test isSame and isEqual
3289     if (!renameTestElementNS->isEqualNode(renameTest)) {
3290         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3291         OK = false;
3292     }
3293     if (!renameTestElementNS->isSameNode(renameTest)) {
3294         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3295         OK = false;
3296     }
3297 
3298 
3299     // rename the Element (null namespace)
3300     renameTest = (DOMElement*) document->renameNode(renameTestElement, 0, tempStr2);
3301     // test the name
3302     if (renameTest->getNamespaceURI())
3303     {
3304         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3305         OK = false;
3306     }
3307     if (renameTest->getLocalName())
3308     {
3309         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3310         OK = false;
3311     }
3312     if (XMLString::compareString(tempStr2, renameTest->getNodeName()))
3313     {
3314         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3315         OK = false;
3316     }
3317     // test the child / nodevalue
3318     if (XMLString::compareString(tempStr5, renameTest->getFirstChild()->getNodeValue()))
3319     {
3320         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3321         OK = false;
3322     }
3323     // test the attribute
3324     if (!renameTest->getAttributeNode(tempStr5))
3325     {
3326         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3327         OK = false;
3328     }
3329     // test the owner element
3330     if (renameTestParent->getElementsByTagName(tempStr2)->getLength() != 1) {
3331         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3332         OK = false;
3333     }
3334     if (renameTestParent->getElementsByTagName(tempStr5)->getLength() != 0) {
3335         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3336         OK = false;
3337     }
3338     // test user data
3339     renamedocument = renameTest->getUserData(tempStr5);
3340     if (document != renamedocument) {
3341         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3342         OK = false;
3343     }
3344     // test isSame and isEqual
3345     if (!renameTestElement->isEqualNode(renameTest)) {
3346         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3347         OK = false;
3348     }
3349     if (!renameTestElement->isSameNode(renameTest)) {
3350         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3351         OK = false;
3352     }
3353 
3354 
3355     // rename the Element (with namespace)
3356     renameTest = (DOMElement*) document->renameNode(renameTestElement, tempStr, tempStr3);
3357     // test the name
3358     if (XMLString::compareString(tempStr, renameTest->getNamespaceURI()))
3359     {
3360         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3361         OK = false;
3362     }
3363     if (XMLString::compareString(tempStr2, renameTest->getLocalName()))
3364     {
3365         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3366         OK = false;
3367     }
3368     if (XMLString::compareString(tempStr3, renameTest->getNodeName()))
3369     {
3370         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3371         OK = false;
3372     }
3373     // test the child / nodevalue
3374     if (XMLString::compareString(tempStr5, renameTest->getFirstChild()->getNodeValue()))
3375     {
3376         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3377         OK = false;
3378     }
3379     if (renameTestElement->getFirstChild())
3380     {
3381         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3382         OK = false;
3383     }
3384     // test the attribute
3385     if (!renameTest->getAttributeNode(tempStr5))
3386     {
3387         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3388         OK = false;
3389     }
3390     if (renameTestElement->getAttributeNode(tempStr5))
3391     {
3392         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3393         OK = false;
3394     }
3395     // test the owner element
3396     // the nodelist should be 2 items as we have to count the renameTestElementNS as well
3397     if (renameTestParent->getElementsByTagNameNS(tempStr, tempStr2)->getLength() != 2) {
3398         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3399         OK = false;
3400     }
3401     if (renameTestParent->getElementsByTagNameNS(0, tempStr2)->getLength() != 0) {
3402         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3403         OK = false;
3404     }
3405     if (renameTestParent->getElementsByTagName(tempStr2)->getLength() != 0) {
3406         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3407         OK = false;
3408     }
3409     // test user data
3410     renamedocument = renameTest->getUserData(tempStr5);
3411     if (document != renamedocument) {
3412         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3413         OK = false;
3414     }
3415     // test userdatahandler
3416     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_RENAMED, tempStr5, document, renameTestElement, renameTest, __LINE__);
3417     // test isSame and isEqual
3418     // a new node is created here, so both isSame and isEqual are not compared
3419     if (renameTestElement->isEqualNode(renameTest)) {
3420         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3421         OK = false;
3422     }
3423     if (renameTestElement->isSameNode(renameTest)) {
3424         fprintf(stderr, "renameNode failed in line %i\n", __LINE__);
3425         OK = false;
3426     }
3427 
3428 
3429     //setIdAttribute tests
3430 
3431     XMLString::transcode("http://nsa", tempStr4, 3999);
3432     XMLString::transcode("aa", tempStr5, 3999);
3433 
3434     DOMAttr *idAtt = document->createAttributeNS(tempStr4, tempStr5);
3435 
3436     //tests for node not being on testElementNode
3437     EXCEPTIONSTEST(testElementNode->setIdAttribute(tempStr4, true), DOMException::NOT_FOUND_ERR, OK,  1000);
3438     EXCEPTIONSTEST(testElementNode->setIdAttributeNS(tempStr4, tempStr5, true), DOMException::NOT_FOUND_ERR, OK,  1001);
3439     EXCEPTIONSTEST(testElementNode->setIdAttributeNode(idAtt, true), DOMException::NOT_FOUND_ERR, OK,  1002);
3440 
3441     //should test NO_MODIFICATION_ALLOWED_ERR but dont know how to without direct access to DOMAttrImpl.
3442 
3443     idAtt = document->createAttributeNS(tempStr4, tempStr5);
3444     idAtt->setValue(tempStr3);
3445     testElementNode->setAttributeNode(idAtt);
3446     testElementNode->setIdAttributeNode(idAtt, true);
3447 
3448     if(!idAtt->isId()) {
3449         fprintf(stderr, "setIdAttributeNode failed in line %i\n", __LINE__);
3450         OK = false;
3451     }
3452 
3453     DOMElement *idEle = document->getElementById(tempStr3);
3454 
3455     if(!idEle || !idEle->isSameNode(testElementNode)) {
3456         fprintf(stderr, "setIdAttributeNode failed in line %i\n", __LINE__);
3457         OK = false;
3458     }
3459 
3460     testElementNode->setIdAttributeNode(idAtt, false);
3461 
3462     if(idAtt->isId()) {
3463         fprintf(stderr, "setIdAttributeNode failed in line %i\n", __LINE__);
3464         OK = false;
3465     }
3466 
3467     idEle = document->getElementById(tempStr3);
3468 
3469     if(idEle) {
3470         fprintf(stderr, "setIdAttributeNode failed in line %i\n", __LINE__);
3471         OK = false;
3472     }
3473 
3474     testElementNode->removeAttributeNode(idAtt);
3475 
3476 
3477     XMLString::transcode("someval", tempStr3, 3999);
3478     idAtt = document->createAttributeNS(tempStr4, tempStr5);
3479     idAtt->setValue(tempStr3);
3480     testElementNode->setAttributeNode(idAtt);
3481     testElementNode->setIdAttributeNS(tempStr4, tempStr5, true);
3482 
3483     if(!idAtt->isId()) {
3484         fprintf(stderr, "setIdAttributeNS failed in line %i\n", __LINE__);
3485         OK = false;
3486     }
3487 
3488     idEle = document->getElementById(tempStr3);
3489 
3490     if(!idEle || !idEle->isSameNode(testElementNode)) {
3491         fprintf(stderr, "setIdAttributeNS failed in line %i\n", __LINE__);
3492         OK = false;
3493     }
3494 
3495     testElementNode->setIdAttributeNS(tempStr4, tempStr5, false);
3496 
3497     if(idAtt->isId()) {
3498         fprintf(stderr, "setIdAttributeNS failed in line %i\n", __LINE__);
3499         OK = false;
3500     }
3501 
3502     idEle = document->getElementById(tempStr3);
3503 
3504     if(idEle) {
3505         fprintf(stderr, "setIdAttributeNS failed in line %i\n", __LINE__);
3506         OK = false;
3507     }
3508 
3509     testElementNode->removeAttributeNode(idAtt);
3510     idAtt->release();
3511 
3512 
3513     XMLString::transcode("somevalDif", tempStr3, 3999);
3514     idAtt = document->createAttribute(tempStr5);
3515     idAtt->setValue(tempStr3);
3516     testElementNode->setAttributeNode(idAtt);
3517     testElementNode->setIdAttribute(tempStr5, true);
3518 
3519     if(!idAtt->isId()) {
3520         fprintf(stderr, "setIdAttribute failed in line %i\n", __LINE__);
3521         OK = false;
3522     }
3523 
3524     idEle = document->getElementById(tempStr3);
3525 
3526     if(!idEle || !idEle->isSameNode(testElementNode)) {
3527         fprintf(stderr, "setIdAttribute failed in line %i\n", __LINE__);
3528         OK = false;
3529     }
3530 
3531     testElementNode->setIdAttribute(tempStr5, false);
3532 
3533     if(idAtt->isId()) {
3534         fprintf(stderr, "setIdAttribute failed in line %i\n", __LINE__);
3535         OK = false;
3536     }
3537 
3538     idEle = document->getElementById(tempStr3);
3539 
3540     if(idEle) {
3541         fprintf(stderr, "setIdAttribute failed in line %i\n", __LINE__);
3542         OK = false;
3543     }
3544 
3545     testElementNode->removeAttributeNode(idAtt);
3546     idAtt->release();
3547 
3548     if (!OK)
3549         printf("\n*****The DOMElement* method calls listed above failed, all others worked correctly.*****\n");
3550     return OK;
3551 }
3552 
3553 
3554 
3555 /**
3556  * This method tests DOMEntity* methods for the XML DOM implementation
3557  * @param document org.w3c.dom.DOMDocument
3558  *
3559  */
testEntity(DOMDocument * document)3560 bool DOMTest::testEntity(DOMDocument* document)
3561 {
3562     DOMEntity* entity;
3563     DOMNode* node, *node2;
3564     bool OK = true;
3565 // For debugging*****   printf("\n          testEntity's outputs:\n\n");
3566     XMLString::transcode("ourEntityNode", tempStr, 3999);
3567     DOMNode*  abc20 = document->getDoctype()->getEntities()->getNamedItem(tempStr);
3568     entity = (DOMEntity*) abc20;
3569     node = entity;
3570     node2 = entity->cloneNode(true);
3571     // Check nodes for equality, both their name and value or lack thereof
3572     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
3573           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
3574         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
3575         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
3576     {
3577         fprintf(stderr, "'cloneNode' did not clone the DOMEntity* node correctly");
3578         OK = false;
3579     }
3580     // Deep clone test comparison is in testNode & testDocument
3581 
3582     // Test the user data
3583     // Test simple set and get
3584     DOMEntity* userTest = entity;
3585     DOMNode*  userFirst = node;
3586     XMLCh* userSecond = tempStr2;
3587     XMLString::transcode("first", tempStr, 3999);
3588 
3589     XMLString::transcode("document", tempStr2, 3999);
3590     userTest->setUserData(tempStr2, (void*) document, 0);
3591     void* mydocument = userTest->getUserData(tempStr2);
3592     if (document != mydocument) {
3593         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3594         OK = false;
3595     }
3596 
3597     userTest->setUserData(tempStr, (void*) userFirst, 0);
3598     void* myFirst = userTest->getUserData(tempStr);
3599     if (userFirst != myFirst) {
3600         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3601         OK = false;
3602     }
3603 
3604     // Test overwrite
3605     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
3606     void* mySecond = userTest->getUserData(tempStr);
3607     if (userSecond != mySecond) {
3608         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3609         OK = false;
3610     }
3611     if (userFirst != myFirst2) {
3612         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3613         OK = false;
3614     }
3615     if (userFirst == mySecond) {
3616         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3617         OK = false;
3618     }
3619 
3620     // Test null
3621     // test non-exist key
3622     XMLString::transcode("not-exist", tempStr3, 3999);
3623     if (userTest->getUserData(tempStr3)) {
3624         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3625         OK = false;
3626     }
3627 
3628     // use a node that does not have user data set before
3629     if (userFirst->getUserData(tempStr)) {
3630         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3631         OK = false;
3632     }
3633 
3634     // Test reset
3635     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
3636     void* myNull = userTest->getUserData(tempStr);
3637     if (userSecond != mySecond2) {
3638         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3639         OK = false;
3640     }
3641     if (myNull) {
3642         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3643         OK = false;
3644     }
3645 
3646     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
3647     void* myNull2 = userTest->getUserData(tempStr2);
3648     if (mydocument != mydocument2) {
3649         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3650         OK = false;
3651     }
3652     if (myNull2) {
3653         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3654         OK = false;
3655     }
3656 
3657     //the userTest user data table should be null now
3658     if (userTest->getUserData(tempStr)) {
3659         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3660         OK = false;
3661     }
3662     if (userTest->getUserData(tempStr2)) {
3663         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3664         OK = false;
3665     }
3666 
3667     // Test DOMUserDataHandler
3668     // test clone
3669     userTest->setUserData(tempStr2, (void*) document, &userhandler);
3670     DOMNode* mycloned = userTest->cloneNode(true);
3671     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
3672 
3673     // test import
3674     document->setUserData(tempStr2, (void*) document, &userhandler);
3675     DOMNode* myimport = document->importNode(userTest,true);
3676     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
3677 
3678     // test delete
3679     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
3680     myimport->release();
3681     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
3682 
3683     // Test isSameNode
3684     if (!userTest->isSameNode(userTest)) {
3685         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3686         OK = false;
3687     }
3688     if (userTest->isSameNode(userFirst)) {
3689         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3690         OK = false;
3691     }
3692 
3693     // Test isEqualNode
3694     if (!userTest->isEqualNode(mycloned)) {
3695         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3696         OK = false;
3697     }
3698 
3699     if (!userTest->isEqualNode(userTest)) {
3700         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3701         OK = false;
3702     }
3703     if (userTest->isEqualNode(userFirst)) {
3704         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3705         OK = false;
3706     }
3707 
3708     // Test rename, should throw exception
3709     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 210 );
3710 
3711     if (!OK)
3712         printf("\n*****The DOMEntity* method calls listed above failed, all others worked correctly.*****\n");
3713     return OK;
3714 }
3715 
3716 
3717 /**
3718  * This method tests DOMEntityReference* methods for the XML DOM implementation
3719  * @param document org.w3c.dom.DOMDocument
3720  *
3721  */
testEntityReference(DOMDocument * document)3722 bool DOMTest::testEntityReference(DOMDocument* document)
3723 {
3724     DOMEntityReference* entityReference;
3725     DOMNode* node, *node2;
3726     bool OK = true;
3727 // For debugging*****   printf("\n          testEntityReference's outputs:\n");
3728     DOMNode*  abc30 = document->getLastChild()->getLastChild()->getLastChild()->getFirstChild();
3729     entityReference = (DOMEntityReference*) abc30;
3730     node = entityReference;
3731     node2 = node->cloneNode(true);
3732     // Check nodes for equality, both their name and value or lack thereof
3733     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
3734           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
3735         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
3736         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
3737     {
3738         fprintf(stderr, "'cloneNode' did not clone the DOMEntityReference* node correctly\n");
3739         OK = false;
3740     }
3741     // Deep clone test comparison is in testNode & testDocument
3742 
3743     //  entityReference->setNodeValue("This shouldn't work");//!! Throws a NO_MODIFICATION_ALLOWED_ERR ********
3744 
3745     // Test the user data
3746     // Test simple set and get
3747     DOMEntityReference* userTest = entityReference;
3748     DOMNode*  userFirst = node2;
3749     XMLCh* userSecond = tempStr2;
3750     XMLString::transcode("first", tempStr, 3999);
3751 
3752     XMLString::transcode("document", tempStr2, 3999);
3753     userTest->setUserData(tempStr2, (void*) document, 0);
3754     void* mydocument = userTest->getUserData(tempStr2);
3755     if (document != mydocument) {
3756         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3757         OK = false;
3758     }
3759 
3760     userTest->setUserData(tempStr, (void*) userFirst, 0);
3761     void* myFirst = userTest->getUserData(tempStr);
3762     if (userFirst != myFirst) {
3763         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3764         OK = false;
3765     }
3766 
3767     // Test overwrite
3768     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
3769     void* mySecond = userTest->getUserData(tempStr);
3770     if (userSecond != mySecond) {
3771         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3772         OK = false;
3773     }
3774     if (userFirst != myFirst2) {
3775         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3776         OK = false;
3777     }
3778     if (userFirst == mySecond) {
3779         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3780         OK = false;
3781     }
3782 
3783     // Test null
3784     // test non-exist key
3785     XMLString::transcode("not-exist", tempStr3, 3999);
3786     if (userTest->getUserData(tempStr3)) {
3787         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3788         OK = false;
3789     }
3790 
3791     // use a node that does not have user data set before
3792     if (node2->getUserData(tempStr)) {
3793         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3794         OK = false;
3795     }
3796 
3797     // Test reset
3798     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
3799     void* myNull = userTest->getUserData(tempStr);
3800     if (userSecond != mySecond2) {
3801         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3802         OK = false;
3803     }
3804     if (myNull) {
3805         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3806         OK = false;
3807     }
3808 
3809     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
3810     void* myNull2 = userTest->getUserData(tempStr2);
3811     if (mydocument != mydocument2) {
3812         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3813         OK = false;
3814     }
3815     if (myNull2) {
3816         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3817         OK = false;
3818     }
3819 
3820     //the userTest user data table should be null now
3821     if (userTest->getUserData(tempStr)) {
3822         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3823         OK = false;
3824     }
3825     if (userTest->getUserData(tempStr2)) {
3826         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3827         OK = false;
3828     }
3829 
3830     // Test DOMUserDataHandler
3831     // test clone
3832     userTest->setUserData(tempStr2, (void*) document, &userhandler);
3833     DOMNode* mycloned = userTest->cloneNode(true);
3834     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
3835 
3836     // test import
3837     document->setUserData(tempStr2, (void*) document, &userhandler);
3838     DOMNode* myimport = document->importNode(userTest,true);
3839     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
3840 
3841     // test delete
3842     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
3843     myimport->release();
3844     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
3845 
3846     // Test isSameNode
3847     if (!userTest->isSameNode(userTest)) {
3848         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3849         OK = false;
3850     }
3851     if (userTest->isSameNode(userFirst)) {
3852         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
3853         OK = false;
3854     }
3855 
3856     // Test isEqualNode
3857     if (!userTest->isEqualNode(mycloned)) {
3858         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3859         OK = false;
3860     }
3861 
3862     if (!userTest->isEqualNode(userTest)) {
3863         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3864         OK = false;
3865     }
3866     if (userTest->isEqualNode(document)) {
3867         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
3868         OK = false;
3869     }
3870 
3871     // Test rename, should throw exception
3872     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 211 );
3873 
3874     if (!OK)
3875         printf("\n*****The DOMEntityReference* method calls listed above failed, all others worked correctly.*****\n");
3876     return OK;
3877 }
3878 
3879 
3880 
3881 /**
3882  * This method tests DOMNode*  methods for the XML DOM implementation
3883  * @param document org.w3c.dom.DOMDocument
3884  *
3885  *
3886  *
3887  ********* This is only for a test of cloneNode "deep"*******
3888  ********* And for error tests*********
3889  */
testNode(DOMDocument * document)3890 bool DOMTest::testNode(DOMDocument* document)
3891 {
3892     DOMNode* node, *node2;
3893     bool result;
3894     bool OK = true;
3895 // For debugging*****   printf("\n          testNode's outputs:\n");
3896     node = document->getDocumentElement();
3897     node2 = node->cloneNode(true);
3898     result = treeCompare(node, node2); // Deep clone test of cloneNode
3899     if (result)
3900     {
3901         //printf("'cloneNode' successfully cloned this whole node tree (deep)!\n");
3902     }
3903     else
3904     {
3905         fprintf(stderr, "'cloneNode' did not successfully clone this whole node tree (deep)!\n");
3906         OK = false;
3907     }
3908     //!! The following gives a did not clone successfully message*********
3909     node = document->getDocumentElement();
3910     node2 = node->getFirstChild();
3911     result = treeCompare(node, node2);
3912     if (!result)
3913     {
3914         //fprintf(stderr, "'cloneNode' did not successfully clone this whole node tree (deep)!\n");
3915     }
3916     else
3917     {
3918         fprintf(stderr, "'cloneNode' was supposed to fail here, either it or 'treeCompare' failed!!!\n");
3919         OK = false;
3920     }
3921     // Deep clone test also in testDocument
3922 
3923     // Test the user data
3924     // Test simple set and get
3925     DOMNode* userTest = node;
3926     DOMNode*  userFirst = node2;
3927     XMLCh* userSecond = tempStr2;
3928     XMLString::transcode("first", tempStr, 3999);
3929 
3930     XMLString::transcode("document", tempStr2, 3999);
3931     userTest->setUserData(tempStr2, (void*) document, 0);
3932     void* mydocument = userTest->getUserData(tempStr2);
3933     if (document != mydocument) {
3934         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3935         OK = false;
3936     }
3937 
3938     userTest->setUserData(tempStr, (void*) userFirst, 0);
3939     void* myFirst = userTest->getUserData(tempStr);
3940     if (userFirst != myFirst) {
3941         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
3942         OK = false;
3943     }
3944 
3945     // Test overwrite
3946     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
3947     void* mySecond = userTest->getUserData(tempStr);
3948     if (userSecond != mySecond) {
3949         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3950         OK = false;
3951     }
3952     if (userFirst != myFirst2) {
3953         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3954         OK = false;
3955     }
3956     if (userFirst == mySecond) {
3957         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3958         OK = false;
3959     }
3960 
3961     // Test null
3962     // test non-exist key
3963     XMLString::transcode("not-exist", tempStr3, 3999);
3964     if (userTest->getUserData(tempStr3)) {
3965         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3966         OK = false;
3967     }
3968 
3969     // use a node that does not have user data set before
3970     if (userFirst->getUserData(tempStr)) {
3971         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
3972         OK = false;
3973     }
3974 
3975     // Test reset
3976     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
3977     void* myNull = userTest->getUserData(tempStr);
3978     if (userSecond != mySecond2) {
3979         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3980         OK = false;
3981     }
3982     if (myNull) {
3983         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3984         OK = false;
3985     }
3986 
3987     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
3988     void* myNull2 = userTest->getUserData(tempStr2);
3989     if (mydocument != mydocument2) {
3990         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3991         OK = false;
3992     }
3993     if (myNull2) {
3994         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
3995         OK = false;
3996     }
3997 
3998     //the userTest user data table should be null now
3999     if (userTest->getUserData(tempStr)) {
4000         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4001         OK = false;
4002     }
4003     if (userTest->getUserData(tempStr2)) {
4004         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4005         OK = false;
4006     }
4007 
4008     // Test DOMUserDataHandler
4009     // test clone
4010     userTest->setUserData(tempStr2, (void*) document, &userhandler);
4011     DOMNode* mycloned = userTest->cloneNode(true);
4012     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
4013 
4014     // test import
4015     document->setUserData(tempStr2, (void*) document, &userhandler);
4016     DOMNode* myimport = document->importNode(userTest,true);
4017     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
4018 
4019     // test delete
4020     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
4021     myimport->release();
4022     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
4023 
4024     // Test isSameNode
4025     if (!userTest->isSameNode(userTest)) {
4026         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4027         OK = false;
4028     }
4029     if (userTest->isSameNode(userFirst)) {
4030         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4031         OK = false;
4032     }
4033 
4034     // Test isEqualNode
4035     if (!userTest->isEqualNode(mycloned)) {
4036         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4037         OK = false;
4038     }
4039 
4040     if (!userTest->isEqualNode(userTest)) {
4041         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4042         OK = false;
4043     }
4044     if (userTest->isEqualNode(document)) {
4045         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4046         OK = false;
4047     }
4048 
4049     if (!OK)
4050         printf("\n*****The DOMNode*  method calls listed above failed, all others worked correctly.*****\n");
4051     return OK;
4052 }
4053 
4054 
4055 
4056 /**
4057  * This method tests DOMNotation* methods for the XML DOM implementation
4058  * @param document org.w3c.dom.DOMDocument
4059  *
4060  */
testNotation(DOMDocument * document)4061 bool DOMTest::testNotation(DOMDocument* document)
4062 {
4063     DOMNode* node, *node2;
4064     DOMNotation* notation;
4065     bool OK = true;
4066 // For debugging*****   printf("\n          testNotation's outputs:\n");
4067     XMLString::transcode("ourNotationNode", tempStr, 3999);
4068 	DOMNode*  abc40 = document->getDoctype()->getNotations()->getNamedItem(tempStr);
4069     notation = (DOMNotation*) abc40;
4070     node = notation;
4071     node2 = notation->cloneNode(true);//*****?
4072     // Check nodes for equality, both their name and value or lack thereof
4073     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
4074           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
4075         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
4076         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
4077     {
4078         fprintf(stderr, "'cloneNode' did not clone the DOMNotation* node correctly");
4079         OK = false;
4080     }
4081     // Deep clone test comparison is in testNode & testDocument
4082 
4083     //  notation->setNodeValue("This shouldn't work");//!! Throws a NO_MODIFICATION_ALLOWED_ERR ********
4084 
4085     // Test the user data
4086     // Test simple set and get
4087     DOMNotation* userTest = notation;
4088     DOMNode*  userFirst = node2;
4089     XMLCh* userSecond = tempStr2;
4090     XMLString::transcode("first", tempStr, 3999);
4091 
4092     XMLString::transcode("document", tempStr2, 3999);
4093     userTest->setUserData(tempStr2, (void*) document, 0);
4094     void* mydocument = userTest->getUserData(tempStr2);
4095     if (document != mydocument) {
4096         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4097         OK = false;
4098     }
4099 
4100     userTest->setUserData(tempStr, (void*) userFirst, 0);
4101     void* myFirst = userTest->getUserData(tempStr);
4102     if (userFirst != myFirst) {
4103         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4104         OK = false;
4105     }
4106 
4107     // Test overwrite
4108     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
4109     void* mySecond = userTest->getUserData(tempStr);
4110     if (userSecond != mySecond) {
4111         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4112         OK = false;
4113     }
4114     if (userFirst != myFirst2) {
4115         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4116         OK = false;
4117     }
4118     if (userFirst == mySecond) {
4119         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4120         OK = false;
4121     }
4122 
4123     // Test null
4124     // test non-exist key
4125     XMLString::transcode("not-exist", tempStr3, 3999);
4126     if (userTest->getUserData(tempStr3)) {
4127         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4128         OK = false;
4129     }
4130 
4131     // use a node that does not have user data set before
4132     if (userFirst->getUserData(tempStr)) {
4133         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4134         OK = false;
4135     }
4136 
4137     // Test reset
4138     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
4139     void* myNull = userTest->getUserData(tempStr);
4140     if (userSecond != mySecond2) {
4141         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4142         OK = false;
4143     }
4144     if (myNull) {
4145         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4146         OK = false;
4147     }
4148 
4149     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
4150     void* myNull2 = userTest->getUserData(tempStr2);
4151     if (mydocument != mydocument2) {
4152         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4153         OK = false;
4154     }
4155     if (myNull2) {
4156         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4157         OK = false;
4158     }
4159 
4160     //the userTest user data table should be null now
4161     if (userTest->getUserData(tempStr)) {
4162         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4163         OK = false;
4164     }
4165     if (userTest->getUserData(tempStr2)) {
4166         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4167         OK = false;
4168     }
4169 
4170     // Test DOMUserDataHandler
4171     // test clone
4172     userTest->setUserData(tempStr2, (void*) document, &userhandler);
4173     DOMNode* mycloned = userTest->cloneNode(true);
4174     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
4175 
4176     // test import
4177     document->setUserData(tempStr2, (void*) document, &userhandler);
4178     DOMNode* myimport = document->importNode(userTest,true);
4179     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
4180 
4181     // test delete
4182     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
4183     myimport->release();
4184     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
4185 
4186     // Test isSameNode
4187     if (!userTest->isSameNode(userTest)) {
4188         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4189         OK = false;
4190     }
4191     if (userTest->isSameNode(userFirst)) {
4192         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4193         OK = false;
4194     }
4195 
4196     // Test isEqualNode
4197     if (!userTest->isEqualNode(mycloned)) {
4198         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4199         OK = false;
4200     }
4201 
4202     if (!userTest->isEqualNode(userTest)) {
4203         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4204         OK = false;
4205     }
4206     if (userTest->isEqualNode(document)) {
4207         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4208         OK = false;
4209     }
4210 
4211     // Test rename, should throw exception
4212     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 212 );
4213 
4214     if (!OK)
4215         printf("\n*****The DOMNotation* method calls listed above failed, all others worked correctly.*****\n");
4216     return OK;
4217 }
4218 
4219 
4220 
4221 /**
4222  * This method tests DOMProcessingInstruction* methods for the XML DOM implementation
4223  * @param document org.w3c.dom.DOMDocument
4224  *
4225  */
testPI(DOMDocument * document)4226 bool DOMTest::testPI(DOMDocument* document)
4227 {
4228     DOMProcessingInstruction* pI, *pI2;
4229     bool OK = true;
4230 // For debugging*****   printf("\n          testPI's outputs:\n");
4231 	DOMNode*   abc50 = document->getDocumentElement()->getFirstChild();// Get doc's DOMProcessingInstruction
4232     pI  = (DOMProcessingInstruction*) abc50;
4233 	DOMNode*   abc51 = pI->cloneNode(true);//*****?
4234     pI2 = (DOMProcessingInstruction*) abc51;
4235     // Check nodes for equality, both their name and value or lack thereof
4236     if (!(!XMLString::compareString(pI->getNodeName(), pI2->getNodeName()) &&         // Compares node names for equality
4237          (pI->getNodeValue() != 0 && pI2->getNodeValue() != 0)  // Checks to make sure each node has a value node
4238         ? !XMLString::compareString(pI->getNodeValue(), pI2->getNodeValue())      // If both have value nodes test those value nodes for equality
4239         :(pI->getNodeValue() == 0 && pI2->getNodeValue() == 0)))// If one node doesn't have a value node make sure both don't
4240     {
4241         fprintf(stderr, "'cloneNode' did not clone the DOMEntity* node correctly\n");
4242         OK = false;
4243     }
4244     // Deep clone test comparison is in testNode & testDocument
4245     // compare = "This is [#document: 0]'s processing instruction";  // AH Revisit.  Where id
4246     //                  this ": 0]" stuff come from in the Java version??  I don' think that it is right.
4247     XMLString::transcode("This is #document's processing instruction", tempStr, 3999);
4248     if (XMLString::compareString(tempStr, pI->getData()))
4249     {
4250         fprintf(stderr, "Warning!!! PI's 'getData' failed!\n");
4251         OK = false;
4252     }
4253 
4254     XMLString::transcode("PI's reset data", tempStr, 3999);
4255     pI->setData(tempStr);
4256     if (XMLString::compareString(tempStr, pI->getData()))
4257     {
4258         fprintf(stderr, "Warning!!! PI's 'setData' failed!\n");
4259         OK = false;
4260     }
4261     XMLString::transcode("dTargetProcessorChannel", tempStr, 3999);
4262     if (XMLString::compareString(tempStr, pI->getTarget()))
4263     {
4264         fprintf(stderr, "Warning!!! PI's 'getTarget' failed!\n");
4265         OK = false;
4266     }
4267 
4268 
4269     // Restore original PI data.
4270     XMLString::transcode("This is #document's processing instruction", tempStr, 3999);
4271     pI->setData(tempStr);
4272 
4273     // Test the user data
4274     // Test simple set and get
4275     DOMProcessingInstruction* userTest = pI;
4276     DOMNode*  userFirst = abc51;
4277     XMLCh* userSecond = tempStr2;
4278     XMLString::transcode("first", tempStr, 3999);
4279 
4280     XMLString::transcode("document", tempStr2, 3999);
4281     userTest->setUserData(tempStr2, (void*) document, 0);
4282     void* mydocument = userTest->getUserData(tempStr2);
4283     if (document != mydocument) {
4284         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4285         OK = false;
4286     }
4287 
4288     userTest->setUserData(tempStr, (void*) userFirst, 0);
4289     void* myFirst = userTest->getUserData(tempStr);
4290     if (userFirst != myFirst) {
4291         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4292         OK = false;
4293     }
4294 
4295     // Test overwrite
4296     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
4297     void* mySecond = userTest->getUserData(tempStr);
4298     if (userSecond != mySecond) {
4299         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4300         OK = false;
4301     }
4302     if (userFirst != myFirst2) {
4303         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4304         OK = false;
4305     }
4306     if (userFirst == mySecond) {
4307         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4308         OK = false;
4309     }
4310 
4311     // Test null
4312     // test non-exist key
4313     XMLString::transcode("not-exist", tempStr3, 3999);
4314     if (userTest->getUserData(tempStr3)) {
4315         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4316         OK = false;
4317     }
4318 
4319     // use a node that does not have user data set before
4320     if (abc51->getUserData(tempStr)) {
4321         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4322         OK = false;
4323     }
4324 
4325     // Test reset(reset)
4326     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
4327     void* myNull = userTest->getUserData(tempStr);
4328     if (userSecond != mySecond2) {
4329         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4330         OK = false;
4331     }
4332     if (myNull) {
4333         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4334         OK = false;
4335     }
4336 
4337     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
4338     void* myNull2 = userTest->getUserData(tempStr2);
4339     if (mydocument != mydocument2) {
4340         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4341         OK = false;
4342     }
4343     if (myNull2) {
4344         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4345         OK = false;
4346     }
4347 
4348     //the userTest user data table should be null now
4349     if (userTest->getUserData(tempStr)) {
4350         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4351         OK = false;
4352     }
4353     if (userTest->getUserData(tempStr2)) {
4354         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4355         OK = false;
4356     }
4357 
4358     // Test DOMUserDataHandler
4359     // test clone
4360     userTest->setUserData(tempStr2, (void*) document, &userhandler);
4361     DOMNode* mycloned = userTest->cloneNode(true);
4362     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
4363 
4364     // test import
4365     document->setUserData(tempStr2, (void*) document, &userhandler);
4366     DOMNode* myimport = document->importNode(userTest,true);
4367     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
4368 
4369     // test delete
4370     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
4371     myimport->release();
4372     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
4373 
4374     // Test isSameNode
4375     if (!userTest->isSameNode(userTest)) {
4376         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4377         OK = false;
4378     }
4379     if (userTest->isSameNode(userFirst)) {
4380         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4381         OK = false;
4382     }
4383 
4384     // Test isEqualNode
4385     if (!userTest->isEqualNode(mycloned)) {
4386         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4387         OK = false;
4388     }
4389 
4390     if (!userTest->isEqualNode(userTest)) {
4391         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4392         OK = false;
4393     }
4394     if (userTest->isEqualNode(document)) {
4395         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4396         OK = false;
4397     }
4398 
4399     // Test rename, should throw exception
4400     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 213 );
4401 
4402     if (!OK)
4403         printf("\n*****The PI method calls listed above failed, all others worked correctly.*****\n");
4404 
4405     return OK;
4406 }
4407 
4408 
4409 
4410 /**
4411  * This method tests DOMText* methods for the XML DOM implementation
4412  * @param document org.w3c.dom.DOMDocument
4413  *
4414  */
testText(DOMDocument * document)4415 bool DOMTest::testText(DOMDocument* document)
4416 {
4417     DOMNode* node, *node2;
4418     DOMText* text;
4419     bool OK = true;
4420 // For debugging*****   printf("\n          testText's outputs:\n");
4421     XMLString::transcode("dBodyLevel31", tempStr, 3999);
4422     DOMNode*  abc70 = document->getDocumentElement()->getElementsByTagName(tempStr)->item(0);
4423     DOMElement* elem = (DOMElement*) abc70;
4424     node = elem->getFirstChild(); // charData gets textNode11
4425     text = (DOMText*) node;
4426     node2 = node->cloneNode(true);//*****?
4427     // Check nodes for equality, both their name and value or lack thereof
4428     if (!(!XMLString::compareString(node->getNodeName(), node2->getNodeName()) &&        // Compares node names for equality
4429           (node->getNodeValue() != 0 && node2->getNodeValue() != 0)     // Checks to make sure each node has a value node
4430         ? !XMLString::compareString(node->getNodeValue(), node2->getNodeValue())         // If both have value nodes test those value nodes for equality
4431         : (node->getNodeValue() == 0 && node2->getNodeValue() == 0)))   // If one node doesn't have a value node make sure both don't
4432     {
4433         fprintf(stderr, "'cloneNode' did not clone the DOMText* node correctly\n");
4434         OK = false;
4435     }
4436     // Deep clone test comparison is in testNode & testDocument
4437 
4438     text->splitText(25);
4439     // Three original text nodes were concatenated by 'normalize' in testElement
4440     XMLString::transcode("dBodyLevel31'sChildTextNo", tempStr, 3999);
4441     if (XMLString::compareString(tempStr, text->getNodeValue()))
4442         {
4443             fprintf(stderr, "First part of DOMText's split text failed!\n" );
4444             OK = false;
4445         }
4446     // Three original text nodes were concatenated by 'normalize' in testElement
4447     XMLString::transcode("de11dBodyLevel31'sChildTextNode12dBodyLevel31'sChildTextNode13", tempStr, 3999);
4448     if (XMLString::compareString(tempStr, text->getNextSibling()->getNodeValue()))
4449         {
4450             fprintf(stderr, "The second part of DOMText's split text failed!\n") ;
4451             OK = false;
4452         }
4453 
4454     // Re-normalize the text nodes under elem, so that this test can be rerun->
4455     elem->normalize();
4456 
4457 
4458 //************************************************* ERROR TESTS
4459     DOMTest tests;
4460     //!! Throws INDEX_SIZE_ERR ********************
4461     //  text.splitText(-1);
4462     //  text.splitText(100);
4463 
4464     // Test the user data
4465     // Test simple set and get
4466     DOMText* userTest = text;
4467     DOMNode*  userFirst = node2;
4468     XMLCh* userSecond = tempStr2;
4469     XMLString::transcode("first", tempStr, 3999);
4470 
4471     XMLString::transcode("document", tempStr2, 3999);
4472     userTest->setUserData(tempStr2, (void*) document, 0);
4473     void* mydocument = userTest->getUserData(tempStr2);
4474     if (document != mydocument) {
4475         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4476         OK = false;
4477     }
4478 
4479     userTest->setUserData(tempStr, (void*) userFirst, 0);
4480     void* myFirst = userTest->getUserData(tempStr);
4481     if (userFirst != myFirst) {
4482         fprintf(stderr, "'set/getUserData' in line %i does not work\n", __LINE__);
4483         OK = false;
4484     }
4485 
4486     // Test overwrite
4487     void* myFirst2 = userTest->setUserData(tempStr, (void*) userSecond, 0);
4488     void* mySecond = userTest->getUserData(tempStr);
4489     if (userSecond != mySecond) {
4490         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4491         OK = false;
4492     }
4493     if (userFirst != myFirst2) {
4494         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4495         OK = false;
4496     }
4497     if (userFirst == mySecond) {
4498         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4499         OK = false;
4500     }
4501 
4502     // Test null
4503     // test non-exist key
4504     XMLString::transcode("not-exist", tempStr3, 3999);
4505     if (userTest->getUserData(tempStr3)) {
4506         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4507         OK = false;
4508     }
4509 
4510     // use a node that does not have user data set before
4511     if (userFirst->getUserData(tempStr)) {
4512         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4513         OK = false;
4514     }
4515 
4516     // Test reset
4517     void* mySecond2 = userTest->setUserData(tempStr, (void*) 0, 0);
4518     void* myNull = userTest->getUserData(tempStr);
4519     if (userSecond != mySecond2) {
4520         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4521         OK = false;
4522     }
4523     if (myNull) {
4524         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4525         OK = false;
4526     }
4527 
4528     void* mydocument2 = userTest->setUserData(tempStr2, (void*) 0, 0);
4529     void* myNull2 = userTest->getUserData(tempStr2);
4530     if (mydocument != mydocument2) {
4531         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4532         OK = false;
4533     }
4534     if (myNull2) {
4535         fprintf(stderr, "overwrite userdata with same key in line %i does not work\n", __LINE__);
4536         OK = false;
4537     }
4538 
4539     //the userTest user data table should be null now
4540     if (userTest->getUserData(tempStr)) {
4541         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4542         OK = false;
4543     }
4544     if (userTest->getUserData(tempStr2)) {
4545         fprintf(stderr, "get non-exist user data in line %i does not work\n", __LINE__);
4546         OK = false;
4547     }
4548 
4549     // Test DOMUserDataHandler
4550     // test clone
4551     userTest->setUserData(tempStr2, (void*) document, &userhandler);
4552     DOMNode* mycloned = userTest->cloneNode(true);
4553     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_CLONED, tempStr2, document, userTest, mycloned, __LINE__);
4554 
4555     // test import
4556     document->setUserData(tempStr2, (void*) document, &userhandler);
4557     DOMNode* myimport = document->importNode(userTest,true);
4558     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_IMPORTED, tempStr2, document, userTest, myimport, __LINE__);
4559 
4560     // test delete
4561     myimport->setUserData(tempStr2, (void*) userTest, &userhandler);
4562     myimport->release();
4563     USERDATAHANDLERTEST(userhandler, DOMUserDataHandler::NODE_DELETED, tempStr2, userTest, 0, 0, __LINE__);
4564 
4565     // Test isSameNode
4566     if (!userTest->isSameNode(userTest)) {
4567         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4568         OK = false;
4569     }
4570     if (userTest->isSameNode(userFirst)) {
4571         fprintf(stderr, "isSameNode failed in line %i\n", __LINE__);
4572         OK = false;
4573     }
4574 
4575     // Test isEqualNode
4576     if (!userTest->isEqualNode(mycloned)) {
4577         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4578         OK = false;
4579     }
4580 
4581     if (!userTest->isEqualNode(userTest)) {
4582         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4583         OK = false;
4584     }
4585     if (userTest->isEqualNode(abc70)) {
4586         fprintf(stderr, "isEqualNode failed in line %i\n", __LINE__);
4587         OK = false;
4588     }
4589 
4590     // Test rename, should throw exception
4591     EXCEPTIONSTEST(document->renameNode(userTest, 0, tempStr2), DOMException::NOT_SUPPORTED_ERR, OK, 214 );
4592 
4593     if (!OK)
4594         printf("\n*****The DOMText* method calls listed above failed, all others worked correctly.*****\n");
4595 
4596     return OK;
4597 }
4598 
4599 
4600 /**
4601  * This method tests setting the DOM Level 3 baseURI attribute at
4602  * parse time on nodes from the document personal-schema.xml.xml
4603  *
4604  */
4605 
testBaseURI(XercesDOMParser * parser)4606 bool DOMTest::testBaseURI(XercesDOMParser* parser) {
4607 
4608     bool OK = true;
4609 
4610     try {
4611         // this one assumes executing in samples/data where personal-schema.xml resides
4612         // please modify if this is not correct
4613         parser->parse("personal-schema.xml");
4614     }
4615     catch (const OutOfMemoryException&)
4616     {
4617 	    fprintf(stderr, "OutOfMemoryException.\n");
4618 	    return false;
4619     }
4620     catch (...) {
4621         fprintf(stderr, "parsing personal-schema.xml failed at line %i\n", __LINE__);
4622         return false;
4623     }
4624 
4625     // test only if there is no error
4626     if (!parser->getErrorCount()) {
4627 
4628         //Setup testing strings
4629         XMLCh *fileSchema = XMLString::transcode("file://");
4630         XMLCh *filePath = XMLString::transcode("samples/data/personal-schema.xml");
4631 
4632         //Test document baseURI
4633         DOMDocument *document = parser->getDocument();
4634 
4635         //The baseURI should contain `file://' and `samples/data/personal-schema.xml'
4636         const XMLCh *docBaseURI = document->getBaseURI();
4637 
4638         if(XMLString::patternMatch(docBaseURI, fileSchema) == -1) {
4639             OK = false;
4640             fprintf(stderr, "checking baseURI failed at line %i\n", __LINE__);
4641         }
4642 
4643         if(XMLString::patternMatch(docBaseURI, filePath) == -1) {
4644             OK = false;
4645             fprintf(stderr, "checking baseURI failed at line %i\n", __LINE__);
4646         }
4647 
4648         //Create relative paths from document baseURI
4649 
4650         XMLCh *docBaseURIRoot = new XMLCh [ XMLString::stringLen(docBaseURI) ];
4651         XMLString::copyNString(docBaseURIRoot, docBaseURI, XMLString::lastIndexOf(docBaseURI, chForwardSlash) + 1);
4652 
4653         XMLCh *base_foobar = new XMLCh [ XMLString::stringLen(docBaseURIRoot) + 8];
4654         XMLString::copyString(base_foobar, docBaseURIRoot);
4655         XMLCh *foobar = XMLString::transcode("foo/bar");
4656         XMLString::catString(base_foobar, foobar);
4657 
4658         XMLCh *base_foobarbar = new XMLCh [ XMLString::stringLen(docBaseURIRoot) + 12];
4659         XMLString::copyString(base_foobarbar, docBaseURIRoot);
4660         XMLCh *foobarbar = XMLString::transcode("foo/bar/bar");
4661         XMLString::catString(base_foobarbar, foobarbar);
4662 
4663         XMLCh *base_foocarbar = new XMLCh [ XMLString::stringLen(docBaseURIRoot) + 12];
4664         XMLString::copyString(base_foocarbar, docBaseURIRoot);
4665         XMLCh *foocarbar = XMLString::transcode("foo/car/bar");
4666         XMLString::catString(base_foocarbar, foocarbar);
4667 
4668         XMLCh *file_autobar = XMLString::transcode("file:///auto/bar");
4669         XMLCh *file_carfoo = XMLString::transcode("file:///car/foo/");
4670         XMLCh *file_carfoobarbar = XMLString::transcode("file:///car/foo/bar/bar");
4671 
4672         XMLCh *http_carcar = XMLString::transcode("http://www.example.com/car/car");
4673         XMLCh *http_barfoo = XMLString::transcode("http://www.example.com/bar/foo/");
4674         XMLCh *http_barfoofoobar = XMLString::transcode("http://www.example.com/bar/foo/foo/bar");
4675 
4676         //Processing instruction before Document Element (has document baseURI)
4677 
4678         DOMNode *node = document->getFirstChild();
4679         while(node->getNodeType() != DOMNode::PROCESSING_INSTRUCTION_NODE)
4680             node = node->getNextSibling();
4681 
4682         if(XMLString::compareString(node->getBaseURI(), docBaseURI) != 0) {
4683             OK = false;
4684             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4685         }
4686 
4687         //Document Element baseURI (same as document)
4688 
4689         node = document->getDocumentElement();
4690 
4691         if(XMLString::compareString(node->getBaseURI(), docBaseURI) != 0) {
4692             OK = false;
4693             fprintf(stderr, "checking baseURI, failed at line %i\n", __LINE__);
4694         }
4695 
4696         // <level 1>
4697 
4698         node = node->getFirstChild();
4699         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4700             node = node->getNextSibling();
4701 
4702         DOMNode *level1 = node;
4703 
4704         // <one>
4705 
4706         node = node->getFirstChild();
4707         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4708             node = node->getNextSibling();
4709 
4710         if(XMLString::compareString(node->getBaseURI(), base_foobar) != 0) {
4711             OK = false;
4712             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4713         }
4714 
4715         // <inner1>
4716 
4717         node = node->getFirstChild();
4718         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4719             node = node->getNextSibling();
4720 
4721         if(XMLString::compareString(node->getBaseURI(), base_foobarbar) != 0) {
4722             OK = false;
4723             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4724         }
4725 
4726         // <inner2>
4727 
4728         node = node->getNextSibling();
4729         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4730             node = node->getNextSibling();
4731 
4732         if(XMLString::compareString(node->getBaseURI(), base_foocarbar) != 0) {
4733             OK = false;
4734             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4735         }
4736 
4737         // <?proc-inst-2?>
4738 
4739         node = node->getNextSibling();
4740         while(node->getNodeType() != DOMNode::PROCESSING_INSTRUCTION_NODE)
4741             node = node->getNextSibling();
4742 
4743         if(XMLString::compareString(node->getBaseURI(), base_foobar) != 0) {
4744             OK = false;
4745             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4746         }
4747 
4748         // <level2>
4749 
4750         node = level1->getNextSibling();
4751         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4752             node = node->getNextSibling();
4753 
4754         DOMNode *level2 = node;
4755 
4756         if(XMLString::compareString(node->getBaseURI(), file_autobar) != 0) {
4757             OK = false;
4758             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4759         }
4760 
4761         // <two>
4762 
4763         node = node->getFirstChild();
4764         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4765             node = node->getNextSibling();
4766 
4767         if(XMLString::compareString(node->getBaseURI(), file_carfoo) != 0) {
4768             OK = false;
4769             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4770         }
4771 
4772         // <inner1>
4773 
4774         node = node->getFirstChild();
4775         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4776             node = node->getNextSibling();
4777 
4778         if(XMLString::compareString(node->getBaseURI(), file_carfoobarbar) != 0) {
4779             OK = false;
4780             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4781         }
4782 
4783         // <level3>
4784 
4785         node = level2->getNextSibling();
4786         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4787             node = node->getNextSibling();
4788 
4789         if(XMLString::compareString(node->getBaseURI(), http_carcar) != 0) {
4790             OK = false;
4791             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4792         }
4793 
4794         // <three>
4795 
4796         node = node->getFirstChild();
4797         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4798             node = node->getNextSibling();
4799 
4800         if(XMLString::compareString(node->getBaseURI(), http_barfoo) != 0) {
4801             OK = false;
4802             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4803         }
4804 
4805         // <inner1>
4806 
4807         node = node->getFirstChild();
4808         while(node->getNodeType() != DOMNode::ELEMENT_NODE)
4809             node = node->getNextSibling();
4810 
4811         if(XMLString::compareString(node->getBaseURI(), http_barfoofoobar) != 0) {
4812             OK = false;
4813             fprintf(stderr, "checking baseURI failed at line %i\n",  __LINE__);
4814         }
4815     }
4816     else {
4817         printf("baseURI test was not carried out\n");
4818     }
4819 
4820     return OK;
4821 }
4822 
testWholeText(XercesDOMParser * parser)4823 bool DOMTest::testWholeText(XercesDOMParser* parser) {
4824 	const char* sXml="<?xml version='1.0'?>"
4825 				"<!DOCTYPE root["
4826                 "<!ENTITY ent1 'Dallas. &ent3; #5668'>"
4827                 "<!ENTITY ent2 '1900 Dallas Road<![CDATA[ (East) ]]>'>"
4828                 "<!ENTITY ent3 'California. &ent4; PO'>  "
4829                 "<!ENTITY ent4 'USA '>"
4830                 "<!ENTITY ent5 'The Content &ent6; never reached'>"
4831                 "<!ENTITY ent6 'ends here. <foo/>'>"
4832                 "]>"
4833                 "<root>&ent1; &ent2;"
4834                   "<elem>Home </elem>"
4835                   "<elem>Test: &ent5;</elem>"
4836                 "</root>";
4837 	MemBufInputSource is((XMLByte*)sXml, strlen(sXml), "bufId");
4838 	parser->parse(is);
4839 	DOMDocument* doc=parser->getDocument();
4840 	// pointing to "Dallas. "
4841 	DOMNode* t1=doc->getDocumentElement()->getFirstChild()->getFirstChild();
4842 	if(t1==NULL || t1->getNodeType()!=DOMNode::TEXT_NODE)
4843 	{
4844         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4845 		return false;
4846 	}
4847 	const XMLCh* s1=((DOMText*)t1)->getWholeText();
4848     const XMLCh* refText=XMLString::transcode("Dallas. California. USA  PO #5668 1900 Dallas Road (East) ");
4849 	if(XMLString::compareString(s1,refText)!=0)
4850 	{
4851         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4852 		return false;
4853 	}
4854 
4855     // pointing to " (East) " [CDATA]
4856 	DOMNode* t2=doc->getDocumentElement()->getFirstChild()->getNextSibling()->getNextSibling()->getFirstChild()->getNextSibling();
4857 	if(t2==NULL || t2->getNodeType()!=DOMNode::CDATA_SECTION_NODE)
4858 	{
4859         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4860 		return false;
4861 	}
4862 	const XMLCh* s2=((DOMText*)t2)->getWholeText();
4863 	if(XMLString::compareString(s2,refText)!=0)
4864 	{
4865         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4866 		return false;
4867 	}
4868 
4869     // pointing to "Home "
4870 	DOMNode* t3=doc->getDocumentElement()->getFirstChild()->getNextSibling()->getNextSibling()->getNextSibling()->getFirstChild();
4871 	if(t3==NULL || t3->getNodeType()!=DOMNode::TEXT_NODE)
4872 	{
4873         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4874 		return false;
4875 	}
4876 	const XMLCh* s3=((DOMText*)t3)->getWholeText();
4877     refText=XMLString::transcode("Home ");
4878 	if(XMLString::compareString(s3,refText)!=0)
4879 	{
4880         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4881 		return false;
4882 	}
4883 
4884     // pointing to "The Content "
4885 	DOMNode* t4=doc->getDocumentElement()->getFirstChild()->getNextSibling()->getNextSibling()->getNextSibling()->getNextSibling()->getFirstChild()->getNextSibling()->getFirstChild();
4886 	if(t4==NULL || t4->getNodeType()!=DOMNode::TEXT_NODE)
4887 	{
4888         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4889 		return false;
4890 	}
4891 	const XMLCh* s4=((DOMText*)t4)->getWholeText();
4892     refText=XMLString::transcode("Test: The Content ends here. ");
4893 	if(XMLString::compareString(s4,refText)!=0)
4894 	{
4895         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4896 		return false;
4897 	}
4898 
4899     DOMNode* newt2=((DOMText*)t2)->replaceWholeText(s2);
4900     UNUSED(newt2); //silence warning
4901     DOMNode* newt3=((DOMText*)t3)->replaceWholeText(s3);
4902     UNUSED(newt3); //silence warning
4903     try
4904     {
4905         DOMNode* newt4=((DOMText*)t4)->replaceWholeText(s4);
4906         UNUSED(newt4); //silence warning
4907         // ent5 contains a <foo/> node, and cannot be removed
4908         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4909         return false;
4910     }
4911     catch(DOMException&)
4912     {
4913     }
4914 
4915     DOMLSSerializer* writer=DOMImplementation::getImplementation()->createLSSerializer();
4916     const XMLCh* xml=writer->writeToString(doc->getDocumentElement());
4917 
4918     refText=XMLString::transcode("<root><![CDATA[Dallas. California. USA  PO #5668 1900 Dallas Road (East) ]]>"
4919                                    "<elem>Home </elem>"
4920                                    "<elem>Test: &ent5;</elem>"
4921                                  "</root>");
4922 	if(XMLString::compareString(xml,refText)!=0)
4923 	{
4924         fprintf(stderr, "checking wholeText failed at line %i\n",  __LINE__);
4925 		return false;
4926 	}
4927     writer->release();
4928     return true;
4929 }
4930 
4931 class ParserAborter : public DOMLSParserFilter
4932 {
4933 public:
ParserAborter()4934     ParserAborter() {}
4935 
acceptNode(DOMNode *)4936     virtual FilterAction acceptNode(DOMNode* ) { return DOMLSParserFilter::FILTER_INTERRUPT; }
startElement(DOMElement *)4937     virtual FilterAction startElement(DOMElement* ) { return DOMLSParserFilter::FILTER_INTERRUPT; }
getWhatToShow() const4938     virtual DOMNodeFilter::ShowType getWhatToShow() const { return DOMNodeFilter::SHOW_ALL; }
4939 };
4940 
4941 class ParserNester : public DOMLSParserFilter
4942 {
4943 public:
ParserNester(DOMLSParser * parser,DOMLSInput * input)4944     ParserNester(DOMLSParser* parser, DOMLSInput* input) { m_parser=parser; m_input=input; }
4945 
acceptNode(DOMNode *)4946     virtual FilterAction acceptNode(DOMNode* ) { m_parser->parse(m_input); return DOMLSParserFilter::FILTER_ACCEPT;}
startElement(DOMElement *)4947     virtual FilterAction startElement(DOMElement* ) { return DOMLSParserFilter::FILTER_ACCEPT; }
getWhatToShow() const4948     virtual DOMNodeFilter::ShowType getWhatToShow() const { return DOMNodeFilter::SHOW_ALL; }
4949 
4950     DOMLSParser* m_parser;
4951     DOMLSInput* m_input;
4952 };
4953 
4954 class ParserSkipper : public DOMLSParserFilter
4955 {
4956 public:
ParserSkipper()4957     ParserSkipper() : fCallbackCalls(0) { }
4958 
acceptNode(DOMNode *)4959     virtual FilterAction acceptNode(DOMNode* /* node */) { fCallbackCalls++; return DOMLSParserFilter::FILTER_ACCEPT;}
startElement(DOMElement * node)4960     virtual FilterAction startElement(DOMElement* node)
4961     {
4962         XMLCh elem[]={chLatin_e, chLatin_l, chLatin_e, chLatin_m, chNull };
4963         if(XMLString::equals(node->getNodeName(), elem))
4964             return DOMLSParserFilter::FILTER_REJECT;
4965         else
4966             return DOMLSParserFilter::FILTER_ACCEPT;
4967     }
getWhatToShow() const4968     virtual DOMNodeFilter::ShowType getWhatToShow() const { return DOMNodeFilter::SHOW_ALL; }
4969 
4970     unsigned int fCallbackCalls;
4971 };
4972 
testLSExceptions()4973 bool DOMTest::testLSExceptions() {
4974     bool OK = true;
4975 
4976 	const char* sXml="<?xml version='1.0'?>"
4977 				"<!DOCTYPE root["
4978                 "<!ENTITY ent1 'Dallas. &ent3; #5668'>"
4979                 "<!ENTITY ent2 '1900 Dallas Road<![CDATA[ (East) ]]>'>"
4980                 "<!ENTITY ent3 'California. &ent4; PO'>  "
4981                 "<!ENTITY ent4 'USA '>"
4982                 "<!ENTITY ent5 'The Content &ent6; never reached'>"
4983                 "<!ENTITY ent6 'ends here. <foo/>'>"
4984                 "]>"
4985                 "<root>&ent1; &ent2;"
4986                   "<elem>Home </elem>"
4987                   "<elem>Test: &ent5;</elem>"
4988                 "</root>";
4989 
4990     static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
4991     DOMImplementationLS *impl = (DOMImplementationLS*)DOMImplementationRegistry::getDOMImplementation(gLS);
4992     DOMLSParser       *domBuilder = impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
4993     DOMLSInput        *input = impl->createLSInput();
4994     XMLString::transcode(sXml, tempStr, 3999);
4995     input->setStringData(tempStr);
4996     try
4997     {
4998         ParserAborter aborter;
4999         domBuilder->setFilter(&aborter);
5000         domBuilder->parse(input);
5001 
5002         fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5003         OK=false;
5004     }
5005     catch(DOMLSException& e)
5006     {
5007         if(e.code!=DOMLSException::PARSE_ERR)
5008         {
5009             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5010             OK=false;
5011         }
5012     }
5013 
5014     try
5015     {
5016         ParserNester nester(domBuilder, input);
5017         domBuilder->setFilter(&nester);
5018         domBuilder->parse(input);
5019 
5020         fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5021         OK=false;
5022     }
5023     catch(DOMException& e)
5024     {
5025         if(e.code!=DOMException::INVALID_STATE_ERR)
5026         {
5027             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5028             OK=false;
5029         }
5030     }
5031 
5032     try
5033     {
5034         ParserSkipper skipper;
5035         domBuilder->setFilter(&skipper);
5036         domBuilder->getDomConfig()->setParameter(XMLUni::fgDOMEntities, false);
5037         DOMDocument* doc=domBuilder->parse(input);
5038 
5039         // verify that we get only 3 calls: for the text node, the CDATA section and the root element
5040         if(doc==NULL || doc->getDocumentElement()==NULL || doc->getDocumentElement()->getChildElementCount()!=0 || skipper.fCallbackCalls!=3)
5041         {
5042             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5043             OK=false;
5044         }
5045     }
5046     catch(DOMException&)
5047     {
5048         fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5049         OK=false;
5050     }
5051 
5052     // this XML should trigger reuse of DOMElement
5053 	const char* sXml2="<?xml version='1.0'?>"
5054                 "<root xmlns:x='urn:yyy'>"
5055                   "<elem xmlns:x='urn:xxx'>Home</elem>"
5056                   "<elem2>Test</elem2>"
5057                   "<elem>Home</elem>"
5058                   "<elem2>Test</elem2>"
5059                 "</root>";
5060     XMLString::transcode(sXml2, tempStr, 3999);
5061     input->setStringData(tempStr);
5062     try
5063     {
5064         ParserSkipper skipper;
5065         domBuilder->setFilter(&skipper);
5066         DOMDocument* doc=domBuilder->parse(input);
5067 
5068         // verify that we get only 5 calls: for the root element, the two elem2 and the two text nodes under them
5069         if(doc==NULL || doc->getDocumentElement()==NULL || doc->getDocumentElement()->getChildElementCount()!=2 || skipper.fCallbackCalls!=5)
5070         {
5071             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5072             OK=false;
5073         }
5074     }
5075     catch(DOMException&)
5076     {
5077         fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5078         OK=false;
5079     }
5080 
5081     // test for parseWithContext
5082     try
5083     {
5084         XMLString::transcode("root", tempStr2, 3999);
5085         domBuilder->setFilter(NULL);
5086         DOMDocument* doc=domBuilder->parse(input);
5087         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_APPEND_AS_CHILDREN);
5088         // the first 'elem' child of 'root' must have a 'root' child
5089         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getFirstElementChild()->getNodeName(), tempStr2))
5090         {
5091             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5092             OK=false;
5093         }
5094 
5095         doc=domBuilder->parse(input);
5096         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_REPLACE_CHILDREN);
5097         // the first 'elem' child of 'root' must have a 'root' child
5098         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getFirstElementChild()->getNodeName(), tempStr2))
5099         {
5100             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5101             OK=false;
5102         }
5103 
5104         doc=domBuilder->parse(input);
5105         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_INSERT_BEFORE);
5106         // the first child of 'root' must be another 'root' child
5107         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getNodeName(), tempStr2))
5108         {
5109             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5110             OK=false;
5111         }
5112 
5113         doc=domBuilder->parse(input);
5114         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_INSERT_AFTER);
5115         // the node after the first child of 'root' must be another 'root' child
5116         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getNextElementSibling()->getNodeName(), tempStr2))
5117         {
5118             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5119             OK=false;
5120         }
5121 
5122         doc=domBuilder->parse(input);
5123         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_REPLACE);
5124         // the first child of 'root' must be another 'root' child
5125         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getNodeName(), tempStr2))
5126         {
5127             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5128             OK=false;
5129         }
5130 
5131         // verify that namespaces are in scope
5132         doc=domBuilder->parse(input);
5133 	    const char* sXml3="<x:root/>";
5134         XMLString::transcode(sXml3, tempStr2, 3999);
5135         input->setStringData(tempStr2);
5136         domBuilder->parseWithContext(input, doc->getDocumentElement()->getFirstElementChild(), DOMLSParser::ACTION_APPEND_AS_CHILDREN);
5137         // the first 'elem' child of 'root' must have a 'x:root' child
5138         XMLString::transcode("urn:xxx", tempStr2, 3999);
5139         if(!XMLString::equals(doc->getDocumentElement()->getFirstElementChild()->getFirstElementChild()->getNamespaceURI(), tempStr2))
5140         {
5141             fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5142             OK=false;
5143         }
5144     }
5145     catch(DOMException&)
5146     {
5147         fprintf(stderr, "checking testLSExceptions failed at line %i\n",  __LINE__);
5148         OK=false;
5149     }
5150 
5151     input->release();
5152     domBuilder->release();
5153 
5154     return OK;
5155 }
5156 
testElementTraversal()5157 bool DOMTest::testElementTraversal() {
5158 	const char* sXml="<?xml version='1.0'?>"
5159 				"<!DOCTYPE g ["
5160                 "<!ENTITY ent1 '<nestedEl>&ent2;</nestedEl>'>"
5161                 "<!ENTITY ent2 'text'>"
5162                 "]>"
5163 				"<g id='shapeGroup'>\n"
5164                 "\n"
5165                 "\t<rect id='rect1' x='5' y='5' width='310' height='220' rx='15' ry='15' fill='skyblue'/>\n"
5166                 "\t<rect id='rect2' x='15' y='15' width='210' height='180' rx='15' ry='15' fill='cornflowerblue'/>\n"
5167                 "\n"
5168                 "\t<ellipse id='ellipse1' cx='90' cy='70' rx='50' ry='30' fill='yellow' stroke='orange'/>\n"
5169                 "\n"
5170                 "\t<path id='path1' stroke-width='15' stroke='orange' fill='none' stroke-linecap='round'\n"
5171                 "\t\td='M25,150 C180,180 290,0 400,140 S420,100 460,90'/>\n"
5172                 "\t<text id='text1' x='0' y='0' font-size='35' fill='yellow' stroke='orange'\n"
5173                 "\t\tstroke-width='2' stroke-linejoin='round' font-weight='bold'>\n"
5174                 "\t\t<textPath id='textPath1' href='#path1'>&ent1;&ent2;&ent1;</textPath></text>\n"
5175                 "</g>";
5176 	MemBufInputSource is((XMLByte*)sXml, strlen(sXml), "bufId");
5177 
5178     static const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
5179     DOMImplementationLS *impl = (DOMImplementationLS*)DOMImplementationRegistry::getDOMImplementation(gLS);
5180     DOMLSParser       *domBuilder = impl->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS, 0);
5181     DOMLSInput        *input = impl->createLSInput();
5182     XMLString::transcode(sXml, tempStr, 3999);
5183     input->setStringData(tempStr);
5184     try
5185     {
5186         DOMDocument* doc=domBuilder->parse(input);
5187 
5188         XMLSize_t c = doc->getDocumentElement()->getChildNodes()->getLength();
5189 	    if(c!=11)
5190 	    {
5191             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5192 		    return false;
5193 	    }
5194         DOMNode* firstNode = doc->getDocumentElement()->getFirstChild();
5195         if(firstNode==NULL || firstNode->getNodeType()!=DOMNode::TEXT_NODE || *firstNode->getNodeValue()=='\r')
5196 	    {
5197             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5198 		    return false;
5199 	    }
5200         DOMElement* childNode = doc->getDocumentElement()->getFirstElementChild();
5201         XMLString::transcode("id", tempStr, 3999);
5202         XMLString::transcode("rect1", tempStr2, 3999);
5203         if(childNode==NULL || childNode->getNodeType()!=DOMNode::ELEMENT_NODE || !XMLString::equals(childNode->getAttribute(tempStr),tempStr2))
5204 	    {
5205             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5206 		    return false;
5207 	    }
5208         XMLSize_t count=0;
5209         while(childNode!=NULL)
5210         {
5211             count++;
5212             childNode=childNode->getNextElementSibling();
5213         }
5214         if(count!=5)
5215 	    {
5216             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5217 		    return false;
5218 	    }
5219         count = doc->getDocumentElement()->getChildElementCount();
5220         if(count!=5)
5221 	    {
5222             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5223 		    return false;
5224 	    }
5225         DOMElement* text=doc->getDocumentElement()->getLastElementChild();
5226         XMLString::transcode("id", tempStr, 3999);
5227         XMLString::transcode("text1", tempStr2, 3999);
5228         if(text==NULL || text->getNodeType()!=DOMNode::ELEMENT_NODE || !XMLString::equals(text->getAttribute(tempStr),tempStr2))
5229 	    {
5230             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5231 		    return false;
5232 	    }
5233         DOMElement* textPath=text->getFirstElementChild();
5234         XMLString::transcode("id", tempStr, 3999);
5235         XMLString::transcode("textPath1", tempStr2, 3999);
5236         if(textPath==NULL || textPath->getNodeType()!=DOMNode::ELEMENT_NODE || !XMLString::equals(textPath->getAttribute(tempStr),tempStr2))
5237 	    {
5238             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5239 		    return false;
5240 	    }
5241         count = textPath->getChildElementCount();
5242         if(count!=2)
5243 	    {
5244             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5245 		    return false;
5246 	    }
5247         DOMElement* insideEntity=textPath->getFirstElementChild();
5248         if(insideEntity==NULL || insideEntity->getNodeType()!=DOMNode::ELEMENT_NODE)
5249 	    {
5250             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5251 		    return false;
5252 	    }
5253         DOMElement* insideEntity2=textPath->getLastElementChild();
5254         if(insideEntity2==NULL || insideEntity2->getNodeType()!=DOMNode::ELEMENT_NODE)
5255 	    {
5256             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5257 		    return false;
5258 	    }
5259         if(insideEntity->getNextElementSibling()!=insideEntity2 || insideEntity!=insideEntity2->getPreviousElementSibling())
5260 	    {
5261             fprintf(stderr, "checking ElementTraversal failed at line %i\n",  __LINE__);
5262 		    return false;
5263 	    }
5264         return true;
5265     }
5266     catch(DOMLSException&)
5267     {
5268         fprintf(stderr, "checking testElementTraversal failed at line %i\n",  __LINE__);
5269         return false;
5270     }
5271 
5272     input->release();
5273     domBuilder->release();
5274 
5275     return true;
5276 }
5277 
5278 /**
5279  *
5280  * @param node org.w3c.dom.DOMNode
5281  * @param node2 org.w3c.dom.DOMNode
5282  *
5283  */
treeCompare(DOMNode * node,DOMNode * node2)5284 bool DOMTest::treeCompare(DOMNode* node, DOMNode* node2)
5285 {
5286     bool answer = true;
5287 
5288     DOMNode*  kid, *kid2;         // Check the subtree for equality
5289     kid = node->getFirstChild();
5290     kid2 = node2->getFirstChild();
5291     if (kid && kid2)
5292     {
5293         answer = treeCompare(kid, kid2);
5294         if (!answer)
5295             return answer;
5296         else
5297             if (kid->getNextSibling() && kid2->getNextSibling())
5298             {
5299                 while (kid->getNextSibling() && kid2->getNextSibling())
5300                 {
5301                     answer = treeCompare(kid->getNextSibling(), kid2->getNextSibling());
5302                     if (!answer)
5303                         return answer;
5304                     else
5305                     {
5306                         kid = kid->getNextSibling();
5307                         kid2 = kid2->getNextSibling();
5308                     }
5309                 }
5310             } else
5311                 if (!(!kid->getNextSibling() && !kid2->getNextSibling()))
5312                 {
5313                     return false;
5314                 }
5315     } else
5316         if (kid != kid2)
5317         {
5318             // One or the other of (kid1, kid2) is 0, but not both.
5319             return false;
5320         }
5321 
5322     if (XMLString::compareString(node->getNodeName(), node2->getNodeName()))
5323         return false;
5324     if (node->getNodeValue()==0 && node2->getNodeValue()!=0)
5325         return false;
5326     if (node->getNodeValue()!=0 && node2->getNodeValue()==0)
5327         return false;
5328     if (XMLString::compareString(node->getNodeValue(), node2->getNodeValue()))
5329         return false;
5330 
5331     return answer;
5332 }
5333 
5334 #define TEST_VALID_REGEX(str, regex, line) \
5335     try \
5336     { \
5337         RegularExpression p(regex); \
5338         if(!p.matches(str)) \
5339         { \
5340             fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5341             OK = false; \
5342         } \
5343     }   \
5344     catch(XMLException& ) \
5345     {   \
5346         fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5347         OK = false; \
5348     }
5349 
5350 #define TEST_INVALID_REGEX(str, regex, line) \
5351     try \
5352     { \
5353         RegularExpression p(regex); \
5354         if(p.matches(str)) \
5355         { \
5356             fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5357             OK = false; \
5358         } \
5359     }   \
5360     catch(XMLException& ) \
5361     {   \
5362         fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5363         OK = false; \
5364     }
5365 
5366 #define TEST_VALID_SCHEMA_REGEX(str, regex, line) \
5367     try \
5368     { \
5369         RegularExpression p(regex, "X"); \
5370         if(!p.matches(str)) \
5371         { \
5372             fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5373             OK = false; \
5374         } \
5375     }   \
5376     catch(XMLException& ) \
5377     {   \
5378         fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5379         OK = false; \
5380     }
5381 
5382 #define TEST_INVALID_SCHEMA_REGEX(str, regex, line) \
5383     try \
5384     { \
5385         RegularExpression p(regex, "X"); \
5386         if(p.matches(str)) \
5387         { \
5388             fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5389             OK = false; \
5390         } \
5391     }   \
5392     catch(XMLException& ) \
5393     {   \
5394         fprintf(stderr, "Regular expression test failed at line %i\n", line); \
5395         OK = false; \
5396     }
5397 
5398 #define HUGE_STRING (100*1024)  // 100Kb
5399 
testRegex()5400 bool DOMTest::testRegex() {
5401     bool OK = true;
5402 
5403     char* hugeString=new char[HUGE_STRING+1];
5404     for(int i=0;i<HUGE_STRING;i++)
5405         hugeString[i]='A';
5406     hugeString[HUGE_STRING]=0;
5407     TEST_VALID_REGEX(hugeString, "([A-F0-9]{2})*", __LINE__);
5408 
5409     TEST_VALID_REGEX("12_END", "[12]{2}_END", __LINE__);
5410     TEST_INVALID_REGEX("12", "[12]{2}_END", __LINE__);
5411 
5412     TEST_VALID_REGEX("AE", "(A|B*|C)E", __LINE__);
5413     TEST_VALID_REGEX("E", "(A|B*|C)E", __LINE__);
5414     TEST_VALID_REGEX("BBBBBBBBBBBBBE", "(A|B*|C)E", __LINE__);
5415     TEST_VALID_REGEX("CE", "(A|B*|C)E", __LINE__);
5416     TEST_INVALID_REGEX("A", "(A|B*|C)E", __LINE__);
5417     TEST_INVALID_REGEX("", "(A|B*|C)E", __LINE__);
5418     TEST_INVALID_REGEX("BBBBBBBBBBBBB", "(A|B*|C)E", __LINE__);
5419     TEST_INVALID_REGEX("C", "(A|B*|C)E", __LINE__);
5420 
5421     // this tests the closures that need fOffset in the context
5422     for(int j=0;j<HUGE_STRING-1;j++)
5423         hugeString[j]=j%2?'B':'C';
5424     hugeString[HUGE_STRING-1]='E';
5425     // we run out of stack space when testing this one
5426     //TEST_VALID_REGEX(hugeString, "(B?C?)*E", __LINE__);
5427     TEST_VALID_REGEX("BBBBE", "(B?C?)*E", __LINE__);
5428     TEST_VALID_REGEX("E", "(B?C?)*E", __LINE__);
5429     TEST_VALID_REGEX("CCCCCCCE", "(B?C?)*E", __LINE__);
5430     TEST_VALID_REGEX("BCBCBBBBCCCCCE", "(B?C?)*E", __LINE__);
5431 
5432     TEST_VALID_REGEX(" C", "[\\s]*[\\S]+[\\s\\S]*", __LINE__);
5433     TEST_VALID_REGEX("C asasaskja ksakj", "[\\s]*[\\S]+[\\s\\S]*", __LINE__);
5434     TEST_VALID_REGEX(" C", "[\\s]*[\\S]+[\\s\\S]*", __LINE__);
5435     TEST_INVALID_REGEX("\t         ", "[\\s]*[\\S]+[\\S]*", __LINE__);
5436 
5437     TEST_VALID_REGEX("U2VsZWN0IFRoaXMgaXMgZmlmdGg7DQogDQpOT1RFOiBUaGUgaW5mb3JtYXRpb24gY29udGFpbmVk\n"
5438                      "IGluIHRoaXMgZW1haWwgbWVzc2FnZSBpcyBjb25zaWRlcmVkIGNvbmZpZGVudGlhbCBhbmQgcHJv\n"
5439                      "cHJpZXRhcnkgdG8gdGhlIHNlbmRlciBhbmQgaXMgaW50ZW5kZWQgc29sZWx5IGZvciByZXZpZXcg\n"
5440                      "YW5kIHVzZSBieSB0aGUgbmFtZWQgcmVjaXBpZW50LiAgQW55IHVuYXV0aG9yaXplZCByZXZpZXcs\n"
5441                      "IHVzZSBvciBkaXN0cmlidXRpb24gaXMgc3RyaWN0bHkgcHJvaGliaXRlZC4gSWYgeW91IGhhdmUg\n"
5442                      "cmVjZWl2ZWQgdGhpcyBtZXNzYWdlIGluIGVycm9yLCBwbGVhc2UgYWR2aXNlIHRoZSBzZW5kZXIg\n"
5443                      "YnkgcmVwbHkgZW1haWwgYW5kIGRlbGV0ZSB0aGUgbWVzc2FnZS4NCg==\n", "(^(([A-Za-z0-9+/=]){4}){1,19}$)*", __LINE__);
5444 
5445     // examples from XMLSchema specs
5446     TEST_VALID_SCHEMA_REGEX("123 456", "123 (\\d+\\s)*456", __LINE__);
5447     TEST_VALID_SCHEMA_REGEX("123 987 456", "123 (\\d+\\s)*456", __LINE__);
5448     TEST_VALID_SCHEMA_REGEX("123 987 567 456", "123 (\\d+\\s)*456", __LINE__);
5449 
5450     TEST_VALID_SCHEMA_REGEX("P0100Y02M", "P\\p{Nd}{4}Y\\p{Nd}{2}M", __LINE__);
5451     TEST_VALID_SCHEMA_REGEX("en-US", "[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*", __LINE__);
5452 
5453     TEST_VALID_SCHEMA_REGEX("123-XX", "\\d{3}-[A-Z]{2}", __LINE__);
5454 
5455     TEST_VALID_SCHEMA_REGEX("01803", "[0-9]{5}(-[0-9]{4})?", __LINE__);
5456 
5457     TEST_VALID_SCHEMA_REGEX("_id1", "\\i\\c*", __LINE__);
5458     TEST_INVALID_SCHEMA_REGEX("0id", "\\i\\c*", __LINE__);
5459 
5460     TEST_VALID_SCHEMA_REGEX("hello", "[\\i-[:]][\\c-[:]]*", __LINE__);
5461     TEST_INVALID_SCHEMA_REGEX("0qq", "[\\i-[:]][\\c-[:]]*", __LINE__);
5462     TEST_INVALID_SCHEMA_REGEX("ns:localname", "[\\i-[:]][\\c-[:]]*", __LINE__);
5463     TEST_INVALID_SCHEMA_REGEX("ns:", "[\\i-[:]][\\c-[:]]*", __LINE__);
5464     TEST_INVALID_SCHEMA_REGEX(":qq", "[\\i-[:]][\\c-[:]]*", __LINE__);
5465 
5466     TEST_VALID_SCHEMA_REGEX("900", "[\\-+]?[0-9]+", __LINE__);
5467     TEST_VALID_SCHEMA_REGEX("-900", "[\\-+]?[0-9]+", __LINE__);
5468     TEST_VALID_SCHEMA_REGEX("+900", "[\\-+]?[0-9]+", __LINE__);
5469     TEST_INVALID_SCHEMA_REGEX("+", "[\\-+]?[0-9]+", __LINE__);
5470     TEST_INVALID_SCHEMA_REGEX("-", "[\\-+]?[0-9]+", __LINE__);
5471     TEST_INVALID_SCHEMA_REGEX("0.4", "[\\-+]?[0-9]+", __LINE__);
5472 
5473     TEST_VALID_SCHEMA_REGEX("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "A.*Z", __LINE__);
5474     TEST_INVALID_SCHEMA_REGEX("Z", "A.*Z", __LINE__);
5475     TEST_INVALID_SCHEMA_REGEX("A", "A.*Z", __LINE__);
5476     TEST_INVALID_SCHEMA_REGEX("ABC", "A.*Z", __LINE__);
5477 
5478     // tests from the XMLSchema Test Suite
5479     TEST_VALID_SCHEMA_REGEX("Chapter b", "Chapter\\s{0,2}\\w", __LINE__);
5480     TEST_VALID_SCHEMA_REGEX("Chapter\tb", "Chapter\\s{0,2}\\w", __LINE__);
5481     TEST_VALID_SCHEMA_REGEX("Chapter\nb", "Chapter\\s{0,2}\\w", __LINE__);
5482 
5483     TEST_VALID_SCHEMA_REGEX("abx", "(a|b)+x", __LINE__);
5484     TEST_VALID_SCHEMA_REGEX("bax", "(a|b)+x", __LINE__);
5485     TEST_VALID_SCHEMA_REGEX("bbx", "(a|b)+x", __LINE__);
5486     TEST_VALID_SCHEMA_REGEX("aaax", "(a|b)+x", __LINE__);
5487     TEST_VALID_SCHEMA_REGEX("abax", "(a|b)+x", __LINE__);
5488     TEST_VALID_SCHEMA_REGEX("abbx", "(a|b)+x", __LINE__);
5489     TEST_VALID_SCHEMA_REGEX("bbax", "(a|b)+x", __LINE__);
5490 
5491     TEST_VALID_SCHEMA_REGEX("1x2abc", ".*abc.*", __LINE__);
5492     TEST_VALID_SCHEMA_REGEX("abc1x2", ".*abc.*", __LINE__);
5493     TEST_VALID_SCHEMA_REGEX("z3455abch00ray", ".*abc.*", __LINE__);
5494     TEST_VALID_SCHEMA_REGEX("bcabcabcab", ".*abc.*", __LINE__);
5495 
5496     TEST_VALID_SCHEMA_REGEX("abbbx", "ab{2,4}x", __LINE__);
5497     TEST_VALID_SCHEMA_REGEX("abbbbx", "ab{2,4}x", __LINE__);
5498     TEST_INVALID_SCHEMA_REGEX("abx", "ab{2,4}x", __LINE__);
5499     TEST_INVALID_SCHEMA_REGEX("abbbbbx", "ab{2,4}x", __LINE__);
5500     TEST_VALID_SCHEMA_REGEX("PAG_1", "PAG_[0-9]{1,}", __LINE__);
5501 
5502     TEST_VALID_SCHEMA_REGEX("5 Bedford Street Boston , MA 15604-1536", "\\d{1,5}\\s([A-Z][a-z]{1,20}\\s){1}Street\\s([A-Z][a-z]{1,20}\\s){1},\\s[A-Z]{2}\\s15604-1536", __LINE__);
5503 
5504     // from X3D schema
5505     TEST_VALID_SCHEMA_REGEX("0.5 0.2 1.0", "((((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))) (((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))) (((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))))?", __LINE__);
5506     TEST_VALID_SCHEMA_REGEX("5.0e-2 .2 1", "((((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))) (((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))) (((\\.[0-9]+|0(\\.[0-9]*)?)((E|e)(\\+|\\-)?[0-9]+)?)|(1(\\.[0]*)?((E|e)\\-[0-9]+)?)|([1-9](\\.[0-9]*)((E|e)\\-[0-9]+))))?", __LINE__);
5507 
5508     TEST_VALID_SCHEMA_REGEX("-0 +3989 -90.76754,+9E77, -0.3e+9", "(((\\+|\\-)?(0|[1-9][0-9]*)?(\\.[0-9]*)?((E|e)(\\+|\\-)?[0-9]+)?)?( )?(,)?( )?)*", __LINE__);
5509 
5510     try
5511     {
5512         Match match;
5513         RegularExpression p("([\\-\\(]?\\d{1,3}([, ]\\d{3})+\\.\\d+[\\)]?|[\\-\\(]?\\d+\\.\\d+[\\)]?).*");
5514         if(!p.matches("13.13", &match) || match.getStartPos(0)!=0 || match.getEndPos(0)!=5)
5515         {
5516             fprintf(stderr, "Regular expression test failed at line %i\n", __LINE__);
5517             OK = false;
5518         }
5519     }
5520     catch(XMLException& )
5521     {
5522         fprintf(stderr, "Regular expression test failed at line %i\n", __LINE__);
5523         OK = false;
5524     }
5525 
5526     delete [] hugeString;
5527 
5528     return OK;
5529 }
5530 
5531 
5532 // support classes to feed data with variable chunks
5533 
5534 class Slicer : public BinInputStream
5535 {
5536 public:
Slicer(const XMLByte * src,XMLSize_t size,const XMLSize_t * slices=0,XMLSize_t count=0)5537 	Slicer(const XMLByte* src, XMLSize_t size, const XMLSize_t* slices = 0, XMLSize_t count = 0)
5538 	: mSrc(src), mSize(size), mSlices(slices), mCount(count), mPos(0), mSlice(0), mBoundary(0)
5539 	{
5540 	}
5541 
curPos() const5542 	XMLFilePos curPos() const
5543 	{
5544 		return mPos;
5545 	}
5546 
readBytes(XMLByte * const toFill,const XMLSize_t maxToRead)5547 	XMLSize_t readBytes(XMLByte* const toFill, const XMLSize_t maxToRead)
5548 	{
5549 		if (mPos == mBoundary)
5550 		{
5551 			if (mSlice < mCount)
5552 			{
5553                 XMLSize_t next = mBoundary + mSlices[mSlice++];
5554                 mBoundary = (mSize<next)?mSize:next;
5555 			}
5556 			else
5557 			{
5558 				mBoundary = mSize;
5559 			}
5560 		}
5561         XMLSize_t remain = mBoundary - mPos;
5562         XMLSize_t toRead = (maxToRead<remain)?maxToRead:remain;
5563 		memcpy(toFill, mSrc + mPos, toRead);
5564 		mPos += toRead;
5565 		return toRead;
5566 	}
5567 
getContentType() const5568     virtual const XMLCh* getContentType() const
5569     {
5570         return 0;
5571     }
5572 
5573 private:
5574 
5575 	const XMLByte* const    mSrc;
5576 	const XMLSize_t         mSize;
5577 	const XMLSize_t* const  mSlices;
5578 	const XMLSize_t         mCount;
5579 
5580 	XMLSize_t               mPos, mSlice;
5581 	XMLSize_t               mBoundary;
5582 };
5583 
5584 
5585 class SlicerSource : public InputSource
5586 {
5587 public:
SlicerSource(const XMLByte * src,XMLSize_t size,const XMLSize_t * slices=0,XMLSize_t count=0)5588 	SlicerSource(const XMLByte* src, XMLSize_t size, const XMLSize_t* slices = 0, XMLSize_t count = 0)
5589 	: mSrc(src), mSize(size), mSlices(slices), mCount(count)
5590 	{
5591 	}
5592 
makeStream() const5593 	XERCES_CPP_NAMESPACE::BinInputStream* makeStream() const
5594 	{
5595 		return new Slicer(mSrc, mSize, mSlices, mCount);
5596 	}
5597 
5598 private:
5599 	const XMLByte* const    mSrc;
5600 	const XMLSize_t         mSize;
5601 	const XMLSize_t* const  mSlices;
5602 	const XMLSize_t         mCount;
5603 };
5604 
testScanner(XercesDOMParser * parser)5605 bool DOMTest::testScanner(XercesDOMParser* parser) {
5606     bool OK = true;
5607 
5608     const char sampleDoc[] =
5609 	    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
5610 	    "<!-- Document element is document -->\n"
5611 	    "<document attr=\"value\">\n"
5612 	    "   <!-- a tag -->\n"
5613 	    "   <tag>foo</tag>\n"
5614 	    "\n"
5615 	    "   <!-- another tag -->\n"
5616 	    "   <tag/>\n"
5617 	    "</document>\n";
5618     const size_t sampleDocSize = sizeof(sampleDoc) - 1;
5619     const XMLByte* sampleDocXML = reinterpret_cast<const XMLByte*>(sampleDoc);
5620 
5621     try
5622 	{
5623 		// First, try parsing the document in one hit
5624 		SlicerSource src(sampleDocXML, sampleDocSize);
5625 		parser->parse(src);
5626 	}
5627 	catch (...)
5628 	{
5629         OK = false;
5630         fprintf(stderr, "Variable chunks parsing failed at line %i\n", __LINE__);
5631 	}
5632 
5633 	try
5634 	{
5635 		// Now, parse it in blocks that end between the '<',  '!' and '--' of a comment
5636 		const XMLSize_t slices[] = { 142, 1 };
5637 		const XMLSize_t count = sizeof(slices) / sizeof(slices[0]);
5638 		SlicerSource src(sampleDocXML, sampleDocSize, slices, count);
5639 		parser->parse(src);
5640 	}
5641 	catch (...)
5642 	{
5643         OK = false;
5644         fprintf(stderr, "Variable chunks parsing failed at line %i\n", __LINE__);
5645 	}
5646 
5647 	try
5648 	{
5649 		// Now, parse it in blocks that end between the '<',  '!-' and '-' of a comment
5650 		const XMLSize_t slices[] = { 142, 2 };
5651 		const XMLSize_t count = sizeof(slices) / sizeof(slices[0]);
5652 		SlicerSource src(sampleDocXML, sampleDocSize, slices, count);
5653 		parser->parse(src);
5654 	}
5655 	catch (...)
5656 	{
5657         OK = false;
5658         fprintf(stderr, "Variable chunks parsing failed at line %i\n", __LINE__);
5659 	}
5660 
5661 	try
5662 	{
5663 		// Now, parse it in blocks that end between the '<',  '!-' and '-' of a comment
5664 		static const XMLSize_t slices[] =
5665         {
5666             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5667             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5668             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5669             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5670             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5671             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5672             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5673             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5674             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5675             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5676             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5677             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5678             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5679             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5680             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5681             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5682             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5683             1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
5684             1, 1, 1, 1};
5685 		static const XMLSize_t count = sizeof(slices) / sizeof(slices[0]);
5686 		SlicerSource src(sampleDocXML, sampleDocSize, slices, count);
5687 		parser->parse(src);
5688 	}
5689 	catch (...)
5690 	{
5691         OK = false;
5692         fprintf(stderr, "Variable chunks parsing failed at line %i\n", __LINE__);
5693 	}
5694 
5695     return OK;
5696 }
5697 
5698 #define TEST_BOOLEAN(x) \
5699     if(!x)  \
5700     {       \
5701         fprintf(stderr, "Boolean expression test failed at line %i\n", __LINE__); \
5702         OK = false; \
5703     }
5704 
5705 #define TEST_STRING(x,y) \
5706     if(!XMLString::equals(x,y))  \
5707     {       \
5708         fprintf(stderr, "String expression test failed at line %i\n", __LINE__); \
5709         OK = false; \
5710     }
5711 
testUtilFunctions()5712 bool DOMTest::testUtilFunctions()
5713 {
5714     bool OK = true;
5715     // test isWSReplaced
5716     XMLString::transcode(" xyz ", tempStr, 3999);
5717     TEST_BOOLEAN(XMLString::isWSReplaced(tempStr));
5718     XMLString::transcode(" x\tyz ", tempStr, 3999);
5719     TEST_BOOLEAN(!XMLString::isWSReplaced(tempStr));
5720     XMLString::transcode(" xyz\n", tempStr, 3999);
5721     TEST_BOOLEAN(!XMLString::isWSReplaced(tempStr));
5722     XMLString::transcode("\rxyz", tempStr, 3999);
5723     TEST_BOOLEAN(!XMLString::isWSReplaced(tempStr));
5724 
5725     // test replaceWS
5726     XMLString::transcode(" x yz ", tempStr2, 3999);
5727     XMLString::transcode(" x yz ", tempStr, 3999);
5728     XMLString::replaceWS(tempStr);
5729     TEST_STRING(tempStr, tempStr2);
5730     XMLString::transcode(" x\tyz ", tempStr, 3999);
5731     XMLString::replaceWS(tempStr);
5732     TEST_STRING(tempStr, tempStr2);
5733     XMLString::transcode(" x yz\n", tempStr, 3999);
5734     XMLString::replaceWS(tempStr);
5735     TEST_STRING(tempStr, tempStr2);
5736     XMLString::transcode("\rx yz ", tempStr, 3999);
5737     XMLString::replaceWS(tempStr);
5738     TEST_STRING(tempStr, tempStr2);
5739 
5740     // test isWSCollapsed
5741     XMLString::transcode(" xyz ", tempStr, 3999);
5742     TEST_BOOLEAN(!XMLString::isWSCollapsed(tempStr));
5743     XMLString::transcode(" x\tyz ", tempStr, 3999);
5744     TEST_BOOLEAN(!XMLString::isWSCollapsed(tempStr));
5745     XMLString::transcode(" xyz\n", tempStr, 3999);
5746     TEST_BOOLEAN(!XMLString::isWSCollapsed(tempStr));
5747     XMLString::transcode("\rxyz", tempStr, 3999);
5748     TEST_BOOLEAN(!XMLString::isWSCollapsed(tempStr));
5749     XMLString::transcode("xyz", tempStr, 3999);
5750     TEST_BOOLEAN(XMLString::isWSCollapsed(tempStr));
5751     XMLString::transcode("x yz", tempStr, 3999);
5752     TEST_BOOLEAN(XMLString::isWSCollapsed(tempStr));
5753     XMLString::transcode("x  yz", tempStr, 3999);
5754     TEST_BOOLEAN(!XMLString::isWSCollapsed(tempStr));
5755 
5756     // test collapseWS
5757     XMLString::transcode("x yz", tempStr2, 3999);
5758     XMLString::transcode(" x\tyz ", tempStr, 3999);
5759     XMLString::collapseWS(tempStr);
5760     TEST_STRING(tempStr, tempStr2);
5761     XMLString::transcode("x yz", tempStr, 3999);
5762     XMLString::collapseWS(tempStr);
5763     TEST_STRING(tempStr, tempStr2);
5764     XMLString::transcode("x  yz", tempStr, 3999);
5765     XMLString::collapseWS(tempStr);
5766     TEST_STRING(tempStr, tempStr2);
5767 
5768     XMLString::transcode("xyz", tempStr2, 3999);
5769     XMLString::transcode(" xyz ", tempStr, 3999);
5770     XMLString::collapseWS(tempStr);
5771     TEST_STRING(tempStr, tempStr2);
5772     XMLString::transcode(" xyz\n", tempStr, 3999);
5773     XMLString::collapseWS(tempStr);
5774     TEST_STRING(tempStr, tempStr2);
5775     XMLString::transcode("\rxyz", tempStr, 3999);
5776     XMLString::collapseWS(tempStr);
5777     TEST_STRING(tempStr, tempStr2);
5778     XMLString::transcode("xyz", tempStr, 3999);
5779     XMLString::collapseWS(tempStr);
5780     TEST_STRING(tempStr, tempStr2);
5781 
5782     // test removeWS
5783     XMLString::transcode("xyz", tempStr2, 3999);
5784     XMLString::transcode(" x\tyz ", tempStr, 3999);
5785     XMLString::removeWS(tempStr);
5786     TEST_STRING(tempStr, tempStr2);
5787     XMLString::transcode("x yz", tempStr, 3999);
5788     XMLString::removeWS(tempStr);
5789     TEST_STRING(tempStr, tempStr2);
5790     XMLString::transcode("x  yz", tempStr, 3999);
5791     XMLString::removeWS(tempStr);
5792     TEST_STRING(tempStr, tempStr2);
5793     XMLString::transcode(" xyz ", tempStr, 3999);
5794     XMLString::removeWS(tempStr);
5795     TEST_STRING(tempStr, tempStr2);
5796     XMLString::transcode(" xyz\n", tempStr, 3999);
5797     XMLString::removeWS(tempStr);
5798     TEST_STRING(tempStr, tempStr2);
5799     XMLString::transcode("\rxyz", tempStr, 3999);
5800     XMLString::removeWS(tempStr);
5801     TEST_STRING(tempStr, tempStr2);
5802     XMLString::transcode("xyz", tempStr, 3999);
5803     XMLString::removeWS(tempStr);
5804     TEST_STRING(tempStr, tempStr2);
5805 
5806     if(XMLString::stringLen((XMLCh*)0)!=0)
5807     {
5808         fprintf(stderr, "strLen test failed at line %i\n", __LINE__);
5809         OK = false;
5810     }
5811     if(XMLString::stringLen(XMLUni::fgZeroLenString)!=0)
5812     {
5813         fprintf(stderr, "strLen test failed at line %i\n", __LINE__);
5814         OK = false;
5815     }
5816     XMLCh one[2]={ chLatin_A, chNull };
5817     if(XMLString::stringLen(one)!=1)
5818     {
5819         fprintf(stderr, "strLen test failed at line %i\n", __LINE__);
5820         OK = false;
5821     }
5822     XMLCh two[3]={ chLatin_A, chLatin_B, chNull };
5823     if(XMLString::stringLen(two)!=2)
5824     {
5825         fprintf(stderr, "strLen test failed at line %i\n", __LINE__);
5826         OK = false;
5827     }
5828 
5829     // test copyNString
5830     XMLCh buffer[100];
5831     XMLString::transcode("xyz", tempStr, 3999);
5832     if(!XMLString::copyNString(buffer, tempStr, 100))
5833     {
5834         fprintf(stderr, "copyNString test failed at line %i\n", __LINE__);
5835         OK = false;
5836     }
5837     if(!XMLString::copyNString(buffer, tempStr, 3))
5838     {
5839         fprintf(stderr, "copyNString test failed at line %i\n", __LINE__);
5840         OK = false;
5841     }
5842     if(XMLString::copyNString(buffer, tempStr, 2))
5843     {
5844         fprintf(stderr, "copyNString test failed at line %i\n", __LINE__);
5845         OK = false;
5846     }
5847     if(!XMLString::copyNString(buffer, tempStr, 4))
5848     {
5849         fprintf(stderr, "copyNString test failed at line %i\n", __LINE__);
5850         OK = false;
5851     }
5852 
5853     // test indexOf
5854     XMLString::transcode("1234567890", tempStr, 3999);
5855     if(XMLString::indexOf(tempStr, '1')!=0)
5856     {
5857         fprintf(stderr, "indexOf test failed at line %i\n", __LINE__);
5858         OK = false;
5859     }
5860     if(XMLString::indexOf(tempStr, '5')!=4)
5861     {
5862         fprintf(stderr, "indexOf test failed at line %i\n", __LINE__);
5863         OK = false;
5864     }
5865     if(XMLString::indexOf(tempStr, '0')!=9)
5866     {
5867         fprintf(stderr, "indexOf test failed at line %i\n", __LINE__);
5868         OK = false;
5869     }
5870     if(XMLString::indexOf(tempStr, 'A')!=-1)
5871     {
5872         fprintf(stderr, "indexOf test failed at line %i\n", __LINE__);
5873         OK = false;
5874     }
5875 
5876     // test lastIndexOf
5877     XMLString::transcode("1234567890", tempStr, 3999);
5878     if(XMLString::lastIndexOf(tempStr, '1')!=0)
5879     {
5880         fprintf(stderr, "lastIndexOf test failed at line %i\n", __LINE__);
5881         OK = false;
5882     }
5883     if(XMLString::lastIndexOf(tempStr, '5')!=4)
5884     {
5885         fprintf(stderr, "lastIndexOf test failed at line %i\n", __LINE__);
5886         OK = false;
5887     }
5888     if(XMLString::lastIndexOf(tempStr, '0')!=9)
5889     {
5890         fprintf(stderr, "lastIndexOf test failed at line %i\n", __LINE__);
5891         OK = false;
5892     }
5893     if(XMLString::lastIndexOf(tempStr, 'A')!=-1)
5894     {
5895         fprintf(stderr, "lastIndexOf test failed at line %i\n", __LINE__);
5896         OK = false;
5897     }
5898 
5899     // this tests the cached bit storage
5900     CMStateSet setT(60);
5901     setT.setBit(8);
5902     setT.setBit(52);
5903     setT.setBit(34);
5904 
5905     if(!setT.getBit(8) || !setT.getBit(52) || !setT.getBit(34))
5906     {
5907         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5908         OK = false;
5909     }
5910 
5911     CMStateSetEnumerator enumT(&setT);
5912     if(!enumT.hasMoreElements() || enumT.nextElement()!=8)
5913     {
5914         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5915         OK = false;
5916     }
5917     if(!enumT.hasMoreElements() || enumT.nextElement()!=34)
5918     {
5919         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5920         OK = false;
5921     }
5922     if(!enumT.hasMoreElements() || enumT.nextElement()!=52)
5923     {
5924         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5925         OK = false;
5926     }
5927     if(enumT.hasMoreElements())
5928     {
5929         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5930         OK = false;
5931     }
5932 
5933     // this tests the dynamic bit storage
5934     CMStateSet setT2(3 * CMSTATE_BITFIELD_CHUNK);
5935     setT2.setBit(0); // first block, begin
5936     setT2.setBit(CMSTATE_BITFIELD_CHUNK/2 -1); // first block, middle
5937     setT2.setBit(CMSTATE_BITFIELD_CHUNK/2); // first block, middle
5938     setT2.setBit(CMSTATE_BITFIELD_CHUNK/2 +1); // first block, middle
5939     setT2.setBit(CMSTATE_BITFIELD_CHUNK-1); // first block, end
5940     setT2.setBit(2*CMSTATE_BITFIELD_CHUNK); // last block, begin
5941     setT2.setBit(2*CMSTATE_BITFIELD_CHUNK + CMSTATE_BITFIELD_CHUNK/2 -1); // last block, middle
5942     setT2.setBit(2*CMSTATE_BITFIELD_CHUNK + CMSTATE_BITFIELD_CHUNK/2); // last block, middle
5943     setT2.setBit(2*CMSTATE_BITFIELD_CHUNK + CMSTATE_BITFIELD_CHUNK/2 +1); // last block, middle
5944     setT2.setBit(3*CMSTATE_BITFIELD_CHUNK-1); // last block, end
5945 
5946     // test just a few ones
5947     if(!setT2.getBit(0) || !setT2.getBit(CMSTATE_BITFIELD_CHUNK-1) || !setT2.getBit(2*CMSTATE_BITFIELD_CHUNK + CMSTATE_BITFIELD_CHUNK/2 +1))
5948     {
5949         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5950         OK = false;
5951     }
5952 
5953     if(setT2.getBitCountInRange(0, 3*CMSTATE_BITFIELD_CHUNK)!=10)
5954     {
5955         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5956         OK = false;
5957     }
5958     CMStateSetEnumerator enumT2(&setT2);
5959     XMLSize_t count=0;
5960     while(enumT2.hasMoreElements())
5961     {
5962         count++;
5963         enumT2.nextElement();
5964     }
5965     if(count!=10)
5966     {
5967         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5968         OK = false;
5969     }
5970 
5971     // test the enumerator with a non-default start
5972     CMStateSetEnumerator enumT2a(&setT2, CMSTATE_BITFIELD_CHUNK/2);
5973     if(!enumT2a.hasMoreElements() || enumT2a.nextElement()!= (CMSTATE_BITFIELD_CHUNK/2))
5974     {
5975         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5976         OK = false;
5977     }
5978     CMStateSetEnumerator enumT2b(&setT2, CMSTATE_BITFIELD_CHUNK/2+2);
5979     if(!enumT2b.hasMoreElements() || enumT2b.nextElement()!= (CMSTATE_BITFIELD_CHUNK-1))
5980     {
5981         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5982         OK = false;
5983     }
5984     CMStateSetEnumerator enumT2c(&setT2, 2*CMSTATE_BITFIELD_CHUNK);
5985     if(!enumT2c.hasMoreElements() || enumT2c.nextElement()!= (2*CMSTATE_BITFIELD_CHUNK))
5986     {
5987         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5988         OK = false;
5989     }
5990 
5991     // this tests the hash generator
5992     CMStateSet setT3(3 * CMSTATE_BITFIELD_CHUNK), setT4(3 * CMSTATE_BITFIELD_CHUNK);
5993     // these two sets will have a single bit set at the beginning of a chunk
5994     setT3.setBit(0);
5995     setT4.setBit(CMSTATE_BITFIELD_CHUNK);
5996     if(setT3.hashCode()==setT4.hashCode())
5997     {
5998         fprintf(stderr, "bitset test failed at line %i\n", __LINE__);
5999         OK = false;
6000     }
6001 
6002 	// TranscodeFrom/ToStr
6003 
6004 	const char* utf8 = "UTF-8";
6005 	char* empty = (char*)TranscodeToStr(XMLUni::fgZeroLenString,utf8).adopt();
6006 	if(XMLString::stringLen(empty)!=0)
6007 	{
6008         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6009         OK = false;
6010 	}
6011 	XMLCh* empty2 = TranscodeFromStr((XMLByte*)empty,strlen(empty),utf8).adopt();
6012 	if(XMLString::stringLen(empty2)!=0)
6013 	{
6014         fprintf(stderr, "TranscodeFromStr failed at line %i\n", __LINE__);
6015         OK = false;
6016 	}
6017 	XMLString::release(&empty);
6018 	XMLString::release(&empty2);
6019 
6020 	const XMLCh aval [] = { 0x0041, 0x0000}; //LATIN CAPITAL LETTER A
6021 	char* ac = (char*)TranscodeToStr(aval,utf8).adopt();
6022 	if(!XMLString::equals(ac, "A"))
6023 	{
6024         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6025         OK = false;
6026 	}
6027 	XMLCh* ac2=TranscodeFromStr((XMLByte*)ac, strlen(ac), utf8).adopt();
6028 	if(!XMLString::equals(ac2, aval))
6029 	{
6030         fprintf(stderr, "TranscodeFromStr failed at line %i\n", __LINE__);
6031         OK = false;
6032 	}
6033 	XMLString::release(&ac);
6034 	XMLString::release(&ac2);
6035 	const XMLCh uval [] = { 0x254B, 0x0000}; //BOX DRAWINGS HEAVY VERTICAL AND HORIZONTAL (needs 3 bytes for utf-8)
6036 	char* uc = (char*)TranscodeToStr(uval,utf8).adopt();
6037 	if(!XMLString::equals(uc, "\xE2\x95\x8B"))
6038 	{
6039         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6040         OK = false;
6041 	}
6042 	XMLCh* uc2=TranscodeFromStr((XMLByte*)uc, strlen(uc), utf8).adopt();
6043 	if(!XMLString::equals(uc2, uval))
6044 	{
6045         fprintf(stderr, "TranscodeFromStr failed at line %i\n", __LINE__);
6046         OK = false;
6047 	}
6048 	XMLString::release(&uc);
6049 	XMLString::release(&uc2);
6050 
6051 	XMLCh uc3[] = { 0x6B65, 0 }; // Unicode Han Character 'step, pace; walk, stroll' (U+6B65); UTF-8 = 0xE6 0xAD 0xA5 (e6ada5)
6052 	char* uc4 = (char*)TranscodeToStr(uc3, utf8).adopt();
6053 	if(!XMLString::equals(uc4, "\xE6\xAD\xA5"))
6054 	{
6055         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6056         OK = false;
6057 	}
6058 	XMLString::release(&uc4);
6059 
6060 	// Input:                    U+7D5E U+308A U+8FBC U+307F U+691C U+7D22
6061 	// Expected byte sequence:   E7 B5 9E E3 82 8A E8 BE BC E3 81 BF E6 A4 9C E7 B4 A2
6062 	const XMLCh xmlStr [] = { 0x7D5E, 0x308A, 0x8FBC, 0x307F, 0x691C, 0x7D22, 0x0000};
6063 	char* bytes = (char*)TranscodeToStr(xmlStr, "UTF-8").adopt();
6064 	if(!XMLString::equals(bytes, "\xE7\xB5\x9E\xE3\x82\x8A\xE8\xBE\xBC\xE3\x81\xBF\xE6\xA4\x9C\xE7\xB4\xA2"))
6065 	{
6066         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6067         OK = false;
6068 	}
6069 	XMLString::release(&bytes);
6070 
6071     // XERCESC-2052
6072     // Input:                    U+4E2D U+56FD U+5236 U+9020 U+4E2D U+570B U+88FD U+9020
6073     // Expected byte sequence:   E4 B8 AD E5 9B BD E5 88 B6 E9 80 A0 20 2F 20 E4 B8 AD E5 9C 8B E8 A3 BD E9 80 A0
6074     const XMLCh xmlStr2[] = { 0x4E2D, 0x56FD, 0x5236, 0x9020, 0x20, 0x2F, 0x20, 0x4E2D, 0x570B, 0x88FD, 0x9020, 0x0000 };
6075     char* bytes2 = (char*)TranscodeToStr(xmlStr2, "UTF-8").adopt();
6076     if (!XMLString::equals(bytes2, "\xE4\xB8\xAD\xE5\x9B\xBD\xE5\x88\xB6\xE9\x80\xA0\x20\x2F\x20\xE4\xB8\xAD\xE5\x9C\x8B\xE8\xA3\xBD\xE9\x80\xA0"))
6077     {
6078         fprintf(stderr, "TranscodeToStr failed at line %i\n", __LINE__);
6079         OK = false;
6080     }
6081     XMLString::release(&bytes2);
6082 
6083     return OK;
6084 }
6085