1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2001
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Peter Van der Beken <peterv@propagandism.org>
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 "txMozillaTextOutput.h"
40 : #include "nsContentCID.h"
41 : #include "nsIContent.h"
42 : #include "nsIDocument.h"
43 : #include "nsIDOMDocument.h"
44 : #include "nsIDOMDocumentFragment.h"
45 : #include "nsIDocumentTransformer.h"
46 : #include "nsNetUtil.h"
47 : #include "nsIParser.h"
48 : #include "nsCharsetAlias.h"
49 : #include "nsIPrincipal.h"
50 : #include "txURIUtils.h"
51 : #include "nsContentCreatorFunctions.h"
52 : #include "nsContentUtils.h"
53 : #include "nsGkAtoms.h"
54 :
55 : using namespace mozilla::dom;
56 :
57 0 : txMozillaTextOutput::txMozillaTextOutput(nsITransformObserver* aObserver)
58 : {
59 0 : MOZ_COUNT_CTOR(txMozillaTextOutput);
60 0 : mObserver = do_GetWeakReference(aObserver);
61 0 : }
62 :
63 0 : txMozillaTextOutput::txMozillaTextOutput(nsIDOMDocumentFragment* aDest)
64 : {
65 0 : MOZ_COUNT_CTOR(txMozillaTextOutput);
66 0 : mTextParent = do_QueryInterface(aDest);
67 0 : mDocument = mTextParent->OwnerDoc();
68 0 : }
69 :
70 0 : txMozillaTextOutput::~txMozillaTextOutput()
71 : {
72 0 : MOZ_COUNT_DTOR(txMozillaTextOutput);
73 0 : }
74 :
75 : nsresult
76 0 : txMozillaTextOutput::attribute(nsIAtom* aPrefix, nsIAtom* aLocalName,
77 : nsIAtom* aLowercaseLocalName,
78 : PRInt32 aNsID, const nsString& aValue)
79 : {
80 0 : return NS_OK;
81 : }
82 :
83 : nsresult
84 0 : txMozillaTextOutput::attribute(nsIAtom* aPrefix, const nsSubstring& aName,
85 : const PRInt32 aNsID,
86 : const nsString& aValue)
87 : {
88 0 : return NS_OK;
89 : }
90 :
91 : nsresult
92 0 : txMozillaTextOutput::characters(const nsSubstring& aData, bool aDOE)
93 : {
94 0 : mText.Append(aData);
95 :
96 0 : return NS_OK;
97 : }
98 :
99 : nsresult
100 0 : txMozillaTextOutput::comment(const nsString& aData)
101 : {
102 0 : return NS_OK;
103 : }
104 :
105 : nsresult
106 0 : txMozillaTextOutput::endDocument(nsresult aResult)
107 : {
108 0 : NS_ENSURE_TRUE(mDocument && mTextParent, NS_ERROR_FAILURE);
109 :
110 0 : nsCOMPtr<nsIContent> text;
111 0 : nsresult rv = NS_NewTextNode(getter_AddRefs(text),
112 0 : mDocument->NodeInfoManager());
113 0 : NS_ENSURE_SUCCESS(rv, rv);
114 :
115 0 : text->SetText(mText, false);
116 0 : rv = mTextParent->AppendChildTo(text, true);
117 0 : NS_ENSURE_SUCCESS(rv, rv);
118 :
119 0 : if (NS_SUCCEEDED(aResult)) {
120 0 : nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
121 0 : if (observer) {
122 0 : observer->OnTransformDone(aResult, mDocument);
123 : }
124 : }
125 :
126 0 : return NS_OK;
127 : }
128 :
129 : nsresult
130 0 : txMozillaTextOutput::endElement()
131 : {
132 0 : return NS_OK;
133 : }
134 :
135 : nsresult
136 0 : txMozillaTextOutput::processingInstruction(const nsString& aTarget,
137 : const nsString& aData)
138 : {
139 0 : return NS_OK;
140 : }
141 :
142 : nsresult
143 0 : txMozillaTextOutput::startDocument()
144 : {
145 0 : return NS_OK;
146 : }
147 :
148 : nsresult
149 0 : txMozillaTextOutput::createResultDocument(nsIDOMDocument* aSourceDocument)
150 : {
151 : /*
152 : * Create an XHTML document to hold the text.
153 : *
154 : * <html>
155 : * <head />
156 : * <body>
157 : * <pre id="transformiixResult"> * The text comes here * </pre>
158 : * <body>
159 : * </html>
160 : *
161 : * Except if we are transforming into a non-displayed document we create
162 : * the following DOM
163 : *
164 : * <transformiix:result> * The text comes here * </transformiix:result>
165 : */
166 :
167 : // Create the document
168 0 : nsresult rv = NS_NewXMLDocument(getter_AddRefs(mDocument));
169 0 : NS_ENSURE_SUCCESS(rv, rv);
170 0 : nsCOMPtr<nsIDocument> source = do_QueryInterface(aSourceDocument);
171 0 : NS_ENSURE_STATE(source);
172 0 : bool hasHadScriptObject = false;
173 : nsIScriptGlobalObject* sgo =
174 0 : source->GetScriptHandlingObject(hasHadScriptObject);
175 0 : NS_ENSURE_STATE(sgo || !hasHadScriptObject);
176 0 : mDocument->SetScriptHandlingObject(sgo);
177 :
178 0 : NS_ASSERTION(mDocument, "Need document");
179 :
180 : // Reset and set up document
181 0 : URIUtils::ResetWithSource(mDocument, aSourceDocument);
182 :
183 : // Set the charset
184 0 : if (!mOutputFormat.mEncoding.IsEmpty()) {
185 0 : NS_LossyConvertUTF16toASCII charset(mOutputFormat.mEncoding);
186 0 : nsCAutoString canonicalCharset;
187 :
188 0 : if (NS_SUCCEEDED(nsCharsetAlias::GetPreferred(charset,
189 : canonicalCharset))) {
190 0 : mDocument->SetDocumentCharacterSetSource(kCharsetFromOtherComponent);
191 0 : mDocument->SetDocumentCharacterSet(canonicalCharset);
192 : }
193 : }
194 :
195 : // Notify the contentsink that the document is created
196 0 : nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
197 0 : if (observer) {
198 0 : rv = observer->OnDocumentCreated(mDocument);
199 0 : NS_ENSURE_SUCCESS(rv, rv);
200 : }
201 :
202 : // Create the content
203 :
204 : // When transforming into a non-displayed document (i.e. when there is no
205 : // observer) we only create a transformiix:result root element.
206 0 : if (!observer) {
207 : PRInt32 namespaceID;
208 0 : rv = nsContentUtils::NameSpaceManager()->
209 0 : RegisterNameSpace(NS_LITERAL_STRING(kTXNameSpaceURI), namespaceID);
210 0 : NS_ENSURE_SUCCESS(rv, rv);
211 :
212 0 : rv = mDocument->CreateElem(nsDependentAtomString(nsGkAtoms::result),
213 : nsGkAtoms::transformiix, namespaceID,
214 0 : getter_AddRefs(mTextParent));
215 0 : NS_ENSURE_SUCCESS(rv, rv);
216 :
217 :
218 0 : rv = mDocument->AppendChildTo(mTextParent, true);
219 0 : NS_ENSURE_SUCCESS(rv, rv);
220 : }
221 : else {
222 0 : nsCOMPtr<nsIContent> html, head, body;
223 0 : rv = createXHTMLElement(nsGkAtoms::html, getter_AddRefs(html));
224 0 : NS_ENSURE_SUCCESS(rv, rv);
225 :
226 0 : rv = createXHTMLElement(nsGkAtoms::head, getter_AddRefs(head));
227 0 : NS_ENSURE_SUCCESS(rv, rv);
228 :
229 0 : rv = html->AppendChildTo(head, false);
230 0 : NS_ENSURE_SUCCESS(rv, rv);
231 :
232 0 : rv = createXHTMLElement(nsGkAtoms::body, getter_AddRefs(body));
233 0 : NS_ENSURE_SUCCESS(rv, rv);
234 :
235 0 : rv = html->AppendChildTo(body, false);
236 0 : NS_ENSURE_SUCCESS(rv, rv);
237 :
238 0 : rv = createXHTMLElement(nsGkAtoms::pre, getter_AddRefs(mTextParent));
239 0 : NS_ENSURE_SUCCESS(rv, rv);
240 :
241 : rv = mTextParent->SetAttr(kNameSpaceID_None, nsGkAtoms::id,
242 0 : NS_LITERAL_STRING("transformiixResult"),
243 0 : false);
244 0 : NS_ENSURE_SUCCESS(rv, rv);
245 :
246 0 : rv = body->AppendChildTo(mTextParent, false);
247 0 : NS_ENSURE_SUCCESS(rv, rv);
248 :
249 0 : rv = mDocument->AppendChildTo(html, true);
250 0 : NS_ENSURE_SUCCESS(rv, rv);
251 : }
252 :
253 0 : return NS_OK;
254 : }
255 :
256 : nsresult
257 0 : txMozillaTextOutput::startElement(nsIAtom* aPrefix, nsIAtom* aLocalName,
258 : nsIAtom* aLowercaseLocalName, PRInt32 aNsID)
259 : {
260 0 : return NS_OK;
261 : }
262 :
263 : nsresult
264 0 : txMozillaTextOutput::startElement(nsIAtom* aPrefix, const nsSubstring& aName,
265 : const PRInt32 aNsID)
266 : {
267 0 : return NS_OK;
268 : }
269 :
270 0 : void txMozillaTextOutput::getOutputDocument(nsIDOMDocument** aDocument)
271 : {
272 0 : CallQueryInterface(mDocument, aDocument);
273 0 : }
274 :
275 : nsresult
276 0 : txMozillaTextOutput::createXHTMLElement(nsIAtom* aName,
277 : nsIContent** aResult)
278 : {
279 0 : *aResult = nsnull;
280 :
281 0 : nsCOMPtr<nsINodeInfo> ni;
282 : ni = mDocument->NodeInfoManager()->
283 : GetNodeInfo(aName, nsnull, kNameSpaceID_XHTML,
284 0 : nsIDOMNode::ELEMENT_NODE);
285 0 : NS_ENSURE_TRUE(ni, NS_ERROR_OUT_OF_MEMORY);
286 :
287 0 : return NS_NewHTMLElement(aResult, ni.forget(), NOT_FROM_PARSER);
288 : }
|