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 "txUnknownHandler.h"
40 : #include "txExecutionState.h"
41 : #include "txStringUtils.h"
42 : #include "txStylesheet.h"
43 : #include "nsGkAtoms.h"
44 :
45 0 : txUnknownHandler::txUnknownHandler(txExecutionState* aEs)
46 : : mEs(aEs),
47 0 : mFlushed(false)
48 : {
49 0 : MOZ_COUNT_CTOR_INHERITED(txUnknownHandler, txBufferingHandler);
50 0 : }
51 :
52 0 : txUnknownHandler::~txUnknownHandler()
53 : {
54 0 : MOZ_COUNT_DTOR_INHERITED(txUnknownHandler, txBufferingHandler);
55 0 : }
56 :
57 : nsresult
58 0 : txUnknownHandler::attribute(nsIAtom* aPrefix, nsIAtom* aLocalName,
59 : nsIAtom* aLowercaseLocalName, PRInt32 aNsID,
60 : const nsString& aValue)
61 : {
62 : return mFlushed ?
63 : mEs->mResultHandler->attribute(aPrefix, aLocalName,
64 0 : aLowercaseLocalName, aNsID, aValue) :
65 : txBufferingHandler::attribute(aPrefix, aLocalName,
66 0 : aLowercaseLocalName, aNsID, aValue);
67 : }
68 :
69 : nsresult
70 0 : txUnknownHandler::attribute(nsIAtom* aPrefix, const nsSubstring& aLocalName,
71 : const PRInt32 aNsID, const nsString& aValue)
72 : {
73 : return mFlushed ?
74 0 : mEs->mResultHandler->attribute(aPrefix, aLocalName, aNsID, aValue) :
75 0 : txBufferingHandler::attribute(aPrefix, aLocalName, aNsID, aValue);
76 : }
77 :
78 : nsresult
79 0 : txUnknownHandler::characters(const nsSubstring& aData, bool aDOE)
80 : {
81 : return mFlushed ?
82 0 : mEs->mResultHandler->characters(aData, aDOE) :
83 0 : txBufferingHandler::characters(aData, aDOE);
84 : }
85 :
86 : nsresult
87 0 : txUnknownHandler::comment(const nsString& aData)
88 : {
89 : return mFlushed ?
90 0 : mEs->mResultHandler->comment(aData) :
91 0 : txBufferingHandler::comment(aData);
92 : }
93 :
94 : nsresult
95 0 : txUnknownHandler::endDocument(nsresult aResult)
96 : {
97 0 : if (!mFlushed) {
98 0 : if (NS_FAILED(aResult)) {
99 0 : return NS_OK;
100 : }
101 :
102 : // This is an unusual case, no output method has been set and we
103 : // didn't create a document element. Switching to XML output mode
104 : // anyway.
105 :
106 : // Make sure that mEs->mResultHandler == this is true, otherwise we'll
107 : // leak mEs->mResultHandler in createHandlerAndFlush.
108 0 : NS_ASSERTION(mEs->mResultHandler == this,
109 : "We're leaking mEs->mResultHandler.");
110 :
111 0 : nsresult rv = createHandlerAndFlush(false, EmptyString(),
112 0 : kNameSpaceID_None);
113 0 : NS_ENSURE_SUCCESS(rv, rv);
114 : }
115 :
116 0 : return mEs->mResultHandler->endDocument(aResult);
117 : }
118 :
119 : nsresult
120 0 : txUnknownHandler::endElement()
121 : {
122 : return mFlushed ?
123 0 : mEs->mResultHandler->endElement() :
124 0 : txBufferingHandler::endElement();
125 : }
126 :
127 : nsresult
128 0 : txUnknownHandler::processingInstruction(const nsString& aTarget,
129 : const nsString& aData)
130 : {
131 : return mFlushed ?
132 0 : mEs->mResultHandler->processingInstruction(aTarget, aData) :
133 0 : txBufferingHandler::processingInstruction(aTarget, aData);
134 : }
135 :
136 : nsresult
137 0 : txUnknownHandler::startDocument()
138 : {
139 : return mFlushed ?
140 0 : mEs->mResultHandler->startDocument() :
141 0 : txBufferingHandler::startDocument();
142 : }
143 :
144 : nsresult
145 0 : txUnknownHandler::startElement(nsIAtom* aPrefix, nsIAtom* aLocalName,
146 : nsIAtom* aLowercaseLocalName, PRInt32 aNsID)
147 : {
148 0 : if (!mFlushed) {
149 : // Make sure that mEs->mResultHandler == this is true, otherwise we'll
150 : // leak mEs->mResultHandler in createHandlerAndFlush.
151 0 : NS_ASSERTION(mEs->mResultHandler == this,
152 : "We're leaking mEs->mResultHandler.");
153 :
154 0 : nsCOMPtr<nsIAtom> owner;
155 0 : if (!aLowercaseLocalName) {
156 0 : owner = TX_ToLowerCaseAtom(aLocalName);
157 0 : NS_ENSURE_TRUE(owner, NS_ERROR_OUT_OF_MEMORY);
158 :
159 0 : aLowercaseLocalName = owner;
160 : }
161 :
162 : bool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix &&
163 0 : aLowercaseLocalName == nsGkAtoms::html;
164 :
165 : // Use aLocalName and not aLowercaseLocalName in case the output
166 : // handler cares about case. For eHTMLOutput the handler will hardcode
167 : // to 'html' anyway.
168 : nsresult rv = createHandlerAndFlush(htmlRoot,
169 0 : nsDependentAtomString(aLocalName),
170 0 : aNsID);
171 0 : NS_ENSURE_SUCCESS(rv, rv);
172 : }
173 :
174 : return mEs->mResultHandler->startElement(aPrefix, aLocalName,
175 0 : aLowercaseLocalName, aNsID);
176 : }
177 :
178 : nsresult
179 0 : txUnknownHandler::startElement(nsIAtom* aPrefix, const nsSubstring& aLocalName,
180 : const PRInt32 aNsID)
181 : {
182 0 : if (!mFlushed) {
183 : // Make sure that mEs->mResultHandler == this is true, otherwise we'll
184 : // leak mEs->mResultHandler in createHandlerAndFlush.
185 0 : NS_ASSERTION(mEs->mResultHandler == this,
186 : "We're leaking mEs->mResultHandler.");
187 :
188 : bool htmlRoot = aNsID == kNameSpaceID_None && !aPrefix &&
189 0 : aLocalName.Equals(NS_LITERAL_STRING("html"),
190 0 : txCaseInsensitiveStringComparator());
191 0 : nsresult rv = createHandlerAndFlush(htmlRoot, aLocalName, aNsID);
192 0 : NS_ENSURE_SUCCESS(rv, rv);
193 : }
194 :
195 0 : return mEs->mResultHandler->startElement(aPrefix, aLocalName, aNsID);
196 : }
197 :
198 0 : nsresult txUnknownHandler::createHandlerAndFlush(bool aHTMLRoot,
199 : const nsSubstring& aName,
200 : const PRInt32 aNsID)
201 : {
202 0 : NS_ENSURE_TRUE(mBuffer, NS_ERROR_NOT_INITIALIZED);
203 :
204 0 : txOutputFormat format;
205 0 : format.merge(*(mEs->mStylesheet->getOutputFormat()));
206 0 : if (format.mMethod == eMethodNotSet) {
207 0 : format.mMethod = aHTMLRoot ? eHTMLOutput : eXMLOutput;
208 : }
209 :
210 0 : nsAutoPtr<txAXMLEventHandler> handler;
211 : nsresult rv = mEs->mOutputHandlerFactory->createHandlerWith(&format, aName,
212 : aNsID,
213 0 : getter_Transfers(handler));
214 0 : NS_ENSURE_SUCCESS(rv, rv);
215 :
216 0 : mEs->mOutputHandler = handler;
217 0 : mEs->mResultHandler = handler.forget();
218 : // Let the executionstate delete us. We need to stay alive because we might
219 : // need to forward hooks to mEs->mResultHandler if someone is currently
220 : // flushing a buffer to mEs->mResultHandler.
221 0 : mEs->mObsoleteHandler = this;
222 :
223 0 : mFlushed = true;
224 :
225 : // Let go of out buffer as soon as we're done flushing it, we're not going
226 : // to need it anymore from this point on (all hooks get forwarded to
227 : // mEs->mResultHandler.
228 0 : nsAutoPtr<txResultBuffer> buffer(mBuffer);
229 0 : return buffer->flushToHandler(mEs->mResultHandler);
230 : }
|