Back to index

lightning-sunbird  0.9+nobinonly
Expr.h
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  *   Larry Fitzpatick, OpenText <lef@opentext.com>
00025  *   Jonas Sicking <sicking@bigfoot.com>
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #ifndef TRANSFRMX_EXPR_H
00042 #define TRANSFRMX_EXPR_H
00043 
00044 #include "txList.h"
00045 #include "nsAutoPtr.h"
00046 #include "txCore.h"
00047 #include "nsString.h"
00048 
00049 #ifdef DEBUG
00050 #define TX_TO_STRING
00051 #endif
00052 
00053 /*
00054   XPath class definitions.
00055   Much of this code was ported from XSL:P.
00056 */
00057 
00058 class nsIAtom;
00059 class txAExprResult;
00060 class txIParseContext;
00061 class txIMatchContext;
00062 class txIEvalContext;
00063 class txNodeSet;
00064 class txXPathNode;
00065 
00069 class Expr : public TxObject {
00070 
00071 public:
00072 
00076     virtual ~Expr()
00077     {
00078     }
00079 
00087     virtual nsresult evaluate(txIEvalContext* aContext,
00088                               txAExprResult** aResult) = 0;
00089 
00090 #ifdef TX_TO_STRING
00091 
00099     virtual void toString(nsAString& str) = 0;
00100 #endif
00101 }; //-- Expr
00102 
00103 #define TX_DECL_EVALUATE \
00104     nsresult evaluate(txIEvalContext* aContext, txAExprResult** aResult)
00105 
00106 #ifndef TX_TO_STRING
00107 #define TX_DECL_EXPR TX_DECL_EVALUATE
00108 #define TX_DECL_FUNCTION TX_DECL_EVALUATE
00109 #else
00110 #define TX_DECL_EXPR \
00111     TX_DECL_EVALUATE; \
00112     void toString(nsAString& aDest)
00113 
00114 #define TX_DECL_FUNCTION \
00115     TX_DECL_EVALUATE; \
00116     nsresult getNameAtom(nsIAtom** aAtom)
00117 #endif
00118 
00123 class FunctionCall : public Expr {
00124 
00125 public:
00126     virtual ~FunctionCall();
00127 
00135     nsresult addParam(Expr* aExpr);
00136 
00148     virtual PRBool requireParams(PRInt32 aParamCountMin,
00149                                  PRInt32 aParamCountMax,
00150                                  txIEvalContext* aContext);
00151 
00152 #ifdef TX_TO_STRING
00153     void toString(nsAString& aDest);
00154 #endif
00155 
00156 protected:
00157 
00158     txList params;
00159 
00160     FunctionCall();
00161 
00162     /*
00163      * Evaluates the given Expression and converts its result to a String.
00164      * The value is appended to the given destination String
00165      */
00166     void evaluateToString(Expr* aExpr, txIEvalContext* aContext,
00167                           nsAString& aDest);
00168 
00169     /*
00170      * Evaluates the given Expression and converts its result to a number.
00171      */
00172     double evaluateToNumber(Expr* aExpr, txIEvalContext* aContext);
00173 
00174     /*
00175      * Evaluates the given Expression and converts its result to a boolean.
00176      */
00177     MBool evaluateToBoolean(Expr* aExpr, txIEvalContext* aContext);
00178 
00179     /*
00180      * Evaluates the given Expression and converts its result to a NodeSet.
00181      * If the result is not a NodeSet an error is returned.
00182      */
00183     nsresult evaluateToNodeSet(Expr* aExpr, txIEvalContext* aContext,
00184                                txNodeSet** aResult);
00185 
00186 #ifdef TX_TO_STRING
00187     /*
00188      * Returns the name of the function as an atom.
00189      */
00190     virtual nsresult getNameAtom(nsIAtom** aAtom) = 0;
00191 #endif
00192 }; //-- FunctionCall
00193 
00194 
00198 class AttributeValueTemplate: public Expr {
00199 
00200 public:
00201 
00202     AttributeValueTemplate();
00203 
00204     virtual ~AttributeValueTemplate();
00205 
00209     void addExpr(Expr* expr);
00210 
00211     TX_DECL_EXPR;
00212 
00213 private:
00214     List expressions;
00215 };
00216 
00217 
00218 /*
00219  * This class represents a NodeTest as defined by the XPath spec
00220  */
00221 class txNodeTest
00222 {
00223 public:
00224     virtual ~txNodeTest() {}
00225     /*
00226      * Virtual methods
00227      * pretty much a txPattern, but not supposed to be used 
00228      * standalone. The NodeTest node() is different to the
00229      * Pattern "node()" (document node isn't matched)
00230      */
00231     virtual PRBool matches(const txXPathNode& aNode,
00232                            txIMatchContext* aContext) = 0;
00233     virtual double getDefaultPriority() = 0;
00234 
00235 #ifdef TX_TO_STRING
00236     virtual void toString(nsAString& aDest) = 0;
00237 #endif
00238 };
00239 
00240 #define TX_DECL_NODE_TEST_BASE \
00241     PRBool matches(const txXPathNode& aNode, txIMatchContext* aContext); \
00242     double getDefaultPriority()
00243 
00244 #ifndef TX_TO_STRING
00245 #define TX_DECL_NODE_TEST TX_DECL_NODE_TEST_BASE
00246 #else
00247 #define TX_DECL_NODE_TEST \
00248     TX_DECL_NODE_TEST_BASE; \
00249     void toString(nsAString& aDest)
00250 #endif
00251 
00252 /*
00253  * This class represents a NameTest as defined by the XPath spec
00254  */
00255 class txNameTest : public txNodeTest
00256 {
00257 public:
00258     /*
00259      * Creates a new txNameTest with the given type and the given
00260      * principal node type
00261      */
00262     txNameTest(nsIAtom* aPrefix, nsIAtom* aLocalName, PRInt32 aNSID,
00263                PRUint16 aNodeType);
00264 
00265     ~txNameTest();
00266 
00267     TX_DECL_NODE_TEST;
00268 
00269 private:
00270     nsCOMPtr<nsIAtom> mPrefix;
00271     nsCOMPtr<nsIAtom> mLocalName;
00272     PRInt32 mNamespace;
00273     PRUint16 mNodeType;
00274 };
00275 
00276 /*
00277  * This class represents a NodeType as defined by the XPath spec
00278  */
00279 class txNodeTypeTest : public txNodeTest
00280 {
00281 public:
00282     enum NodeType {
00283         COMMENT_TYPE,
00284         TEXT_TYPE,
00285         PI_TYPE,
00286         NODE_TYPE
00287     };
00288 
00289     /*
00290      * Creates a new txNodeTypeTest of the given type
00291      */
00292     txNodeTypeTest(NodeType aNodeType);
00293 
00294     ~txNodeTypeTest();
00295 
00296     /*
00297      * Sets the name of the node to match. Only availible for pi nodes
00298      */
00299     void setNodeName(const nsAString& aName);
00300 
00301     TX_DECL_NODE_TEST;
00302 
00303 private:
00304     NodeType mNodeType;
00305     nsCOMPtr<nsIAtom> mNodeName;
00306 };
00307 
00312 class PredicateList  {
00313 
00314 public:
00315 
00319     PredicateList();
00324     virtual ~PredicateList();
00325 
00333     nsresult add(Expr* aExpr);
00334 
00335     nsresult evaluatePredicates(txNodeSet* aNodes, txIMatchContext* aContext);
00336 
00340     MBool isEmpty();
00341 
00342 #ifdef TX_TO_STRING
00343 
00351     virtual void toString(nsAString& dest);
00352 #endif
00353 
00354 protected:
00355     //-- list of predicates
00356     List predicates;
00357 }; //-- PredicateList
00358 
00359 class LocationStep : public PredicateList,
00360                      public Expr
00361 {
00362 public:
00363     enum LocationStepType {
00364         ANCESTOR_AXIS = 0,
00365         ANCESTOR_OR_SELF_AXIS,
00366         ATTRIBUTE_AXIS,
00367         CHILD_AXIS,
00368         DESCENDANT_AXIS,
00369         DESCENDANT_OR_SELF_AXIS,
00370         FOLLOWING_AXIS,
00371         FOLLOWING_SIBLING_AXIS,
00372         NAMESPACE_AXIS,
00373         PARENT_AXIS,
00374         PRECEDING_AXIS,
00375         PRECEDING_SIBLING_AXIS,
00376         SELF_AXIS
00377     };
00378 
00384     LocationStep(nsAutoPtr<txNodeTest>& aNodeTest,
00385                  LocationStepType aAxisIdentifier)
00386         : mNodeTest(aNodeTest),
00387           mAxisIdentifier(aAxisIdentifier)
00388     {
00389     }
00390 
00391     TX_DECL_EXPR;
00392 
00393 private:
00394     void fromDescendants(const txXPathNode& aNode, txIMatchContext* aCs,
00395                          txNodeSet* aNodes);
00396     void fromDescendantsRev(const txXPathNode& aNode, txIMatchContext* aCs,
00397                             txNodeSet* aNodes);
00398 
00399     nsAutoPtr<txNodeTest> mNodeTest;
00400     LocationStepType mAxisIdentifier;
00401 };
00402 
00403 class FilterExpr : public PredicateList, public Expr {
00404 
00405 public:
00406 
00411     FilterExpr(nsAutoPtr<Expr>& aExpr)
00412         : expr(aExpr)
00413     {
00414     }
00415 
00416     TX_DECL_EXPR;
00417 
00418 private:
00419     nsAutoPtr<Expr> expr;
00420 
00421 }; //-- FilterExpr
00422 
00423 
00424 class txLiteralExpr : public Expr {
00425 public:
00426     txLiteralExpr(double aDbl);
00427     txLiteralExpr(const nsAString& aStr);
00428 
00429     TX_DECL_EXPR;
00430 
00431 private:
00432     nsRefPtr<txAExprResult> mValue;
00433 };
00434 
00441 class AdditiveExpr : public Expr {
00442 
00443 public:
00444 
00445     //-- AdditiveExpr Types
00446     //-- LF, changed from static const short to enum
00447     enum _AdditiveExprType { ADDITION = 1, SUBTRACTION };
00448 
00449      AdditiveExpr(nsAutoPtr<Expr>& aLeftExpr, nsAutoPtr<Expr>& aRightExpr,
00450                   short aOp)
00451          : op(aOp),
00452            leftExpr(aLeftExpr),
00453            rightExpr(aRightExpr)
00454     {
00455     }
00456 
00457     TX_DECL_EXPR;
00458 
00459 private:
00460     short op;
00461     nsAutoPtr<Expr> leftExpr, rightExpr;
00462 }; //-- AdditiveExpr
00463 
00467 class UnaryExpr : public Expr {
00468 
00469 public:
00470 
00471     UnaryExpr(nsAutoPtr<Expr>& aExpr)
00472         : expr(aExpr)
00473     {
00474     }
00475 
00476     TX_DECL_EXPR;
00477 
00478 private:
00479     nsAutoPtr<Expr> expr;
00480 }; //-- UnaryExpr
00481 
00486 class BooleanExpr : public Expr {
00487 
00488 public:
00489 
00490     //-- BooleanExpr Types
00491     enum _BooleanExprType { AND = 1, OR };
00492 
00493      BooleanExpr(nsAutoPtr<Expr>& aLeftExpr, nsAutoPtr<Expr>& aRightExpr,
00494                  short aOp)
00495          : op(aOp),
00496            leftExpr(aLeftExpr),
00497            rightExpr(aRightExpr)
00498     {
00499     }
00500 
00501     TX_DECL_EXPR;
00502 
00503 private:
00504     short op;
00505     nsAutoPtr<Expr> leftExpr, rightExpr;
00506 }; //-- BooleanExpr
00507 
00516 class MultiplicativeExpr : public Expr {
00517 
00518 public:
00519 
00520     //-- MultiplicativeExpr Types
00521     //-- LF, changed from static const short to enum
00522     enum _MultiplicativeExprType { DIVIDE = 1, MULTIPLY, MODULUS };
00523 
00524      MultiplicativeExpr(nsAutoPtr<Expr>& aLeftExpr,
00525                         nsAutoPtr<Expr>& aRightExpr,
00526                         short aOp)
00527          : op(aOp),
00528            leftExpr(aLeftExpr),
00529            rightExpr(aRightExpr)
00530     {
00531     }
00532 
00533     TX_DECL_EXPR;
00534 
00535 private:
00536     short op;
00537     nsAutoPtr<Expr> leftExpr, rightExpr;
00538 }; //-- MultiplicativeExpr
00539 
00550 class RelationalExpr : public Expr {
00551 
00552 public:
00553     enum RelationalExprType {
00554         EQUAL,
00555         NOT_EQUAL,
00556         LESS_THAN,
00557         GREATER_THAN,
00558         LESS_OR_EQUAL,
00559         GREATER_OR_EQUAL
00560     };
00561 
00562     RelationalExpr(nsAutoPtr<Expr>& aLeftExpr, nsAutoPtr<Expr>& aRightExpr,
00563                    RelationalExprType aOp)
00564         : mLeftExpr(aLeftExpr),
00565           mRightExpr(aRightExpr),
00566           mOp(aOp)
00567     {
00568     }
00569 
00570 
00571     TX_DECL_EXPR;
00572 
00573 private:
00574     PRBool compareResults(txIEvalContext* aContext, txAExprResult* aLeft,
00575                           txAExprResult* aRight);
00576 
00577     nsAutoPtr<Expr> mLeftExpr;
00578     nsAutoPtr<Expr> mRightExpr;
00579     RelationalExprType mOp;
00580 };
00581 
00586 class VariableRefExpr : public Expr {
00587 
00588 public:
00589 
00590     VariableRefExpr(nsIAtom* aPrefix, nsIAtom* aLocalName, PRInt32 aNSID);
00591     ~VariableRefExpr();
00592 
00593     TX_DECL_EXPR;
00594 
00595 private:
00596     nsCOMPtr<nsIAtom> mPrefix;
00597     nsCOMPtr<nsIAtom> mLocalName;
00598     PRInt32 mNamespace;
00599 };
00600 
00604 class PathExpr : public Expr {
00605 
00606 public:
00607 
00608     //-- Path Operators
00609     //-- RELATIVE_OP is the default
00610     //-- LF, changed from static const short to enum
00611     enum PathOperator { RELATIVE_OP, DESCENDANT_OP };
00612 
00616     PathExpr();
00617 
00621     virtual ~PathExpr();
00622 
00630     nsresult addExpr(Expr* aExpr, PathOperator pathOp);
00631 
00632     TX_DECL_EXPR;
00633 
00634 private:
00635     class PathExprItem {
00636     public:
00637         PathExprItem(Expr* aExpr, PathOperator aOp)
00638             : expr(aExpr),
00639               pathOp(aOp)
00640         {}
00641         nsAutoPtr<Expr> expr;
00642         PathOperator pathOp;
00643     };
00644 
00645     List expressions;
00646 
00647     /*
00648      * Selects from the descendants of the context node
00649      * all nodes that match the Expr
00650      */
00651     nsresult evalDescendants(Expr* aStep, const txXPathNode& aNode,
00652                              txIMatchContext* aContext,
00653                              txNodeSet* resNodes);
00654 };
00655 
00659 class RootExpr : public Expr {
00660 public:
00664     RootExpr()
00665 #ifdef TX_TO_STRING
00666         : mSerialize(PR_TRUE)
00667 #endif
00668     {
00669     }
00670 
00671     TX_DECL_EXPR;
00672 
00673 #ifdef TX_TO_STRING
00674 public:
00675     void setSerialize(PRBool aSerialize)
00676     {
00677         mSerialize = aSerialize;
00678     }
00679 
00680 private:
00681     // When a RootExpr is used in a PathExpr it shouldn't be serialized
00682     PRBool mSerialize;
00683 #endif
00684 }; //-- RootExpr
00685 
00689 class UnionExpr : public Expr {
00690 
00691 public:
00692 
00696     UnionExpr();
00697 
00701     virtual ~UnionExpr();
00702 
00710     nsresult addExpr(Expr* aExpr);
00711 
00712     TX_DECL_EXPR;
00713 
00714 private:
00715 
00716    List expressions;
00717 
00718 }; //-- UnionExpr
00719 
00723 class txErrorExpr : public Expr
00724 {
00725 public:
00726 #ifdef TX_TO_STRING
00727     txErrorExpr(const nsAString& aStr)
00728       : mStr(aStr)
00729     {
00730     }
00731 #endif
00732 
00733     TX_DECL_EXPR;
00734 
00735 #ifdef TX_TO_STRING
00736 private:
00737     nsString mStr;
00738 #endif
00739 };
00740 
00741 #endif
00742 
00743