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

词法分析

开发平台:

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: DeepNodeListImpl.cpp,v 1.4 2003/05/22 02:26:50 knoaman Exp $
  58.  */
  59. #include "DeepNodeListImpl.hpp"
  60. #include "NodeVector.hpp"
  61. #include "NodeImpl.hpp"
  62. #include "ElementImpl.hpp"
  63. #include "DStringPool.hpp"
  64. #include "DocumentImpl.hpp"
  65. #include <limits.h>
  66. XERCES_CPP_NAMESPACE_BEGIN
  67. static DOMString *kAstr = 0;
  68. static XMLRegisterCleanup kAstrCleanup;
  69. DeepNodeListImpl::DeepNodeListImpl(NodeImpl *rootNod, const DOMString &tagNam)
  70. {
  71.     changes = 0;
  72.     this->rootNode = rootNod;
  73.     this->tagName = tagNam;
  74.     MemoryManager* manager= rootNod->getDocument()->getMemoryManager();
  75.     nodes=new (manager) NodeVector(manager);
  76.     matchAll = tagName.equals(DStringPool::getStaticString("*"
  77.                                                          , &kAstr
  78.                                                          , reinitDeepNodeListImpl
  79.                                                          , kAstrCleanup));
  80.     this->namespaceURI = null; //DOM Level 2
  81.     this->matchAllURI = false; //DOM Level 2
  82.     this->matchURIandTagname = false; //DOM Level 2
  83. };
  84. //DOM Level 2
  85. DeepNodeListImpl::DeepNodeListImpl(NodeImpl *rootNod,
  86.     const DOMString &fNamespaceURI, const DOMString &localName)
  87. {
  88.     changes = 0;
  89.     this->rootNode = rootNod;
  90.     this->tagName = localName;
  91.     MemoryManager* manager = rootNod->getDocument()->getMemoryManager();
  92.     nodes=new (manager) NodeVector(manager);
  93.     matchAll = tagName.equals(DStringPool::getStaticString("*"
  94.                                                          , &kAstr
  95.                                                          , reinitDeepNodeListImpl
  96.                                                          , kAstrCleanup));
  97.     this->namespaceURI = fNamespaceURI;
  98.     this->matchAllURI = fNamespaceURI.equals(DStringPool::getStaticString("*"
  99.                                                                         , &kAstr
  100.                                                                         , reinitDeepNodeListImpl
  101.                                                                         , kAstrCleanup));
  102.     this->matchURIandTagname = true;
  103. };
  104. DeepNodeListImpl::~DeepNodeListImpl()
  105. {
  106.     delete nodes;
  107. };
  108. unsigned int DeepNodeListImpl::getLength()
  109. {
  110.     // Preload all matching elements. (Stops when we run out of subtree!)
  111.     item(INT_MAX);
  112.     return nodes->size();
  113. };
  114. // Start from the first child and count forward, 0-based. index>length-1
  115. // should return null.
  116. //
  117. // Attempts to do only work actually requested, cache work already
  118. // done, and to flush that cache when the tree has changed.
  119. //
  120. // LIMITATION: ????? Unable to tell relevant tree-changes from
  121. // irrelevant ones.  Doing so in a really useful manner would seem
  122. // to involve a tree-walk in its own right, or maintaining our data
  123. // in a parallel tree.
  124. NodeImpl *DeepNodeListImpl::item(unsigned int index)
  125. {
  126.     NodeImpl *thisNode;
  127.     if(rootNode->changes() != changes)
  128.     {
  129.         nodes->reset();     // Tree changed. Do it all from scratch!
  130.         changes = rootNode->changes();
  131.     }
  132.     if(index< nodes->size())      // In the cache
  133.         return nodes->elementAt((int) index);
  134.     else                        // Not yet seen
  135.     {
  136.         if(nodes->size()==0)     // Pick up where we left off
  137.             thisNode=rootNode; // (Which may be the beginning)
  138.         else
  139.             thisNode=nodes->lastElement();
  140.         while(thisNode!=null && index >= nodes->size() && thisNode!=null)
  141.         {
  142.             thisNode=nextMatchingElementAfter(thisNode);
  143.             if(thisNode!=null)
  144.                 nodes->addElement(thisNode);
  145.         }
  146.         return thisNode;           // Either what we want, or null (not avail.)
  147.     }
  148. };
  149. /* Iterative tree-walker. When you have a Parent link, there's often no
  150. need to resort to recursion. NOTE THAT only Element nodes are matched
  151. since we're specifically supporting getElementsByTagName().
  152. */
  153. NodeImpl *DeepNodeListImpl::nextMatchingElementAfter(NodeImpl *current)
  154. {
  155.     NodeImpl *next;
  156.     while (current != null)
  157.     {
  158.         // Look down to first child.
  159.         if (current->hasChildNodes())
  160.         {
  161.             current = current->getFirstChild();
  162.         }
  163.         // Look right to sibling (but not from root!)
  164.         else
  165.         {
  166.             if (current != rootNode && null != (next = current->getNextSibling()))
  167.             {
  168.                 current = next;
  169.             }
  170.             // Look up and right (but not past root!)
  171.             else
  172.             {
  173.                 next = null;
  174.                 for (; current != rootNode; // Stop when we return to starting point
  175.                 current = current->getParentNode())
  176.                 {
  177.                     next = current->getNextSibling();
  178.                     if (next != null)
  179.                         break;
  180.                 }
  181.                 current = next;
  182.             }
  183.         }
  184.         // Have we found an Element with the right tagName?
  185.         // ("*" matches anything.)
  186.         if (current != null && current != rootNode && current->isElementImpl()) {
  187.     if (!matchURIandTagname) { //DOM Level 1
  188. if (matchAll || ((ElementImpl *)current)->getTagName().equals(tagName))
  189.     return current;
  190.     } else { //DOM Level 2
  191. if (!matchAllURI && !(current -> getNamespaceURI().equals(namespaceURI)))
  192.     continue;
  193. if (matchAll || current -> getLocalName().equals(tagName))
  194.     return current;
  195.     }
  196. }
  197.         // Otherwise continue walking the tree
  198.     }
  199.     // Fell out of tree-walk; no more instances found
  200.     return null;
  201. };
  202. //
  203. //  unreferenced()      The RefCountedImpl base class calls back to this function
  204. //                      when the ref count goes to zero.
  205. //
  206. //
  207. void DeepNodeListImpl::unreferenced()
  208. {
  209. //    delete this;
  210.       DeepNodeListImpl* ptr = this;
  211.       delete ptr;
  212. };
  213. // -----------------------------------------------------------------------
  214. //  Notification that lazy data has been deleted
  215. // -----------------------------------------------------------------------
  216. void DeepNodeListImpl::reinitDeepNodeListImpl() {
  217. delete kAstr;
  218. kAstr = 0;
  219. }
  220. XERCES_CPP_NAMESPACE_END