~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/transformiix/source/xslt/txMozillaTextOutput.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 the TransforMiiX XSLT processor.
 
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@netscape.com>
 
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 "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"
 
52
 
 
53
static NS_DEFINE_CID(kXMLDocumentCID, NS_XMLDOCUMENT_CID);
 
54
 
 
55
txMozillaTextOutput::txMozillaTextOutput(nsIDOMDocument* aSourceDocument,
 
56
                                         nsIDOMDocument* aResultDocument,
 
57
                                         nsITransformObserver* aObserver)
 
58
{
 
59
    mObserver = do_GetWeakReference(aObserver);
 
60
    createResultDocument(aSourceDocument, aResultDocument);
 
61
}
 
62
 
 
63
txMozillaTextOutput::txMozillaTextOutput(nsIDOMDocumentFragment* aDest)
 
64
{
 
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));
 
71
    if (NS_FAILED(rv)) {
 
72
        return;
 
73
    }
 
74
    nsCOMPtr<nsIDOMNode> dummy;
 
75
    rv = aDest->AppendChild(textNode, getter_AddRefs(dummy));
 
76
    if (NS_FAILED(rv)) {
 
77
        return;
 
78
    }
 
79
 
 
80
    mTextNode = textNode;
 
81
    return;
 
82
}
 
83
 
 
84
txMozillaTextOutput::~txMozillaTextOutput()
 
85
{
 
86
}
 
87
 
 
88
void txMozillaTextOutput::attribute(const nsAString& aName,
 
89
                                    const PRInt32 aNsID,
 
90
                                    const nsAString& aValue)
 
91
{
 
92
}
 
93
 
 
94
void txMozillaTextOutput::characters(const nsAString& aData, PRBool aDOE)
 
95
{
 
96
    if (mTextNode)
 
97
        mTextNode->AppendData(aData);
 
98
}
 
99
 
 
100
void txMozillaTextOutput::comment(const nsAString& aData)
 
101
{
 
102
}
 
103
 
 
104
void txMozillaTextOutput::endDocument()
 
105
{
 
106
    nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
 
107
    if (observer) {
 
108
        observer->OnTransformDone(NS_OK, mDocument);
 
109
    }
 
110
}
 
111
 
 
112
void txMozillaTextOutput::endElement(const nsAString& aName,
 
113
                                     const PRInt32 aNsID)
 
114
{
 
115
}
 
116
 
 
117
void txMozillaTextOutput::processingInstruction(const nsAString& aTarget,
 
118
                                                const nsAString& aData)
 
119
{
 
120
}
 
121
 
 
122
void txMozillaTextOutput::startDocument()
 
123
{
 
124
}
 
125
 
 
126
void txMozillaTextOutput::createResultDocument(nsIDOMDocument* aSourceDocument,
 
127
                                               nsIDOMDocument* aResultDocument)
 
128
{
 
129
    nsresult rv = NS_OK;
 
130
    
 
131
    /*
 
132
     * Create an XHTML document to hold the text.
 
133
     *
 
134
     * <html>
 
135
     *   <head />
 
136
     *   <body>
 
137
     *     <pre> * The text comes here * </pre>
 
138
     *   <body>
 
139
     * </html>
 
140
     *
 
141
     * Except if we are transforming into a non-displayed document we create
 
142
     * the following DOM
 
143
     *
 
144
     * <transformiix:result> * The text comes here * </transformiix:result>
 
145
     */
 
146
     
 
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);
 
153
    }
 
154
    else {
 
155
        mDocument = aResultDocument;
 
156
        doc = do_QueryInterface(aResultDocument);
 
157
        NS_ASSERTION(doc, "Couldn't QI to nsIDocument");
 
158
    }
 
159
 
 
160
    if (!doc) {
 
161
        return;
 
162
    }
 
163
 
 
164
    NS_ASSERTION(mDocument, "Need document");
 
165
 
 
166
    nsCOMPtr<nsIDOMNSDocument> nsDoc = do_QueryInterface(mDocument);
 
167
    if (nsDoc) {
 
168
        nsDoc->SetTitle(nsString());
 
169
    }
 
170
 
 
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);
 
176
    if (serv) {
 
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));
 
182
    }
 
183
    doc->Reset(channel, loadGroup);
 
184
    doc->SetBaseURI(sourceDoc->GetBaseURI());
 
185
 
 
186
    // Set the charset
 
187
    if (!mOutputFormat.mEncoding.IsEmpty()) {
 
188
        doc->SetDocumentCharacterSet(
 
189
            NS_LossyConvertUTF16toASCII(mOutputFormat.mEncoding));
 
190
        doc->SetDocumentCharacterSetSource(kCharsetFromOtherComponent);
 
191
    }
 
