From a826b8c9337e06797155aa86ea8c182d41b96e49 Mon Sep 17 00:00:00 2001 From: "axel%pike.org" Date: Mon, 10 Sep 2001 18:01:23 +0000 Subject: [PATCH] bug 98704, xml/dom part of beating DOMHelper::getParentNode out of the tree, r=peterv, rs=brendan git-svn-id: svn://10.0.0.236/trunk@102651 18797224-902f-48f8-a5cc-f745e15eee43 --- .../source/xml/dom/mozImpl/MozillaAttr.cpp | 18 ++- .../source/xml/dom/mozImpl/MozillaNode.cpp | 11 +- .../source/xml/dom/mozImpl/mozilladom.h | 8 +- .../source/xml/dom/standalone/Attr.cpp | 15 +- .../source/xml/dom/standalone/Element.cpp | 9 +- .../xml/dom/standalone/NamedNodeMap.cpp | 28 +++- .../xml/dom/standalone/NodeDefinition.cpp | 74 ++++----- .../source/xml/dom/standalone/dom.h | 41 ++++- .../source/xml/util/DOMHelper.cpp | 144 +----------------- .../transformiix/source/xml/util/DOMHelper.h | 57 +------ 10 files changed, 152 insertions(+), 253 deletions(-) diff --git a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaAttr.cpp b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaAttr.cpp index 72d0ba0a176..75179cb4413 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaAttr.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaAttr.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -103,3 +104,18 @@ void Attr::setValue(const String& aNewValue) if (nsAttr) nsAttr->SetValue(aNewValue.getConstNSString()); } + +/* + * Call nsIDOMAttr::GetOwnerElement to get the owning element + * + * @return the xpath parent + */ +Node* Attr::getXPathParent() +{ + NSI_FROM_TX_NULL_CHECK(Attr) + nsCOMPtr ownerElem; + + if (NS_SUCCEEDED(nsAttr->GetOwnerElement(getter_AddRefs(ownerElem)))) + return ownerDocument->createWrapper(ownerElem); + return NULL; +} diff --git a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaNode.cpp b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaNode.cpp index 0bea4bcfce1..7fe4b6f03d5 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaNode.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/MozillaNode.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -390,3 +391,11 @@ String Node::getBaseURI() return url; } + +/* + * Returns the parent node according to the XPath datamodel + */ +Node* Node::getXPathParent() +{ + return getParentNode(); +} diff --git a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/mozilladom.h b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/mozilladom.h index 1c0fd2c4891..92ae4c0f833 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/mozImpl/mozilladom.h +++ b/mozilla/extensions/transformiix/source/xml/dom/mozImpl/mozilladom.h @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -240,6 +241,9 @@ class Node : public MozillaObjectWrapper //From DOM3 26-Jan-2001 WD virtual String getBaseURI(); + // txXPathNode functions + virtual Node* getXPathParent(); + protected: String nodeName; String nodeValue; @@ -409,6 +413,8 @@ class Attr : public Node const String& getValue(); void setValue(const String& aNewValue); + // txXPathNode functions override + Node* getXPathParent(); }; /** diff --git a/mozilla/extensions/transformiix/source/xml/dom/standalone/Attr.cpp b/mozilla/extensions/transformiix/source/xml/dom/standalone/Attr.cpp index 683891c25b3..3b0ed8c16b4 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/standalone/Attr.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/standalone/Attr.cpp @@ -1,4 +1,5 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -22,12 +23,6 @@ // // Implementation of the Document Object Model Level 1 Core // Implementation of the Attr class -// -// Modification History: -// Who When What -// TK 03/29/99 Created -// LF 08/06/1999 fixed typo: defalut to default - // #include "dom.h" @@ -139,3 +134,11 @@ Node* Attr::insertBefore(Node* newChild, Node* refChild) return returnVal; } + +// +//Return the attributes owning element +// +Node* Attr::getXPathParent() +{ + return ownerElement; +} diff --git a/mozilla/extensions/transformiix/source/xml/dom/standalone/Element.cpp b/mozilla/extensions/transformiix/source/xml/dom/standalone/Element.cpp index 20ad5908fa4..cb4d64363c1 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/standalone/Element.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/standalone/Element.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -23,11 +24,6 @@ // Implementation of the Document Object Model Level 1 Core // Implementation of the Element class // -// Modification History: -// Who When What -// TK 03/29/99 Created -// LF 08/06/1999 fixed typo: defalut to default -// #include "dom.h" @@ -39,6 +35,7 @@ Element::Element(const String& tagName, Document* owner) : NodeDefinition(Node::ELEMENT_NODE, tagName, NULL_STRING, owner) { + attributes.ownerElement = this; } // diff --git a/mozilla/extensions/transformiix/source/xml/dom/standalone/NamedNodeMap.cpp b/mozilla/extensions/transformiix/source/xml/dom/standalone/NamedNodeMap.cpp index a38eea277e7..9be582603f3 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/standalone/NamedNodeMap.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/standalone/NamedNodeMap.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -23,10 +24,6 @@ // Implementation of the Document Object Model Level 1 Core // Implementation of the NamedNodeMap class // -// Modification History: -// Who When What -// TK 03/29/99 Created -// #include "dom.h" @@ -109,3 +106,24 @@ NodeListDefinition::ListItem* return NULL; } + +AttrMap::AttrMap() +{ + ownerElement = NULL; +} + +Node* AttrMap::setNamedItem(Node* arg) +{ + if (arg->getNodeType() != Node::ATTRIBUTE_NODE) + return NULL; + ((Attr*)arg)->ownerElement = ownerElement; + return NamedNodeMap::setNamedItem(arg); +} + +Node* AttrMap::removeNamedItem(const String& name) +{ + Attr* node = (Attr*)NamedNodeMap::removeNamedItem(name); + if (node) + node->ownerElement = NULL; + return node; +} diff --git a/mozilla/extensions/transformiix/source/xml/dom/standalone/NodeDefinition.cpp b/mozilla/extensions/transformiix/source/xml/dom/standalone/NodeDefinition.cpp index 2aca1c92adb..5d74657ba96 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/standalone/NodeDefinition.cpp +++ b/mozilla/extensions/transformiix/source/xml/dom/standalone/NodeDefinition.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- + * * (C) Copyright The MITRE Corporation 1999 All rights reserved. * * The contents of this file are subject to the Mozilla Public License @@ -359,48 +360,53 @@ MBool NodeDefinition::hasChildNodes() const return MB_FALSE; } -/** +Node* NodeDefinition::getXPathParent() +{ + return parentNode; +} + +/* * Returns the base URI of the node. Acccounts for xml:base * attributes. * * @return base URI for the node -**/ + */ String NodeDefinition::getBaseURI() { - Node* node=this; - ArrayList baseUrls; - String url; - Node* xbAttr; + Node* node = this; + ArrayList baseUrls; + String url; + Node* xbAttr; + + while (node) { + switch (node->getNodeType()) { + case Node::ELEMENT_NODE : + xbAttr = ((Element*)node)->getAttributeNode(XMLBASE_ATTR); + if (xbAttr) + baseUrls.add(new String(xbAttr->getNodeValue())); + break; + + case Node::DOCUMENT_NODE : + baseUrls.add(new String(((Document*)node)->getBaseURI())); + break; - while(node) { - switch(node->getNodeType()) { - case Node::ELEMENT_NODE : - xbAttr = ((Element*)node)->getAttributeNode(XMLBASE_ATTR); - if(xbAttr) - baseUrls.add(new String(xbAttr->getNodeValue())); - break; - - case Node::DOCUMENT_NODE : - baseUrls.add(new String(((Document*)node)->getBaseURI())); - break; - - default: - break; - } - node = node->getParentNode(); + default: + break; } + node = node->getParentNode(); + } - if(baseUrls.size()) { - url = *((String*)baseUrls.get(baseUrls.size()-1)); + if (baseUrls.size()) { + url = *((String*)baseUrls.get(baseUrls.size()-1)); - for(int i=baseUrls.size()-2;i>=0;i--) { - String dest; - URIUtils::resolveHref(*(String*)baseUrls.get(i), url, dest); - url = dest; - } + for (int i=baseUrls.size()-2;i>=0;i--) { + String dest; + URIUtils::resolveHref(*(String*)baseUrls.get(i), url, dest); + url = dest; } + } - baseUrls.clear(MB_TRUE); - - return url; -} //-- getBaseURI + baseUrls.clear(MB_TRUE); + + return url; +} // getBaseURI diff --git a/mozilla/extensions/transformiix/source/xml/dom/standalone/dom.h b/mozilla/extensions/transformiix/source/xml/dom/standalone/dom.h index a25caa59f30..fb5eb410821 100644 --- a/mozilla/extensions/transformiix/source/xml/dom/standalone/dom.h +++ b/mozilla/extensions/transformiix/source/xml/dom/standalone/dom.h @@ -127,6 +127,9 @@ class Node : public TxObject //From DOM3 26-Jan-2001 WD virtual String getBaseURI() = 0; + + //txXPathNode functions + virtual Node* getXPathParent() = 0; }; // @@ -189,11 +192,31 @@ class NamedNodeMap : public NodeListDefinition ~NamedNodeMap(); Node* getNamedItem(const String& name); + virtual Node* setNamedItem(Node* arg); + virtual Node* removeNamedItem(const String& name); + + private: + NodeListDefinition::ListItem* findListItemByName(const String& name); +}; + +// +// Subclass of NamedNodeMap that contains a list of attributes. +// Whenever an attribute is added to or removed from the map, the attributes +// ownerElement is updated. +// +class AttrMap : public NamedNodeMap +{ + // Elenent needs to be friend to be able to set the AttrMaps ownerElement + friend Element; + + public: + AttrMap(); + Node* setNamedItem(Node* arg); Node* removeNamedItem(const String& name); private: - NodeListDefinition::ListItem* findListItemByName(const String& name); + Element* ownerElement; }; // @@ -234,10 +257,14 @@ class NodeDefinition : public Node, public NodeList Node* cloneNode(MBool deep, Node* dest); MBool hasChildNodes() const; - + + //From DOM3 26-Jan-2001 WD virtual String getBaseURI(); - //Inherrited from NodeList + //txXPathNode functions + virtual Node* getXPathParent(); + + //Inherited from NodeList Node* item(PRUint32 index); PRUint32 getLength(); @@ -247,7 +274,7 @@ class NodeDefinition : public Node, public NodeList //than the generic node does. String nodeName; String nodeValue; - NamedNodeMap attributes; + AttrMap attributes; void DeleteChildren(); @@ -360,6 +387,8 @@ class Element : public NodeDefinition // class Attr : public NodeDefinition { + // AttrMap needs to be friend to be able to update the ownerElement + friend AttrMap; public: Attr(const String& name, Document* owner); @@ -377,7 +406,11 @@ class Attr : public NodeDefinition //children Node* insertBefore(Node* newChild, Node* refChild); + //txXPathNode functions override + Node* getXPathParent(); + private: + Element* ownerElement; MBool specified; }; diff --git a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp index 285d8f66870..04ca3b1ba62 100644 --- a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp +++ b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.cpp @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of @@ -37,14 +38,7 @@ DOMHelper::DOMHelper() {}; /** * Delets this DOMHelper **/ -DOMHelper::~DOMHelper() { - ListIterator* iter = indexes.iterator(); - while (iter->hasNext()) { - IndexState* idxState = (IndexState*)iter->next(); - delete idxState; - } - delete iter; -} //-- ~DOMHelper +DOMHelper::~DOMHelper() {} /** @@ -108,32 +102,8 @@ void DOMHelper::generateId(Node* node, String& dest) { * @param node the Node to return the parent of **/ Node* DOMHelper::getParentNode(Node* node) { - if (!node) return 0; - //XXX Namespace: the parent of a namespace node is the element - if (node->getNodeType() != Node::ATTRIBUTE_NODE) - return node->getParentNode(); - -#ifndef TX_EXE - // XXX temporary fix for 70979 - nsCOMPtr attr(do_QueryInterface(node->getNSObj())); - nsCOMPtr tmpParent; - - if (attr && NS_SUCCEEDED(attr->GetOwnerElement(getter_AddRefs(tmpParent)))) - return node->getOwnerDocument()->createWrapper(tmpParent); - return NULL; -#else - TxObjectWrapper* wrapper = 0; - - wrapper = (TxObjectWrapper*) parents.get(node); - if (!wrapper) { - continueIndexing(node); - wrapper = (TxObjectWrapper*) parents.get(node); - } - - if (wrapper) return (Node*)wrapper->object; - return 0; -#endif + return node->getXPathParent(); } //-- getParentNode @@ -141,94 +111,6 @@ Node* DOMHelper::getParentNode(Node* node) { //- Private Methods -/ //-------------------/ -/** - * Adds the given child/parent mapping -**/ -void DOMHelper::addParentReference(Node* child, Node* parent) { - TxObjectWrapper* wrapper = (TxObjectWrapper*) parents.get(child); - if (!wrapper) { - wrapper = new TxObjectWrapper(); - parents.put(wrapper, child); - } - wrapper->object = parent; - -} //-- addParentReference - -/** - * Continues indexing until the given node has been indexed - * @param node the target Node -**/ -void DOMHelper::continueIndexing(Node* node) { - if (!node) return; - - //-- get indexing information - Document* doc = 0; - if (node->getNodeType() == Node::DOCUMENT_NODE) - doc = (Document*)node; - else - doc = node->getOwnerDocument(); - - ListIterator* iter = indexes.iterator(); - IndexState* idxState = 0; - while (iter->hasNext()) { - idxState = (IndexState*)iter->next(); - if (idxState->document == doc) break; - idxState = 0; - } - - if (!idxState) { - idxState = new IndexState(); - indexes.add(idxState); - idxState->document = doc; - idxState->next = doc->getDocumentElement(); - if (!idxState->next) idxState->done = MB_TRUE; - } - - if (idxState->done) return; - - MBool found = MB_FALSE; - MBool alreadyProcessed = MB_FALSE; - - while (!found) { - - if (!idxState->next) { - idxState->done = MB_TRUE; - break; - } - - if (!alreadyProcessed) { - //-- index attributes - if (idxState->next->getNodeType() == Node::ELEMENT_NODE) { - Element* element = (Element*)idxState->next; - NamedNodeMap* atts = element->getAttributes(); - if (atts) { - for (PRUint32 i = 0; i < atts->getLength(); i++) { - Node* tmpNode = atts->item(i); - addParentReference(tmpNode, element); - if (node == tmpNode) found = MB_TRUE; - } - } - } - } - - //-- set next node to check - if ((!alreadyProcessed) && (idxState->next->hasChildNodes())) { - Node* child = idxState->next->getFirstChild(); - idxState->next = child; - } - else if (idxState->next->getNextSibling()) { - idxState->next = idxState->next->getNextSibling(); - alreadyProcessed = MB_FALSE; - } - else { - idxState->next = getParentNode(idxState->next); - alreadyProcessed = MB_TRUE; - } - } - -} //-- continueIndexing - - /** * Returns the child number of the given node. Numbering * starts at 1 for all nodes except the Document node and @@ -304,24 +186,6 @@ OrderInfo* DOMHelper::getDocumentOrder(Node* node) { } //-- getDocumentOrder -//--------------------------------/ -//- implementation of IndexState -/ -//--------------------------------/ - -/** - * Creates a new IndexState -**/ -IndexState::IndexState() { - document = 0; - done = MB_FALSE; - next = 0; -} //-- IndexState - -/** - * Deletes this IndexState -**/ -IndexState::~IndexState() {}; - //-------------------------------/ //- Implementation of OrderInfo -/ //-------------------------------/ diff --git a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h index 6c2420cdc70..77c3ff14492 100644 --- a/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h +++ b/mozilla/extensions/transformiix/source/xml/util/DOMHelper.h @@ -1,4 +1,5 @@ -/* +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of @@ -107,19 +108,6 @@ public: private: - - - /** - * Adds the given child/parent mapping to the list of parents - **/ - void addParentReference(Node* child, Node* parent); - - /** - * Indexes until the given node has been indexed - **/ - void continueIndexing(Node* node); - - /** * Returns the DocumentOrder for the given Node * @param node a pointer to the Node in which to return the @@ -128,53 +116,12 @@ private: **/ OrderInfo* getDocumentOrder(Node* node); - - /** - * A Hashtable of attribute's parent nodes - **/ - Map parents; - /** * A Hashtable of Node/OrderInfo mappings **/ Map orders; - /** - * A list of IndexState objects (one for each Document) - **/ - List indexes; - - }; //-- DOMHelper -/** - * A class which holds information about the current state of - * indexing -**/ -class IndexState { - - public: - - IndexState(); - ~IndexState(); - - /** - * The Document that this IndexState is for - **/ - Document* document; - - /** - * The next node to index - **/ - Node* next; - - /** - * A boolean indicating if the indexing has completed - **/ - MBool done; - -}; //-- IndexState - - #endif