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
15 * The Original Code is the Mozilla XTF project.
17 * The Initial Developer of the Original Code is
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
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 ***** */
41 #include "nsIXMLContentBuilder.h"
42 #include "nsINameSpaceManager.h"
43 #include "nsINodeInfo.h"
44 #include "nsIContent.h"
45 #include "nsIComponentManager.h"
46 #include "nsIServiceManager.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
62 friend nsresult
NS_NewXMLContentBuilder(nsIXMLContentBuilder
** aResult
);
64 nsXMLContentBuilder();
65 ~nsXMLContentBuilder();
68 // nsISupports interface
71 // nsIXMLContentBuilder interface
72 NS_DECL_NSIXMLCONTENTBUILDER
77 nsCOMPtr
<nsIContent
> mTop
;
78 nsCOMPtr
<nsIContent
> mCurrent
;
79 nsCOMPtr
<nsIDocument
> mDocument
;
83 //----------------------------------------------------------------------
86 nsXMLContentBuilder::nsXMLContentBuilder()
87 : mNamespaceId(kNameSpaceID_None
)
90 // printf("nsXMLContentBuilder CTOR\n");
94 nsXMLContentBuilder::~nsXMLContentBuilder()
97 // printf("~nsXMLContentBuilder\n");
102 NS_NewXMLContentBuilder(nsIXMLContentBuilder
** aResult
)
104 nsXMLContentBuilder
* result
= new nsXMLContentBuilder();
106 return NS_ERROR_OUT_OF_MEMORY
;
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
;
132 /* void setDocument (in nsIDOMDocument doc); */
133 NS_IMETHODIMP
nsXMLContentBuilder::SetDocument(nsIDOMDocument
*doc
)
135 mDocument
= do_QueryInterface(doc
);
138 NS_WARNING("null document in nsXMLContentBuilder::SetDocument");
143 /* void setElementNamespace (in AString ns); */
144 NS_IMETHODIMP
nsXMLContentBuilder::SetElementNamespace(const nsAString
& ns
)
146 nsContentUtils::NameSpaceManager()->RegisterNameSpace(ns
, mNamespaceId
);
150 /* void beginElement (in AString tagname); */
151 NS_IMETHODIMP
nsXMLContentBuilder::BeginElement(const nsAString
& tagname
)
153 nsCOMPtr
<nsIContent
> node
;
156 nsCOMPtr
<nsIAtom
> nameAtom
= do_GetAtom(tagname
);
157 mDocument
->CreateElem(nameAtom
, nsnull
, mNamespaceId
, PR_FALSE
, getter_AddRefs(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.
168 NS_ERROR("Building of multi-rooted trees not supported");
169 return NS_ERROR_FAILURE
;
175 mCurrent
->AppendChildTo(node
, PR_TRUE
);
182 /* void endElement (); */
183 NS_IMETHODIMP
nsXMLContentBuilder::EndElement()
185 NS_ASSERTION(mCurrent
, "unbalanced begin/endelement");
186 mCurrent
= mCurrent
->GetParent();
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
);
199 /* void textNode (in AString text); */
200 NS_IMETHODIMP
nsXMLContentBuilder::TextNode(const nsAString
& text
)
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
);
215 /* readonly attribute nsIDOMElement root; */
216 NS_IMETHODIMP
nsXMLContentBuilder::GetRoot(nsIDOMElement
* *aRoot
)
222 return CallQueryInterface(mTop
, aRoot
);
225 /* readonly attribute nsIDOMElement current; */
226 NS_IMETHODIMP
nsXMLContentBuilder::GetCurrent(nsIDOMElement
* *aCurrent
)
232 return CallQueryInterface(mCurrent
, aCurrent
);
235 //----------------------------------------------------------------------
236 // implementation helpers
238 void nsXMLContentBuilder::EnsureDoc()
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");