PageRenderTime 47ms 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/validators/common/MixedContentModel.cpp

http://atf2flightsim.googlecode.com/
C++ | 513 lines | 244 code | 48 blank | 221 comment | 124 complexity | 6b8463e16ecb5d939a7ca2fa11d6970d 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) 1999-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) 1999, 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: MixedContentModel.cpp,v $
  58. * Revision 1.1.1.1 2009/03/14 06:43:09 whitegr
  59. * epics channel archiver
  60. *
  61. * Revision 1.8 2003/11/20 18:12:20 knoaman
  62. * Use a bitwise operation to check the node type.
  63. *
  64. * Revision 1.7 2003/11/07 17:08:11 knoaman
  65. * For PSVI support, distinguish wildcard elements with namespace lists.
  66. *
  67. * Revision 1.6 2003/05/18 14:02:06 knoaman
  68. * Memory manager implementation: pass per instance manager.
  69. *
  70. * Revision 1.5 2003/05/16 21:43:20 knoaman
  71. * Memory manager implementation: Modify constructors to pass in the memory manager.
  72. *
  73. * Revision 1.4 2003/05/15 18:48:27 knoaman
  74. * Partial implementation of the configurable memory manager.
  75. *
  76. * Revision 1.3 2002/11/04 14:54:58 tng
  77. * C++ Namespace Support.
  78. *
  79. * Revision 1.2 2002/09/24 19:48:39 tng
  80. * Performance: use XMLString::equals instead of XMLString::compareString
  81. *
  82. * Revision 1.1.1.1 2002/02/01 22:22:39 peiyongz
  83. * sane_include
  84. *
  85. * Revision 1.13 2001/08/24 12:48:48 tng
  86. * Schema: AllContentModel
  87. *
  88. * Revision 1.12 2001/08/21 16:06:11 tng
  89. * Schema: Unique Particle Attribution Constraint Checking.
  90. *
  91. * Revision 1.11 2001/07/09 15:22:37 knoaman
  92. * complete <any> declaration.
  93. *
  94. * Revision 1.10 2001/06/12 22:13:33 peiyongz
  95. * validateContentSpecial() : apply SubstitutionGroupComparator.isEquivalentTo()
  96. *
  97. * Revision 1.9 2001/05/11 13:27:19 tng
  98. * Copyright update.
  99. *
  100. * Revision 1.8 2001/05/03 21:02:30 tng
  101. * Schema: Add SubstitutionGroupComparator and update exception messages. By Pei Yong Zhang.
  102. *
  103. * Revision 1.7 2001/04/19 18:17:32 tng
  104. * Schema: SchemaValidator update, and use QName in Content Model
  105. *
  106. * Revision 1.6 2001/03/21 21:56:28 tng
  107. * Schema: Add Schema Grammar, Schema Validator, and split the DTDValidator into DTDValidator, DTDScanner, and DTDGrammar.
  108. *
  109. * Revision 1.5 2001/03/21 19:29:57 tng
  110. * Schema: Content Model Updates, by Pei Yong Zhang.
  111. *
  112. * Revision 1.4 2001/02/27 18:32:32 tng
  113. * Schema: Use XMLElementDecl instead of DTDElementDecl in Content Model.
  114. *
  115. * Revision 1.3 2001/02/27 14:48:54 tng
  116. * Schema: Add CMAny and ContentLeafNameTypeVector, by Pei Yong Zhang
  117. *
  118. * Revision 1.2 2001/02/16 14:58:57 tng
  119. * Schema: Update Makefile, configure files, project files, and include path in
  120. * certain cpp files because of the move of the common Content Model files. By Pei Yong Zhang.
  121. *
  122. * Revision 1.1 2001/02/16 14:17:29 tng
  123. * Schema: Move the common Content Model files that are shared by DTD
  124. * and schema from 'DTD' folder to 'common' folder. By Pei Yong Zhang.
  125. *
  126. * Revision 1.5 2000/05/15 22:31:32 andyh
  127. * Replace #include<memory.h> with <string.h> everywhere.
  128. *
  129. * Revision 1.4 2000/03/18 00:00:05 roddey
  130. * Initial updates for two way transcoding support
  131. *
  132. * Revision 1.3 2000/03/02 19:55:39 roddey
  133. * This checkin includes many changes done while waiting for the
  134. * 1.1.0 code to be finished. I can't list them all here, but a list is
  135. * available elsewhere.
  136. *
  137. * Revision 1.2 2000/02/09 21:42:39 abagchi
  138. * Copyright swatswat
  139. *
  140. * Revision 1.1.1.1 1999/11/09 01:03:43 twl
  141. * Initial checkin
  142. *
  143. * Revision 1.3 1999/11/08 20:45:43 rahul
  144. * Swat for adding in Product name and CVS comment log variable.
  145. *
  146. */
  147. // ---------------------------------------------------------------------------
  148. // Includes
  149. // ---------------------------------------------------------------------------
  150. #include <string.h>
  151. #include <xercesc/util/RuntimeException.hpp>
  152. #include <xercesc/framework/XMLElementDecl.hpp>
  153. #include <xercesc/validators/common/ContentSpecNode.hpp>
  154. #include <xercesc/validators/common/MixedContentModel.hpp>
  155. #include <xercesc/validators/common/CMStateSet.hpp>
  156. #include <xercesc/validators/common/Grammar.hpp>
  157. #include <xercesc/validators/schema/SubstitutionGroupComparator.hpp>
  158. XERCES_CPP_NAMESPACE_BEGIN
  159. // ---------------------------------------------------------------------------
  160. // MixedContentModel: Constructors and Destructor
  161. // ---------------------------------------------------------------------------
  162. MixedContentModel::MixedContentModel(const bool dtd
  163. , ContentSpecNode* const parentContentSpec
  164. , const bool ordered
  165. , MemoryManager* const manager) :
  166. fCount(0)
  167. , fChildren(0)
  168. , fChildTypes(0)
  169. , fOrdered(ordered)
  170. , fDTD(dtd)
  171. , fMemoryManager(manager)
  172. {
  173. //
  174. // Create a vector of unsigned ints that will be filled in with the
  175. // ids of the child nodes. It will be expanded as needed but we give
  176. // it an initial capacity of 64 which should be more than enough for
  177. // 99% of the scenarios.
  178. //
  179. ValueVectorOf<QName*> children(64, fMemoryManager);
  180. ValueVectorOf<ContentSpecNode::NodeTypes> childTypes(64, fMemoryManager);
  181. //
  182. // Get the parent element's content spec. This is the head of the tree
  183. // of nodes that describes the content model. We will iterate this
  184. // tree.
  185. //
  186. ContentSpecNode* curNode = parentContentSpec;
  187. if (!curNode)
  188. ThrowXML(RuntimeException, XMLExcepts::CM_NoParentCSN);
  189. // And now call the private recursive method that iterates the tree
  190. buildChildList(curNode, children, childTypes);
  191. //
  192. // And now we know how many elements we need in our member list. So
  193. // fill them in.
  194. //
  195. fCount = children.size();
  196. fChildren = (QName**) fMemoryManager->allocate(fCount * sizeof(QName*)); //new QName*[fCount];
  197. fChildTypes = (ContentSpecNode::NodeTypes*) fMemoryManager->allocate
  198. (
  199. fCount * sizeof(ContentSpecNode::NodeTypes)
  200. ); //new ContentSpecNode::NodeTypes[fCount];
  201. for (unsigned int index = 0; index < fCount; index++) {
  202. fChildren[index] = children.elementAt(index);
  203. fChildTypes[index] = childTypes.elementAt(index);
  204. }
  205. }
  206. MixedContentModel::~MixedContentModel()
  207. {
  208. fMemoryManager->deallocate(fChildren); //delete [] fChildren;
  209. fMemoryManager->deallocate(fChildTypes); //delete [] fChildTypes;
  210. }
  211. // ---------------------------------------------------------------------------
  212. // MixedContentModel: Getter methods
  213. // ---------------------------------------------------------------------------
  214. bool MixedContentModel::hasDups() const
  215. {
  216. // Can't have dups if only one child
  217. if (fCount == 1)
  218. return false;
  219. for (unsigned int index = 0; index < fCount; index++)
  220. {
  221. const QName* curVal = fChildren[index];
  222. for (unsigned int iIndex = 0; iIndex < fCount; iIndex++)
  223. {
  224. if (iIndex == index)
  225. continue;
  226. if (fDTD) {
  227. if (XMLString::equals(curVal->getRawName(), fChildren[iIndex]->getRawName())) {
  228. return true;
  229. }
  230. }
  231. else {
  232. if ((curVal->getURI() == fChildren[iIndex]->getURI()) &&
  233. (XMLString::equals(curVal->getLocalPart(), fChildren[iIndex]->getLocalPart()))) {
  234. return true;
  235. }
  236. }
  237. }
  238. }
  239. return false;
  240. }
  241. // ---------------------------------------------------------------------------
  242. // MixedContentModel: Implementation of the ContentModel virtual interface
  243. // ---------------------------------------------------------------------------
  244. //
  245. //Under the XML Schema mixed model,
  246. //the order and number of child elements appearing in an instance
  247. //must agree with
  248. //the order and number of child elements specified in the model.
  249. //
  250. int
  251. MixedContentModel::validateContent( QName** const children
  252. , const unsigned int childCount
  253. , const unsigned int emptyNamespaceId) const
  254. {
  255. // must match order
  256. if (fOrdered) {
  257. unsigned int inIndex = 0;
  258. for (unsigned int outIndex = 0; outIndex < childCount; outIndex++) {
  259. // Get the current child out of the source index
  260. const QName* curChild = children[outIndex];
  261. // If its PCDATA, then we just accept that
  262. if (curChild->getURI() == XMLElementDecl::fgPCDataElemId)
  263. continue;
  264. ContentSpecNode::NodeTypes type = fChildTypes[inIndex];
  265. const QName* inChild = fChildren[inIndex];
  266. if (type == ContentSpecNode::Leaf) {
  267. if (fDTD) {
  268. if (!XMLString::equals(inChild->getRawName(), curChild->getRawName())) {
  269. return outIndex;
  270. }
  271. }
  272. else {
  273. if ((inChild->getURI() != curChild->getURI()) ||
  274. (!XMLString::equals(inChild->getLocalPart(), curChild->getLocalPart()))) {
  275. return outIndex;
  276. }
  277. }
  278. }
  279. else if (type == ContentSpecNode::Any) {
  280. }
  281. else if (type == ContentSpecNode::Any_NS) {
  282. if (inChild->getURI() != curChild->getURI())
  283. return outIndex;
  284. }
  285. else if (type == ContentSpecNode::Any_Other) {
  286. if (inChild->getURI() == curChild->getURI())
  287. return outIndex;
  288. }
  289. // advance index
  290. inIndex++;
  291. }
  292. }
  293. // can appear in any order
  294. else {
  295. for (unsigned int outIndex = 0; outIndex < childCount; outIndex++) {
  296. // Get the current child out of the source index
  297. const QName* curChild = children[outIndex];
  298. // If its PCDATA, then we just accept that
  299. if (curChild->getURI() == XMLElementDecl::fgPCDataElemId)
  300. continue;
  301. // And try to find it in our list
  302. unsigned int inIndex = 0;
  303. for (; inIndex < fCount; inIndex++)
  304. {
  305. ContentSpecNode::NodeTypes type = fChildTypes[inIndex];
  306. const QName* inChild = fChildren[inIndex];
  307. if (type == ContentSpecNode::Leaf) {
  308. if (fDTD) {
  309. if (XMLString::equals(inChild->getRawName(), curChild->getRawName())) {
  310. break;
  311. }
  312. }
  313. else {
  314. if ((inChild->getURI() == curChild->getURI()) &&
  315. (XMLString::equals(inChild->getLocalPart(), curChild->getLocalPart()))) {
  316. break;
  317. }
  318. }
  319. }
  320. else if (type == ContentSpecNode::Any) {
  321. break;
  322. }
  323. else if (type == ContentSpecNode::Any_NS) {
  324. if (inChild->getURI() == curChild->getURI())
  325. break;
  326. }
  327. else if (type == ContentSpecNode::Any_Other) {
  328. if (inChild->getURI() != curChild->getURI())
  329. break;
  330. }
  331. // REVISIT: What about checking for multiple ANY matches?
  332. // The content model ambiguity *could* be checked
  333. // by the caller before constructing the mixed
  334. // content model.
  335. }
  336. // We did not find this one, so the validation failed
  337. if (inIndex == fCount)
  338. return outIndex;
  339. }
  340. }
  341. // Everything seems to be in order, so return success
  342. // success
  343. return -1;
  344. }
  345. int MixedContentModel::validateContentSpecial(QName** const children
  346. , const unsigned int childCount
  347. , const unsigned int emptyNamespaceId
  348. , GrammarResolver* const pGrammarResolver
  349. , XMLStringPool* const pStringPool) const
  350. {
  351. SubstitutionGroupComparator comparator(pGrammarResolver, pStringPool);
  352. // must match order
  353. if (fOrdered) {
  354. unsigned int inIndex = 0;
  355. for (unsigned int outIndex = 0; outIndex < childCount; outIndex++) {
  356. // Get the current child out of the source index
  357. QName* curChild = children[outIndex];
  358. // If its PCDATA, then we just accept that
  359. if (curChild->getURI() == XMLElementDecl::fgPCDataElemId)
  360. continue;
  361. ContentSpecNode::NodeTypes type = fChildTypes[inIndex];
  362. QName* inChild = fChildren[inIndex];
  363. if (type == ContentSpecNode::Leaf) {
  364. if ( !comparator.isEquivalentTo(curChild, inChild))
  365. return outIndex;
  366. }
  367. else if (type == ContentSpecNode::Any) {
  368. }
  369. else if (type == ContentSpecNode::Any_NS) {
  370. if (inChild->getURI() != curChild->getURI())
  371. return outIndex;
  372. }
  373. else if (type == ContentSpecNode::Any_Other) {
  374. if (inChild->getURI() == curChild->getURI())
  375. return outIndex;
  376. }
  377. // advance index
  378. inIndex++;
  379. }
  380. }
  381. // can appear in any order
  382. else {
  383. for (unsigned int outIndex = 0; outIndex < childCount; outIndex++) {
  384. // Get the current child out of the source index
  385. QName* curChild = children[outIndex];
  386. // If its PCDATA, then we just accept that
  387. if (curChild->getURI() == XMLElementDecl::fgPCDataElemId)
  388. continue;
  389. // And try to find it in our list
  390. unsigned int inIndex = 0;
  391. for (; inIndex < fCount; inIndex++)
  392. {
  393. ContentSpecNode::NodeTypes type = fChildTypes[inIndex];
  394. QName* inChild = fChildren[inIndex];
  395. if (type == ContentSpecNode::Leaf) {
  396. if ( comparator.isEquivalentTo(curChild, inChild))
  397. break;
  398. }
  399. else if (type == ContentSpecNode::Any) {
  400. break;
  401. }
  402. else if (type == ContentSpecNode::Any_NS) {
  403. if (inChild->getURI() == curChild->getURI())
  404. break;
  405. }
  406. else if (type == ContentSpecNode::Any_Other) {
  407. if (inChild->getURI() != curChild->getURI())
  408. break;
  409. }
  410. // REVISIT: What about checking for multiple ANY matches?
  411. // The content model ambiguity *could* be checked
  412. // by the caller before constructing the mixed
  413. // content model.
  414. }
  415. // We did not find this one, so the validation failed
  416. if (inIndex == fCount)
  417. return outIndex;
  418. }
  419. }
  420. // Everything seems to be in order, so return success
  421. // success
  422. return -1;
  423. }
  424. // ---------------------------------------------------------------------------
  425. // MixedContentModel: Private helper methods
  426. // ---------------------------------------------------------------------------
  427. void
  428. MixedContentModel::buildChildList( ContentSpecNode* const curNode
  429. , ValueVectorOf<QName*>& toFill
  430. , ValueVectorOf<ContentSpecNode::NodeTypes>& toType)
  431. {
  432. // Get the type of spec node our current node is
  433. const ContentSpecNode::NodeTypes curType = curNode->getType();
  434. // If its a leaf, then store its id in the target list
  435. if ((curType == ContentSpecNode::Leaf) ||
  436. (curType == ContentSpecNode::Any) ||
  437. (curType == ContentSpecNode::Any_Other) ||
  438. (curType == ContentSpecNode::Any_NS) )
  439. {
  440. toFill.addElement(curNode->getElement());
  441. toType.addElement(curType);
  442. return;
  443. }
  444. // Get both the child node pointers
  445. ContentSpecNode* leftNode = curNode->getFirst();
  446. ContentSpecNode* rightNode = curNode->getSecond();
  447. // And recurse according to the type of node
  448. if (((curType & 0x0f) == ContentSpecNode::Choice)
  449. || ((curType & 0x0f) == ContentSpecNode::Sequence))
  450. {
  451. // Recurse on the left and right nodes
  452. buildChildList(leftNode, toFill, toType);
  453. // The last node of a choice or sequence has a null right
  454. if (rightNode)
  455. buildChildList(rightNode, toFill, toType);
  456. }
  457. else if ((curType == ContentSpecNode::OneOrMore)
  458. || (curType == ContentSpecNode::ZeroOrOne)
  459. || (curType == ContentSpecNode::ZeroOrMore))
  460. {
  461. // Just do the left node on this one
  462. buildChildList(leftNode, toFill, toType);
  463. }
  464. }
  465. XERCES_CPP_NAMESPACE_END