PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/src/xercesc/util/QName.cpp

http://atf2flightsim.googlecode.com/
C++ | 547 lines | 289 code | 60 blank | 198 comment | 26 complexity | 681c99c09a6bb0bd28c9cbd20a18e228 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) 2001 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. /*
  57. * $Log: QName.cpp,v $
  58. * Revision 1.1.1.1 2009/03/14 06:42:21 whitegr
  59. * epics channel archiver
  60. *
  61. * Revision 1.11 2003/10/01 16:32:39 neilg
  62. * improve handling of out of memory conditions, bug #23415. Thanks to David Cargill.
  63. *
  64. * Revision 1.10 2003/09/25 22:24:28 peiyongz
  65. * Using writeString/readString
  66. *
  67. * Revision 1.9 2003/09/25 15:22:34 peiyongz
  68. * Implementation of Serialization
  69. *
  70. * Revision 1.8 2003/05/16 06:01:52 knoaman
  71. * Partial implementation of the configurable memory manager.
  72. *
  73. * Revision 1.7 2003/05/15 19:04:35 knoaman
  74. * Partial implementation of the configurable memory manager.
  75. *
  76. * Revision 1.6 2002/11/04 15:22:04 tng
  77. * C++ Namespace Support.
  78. *
  79. * Revision 1.5 2002/10/30 21:52:00 tng
  80. * [Bug 13641] compiler-generated copy-constructor for QName doesn't do the right thing.
  81. *
  82. * Revision 1.4 2002/09/24 19:51:24 tng
  83. * Performance: use XMLString::equals instead of XMLString::compareString
  84. *
  85. * Revision 1.3 2002/09/05 16:06:41 tng
  86. * [Bug 12232] Make operator to be constant.
  87. *
  88. * Revision 1.2 2002/04/02 15:31:48 knoaman
  89. * Modiy QName comparison (operator=).
  90. *
  91. * Revision 1.1.1.1 2002/02/01 22:22:11 peiyongz
  92. * sane_include
  93. *
  94. * Revision 1.8 2001/12/06 17:48:36 tng
  95. * Performance Enhancement. Added setNPrefix and setNLocalPart methods that allow code to take advantage of the fact that it knows the length of the prefix and local name, when possible. That can avoid a copy of the prefix into a null-terminated temporary variable before copying into the fPrefix.
  96. * Also changed the getRawName method so that it would simply return the local part when there is no prefix, instead of allocating another buffer to copy the local part into the fRawName.
  97. * When there is a prefix, changed the getRawName to copy the prefix and local part into the fRawName using XMLString::moveChars instead of using XMLString::copyString and XMLString::catString. The catString method has to loop past the prefix portion of the fRawName, which seems like a waste.
  98. * By Henry Zongaro.
  99. *
  100. * Revision 1.7 2001/07/24 18:31:47 knoaman
  101. * Added support for <group> + extra constraint checking for complexType
  102. *
  103. * Revision 1.6 2001/05/11 13:26:28 tng
  104. * Copyright update.
  105. *
  106. * Revision 1.5 2001/04/19 18:17:08 tng
  107. * Schema: SchemaValidator update, and use QName in Content Model
  108. *
  109. * Revision 1.4 2001/03/21 21:56:11 tng
  110. * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  111. *
  112. * Revision 1.3 2001/02/27 14:48:35 tng
  113. * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
  114. *
  115. * Revision 1.2 2001/02/26 21:56:15 tng
  116. * Schema: QName can also be constructed with rawName.
  117. *
  118. * Revision 1.1 2001/02/26 19:44:25 tng
  119. * Schema: add utility class QName, by Pei Yong Zhang.
  120. *
  121. */
  122. #include <xercesc/util/QName.hpp>
  123. #include <xercesc/util/OutOfMemoryException.hpp>
  124. XERCES_CPP_NAMESPACE_BEGIN
  125. // ---------------------------------------------------------------------------
  126. // QName: Constructors and Destructor
  127. // ---------------------------------------------------------------------------
  128. QName::QName(MemoryManager* const manager) :
  129. fMemoryManager(manager)
  130. , fPrefix(0)
  131. , fPrefixBufSz(0)
  132. , fLocalPart(0)
  133. , fLocalPartBufSz(0)
  134. , fRawName(0)
  135. , fRawNameBufSz(0)
  136. , fURIId(0)
  137. {
  138. }
  139. QName::QName( const XMLCh* const prefix
  140. , const XMLCh* const localPart
  141. , const unsigned int uriId
  142. , MemoryManager* const manager) :
  143. fMemoryManager(manager)
  144. , fPrefix(0)
  145. , fPrefixBufSz(0)
  146. , fLocalPart(0)
  147. , fLocalPartBufSz(0)
  148. , fRawName(0)
  149. , fRawNameBufSz(0)
  150. , fURIId(0)
  151. {
  152. try
  153. {
  154. //
  155. // Just call the local setters to set up everything. Too much
  156. // work is required to replicate that functionality here.
  157. //
  158. setName(prefix, localPart, uriId);
  159. }
  160. catch(const OutOfMemoryException&)
  161. {
  162. throw;
  163. }
  164. catch(...)
  165. {
  166. cleanUp();
  167. }
  168. }
  169. QName::QName( const XMLCh* const rawName
  170. , const unsigned int uriId
  171. , MemoryManager* const manager) :
  172. fMemoryManager(manager)
  173. , fPrefix(0)
  174. , fPrefixBufSz(0)
  175. , fLocalPart(0)
  176. , fLocalPartBufSz(0)
  177. , fRawName(0)
  178. , fRawNameBufSz(0)
  179. , fURIId(0)
  180. {
  181. try
  182. {
  183. //
  184. // Just call the local setters to set up everything. Too much
  185. // work is required to replicate that functionality here.
  186. //
  187. setName(rawName, uriId);
  188. }
  189. catch(const OutOfMemoryException&)
  190. {
  191. throw;
  192. }
  193. catch(...)
  194. {
  195. cleanUp();
  196. }
  197. }
  198. QName::~QName()
  199. {
  200. cleanUp();
  201. }
  202. // ---------------------------------------------------------------------------
  203. // QName: Copy Constructors
  204. // ---------------------------------------------------------------------------
  205. QName::QName(const QName& qname) :
  206. fMemoryManager(qname.fMemoryManager)
  207. , fPrefix(0)
  208. , fPrefixBufSz(0)
  209. , fLocalPart(0)
  210. , fLocalPartBufSz(0)
  211. , fRawName(0)
  212. , fRawNameBufSz(0)
  213. , fURIId(0)
  214. {
  215. unsigned int newLen;
  216. newLen = XMLString::stringLen(qname.getLocalPart());
  217. fLocalPartBufSz = newLen + 8;
  218. fLocalPart = (XMLCh*) fMemoryManager->allocate
  219. (
  220. (fLocalPartBufSz + 1) * sizeof(XMLCh)
  221. ); //new XMLCh[fLocalPartBufSz + 1];
  222. XMLString::moveChars(fLocalPart, qname.getLocalPart(), newLen + 1);
  223. newLen = XMLString::stringLen(qname.getPrefix());
  224. fPrefixBufSz = newLen + 8;
  225. fPrefix = (XMLCh*) fMemoryManager->allocate
  226. (
  227. (fPrefixBufSz + 1) * sizeof(XMLCh)
  228. ); //new XMLCh[fPrefixBufSz + 1];
  229. XMLString::moveChars(fPrefix, qname.getPrefix(), newLen + 1);
  230. fURIId = qname.getURI();
  231. }
  232. // ---------------------------------------------------------------------------
  233. // QName: Getter methods
  234. // ---------------------------------------------------------------------------
  235. const XMLCh* QName::getRawName() const
  236. {
  237. //
  238. // If there is no buffer, or if there is but we've not faulted in the
  239. // value yet, then we have to do that now.
  240. //
  241. if (!fRawName || !*fRawName)
  242. {
  243. //
  244. // If we have a prefix, then do the prefix:name version. Else, its
  245. // just the name.
  246. //
  247. if (*fPrefix)
  248. {
  249. //
  250. // Calculate the worst case size buffer we will need. We use the
  251. // current high water marks of the prefix and name buffers, so it
  252. // might be a little wasteful of memory but we don't have to do
  253. // string len operations on the two strings.
  254. //
  255. const unsigned int neededLen = fPrefixBufSz + fLocalPartBufSz + 1;
  256. //
  257. // If no buffer, or the current one is too small, then allocate one
  258. // and get rid of any old one.
  259. //
  260. if (!fRawName || (neededLen > fRawNameBufSz))
  261. {
  262. fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  263. // We have to cast off the const'ness to do this
  264. ((QName*)this)->fRawNameBufSz = neededLen;
  265. ((QName*)this)->fRawName = (XMLCh*) fMemoryManager->allocate
  266. (
  267. (neededLen + 1) * sizeof(XMLCh)
  268. ); //new XMLCh[neededLen + 1];
  269. // Make sure its initially empty
  270. *fRawName = 0;
  271. }
  272. const unsigned int prefixLen = XMLString::stringLen(fPrefix);
  273. XMLString::moveChars(fRawName, fPrefix, prefixLen);
  274. fRawName[prefixLen] = chColon;
  275. XMLString::copyString(&fRawName[prefixLen+1], fLocalPart);
  276. }
  277. else
  278. {
  279. return fLocalPart;
  280. }
  281. }
  282. return fRawName;
  283. }
  284. XMLCh* QName::getRawName()
  285. {
  286. //
  287. // If there is no buffer, or if there is but we've not faulted in the
  288. // value yet, then we have to do that now.
  289. //
  290. if (!fRawName || !*fRawName)
  291. {
  292. //
  293. // If we have a prefix, then do the prefix:name version. Else, its
  294. // just the name.
  295. //
  296. if (*fPrefix)
  297. {
  298. //
  299. // Calculate the worst case size buffer we will need. We use the
  300. // current high water marks of the prefix and name buffers, so it
  301. // might be a little wasteful of memory but we don't have to do
  302. // string len operations on the two strings.
  303. //
  304. const unsigned int neededLen = fPrefixBufSz + fLocalPartBufSz + 1;
  305. //
  306. // If no buffer, or the current one is too small, then allocate one
  307. // and get rid of any old one.
  308. //
  309. if (!fRawName || (neededLen > fRawNameBufSz))
  310. {
  311. fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  312. // We have to cast off the const'ness to do this
  313. ((QName*)this)->fRawNameBufSz = neededLen;
  314. ((QName*)this)->fRawName = (XMLCh*) fMemoryManager->allocate
  315. (
  316. (neededLen + 1) * sizeof(XMLCh)
  317. ); //new XMLCh[neededLen + 1];
  318. // Make sure its initially empty
  319. *fRawName = 0;
  320. }
  321. const unsigned int prefixLen = XMLString::stringLen(fPrefix);
  322. XMLString::moveChars(fRawName, fPrefix, prefixLen);
  323. fRawName[prefixLen] = chColon;
  324. XMLString::copyString(&fRawName[prefixLen+1], fLocalPart);
  325. }
  326. else
  327. {
  328. return fLocalPart;
  329. }
  330. }
  331. return fRawName;
  332. }
  333. // ---------------------------------------------------------------------------
  334. // QName: Setter methods
  335. // ---------------------------------------------------------------------------
  336. void QName::setName(const XMLCh* const prefix
  337. , const XMLCh* const localPart
  338. , const unsigned int uriId)
  339. {
  340. setPrefix(prefix);
  341. setLocalPart(localPart);
  342. // And clean up any QName and leave it undone until/if asked for again
  343. if (fRawName)
  344. *fRawName = 0;
  345. // And finally store the URI id parameter
  346. fURIId = uriId;
  347. }
  348. void QName::setName(const XMLCh* const rawName
  349. , const unsigned int uriId)
  350. {
  351. //set the rawName
  352. unsigned int newLen;
  353. newLen = XMLString::stringLen(rawName);
  354. if (!fRawNameBufSz || (newLen > fRawNameBufSz))
  355. {
  356. fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  357. fRawNameBufSz = newLen + 8;
  358. fRawName = (XMLCh*) fMemoryManager->allocate
  359. (
  360. (fRawNameBufSz + 1) * sizeof(XMLCh)
  361. ); //new XMLCh[fRawNameBufSz + 1];
  362. }
  363. XMLString::moveChars(fRawName, rawName, newLen + 1);
  364. //find out the prefix and localPart from the rawName
  365. const int colonInd = XMLString::indexOf(rawName, chColon);
  366. if (colonInd >= 0)
  367. {
  368. setNPrefix(rawName, colonInd);
  369. }
  370. else
  371. {
  372. // No colon, so we just have a name with no prefix
  373. setPrefix(XMLUni::fgZeroLenString);
  374. }
  375. setNLocalPart(&rawName[colonInd+1], newLen-colonInd-1);
  376. // And finally store the URI id parameter
  377. fURIId = uriId;
  378. }
  379. void QName::setPrefix(const XMLCh* prefix)
  380. {
  381. unsigned int newLen;
  382. newLen = XMLString::stringLen(prefix);
  383. if (!fPrefixBufSz || (newLen > fPrefixBufSz))
  384. {
  385. fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  386. fPrefixBufSz = newLen + 8;
  387. fPrefix = (XMLCh*) fMemoryManager->allocate
  388. (
  389. (fPrefixBufSz + 1) * sizeof(XMLCh)
  390. ); //new XMLCh[fPrefixBufSz + 1];
  391. }
  392. XMLString::moveChars(fPrefix, prefix, newLen + 1);
  393. }
  394. void QName::setNPrefix(const XMLCh* prefix, const unsigned int newLen)
  395. {
  396. if (!fPrefixBufSz || (newLen > fPrefixBufSz))
  397. {
  398. fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  399. fPrefixBufSz = newLen + 8;
  400. fPrefix = (XMLCh*) fMemoryManager->allocate
  401. (
  402. (fPrefixBufSz + 1) * sizeof(XMLCh)
  403. ); //new XMLCh[fPrefixBufSz + 1];
  404. }
  405. XMLString::moveChars(fPrefix, prefix, newLen);
  406. fPrefix[newLen] = chNull;
  407. }
  408. void QName::setLocalPart(const XMLCh* localPart)
  409. {
  410. unsigned int newLen;
  411. newLen = XMLString::stringLen(localPart);
  412. if (!fLocalPartBufSz || (newLen > fLocalPartBufSz))
  413. {
  414. fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  415. fLocalPartBufSz = newLen + 8;
  416. fLocalPart = (XMLCh*) fMemoryManager->allocate
  417. (
  418. (fLocalPartBufSz + 1) * sizeof(XMLCh)
  419. ); //new XMLCh[fLocalPartBufSz + 1];
  420. }
  421. XMLString::moveChars(fLocalPart, localPart, newLen + 1);
  422. }
  423. void QName::setNLocalPart(const XMLCh* localPart, const unsigned int newLen)
  424. {
  425. if (!fLocalPartBufSz || (newLen > fLocalPartBufSz))
  426. {
  427. fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  428. fLocalPartBufSz = newLen + 8;
  429. fLocalPart = (XMLCh*) fMemoryManager->allocate
  430. (
  431. (fLocalPartBufSz + 1) * sizeof(XMLCh)
  432. ); //new XMLCh[fLocalPartBufSz + 1];
  433. }
  434. XMLString::moveChars(fLocalPart, localPart, newLen);
  435. fLocalPart[newLen] = chNull;
  436. }
  437. void QName::setValues(const QName& qname)
  438. {
  439. setPrefix(qname.getPrefix());
  440. setLocalPart(qname.getLocalPart());
  441. setURI(qname.getURI());
  442. }
  443. // -----------------------------------------------------------------------
  444. // comparison
  445. // -----------------------------------------------------------------------
  446. bool QName::operator==(const QName& qname) const
  447. {
  448. if (fURIId == 0) // null URI
  449. return (XMLString::equals(getRawName(),qname.getRawName()));
  450. return ((fURIId == qname.getURI()) &&
  451. (XMLString::equals(fLocalPart, qname.getLocalPart())));
  452. }
  453. // ---------------------------------------------------------------------------
  454. // QName: Private, helper methods
  455. // ---------------------------------------------------------------------------
  456. void QName::cleanUp()
  457. {
  458. fMemoryManager->deallocate(fLocalPart); //delete [] fLocalPart;
  459. fMemoryManager->deallocate(fPrefix); //delete [] fPrefix;
  460. fMemoryManager->deallocate(fRawName); //delete [] fRawName;
  461. fLocalPart = fPrefix = fRawName = 0;
  462. }
  463. /***
  464. * Support for Serialization/De-serialization
  465. ***/
  466. IMPL_XSERIALIZABLE_TOCREATE(QName)
  467. void QName::serialize(XSerializeEngine& serEng)
  468. {
  469. if (serEng.isStoring())
  470. {
  471. serEng.writeString(fPrefix, fPrefixBufSz, XSerializeEngine::toWriteBufferLen);
  472. serEng.writeString(fLocalPart, fLocalPartBufSz, XSerializeEngine::toWriteBufferLen);
  473. //do not serialize rawName
  474. serEng<<fURIId;
  475. }
  476. else
  477. {
  478. int dataLen = 0;
  479. serEng.readString(fPrefix, (int&)fPrefixBufSz, dataLen, XSerializeEngine::toReadBufferLen);
  480. serEng.readString(fLocalPart, (int&)fLocalPartBufSz, dataLen, XSerializeEngine::toReadBufferLen);
  481. //force raw name rebuilt
  482. fRawNameBufSz = 0;
  483. fRawName = 0;
  484. serEng>>fURIId;
  485. }
  486. }
  487. XERCES_CPP_NAMESPACE_END