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
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 TransforMiiX XSLT processor.
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.
23
* Peter Van der Beken <peterv@netscape.com>
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 "txMozillaTextOutput.h"
40
#include "nsContentCID.h"
41
#include "nsIContent.h"
42
#include "nsIDocument.h"
43
#include "nsIDOMDocument.h"
44
#include "nsIDOMDocumentFragment.h"
45
#include "nsIDOMElement.h"
46
#include "nsIDOMHTMLElement.h"
47
#include "nsIDOMText.h"
48
#include "nsIDocumentTransformer.h"
49
#include "nsNetUtil.h"
50
#include "nsIDOMNSDocument.h"
51
#include "nsIParser.h"
53
static NS_DEFINE_CID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
55
txMozillaTextOutput::txMozillaTextOutput(nsIDOMDocument* aSourceDocument,
56
nsIDOMDocument* aResultDocument,
57
nsITransformObserver* aObserver)
59
mObserver = do_GetWeakReference(aObserver);
60
createResultDocument(aSourceDocument, aResultDocument);
63
txMozillaTextOutput::txMozillaTextOutput(nsIDOMDocumentFragment* aDest)
65
nsCOMPtr<nsIDOMDocument> doc;
66
aDest->GetOwnerDocument(getter_AddRefs(doc));
67
NS_ASSERTION(doc, "unable to get ownerdocument");
68
nsCOMPtr<nsIDOMText> textNode;
69
nsresult rv = doc->CreateTextNode(nsString(),
70
getter_AddRefs(textNode));
74
nsCOMPtr<nsIDOMNode> dummy;
75
rv = aDest->AppendChild(textNode, getter_AddRefs(dummy));
84
txMozillaTextOutput::~txMozillaTextOutput()
88
void txMozillaTextOutput::attribute(const nsAString& aName,
90
const nsAString& aValue)
94
void txMozillaTextOutput::characters(const nsAString& aData, PRBool aDOE)
97
mTextNode->AppendData(aData);
100
void txMozillaTextOutput::comment(const nsAString& aData)
104
void txMozillaTextOutput::endDocument()
106
nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
108
observer->OnTransformDone(NS_OK, mDocument);
112
void txMozillaTextOutput::endElement(const nsAString& aName,
117
void txMozillaTextOutput::processingInstruction(const nsAString& aTarget,
118
const nsAString& aData)
122
void txMozillaTextOutput::startDocument()
126
void txMozillaTextOutput::createResultDocument(nsIDOMDocument* aSourceDocument,
127
nsIDOMDocument* aResultDocument)
132
* Create an XHTML document to hold the text.
137
* <pre> * The text comes here * </pre>
141
* Except if we are transforming into a non-displayed document we create
144
* <transformiix:result> * The text comes here * </transformiix:result>
147
nsCOMPtr<nsIDocument> doc;
148
if (!aResultDocument) {
149
// Create the document
150
doc = do_CreateInstance(kXMLDocumentCID, &rv);
151
NS_ASSERTION(NS_SUCCEEDED(rv), "Couldn't create document");
152
mDocument = do_QueryInterface(doc);
155
mDocument = aResultDocument;
156
doc = do_QueryInterface(aResultDocument);
157
NS_ASSERTION(doc, "Couldn't QI to nsIDocument");
164
NS_ASSERTION(mDocument, "Need document");
166
nsCOMPtr<nsIDOMNSDocument> nsDoc = do_QueryInterface(mDocument);
168
nsDoc->SetTitle(nsString());
171
// Reset and set up document
172
nsCOMPtr<nsIChannel> channel;
173
nsCOMPtr<nsIDocument> sourceDoc = do_QueryInterface(aSourceDocument);
174
nsCOMPtr<nsILoadGroup> loadGroup = sourceDoc->GetDocumentLoadGroup();
175
nsCOMPtr<nsIIOService> serv = do_GetService(NS_IOSERVICE_CONTRACTID);
177
// Create a temporary channel to get nsIDocument->Reset to
178
// do the right thing. We want the output document to get
179
// much of the input document's characteristics.
180
serv->NewChannelFromURI(sourceDoc->GetDocumentURI(),
181
getter_AddRefs(channel));
183
doc->Reset(channel, loadGroup);
184
doc->SetBaseURI(sourceDoc->GetBaseURI());
187
if (!mOutputFormat.mEncoding.IsEmpty()) {
188
doc->SetDocumentCharacterSet(
189
NS_LossyConvertUTF16toASCII(mOutputFormat.mEncoding));
190
doc->SetDocumentCharacterSetSource(kCharsetFromOtherComponent);
193
doc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());
194
doc->SetDocumentCharacterSetSource(
195
sourceDoc->GetDocumentCharacterSetSource());
198
// Notify the contentsink that the document is created
199
nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
201
observer->OnDocumentCreated(mDocument);
204
// Create the content
206
// When transforming into a non-displayed document (i.e. when there is no
207
// observer) we only create a transformiix:result root element.
208
// Don't do this when called through nsIXSLTProcessorObsolete (i.e. when
209
// aResultDocument is set) for compability reasons
210
nsCOMPtr<nsIDOMNode> textContainer;
211
if (!aResultDocument && !observer) {
212
nsCOMPtr<nsIDOMElement> docElement;
213
mDocument->CreateElementNS(NS_LITERAL_STRING(kTXNameSpaceURI),
214
NS_LITERAL_STRING(kTXWrapper),
215
getter_AddRefs(docElement));
216
NS_ASSERTION(docElement, "Failed to create wrapper element");
221
rv = mDocument->AppendChild(docElement, getter_AddRefs(textContainer));
222
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the wrapper element");
228
nsCOMPtr<nsIDOMElement> element, docElement;
229
nsCOMPtr<nsIDOMNode> parent, pre;
231
NS_NAMED_LITERAL_STRING(XHTML_NSURI, "http://www.w3.org/1999/xhtml");
233
mDocument->CreateElementNS(XHTML_NSURI,
234
NS_LITERAL_STRING("html"),
235
getter_AddRefs(docElement));
236
nsCOMPtr<nsIContent> rootContent = do_QueryInterface(docElement);
237
NS_ASSERTION(rootContent, "Need root element");
242
rootContent->SetDocument(doc, PR_FALSE, PR_TRUE);
244
doc->SetRootContent(rootContent);
246
mDocument->CreateElementNS(XHTML_NSURI,
247
NS_LITERAL_STRING("head"),
248
getter_AddRefs(element));
249
NS_ASSERTION(element, "Failed to create head element");
254
rv = docElement->AppendChild(element, getter_AddRefs(parent));
255
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the head element");
260
mDocument->CreateElementNS(XHTML_NSURI,
261
NS_LITERAL_STRING("body"),
262
getter_AddRefs(element));
263
NS_ASSERTION(element, "Failed to create body element");
268
rv = docElement->AppendChild(element, getter_AddRefs(parent));
269
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the body element");
274
mDocument->CreateElementNS(XHTML_NSURI,
275
NS_LITERAL_STRING("pre"),
276
getter_AddRefs(element));
277
NS_ASSERTION(element, "Failed to create pre element");
282
rv = parent->AppendChild(element, getter_AddRefs(pre));
283
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the pre element");
288
nsCOMPtr<nsIDOMHTMLElement> htmlElement = do_QueryInterface(pre);
289
htmlElement->SetId(NS_LITERAL_STRING("transformiixResult"));
290
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the id");
295
nsCOMPtr<nsIDOMText> textNode;
296
mDocument->CreateTextNode(nsString(),
297
getter_AddRefs(textNode));
298
NS_ASSERTION(textNode, "Failed to create the text node");
303
nsCOMPtr<nsIDOMNode> dummy;
304
rv = textContainer->AppendChild(textNode, getter_AddRefs(dummy));
305
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the text node");
310
mTextNode = textNode;
313
void txMozillaTextOutput::startElement(const nsAString& aName,
318
void txMozillaTextOutput::getOutputDocument(nsIDOMDocument** aDocument)
320
*aDocument = mDocument;
321
NS_IF_ADDREF(*aDocument);