Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / content / xtf / src / nsXMLContentBuilder.cpp
blobc069bdb2b18892132689bb0e91f5ccde674f40f5
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Mozilla XTF project.
17 * The Initial Developer of the Original Code is
18 * Alex Fritze.
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Alex Fritze <alex@croczilla.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsCOMPtr.h"
40 #include "nsString.h"
41 #include "nsIXMLContentBuilder.h"
42 #include "nsINameSpaceManager.h"
43 #include "nsINodeInfo.h"
44 #include "nsIContent.h"
45 #include "nsIComponentManager.h"
46 #include "nsIServiceManager.h"
47 #include "nsIAtom.h"
48 #include "nsContentCID.h"
49 #include "nsIDocument.h"
50 #include "nsIDOMElement.h"
51 #include "nsIDOMDocument.h"
52 #include "nsIDOMText.h"
53 #include "nsNodeInfoManager.h"
54 #include "nsContentCreatorFunctions.h"
55 #include "nsContentUtils.h"
57 static NS_DEFINE_CID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
59 class nsXMLContentBuilder : public nsIXMLContentBuilder
61 protected:
62 friend nsresult NS_NewXMLContentBuilder(nsIXMLContentBuilder** aResult);
64 nsXMLContentBuilder();
65 ~nsXMLContentBuilder();
67 public:
68 // nsISupports interface
69 NS_DECL_ISUPPORTS
71 // nsIXMLContentBuilder interface
72 NS_DECL_NSIXMLCONTENTBUILDER
74 private:
75 void EnsureDoc();
77 nsCOMPtr<nsIContent> mTop;
78 nsCOMPtr<nsIContent> mCurrent;
79 nsCOMPtr<nsIDocument> mDocument;
80 PRInt32 mNamespaceId;
83 //----------------------------------------------------------------------
84 // implementation:
86 nsXMLContentBuilder::nsXMLContentBuilder()
87 : mNamespaceId(kNameSpaceID_None)
89 #ifdef DEBUG
90 // printf("nsXMLContentBuilder CTOR\n");
91 #endif
94 nsXMLContentBuilder::~nsXMLContentBuilder()
96 #ifdef DEBUG
97 // printf("~nsXMLContentBuilder\n");
98 #endif
101 nsresult
102 NS_NewXMLContentBuilder(nsIXMLContentBuilder** aResult)
104 nsXMLContentBuilder* result = new nsXMLContentBuilder();
105 if (! result)
106 return NS_ERROR_OUT_OF_MEMORY;
108 NS_ADDREF(result);
109 *aResult = result;
110 return NS_OK;
113 //----------------------------------------------------------------------
114 // nsISupports methods
116 NS_IMPL_ISUPPORTS1(nsXMLContentBuilder, nsIXMLContentBuilder)
118 //----------------------------------------------------------------------
119 // nsIXMLContentBuilder implementation
121 /* void clear (in nsIDOMElement root); */
122 NS_IMETHODIMP nsXMLContentBuilder::Clear(nsIDOMElement *root)
124 mCurrent = do_QueryInterface(root);
125 mTop = do_QueryInterface(root);
126 if (mNamespaceId != kNameSpaceID_None) {
127 mNamespaceId = kNameSpaceID_None;
129 return NS_OK;
132 /* void setDocument (in nsIDOMDocument doc); */
133 NS_IMETHODIMP nsXMLContentBuilder::SetDocument(nsIDOMDocument *doc)
135 mDocument = do_QueryInterface(doc);
136 #ifdef DEBUG
137 if (!mDocument)
138 NS_WARNING("null document in nsXMLContentBuilder::SetDocument");
139 #endif
140 return NS_OK;
143 /* void setElementNamespace (in AString ns); */
144 NS_IMETHODIMP nsXMLContentBuilder::SetElementNamespace(const nsAString & ns)
146 nsContentUtils::NameSpaceManager()->RegisterNameSpace(ns, mNamespaceId);
147 return NS_OK;
150 /* void beginElement (in AString tagname); */
151 NS_IMETHODIMP nsXMLContentBuilder::BeginElement(const nsAString & tagname)
153 nsCOMPtr<nsIContent> node;
155 EnsureDoc();
156 nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(tagname);
157 mDocument->CreateElem(nameAtom, nsnull, mNamespaceId, PR_FALSE, getter_AddRefs(node));
159 if (!node) {
160 NS_ERROR("could not create node");
161 return NS_ERROR_FAILURE;
164 // ok, we created a content element. now either append it to our
165 // top-level array or to the current element.
166 if (!mCurrent) {
167 if (mTop) {
168 NS_ERROR("Building of multi-rooted trees not supported");
169 return NS_ERROR_FAILURE;
171 mTop = node;
172 mCurrent = mTop;
174 else {
175 mCurrent->AppendChildTo(node, PR_TRUE);
176 mCurrent = node;
179 return NS_OK;
182 /* void endElement (); */
183 NS_IMETHODIMP nsXMLContentBuilder::EndElement()
185 NS_ASSERTION(mCurrent, "unbalanced begin/endelement");
186 mCurrent = mCurrent->GetParent();
187 return NS_OK;
190 /* void attrib (in AString name, in AString value); */
191 NS_IMETHODIMP nsXMLContentBuilder::Attrib(const nsAString & name, const nsAString & value)
193 NS_ASSERTION(mCurrent, "can't set attrib w/o open element");
194 nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(name);
195 mCurrent->SetAttr(0, nameAtom, value, PR_TRUE);
196 return NS_OK;
199 /* void textNode (in AString text); */
200 NS_IMETHODIMP nsXMLContentBuilder::TextNode(const nsAString & text)
202 EnsureDoc();
203 NS_ASSERTION(mCurrent, "can't append textnode w/o open element");
204 nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(mDocument);
205 NS_ASSERTION(domDoc, "no dom document");
207 nsCOMPtr<nsIDOMText> textNode;
208 domDoc->CreateTextNode(text, getter_AddRefs(textNode));
209 NS_ASSERTION(textNode, "Failed to create text node");
210 nsCOMPtr<nsIContent> textContent = do_QueryInterface(textNode);
211 mCurrent->AppendChildTo(textContent, PR_TRUE);
212 return NS_OK;
215 /* readonly attribute nsIDOMElement root; */
216 NS_IMETHODIMP nsXMLContentBuilder::GetRoot(nsIDOMElement * *aRoot)
218 if (!mTop) {
219 *aRoot = nsnull;
220 return NS_OK;
222 return CallQueryInterface(mTop, aRoot);
225 /* readonly attribute nsIDOMElement current; */
226 NS_IMETHODIMP nsXMLContentBuilder::GetCurrent(nsIDOMElement * *aCurrent)
228 if (!mCurrent) {
229 *aCurrent = nsnull;
230 return NS_OK;
232 return CallQueryInterface(mCurrent, aCurrent);
235 //----------------------------------------------------------------------
236 // implementation helpers
238 void nsXMLContentBuilder::EnsureDoc()
240 if (!mDocument) {
241 mDocument = do_CreateInstance(kXMLDocumentCID);
242 // XXX should probably do some doc initialization here, such as
243 // setting the base url
245 NS_ASSERTION(mDocument, "null doc");