PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/tests/DOM/Normalizer/Normalizer.cpp

http://atf2flightsim.googlecode.com/
C++ | 460 lines | 261 code | 87 blank | 112 comment | 11 complexity | d1e0ec24c508d9b3c843fd9c139cfdbf MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2003 The Apache Software Foundation-> All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution->
  18. *
  19. * 3. The end-user documentation included with the redistribution,
  20. * if any, must include the following acknowledgment:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowledgment may appear in the software itself,
  24. * if and wherever such third-party acknowledgments normally appear.
  25. *
  26. * 4. The names "Xerces" and "Apache Software Foundation" must
  27. * not be used to endorse or promote products derived from this
  28. * software without prior written permission-> For written
  29. * permission, please contact apache\@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache",
  32. * nor may "Apache" appear in their name, without prior written
  33. * permission of the Apache Software Foundation->
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation, and was
  51. * originally based on software copyright (c) 2001, International
  52. * Business Machines, Inc., http://www.ibm.com . For more information
  53. * on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. */
  56. #include "Normalizer.hpp"
  57. #include <xercesc/util/PlatformUtils.hpp>
  58. #include <xercesc/framework/StdOutFormatTarget.hpp>
  59. #include <xercesc/framework/XMLBuffer.hpp>
  60. #include <xercesc/parsers/XercesDOMParser.hpp>
  61. #if defined(XERCES_NEW_IOSTREAMS)
  62. #include <iostream>
  63. #else
  64. #include <iostream.h>
  65. #endif
  66. #include <xercesc/util/XMLUni.hpp>
  67. #include <xercesc/util/XMLUniDefs.hpp>
  68. #include "../../../src/xercesc/dom/impl/DOMConfigurationImpl.hpp"
  69. #include "../../../src/xercesc/dom/impl/DOMDocumentImpl.hpp"
  70. #include "../../../src/xercesc/dom/impl/DOMEntityImpl.hpp"
  71. #include "../../../src/xercesc/dom/impl/DOMEntityReferenceImpl.hpp"
  72. // ---------------------------------------------------------------------------
  73. // This is a simple class that lets us do easy (though not terribly efficient)
  74. // trancoding of char* data to XMLCh data.
  75. // ---------------------------------------------------------------------------
  76. class XStr
  77. {
  78. public :
  79. // -----------------------------------------------------------------------
  80. // Constructors and Destructor
  81. // -----------------------------------------------------------------------
  82. XStr(const char* const toTranscode)
  83. {
  84. // Call the private transcoding method
  85. fUnicodeForm = XMLString::transcode(toTranscode);
  86. }
  87. ~XStr()
  88. {
  89. XMLString::release(&fUnicodeForm);
  90. }
  91. // -----------------------------------------------------------------------
  92. // Getter methods
  93. // -----------------------------------------------------------------------
  94. const XMLCh* unicodeForm() const
  95. {
  96. return fUnicodeForm;
  97. }
  98. private :
  99. // -----------------------------------------------------------------------
  100. // Private data members
  101. //
  102. // fUnicodeForm
  103. // This is the Unicode XMLCh format of the string.
  104. // -----------------------------------------------------------------------
  105. XMLCh* fUnicodeForm;
  106. };
  107. #define X(str) XStr(str).unicodeForm()
  108. // This is a simple class that lets us do easy (though not terribly efficient)
  109. // trancoding of XMLCh data to local code page for display.
  110. // ---------------------------------------------------------------------------
  111. class StrX
  112. {
  113. public :
  114. // -----------------------------------------------------------------------
  115. // Constructors and Destructor
  116. // -----------------------------------------------------------------------
  117. StrX(const XMLCh* const toTranscode)
  118. {
  119. // Call the private transcoding method
  120. fLocalForm = XMLString::transcode(toTranscode);
  121. }
  122. ~StrX()
  123. {
  124. XMLString::release(&fLocalForm);
  125. }
  126. // -----------------------------------------------------------------------
  127. // Getter methods
  128. // -----------------------------------------------------------------------
  129. const char* localForm() const
  130. {
  131. return fLocalForm;
  132. }
  133. private :
  134. // -----------------------------------------------------------------------
  135. // Private data members
  136. //
  137. // fLocalForm
  138. // This is the local code page form of the string.
  139. // -----------------------------------------------------------------------
  140. char* fLocalForm;
  141. };
  142. #define StrX(str) StrX(str).localForm()
  143. Normalizer::Normalizer() {
  144. try
  145. {
  146. XMLPlatformUtils::Initialize();
  147. }
  148. catch(const XMLException &toCatch)
  149. {
  150. XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n"
  151. << " Exception message:"
  152. << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
  153. }
  154. parser = 0;
  155. }
  156. Normalizer::~Normalizer() {
  157. XMLPlatformUtils::Terminate();
  158. }
  159. void Normalizer::printEntityRefNodes(DOMElement *ele) {
  160. DOMNode *child = ele->getFirstChild();
  161. while(child != 0) {
  162. if(child->getNodeType() == DOMNode::ENTITY_REFERENCE_NODE) {
  163. XERCES_STD_QUALIFIER cout << "start of entity ref node" << XERCES_STD_QUALIFIER endl;
  164. DOMNode *entChild = ((DOMEntityReference*)child)->getFirstChild();
  165. while(entChild != 0) {
  166. serializeNode(entChild);
  167. entChild = entChild->getNextSibling();
  168. }
  169. XERCES_STD_QUALIFIER cout << "\nend of entity ref node\n\n" << XERCES_STD_QUALIFIER endl;
  170. }
  171. if(child->getNodeType() == DOMNode::ELEMENT_NODE) {
  172. printEntityRefNodes((DOMElement*)child);
  173. }
  174. child = child->getNextSibling();
  175. }
  176. }
  177. bool Normalizer::handleError(const DOMError& domError)
  178. {
  179. // Display whatever error message passed from the serializer
  180. if (domError.getSeverity() == DOMError::DOM_SEVERITY_WARNING)
  181. XERCES_STD_QUALIFIER cerr << "\nWarning Message: ";
  182. else if (domError.getSeverity() == DOMError::DOM_SEVERITY_ERROR)
  183. XERCES_STD_QUALIFIER cerr << "\nError Message: ";
  184. else
  185. XERCES_STD_QUALIFIER cerr << "\nFatal Message: ";
  186. char *msg = XMLString::transcode(domError.getMessage());
  187. XERCES_STD_QUALIFIER cerr<< msg <<XERCES_STD_QUALIFIER endl;
  188. XMLString::release(&msg);
  189. XERCES_STD_QUALIFIER cerr << "Related data ";
  190. msg = XMLString::transcode(((DOMNode*)domError.getRelatedData())->getNodeName());
  191. XERCES_STD_QUALIFIER cerr << msg <<XERCES_STD_QUALIFIER endl;
  192. XMLString::release(&msg);
  193. // continue serialization if possible.
  194. return true;
  195. }
  196. DOMDocument* Normalizer::createDocument() {
  197. XMLCh coreStr[100];
  198. XMLString::transcode("Core",coreStr,99);
  199. DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(coreStr);
  200. return impl->createDocument();
  201. };
  202. void Normalizer::serializeNode(const DOMNode * const node) {
  203. XMLCh tempStr[100];
  204. XMLString::transcode("LS", tempStr, 99);
  205. DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr);
  206. DOMWriter *theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter();
  207. theSerializer->setFeature(X("format-pretty-print"), true);
  208. XMLFormatTarget *myFormTarget;
  209. myFormTarget = new StdOutFormatTarget();
  210. theSerializer->writeNode(myFormTarget, *node);
  211. }
  212. int main(int argc, char **argv) {
  213. Normalizer *normalizer = new Normalizer();
  214. DOMDocument *doc = normalizer->createDocument();
  215. bool *tmpTrue = new bool(true);
  216. bool *tmpFalse = new bool(false);
  217. DOMElement* docFirstElement = doc->createElementNS(X("http://www.test.com"),X("docEle"));
  218. doc->appendChild(docFirstElement);
  219. DOMElement* docFirstElementChild = doc->createElementNS(X("http://www.test2.com"),X("docEleChild"));
  220. docFirstElement->appendChild(docFirstElementChild);
  221. //create default ns
  222. doc->normalizeDocument();
  223. normalizer->serializeNode(doc);
  224. XERCES_STD_QUALIFIER cout << "\n\n";
  225. //add in binding
  226. docFirstElement->setPrefix(X("po"));
  227. doc->normalizeDocument();
  228. normalizer->serializeNode(doc);
  229. XERCES_STD_QUALIFIER cout << "\n\n";
  230. //use default
  231. DOMElement* docFirstElementChildChild = doc->createElementNS(X("http://www.test2.com"),X("docEleChildChild"));
  232. docFirstElementChild->appendChild(docFirstElementChildChild);
  233. doc->normalizeDocument();
  234. normalizer->serializeNode(doc);
  235. XERCES_STD_QUALIFIER cout << "\n\n";
  236. //use a binding
  237. XMLBuffer buf;
  238. buf.set(XMLUni::fgXMLNSString);
  239. buf.append(chColon);
  240. buf.append(X("po2"));
  241. docFirstElementChild->removeAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString);
  242. docFirstElement->removeAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString);
  243. docFirstElement->setAttributeNS(XMLUni::fgXMLNSURIName, buf.getRawBuffer(), X("http://www.test2.com"));
  244. docFirstElementChild->setPrefix(X("po2"));
  245. doc->normalizeDocument();
  246. normalizer->serializeNode(doc);
  247. XERCES_STD_QUALIFIER cout << "\n\n";
  248. //some siblngs to ensure the scope stacks are working
  249. docFirstElementChildChild = doc->createElementNS(X("http://www.test3.com"),X("docEleChildChild2"));
  250. docFirstElementChild->appendChild(docFirstElementChildChild);
  251. docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("po4:docEleChildChild3"));
  252. docFirstElementChild->appendChild(docFirstElementChildChild);
  253. docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("po4:docEleChildChild4"));
  254. docFirstElementChild->appendChild(docFirstElementChildChild);
  255. doc->normalizeDocument();
  256. normalizer->serializeNode(doc);
  257. XERCES_STD_QUALIFIER cout << "\n\n";
  258. //conflicting prefix
  259. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("po4"), X("conflict"));
  260. doc->normalizeDocument();
  261. normalizer->serializeNode(doc);
  262. XERCES_STD_QUALIFIER cout << "\n\n";
  263. //conflicting default
  264. docFirstElementChildChild = doc->createElementNS(X("http://www.test4.com"),X("docEleChildChild5"));
  265. docFirstElementChild->appendChild(docFirstElementChildChild);
  266. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, XMLUni::fgXMLNSString, X("conflict"));
  267. doc->normalizeDocument();
  268. normalizer->serializeNode(doc);
  269. XERCES_STD_QUALIFIER cout << "\n\n";
  270. //set the xmlns to ""
  271. DOMElement *noNamespaceEle = doc->createElementNS(X(""),X("noNamespace"));
  272. docFirstElementChildChild->appendChild(noNamespaceEle);
  273. doc->normalizeDocument();
  274. normalizer->serializeNode(doc);
  275. XERCES_STD_QUALIFIER cout << "\n\n";
  276. //now lets do a bit off attribute testing on the doc ele
  277. docFirstElement->setAttributeNS(X("http://testattr.com"), X("attr1"), X("value"));
  278. docFirstElement->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
  279. docFirstElement->setAttributeNS(X("http://testattr2.com"), X("attr3"), X("value"));
  280. docFirstElement->setAttributeNS(X("http://www.test.com"), X("attr4"), X("value"));
  281. docFirstElement->setAttributeNS(X("http://testattr2.com"), X("po:attr5"), X("value"));
  282. docFirstElement->setAttributeNS(X("http://testattr2.com"), X("poFake:attr6"), X("value"));
  283. docFirstElement->setAttributeNS(X("http://testattr3.com"), X("po3:attr7"), X("value"));
  284. doc->normalizeDocument();
  285. normalizer->serializeNode(doc);
  286. XERCES_STD_QUALIFIER cout << "\n\n";
  287. //and now on one of its children
  288. docFirstElementChildChild->setAttributeNS(X("http://testattr.com"), X("attr1"), X("value"));
  289. docFirstElementChildChild->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
  290. docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("attr3"), X("value"));
  291. docFirstElementChildChild->setAttributeNS(X("http://www.test.com"), X("attr4"), X("value"));
  292. docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("po:attr5"), X("value"));
  293. docFirstElementChildChild->setAttributeNS(X("http://testattr2.com"), X("poFake:attr6"), X("value"));
  294. docFirstElementChildChild->setAttributeNS(X("http://testattr3.com"), X("po3:attr7"), X("value"));
  295. docFirstElementChildChild->setAttributeNS(X("http://testattr4.com"), X("po4:attr8"), X("value"));
  296. //test for a clash with our NSx attrs
  297. docFirstElementChildChild->setAttributeNS(X("http://testclash.com"), X("NS1:attr9"), X("value"));
  298. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:NS1"), X("http://testclash.com"));
  299. //clash with standard prefix
  300. docFirstElementChildChild->setAttributeNS(X("http://testattr5.com"), X("po:attr10"), X("value"));
  301. doc->normalizeDocument();
  302. normalizer->serializeNode(doc);
  303. XERCES_STD_QUALIFIER cout << "\n\n";
  304. //2 prefix with the same uri
  305. docFirstElementChildChild = doc->createElementNS(X("http://www.uri1.com"),X("docEleChildChild6"));
  306. docFirstElementChild->appendChild(docFirstElementChildChild);
  307. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:uri1"), X("http://www.uri1.com"));
  308. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:uri1b"), X("http://www.uri1.com"));
  309. docFirstElementChildChild->setAttributeNS(X("http://www.uri1.com"), X("uri1:attr1"), X("value"));
  310. docFirstElementChildChild->setAttributeNS(X("http://www.uri1.com"), X("uri1b:attr2"), X("value"));
  311. doc->normalizeDocument();
  312. normalizer->serializeNode(doc);
  313. XERCES_STD_QUALIFIER cout << "\n\n";
  314. //check to see we use the nearest binding and for more inheritence
  315. DOMElement *docFirstElementChildChildChild = doc->createElementNS(X("http://www.uri1.com"),X("docEleChildChildChild"));
  316. docFirstElementChildChild->appendChild(docFirstElementChildChildChild);
  317. docFirstElementChildChild->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:nearerThanPo"), X("http://www.test.com"));
  318. docFirstElementChildChildChild->setAttributeNS(X("http://testattr.com"), X("attr2"), X("value"));
  319. docFirstElementChildChildChild->setAttributeNS(X("http://www.test.com"), X("attr1"), X("value"));
  320. doc->normalizeDocument();
  321. normalizer->serializeNode(doc);
  322. XERCES_STD_QUALIFIER cout << "\n\n";
  323. //NS1.1 stuff
  324. //test creating default prefix when NS1 has been set to ""
  325. noNamespaceEle->setAttributeNS(XMLUni::fgXMLNSURIName, X("xmlns:NS1"), X(""));
  326. DOMElement *noNamespaceChild = doc->createElementNS(X("http://testclash.com"),X("testing1.1Stuff"));
  327. noNamespaceEle->appendChild(noNamespaceChild);
  328. doc->normalizeDocument();
  329. normalizer->serializeNode(doc);
  330. noNamespaceChild = doc->createElementNS(X("http://testclash.com"),X("NS1:testing1.1Stuff"));
  331. noNamespaceEle->appendChild(noNamespaceChild);
  332. noNamespaceChild->setAttributeNS(X("http://www.someRandomUri.com"), X("attr"), X("value"));
  333. doc->normalizeDocument();
  334. normalizer->serializeNode(doc);
  335. //check error conditions
  336. XERCES_STD_QUALIFIER cout << "error conditions" << XERCES_STD_QUALIFIER endl;
  337. DOMConfigurationImpl *cImpl = new ((DOMDocumentImpl*)doc) DOMConfigurationImpl();
  338. ((DOMDocumentImpl*)doc)->setDOMConfiguration(cImpl);
  339. cImpl->setErrorHandler(normalizer);
  340. cImpl->setParameter(X("namespaces"), (void*)tmpTrue);
  341. DOMElement *level1Node = doc->createElement(X("level1Node"));
  342. docFirstElement->appendChild(level1Node);
  343. doc->normalizeDocument();
  344. docFirstElement->removeChild(level1Node);
  345. docFirstElement->setAttribute(X("level1Attr"), X("level1"));
  346. doc->normalizeDocument();
  347. docFirstElement->removeAttribute(X("level1Attr"));
  348. //cant check this as Xerces does not let us do it
  349. // noNamespaceChild->setAttributeNS(X("http://www.someRandomUri.com"), X("xmlns"), X("value"));
  350. // doc->normalizeDocument();
  351. //lets do a sanity test on a comment
  352. DOMComment *comment = doc->createComment(X("some comment"));
  353. docFirstElement->appendChild(comment);
  354. doc->normalizeDocument();
  355. normalizer->serializeNode(doc);
  356. cImpl->setParameter(X("comments"), (void*)tmpFalse);
  357. docFirstElement->appendChild(comment);
  358. doc->normalizeDocument();
  359. normalizer->serializeNode(doc);
  360. //and on a CDATA
  361. DOMCDATASection *cData = doc->createCDATASection(X("some cdata"));
  362. docFirstElement->appendChild(cData);
  363. doc->normalizeDocument();
  364. normalizer->serializeNode(doc);
  365. cImpl->setParameter(X("cdata-sections"), (void*)tmpFalse);
  366. docFirstElement->appendChild(cData);
  367. doc->normalizeDocument();
  368. normalizer->serializeNode(doc);
  369. delete normalizer;
  370. delete tmpTrue;
  371. delete tmpFalse;
  372. return 0;
  373. }