Back to index

lightning-sunbird  0.9+nobinonly
Document.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 // Tom Kneeland (3/29/99)
00039 //
00040 //  Implementation of the Document Object Model Level 1 Core
00041 //    Implementation of the Document class
00042 //
00043 // Modification History:
00044 // Who  When        What
00045 // TK   03/29/99    Created
00046 // LF   08/06/1999  Removed Default argument initializer from
00047 //                  Document() constructor
00048 // LF   08/06/1999  fixed typo: defalut to default
00049 //
00050 
00051 #include "dom.h"
00052 
00053 //
00054 //Construct a Document.  Currently no parameters are required, but the the
00055 //node constructor is called to identify the node type.
00056 //
00057 Document::Document() : NodeDefinition(Node::DOCUMENT_NODE, EmptyString(), NULL)
00058 {
00059   mIDMap.Init(0);
00060   documentElement = nsnull;
00061 }
00062 
00063 //
00064 //Return the one and only element for this document
00065 //
00066 Element* Document::getDocumentElement()
00067 {
00068   return documentElement;
00069 }
00070 
00071 //
00072 //Construct an empty document fragment.
00073 //    NOTE:  The caller is responsible for cleaning up this fragment's memory
00074 //           when it is no longer needed.
00075 //
00076 Node* Document::createDocumentFragment()
00077 {
00078   return new DocumentFragment(this);
00079 }
00080 
00081 //
00082 //Construct an element with the specified tag name.
00083 //    NOTE:  The caller is responsible for cleaning up the element's menory
00084 //
00085 Element* Document::createElement(const nsAString& tagName)
00086 {
00087   return new Element(tagName, this);
00088 }
00089 
00090 Element* Document::createElementNS(const nsAString& aNamespaceURI,
00091                                    const nsAString& aTagName)
00092 {
00093   return new Element(aNamespaceURI, aTagName, this);
00094 }
00095 
00096 //
00097 //Construct an attribute with the specified name
00098 //
00099 Attr* Document::createAttribute(const nsAString& name)
00100 {
00101   return new Attr(name, this);
00102 }
00103 
00104 Attr* Document::createAttributeNS(const nsAString& aNamespaceURI,
00105                                   const nsAString& aName)
00106 {
00107   return new Attr(aNamespaceURI, aName, this);
00108 }
00109 
00110 //
00111 //Construct a text node with the given data
00112 //
00113 Node* Document::createTextNode(const nsAString& theData)
00114 {
00115   return new NodeDefinition(Node::TEXT_NODE, theData, this);
00116 }
00117 
00118 //
00119 //Construct a comment node with the given data
00120 //
00121 Node* Document::createComment(const nsAString& theData)
00122 {
00123   return new NodeDefinition(Node::COMMENT_NODE, theData, this);
00124 }
00125 
00126 //
00127 //Construct a ProcessingInstruction node with the given targe and data.
00128 //
00129 ProcessingInstruction*
00130   Document::createProcessingInstruction(const nsAString& target,
00131                                         const nsAString& data)
00132 {
00133   return new ProcessingInstruction(target, data, this);
00134 }
00135 
00136 //
00137 //Return an Element by ID, introduced by DOM2
00138 //
00139 DHASH_WRAPPER(txIDMap, txIDEntry, nsAString&)
00140 
00141 Element* Document::getElementById(const nsAString& aID)
00142 {
00143   txIDEntry* entry = mIDMap.GetEntry(aID);
00144   if (entry)
00145     return entry->mElement;
00146   return nsnull;
00147 }
00148 
00152 PRBool
00153 Document::setElementID(const nsAString& aID, Element* aElement)
00154 {
00155   txIDEntry* id = mIDMap.AddEntry(aID);
00156   // make sure IDs are unique
00157   if (id->mElement) {
00158     return PR_FALSE;
00159   }
00160   id->mElement = aElement;
00161   id->mElement->setIDValue(aID);
00162   return PR_TRUE;
00163 }
00164 
00165 Node* Document::appendChild(Node* newChild)
00166 {
00167   unsigned short nodeType = newChild->getNodeType();
00168 
00169   // Convert to a NodeDefinition Pointer
00170   NodeDefinition* pNewChild = (NodeDefinition*)newChild;
00171 
00172   if (pNewChild->parentNode == this)
00173     {
00174       pNewChild = implRemoveChild(pNewChild);
00175       if (nodeType == Node::ELEMENT_NODE)
00176         documentElement = nsnull;
00177     }
00178 
00179   switch (nodeType)
00180     {
00181       case Node::PROCESSING_INSTRUCTION_NODE :
00182       case Node::COMMENT_NODE :
00183       case Node::DOCUMENT_TYPE_NODE :
00184         return implAppendChild(pNewChild);
00185 
00186       case Node::ELEMENT_NODE :
00187         if (!documentElement)
00188           {
00189             Node* returnVal = implAppendChild(pNewChild);
00190             documentElement = (Element*)pNewChild;
00191             return returnVal;
00192           }
00193 
00194       default:
00195         break;
00196     }
00197 
00198   return nsnull;
00199 }
00200 
00201 nsresult Document::getBaseURI(nsAString& aURI)
00202 {
00203   aURI = documentBaseURI;
00204   return NS_OK;
00205 }