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
4 : *
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/
9 : *
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.
14 : *
15 : * The Original Code is the Mozilla XTF project.
16 : *
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.
21 : *
22 : * Contributor(s):
23 : * Alex Fritze <alex@croczilla.com> (original author)
24 : *
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.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
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"
56 :
57 : static NS_DEFINE_CID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
58 :
59 : class nsXMLContentBuilder : public nsIXMLContentBuilder
60 : {
61 : protected:
62 : friend nsresult NS_NewXMLContentBuilder(nsIXMLContentBuilder** aResult);
63 :
64 : nsXMLContentBuilder();
65 : ~nsXMLContentBuilder();
66 :
67 : public:
68 : // nsISupports interface
69 : NS_DECL_ISUPPORTS
70 :
71 : // nsIXMLContentBuilder interface
72 : NS_DECL_NSIXMLCONTENTBUILDER
73 :
74 : private:
75 : void EnsureDoc();
76 :
77 : nsCOMPtr<nsIContent> mTop;
78 : nsCOMPtr<nsIContent> mCurrent;
79 : nsCOMPtr<nsIDocument> mDocument;
80 : PRInt32 mNamespaceId;
81 : };
82 :
83 : //----------------------------------------------------------------------
84 : // implementation:
85 :
86 0 : nsXMLContentBuilder::nsXMLContentBuilder()
87 0 : : mNamespaceId(kNameSpaceID_None)
88 : {
89 : #ifdef DEBUG
90 : // printf("nsXMLContentBuilder CTOR\n");
91 : #endif
92 0 : }
93 :
94 0 : nsXMLContentBuilder::~nsXMLContentBuilder()
95 : {
96 : #ifdef DEBUG
97 : // printf("~nsXMLContentBuilder\n");
98 : #endif
99 0 : }
100 :
101 : nsresult
102 0 : NS_NewXMLContentBuilder(nsIXMLContentBuilder** aResult)
103 : {
104 0 : nsXMLContentBuilder* result = new nsXMLContentBuilder();
105 0 : if (! result)
106 0 : return NS_ERROR_OUT_OF_MEMORY;
107 :
108 0 : NS_ADDREF(result);
109 0 : *aResult = result;
110 0 : return NS_OK;
111 : }
112 :
113 : //----------------------------------------------------------------------
114 : // nsISupports methods
115 :
116 0 : NS_IMPL_ISUPPORTS1(nsXMLContentBuilder, nsIXMLContentBuilder)
117 :
118 : //----------------------------------------------------------------------
119 : // nsIXMLContentBuilder implementation
120 :
121 : /* void clear (in nsIDOMElement root); */
122 0 : NS_IMETHODIMP nsXMLContentBuilder::Clear(nsIDOMElement *root)
123 : {
124 0 : mCurrent = do_QueryInterface(root);
125 0 : mTop = do_QueryInterface(root);
126 0 : if (mNamespaceId != kNameSpaceID_None) {
127 0 : mNamespaceId = kNameSpaceID_None;
128 : }
129 0 : return NS_OK;
130 : }
131 :
132 : /* void setDocument (in nsIDOMDocument doc); */
133 0 : NS_IMETHODIMP nsXMLContentBuilder::SetDocument(nsIDOMDocument *doc)
134 : {
135 0 : mDocument = do_QueryInterface(doc);
136 : #ifdef DEBUG
137 0 : if (!mDocument)
138 0 : NS_WARNING("null document in nsXMLContentBuilder::SetDocument");
139 : #endif
140 0 : return NS_OK;
141 : }
142 :
143 : /* void setElementNamespace (in AString ns); */
144 0 : NS_IMETHODIMP nsXMLContentBuilder::SetElementNamespace(const nsAString & ns)
145 : {
146 0 : nsContentUtils::NameSpaceManager()->RegisterNameSpace(ns, mNamespaceId);
147 0 : return NS_OK;
148 : }
149 :
150 : /* void beginElement (in AString tagname); */
151 0 : NS_IMETHODIMP nsXMLContentBuilder::BeginElement(const nsAString & tagname)
152 : {
153 0 : nsCOMPtr<nsIContent> node;
154 : {
155 0 : EnsureDoc();
156 0 : mDocument->CreateElem(tagname, nsnull, mNamespaceId, getter_AddRefs(node));
157 : }
158 0 : if (!node) {
159 0 : NS_ERROR("could not create node");
160 0 : return NS_ERROR_FAILURE;
161 : }
162 :
163 : // ok, we created a content element. now either append it to our
164 : // top-level array or to the current element.
165 0 : if (!mCurrent) {
166 0 : if (mTop) {
167 0 : NS_ERROR("Building of multi-rooted trees not supported");
168 0 : return NS_ERROR_FAILURE;
169 : }
170 0 : mTop = node;
171 0 : mCurrent = mTop;
172 : }
173 : else {
174 0 : mCurrent->AppendChildTo(node, true);
175 0 : mCurrent = node;
176 : }
177 :
178 0 : return NS_OK;
179 : }
180 :
181 : /* void endElement (); */
182 0 : NS_IMETHODIMP nsXMLContentBuilder::EndElement()
183 : {
184 0 : NS_ASSERTION(mCurrent, "unbalanced begin/endelement");
185 0 : mCurrent = mCurrent->GetParent();
186 0 : return NS_OK;
187 : }
188 :
189 : /* void attrib (in AString name, in AString value); */
190 0 : NS_IMETHODIMP nsXMLContentBuilder::Attrib(const nsAString & name, const nsAString & value)
191 : {
192 0 : NS_ASSERTION(mCurrent, "can't set attrib w/o open element");
193 0 : nsCOMPtr<nsIAtom> nameAtom = do_GetAtom(name);
194 0 : mCurrent->SetAttr(0, nameAtom, value, true);
195 0 : return NS_OK;
196 : }
197 :
198 : /* void textNode (in AString text); */
199 0 : NS_IMETHODIMP nsXMLContentBuilder::TextNode(const nsAString & text)
200 : {
201 0 : EnsureDoc();
202 0 : NS_ASSERTION(mCurrent, "can't append textnode w/o open element");
203 0 : nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(mDocument);
204 0 : NS_ASSERTION(domDoc, "no dom document");
205 :
206 0 : nsCOMPtr<nsIDOMText> textNode;
207 0 : domDoc->CreateTextNode(text, getter_AddRefs(textNode));
208 0 : NS_ASSERTION(textNode, "Failed to create text node");
209 0 : nsCOMPtr<nsIContent> textContent = do_QueryInterface(textNode);
210 0 : mCurrent->AppendChildTo(textContent, true);
211 0 : return NS_OK;
212 : }
213 :
214 : /* readonly attribute nsIDOMElement root; */
215 0 : NS_IMETHODIMP nsXMLContentBuilder::GetRoot(nsIDOMElement * *aRoot)
216 : {
217 0 : if (!mTop) {
218 0 : *aRoot = nsnull;
219 0 : return NS_OK;
220 : }
221 0 : return CallQueryInterface(mTop, aRoot);
222 : }
223 :
224 : /* readonly attribute nsIDOMElement current; */
225 0 : NS_IMETHODIMP nsXMLContentBuilder::GetCurrent(nsIDOMElement * *aCurrent)
226 : {
227 0 : if (!mCurrent) {
228 0 : *aCurrent = nsnull;
229 0 : return NS_OK;
230 : }
231 0 : return CallQueryInterface(mCurrent, aCurrent);
232 : }
233 :
234 : //----------------------------------------------------------------------
235 : // implementation helpers
236 :
237 0 : void nsXMLContentBuilder::EnsureDoc()
238 : {
239 0 : if (!mDocument) {
240 0 : mDocument = do_CreateInstance(kXMLDocumentCID);
241 : // XXX should probably do some doc initialization here, such as
242 : // setting the base url
243 : }
244 0 : NS_ASSERTION(mDocument, "null doc");
245 0 : }
|