source: icXML/icXML-devel/tests/src/DOM/Normalizer/Normalizer.cpp @ 2726

Last change on this file since 2726 was 2726, checked in by cameron, 6 years ago

Add original Xerces tests and samples directories

File size: 16.1 KB
Line 
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#include "Normalizer.hpp"
19#include <xercesc/util/PlatformUtils.hpp>
20#include <xercesc/framework/StdOutFormatTarget.hpp>
21#include <xercesc/framework/XMLBuffer.hpp>
22#include <xercesc/parsers/XercesDOMParser.hpp>
23
24#if defined(XERCES_NEW_IOSTREAMS)
25#include <iostream>
26#else
27#include <iostream.h>
28#endif
29
30#include <xercesc/util/XMLUni.hpp>
31#include <xercesc/util/XMLUniDefs.hpp>
32#include "xercesc/dom/impl/DOMConfigurationImpl.hpp"
33#include "xercesc/dom/impl/DOMDocumentImpl.hpp"
34#include "xercesc/dom/impl/DOMEntityImpl.hpp"
35#include "xercesc/dom/impl/DOMEntityReferenceImpl.hpp"
36
37// ---------------------------------------------------------------------------
38//  This is a simple class that lets us do easy (though not terribly efficient)
39//  trancoding of char* data to XMLCh data.
40// ---------------------------------------------------------------------------
41class XStr
42{
43public :
44    // -----------------------------------------------------------------------
45    //  Constructors and Destructor
46    // -----------------------------------------------------------------------
47    XStr(const char* const toTranscode)
48    {
49        // Call the private transcoding method
50        fUnicodeForm = XMLString::transcode(toTranscode);
51    }
52
53    ~XStr()
54    {
55        XMLString::release(&fUnicodeForm);
56    }
57
58
59    // -----------------------------------------------------------------------
60    //  Getter methods
61    // -----------------------------------------------------------------------
62    const XMLCh* unicodeForm() const
63    {
64        return fUnicodeForm;
65    }
66
67private :
68    // -----------------------------------------------------------------------
69    //  Private data members
70    //
71    //  fUnicodeForm
72    //      This is the Unicode XMLCh format of the string.
73    // -----------------------------------------------------------------------
74    XMLCh*   fUnicodeForm;
75};
76
77#define X(str) XStr(str).unicodeForm()
78
79
80//  This is a simple class that lets us do easy (though not terribly efficient)
81//  trancoding of XMLCh data to local code page for display.
82// ---------------------------------------------------------------------------
83class StrX
84{
85public :
86    // -----------------------------------------------------------------------
87    //  Constructors and Destructor
88    // -----------------------------------------------------------------------
89    StrX(const XMLCh* const toTranscode)
90    {
91        // Call the private transcoding method
92        fLocalForm = XMLString::transcode(toTranscode);
93    }
94
95    ~StrX()
96    {
97        XMLString::release(&fLocalForm);
98    }
99
100
101    // -----------------------------------------------------------------------
102    //  Getter methods
103    // -----------------------------------------------------------------------
104    const char* localForm() const
105    {
106        return fLocalForm;
107    }
108
109private :
110    // -----------------------------------------------------------------------
111    //  Private data members
112    //
113    //  fLocalForm
114    //      This is the local code page form of the string.
115    // -----------------------------------------------------------------------
116    char*   fLocalForm;
117};
118
119#define StrX(str) StrX(str).localForm()
120
121
122
123Normalizer::Normalizer() {
124    try
125    {
126        XMLPlatformUtils::Initialize();
127    }
128
129    catch(const XMLException &toCatch)
130    {
131        XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
132             << "  Exception message:"
133             << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
134    }
135    parser = 0;
136
137}
138
139Normalizer::~Normalizer() {
140    XMLPlatformUtils::Terminate();
141}
142
143void Normalizer::printEntityRefNodes(DOMElement *ele) {
144   
145    DOMNode *child = ele->getFirstChild();
146
147    while(child != 0) {
148        if(child->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE) {
149            XERCES_STD_QUALIFIER cout << "start of entity ref node" << XERCES_STD_QUALIFIER endl;
150            DOMNode *entChild = ((DOMEntityReference*)child)->getFirstChild();
151            while(entChild != 0) {
152                serializeNode(entChild);
153                entChild = entChild->getNextSibling();
154            }
155            XERCES_STD_QUALIFIER cout << "\nend of entity ref node\n\n" << XERCES_STD_QUALIFIER endl;
156
157        }
158
159        if(child->getNodeType() == DOMNode::ELEMENT_NODE) {
160            printEntityRefNodes((DOMElement*)child);
161        }
162
163        child = child->getNextSibling();
164    }
165   
166}
167
168
169bool Normalizer::handleError(const DOMError& domError)
170{
171    // Display whatever error message passed from the serializer
172    if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)
173        XERCES_STD_QUALIFIER cerr << "\nWarning Message: ";
174    else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)
175        XERCES_STD_QUALIFIER cerr << "\nError Message: ";
176    else
177        XERCES_STD_QUALIFIER cerr << "\nFatal Message: ";
178
179    char *msg = XMLString::transcode(domError.getMessage());
180    XERCES_STD_QUALIFIER cerr<< msg <<XERCES_STD_QUALIFIER endl;
181    XMLString::release(&msg);
182
183    XERCES_STD_QUALIFIER cerr << "Related data ";
184
185    msg = XMLString::transcode(((DOMNode*)domError.getRelatedData())->getNodeName());
186    XERCES_STD_QUALIFIER cerr << msg <<XERCES_STD_QUALIFIER endl;
187    XMLString::release(&msg);
188
189 
190
191    // continue serialization if possible.
192    return true;
193}
194
195
196
197DOMDocument* Normalizer::createDocument() {
198    XMLCh coreStr[100];
199    XMLString::transcode("Core",coreStr,99);
200
201    DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(coreStr);
202    return impl->createDocument();
203};
204
205void Normalizer::serializeNode(const DOMNode * const node) {
206    XMLCh tempStr[100];
207    XMLString::transcode("LS", tempStr, 99);
208    DOMImplementation *impl          = DOMImplementationRegistry::getDOMImplementation(tempStr);
209    DOMLSSerializer   *theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
210    DOMLSOutput       *theOutput     = ((DOMImplementationLS*)impl)->createLSOutput();
211    theSerializer->getDomConfig()->setParameter(X("format-pretty-print"), true);
212    XMLFormatTarget *myFormTarget;
213    myFormTarget = new StdOutFormatTarget();
214
215    theOutput->setByteStream(myFormTarget);
216    theSerializer->write(node,theOutput);
217    delete myFormTarget;
218    theSerializer->release();
219    theOutput->release();
220}
221
222int main(int /*argc*/, char ** /*argv*/) {
223
224    Normalizer *normalizer = new Normalizer();
225
226    DOMDocument *doc = normalizer->createDocument();
227    bool *tmpTrue = new bool(true);
228    bool *tmpFalse = new bool(false);
229
230    DOMElement* docFirstElement = doc->createElementNS(X("http://www.test.com"),X("docEle"));
231    doc->appendChild(docFirstElement);
232    DOMElement* docFirstElementChild = doc->createElementNS(X("http://www.test2.com"),X("docEleChild"));
233    docFirstElement->appendChild(docFirstElementChild);
234
235    //create default ns
236    doc->normalizeDocument();
237    normalizer->serializeNode(doc);
238    XERCES_STD_QUALIFIER cout << "\n\n";
239
240    //add in binding
241    docFirstElement->setPrefix(X("po"));
242    doc->normalizeDocument();
243    normalizer->serializeNode(doc);
244    XERCES_STD_QUALIFIER cout << "\n\n";
245
246    //use default
247    DOMElement* docFirstElementChildChild = doc->createElementNS(X("http://www.test2.com"),X("docEleChildChild"));
248    docFirstElementChild->appendChild(docFirstElementChildChild);
249    doc->normalizeDocument();
250    normalizer->serializeNode(doc);
251    XERCES_STD_QUALIFIER cout << "\n\n";
252
253    // this block is needed to destroy the XMLBuffer
254    {
255        //use a binding
256        XMLBuffer buf;
257        buf.set(XMLUni::fgXMLNSString);
258        buf.append(chColon);
259        buf.append(X("po2"));
260        docFirstElementChild->removeAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString);
261        docFirstElement->removeAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString);
262        docFirstElement->setAttributeNS(XMLUni::fgXMLNSURIName, buf.getRawBuffer(), X("http://www.test2.com"));
263        docFirstElementChild->setPrefix(X("po2"));
264        doc->normalizeDocument();
265        normalizer->serializeNode(doc);
266        XERCES_STD_QUALIFIER cout << "\n\n";
267    }
268
269    //some siblngs to ensure the scope stacks are working
270    docFirstElementChildChild = doc->createElementNS(X("http://www.test3.com"),X("docEleChildChild2"));
271    docFirstElementChild->appendChild(docFirstElementChildChild);
272    docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("po4:docEleChildChild3"));
273    docFirstElementChild->appendChild(docFirstElementChildChild);
274    docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("po4:docEleChildChild4"));
275    docFirstElementChild->appendChild(docFirstElementChildChild);
276    doc->normalizeDocument();
277    normalizer->serializeNode(doc);
278    XERCES_STD_QUALIFIER cout << "\n\n";
279
280    //conflicting prefix
281    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("po4"), X("conflict"));
282    doc->normalizeDocument();
283    normalizer->serializeNode(doc);
284    XERCES_STD_QUALIFIER cout << "\n\n";
285   
286    //conflicting default
287    docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("docEleChildChild5"));
288    docFirstElementChild->appendChild(docFirstElementChildChild);
289    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString, X("conflict"));
290    doc->normalizeDocument();
291    normalizer->serializeNode(doc);
292    XERCES_STD_QUALIFIER cout << "\n\n";
293
294    //set the xmlns to ""
295    DOMElement *noNamespaceEle = doc->createElementNS(X(""),X("noNamespace"));
296    docFirstElementChildChild->appendChild(noNamespaceEle);
297    doc->normalizeDocument();
298    normalizer->serializeNode(doc);
299    XERCES_STD_QUALIFIER cout << "\n\n";
300
301
302    //now lets do a bit off attribute testing on the doc ele
303    docFirstElement->setAttributeNS(X("http://testattr.com"), X("attr1"), X("value"));
304    docFirstElement->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
305    docFirstElement->setAttributeNS(X("http://testattr2.com"), X("attr3"), X("value"));
306    docFirstElement->setAttributeNS(X("http://www.test.com"), X("attr4"), X("value"));
307    docFirstElement->setAttributeNS(X("http://testattr2.com"), X("po:attr5"), X("value"));
308    docFirstElement->setAttributeNS(X("http://testattr2.com"), X("poFake:attr6"), X("value"));
309    docFirstElement->setAttributeNS(X("http://testattr3.com"), X("po3:attr7"), X("value"));
310    doc->normalizeDocument();
311    normalizer->serializeNode(doc);
312    XERCES_STD_QUALIFIER cout << "\n\n";
313
314    //and now on one of its children
315    docFirstElementChildChild->setAttributeNS(X("http://testattr.com"), X("attr1"), X("value"));
316    docFirstElementChildChild->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
317    docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("attr3"), X("value"));
318    docFirstElementChildChild->setAttributeNS(X("http://www.test.com"), X("attr4"), X("value"));
319    docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("po:attr5"), X("value"));
320    docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("poFake:attr6"), X("value"));
321    docFirstElementChildChild->setAttributeNS(X("http://testattr3.com"), X("po3:attr7"), X("value"));
322    docFirstElementChildChild->setAttributeNS(X("http://testattr4.com"), X("po4:attr8"), X("value"));
323   
324
325    //test for a clash with our NSx attrs
326    docFirstElementChildChild->setAttributeNS(X("http://testclash.com"), X("NS1:attr9"), X("value"));
327    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:NS1"), X("http://testclash.com"));
328
329    //clash with standard prefix
330    docFirstElementChildChild->setAttributeNS(X("http://testattr5.com"), X("po:attr10"), X("value"));
331
332    doc->normalizeDocument();
333    normalizer->serializeNode(doc);
334    XERCES_STD_QUALIFIER cout << "\n\n";
335
336
337    //2 prefix with the same uri
338    docFirstElementChildChild = doc->createElementNS(X("http://www.uri1.com"),X("docEleChildChild6"));
339    docFirstElementChild->appendChild(docFirstElementChildChild);
340    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:uri1"), X("http://www.uri1.com"));
341    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:uri1b"), X("http://www.uri1.com"));
342    docFirstElementChildChild->setAttributeNS(X("http://www.uri1.com"), X("uri1:attr1"), X("value"));
343    docFirstElementChildChild->setAttributeNS(X("http://www.uri1.com"), X("uri1b:attr2"), X("value"));
344    doc->normalizeDocument();
345    normalizer->serializeNode(doc);
346    XERCES_STD_QUALIFIER cout << "\n\n";
347
348    //check to see we use the nearest binding and for more inheritence
349    DOMElement *docFirstElementChildChildChild = doc->createElementNS(X("http://www.uri1.com"),X("docEleChildChildChild"));
350    docFirstElementChildChild->appendChild(docFirstElementChildChildChild);
351    docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:nearerThanPo"), X("http://www.test.com"));
352    docFirstElementChildChildChild->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
353    docFirstElementChildChildChild->setAttributeNS(X("http://www.test.com"), X("attr1"), X("value"));
354    doc->normalizeDocument();
355    normalizer->serializeNode(doc);
356    XERCES_STD_QUALIFIER cout << "\n\n";
357
358
359    //NS1.1 stuff
360
361    //test creating default prefix when NS1 has been set to ""
362    noNamespaceEle->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:NS1"), X(""));
363    DOMElement *noNamespaceChild = doc->createElementNS(X("http://testclash.com"),X("testing1.1Stuff"));
364    noNamespaceEle->appendChild(noNamespaceChild);
365    doc->normalizeDocument();
366    normalizer->serializeNode(doc);
367
368    noNamespaceChild = doc->createElementNS(X("http://testclash.com"),X("NS1:testing1.1Stuff"));
369    noNamespaceEle->appendChild(noNamespaceChild);
370   
371    noNamespaceChild->setAttributeNS(X("http://www.someRandomUri.com"), X("attr"), X("value"));
372    doc->normalizeDocument();
373    normalizer->serializeNode(doc);
374
375
376    //check error conditions
377    XERCES_STD_QUALIFIER cout << "error conditions" << XERCES_STD_QUALIFIER endl;
378
379    DOMConfiguration *conf = doc->getDOMConfig();
380    conf->setParameter(XMLUni::fgDOMErrorHandler, normalizer);
381    conf->setParameter(XMLUni::fgDOMNamespaces, true);
382
383    DOMElement *level1Node = doc->createElement(X("level1Node"));
384    docFirstElement->appendChild(level1Node);
385    doc->normalizeDocument();
386
387    docFirstElement->removeChild(level1Node);
388    docFirstElement->setAttribute(X("level1Attr"), X("level1"));
389    doc->normalizeDocument();
390    docFirstElement->removeAttribute(X("level1Attr"));
391
392    //cant check this as Xerces does not let us do it
393    //    noNamespaceChild->setAttributeNS(X("http://www.someRandomUri.com"), X("xmlns"), X("value"));
394    //    doc->normalizeDocument();
395
396
397
398    //lets do a sanity test on a comment
399    DOMComment *comment = doc->createComment(X("some comment"));
400    docFirstElement->appendChild(comment);
401    doc->normalizeDocument();
402    normalizer->serializeNode(doc);
403
404    conf->setParameter(XMLUni::fgDOMComments, false);
405    docFirstElement->appendChild(comment);
406    doc->normalizeDocument();
407    normalizer->serializeNode(doc);
408
409
410    //and on a CDATA
411    DOMCDATASection *cData = doc->createCDATASection(X("some cdata"));
412    docFirstElement->appendChild(cData);
413    doc->normalizeDocument();
414    normalizer->serializeNode(doc);
415
416    conf->setParameter(XMLUni::fgDOMCDATASections, false);
417    docFirstElement->appendChild(cData);
418    doc->normalizeDocument();
419    normalizer->serializeNode(doc);
420
421    delete normalizer;
422    delete tmpTrue;
423    delete tmpFalse;
424
425    return 0;
426}
427
428
Note: See TracBrowser for help on using the repository browser.