Back to index

lightning-sunbird  0.9+nobinonly
DocumentFunctionCall.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  * The MITRE Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Olivier Gerardin <ogerardin@vo.lu> (Original Author)
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 /*
00040  * DocumentFunctionCall
00041  * A representation of the XSLT additional function: document()
00042  */
00043 
00044 #include "txAtoms.h"
00045 #include "txIXPathContext.h"
00046 #include "XSLTFunctions.h"
00047 #include "txExecutionState.h"
00048 #include "txURIUtils.h"
00049 
00050 /*
00051  * Creates a new DocumentFunctionCall.
00052  */
00053 DocumentFunctionCall::DocumentFunctionCall(const nsAString& aBaseURI)
00054     : mBaseURI(aBaseURI)
00055 {
00056 }
00057 
00058 static void
00059 retrieveNode(txExecutionState* aExecutionState, const nsAString& aUri,
00060              const nsAString& aBaseUri, txNodeSet* aNodeSet)
00061 {
00062     nsAutoString absUrl;
00063     URIUtils::resolveHref(aUri, aBaseUri, absUrl);
00064 
00065     PRInt32 hash = absUrl.RFindChar(PRUnichar('#'));
00066     PRUint32 urlEnd, fragStart, fragEnd;
00067     if (hash == kNotFound) {
00068         urlEnd = absUrl.Length();
00069         fragStart = 0;
00070         fragEnd = 0;
00071     }
00072     else {
00073         urlEnd = hash;
00074         fragStart = hash + 1;
00075         fragEnd = absUrl.Length();
00076     }
00077 
00078     nsDependentSubstring docUrl(absUrl, 0, urlEnd);
00079     nsDependentSubstring frag(absUrl, fragStart, fragEnd);
00080 
00081     const txXPathNode* loadNode = aExecutionState->retrieveDocument(docUrl);
00082     if (loadNode) {
00083         if (frag.IsEmpty()) {
00084             aNodeSet->add(*loadNode);
00085         }
00086         else {
00087             txXPathTreeWalker walker(*loadNode);
00088             if (walker.moveToElementById(frag)) {
00089                 aNodeSet->add(walker.getCurrentPosition());
00090             }
00091         }
00092     }
00093 }
00094 
00095 /*
00096  * Evaluates this Expr based on the given context node and processor state
00097  * NOTE: the implementation is incomplete since it does not make use of the
00098  * second argument (base URI)
00099  * @param context the context node for evaluation of this Expr
00100  * @return the result of the evaluation
00101  */
00102 nsresult
00103 DocumentFunctionCall::evaluate(txIEvalContext* aContext,
00104                                txAExprResult** aResult)
00105 {
00106     *aResult = nsnull;
00107     txExecutionState* es =
00108         NS_STATIC_CAST(txExecutionState*, aContext->getPrivateContext());
00109 
00110     nsRefPtr<txNodeSet> nodeSet;
00111     nsresult rv = aContext->recycler()->getNodeSet(getter_AddRefs(nodeSet));
00112     NS_ENSURE_SUCCESS(rv, rv);
00113 
00114     // document(object, node-set?)
00115     if (!requireParams(1, 2, aContext)) {
00116         return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
00117     }
00118 
00119     txListIterator iter(&params);
00120     Expr* param1 = (Expr*)iter.next();
00121     nsRefPtr<txAExprResult> exprResult1;
00122     rv = param1->evaluate(aContext, getter_AddRefs(exprResult1));
00123     NS_ENSURE_SUCCESS(rv, rv);
00124 
00125     nsAutoString baseURI;
00126     MBool baseURISet = MB_FALSE;
00127 
00128     if (iter.hasNext()) {
00129         // We have 2 arguments, get baseURI from the first node
00130         // in the resulting nodeset
00131         nsRefPtr<txNodeSet> nodeSet2;
00132         rv = evaluateToNodeSet(NS_STATIC_CAST(Expr*, iter.next()),
00133                                aContext, getter_AddRefs(nodeSet2));
00134         NS_ENSURE_SUCCESS(rv, rv);
00135 
00136         // Make this true, even if nodeSet2 is empty. For relative URLs,
00137         // we'll fail to load the document with an empty base URI, and for
00138         // absolute URLs, the base URI doesn't matter
00139         baseURISet = MB_TRUE;
00140 
00141         if (!nodeSet2->isEmpty()) {
00142             txXPathNodeUtils::getBaseURI(nodeSet2->get(0), baseURI);
00143         }
00144     }
00145 
00146     if (exprResult1->getResultType() == txAExprResult::NODESET) {
00147         // The first argument is a NodeSet, iterate on its nodes
00148         txNodeSet* nodeSet1 = NS_STATIC_CAST(txNodeSet*,
00149                                              NS_STATIC_CAST(txAExprResult*,
00150                                                             exprResult1));
00151         PRInt32 i;
00152         for (i = 0; i < nodeSet1->size(); ++i) {
00153             const txXPathNode& node = nodeSet1->get(i);
00154             nsAutoString uriStr;
00155             txXPathNodeUtils::appendNodeValue(node, uriStr);
00156             if (!baseURISet) {
00157                 // if the second argument wasn't specified, use
00158                 // the baseUri of node itself
00159                 txXPathNodeUtils::getBaseURI(node, baseURI);
00160             }
00161             retrieveNode(es, uriStr, baseURI, nodeSet);
00162         }
00163         
00164         NS_ADDREF(*aResult = nodeSet);
00165         
00166         return NS_OK;
00167     }
00168 
00169     // The first argument is not a NodeSet
00170     nsAutoString uriStr;
00171     exprResult1->stringValue(uriStr);
00172     const nsAString* base = baseURISet ? &baseURI : &mBaseURI;
00173     retrieveNode(es, uriStr, *base, nodeSet);
00174 
00175     NS_ADDREF(*aResult = nodeSet);
00176 
00177     return NS_OK;
00178 }
00179 
00180 #ifdef TX_TO_STRING
00181 nsresult
00182 DocumentFunctionCall::getNameAtom(nsIAtom** aAtom)
00183 {
00184     *aAtom = txXSLTAtoms::document;
00185     NS_ADDREF(*aAtom);
00186     return NS_OK;
00187 }
00188 #endif