192
    else {
 
193
        doc->SetDocumentCharacterSet(sourceDoc->GetDocumentCharacterSet());
 
194
        doc->SetDocumentCharacterSetSource(
 
195
            sourceDoc->GetDocumentCharacterSetSource());
 
196
    }
 
197
 
 
198
    // Notify the contentsink that the document is created
 
199
    nsCOMPtr<nsITransformObserver> observer = do_QueryReferent(mObserver);
 
200
    if (observer) {
 
201
        observer->OnDocumentCreated(mDocument);
 
202
    }
 
203
 
 
204
    // Create the content
 
205
 
 
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");
 
217
        if (!docElement) {
 
218
            return;
 
219
        }
 
220
 
 
221
        rv = mDocument->AppendChild(docElement, getter_AddRefs(textContainer));
 
222
        NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the wrapper element");
 
223
        if (NS_FAILED(rv)) {
 
224
            return;
 
225
        }
 
226
    }
 
227
    else {
 
228
        nsCOMPtr<nsIDOMElement> element, docElement;
 
229
        nsCOMPtr<nsIDOMNode> parent, pre;
 
230
 
 
231
        NS_NAMED_LITERAL_STRING(XHTML_NSURI, "http://www.w3.org/1999/xhtml");
 
232
 
 
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");
 
238
        if (!rootContent) {
 
239
            return;
 
240
        }
 
241
 
 
242
        rootContent->SetDocument(doc, PR_FALSE, PR_TRUE);
 
243
 
 
244
        doc->SetRootContent(rootContent);
 
245
 
 
246
        mDocument->CreateElementNS(XHTML_NSURI,
 
247
                                   NS_LITERAL_STRING("head"),
 
248
                                   getter_AddRefs(element));
 
249
        NS_ASSERTION(element, "Failed to create head element");
 
250
        if (!element) {
 
251
            return;
 
252
        }
 
253
 
 
254
        rv = docElement->AppendChild(element, getter_AddRefs(parent));
 
255
        NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the head element");
 
256
        if (NS_FAILED(rv)) {
 
257
            return;
 
258
        }
 
259
 
 
260
        mDocument->CreateElementNS(XHTML_NSURI,
 
261
                                   NS_LITERAL_STRING("body"),
 
262
                                   getter_AddRefs(element));
 
263
        NS_ASSERTION(element, "Failed to create body element");
 
264
        if (!element) {
 
265
            return;
 
266
        }
 
267
 
 
268
        rv = docElement->AppendChild(element, getter_AddRefs(parent));
 
269
        NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the body element");
 
270
        if (NS_FAILED(rv)) {
 
271
            return;
 
272
        }
 
273
 
 
274
        mDocument->CreateElementNS(XHTML_NSURI,
 
275
                                   NS_LITERAL_STRING("pre"),
 
276
                                   getter_AddRefs(element));
 
277
        NS_ASSERTION(element, "Failed to create pre element");
 
278
        if (!element) {
 
279
            return;
 
280
        }
 
281
 
 
282
        rv = parent->AppendChild(element, getter_AddRefs(pre));
 
283
        NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the pre element");
 
284
        if (NS_FAILED(rv)) {
 
285
            return;
 
286
        }
 
287
 
 
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");
 
291
        
 
292
        textContainer = pre;
 
293
    }
 
294
 
 
295
    nsCOMPtr<nsIDOMText> textNode;
 
296
    mDocument->CreateTextNode(nsString(),
 
297
                              getter_AddRefs(textNode));
 
298
    NS_ASSERTION(textNode, "Failed to create the text node");
 
299
    if (!textNode) {
 
300
        return;
 
301
    }
 
302
 
 
303
    nsCOMPtr<nsIDOMNode> dummy;
 
304
    rv = textContainer->AppendChild(textNode, getter_AddRefs(dummy));
 
305
    NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to append the text node");
 
306
    if (NS_FAILED(rv)) {
 
307
        return;
 
308
    }
 
309
 
 
310
    mTextNode = textNode;
 
311
}
 
312
 
 
313
void txMozillaTextOutput::startElement(const nsAString& aName,
 
314
                                       const PRInt32 aNsID)
 
315
{
 
316
}
 
317
 
 
318
void txMozillaTextOutput::getOutputDocument(nsIDOMDocument** aDocument)
 
319
{
 
320
    *aDocument = mDocument;
 
321
    NS_IF_ADDREF(*aDocument);
 
322
}