Back to index

lightning-sunbird  0.9+nobinonly
nsXPathResult.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 "nsXPathResult.h"
00040 #include "ExprResult.h"
00041 #include "txNodeSet.h"
00042 #include "nsDOMError.h"
00043 #include "nsIContent.h"
00044 #include "nsIDOMClassInfo.h"
00045 #include "nsIDOMNode.h"
00046 #include "nsXPathException.h"
00047 #include "nsIDOMDocument.h"
00048 #include "nsDOMString.h"
00049 
00050 nsXPathResult::nsXPathResult() : mDocument(nsnull),
00051                                  mCurrentPos(0),
00052                                  mResultType(ANY_TYPE),
00053                                  mInvalidIteratorState(PR_TRUE)
00054 {
00055 }
00056 
00057 nsXPathResult::~nsXPathResult()
00058 {
00059     if (mDocument) {
00060         mDocument->RemoveObserver(this);
00061     }
00062 }
00063 
00064 NS_IMPL_ADDREF(nsXPathResult)
00065 NS_IMPL_RELEASE(nsXPathResult)
00066 NS_INTERFACE_MAP_BEGIN(nsXPathResult)
00067   NS_INTERFACE_MAP_ENTRY(nsIDOMXPathResult)
00068   NS_INTERFACE_MAP_ENTRY(nsIDocumentObserver)
00069   NS_INTERFACE_MAP_ENTRY(nsIXPathResult)
00070   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMXPathResult)
00071   NS_INTERFACE_MAP_ENTRY_EXTERNAL_DOM_CLASSINFO(XPathResult)
00072 NS_INTERFACE_MAP_END
00073 
00074 NS_IMETHODIMP
00075 nsXPathResult::GetResultType(PRUint16 *aResultType)
00076 {
00077     *aResultType = mResultType;
00078 
00079     return NS_OK;
00080 }
00081 
00082 NS_IMETHODIMP
00083 nsXPathResult::GetNumberValue(double *aNumberValue)
00084 {
00085     if (mResultType != NUMBER_TYPE) {
00086         return NS_ERROR_DOM_TYPE_ERR;
00087     }
00088 
00089     *aNumberValue = mResult.get()->numberValue();
00090 
00091     return NS_OK;
00092 }
00093 
00094 NS_IMETHODIMP
00095 nsXPathResult::GetStringValue(nsAString &aStringValue)
00096 {
00097     if (mResultType != STRING_TYPE) {
00098         return NS_ERROR_DOM_TYPE_ERR;
00099     }
00100 
00101     mResult.get()->stringValue(aStringValue);
00102 
00103     return NS_OK;
00104 }
00105 
00106 NS_IMETHODIMP
00107 nsXPathResult::GetBooleanValue(PRBool *aBooleanValue)
00108 {
00109     if (mResultType != BOOLEAN_TYPE) {
00110         return NS_ERROR_DOM_TYPE_ERR;
00111     }
00112 
00113     *aBooleanValue = mResult.get()->booleanValue();
00114 
00115     return NS_OK;
00116 }
00117 
00118 NS_IMETHODIMP
00119 nsXPathResult::GetSingleNodeValue(nsIDOMNode **aSingleNodeValue)
00120 {
00121     if (!isNode()) {
00122         return NS_ERROR_DOM_TYPE_ERR;
00123     }
00124 
00125     txNodeSet *nodeSet = NS_STATIC_CAST(txNodeSet*, mResult.get());
00126     if (nodeSet->size() > 0) {
00127         return txXPathNativeNode::getNode(nodeSet->get(0), aSingleNodeValue);
00128     }
00129 
00130     *aSingleNodeValue = nsnull;
00131 
00132     return NS_OK;
00133 }
00134 
00135 NS_IMETHODIMP
00136 nsXPathResult::GetInvalidIteratorState(PRBool *aInvalidIteratorState)
00137 {
00138     *aInvalidIteratorState = isIterator() && mInvalidIteratorState;
00139 
00140     return NS_OK;
00141 }
00142 
00143 NS_IMETHODIMP
00144 nsXPathResult::GetSnapshotLength(PRUint32 *aSnapshotLength)
00145 {
00146     if (!isSnapshot()) {
00147         return NS_ERROR_DOM_TYPE_ERR;
00148     }
00149 
00150     txNodeSet *nodeSet = NS_STATIC_CAST(txNodeSet*, mResult.get());
00151     *aSnapshotLength = (PRUint32)nodeSet->size();
00152 
00153     return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP
00157 nsXPathResult::IterateNext(nsIDOMNode **aResult)
00158 {
00159     if (!isIterator()) {
00160         return NS_ERROR_DOM_TYPE_ERR;
00161     }
00162 
00163     if (mDocument) {
00164         mDocument->FlushPendingNotifications(Flush_Content);
00165     }
00166 
00167     if (mInvalidIteratorState) {
00168         return NS_ERROR_DOM_INVALID_STATE_ERR;
00169     }
00170 
00171     txNodeSet *nodeSet = NS_STATIC_CAST(txNodeSet*, mResult.get());
00172     if (mCurrentPos < (PRUint32)nodeSet->size()) {
00173         return txXPathNativeNode::getNode(nodeSet->get(mCurrentPos++),
00174                                           aResult);
00175     }
00176 
00177     *aResult = nsnull;
00178 
00179     return NS_OK;
00180 }
00181 
00182 NS_IMETHODIMP
00183 nsXPathResult::SnapshotItem(PRUint32 aIndex, nsIDOMNode **aResult)
00184 {
00185     if (!isSnapshot()) {
00186         return NS_ERROR_DOM_TYPE_ERR;
00187     }
00188 
00189     txNodeSet *nodeSet = NS_STATIC_CAST(txNodeSet*, mResult.get());
00190     if (aIndex < (PRUint32)nodeSet->size()) {
00191         return txXPathNativeNode::getNode(nodeSet->get(aIndex), aResult);
00192     }
00193 
00194     *aResult = nsnull;
00195 
00196     return NS_OK;
00197 }
00198 
00199 NS_IMPL_NSIDOCUMENTOBSERVER_CORE_STUB(nsXPathResult)
00200 NS_IMPL_NSIDOCUMENTOBSERVER_LOAD_STUB(nsXPathResult)
00201 NS_IMPL_NSIDOCUMENTOBSERVER_REFLOW_STUB(nsXPathResult)
00202 NS_IMPL_NSIDOCUMENTOBSERVER_STYLE_STUB(nsXPathResult)
00203 NS_IMPL_NSIDOCUMENTOBSERVER_STATE_STUB(nsXPathResult)
00204 
00205 void
00206 nsXPathResult::CharacterDataChanged(nsIDocument* aDocument,
00207                                     nsIContent *aContent,
00208                                     PRBool aAppend)
00209 {
00210     Invalidate();
00211 }
00212 
00213 void
00214 nsXPathResult::AttributeChanged(nsIDocument* aDocument,
00215                                 nsIContent* aContent,
00216                                 PRInt32 aNameSpaceID,
00217                                 nsIAtom* aAttribute,
00218                                 PRInt32 aModType)
00219 {
00220     Invalidate();
00221 }
00222 
00223 void
00224 nsXPathResult::ContentAppended(nsIDocument* aDocument,
00225                                nsIContent* aContainer,
00226                                PRInt32 aNewIndexInContainer)
00227 {
00228     Invalidate();
00229 }
00230 
00231 void
00232 nsXPathResult::ContentInserted(nsIDocument* aDocument,
00233                                nsIContent* aContainer,
00234                                nsIContent* aChild,
00235                                PRInt32 aIndexInContainer)
00236 {
00237     Invalidate();
00238 }
00239 
00240 void
00241 nsXPathResult::ContentRemoved(nsIDocument* aDocument,
00242                               nsIContent* aContainer,
00243                               nsIContent* aChild,
00244                               PRInt32 aIndexInContainer)
00245 {
00246     Invalidate();
00247 }
00248 
00249 nsresult
00250 nsXPathResult::SetExprResult(txAExprResult* aExprResult, PRUint16 aResultType)
00251 {
00252     mResultType = aResultType;
00253 
00254     if ((isSnapshot() || isIterator() || isNode()) &&
00255         aExprResult->getResultType() != txAExprResult::NODESET) {
00256         return NS_ERROR_DOM_TYPE_ERR;
00257     }
00258 
00259     if (mDocument) {
00260         mDocument->RemoveObserver(this);
00261         mDocument = nsnull;
00262     }
00263  
00264     mResult.set(aExprResult);
00265 
00266     if (!isIterator()) {
00267         return NS_OK;
00268     }
00269 
00270     mInvalidIteratorState = PR_FALSE;
00271 
00272     txNodeSet* nodeSet = NS_STATIC_CAST(txNodeSet*, aExprResult);
00273     nsCOMPtr<nsIDOMNode> node;
00274     if (nodeSet->size() > 0) {
00275         nsresult rv = txXPathNativeNode::getNode(nodeSet->get(0),
00276                                                  getter_AddRefs(node));
00277         NS_ENSURE_SUCCESS(rv, rv);
00278 
00279         // If we support the document() function in DOM-XPath we need to
00280         // observe all documents that we have resultnodes in.
00281         nsCOMPtr<nsIDOMDocument> document;
00282         node->GetOwnerDocument(getter_AddRefs(document));
00283         if (document) {
00284             mDocument = do_QueryInterface(document);
00285         }
00286         else {
00287             mDocument = do_QueryInterface(node);
00288         }
00289 
00290         NS_ASSERTION(mDocument, "We need a document!");
00291         if (mDocument) {
00292             mDocument->AddObserver(this);
00293         }
00294     }
00295 
00296     return NS_OK;
00297 }
00298 
00299 void
00300 nsXPathResult::Invalidate()
00301 {
00302     if (mDocument) {
00303         mDocument->RemoveObserver(this);
00304         mDocument = nsnull;
00305     }
00306     mInvalidIteratorState = PR_TRUE;
00307 }
00308 
00309 nsresult
00310 nsXPathResult::GetExprResult(txAExprResult** aExprResult)
00311 {
00312     if (isIterator() && mInvalidIteratorState) {
00313         return NS_ERROR_DOM_INVALID_STATE_ERR;
00314     }
00315 
00316     *aExprResult = mResult.get();
00317     if (!*aExprResult) {
00318         return NS_ERROR_DOM_INVALID_STATE_ERR;
00319     }
00320 
00321     NS_ADDREF(*aExprResult);
00322 
00323     return NS_OK;
00324 }
00325 
00326 nsresult
00327 nsXPathResult::Clone(nsIXPathResult **aResult)
00328 {
00329     *aResult = nsnull;
00330 
00331     if (isIterator() && mInvalidIteratorState) {
00332         return NS_ERROR_DOM_INVALID_STATE_ERR;
00333     }
00334 
00335     nsCOMPtr<nsIXPathResult> result = new nsXPathResult();
00336     if (!result) {
00337         return NS_ERROR_OUT_OF_MEMORY;
00338     }
00339 
00340     nsresult rv = result->SetExprResult(mResult.get(), mResultType);
00341     NS_ENSURE_SUCCESS(rv, rv);
00342 
00343     result.swap(*aResult);
00344 
00345     return NS_OK;
00346 }
00347 
00348 void
00349 txResultHolder::set(txAExprResult *aResult)
00350 {
00351     releaseNodeSet();
00352 
00353     // XXX This will keep the recycler alive, should we clear it?
00354     mResult = aResult;
00355 
00356     if (mResult && mResult->getResultType() == txAExprResult::NODESET) {
00357         txNodeSet *nodeSet =
00358             NS_STATIC_CAST(txNodeSet*,
00359                            NS_STATIC_CAST(txAExprResult*, mResult));
00360         PRInt32 i, count = nodeSet->size();
00361         for (i = 0; i < count; ++i) {
00362             txXPathNativeNode::addRef(nodeSet->get(i));
00363         }
00364     }
00365 }
00366 
00367 void
00368 txResultHolder::releaseNodeSet()
00369 {
00370     if (mResult && mResult->getResultType() == txAExprResult::NODESET) {
00371         txNodeSet *nodeSet =
00372             NS_STATIC_CAST(txNodeSet*,
00373                            NS_STATIC_CAST(txAExprResult*, mResult));
00374         PRInt32 i, count = nodeSet->size();
00375         for (i = 0; i < count; ++i) {
00376             txXPathNativeNode::release(nodeSet->get(i));
00377         }
00378     }
00379 }