ElementImpl.cpp
上传用户:zhuqijet
上传日期:2013-06-25
资源大小:10074k
文件大小:17k
源码类别:

词法分析

开发平台:

Visual C++

  1. /*
  2.  * The Apache Software License, Version 1.1
  3.  *
  4.  * Copyright (c) 1999-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) 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: ElementImpl.cpp,v 1.5 2003/05/22 02:26:50 knoaman Exp $
  58.  */
  59. #include "DeepNodeListImpl.hpp"
  60. #include "DocumentImpl.hpp"
  61. #include "DocumentTypeImpl.hpp"
  62. #include "DOM_DOMException.hpp"
  63. #include "DStringPool.hpp"
  64. #include "ElementImpl.hpp"
  65. #include "ElementDefinitionImpl.hpp"
  66. #include "NamedNodeMapImpl.hpp"
  67. #include "NodeVector.hpp"
  68. XERCES_CPP_NAMESPACE_BEGIN
  69. static DOMString *gEmptyString = 0;
  70. static XMLRegisterCleanup emptyStringCleanup;
  71. ElementImpl::ElementImpl(DocumentImpl *ownerDoc, const DOMString &eName)
  72.     : ParentNode(ownerDoc)
  73. {
  74.     name = eName.clone();
  75.     attributes = null;
  76. setupDefaultAttributes();
  77. };
  78. ElementImpl::ElementImpl(const ElementImpl &other, bool deep)
  79.     : ParentNode(other)
  80. {
  81.     name = other.name.clone();
  82. attributes = null;
  83. setupDefaultAttributes();
  84.     if (deep)
  85.         cloneChildren(other);
  86. if (other.attributes != null)
  87. {
  88. if (attributes)
  89. {
  90. attributes->removeAll();
  91. NamedNodeMapImpl::removeRef(attributes);
  92. }
  93. attributes = other.attributes->cloneAttrMap(this);
  94. }
  95. };
  96. ElementImpl::~ElementImpl()
  97. {
  98.     if (attributes)
  99.     {
  100.         attributes->removeAll();
  101.         NamedNodeMapImpl::removeRef(attributes);
  102.     }
  103. };
  104. NodeImpl *ElementImpl::cloneNode(bool deep)
  105. {
  106.     return new (getOwnerDocument()->getMemoryManager()) ElementImpl(*this, deep);
  107. };
  108. /**
  109.  * NON-DOM
  110.  * set the ownerDocument of this node, its children, and its attributes
  111.  */
  112. void ElementImpl::setOwnerDocument(DocumentImpl *doc) {
  113.     ParentNode::setOwnerDocument(doc);
  114. if (attributes != null)
  115. attributes->setOwnerDocument(doc);
  116. }
  117. DOMString ElementImpl::getNodeName() {
  118.     return name;
  119. };
  120. short ElementImpl::getNodeType() {
  121.     return DOM_Node::ELEMENT_NODE;
  122. };
  123. DOMString ElementImpl::getAttribute(const DOMString &nam)
  124. {
  125.     AttrImpl * attr=null;
  126.     if (attributes != null)
  127. attr=(AttrImpl *)(attributes->getNamedItem(nam));
  128.     return (attr==null) ? DStringPool::getStaticString(""
  129.                                                      , &gEmptyString
  130.                                                      , reinitElementImpl
  131.                                                      , emptyStringCleanup) : attr->getValue();
  132. };
  133. AttrImpl *ElementImpl::getAttributeNode(const DOMString &nam)
  134. {
  135.     return (attributes == 0) ? null : (AttrImpl *)(attributes->getNamedItem(nam));
  136. };
  137. NamedNodeMapImpl *ElementImpl::getAttributes()
  138. {
  139.     return attributes;
  140. };
  141. DeepNodeListImpl *ElementImpl::getElementsByTagName(const DOMString &tagname)
  142. {
  143.     return new (getOwnerDocument()->getMemoryManager()) DeepNodeListImpl(this,tagname);
  144. };
  145. DOMString ElementImpl::getTagName()
  146. {
  147.     return name;
  148. }
  149. bool ElementImpl::isElementImpl()
  150. {
  151.     return true;
  152. };
  153. void ElementImpl::removeAttribute(const DOMString &nam)
  154. {
  155.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  156.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  157.                                null);
  158.     }
  159.     if (attributes != null)
  160.     {
  161.      AttrImpl *att = (AttrImpl *) attributes->getNamedItem(nam);
  162.      // Remove it
  163.      if (att != null)
  164.      {
  165.          attributes->removeNamedItem(nam);
  166.          if (att->nodeRefCount == 0)
  167.              NodeImpl::deleteIf(att);
  168.      }
  169.     }
  170. };
  171. AttrImpl *ElementImpl::removeAttributeNode(AttrImpl *oldAttr)
  172. {
  173.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  174.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  175.                                null);
  176.     }
  177.     if (attributes != null)
  178.     {
  179.     AttrImpl *found = (AttrImpl *) attributes->getNamedItem(oldAttr->getName());
  180.     // If it is in fact the right object, remove it.
  181.     if (found == oldAttr)
  182.         attributes->removeNamedItem(oldAttr->getName());
  183.     else
  184.         throw DOM_DOMException(DOM_DOMException::NOT_FOUND_ERR, null);
  185.         return found;
  186. }
  187. return null; // just to keep the compiler happy
  188. };
  189. AttrImpl *ElementImpl::setAttribute(const DOMString &nam, const DOMString &val)
  190. {
  191.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  192.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  193.                                null);
  194.     }
  195.     AttrImpl* newAttr = (AttrImpl*)getAttributeNode(nam);
  196.     if (!newAttr)
  197.     {
  198. if (attributes == 0) {
  199.             attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this, null);
  200.         }
  201.         newAttr = (AttrImpl*)ownerDocument->createAttribute(nam);
  202.         attributes->setNamedItem(newAttr);
  203.     }
  204.     newAttr->setNodeValue(val);       // Note that setNodeValue on attribute
  205.                                       //   nodes takes care of deleting
  206.                                       //   any previously existing children.
  207.     return newAttr;
  208. };
  209. AttrImpl * ElementImpl::setAttributeNode(AttrImpl *newAttr)
  210. {
  211.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  212.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  213.                                null);
  214.     }
  215.     if (!(newAttr->isAttrImpl()))
  216.         throw DOM_DOMException(DOM_DOMException::WRONG_DOCUMENT_ERR, null);
  217. if (attributes == 0) {
  218.         attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this, null);
  219.     }
  220.     AttrImpl *oldAttr =
  221.       (AttrImpl *) attributes->getNamedItem(newAttr->getName());
  222.     // This will throw INUSE if necessary
  223.     attributes->setNamedItem(newAttr);
  224.     // Attr node reference counting note:
  225.     // If oldAttr's refcount is zero at this point, here's what happens...
  226.     //
  227.     //      oldAttr is returned from this function to DOM_Attr::setAttributeNode,
  228.     //      which wraps a DOM_Attr around the returned pointer and sends it
  229.     //      up to application code, incrementing the reference count in the process.
  230.     //      When the app DOM_Attr's destructor runs, the reference count is
  231.     //      decremented back to zero and oldAttr will be deleted at that time.
  232.     return oldAttr;
  233. };
  234. void ElementImpl::setReadOnly(bool readOnl, bool deep)
  235. {
  236.     ParentNode::setReadOnly(readOnl,deep);
  237.     if (attributes != null)
  238.         attributes->setReadOnly(readOnl,true);
  239. };
  240. //Introduced in DOM Level 2
  241. DOMString ElementImpl::getAttributeNS(const DOMString &fNamespaceURI,
  242. const DOMString &fLocalName)
  243. {
  244.     AttrImpl * attr= (attributes != null) ?
  245.       (AttrImpl *)(attributes->getNamedItemNS(fNamespaceURI, fLocalName))
  246.     : null;
  247.     return (attr==null) ? DOMString(null) : attr->getValue();
  248. }
  249. AttrImpl *ElementImpl::setAttributeNS(const DOMString &fNamespaceURI,
  250. const DOMString &qualifiedName, const DOMString &fValue)
  251. {
  252.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  253.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  254.                                null);
  255.     }
  256.     AttrImpl *newAttr =
  257.       (AttrImpl *) ownerDocument->createAttributeNS(fNamespaceURI,
  258.                                                     qualifiedName);
  259.     newAttr->setNodeValue(fValue);
  260. if (attributes == 0) {
  261.         attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this, null);
  262.     }
  263.     AttrImpl *oldAttr = (AttrImpl *)attributes->setNamedItem(newAttr);
  264.     if (oldAttr) {
  265. if (oldAttr->nodeRefCount == 0)
  266.     NodeImpl::deleteIf(oldAttr);
  267.     }
  268.     return newAttr;
  269. }
  270. void ElementImpl::removeAttributeNS(const DOMString &fNamespaceURI,
  271. const DOMString &fLocalName)
  272. {
  273.     if (getOwnerDocument()->getErrorChecking() && isReadOnly()) {
  274.         throw DOM_DOMException(DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  275.                                null);
  276.     }
  277.     if (attributes != null)
  278.     {
  279. AttrImpl *att =
  280.   (AttrImpl *) attributes->getNamedItemNS(fNamespaceURI, fLocalName);
  281. // Remove it
  282. if (att != null) {
  283. attributes->removeNamedItemNS(fNamespaceURI, fLocalName);
  284. if (att->nodeRefCount == 0)
  285. NodeImpl::deleteIf(att);
  286. }
  287. }
  288. }
  289. AttrImpl *ElementImpl::getAttributeNodeNS(const DOMString &fNamespaceURI,
  290. const DOMString &fLocalName)
  291. {
  292.     return (attributes == 0) ? null : (AttrImpl *)(attributes->getNamedItemNS(fNamespaceURI, fLocalName));
  293. }
  294. AttrImpl *ElementImpl::setAttributeNodeNS(AttrImpl *newAttr)
  295. {
  296.     if (getOwnerDocument()->getErrorChecking()) {
  297.         if (isReadOnly()) {
  298.             throw DOM_DOMException(
  299.                                  DOM_DOMException::NO_MODIFICATION_ALLOWED_ERR,
  300.                                  null);
  301.         }
  302.         if (newAttr->getOwnerDocument() != this->getOwnerDocument()) {
  303.             throw DOM_DOMException(DOM_DOMException::WRONG_DOCUMENT_ERR, null);
  304.         }
  305.     }
  306.     if (attributes == 0) {
  307.         attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this, null);
  308.     }
  309.     AttrImpl *oldAttr = (AttrImpl *) attributes->getNamedItemNS(newAttr->getNamespaceURI(), newAttr->getLocalName());
  310.     // This will throw INUSE if necessary
  311.     attributes->setNamedItemNS(newAttr);
  312.     // Attr node reference counting note:
  313.     // If oldAttr's refcount is zero at this point, here's what happens...
  314.     //
  315.     //      oldAttr is returned from this function to DOM_Attr::setAttributeNode,
  316.     //      which wraps a DOM_Attr around the returned pointer and sends it
  317.     //      up to application code, incrementing the reference count in the process.
  318.     //      When the app DOM_Attr's destructor runs, the reference count is
  319.     //      decremented back to zero and oldAttr will be deleted at that time.
  320.     return oldAttr;
  321. }
  322. DeepNodeListImpl *ElementImpl::getElementsByTagNameNS(const DOMString &fNamespaceURI,
  323. const DOMString &fLocalName)
  324. {
  325.     return new (getOwnerDocument()->getMemoryManager())DeepNodeListImpl(this,fNamespaceURI, fLocalName);
  326. }
  327. bool ElementImpl::hasAttributes()
  328. {
  329.     return (attributes != null && attributes->getLength() != 0);
  330. };
  331. bool ElementImpl::hasAttribute(const DOMString &name)
  332. {
  333.     return (getAttributeNode(name) != null);
  334. };
  335. bool ElementImpl::hasAttributeNS(const DOMString &namespaceURI,
  336. const DOMString &localName)
  337. {
  338.     return (getAttributeNodeNS(namespaceURI, localName) != null);
  339. };
  340. // DOM_NamedNodeMap UTILITIES
  341. NamedNodeMapImpl *ElementImpl::NNM_cloneMap(NodeImpl *nnm_ownerNode)
  342. {
  343. return (getAttributes() == null) ? null : nnm_ownerNode->getAttributes()->cloneMap(nnm_ownerNode);
  344. }
  345. int ElementImpl::NNM_findNamePoint(const DOMString &nnm_name)
  346. {
  347. return (getAttributes() == null) ? -1 : getAttributes()->findNamePoint(nnm_name);
  348. }
  349. unsigned int ElementImpl::NNM_getLength()
  350. {
  351. return (getAttributes() == null) ? 0 : getAttributes()->getLength();
  352. }
  353. NodeImpl *ElementImpl::NNM_getNamedItem(const DOMString &nnm_name)
  354. {
  355. return (getAttributes() == null) ? null : getAttributes()->getNamedItem(nnm_name);
  356. }
  357. NodeImpl *ElementImpl::NNM_item(unsigned int nnm_index)
  358. {
  359. return (getAttributes() == null) ? null : getAttributes()->item(nnm_index);
  360. }
  361. void ElementImpl::NNM_removeAll()
  362. {
  363. if (getAttributes() != null)
  364. getAttributes()->removeAll();
  365. }
  366. NodeImpl *ElementImpl::NNM_removeNamedItem(const DOMString &nnm_name)
  367. {
  368. if (getAttributes() == null)
  369. throw DOM_DOMException(DOM_DOMException::NOT_FOUND_ERR, null);
  370. else
  371. return getAttributes()->removeNamedItem(nnm_name);
  372. return null;
  373. }
  374. NodeImpl *ElementImpl::NNM_setNamedItem(NodeImpl *nnm_arg)
  375. {
  376. if (getAttributes() == null) {
  377.     attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this);
  378.     }
  379. return attributes->setNamedItem(nnm_arg);
  380. }
  381. void ElementImpl::NNM_setReadOnly(bool nnm_readOnly, bool nnm_deep)
  382. {
  383. if (getAttributes() != null)
  384. getAttributes()->setReadOnly(nnm_readOnly, nnm_deep);
  385. }
  386. int ElementImpl::NNM_findNamePoint(const DOMString &nnm_namespaceURI, const DOMString &nnm_localName)
  387. {
  388. return (getAttributes() == null) ? -1 : getAttributes()->findNamePoint(nnm_namespaceURI, nnm_localName);
  389. }
  390. NodeImpl *ElementImpl::NNM_getNamedItemNS(const DOMString &nnm_namespaceURI, const DOMString &nnm_localName)
  391. {
  392. return (getAttributes() == null) ? null : getAttributes()->getNamedItemNS(nnm_namespaceURI, nnm_localName);
  393. }
  394. NodeImpl *ElementImpl::NNM_setNamedItemNS(NodeImpl *nnm_arg)
  395. {
  396. if (getAttributes() == null) {
  397.         attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this);
  398.     }
  399. return getAttributes()->setNamedItemNS(nnm_arg);
  400. }
  401. NodeImpl *ElementImpl::NNM_removeNamedItemNS(const DOMString &nnm_namespaceURI, const DOMString &nnm_localName)
  402. {
  403. if (getAttributes() == null)
  404.         throw DOM_DOMException(DOM_DOMException::NOT_FOUND_ERR, null);
  405. else
  406. return getAttributes()->removeNamedItemNS(nnm_namespaceURI, nnm_localName);
  407. return null;
  408. }
  409. void ElementImpl::NNM_setOwnerDocument(DocumentImpl *nnm_doc)
  410. {
  411. if (getAttributes() != null)
  412. getAttributes()->setOwnerDocument(nnm_doc);
  413. }
  414. // util functions for default attributes
  415. // returns the default attribute map for this node from the owner document
  416. AttrMapImpl *ElementImpl::getDefaultAttributes()
  417. {
  418. if ((ownerNode == null) || (getOwnerDocument() == null))
  419. return null;
  420. DocumentImpl *tmpdoc = getOwnerDocument();
  421. if (tmpdoc->getDoctype() == null)
  422. return null;
  423. NodeImpl *eldef = tmpdoc->getDoctype()->getElements()->getNamedItem(getNodeName());
  424. return (eldef == null) ? null : (AttrMapImpl *)(eldef->getAttributes());
  425. }
  426. // resets all attributes for this node to their default values
  427. void ElementImpl::setupDefaultAttributes()
  428. {
  429. if ((ownerNode == null) || (getOwnerDocument() == null) || (getOwnerDocument()->getDoctype() == null))
  430. return;
  431. if (attributes != 0)
  432. delete attributes;
  433. AttrMapImpl* defAttrs = getDefaultAttributes();
  434. if (defAttrs) {
  435.         attributes = new (getOwnerDocument()->getMemoryManager()) AttrMapImpl(this, defAttrs);
  436.     }
  437. }
  438. // -----------------------------------------------------------------------
  439. //  Notification that lazy data has been deleted
  440. // -----------------------------------------------------------------------
  441. void ElementImpl::reinitElementImpl() {
  442.     delete gEmptyString;
  443.     gEmptyString = 0;
  444. }
  445. XERCES_CPP_NAMESPACE_END