зеркало из https://github.com/mozilla/gecko-dev.git
added support for extension functions
This commit is contained in:
Родитель
714ddbb21f
Коммит
5b802effd9
|
@ -25,18 +25,9 @@
|
|||
* - changed constant short declarations in many of the classes
|
||||
* with enumerations, commented with //--LF
|
||||
*
|
||||
* $Id: txExpr.h,v 1.3 2005/11/02 07:33:28 nisheeth%netscape.com Exp $
|
||||
* $Id: txExpr.h,v 1.4 2005/11/02 07:33:29 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* XSL expression class definitions.
|
||||
* Much of this code was ported from XSL:P. <BR />
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.3 $ $Date: 2005/11/02 07:33:28 $
|
||||
**/
|
||||
|
||||
#ifndef TRANSFRMX_EXPR_H
|
||||
#define TRANSFRMX_EXPR_H
|
||||
|
||||
#include <math.h>
|
||||
#include "TxString.h"
|
||||
|
@ -50,6 +41,22 @@
|
|||
#include "MITREObject.h"
|
||||
#include "primitives.h"
|
||||
|
||||
/*
|
||||
XPath class definitions.
|
||||
Much of this code was ported from XSL:P.
|
||||
@version $Revision: 1.4 $ $Date: 2005/11/02 07:33:29 $
|
||||
*/
|
||||
|
||||
#ifndef TRANSFRMX_EXPR_H
|
||||
#define TRANSFRMX_EXPR_H
|
||||
|
||||
|
||||
//necessary prototypes
|
||||
class FunctionCall;
|
||||
|
||||
/**
|
||||
* The expression context and state class used when evaluating XPath Expressions.
|
||||
**/
|
||||
class ContextState : public ErrorObserver {
|
||||
|
||||
public:
|
||||
|
@ -81,6 +88,13 @@ public:
|
|||
virtual MBool isStripSpaceAllowed(Node* node) = 0;
|
||||
|
||||
|
||||
/**
|
||||
* Returns a call to the function that has the given name.
|
||||
* This method is used for XPath Extension Functions.
|
||||
* @return the FunctionCall for the function with the given name.
|
||||
**/
|
||||
virtual FunctionCall* resolveFunctionCall(const String& name) = 0;
|
||||
|
||||
/**
|
||||
* Sorts the given NodeSet by DocumentOrder.
|
||||
* @param nodes the NodeSet to sort
|
||||
|
@ -125,6 +139,88 @@ public:
|
|||
}; //-- Expr
|
||||
|
||||
|
||||
/**
|
||||
* This class represents a FunctionCall as defined by the XPath 1.0
|
||||
* Recommendation.
|
||||
**/
|
||||
class FunctionCall : public Expr {
|
||||
|
||||
public:
|
||||
|
||||
static const String INVALID_PARAM_COUNT;
|
||||
|
||||
|
||||
virtual ~FunctionCall();
|
||||
|
||||
/**
|
||||
* Adds the given parameter to this FunctionCall's parameter list
|
||||
* @param expr the Expr to add to this FunctionCall's parameter list
|
||||
**/
|
||||
void addParam(Expr* expr);
|
||||
|
||||
/**
|
||||
* Evaluates this Expr based on the given context node and processor state
|
||||
* @param context the context node for evaluation of this Expr
|
||||
* @param ps the ContextState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
**/
|
||||
virtual ExprResult* evaluate(Node* context, ContextState* cs) = 0;
|
||||
|
||||
/**
|
||||
* Returns the name of this FunctionCall
|
||||
* @return the name of this FunctionCall
|
||||
**/
|
||||
const String& getName();
|
||||
|
||||
virtual MBool requireParams(int paramCountMin, ContextState* cs);
|
||||
|
||||
virtual MBool requireParams(int paramCountMin,
|
||||
int paramCountMax,
|
||||
ContextState* cs);
|
||||
|
||||
/**
|
||||
* Sets the function name of this FunctionCall
|
||||
* @param name the name of this Function
|
||||
**/
|
||||
void setName(const String& name);
|
||||
/**
|
||||
* Returns the String representation of this Pattern.
|
||||
* @param dest the String to use when creating the String
|
||||
* representation. The String representation will be appended to
|
||||
* any data in the destination String, to allow cascading calls to
|
||||
* other #toString() methods for Expressions.
|
||||
* @return the String representation of this Pattern.
|
||||
**/
|
||||
virtual void toString(String& dest);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
List params;
|
||||
|
||||
FunctionCall();
|
||||
FunctionCall(const String& name);
|
||||
FunctionCall(const String& name, List* parameters);
|
||||
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a String.
|
||||
* The value is appended to the given destination String
|
||||
**/
|
||||
void evaluateToString
|
||||
(Expr* expr, Node* context, ContextState* cs, String& dest);
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a number.
|
||||
**/
|
||||
double evaluateToNumber(Expr* expr, Node* context, ContextState* cs);
|
||||
|
||||
private:
|
||||
|
||||
String name;
|
||||
}; //-- FunctionCall
|
||||
|
||||
/**
|
||||
* A base Pattern class
|
||||
**/
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
* -- fixed bug in ::parsePredicates,
|
||||
* made sure we continue looking for more predicates.
|
||||
*
|
||||
* $Id: txExprParser.cpp,v 1.3 2005/11/02 07:33:27 Peter.VanderBeken%pandora.be Exp $
|
||||
* $Id: txExprParser.cpp,v 1.4 2005/11/02 07:33:28 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -34,7 +34,7 @@
|
|||
* This class is used to parse XSL Expressions
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @see ExprLexer
|
||||
* @version $Revision: 1.3 $ $Date: 2005/11/02 07:33:27 $
|
||||
* @version $Revision: 1.4 $ $Date: 2005/11/02 07:33:28 $
|
||||
**/
|
||||
|
||||
#include "ExprParser.h"
|
||||
|
@ -468,10 +468,9 @@ FunctionCall* ExprParser::createFunctionCall(ExprLexer& lexer) {
|
|||
}
|
||||
// OG-
|
||||
else {
|
||||
//-- create error function() for now, should be ext function
|
||||
String err = "not a valid function: ";
|
||||
err.append(tok->value);
|
||||
fnCall = new ErrorFunctionCall(err);
|
||||
//-- Most likely an Extension Function, or error, but it's
|
||||
//-- not our job to report an invalid function call here
|
||||
fnCall = new ExtensionFunctionCall(fnName);
|
||||
}
|
||||
//-- handle parametes
|
||||
List params;
|
||||
|
|
|
@ -21,15 +21,15 @@
|
|||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txFunctionCall.cpp,v 1.1 2005/11/02 07:33:38 kvisco%ziplink.net Exp $
|
||||
* $Id: txFunctionCall.cpp,v 1.2 2005/11/02 07:33:39 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "FunctionLib.h"
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* This class represents a FunctionCall as defined by the XSL Working Draft
|
||||
* @author <A HREF="mailto:kvisco@ziplink">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:38 $
|
||||
* @version $Revision: 1.2 $ $Date: 2005/11/02 07:33:39 $
|
||||
**/
|
||||
|
||||
const String FunctionCall::INVALID_PARAM_COUNT =
|
||||
|
|
|
@ -24,14 +24,9 @@
|
|||
* Olivier Gerardin, ogerardin@vo.lu
|
||||
* -- added number functions
|
||||
*
|
||||
* $Id: txFunctionLib.h,v 1.4 2005/11/02 07:33:46 Peter.VanderBeken%pandora.be Exp $
|
||||
* $Id: txFunctionLib.h,v 1.5 2005/11/02 07:33:47 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef MITREXSL_FUNCTIONLIB_H
|
||||
#define MITREXSL_FUNCTIONLIB_H
|
||||
|
||||
#include "TxString.h"
|
||||
#include "primitives.h"
|
||||
#include "NodeSet.h"
|
||||
|
@ -44,6 +39,11 @@
|
|||
#include "XMLUtils.h"
|
||||
#include <math.h>
|
||||
|
||||
|
||||
#ifndef TRANSFRMX_FUNCTIONLIB_H
|
||||
#define TRANSFRMX_FUNCTIONLIB_H
|
||||
|
||||
|
||||
class XPathNames {
|
||||
|
||||
public:
|
||||
|
@ -80,14 +80,16 @@ static const String ERROR_FN;
|
|||
|
||||
}; //-- XPathNames
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This class represents a FunctionCall as defined by the XSL
|
||||
* Working Draft.
|
||||
* This file was ported from XSL:P <BR />
|
||||
* @author <a href="mailto:kvisco@mitre.org">Keith Visco</a>
|
||||
* <BR/>
|
||||
* The following are definitions for the XPath functions
|
||||
*
|
||||
* <PRE>
|
||||
* Modifications:
|
||||
* 20000418: Keith Visco
|
||||
* -- added ExtensionFunctionCall
|
||||
*
|
||||
* 19990805: Keith Visco
|
||||
* - added NodeSetFunctionCall
|
||||
* - moved position() function into NodeSetFunctionCall
|
||||
|
@ -100,94 +102,16 @@ static const String ERROR_FN;
|
|||
* - stated using Larry's enum suggestion instead of using static const shorts,
|
||||
* as you can see, I am a Java developer! ;-)
|
||||
* </PRE>
|
||||
**/
|
||||
class FunctionCall : public Expr {
|
||||
|
||||
public:
|
||||
|
||||
static const String INVALID_PARAM_COUNT;
|
||||
|
||||
|
||||
virtual ~FunctionCall();
|
||||
|
||||
/**
|
||||
* Adds the given parameter to this FunctionCall's parameter list
|
||||
* @param expr the Expr to add to this FunctionCall's parameter list
|
||||
**/
|
||||
void addParam(Expr* expr);
|
||||
|
||||
/**
|
||||
* Evaluates this Expr based on the given context node and processor state
|
||||
* @param context the context node for evaluation of this Expr
|
||||
* @param ps the ContextState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
**/
|
||||
virtual ExprResult* evaluate(Node* context, ContextState* cs) = 0;
|
||||
|
||||
/**
|
||||
* Returns the name of this FunctionCall
|
||||
* @return the name of this FunctionCall
|
||||
**/
|
||||
const String& getName();
|
||||
|
||||
virtual MBool requireParams(int paramCountMin, ContextState* cs);
|
||||
|
||||
virtual MBool requireParams(int paramCountMin,
|
||||
int paramCountMax,
|
||||
ContextState* cs);
|
||||
|
||||
/**
|
||||
* Sets the function name of this FunctionCall
|
||||
* @param name the name of this Function
|
||||
**/
|
||||
void setName(const String& name);
|
||||
/**
|
||||
* Returns the String representation of this Pattern.
|
||||
* @param dest the String to use when creating the String
|
||||
* representation. The String representation will be appended to
|
||||
* any data in the destination String, to allow cascading calls to
|
||||
* other #toString() methods for Expressions.
|
||||
* @return the String representation of this Pattern.
|
||||
**/
|
||||
virtual void toString(String& dest);
|
||||
|
||||
|
||||
protected:
|
||||
|
||||
List params;
|
||||
|
||||
FunctionCall();
|
||||
FunctionCall(const String& name);
|
||||
FunctionCall(const String& name, List* parameters);
|
||||
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a String.
|
||||
* The value is appended to the given destination String
|
||||
**/
|
||||
void evaluateToString
|
||||
(Expr* expr, Node* context, ContextState* cs, String& dest);
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a number.
|
||||
**/
|
||||
double evaluateToNumber(Expr* expr, Node* context, ContextState* cs);
|
||||
|
||||
private:
|
||||
|
||||
String name;
|
||||
}; //-- FunctionCall
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents the Set of boolean functions
|
||||
* @author <a href="mailto:kvisco@mitre.org">Keith Visco</a>
|
||||
**/
|
||||
class BooleanFunctionCall : public FunctionCall {
|
||||
|
||||
public:
|
||||
|
||||
enum BooleanFunctions { TX_BOOLEAN = 1, TX_FALSE, TX_NOT, TX_TRUE };
|
||||
enum booleanFunctions { TX_BOOLEAN = 1, TX_FALSE, TX_NOT, TX_TRUE };
|
||||
|
||||
/**
|
||||
* Creates a default BooleanFunctionCall, which always evaluates to False
|
||||
|
@ -239,6 +163,89 @@ private:
|
|||
|
||||
}; //-- ErrorFunctionCall
|
||||
|
||||
|
||||
/**
|
||||
* Used for extension functions
|
||||
**/
|
||||
class ExtensionFunctionCall : public FunctionCall {
|
||||
|
||||
public:
|
||||
|
||||
static const String UNDEFINED_FUNCTION;
|
||||
|
||||
/**
|
||||
* Creates a new ExtensionFunctionCall with the given function name
|
||||
* @param name the name of the extension function
|
||||
**/
|
||||
ExtensionFunctionCall(const String& name);
|
||||
|
||||
/**
|
||||
* Destructor for extension function call
|
||||
**/
|
||||
virtual ~ExtensionFunctionCall();
|
||||
|
||||
/**
|
||||
* Evaluates this Expr based on the given context node and processor state
|
||||
* @param context the context node for evaluation of this Expr
|
||||
* @param ps the ContextState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
**/
|
||||
virtual ExprResult* evaluate(Node* context, ContextState* cs);
|
||||
|
||||
private:
|
||||
|
||||
String fname;
|
||||
FunctionCall* fnCall;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* This class is used by ExtensionFunctionCall, to prevent deletion
|
||||
* of the parameter expressions, by the resolved function call. The implementation
|
||||
* for this class is in ExtensionFunctionCall.cpp
|
||||
**/
|
||||
class ExprWrapper : public Expr {
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Creates a new ExprWrapper for the given Expr
|
||||
**/
|
||||
ExprWrapper(Expr* expr);
|
||||
|
||||
/**
|
||||
* Destructor for ExprWrapper
|
||||
**/
|
||||
virtual ~ExprWrapper();
|
||||
|
||||
/**
|
||||
* Evaluates this Expr based on the given context node and processor state
|
||||
* @param context the context node for evaluation of this Expr
|
||||
* @param ps the ContextState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
**/
|
||||
virtual ExprResult* evaluate(Node* context, ContextState* cs);
|
||||
|
||||
/**
|
||||
* Returns the String representation of this Expr.
|
||||
* @param dest the String to use when creating the String
|
||||
* representation. The String representation will be appended to
|
||||
* any data in the destination String, to allow cascading calls to
|
||||
* other #toString() methods for Expressions.
|
||||
* @return the String representation of this Expr.
|
||||
**/
|
||||
virtual void toString(String& str);
|
||||
|
||||
private:
|
||||
|
||||
Expr* expr;
|
||||
|
||||
}; //-- ExprWrapper
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Represents the XPath NodeSet function calls
|
||||
**/
|
||||
|
@ -246,7 +253,7 @@ class NodeSetFunctionCall : public FunctionCall {
|
|||
|
||||
public:
|
||||
|
||||
enum _NodeSetFunctions {
|
||||
enum nodeSetFunctions {
|
||||
COUNT = 1, //-- count()
|
||||
LAST, //-- last()
|
||||
LOCAL_NAME, //-- local-name()
|
||||
|
@ -286,7 +293,7 @@ class StringFunctionCall : public FunctionCall {
|
|||
|
||||
public:
|
||||
|
||||
enum _StringFunctions {
|
||||
enum stringFunctions {
|
||||
CONCAT = 1, //-- concat()
|
||||
CONTAINS, //-- contains()
|
||||
NORMALIZE, //-- normalize()
|
||||
|
@ -331,11 +338,11 @@ class NumberFunctionCall : public FunctionCall {
|
|||
|
||||
public:
|
||||
|
||||
enum _NumberFunctions {
|
||||
enum numberFunctions {
|
||||
NUMBER = 1, //-- number()
|
||||
ROUND, //-- round()
|
||||
FLOOR, //-- floor()
|
||||
CEILING //-- ceiling()
|
||||
ROUND, //-- round()
|
||||
FLOOR, //-- floor()
|
||||
CEILING //-- ceiling()
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
Загрузка…
Ссылка в новой задаче