Back to index

lightning-sunbird  0.9+nobinonly
txUnknownHandler.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Peter Van der Beken <peterv@propagandism.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "txUnknownHandler.h"
00040 #include "txExecutionState.h"
00041 #include "txStringUtils.h"
00042 #include "txStylesheet.h"
00043 
00044 txUnknownHandler::txUnknownHandler(txExecutionState* aEs)
00045     : mEs(aEs)
00046 {
00047 }
00048 
00049 txUnknownHandler::~txUnknownHandler()
00050 {
00051 }
00052 
00053 void txUnknownHandler::attribute(const nsAString& aName,
00054                                  const PRInt32 aNsID,
00055                                  const nsAString& aValue)
00056 {
00057     // If this is called then the stylesheet is trying to add an attribute
00058     // without adding an element first. So we'll just ignore it.
00059     // XXX ErrorReport: Signal this?
00060 }
00061 
00062 void txUnknownHandler::endDocument(nsresult aResult)
00063 {
00064     if (NS_FAILED(aResult)) {
00065         return;
00066     }
00067 
00068     // This is an unusual case, no output method has been set and we
00069     // didn't create a document element. Switching to XML output mode
00070     // anyway.
00071 
00072     // Make sure that mEs->mResultHandler == this is true, otherwise we'll
00073     // leak mEs->mResultHandler in createHandlerAndFlush and we'll crash on
00074     // the last line (delete this).
00075     NS_ASSERTION(mEs->mResultHandler == this,
00076                  "We're leaking mEs->mResultHandler and are going to crash.");
00077 
00078     nsresult rv = createHandlerAndFlush(eXMLOutput, EmptyString(),
00079                                         kNameSpaceID_None);
00080     if (NS_FAILED(rv))
00081         return;
00082 
00083     mEs->mResultHandler->endDocument(aResult);
00084 
00085     delete this;
00086 }
00087 
00088 void txUnknownHandler::startElement(const nsAString& aName,
00089                                     const PRInt32 aNsID)
00090 {
00091     // Make sure that mEs->mResultHandler == this is true, otherwise we'll
00092     // leak mEs->mResultHandler in createHandlerAndFlush and we may crash
00093     // later on trying to delete this handler again.
00094     NS_ASSERTION(mEs->mResultHandler == this,
00095                  "We're leaking mEs->mResultHandler.");
00096 
00097     nsresult rv = NS_OK;
00098     txOutputFormat* format = mEs->mStylesheet->getOutputFormat();
00099     if (format->mMethod != eMethodNotSet) {
00100         rv = createHandlerAndFlush(format->mMethod, aName, aNsID);
00101     }
00102     else if (aNsID == kNameSpaceID_None &&
00103              aName.Equals(NS_LITERAL_STRING("html"),
00104                           txCaseInsensitiveStringComparator())) {
00105         rv = createHandlerAndFlush(eHTMLOutput, aName, aNsID);
00106     }
00107     else {
00108         rv = createHandlerAndFlush(eXMLOutput, aName, aNsID);
00109     }
00110     if (NS_FAILED(rv))
00111         return;
00112 
00113     mEs->mResultHandler->startElement(aName, aNsID);
00114 
00115     delete this;
00116 }
00117 
00118 nsresult txUnknownHandler::createHandlerAndFlush(txOutputMethod aMethod,
00119                                                  const nsAString& aName,
00120                                                  const PRInt32 aNsID)
00121 {
00122     NS_ENSURE_TRUE(mBuffer, NS_ERROR_NOT_INITIALIZED);
00123 
00124     txOutputFormat format;
00125     format.merge(*mEs->mStylesheet->getOutputFormat());
00126     format.mMethod = aMethod;
00127 
00128     txAXMLEventHandler *handler = nsnull;
00129     nsresult rv = mEs->mOutputHandlerFactory->createHandlerWith(&format, aName,
00130                                                                 aNsID,
00131                                                                 &handler);
00132     NS_ENSURE_SUCCESS(rv, rv);
00133 
00134     mEs->mOutputHandler = handler;
00135     mEs->mResultHandler = handler;
00136 
00137     return mBuffer->flushToHandler(&handler);
00138 }