Back to index

lightning-sunbird  0.9+nobinonly
PathExpr.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  *   Keith Visco <kvisco@ziplink.net> (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 #include "Expr.h"
00040 #include "txNodeSet.h"
00041 #include "txNodeSetContext.h"
00042 #include "txSingleNodeContext.h"
00043 #include "XMLUtils.h"
00044 
00045   //------------/
00046  //- PathExpr -/
00047 //------------/
00048 
00052 PathExpr::PathExpr()
00053 {
00054     //-- do nothing
00055 }
00056 
00060 PathExpr::~PathExpr()
00061 {
00062     txListIterator iter(&expressions);
00063     while (iter.hasNext()) {
00064          delete NS_STATIC_CAST(PathExprItem*, iter.next());
00065     }
00066 } //-- ~PathExpr
00067 
00072 nsresult
00073 PathExpr::addExpr(Expr* aExpr, PathOperator pathOp)
00074 {
00075     NS_ASSERTION(expressions.getLength() > 0 || pathOp == RELATIVE_OP,
00076                  "First step has to be relative in PathExpr");
00077     PathExprItem* pxi = new PathExprItem(aExpr, pathOp);
00078     if (!pxi) {
00079         delete aExpr;
00080         return NS_ERROR_OUT_OF_MEMORY;
00081     }
00082     nsresult rv = expressions.add(pxi);
00083     if (NS_FAILED(rv)) {
00084         delete pxi;
00085     }
00086     return rv;
00087 } //-- addExpr
00088 
00089     //-----------------------------/
00090   //- Virtual methods from Expr -/
00091 //-----------------------------/
00092 
00100 nsresult
00101 PathExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
00102 {
00103     *aResult = nsnull;
00104 
00105     nsRefPtr<txNodeSet> nodes;
00106     nsresult rv = aContext->recycler()->getNodeSet(aContext->getContextNode(),
00107                                                    getter_AddRefs(nodes));
00108     NS_ENSURE_SUCCESS(rv, rv);
00109 
00110     txListIterator iter(&expressions);
00111     PathExprItem* pxi;
00112     while ((pxi = (PathExprItem*)iter.next())) {
00113         nsRefPtr<txNodeSet> tmpNodes;
00114         txNodeSetContext eContext(nodes, aContext);
00115         while (eContext.hasNext()) {
00116             eContext.next();
00117 
00118             nsRefPtr<txNodeSet> resNodes;
00119             if (pxi->pathOp == DESCENDANT_OP) {
00120                 rv = aContext->recycler()->getNodeSet(getter_AddRefs(resNodes));
00121                 NS_ENSURE_SUCCESS(rv, rv);
00122 
00123                 rv = evalDescendants(pxi->expr, eContext.getContextNode(),
00124                                      &eContext, resNodes);
00125                 NS_ENSURE_SUCCESS(rv, rv);
00126             }
00127             else {
00128                 nsRefPtr<txAExprResult> res;
00129                 rv = pxi->expr->evaluate(&eContext, getter_AddRefs(res));
00130                 NS_ENSURE_SUCCESS(rv, rv);
00131 
00132                 if (res->getResultType() != txAExprResult::NODESET) {
00133                     //XXX ErrorReport: report nonnodeset error
00134                     return NS_ERROR_XSLT_NODESET_EXPECTED;
00135                 }
00136                 resNodes = NS_STATIC_CAST(txNodeSet*,
00137                                           NS_STATIC_CAST(txAExprResult*,
00138                                                          res));
00139             }
00140 
00141             if (tmpNodes) {
00142                 if (!resNodes->isEmpty()) {
00143                     nsRefPtr<txNodeSet> oldSet;
00144                     oldSet.swap(tmpNodes);
00145                     rv = aContext->recycler()->
00146                         getNonSharedNodeSet(oldSet, getter_AddRefs(tmpNodes));
00147                     NS_ENSURE_SUCCESS(rv, rv);
00148 
00149                     oldSet.swap(resNodes);
00150                     rv = aContext->recycler()->
00151                         getNonSharedNodeSet(oldSet, getter_AddRefs(resNodes));
00152                     NS_ENSURE_SUCCESS(rv, rv);
00153 
00154                     tmpNodes->addAndTransfer(resNodes);
00155                 }
00156             }
00157             else {
00158                 tmpNodes = resNodes;
00159             }
00160         }
00161         nodes = tmpNodes;
00162         if (nodes->isEmpty()) {
00163             break;
00164         }
00165     }
00166 
00167     *aResult = nodes;
00168     NS_ADDREF(*aResult);
00169     
00170     return NS_OK;
00171 } //-- evaluate
00172 
00177 nsresult
00178 PathExpr::evalDescendants(Expr* aStep, const txXPathNode& aNode,
00179                           txIMatchContext* aContext, txNodeSet* resNodes)
00180 {
00181     txSingleNodeContext eContext(aNode, aContext);
00182     nsRefPtr<txAExprResult> res;
00183     nsresult rv = aStep->evaluate(&eContext, getter_AddRefs(res));
00184     NS_ENSURE_SUCCESS(rv, rv);
00185 
00186     if (res->getResultType() != txAExprResult::NODESET) {
00187         //XXX ErrorReport: report nonnodeset error
00188         return NS_ERROR_XSLT_NODESET_EXPECTED;
00189     }
00190 
00191     txNodeSet* oldSet = NS_STATIC_CAST(txNodeSet*,
00192                                        NS_STATIC_CAST(txAExprResult*, res));
00193     nsRefPtr<txNodeSet> newSet;
00194     rv = aContext->recycler()->getNonSharedNodeSet(oldSet,
00195                                                    getter_AddRefs(newSet));
00196     NS_ENSURE_SUCCESS(rv, rv);
00197 
00198     resNodes->addAndTransfer(newSet);
00199 
00200     MBool filterWS = aContext->isStripSpaceAllowed(aNode);
00201 
00202     txXPathTreeWalker walker(aNode);
00203     if (!walker.moveToFirstChild()) {
00204         return NS_OK;
00205     }
00206 
00207     do {
00208         const txXPathNode& node = walker.getCurrentPosition();
00209         if (!(filterWS && txXPathNodeUtils::isText(node) &&
00210               txXPathNodeUtils::isWhitespace(node))) {
00211             rv = evalDescendants(aStep, node, aContext, resNodes);
00212             NS_ENSURE_SUCCESS(rv, rv);
00213         }
00214     } while (walker.moveToNextSibling());
00215 
00216     return NS_OK;
00217 } //-- evalDescendants
00218 
00219 #ifdef TX_TO_STRING
00220 void
00221 PathExpr::toString(nsAString& dest)
00222 {
00223     txListIterator iter(&expressions);
00224     
00225     PathExprItem* pxi = (PathExprItem*)iter.next();
00226     if (pxi) {
00227         NS_ASSERTION(pxi->pathOp == RELATIVE_OP,
00228                      "First step should be relative");
00229         pxi->expr->toString(dest);
00230     }
00231     
00232     while ((pxi = (PathExprItem*)iter.next())) {
00233         switch (pxi->pathOp) {
00234             case DESCENDANT_OP:
00235                 dest.AppendLiteral("//");
00236                 break;
00237             case RELATIVE_OP:
00238                 dest.Append(PRUnichar('/'));
00239                 break;
00240         }
00241         pxi->expr->toString(dest);
00242     }
00243 }
00244 #endif