PageRenderTime 41ms CodeModel.GetById 12ms 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/schema/XSDDOMParser.cpp

http://atf2flightsim.googlecode.com/
C++ | 564 lines | 381 code | 71 blank | 112 comment | 73 complexity | 5d45e0f7d1767882508ba1e1e4e135c4 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) 2002 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. * $Id: XSDDOMParser.cpp,v 1.1.1.1 2009/03/14 06:43:11 whitegr Exp $
  58. */
  59. // ---------------------------------------------------------------------------
  60. // Includes
  61. // ---------------------------------------------------------------------------
  62. #include <xercesc/validators/schema/XSDDOMParser.hpp>
  63. #include <xercesc/validators/schema/SchemaSymbols.hpp>
  64. #include <xercesc/internal/XMLScanner.hpp>
  65. #include <xercesc/internal/ElemStack.hpp>
  66. #include <xercesc/dom/DOMDocument.hpp>
  67. #include <xercesc/dom/impl/DOMElementImpl.hpp>
  68. #include <xercesc/dom/impl/DOMAttrImpl.hpp>
  69. #include <xercesc/dom/impl/DOMTextImpl.hpp>
  70. #include <xercesc/framework/XMLValidityCodes.hpp>
  71. XERCES_CPP_NAMESPACE_BEGIN
  72. // ---------------------------------------------------------------------------
  73. // XSDDOMParser: Constructors and Destructor
  74. // ---------------------------------------------------------------------------
  75. XSDDOMParser::XSDDOMParser( XMLValidator* const valToAdopt
  76. , MemoryManager* const manager
  77. , XMLGrammarPool* const gramPool):
  78. XercesDOMParser(valToAdopt, manager, gramPool)
  79. , fSawFatal(false)
  80. , fAnnotationDepth(-1)
  81. , fInnerAnnotationDepth(-1)
  82. , fDepth(-1)
  83. , fUserErrorReporter(0)
  84. , fUserEntityHandler(0)
  85. , fAnnotationBuf(1023, manager)
  86. , fURIs(0)
  87. {
  88. fURIs = new (manager) ValueVectorOf<unsigned int>(16, manager);
  89. fXSDErrorReporter.setErrorReporter(this);
  90. setValidationScheme(XercesDOMParser::Val_Never);
  91. setDoNamespaces(true);
  92. }
  93. XSDDOMParser::~XSDDOMParser()
  94. {
  95. delete fURIs;
  96. }
  97. // ---------------------------------------------------------------------------
  98. // XSDDOMParser: Helper methods
  99. // ---------------------------------------------------------------------------
  100. DOMElement* XSDDOMParser::createElementNSNode(const XMLCh *namespaceURI,
  101. const XMLCh *qualifiedName)
  102. {
  103. ReaderMgr::LastExtEntityInfo lastInfo;
  104. ((ReaderMgr*) fScanner->getLocator())->getLastExtEntityInfo(lastInfo);
  105. return getDocument()->createElementNS(namespaceURI, qualifiedName,
  106. lastInfo.lineNumber, lastInfo.colNumber);
  107. }
  108. void XSDDOMParser::startAnnotation( const XMLElementDecl& elemDecl
  109. , const RefVectorOf<XMLAttr>& attrList
  110. , const unsigned int attrCount)
  111. {
  112. fAnnotationBuf.append(chOpenAngle);
  113. fAnnotationBuf.append(elemDecl.getFullName());
  114. fAnnotationBuf.append(chSpace);
  115. // attributes are a bit of a pain. To get this right, we have to keep track
  116. // of the namespaces we've seen declared, then examine the namespace context
  117. // for other namespaces so that we can also include them.
  118. // optimized for simplicity and the case that not many
  119. // namespaces are declared on this annotation...
  120. fURIs->removeAllElements();
  121. for (unsigned int i=0; i < attrCount; i++) {
  122. const XMLAttr* oneAttrib = attrList.elementAt(i);
  123. const XMLCh* attrValue = oneAttrib->getValue();
  124. unsigned int attrURIId = oneAttrib->getURIId();
  125. if (XMLString::equals(oneAttrib->getName(), XMLUni::fgXMLNSString))
  126. fURIs->addElement(fScanner->getPrefixId(XMLUni::fgZeroLenString));
  127. else if (!XMLString::compareNString(oneAttrib->getQName(), XMLUni::fgXMLNSColonString, 6))
  128. fURIs->addElement(fScanner->getPrefixId(oneAttrib->getName()));
  129. fAnnotationBuf.append(oneAttrib->getQName());
  130. fAnnotationBuf.append(chEqual);
  131. fAnnotationBuf.append(chDoubleQuote);
  132. fAnnotationBuf.append(attrValue);
  133. fAnnotationBuf.append(chDoubleQuote);
  134. fAnnotationBuf.append(chSpace);
  135. }
  136. // now we have to look through currently in-scope namespaces to see what
  137. // wasn't declared here
  138. ValueVectorOf<PrefMapElem*>* namespaceContext = fScanner->getNamespaceContext();
  139. for (unsigned int j=0; j < namespaceContext->size(); j++)
  140. {
  141. unsigned int prefId = namespaceContext->elementAt(j)->fPrefId;
  142. if (!fURIs->containsElement(prefId)) {
  143. const XMLCh* prefix = fScanner->getPrefixForId(prefId);
  144. if (XMLString::equals(prefix, XMLUni::fgZeroLenString)) {
  145. fAnnotationBuf.append(XMLUni::fgXMLNSString);
  146. }
  147. else {
  148. fAnnotationBuf.append(XMLUni::fgXMLNSColonString);
  149. fAnnotationBuf.append(prefix);
  150. }
  151. fAnnotationBuf.append(chEqual);
  152. fAnnotationBuf.append(chDoubleQuote);
  153. fAnnotationBuf.append(fScanner->getURIText(namespaceContext->elementAt(j)->fURIId));
  154. fAnnotationBuf.append(chDoubleQuote);
  155. fAnnotationBuf.append(chSpace);
  156. }
  157. }
  158. fAnnotationBuf.append(chCloseAngle);
  159. fAnnotationBuf.append(chLF);
  160. }
  161. void XSDDOMParser::startAnnotationElement( const XMLElementDecl& elemDecl
  162. , const RefVectorOf<XMLAttr>& attrList
  163. , const unsigned int attrCount)
  164. {
  165. fAnnotationBuf.append(chOpenAngle);
  166. fAnnotationBuf.append(elemDecl.getFullName());
  167. //fAnnotationBuf.append(chSpace);
  168. for(unsigned int i=0; i < attrCount; i++) {
  169. const XMLAttr* oneAttr = attrList.elementAt(i);
  170. fAnnotationBuf.append(chSpace);
  171. fAnnotationBuf.append(oneAttr ->getQName());
  172. fAnnotationBuf.append(chEqual);
  173. fAnnotationBuf.append(chDoubleQuote);
  174. fAnnotationBuf.append(oneAttr->getValue());
  175. fAnnotationBuf.append(chDoubleQuote);
  176. }
  177. fAnnotationBuf.append(chCloseAngle);
  178. }
  179. void XSDDOMParser::endAnnotationElement( const XMLElementDecl& elemDecl
  180. , bool complete)
  181. {
  182. if (complete)
  183. {
  184. fAnnotationBuf.append(chLF);
  185. fAnnotationBuf.append(chOpenAngle);
  186. fAnnotationBuf.append(chForwardSlash);
  187. fAnnotationBuf.append(elemDecl.getFullName());
  188. fAnnotationBuf.append(chCloseAngle);
  189. // note that this is always called after endElement on <annotation>'s
  190. // child and before endElement on annotation.
  191. // hence, we must make this the child of the current
  192. // parent's only child.
  193. DOMTextImpl *node = (DOMTextImpl *)fDocument->createTextNode(fAnnotationBuf.getRawBuffer());
  194. fCurrentNode->appendChild(node);
  195. fAnnotationBuf.reset();
  196. }
  197. else //capturing character calls
  198. {
  199. fAnnotationBuf.append(chOpenAngle);
  200. fAnnotationBuf.append(chForwardSlash);
  201. fAnnotationBuf.append(elemDecl.getFullName());
  202. fAnnotationBuf.append(chCloseAngle);
  203. }
  204. }
  205. // ---------------------------------------------------------------------------
  206. // XSDDOMParser: Setter methods
  207. // ---------------------------------------------------------------------------
  208. void XSDDOMParser::setUserErrorReporter(XMLErrorReporter* const errorReporter)
  209. {
  210. fUserErrorReporter = errorReporter;
  211. fScanner->setErrorReporter(this);
  212. }
  213. void XSDDOMParser::setUserEntityHandler(XMLEntityHandler* const entityHandler)
  214. {
  215. fUserEntityHandler = entityHandler;
  216. fScanner->setEntityHandler(this);
  217. }
  218. // ---------------------------------------------------------------------------
  219. // XSDDOMParser: Implementation of the XMLDocumentHandler interface
  220. // ---------------------------------------------------------------------------
  221. void XSDDOMParser::startElement( const XMLElementDecl& elemDecl
  222. , const unsigned int urlId
  223. , const XMLCh* const elemPrefix
  224. , const RefVectorOf<XMLAttr>& attrList
  225. , const unsigned int attrCount
  226. , const bool isEmpty
  227. , const bool isRoot)
  228. {
  229. fDepth++;
  230. // while it is true that non-whitespace character data
  231. // may only occur in appInfo or documentation
  232. // elements, it's certainly legal for comments and PI's to
  233. // occur as children of annotation; we need
  234. // to account for these here.
  235. if (fAnnotationDepth == -1)
  236. {
  237. if (XMLString::equals(elemDecl.getBaseName(), SchemaSymbols::fgELT_ANNOTATION) &&
  238. XMLString::equals(getURIText(urlId), SchemaSymbols::fgURI_SCHEMAFORSCHEMA))
  239. {
  240. fAnnotationDepth = fDepth;
  241. startAnnotation(elemDecl, attrList, attrCount);
  242. }
  243. }
  244. else if (fDepth == fAnnotationDepth+1)
  245. {
  246. fInnerAnnotationDepth = fDepth;
  247. startAnnotationElement(elemDecl, attrList, attrCount);
  248. }
  249. else
  250. {
  251. startAnnotationElement(elemDecl, attrList, attrCount);
  252. // avoid falling through; don't call startElement in this case
  253. return;
  254. }
  255. DOMElement *elem;
  256. if (urlId != fScanner->getEmptyNamespaceId()) //TagName has a prefix
  257. {
  258. if (elemPrefix && *elemPrefix)
  259. {
  260. XMLBufBid elemQName(&fBufMgr);
  261. elemQName.set(elemPrefix);
  262. elemQName.append(chColon);
  263. elemQName.append(elemDecl.getBaseName());
  264. elem = createElementNSNode(
  265. fScanner->getURIText(urlId), elemQName.getRawBuffer());
  266. }
  267. else {
  268. elem = createElementNSNode(
  269. fScanner->getURIText(urlId), elemDecl.getBaseName());
  270. }
  271. }
  272. else {
  273. elem = createElementNSNode(0, elemDecl.getBaseName());
  274. }
  275. DOMElementImpl *elemImpl = (DOMElementImpl *) elem;
  276. for (unsigned int index = 0; index < attrCount; ++index)
  277. {
  278. const XMLAttr* oneAttrib = attrList.elementAt(index);
  279. unsigned int attrURIId = oneAttrib->getURIId();
  280. const XMLCh* namespaceURI = 0;
  281. //for xmlns=...
  282. if (XMLString::equals(oneAttrib->getName(), XMLUni::fgXMLNSString))
  283. attrURIId = fScanner->getXMLNSNamespaceId();
  284. //TagName has a prefix
  285. if (attrURIId != fScanner->getEmptyNamespaceId())
  286. namespaceURI = fScanner->getURIText(attrURIId); //get namespaceURI
  287. // revisit. Optimize to init the named node map to the
  288. // right size up front.
  289. DOMAttrImpl *attr = (DOMAttrImpl *)
  290. fDocument->createAttributeNS(namespaceURI, oneAttrib->getQName());
  291. attr->setValue(oneAttrib -> getValue());
  292. DOMNode* remAttr = elemImpl->setAttributeNodeNS(attr);
  293. if (remAttr)
  294. remAttr->release();
  295. // Attributes of type ID. If this is one, add it to the hashtable of IDs
  296. // that is constructed for use by GetElementByID().
  297. if (oneAttrib->getType()==XMLAttDef::ID)
  298. {
  299. if (fDocument->fNodeIDMap == 0)
  300. fDocument->fNodeIDMap = new (fDocument) DOMNodeIDMap(500, fDocument);
  301. fDocument->fNodeIDMap->add(attr);
  302. attr->fNode.isIdAttr(true);
  303. }
  304. attr->setSpecified(oneAttrib->getSpecified());
  305. }
  306. // set up the default attributes
  307. if (elemDecl.hasAttDefs())
  308. {
  309. XMLAttDefList* defAttrs = &elemDecl.getAttDefList();
  310. XMLAttDef* attr = 0;
  311. DOMAttrImpl * insertAttr = 0;
  312. while (defAttrs->hasMoreElements())
  313. {
  314. attr = &defAttrs->nextElement();
  315. const XMLAttDef::DefAttTypes defType = attr->getDefaultType();
  316. if ((defType == XMLAttDef::Default)
  317. || (defType == XMLAttDef::Fixed))
  318. {
  319. // DOM Level 2 wants all namespace declaration attributes
  320. // to be bound to "http://www.w3.org/2000/xmlns/"
  321. // So as long as the XML parser doesn't do it, it needs to
  322. // done here.
  323. const XMLCh* qualifiedName = attr->getFullName();
  324. XMLBufBid bbPrefixQName(&fBufMgr);
  325. XMLBuffer& prefixBuf = bbPrefixQName.getBuffer();
  326. int colonPos = -1;
  327. unsigned int uriId = fScanner->resolveQName(qualifiedName, prefixBuf, ElemStack::Mode_Attribute, colonPos);
  328. const XMLCh* namespaceURI = 0;
  329. if (XMLString::equals(qualifiedName, XMLUni::fgXMLNSString))
  330. uriId = fScanner->getXMLNSNamespaceId();
  331. //TagName has a prefix
  332. if (uriId != fScanner->getEmptyNamespaceId())
  333. namespaceURI = fScanner->getURIText(uriId);
  334. insertAttr = (DOMAttrImpl *) fDocument->createAttributeNS(
  335. namespaceURI, qualifiedName);
  336. DOMAttr* remAttr = elemImpl->setDefaultAttributeNodeNS(insertAttr);
  337. if (remAttr)
  338. remAttr->release();
  339. if (attr->getValue() != 0)
  340. {
  341. insertAttr->setValue(attr->getValue());
  342. insertAttr->setSpecified(false);
  343. }
  344. }
  345. insertAttr = 0;
  346. attr->reset();
  347. }
  348. }
  349. fCurrentParent->appendChild(elem);
  350. fNodeStack->push(fCurrentParent);
  351. fCurrentParent = elem;
  352. fCurrentNode = elem;
  353. fWithinElement = true;
  354. // If an empty element, do end right now (no endElement() will be called)
  355. if (isEmpty)
  356. endElement(elemDecl, urlId, isRoot, elemPrefix);
  357. }
  358. void XSDDOMParser::endElement( const XMLElementDecl& elemDecl
  359. , const unsigned int urlId
  360. , const bool isRoot
  361. , const XMLCh* const elemPrefix)
  362. {
  363. if(fAnnotationDepth > -1)
  364. {
  365. if (fInnerAnnotationDepth == fDepth)
  366. {
  367. fInnerAnnotationDepth = -1;
  368. endAnnotationElement(elemDecl, false);
  369. }
  370. else if (fAnnotationDepth == fDepth)
  371. {
  372. fAnnotationDepth = -1;
  373. endAnnotationElement(elemDecl, true);
  374. }
  375. else
  376. { // inside a child of annotation
  377. endAnnotationElement(elemDecl, false);
  378. fDepth--;
  379. return;
  380. }
  381. }
  382. fDepth--;
  383. fCurrentNode = fCurrentParent;
  384. fCurrentParent = fNodeStack->pop();
  385. // If we've hit the end of content, clear the flag
  386. if (fNodeStack->empty())
  387. fWithinElement = false;
  388. }
  389. void XSDDOMParser::docCharacters( const XMLCh* const chars
  390. , const unsigned int length
  391. , const bool cdataSection)
  392. {
  393. // Ignore chars outside of content
  394. if (!fWithinElement)
  395. return;
  396. if (fInnerAnnotationDepth == -1)
  397. {
  398. if (!((ReaderMgr*) fScanner->getReaderMgr())->getCurrentReader()->isAllSpaces(chars, length))
  399. {
  400. ReaderMgr::LastExtEntityInfo lastInfo;
  401. fScanner->getReaderMgr()->getLastExtEntityInfo(lastInfo);
  402. fXSLocator.setValues(lastInfo.systemId, lastInfo.publicId, lastInfo.lineNumber, lastInfo.colNumber);
  403. fXSDErrorReporter.emitError(XMLValid::NonWSContent, XMLUni::fgValidityDomain, &fXSLocator);
  404. }
  405. }
  406. // when it's within either of the 2 annotation subelements, characters are
  407. // allowed and we need to store them.
  408. else if (cdataSection == true)
  409. {
  410. fAnnotationBuf.append(XMLUni::fgCDataStart);
  411. fAnnotationBuf.append(chars, length);
  412. fAnnotationBuf.append(XMLUni::fgCDataEnd);
  413. }
  414. else
  415. {
  416. for(unsigned int i = 0; i < length; i++ )
  417. {
  418. if(chars[i] == chAmpersand)
  419. {
  420. fAnnotationBuf.append(chAmpersand);
  421. fAnnotationBuf.append(XMLUni::fgAmp);
  422. fAnnotationBuf.append(chSemiColon);
  423. }
  424. else if (chars[i] == chOpenAngle)
  425. {
  426. fAnnotationBuf.append(chAmpersand);
  427. fAnnotationBuf.append(XMLUni::fgLT);
  428. fAnnotationBuf.append(chSemiColon);
  429. }
  430. else {
  431. fAnnotationBuf.append(chars[i]);
  432. }
  433. }
  434. }
  435. }
  436. void XSDDOMParser::docComment(const XMLCh* const comment)
  437. {
  438. if (fAnnotationDepth > -1)
  439. {
  440. fAnnotationBuf.append(XMLUni::fgCommentString);
  441. fAnnotationBuf.append(comment);
  442. fAnnotationBuf.append(chDash);
  443. fAnnotationBuf.append(chDash);
  444. fAnnotationBuf.append(chCloseAngle);
  445. }
  446. }
  447. void XSDDOMParser::startEntityReference(const XMLEntityDecl& entDecl)
  448. {
  449. int i=0;
  450. i++;
  451. }
  452. void XSDDOMParser::endEntityReference(const XMLEntityDecl& entDecl)
  453. {
  454. int j=0;
  455. j++;
  456. }
  457. void XSDDOMParser::ignorableWhitespace( const XMLCh* const chars
  458. , const unsigned int length
  459. , const bool cdataSection)
  460. {
  461. // Ignore chars before the root element
  462. if (!fWithinElement || !fIncludeIgnorableWhitespace)
  463. return;
  464. if (fAnnotationDepth > -1)
  465. fAnnotationBuf.append(chars, length);
  466. }
  467. // ---------------------------------------------------------------------------
  468. // XSDDOMParser: Implementation of the XMLErrorReporter interface
  469. // ---------------------------------------------------------------------------
  470. void XSDDOMParser::error(const unsigned int code
  471. , const XMLCh* const msgDomain
  472. , const XMLErrorReporter::ErrTypes errType
  473. , const XMLCh* const errorText
  474. , const XMLCh* const systemId
  475. , const XMLCh* const publicId
  476. , const XMLSSize_t lineNum
  477. , const XMLSSize_t colNum)
  478. {
  479. if (errType >= XMLErrorReporter::ErrType_Fatal)
  480. fSawFatal = true;
  481. if (fUserErrorReporter)
  482. fUserErrorReporter->error(code, msgDomain, errType, errorText,
  483. systemId, publicId, lineNum, colNum);
  484. }
  485. InputSource* XSDDOMParser::resolveEntity(const XMLCh* const publicId,
  486. const XMLCh* const systemId,
  487. const XMLCh* const baseURI)
  488. {
  489. if (fUserEntityHandler)
  490. return fUserEntityHandler->resolveEntity(publicId, systemId, baseURI);
  491. return 0;
  492. }
  493. XERCES_CPP_NAMESPACE_END