PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/xerces-c-src2_4_0/src/xercesc/validators/DTD/DTDElementDecl.cpp

http://atf2flightsim.googlecode.com/
C++ | 535 lines | 297 code | 70 blank | 168 comment | 51 complexity | ec5d3ca4a6c7b3c1095efb16c6f1a035 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. * $Id: DTDElementDecl.cpp,v 1.1.1.1 2009/03/14 06:43:04 whitegr Exp $
  58. */
  59. // ---------------------------------------------------------------------------
  60. // Includes
  61. // ---------------------------------------------------------------------------
  62. #include <xercesc/util/XMLString.hpp>
  63. #include <xercesc/util/XMLUniDefs.hpp>
  64. #include <xercesc/util/XMLUni.hpp>
  65. #include <xercesc/framework/XMLBuffer.hpp>
  66. #include <xercesc/validators/common/DFAContentModel.hpp>
  67. #include <xercesc/validators/common/ContentSpecNode.hpp>
  68. #include <xercesc/validators/common/MixedContentModel.hpp>
  69. #include <xercesc/validators/common/SimpleContentModel.hpp>
  70. #include <xercesc/validators/DTD/DTDAttDefList.hpp>
  71. #include <xercesc/validators/DTD/DTDElementDecl.hpp>
  72. #include <xercesc/internal/XTemplateSerializer.hpp>
  73. XERCES_CPP_NAMESPACE_BEGIN
  74. // ---------------------------------------------------------------------------
  75. // DTDElementDecl: Constructors and Destructor
  76. // ---------------------------------------------------------------------------
  77. DTDElementDecl::DTDElementDecl(MemoryManager* const manager) :
  78. XMLElementDecl(manager)
  79. , fAttDefs(0)
  80. , fAttList(0)
  81. , fContentSpec(0)
  82. , fModelType(Any)
  83. , fContentModel(0)
  84. , fFormattedModel(0)
  85. {
  86. }
  87. DTDElementDecl::DTDElementDecl( const XMLCh* const elemRawName
  88. , const unsigned int uriId
  89. , const DTDElementDecl::ModelTypes type
  90. , MemoryManager* const manager) :
  91. XMLElementDecl(manager)
  92. , fAttDefs(0)
  93. , fAttList(0)
  94. , fContentSpec(0)
  95. , fModelType(type)
  96. , fContentModel(0)
  97. , fFormattedModel(0)
  98. {
  99. setElementName(elemRawName, uriId);
  100. }
  101. DTDElementDecl::DTDElementDecl( QName* const elementName
  102. , const DTDElementDecl::ModelTypes type
  103. , MemoryManager* const manager) :
  104. XMLElementDecl(manager)
  105. , fAttDefs(0)
  106. , fAttList(0)
  107. , fContentSpec(0)
  108. , fModelType(type)
  109. , fContentModel(0)
  110. , fFormattedModel(0)
  111. {
  112. setElementName(elementName);
  113. }
  114. DTDElementDecl::~DTDElementDecl()
  115. {
  116. delete fAttDefs;
  117. delete fAttList;
  118. delete fContentSpec;
  119. delete fContentModel;
  120. getMemoryManager()->deallocate(fFormattedModel);//delete [] fFormattedModel;
  121. }
  122. // ---------------------------------------------------------------------------
  123. // The virtual element decl interface
  124. // ---------------------------------------------------------------------------
  125. XMLAttDef* DTDElementDecl::findAttr(const XMLCh* const qName
  126. , const unsigned int uriId
  127. , const XMLCh* const baseName
  128. , const XMLCh* const prefix
  129. , const LookupOpts options
  130. , bool& wasAdded) const
  131. {
  132. DTDAttDef* retVal = 0;
  133. // If no att list faulted in yet, then it cannot exist
  134. if (fAttDefs)
  135. retVal = fAttDefs->get(qName);
  136. // Fault it in if not found and ask to add it
  137. if (!retVal && (options == XMLElementDecl::AddIfNotFound))
  138. {
  139. // Fault in the list itself if not already
  140. if (!fAttDefs)
  141. faultInAttDefList();
  142. // And add a default attribute for this name
  143. retVal = new (getMemoryManager()) DTDAttDef
  144. (
  145. qName
  146. , XMLAttDef::CData
  147. , XMLAttDef::Implied
  148. , getMemoryManager()
  149. );
  150. retVal->setElemId(getId());
  151. fAttDefs->put((void*)retVal->getFullName(), retVal);
  152. // update and/or create fAttList
  153. if(!fAttList)
  154. ((DTDElementDecl*)this)->fAttList = new (getMemoryManager()) DTDAttDefList(fAttDefs,getMemoryManager());
  155. fAttList->addAttDef(retVal);
  156. wasAdded = true;
  157. }
  158. else
  159. {
  160. wasAdded = false;
  161. }
  162. return retVal;
  163. }
  164. XMLAttDefList& DTDElementDecl::getAttDefList() const
  165. {
  166. if (!fAttList)
  167. {
  168. // If the att def list is not made yet, then fault it in too
  169. if (!fAttDefs)
  170. faultInAttDefList();
  171. ((DTDElementDecl*)this)->fAttList = new (getMemoryManager()) DTDAttDefList(fAttDefs,getMemoryManager());
  172. }
  173. // Reset it before we return it
  174. fAttList->Reset();
  175. return *fAttList;
  176. }
  177. XMLElementDecl::CharDataOpts DTDElementDecl::getCharDataOpts() const
  178. {
  179. XMLElementDecl::CharDataOpts retVal;
  180. switch(fModelType)
  181. {
  182. case Children :
  183. retVal = XMLElementDecl::SpacesOk;
  184. break;
  185. case Empty :
  186. retVal = XMLElementDecl::NoCharData;
  187. break;
  188. default :
  189. retVal = XMLElementDecl::AllCharData;
  190. break;
  191. }
  192. return retVal;
  193. }
  194. bool DTDElementDecl::hasAttDefs() const
  195. {
  196. // If the collection hasn't been faulted in, then no att defs
  197. if (!fAttDefs)
  198. return false;
  199. return !fAttDefs->isEmpty();
  200. }
  201. bool DTDElementDecl::resetDefs()
  202. {
  203. // If the collection hasn't been faulted in, then no att defs
  204. if (!fAttDefs)
  205. return false;
  206. //
  207. // Ok, run through them and clear the 'provided' flag on each of them.
  208. // This lets the scanner use them to track which has been provided and
  209. // which have not.
  210. //
  211. RefHashTableOfEnumerator<DTDAttDef> enumDefs(fAttDefs);
  212. while (enumDefs.hasMoreElements())
  213. enumDefs.nextElement().setProvided(false);
  214. return true;
  215. }
  216. void
  217. DTDElementDecl::setContentSpec(ContentSpecNode* toAdopt)
  218. {
  219. delete fContentSpec;
  220. fContentSpec = toAdopt;
  221. //reset Content Model
  222. setContentModel(0);
  223. }
  224. const XMLCh*
  225. DTDElementDecl::getFormattedContentModel() const
  226. {
  227. //
  228. // If its not already built, then call the protected virtual method
  229. // to allow the derived class to build it (since only it knows.)
  230. // Otherwise, just return the previously formatted methods.
  231. //
  232. // Since we are faulting this in, within a const getter, we have to
  233. // cast off the const-ness.
  234. //
  235. if (!fFormattedModel)
  236. ((DTDElementDecl*)this)->fFormattedModel = formatContentModel();
  237. return fFormattedModel;
  238. }
  239. // ---------------------------------------------------------------------------
  240. // DTDElementDecl: Getter methods
  241. // ---------------------------------------------------------------------------
  242. const DTDAttDef* DTDElementDecl::getAttDef(const XMLCh* const attName) const
  243. {
  244. // If no list, then return a null
  245. if (!fAttDefs)
  246. return 0;
  247. return fAttDefs->get(attName);
  248. }
  249. DTDAttDef* DTDElementDecl::getAttDef(const XMLCh* const attName)
  250. {
  251. // If no list, then return a null
  252. if (!fAttDefs)
  253. return 0;
  254. return fAttDefs->get(attName);
  255. }
  256. // ---------------------------------------------------------------------------
  257. // DTDElementDecl: Implementation of the protected virtual interface
  258. // ---------------------------------------------------------------------------
  259. void DTDElementDecl::addAttDef(DTDAttDef* const toAdd)
  260. {
  261. // Fault in the att list if required
  262. if (!fAttDefs)
  263. faultInAttDefList();
  264. // Tell this guy the element id of its parent (us)
  265. toAdd->setElemId(getId());
  266. fAttDefs->put((void*)(toAdd->getFullName()), toAdd);
  267. // update and/or create fAttList
  268. if(!fAttList)
  269. ((DTDElementDecl*)this)->fAttList = new (getMemoryManager()) DTDAttDefList(fAttDefs,getMemoryManager());
  270. fAttList->addAttDef(toAdd);
  271. }
  272. // ---------------------------------------------------------------------------
  273. // DTDElementDecl: Private helper methods
  274. // ---------------------------------------------------------------------------
  275. XMLCh* DTDElementDecl::formatContentModel() const
  276. {
  277. XMLCh* newValue = 0;
  278. if (fModelType == Any)
  279. {
  280. newValue = XMLString::replicate(XMLUni::fgAnyString, getMemoryManager());
  281. }
  282. else if (fModelType == Empty)
  283. {
  284. newValue = XMLString::replicate(XMLUni::fgEmptyString, getMemoryManager());
  285. }
  286. else
  287. {
  288. //
  289. // Use a temp XML buffer to format into. Content models could be
  290. // pretty long, but very few will be longer than one K. The buffer
  291. // will expand to handle the more pathological ones.
  292. //
  293. XMLBuffer bufFmt(1023, getMemoryManager());
  294. getContentSpec()->formatSpec(bufFmt);
  295. newValue = XMLString::replicate(bufFmt.getRawBuffer(), getMemoryManager());
  296. }
  297. return newValue;
  298. }
  299. XMLContentModel* DTDElementDecl::makeContentModel()
  300. {
  301. XMLContentModel* cmRet = 0;
  302. if (fModelType == Mixed_Simple)
  303. {
  304. //
  305. // Just create a mixel content model object. This type of
  306. // content model is optimized for mixed content validation.
  307. //
  308. cmRet = new (getMemoryManager()) MixedContentModel(true, this->getContentSpec(), false, getMemoryManager());
  309. }
  310. else if (fModelType == Children)
  311. {
  312. //
  313. // This method will create an optimal model for the complexity
  314. // of the element's defined model. If its simple, it will create
  315. // a SimpleContentModel object. If its a simple list, it will
  316. // create a SimpleListContentModel object. If its complex, it
  317. // will create a DFAContentModel object.
  318. //
  319. cmRet = createChildModel();
  320. }
  321. else
  322. {
  323. ThrowXML(RuntimeException, XMLExcepts::CM_MustBeMixedOrChildren);
  324. }
  325. return cmRet;
  326. }
  327. XMLContentModel* DTDElementDecl::createChildModel()
  328. {
  329. // Get the content spec node of the element
  330. ContentSpecNode* specNode = getContentSpec();
  331. if(!specNode)
  332. ThrowXML(RuntimeException, XMLExcepts::CM_UnknownCMSpecType);
  333. //
  334. // Do a sanity check that the node does not have a PCDATA id. Since,
  335. // if it was, it should have already gotten taken by the Mixed model.
  336. //
  337. if (specNode->getElement()) {
  338. if (specNode->getElement()->getURI() == XMLElementDecl::fgPCDataElemId)
  339. ThrowXML(RuntimeException, XMLExcepts::CM_NoPCDATAHere);
  340. }
  341. //
  342. // According to the type of node, we will create the correct type of
  343. // content model.
  344. //
  345. if (specNode->getType() == ContentSpecNode::Leaf)
  346. {
  347. // Create a simple content model
  348. return new (getMemoryManager()) SimpleContentModel
  349. (
  350. true
  351. , specNode->getElement()
  352. , 0
  353. , ContentSpecNode::Leaf
  354. , getMemoryManager()
  355. );
  356. }
  357. else if ((specNode->getType() == ContentSpecNode::Choice)
  358. || (specNode->getType() == ContentSpecNode::Sequence))
  359. {
  360. //
  361. // Lets see if both of the children are leafs. If so, then it has to
  362. // be a simple content model
  363. //
  364. if ((specNode->getFirst()->getType() == ContentSpecNode::Leaf)
  365. && (specNode->getSecond()->getType() == ContentSpecNode::Leaf))
  366. {
  367. return new (getMemoryManager()) SimpleContentModel
  368. (
  369. true
  370. , specNode->getFirst()->getElement()
  371. , specNode->getSecond()->getElement()
  372. , specNode->getType()
  373. , getMemoryManager()
  374. );
  375. }
  376. }
  377. else if ((specNode->getType() == ContentSpecNode::OneOrMore)
  378. || (specNode->getType() == ContentSpecNode::ZeroOrMore)
  379. || (specNode->getType() == ContentSpecNode::ZeroOrOne))
  380. {
  381. //
  382. // Its a repetition, so see if its one child is a leaf. If so its a
  383. // repetition of a single element, so we can do a simple content
  384. // model for that.
  385. //
  386. if (specNode->getFirst()->getType() == ContentSpecNode::Leaf)
  387. {
  388. return new (getMemoryManager()) SimpleContentModel
  389. (
  390. true
  391. , specNode->getFirst()->getElement()
  392. , 0
  393. , specNode->getType()
  394. , getMemoryManager()
  395. );
  396. }
  397. }
  398. else
  399. {
  400. ThrowXML(RuntimeException, XMLExcepts::CM_UnknownCMSpecType);
  401. }
  402. // Its not any simple type of content, so create a DFA based content model
  403. return new (getMemoryManager()) DFAContentModel
  404. (
  405. true
  406. , this->getContentSpec()
  407. , getMemoryManager()
  408. );
  409. }
  410. void DTDElementDecl::faultInAttDefList() const
  411. {
  412. // Use a hash modulus of 29 and tell it owns its elements
  413. ((DTDElementDecl*)this)->fAttDefs = new (getMemoryManager()) RefHashTableOf<DTDAttDef>(29, true, getMemoryManager());
  414. }
  415. /***
  416. * Support for Serialization/De-serialization
  417. ***/
  418. IMPL_XSERIALIZABLE_TOCREATE(DTDElementDecl)
  419. void DTDElementDecl::serialize(XSerializeEngine& serEng)
  420. {
  421. XMLElementDecl::serialize(serEng);
  422. if (serEng.isStoring())
  423. {
  424. /***
  425. *
  426. * Serialize RefHashTableOf<DTDAttDef>
  427. *
  428. ***/
  429. XTemplateSerializer::storeObject(fAttDefs, serEng);
  430. serEng<<fAttList;
  431. serEng<<fContentSpec;
  432. serEng<<(int) fModelType;
  433. /***
  434. * don't serialize
  435. *
  436. * XMLContentModel* fContentModel;
  437. * XMLCh* fFormattedModel;
  438. *
  439. ***/
  440. }
  441. else
  442. {
  443. /***
  444. *
  445. * Deserialize RefHashTableOf<DTDAttDef>
  446. *
  447. ***/
  448. XTemplateSerializer::loadObject(&fAttDefs, 29, true, serEng);
  449. serEng>>fAttList;
  450. serEng>>fContentSpec;
  451. int i;
  452. serEng>>i;
  453. fModelType=(ModelTypes)i;
  454. /***
  455. * don't deserialize
  456. *
  457. * XMLContentModel* fContentModel;
  458. * XMLCh* fFormattedModel;
  459. *
  460. ***/
  461. fContentModel = 0;
  462. fFormattedModel = 0;
  463. }
  464. }
  465. XERCES_CPP_NAMESPACE_END