зеркало из https://github.com/mozilla/gecko-dev.git
Changed directory structure, changed name of XSLProcessor to XSLTProcessor
This commit is contained in:
Родитель
c20cdf2f30
Коммит
ca2e64bd48
|
@ -21,11 +21,11 @@
|
|||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: transformiix.cpp,v 1.3 2005/11/02 07:33:34 kvisco%ziplink.net Exp $
|
||||
* $Id: transformiix.cpp,v 1.4 2005/11/02 07:33:35 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
|
||||
#include "XSLProcessor.h"
|
||||
#include "XSLTProcessor.h"
|
||||
|
||||
//--------------/
|
||||
//- Prototypes -/
|
||||
|
@ -47,11 +47,11 @@ void printUsage();
|
|||
**/
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
XSLProcessor xslProcessor;
|
||||
XSLTProcessor xsltProcessor;
|
||||
|
||||
String copyright("(C) 1999 The MITRE Corporation, Keith Visco, and contributors");
|
||||
cout << xslProcessor.getAppName() << " ";
|
||||
cout << xslProcessor.getAppVersion() << endl;
|
||||
cout << xsltProcessor.getAppName() << " ";
|
||||
cout << xsltProcessor.getAppVersion() << endl;
|
||||
cout << copyright <<endl;
|
||||
|
||||
//-- print banner line
|
||||
|
@ -63,7 +63,7 @@ int main(int argc, char** argv) {
|
|||
|
||||
//-- add ErrorObserver
|
||||
SimpleErrorObserver seo;
|
||||
xslProcessor.addErrorObserver(seo);
|
||||
xsltProcessor.addErrorObserver(seo);
|
||||
|
||||
//-- available flags
|
||||
StringList flags;
|
||||
|
@ -80,11 +80,11 @@ int main(int argc, char** argv) {
|
|||
return 0;
|
||||
}
|
||||
String* xmlFilename = (String*)options.get("i");
|
||||
String* xslFilename = (String*)options.get("s");
|
||||
String* xsltFilename = (String*)options.get("s");
|
||||
String* outFilename = (String*)options.get("o");
|
||||
|
||||
if ( !xmlFilename ) {
|
||||
cout << " missing XML filename."<<endl <<endl;
|
||||
cout << "error: missing XML filename."<<endl <<endl;
|
||||
printUsage();
|
||||
return -1;
|
||||
}
|
||||
|
@ -113,33 +113,33 @@ int main(int argc, char** argv) {
|
|||
resultOutput = &resultFileStream;
|
||||
}
|
||||
//-- process
|
||||
if ( !xslFilename ) {
|
||||
xslProcessor.process(xmlInput, *resultOutput, documentBase);
|
||||
if ( !xsltFilename ) {
|
||||
xsltProcessor.process(xmlInput, *resultOutput, documentBase);
|
||||
}
|
||||
else {
|
||||
//-- open XSL file
|
||||
chars = new char[xslFilename->length()+1];
|
||||
ifstream xslInput(xslFilename->toCharArray(chars), ios::in);
|
||||
//-- open XSLT file
|
||||
chars = new char[xsltFilename->length()+1];
|
||||
ifstream xsltInput(xsltFilename->toCharArray(chars), ios::in);
|
||||
delete chars;
|
||||
xslProcessor.process(xmlInput, xslInput, *resultOutput, documentBase);
|
||||
xsltProcessor.process(xmlInput, xsltInput, *resultOutput, documentBase);
|
||||
}
|
||||
resultFileStream.close();
|
||||
return 0;
|
||||
} //-- main
|
||||
|
||||
void printHelp() {
|
||||
cout << "The following flags are available for use with TransforMiiX -";
|
||||
cout << "The following flags are available for use with Transformiix -";
|
||||
cout<<endl<<endl;
|
||||
cout << "-i filename : The XML file to process" << endl;
|
||||
cout << "-o filename : The Output file to create" << endl;
|
||||
cout << "-s filename : The XSL file to use for processing (Optional)" << endl;
|
||||
cout << "-h : This help screen (Optional)" << endl;
|
||||
cout << "-s filename : The XSLT file to use for processing (Optional)" << endl;
|
||||
cout << "-h : This help screen (Optional)" << endl;
|
||||
cout << endl;
|
||||
}
|
||||
void printUsage() {
|
||||
cout << endl;
|
||||
cout << "usage:";
|
||||
cout << "transfrmx -i xml-file [-s xsl-file] [-o output-file]"<<endl;
|
||||
cout << "transfrmx -i xml-file [-s xslt-file] [-o output-file]"<<endl;
|
||||
cout << endl;
|
||||
cout << "for more infomation use the -h flag"<<endl;
|
||||
} //-- printUsage
|
||||
|
|
|
@ -0,0 +1,139 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txAdditiveExpr.cpp,v 1.1 2005/11/02 07:33:58 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents a AdditiveExpr, an binary expression that
|
||||
* performs an additive operation between it's lvalue and rvalue:<BR/>
|
||||
* + : addition
|
||||
* - : subtraction
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:58 $
|
||||
**/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Creates a new AdditiveExpr using the default operator (ADDITION)
|
||||
**/
|
||||
AdditiveExpr::AdditiveExpr() {
|
||||
this->op = ADDITION;
|
||||
this->leftExpr = 0;
|
||||
this->rightExpr = 0;
|
||||
} //-- AdditiveExpr
|
||||
|
||||
/**
|
||||
* Creates a new AdditiveExpr using the given operator
|
||||
**/
|
||||
AdditiveExpr::AdditiveExpr(Expr* leftExpr, Expr* rightExpr, short op) {
|
||||
this->op = op;
|
||||
this->leftExpr = leftExpr;
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- AdditiveExpr
|
||||
|
||||
AdditiveExpr::~AdditiveExpr() {
|
||||
delete leftExpr;
|
||||
delete rightExpr;
|
||||
} //-- ~AdditiveExpr
|
||||
|
||||
/**
|
||||
* Sets the left side of this AdditiveExpr
|
||||
**/
|
||||
void AdditiveExpr::setLeftExpr(Expr* leftExpr) {
|
||||
this->leftExpr = leftExpr;
|
||||
} //-- setLeftExpr
|
||||
|
||||
/**
|
||||
* Sets the right side of this AdditiveExpr
|
||||
**/
|
||||
void AdditiveExpr::setRightExpr(Expr* rightExpr) {
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- setRightExpr
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* AdditiveExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
|
||||
double rightDbl = Double::NaN;
|
||||
ExprResult* exprRes = 0;
|
||||
|
||||
if ( rightExpr ) {
|
||||
exprRes = rightExpr->evaluate(context, cs);
|
||||
if ( exprRes ) rightDbl = exprRes->numberValue();
|
||||
delete exprRes;
|
||||
}
|
||||
|
||||
double leftDbl = Double::NaN;
|
||||
if ( leftExpr ) {
|
||||
exprRes = leftExpr->evaluate(context, cs);
|
||||
if ( exprRes ) leftDbl = exprRes->numberValue();
|
||||
delete exprRes;
|
||||
}
|
||||
|
||||
double result = 0;
|
||||
|
||||
switch ( op ) {
|
||||
case SUBTRACTION:
|
||||
result = leftDbl - rightDbl;
|
||||
break;
|
||||
default:
|
||||
result = leftDbl + rightDbl;
|
||||
break;
|
||||
}
|
||||
return new NumberResult(result);
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void AdditiveExpr::toString(String& str) {
|
||||
|
||||
if ( leftExpr ) leftExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
switch ( op ) {
|
||||
case SUBTRACTION:
|
||||
str.append(" - ");
|
||||
break;
|
||||
default:
|
||||
str.append(" + ");
|
||||
break;
|
||||
}
|
||||
if ( rightExpr ) rightExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txAttributeValueTemplate.cpp,v 1.1 2005/11/02 07:34:00 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* AttributeValueTemplate
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:34:00 $
|
||||
**/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Create a new AttributeValueTemplate
|
||||
**/
|
||||
AttributeValueTemplate::AttributeValueTemplate() {};
|
||||
|
||||
/**
|
||||
* Default destructor
|
||||
**/
|
||||
AttributeValueTemplate::~AttributeValueTemplate() {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
iter->next(); //advance iterator to allow remove
|
||||
Expr* expr = (Expr*)iter->remove();
|
||||
delete expr;
|
||||
}
|
||||
delete iter;
|
||||
|
||||
} //-- ~AttributeValueTemplate
|
||||
|
||||
/**
|
||||
* Adds the given Expr to this AttributeValueTemplate
|
||||
**/
|
||||
void AttributeValueTemplate::addExpr(Expr* expr) {
|
||||
if (expr) expressions.add(expr);
|
||||
} //-- addExpr
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* AttributeValueTemplate::evaluate(Node* context, ContextState* cs) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
String result;
|
||||
while ( iter->hasNext() ) {
|
||||
Expr* expr = (Expr*)iter->next();
|
||||
ExprResult* exprResult = expr->evaluate(context, cs);
|
||||
exprResult->stringValue(result);
|
||||
delete exprResult;
|
||||
}
|
||||
delete iter;
|
||||
return new StringResult(result);
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void AttributeValueTemplate::toString(String& str) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
str.append('{');
|
||||
Expr* expr = (Expr*)iter->next();
|
||||
expr->toString(str);
|
||||
str.append('}');
|
||||
}
|
||||
delete iter;
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txBooleanExpr.cpp,v 1.1 2005/11/02 07:33:46 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Represents a BooleanExpr, a binary expression that
|
||||
* performs a boolean operation between it's lvalue and rvalue:<BR/>
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:46 $
|
||||
**/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Creates a new BooleanExpr using the default operator (AND)
|
||||
**/
|
||||
BooleanExpr::BooleanExpr() {
|
||||
this->op = AND;
|
||||
this->leftExpr = 0;
|
||||
this->rightExpr = 0;
|
||||
} //-- BooleanExpr
|
||||
|
||||
/**
|
||||
* Creates a new BooleanExpr using the given operator
|
||||
**/
|
||||
BooleanExpr::BooleanExpr(Expr* leftExpr, Expr* rightExpr, short op) {
|
||||
this->op = op;
|
||||
this->leftExpr = leftExpr;
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- BooleanExpr
|
||||
|
||||
BooleanExpr::~BooleanExpr() {
|
||||
delete leftExpr;
|
||||
delete rightExpr;
|
||||
} //-- ~BooleanExpr
|
||||
|
||||
/**
|
||||
* Sets the left side of this AdditiveExpr
|
||||
**/
|
||||
void BooleanExpr::setLeftExpr(Expr* leftExpr) {
|
||||
this->leftExpr = leftExpr;
|
||||
} //-- setLeftExpr
|
||||
|
||||
/**
|
||||
* Sets the right side of this AdditiveExpr
|
||||
**/
|
||||
void BooleanExpr::setRightExpr(Expr* rightExpr) {
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- setRightExpr
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* BooleanExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
|
||||
MBool lval = MB_FALSE;
|
||||
ExprResult* exprRes = 0;
|
||||
if ( leftExpr ) {
|
||||
exprRes = leftExpr->evaluate(context, cs);
|
||||
if ( exprRes ) lval = exprRes->booleanValue();
|
||||
delete exprRes;
|
||||
}
|
||||
|
||||
|
||||
//-- check left expression for early decision
|
||||
if (( op == OR ) && (lval)) return new BooleanResult(MB_TRUE);
|
||||
else if (!lval) return new BooleanResult(MB_FALSE);
|
||||
|
||||
|
||||
MBool rval = MB_FALSE;
|
||||
if ( rightExpr ) {
|
||||
exprRes = rightExpr->evaluate(context, cs);
|
||||
if ( exprRes ) rval = exprRes->booleanValue();
|
||||
delete exprRes;
|
||||
}
|
||||
//-- just use rval, since we already checked lval
|
||||
return new BooleanResult(rval);
|
||||
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void BooleanExpr::toString(String& str) {
|
||||
|
||||
if ( leftExpr ) leftExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
switch ( op ) {
|
||||
case OR:
|
||||
str.append(" or ");
|
||||
break;
|
||||
default:
|
||||
str.append(" and ");
|
||||
break;
|
||||
}
|
||||
if ( rightExpr ) rightExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,103 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txBooleanFunctionCall.cpp,v 1.1 2005/11/02 07:33:34 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "FunctionLib.h"
|
||||
|
||||
/**
|
||||
* Creates a default BooleanFunctionCall, which always evaluates to False
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:34 $
|
||||
**/
|
||||
BooleanFunctionCall::BooleanFunctionCall() : FunctionCall(XPathNames::FALSE_FN) {
|
||||
this->type = FALSE;
|
||||
} //-- BooleanFunctionCall
|
||||
|
||||
/**
|
||||
* Creates a BooleanFunctionCall of the given type
|
||||
**/
|
||||
BooleanFunctionCall::BooleanFunctionCall(short type) : FunctionCall()
|
||||
{
|
||||
switch ( type ) {
|
||||
case BOOLEAN :
|
||||
FunctionCall::setName(XPathNames::BOOLEAN_FN);
|
||||
break;
|
||||
case NOT :
|
||||
FunctionCall::setName(XPathNames::NOT_FN);
|
||||
break;
|
||||
case TRUE :
|
||||
FunctionCall::setName(XPathNames::TRUE_FN);
|
||||
break;
|
||||
default:
|
||||
FunctionCall::setName(XPathNames::FALSE_FN);
|
||||
break;
|
||||
}
|
||||
this->type = type;
|
||||
} //-- BooleanFunctionCall
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* BooleanFunctionCall::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
BooleanResult* result = new BooleanResult();
|
||||
ListIterator* iter = params.iterator();
|
||||
int argc = params.getLength();
|
||||
Expr* param = 0;
|
||||
String err;
|
||||
|
||||
|
||||
switch ( type ) {
|
||||
case BOOLEAN :
|
||||
if ( requireParams(1,1,cs) ) {
|
||||
param = (Expr*)iter->next();
|
||||
ExprResult* exprResult = param->evaluate(context, cs);
|
||||
result->setValue(exprResult->booleanValue());
|
||||
delete exprResult;
|
||||
}
|
||||
break;
|
||||
case NOT :
|
||||
if ( requireParams(1,1,cs) ) {
|
||||
param = (Expr*)iter->next();
|
||||
ExprResult* exprResult = param->evaluate(context, cs);
|
||||
result->setValue(!exprResult->booleanValue());
|
||||
delete exprResult;
|
||||
}
|
||||
break;
|
||||
case TRUE :
|
||||
result->setValue(MB_TRUE);
|
||||
break;
|
||||
default:
|
||||
result->setValue(MB_FALSE);
|
||||
break;
|
||||
}
|
||||
delete iter;
|
||||
return result;
|
||||
} //-- evaluate
|
||||
|
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txBooleanResult.cpp,v 1.1 2005/11/02 07:33:46 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Boolean Expression result
|
||||
* @author <A href="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:46 $
|
||||
**/
|
||||
|
||||
#include "ExprResult.h"
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
**/
|
||||
BooleanResult::BooleanResult() {
|
||||
value = MB_FALSE;
|
||||
} //-- BooleanResult
|
||||
|
||||
BooleanResult::BooleanResult(const BooleanResult& boolResult) {
|
||||
this->value = boolResult.getValue();
|
||||
} //-- BooleanResult
|
||||
|
||||
/**
|
||||
* Creates a new BooleanResult with the value of the given MBool parameter
|
||||
* @param boolean the MBool to use for initialization of this BooleanResult's value
|
||||
**/
|
||||
BooleanResult::BooleanResult(MBool boolean) {
|
||||
this->value = boolean;
|
||||
} //-- BooleanResult
|
||||
|
||||
/**
|
||||
* Returns the value of this BooleanResult
|
||||
* @return the value of this BooleanResult
|
||||
**/
|
||||
MBool BooleanResult::getValue() const {
|
||||
return this->value;
|
||||
} //-- getValue
|
||||
|
||||
/**
|
||||
* Sets the value of this BooleanResult
|
||||
* @param boolean the MBool to use for this BooleanResult's value
|
||||
**/
|
||||
void BooleanResult::setValue(MBool boolean) {
|
||||
this->value = boolean;
|
||||
} //-- setValue
|
||||
|
||||
/**
|
||||
* Sets the value of this BooleanResult
|
||||
* @param boolResult the BooleanResult to use for setting this BooleanResult's value
|
||||
**/
|
||||
void BooleanResult::setValue(const BooleanResult& boolResult) {
|
||||
this->value = boolResult.getValue();
|
||||
} //-- setValue
|
||||
|
||||
/*
|
||||
* Virtual Methods from ExprResult
|
||||
*/
|
||||
|
||||
short BooleanResult::getResultType() {
|
||||
return ExprResult::BOOLEAN;
|
||||
} //-- getResultType
|
||||
|
||||
void BooleanResult::stringValue(String& str) {
|
||||
if ( value ) str.append("true");
|
||||
else str.append("false");
|
||||
} //-- toString
|
||||
|
||||
MBool BooleanResult::booleanValue() {
|
||||
return this->value;
|
||||
} //-- toBoolean
|
||||
|
||||
double BooleanResult::numberValue() {
|
||||
return ( value ) ? 1.0 : 0.0;
|
||||
} //-- toNumber
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,698 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
* -- fixed bug with '<=' and '>=' reported by Bob Miller
|
||||
*
|
||||
* Bob Miller, Oblix Inc., kbob@oblix.com
|
||||
* -- fixed bug with single quotes inside double quotes
|
||||
*
|
||||
* $Id: txExprLexer.cpp,v 1.1 2005/11/02 07:33:40 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Lexical analyzer for XPath expressions
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:40 $
|
||||
**/
|
||||
|
||||
#include <iostream.h>
|
||||
#include "ExprLexer.h"
|
||||
|
||||
//---------------------------/
|
||||
//- Implementation of Token -/
|
||||
//---------------------------/
|
||||
|
||||
|
||||
/**
|
||||
* Default constructor for Token
|
||||
**/
|
||||
Token::Token() {
|
||||
this->type =0;
|
||||
} //-- Token;
|
||||
|
||||
/**
|
||||
* Constructor for Token
|
||||
* @param type, the type of Token being represented
|
||||
**/
|
||||
Token::Token(short type) {
|
||||
this->type = type;
|
||||
} //-- Token;
|
||||
|
||||
/**
|
||||
* Constructor for Token
|
||||
* @param value the value of this Token
|
||||
* @param type, the type of Token being represented
|
||||
**/
|
||||
Token::Token(const String& value, short type) {
|
||||
this->type = type;
|
||||
//-- make copy of value String
|
||||
this->value = value;
|
||||
} //-- Token
|
||||
|
||||
Token::Token(UNICODE_CHAR uniChar, short type) {
|
||||
this->type = type;
|
||||
this->value.append(uniChar);
|
||||
} //-- Token
|
||||
|
||||
/**
|
||||
* Copy Constructor
|
||||
**/
|
||||
Token::Token(const Token& token) {
|
||||
this->type = token.type;
|
||||
this->value = token.value;
|
||||
} //-- Token
|
||||
|
||||
/**
|
||||
* Destructor for Token
|
||||
**/
|
||||
Token::~Token() {
|
||||
//-- currently nothing is needed
|
||||
} //-- ~Token
|
||||
|
||||
|
||||
//--------------------------------/
|
||||
//- Implementation of ExprLexer -/
|
||||
//-------------------------------/
|
||||
|
||||
/*
|
||||
* Complex Tokens
|
||||
*/
|
||||
//-- Nodetype tokens
|
||||
const String ExprLexer::COMMENT = "comment";
|
||||
const String ExprLexer::NODE = "node";
|
||||
const String ExprLexer::PI = "processing-instruction";
|
||||
const String ExprLexer::TEXT = "text";
|
||||
|
||||
//-- boolean
|
||||
const String ExprLexer::AND = "and";
|
||||
const String ExprLexer::OR = "or";
|
||||
|
||||
//-- multiplicative operators
|
||||
const String ExprLexer::MODULUS = "mod";
|
||||
const String ExprLexer::DIVIDE = "div";
|
||||
|
||||
|
||||
/**
|
||||
* The set of a XSL Expression Tokens
|
||||
**/
|
||||
const Token ExprLexer::TOKENS[] = {
|
||||
|
||||
//-- Nodetype tokens
|
||||
Token(ExprLexer::COMMENT, Token::COMMENT),
|
||||
Token(ExprLexer::NODE, Token::NODE),
|
||||
Token(ExprLexer::PI, Token::PI),
|
||||
Token(ExprLexer::TEXT, Token::TEXT),
|
||||
//-- boolean operators
|
||||
Token(ExprLexer::AND, Token::AND_OP),
|
||||
Token(ExprLexer::OR, Token::OR_OP),
|
||||
|
||||
//-- multiplicative operators
|
||||
Token(ExprLexer::MODULUS, Token::MODULUS_OP),
|
||||
Token(ExprLexer::DIVIDE, Token::DIVIDE_OP)
|
||||
};
|
||||
|
||||
const short ExprLexer::NUMBER_OF_TOKENS = 8;
|
||||
|
||||
//---------------/
|
||||
//- Contructors -/
|
||||
//---------------/
|
||||
|
||||
/**
|
||||
* Creates a new ExprLexer using the given String
|
||||
**/
|
||||
ExprLexer::ExprLexer(const String& pattern) {
|
||||
|
||||
firstItem = 0;
|
||||
lastItem = 0;
|
||||
tokenCount = 0;
|
||||
prevToken = 0;
|
||||
parse(pattern);
|
||||
currentItem = firstItem;
|
||||
} //-- ExprLexer
|
||||
|
||||
/**
|
||||
* Destroys this instance of an ExprLexer
|
||||
**/
|
||||
ExprLexer::~ExprLexer() {
|
||||
//-- delete tokens
|
||||
|
||||
//cout << "~ExprLexer() - start"<<endl;
|
||||
currentItem = firstItem;
|
||||
while ( currentItem ) {
|
||||
TokenListItem* temp = currentItem->next;
|
||||
//cout << "deleting token: " << currentItem->token->value << endl;
|
||||
delete currentItem->token;
|
||||
delete currentItem;
|
||||
currentItem = temp;
|
||||
}
|
||||
//cout << "~ExprLexer() - done"<<endl;
|
||||
} //-- ~ExprLexer
|
||||
|
||||
|
||||
int ExprLexer::countAllTokens() {
|
||||
return tokenCount;
|
||||
} //-- countAllTokens
|
||||
|
||||
int ExprLexer::countRemainingTokens() {
|
||||
TokenListItem* temp = currentItem;
|
||||
int c = 0;
|
||||
while ( temp ) {
|
||||
++c;
|
||||
temp = temp->next;
|
||||
}
|
||||
return c;
|
||||
} //-- countRemainingTokens
|
||||
|
||||
|
||||
MBool ExprLexer::hasMoreTokens() {
|
||||
return (MBool) ( currentItem );
|
||||
} //-- hasMoreTokens
|
||||
|
||||
Token* ExprLexer::nextToken() {
|
||||
if ( currentItem ) {
|
||||
Token* token = currentItem->token;
|
||||
currentItem = currentItem->next;
|
||||
return token;
|
||||
}
|
||||
return 0;
|
||||
} //-- nextToken
|
||||
|
||||
void ExprLexer::pushBack() {
|
||||
if ( !currentItem ) {
|
||||
currentItem = lastItem;
|
||||
}
|
||||
else currentItem = currentItem->previous;
|
||||
} //-- pushBack
|
||||
|
||||
/*
|
||||
Token* ExprLexer::lastToken() {
|
||||
if (lastItem) {
|
||||
return lastItem->token;
|
||||
}
|
||||
return 0;
|
||||
} //-- lastToken
|
||||
*/
|
||||
|
||||
Token* ExprLexer::peek() {
|
||||
Token* token = 0;
|
||||
TokenListItem* tlItem = currentItem;
|
||||
if (tlItem) token = tlItem->token;
|
||||
return token;
|
||||
} //-- peek
|
||||
|
||||
Token* ExprLexer::lookAhead(int offset) {
|
||||
Token* token = 0;
|
||||
TokenListItem* tlItem = currentItem;
|
||||
//-- advance to proper offset
|
||||
for ( int i = 0; i < offset; i++ )
|
||||
if ( tlItem ) tlItem = currentItem->next;
|
||||
|
||||
if (tlItem) token = tlItem->token;
|
||||
return token;
|
||||
} //-- lookAhead
|
||||
|
||||
void ExprLexer::addToken(Token* token) {
|
||||
TokenListItem* tlItem = new TokenListItem;
|
||||
tlItem->token = token;
|
||||
tlItem->next = 0;
|
||||
if (lastItem) {
|
||||
tlItem->previous = lastItem;
|
||||
lastItem->next = tlItem;
|
||||
}
|
||||
if (!firstItem) firstItem = tlItem;
|
||||
lastItem = tlItem;
|
||||
prevToken = token;
|
||||
++tokenCount;
|
||||
} //-- addToken
|
||||
|
||||
/**
|
||||
* Returns true if the given character represents an Alpha letter
|
||||
**/
|
||||
MBool ExprLexer::isAlphaChar(Int32 ch) {
|
||||
if ((ch >= 'a' ) && (ch <= 'z' )) return MB_TRUE;
|
||||
if ((ch >= 'A' ) && (ch <= 'Z' )) return MB_TRUE;
|
||||
return MB_FALSE;
|
||||
} //-- isAlphaChar
|
||||
|
||||
/**
|
||||
* Returns true if the given character represents a numeric letter (digit)
|
||||
**/
|
||||
MBool ExprLexer::isDigit(Int32 ch) {
|
||||
if ((ch >= '0') && (ch <= '9')) return MB_TRUE;
|
||||
return MB_FALSE;
|
||||
} //-- isDigit
|
||||
|
||||
/**
|
||||
* Returns true if the given character is an allowable QName character
|
||||
**/
|
||||
MBool ExprLexer::isNCNameChar(Int32 ch) {
|
||||
if (isDigit(ch) || isAlphaChar(ch)) return MB_TRUE;
|
||||
return (MBool) ((ch == '.') ||(ch == '_') || (ch == '-'));
|
||||
} //-- isNCNameChar
|
||||
|
||||
/**
|
||||
* Returns true if the given character is an allowable NCName character
|
||||
**/
|
||||
MBool ExprLexer::isQNameChar(Int32 ch) {
|
||||
return (MBool) (( ch == ':') || isNCNameChar(ch));
|
||||
} //-- isQNameChar
|
||||
|
||||
/**
|
||||
* Returns true if the given String is a valid XML QName
|
||||
**/
|
||||
MBool ExprLexer::isValidQName(String& name) {
|
||||
|
||||
int size = name.length();
|
||||
if ( size == 0 ) return MB_FALSE;
|
||||
else if ( !isAlphaChar(name.charAt(0))) return MB_FALSE;
|
||||
else {
|
||||
for ( int i = 1; i < size; i++) {
|
||||
if ( ! isQNameChar(name.charAt(i))) return MB_FALSE;
|
||||
}
|
||||
}
|
||||
return MB_TRUE;
|
||||
} //-- isValidQName
|
||||
|
||||
MBool ExprLexer::isOperatorToken(Token* token) {
|
||||
if ( !token ) return MB_FALSE;
|
||||
switch ( token->type ) {
|
||||
//-- boolean operators
|
||||
case Token::AND_OP:
|
||||
case Token::OR_OP:
|
||||
//-- relational operators
|
||||
case Token::EQUAL_OP:
|
||||
case Token::NOT_EQUAL_OP:
|
||||
case Token::LESS_THAN_OP:
|
||||
case Token::GREATER_THAN_OP:
|
||||
case Token::LESS_OR_EQUAL_OP:
|
||||
case Token::GREATER_OR_EQUAL_OP:
|
||||
//-- additive operators
|
||||
case Token::ADDITION_OP:
|
||||
case Token::SUBTRACTION_OP:
|
||||
//-- multiplicative operators
|
||||
case Token::DIVIDE_OP:
|
||||
case Token::MODULUS_OP:
|
||||
case Token::MULTIPLY_OP:
|
||||
return MB_TRUE;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return MB_FALSE;
|
||||
} //-- isOperatorToken
|
||||
|
||||
MBool ExprLexer::matchDelimiter(UNICODE_CHAR ch) {
|
||||
|
||||
short tokenType = 0;
|
||||
MBool addChar = MB_TRUE;
|
||||
switch (ch) {
|
||||
case FORWARD_SLASH :
|
||||
tokenType = Token::PARENT_OP;
|
||||
break;
|
||||
case L_PAREN :
|
||||
tokenType = Token::L_PAREN;
|
||||
break;
|
||||
case R_PAREN :
|
||||
tokenType = Token::R_PAREN;
|
||||
break;
|
||||
case L_BRACKET :
|
||||
tokenType = Token::L_BRACKET;
|
||||
break;
|
||||
case R_BRACKET :
|
||||
tokenType = Token::R_BRACKET;
|
||||
break;
|
||||
case L_ANGLE :
|
||||
tokenType = Token::LESS_THAN_OP;
|
||||
break;
|
||||
case R_ANGLE :
|
||||
tokenType = Token::GREATER_THAN_OP;
|
||||
break;
|
||||
case COMMA :
|
||||
tokenType = Token::COMMA;
|
||||
break;
|
||||
case PERIOD :
|
||||
tokenType = Token::SELF_NODE;
|
||||
break;
|
||||
case EQUAL :
|
||||
tokenType = Token::EQUAL_OP;
|
||||
break;
|
||||
case PLUS :
|
||||
tokenType = Token::ADDITION_OP;
|
||||
break;
|
||||
case HYPHEN :
|
||||
tokenType = Token::SUBTRACTION_OP;
|
||||
break;
|
||||
case VERT_BAR:
|
||||
tokenType = Token::UNION_OP;
|
||||
break;
|
||||
case ASTERIX:
|
||||
tokenType = Token::WILD_CARD;
|
||||
break;
|
||||
case AT_SIGN:
|
||||
tokenType = Token::AT_SIGN;
|
||||
break;
|
||||
case DOLLAR_SIGN:
|
||||
tokenType = Token::VAR_REFERENCE;
|
||||
addChar = MB_FALSE;
|
||||
break;
|
||||
default:
|
||||
return MB_FALSE;;
|
||||
}
|
||||
Token* token = 0;
|
||||
if ( addChar ) token = new Token(ch, tokenType);
|
||||
else token = new Token(tokenType);
|
||||
|
||||
addToken(token);
|
||||
return MB_TRUE;
|
||||
} //-- matchDelimiter
|
||||
|
||||
/**
|
||||
* Returns true if the value of the given String matches
|
||||
* an OperatorName
|
||||
**/
|
||||
MBool ExprLexer::matchesOperator(String& buffer) {
|
||||
|
||||
int index = 0;
|
||||
while (index < NUMBER_OF_TOKENS) {
|
||||
Token tok = TOKENS[index++];
|
||||
if ( tok.value.isEqual(buffer) ) {
|
||||
if (isOperatorToken( &tok )) return MB_TRUE;
|
||||
}
|
||||
}
|
||||
return MB_FALSE;
|
||||
|
||||
} //-- matchesOperator
|
||||
|
||||
/**
|
||||
* Matches the given String to the appropriate Token
|
||||
* @param buffer the current StringBuffer representing the value of the Token
|
||||
* @param ch, the current delimiter token
|
||||
**/
|
||||
void ExprLexer::matchToken(String& buffer, UNICODE_CHAR ch) {
|
||||
|
||||
if ( buffer.length() == 0) return;
|
||||
|
||||
Token* match = new Token();
|
||||
MBool foundMatch = MB_FALSE;
|
||||
int index = 0;
|
||||
|
||||
//-- check previous token
|
||||
switch(prevToken->type) {
|
||||
case Token::VAR_REFERENCE :
|
||||
if ( prevToken->value.length() == 0) {
|
||||
prevToken->value.append(buffer);
|
||||
buffer.clear();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
//-- look for next match
|
||||
while ( !foundMatch && (index < NUMBER_OF_TOKENS) ) {
|
||||
|
||||
Token tok = TOKENS[index++];
|
||||
|
||||
if ( tok.value.isEqual(buffer) ) {
|
||||
|
||||
foundMatch = MB_TRUE;
|
||||
|
||||
switch (tok.type) {
|
||||
|
||||
//-- NodeType tokens
|
||||
case Token::COMMENT:
|
||||
case Token::NODE :
|
||||
case Token::PI :
|
||||
case Token::TEXT :
|
||||
// make sure next delimiter is '('
|
||||
if ( ch == L_PAREN) {
|
||||
//-- copy buffer
|
||||
match->value = buffer;
|
||||
//-- copy type
|
||||
match->type = tok.type;
|
||||
}
|
||||
break;
|
||||
case Token::MULTIPLY_OP :
|
||||
case Token::DIVIDE_OP:
|
||||
case Token::MODULUS_OP:
|
||||
switch ( prevToken->type ) {
|
||||
case Token::AT_SIGN :
|
||||
case Token::NULL_TOKEN:
|
||||
case Token::L_PAREN:
|
||||
case Token::L_BRACKET:
|
||||
foundMatch = MB_FALSE;
|
||||
break; //-- do not match
|
||||
default:
|
||||
if ( isOperatorToken(prevToken) ) {
|
||||
foundMatch = MB_FALSE;
|
||||
break; //-- do not match
|
||||
}
|
||||
match->value = buffer;
|
||||
match->type = tok.type;
|
||||
}
|
||||
break;
|
||||
default :
|
||||
//-- copy buffer
|
||||
match->value = buffer;
|
||||
match->type = tok.type;
|
||||
break;
|
||||
}
|
||||
} //-- if equal
|
||||
} //-- while
|
||||
|
||||
if (!foundMatch) {
|
||||
//-- copy buffer
|
||||
match->value = buffer;
|
||||
//-- look for function name
|
||||
if ( ch == L_PAREN) match->type = Token::FUNCTION_NAME;
|
||||
else match->type = Token::CNAME;
|
||||
}
|
||||
addToken(match);
|
||||
buffer.clear();
|
||||
} //-- matchToken
|
||||
|
||||
/**
|
||||
* Parses the given String into the set of Tokens
|
||||
**/
|
||||
void ExprLexer::parse(const String& pattern) {
|
||||
|
||||
|
||||
String tokenBuffer;
|
||||
UNICODE_CHAR inLiteral = '\0';
|
||||
MBool inNumber = MB_FALSE;
|
||||
|
||||
Int32 currentPos = 0;
|
||||
|
||||
UNICODE_CHAR ch = '\0';
|
||||
UNICODE_CHAR prevCh = ch;
|
||||
|
||||
//-- initialize previous token, this will automatically get
|
||||
//-- deleted when it goes out of scope
|
||||
Token nullToken('\0', Token::NULL_TOKEN);
|
||||
|
||||
prevToken = &nullToken;
|
||||
|
||||
while (currentPos < pattern.length()) {
|
||||
|
||||
prevCh = ch;
|
||||
ch = pattern.charAt(currentPos);
|
||||
|
||||
if ( inLiteral ) {
|
||||
//-- look for end of literal
|
||||
if ( ch == inLiteral ) {
|
||||
inLiteral = '\0';
|
||||
addToken(new Token(tokenBuffer, Token::LITERAL));
|
||||
tokenBuffer.clear();
|
||||
}
|
||||
else {
|
||||
tokenBuffer.append(ch);
|
||||
}
|
||||
}
|
||||
else if ( inNumber ) {
|
||||
if (isDigit(ch) || (ch == '.')) {
|
||||
tokenBuffer.append(ch);
|
||||
}
|
||||
else {
|
||||
inNumber = MB_FALSE;
|
||||
addToken(new Token(tokenBuffer, Token::NUMBER));
|
||||
tokenBuffer.clear();
|
||||
//-- push back last char
|
||||
--currentPos;
|
||||
}
|
||||
}
|
||||
else if (isDigit(ch)) {
|
||||
if ((tokenBuffer.length() == 0 ) || matchesOperator(tokenBuffer) ) {
|
||||
//-- match operator and free up token buffer
|
||||
matchToken(tokenBuffer, ch);
|
||||
inNumber = MB_TRUE;
|
||||
}
|
||||
else if (( tokenBuffer.length() == 1 ) && (prevCh = '-')) {
|
||||
inNumber = MB_TRUE;
|
||||
}
|
||||
tokenBuffer.append(ch);
|
||||
}
|
||||
else {
|
||||
switch (ch) {
|
||||
//-- ignore whitespace
|
||||
case SPACE:
|
||||
case TAB:
|
||||
case CR:
|
||||
case LF:
|
||||
break;
|
||||
case S_QUOTE :
|
||||
case D_QUOTE :
|
||||
matchToken(tokenBuffer, ch);
|
||||
inLiteral = ch;
|
||||
break;
|
||||
case PERIOD:
|
||||
if ( inNumber ) tokenBuffer.append(ch);
|
||||
else if ( prevToken->type == Token::SELF_NODE ) {
|
||||
prevToken->type = Token::PARENT_NODE;
|
||||
}
|
||||
else if ( tokenBuffer.length() > 0 )
|
||||
tokenBuffer.append(ch);
|
||||
else matchDelimiter(ch);
|
||||
break;
|
||||
case COLON:
|
||||
if ( prevCh == ch) {
|
||||
Int32 bufSize = tokenBuffer.length();
|
||||
tokenBuffer.setLength(bufSize-1);
|
||||
addToken(new Token(tokenBuffer, Token::AXIS_IDENTIFIER));
|
||||
tokenBuffer.clear();
|
||||
}
|
||||
else tokenBuffer.append(ch);
|
||||
break;
|
||||
case FORWARD_SLASH :
|
||||
matchToken(tokenBuffer, ch);
|
||||
if ( prevToken->type == Token::PARENT_OP ) {
|
||||
prevToken->type = Token::ANCESTOR_OP;
|
||||
prevToken->value.append(ch);
|
||||
}
|
||||
else matchDelimiter(ch);
|
||||
break;
|
||||
case BANG : //-- used as previous...see EQUAL
|
||||
matchToken(tokenBuffer,ch);
|
||||
addToken(new Token(ch, Token::ERROR));
|
||||
break;
|
||||
case EQUAL:
|
||||
switch ( prevCh ) {
|
||||
case BANG:
|
||||
prevToken->type = Token::NOT_EQUAL_OP;
|
||||
prevToken->value.append("=");
|
||||
break;
|
||||
case L_ANGLE:
|
||||
prevToken->type = Token::LESS_OR_EQUAL_OP;
|
||||
prevToken->value.append("=");
|
||||
break;
|
||||
case R_ANGLE:
|
||||
prevToken->type = Token::GREATER_OR_EQUAL_OP;
|
||||
prevToken->value.append("=");
|
||||
break;
|
||||
default:
|
||||
matchToken(tokenBuffer, ch);
|
||||
matchDelimiter(ch);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case L_ANGLE :
|
||||
case R_ANGLE :
|
||||
matchToken(tokenBuffer, ch);
|
||||
matchDelimiter(ch);
|
||||
break;
|
||||
case HYPHEN :
|
||||
if ( isValidQName(tokenBuffer) ) tokenBuffer.append(ch);
|
||||
else {
|
||||
switch ( prevToken->type ) {
|
||||
case Token::NULL_TOKEN:
|
||||
case Token::L_PAREN:
|
||||
case Token::L_BRACKET:
|
||||
case Token::COMMA:
|
||||
inNumber = MB_TRUE;
|
||||
tokenBuffer.append(ch);
|
||||
break;
|
||||
default:
|
||||
matchToken(tokenBuffer, ch);
|
||||
matchDelimiter(ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ASTERIX:
|
||||
matchToken(tokenBuffer, ch);
|
||||
switch ( prevToken->type ) {
|
||||
case Token::PARENT_OP :
|
||||
case Token::ANCESTOR_OP:
|
||||
case Token::AT_SIGN :
|
||||
case Token::NULL_TOKEN:
|
||||
case Token::L_PAREN:
|
||||
case Token::L_BRACKET:
|
||||
matchDelimiter(ch);
|
||||
break;
|
||||
default:
|
||||
if ( isOperatorToken(prevToken) ) {
|
||||
matchDelimiter(ch);
|
||||
break; //-- do not match
|
||||
}
|
||||
addToken( new Token(ch, Token::MULTIPLY_OP) );
|
||||
}
|
||||
break;
|
||||
case L_PAREN:
|
||||
case R_PAREN:
|
||||
case L_BRACKET:
|
||||
case R_BRACKET:
|
||||
case COMMA:
|
||||
case AT_SIGN :
|
||||
case PLUS:
|
||||
case DOLLAR_SIGN :
|
||||
case VERT_BAR:
|
||||
matchToken(tokenBuffer, ch);
|
||||
matchDelimiter(ch);
|
||||
break;
|
||||
default:
|
||||
switch (prevCh) {
|
||||
case SPACE :
|
||||
case TAB :
|
||||
case CR :
|
||||
case LF :
|
||||
matchToken(tokenBuffer, ch);
|
||||
tokenBuffer.append(ch);
|
||||
break;
|
||||
default:
|
||||
tokenBuffer.append(ch);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
++currentPos;
|
||||
}
|
||||
//-- end lexical parsing of current token
|
||||
//-- freeBuffer if needed
|
||||
|
||||
if ( inNumber ) {
|
||||
addToken(new Token(tokenBuffer, Token::NUMBER));
|
||||
}
|
||||
else matchToken(tokenBuffer, ch);
|
||||
prevToken = 0;
|
||||
} //-- parse
|
||||
|
|
@ -0,0 +1,291 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* Larry Fitzpatrick
|
||||
* -- changed constant short declarations in Token and ExprLexer to
|
||||
* enumerations, commented with //--LF
|
||||
*
|
||||
* $Id: txExprLexer.h,v 1.1 2005/11/02 07:33:49 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
|
||||
#ifndef MITREXSL_EXPRLEXER_H
|
||||
#define MITREXSL_EXPRLEXER_H
|
||||
|
||||
#include "String.h"
|
||||
#include "baseutils.h"
|
||||
#include <iostream.h>
|
||||
|
||||
/**
|
||||
* A Token class for the ExprLexer.
|
||||
* <BR />
|
||||
* This class was ported from XSL:P, an open source Java based
|
||||
* XSLT processor, written by yours truly.
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:49 $
|
||||
**/
|
||||
class Token {
|
||||
|
||||
public:
|
||||
|
||||
//---------------/
|
||||
//- Token Types -/
|
||||
//---------------/
|
||||
|
||||
//-- LF - changed from static const short declarations to enum
|
||||
//-- token types
|
||||
enum TokenType {
|
||||
//-- Trivial Tokens
|
||||
ERROR = 0,
|
||||
NULL_TOKEN,
|
||||
LITERAL,
|
||||
NUMBER,
|
||||
CNAME,
|
||||
L_PAREN,
|
||||
R_PAREN,
|
||||
L_BRACKET,
|
||||
R_BRACKET,
|
||||
COMMA,
|
||||
FUNCTION_NAME,
|
||||
WILD_CARD,
|
||||
AT_SIGN,
|
||||
VAR_REFERENCE,
|
||||
PARENT_NODE,
|
||||
SELF_NODE,
|
||||
AXIS_IDENTIFIER,
|
||||
//-------------/
|
||||
//- operators -/
|
||||
//-------------/
|
||||
|
||||
//-- boolean ops
|
||||
AND_OP,
|
||||
OR_OP,
|
||||
|
||||
//-- relational
|
||||
EQUAL_OP,
|
||||
NOT_EQUAL_OP,
|
||||
LESS_THAN_OP,
|
||||
GREATER_THAN_OP,
|
||||
LESS_OR_EQUAL_OP,
|
||||
GREATER_OR_EQUAL_OP,
|
||||
//-- additive operators
|
||||
ADDITION_OP,
|
||||
SUBTRACTION_OP,
|
||||
//-- multiplicative
|
||||
DIVIDE_OP ,
|
||||
MULTIPLY_OP,
|
||||
MODULUS_OP,
|
||||
//-- path operators
|
||||
PARENT_OP,
|
||||
ANCESTOR_OP,
|
||||
UNION_OP,
|
||||
//-- node type tokens
|
||||
COMMENT,
|
||||
NODE,
|
||||
PI,
|
||||
TEXT
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
**/
|
||||
Token();
|
||||
Token(short type);
|
||||
Token(const String& value, short type);
|
||||
Token(UNICODE_CHAR uniChar, short type);
|
||||
/**
|
||||
* Copy Constructor
|
||||
**/
|
||||
Token(const Token& token);
|
||||
|
||||
~Token();
|
||||
String value;
|
||||
short type;
|
||||
}; //--Token
|
||||
|
||||
/**
|
||||
* A class for splitting an "Expr" String into tokens and
|
||||
* performing basic Lexical Analysis.
|
||||
*
|
||||
* <BR>This class was ported from XSL:P, an open source Java based XSL processor
|
||||
* @author <a href="mailto:kvisco@mitre.org">Keith Visco</a>
|
||||
**/
|
||||
class ExprLexer {
|
||||
|
||||
|
||||
public:
|
||||
|
||||
/*
|
||||
* Trivial Tokens
|
||||
*/
|
||||
//-- LF, changed to enum
|
||||
enum _TrivialTokens {
|
||||
D_QUOTE = '\"',
|
||||
S_QUOTE = '\'',
|
||||
L_PAREN = '(',
|
||||
R_PAREN = ')',
|
||||
L_BRACKET = '[',
|
||||
R_BRACKET = ']',
|
||||
L_ANGLE = '<',
|
||||
R_ANGLE = '>',
|
||||
COMMA = ',',
|
||||
PERIOD = '.',
|
||||
ASTERIX = '*',
|
||||
FORWARD_SLASH = '/',
|
||||
EQUAL = '=',
|
||||
BANG = '!',
|
||||
VERT_BAR = '|',
|
||||
AT_SIGN = '@',
|
||||
DOLLAR_SIGN = '$',
|
||||
PLUS = '+',
|
||||
HYPHEN = '-',
|
||||
COLON = ':',
|
||||
//-- whitespace tokens
|
||||
SPACE = ' ',
|
||||
TAB = '\t',
|
||||
CR = '\n',
|
||||
LF = '\r'
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* Complex Tokens
|
||||
*/
|
||||
//-- Nodetype tokens
|
||||
static const String COMMENT;
|
||||
static const String NODE;
|
||||
static const String PI;
|
||||
static const String TEXT;
|
||||
|
||||
//-- boolean
|
||||
static const String AND;
|
||||
static const String OR;
|
||||
|
||||
//-- Multiplicative
|
||||
static const String MODULUS;
|
||||
static const String DIVIDE;
|
||||
|
||||
/*
|
||||
* Default Token Set
|
||||
*/
|
||||
static const Token TOKENS[];
|
||||
static const short NUMBER_OF_TOKENS;
|
||||
|
||||
/**
|
||||
* Constructor for ExprLexer
|
||||
**/
|
||||
ExprLexer(const String& pattern);
|
||||
|
||||
~ExprLexer();
|
||||
|
||||
/**
|
||||
* Counts the total number of tokens in this Lexer, even if the token
|
||||
* has already been seen
|
||||
* @return the total number of tokens in this Lexer
|
||||
**/
|
||||
int countAllTokens();
|
||||
|
||||
/**
|
||||
* Counts the remaining number of tokens in this Lexer
|
||||
* @return the number of remaining tokens in this Lexer
|
||||
**/
|
||||
int countRemainingTokens();
|
||||
|
||||
/**
|
||||
* Returns the type of token that was last return by a call to nextToken
|
||||
**/
|
||||
|
||||
Token* lookAhead(int offset);
|
||||
Token* nextToken();
|
||||
Token* peek();
|
||||
void pushBack();
|
||||
MBool hasMoreTokens();
|
||||
|
||||
MBool isOperatorToken(Token* token);
|
||||
|
||||
private:
|
||||
|
||||
struct TokenListItem {
|
||||
Token* token;
|
||||
TokenListItem* next;
|
||||
TokenListItem* previous;
|
||||
};
|
||||
|
||||
TokenListItem* currentItem;
|
||||
TokenListItem* firstItem;
|
||||
TokenListItem* lastItem;
|
||||
|
||||
int tokenCount;
|
||||
|
||||
Token* prevToken;
|
||||
|
||||
void addToken(Token* token);
|
||||
|
||||
/**
|
||||
* Returns true if the given character represents an Alpha letter
|
||||
**/
|
||||
static MBool isAlphaChar(Int32 ch);
|
||||
|
||||
/**
|
||||
* Returns true if the given character represents a numeric letter (digit)
|
||||
**/
|
||||
static MBool isDigit(Int32 ch);
|
||||
|
||||
/**
|
||||
* Returns true if the given character is an allowable QName character
|
||||
**/
|
||||
static MBool isQNameChar(Int32 ch);
|
||||
|
||||
/**
|
||||
* Returns true if the given character is an allowable NCName character
|
||||
**/
|
||||
static MBool isNCNameChar(Int32 ch);
|
||||
|
||||
/**
|
||||
* Returns true if the given String is a valid XML QName
|
||||
**/
|
||||
static MBool isValidQName(String& name);
|
||||
|
||||
MBool matchDelimiter(UNICODE_CHAR ch);
|
||||
|
||||
/**
|
||||
* Returns true if the value of the given String matches
|
||||
* an OperatorName
|
||||
**/
|
||||
MBool matchesOperator(String& buffer);
|
||||
|
||||
/**
|
||||
* Matches the given String to the appropriate Token
|
||||
* @param buffer the current StringBuffer representing the value of the Token
|
||||
* @param ch, the current delimiter token
|
||||
**/
|
||||
void matchToken(String& buffer, UNICODE_CHAR ch);
|
||||
|
||||
|
||||
void parse(const String& pattern);
|
||||
|
||||
}; //-- ExprLexer
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,905 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
* Olivier Gerardin, ogerardin@vo.lu
|
||||
* -- fixed a bug in CreateExpr (@xxx=/yyy was parsed as
|
||||
* @xxx=@xxx/yyy)
|
||||
*
|
||||
* $Id: txExprParser.cpp,v 1.1 2005/11/02 07:33:25 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* ExprParser
|
||||
* This class is used to parse XSL Expressions
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @see ExprLexer
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:25 $
|
||||
**/
|
||||
|
||||
#include "ExprParser.h"
|
||||
|
||||
const String ExprParser::L_CURLY_BRACE = "{";
|
||||
const String ExprParser::R_CURLY_BRACE = "}";
|
||||
|
||||
/**
|
||||
* Creates a new ExprParser
|
||||
**/
|
||||
ExprParser::ExprParser() {};
|
||||
|
||||
/**
|
||||
* Default Destructor
|
||||
**/
|
||||
ExprParser::~ExprParser() {};
|
||||
|
||||
/**
|
||||
* Creates an Attribute Value Template using the given value
|
||||
**/
|
||||
AttributeValueTemplate* ExprParser::createAttributeValueTemplate
|
||||
(const String& attValue)
|
||||
{
|
||||
|
||||
AttributeValueTemplate* avt = new AttributeValueTemplate();
|
||||
Int32 size = attValue.length();
|
||||
int cc = 0;
|
||||
String buffer;
|
||||
MBool inExpr = MB_FALSE;
|
||||
MBool inLiteral = MB_FALSE;
|
||||
char endLiteral = '"';
|
||||
char prevCh = '\0';
|
||||
|
||||
while ( cc < size) {
|
||||
UNICODE_CHAR ch = attValue.charAt(cc++);
|
||||
// if in literal just add ch to buffer
|
||||
if ( inLiteral && (ch != endLiteral) ) {
|
||||
buffer.append(ch);
|
||||
prevCh = ch;
|
||||
continue;
|
||||
}
|
||||
switch ( ch ) {
|
||||
case '\'' :
|
||||
case '"' :
|
||||
buffer.append(ch);
|
||||
if (inLiteral) inLiteral = MB_FALSE;
|
||||
else {
|
||||
inLiteral = MB_TRUE;
|
||||
endLiteral = ch;
|
||||
}
|
||||
break;
|
||||
case '{' :
|
||||
// Ignore case where we find two { without a }
|
||||
if (!inExpr) {
|
||||
//-- clear buffer
|
||||
if ( buffer.length() > 0) {
|
||||
avt->addExpr(new StringExpr(buffer));
|
||||
buffer.clear();
|
||||
}
|
||||
inExpr = MB_TRUE;
|
||||
}
|
||||
else if (prevCh == ch) {
|
||||
inExpr = MB_FALSE;
|
||||
buffer.append(ch);
|
||||
}
|
||||
else {
|
||||
buffer.append(ch); //-- simply append '{'
|
||||
ch = '\0';
|
||||
}
|
||||
break;
|
||||
case '}':
|
||||
if (inExpr) {
|
||||
inExpr = MB_FALSE;
|
||||
avt->addExpr(createExpr(buffer));
|
||||
buffer.clear();
|
||||
//-- change in case another '}' follows
|
||||
ch = '\0';
|
||||
}
|
||||
else if (prevCh != ch) {
|
||||
if ( buffer.length() > 0) buffer.append('}');
|
||||
else avt->addExpr(new StringExpr(R_CURLY_BRACE));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
buffer.append(ch);
|
||||
break;
|
||||
}
|
||||
prevCh = ch;
|
||||
}
|
||||
if ( buffer.length() > 0) {
|
||||
if ( inExpr ) {
|
||||
//-- error
|
||||
String errMsg("#error evaluating AttributeValueTemplate. ");
|
||||
errMsg.append("Missing '}' after: ");
|
||||
errMsg.append(buffer);
|
||||
avt->addExpr(new StringExpr(errMsg));
|
||||
}
|
||||
else avt->addExpr(new StringExpr(buffer));
|
||||
}
|
||||
return avt;
|
||||
|
||||
} //-- createAttributeValueTemplate
|
||||
|
||||
Expr* ExprParser::createExpr(const String& pattern) {
|
||||
ExprLexer lexer(pattern);
|
||||
return createExpr(lexer);
|
||||
} //-- createExpr
|
||||
|
||||
PatternExpr* ExprParser::createPatternExpr(const String& pattern) {
|
||||
ExprLexer lexer(pattern);
|
||||
return createUnionExpr(lexer);
|
||||
} //-- createPatternExpr
|
||||
|
||||
LocationStep* ExprParser::createLocationStep(const String& path) {
|
||||
ExprLexer lexer(path);
|
||||
LocationStep* lstep = createLocationStep(lexer);
|
||||
return lstep;
|
||||
} //-- createLocationPath
|
||||
|
||||
//--------------------/
|
||||
//- Private Methods -/
|
||||
//-------------------/
|
||||
|
||||
/**
|
||||
* Creates a binary Expr for the given operator
|
||||
**/
|
||||
Expr* ExprParser::createBinaryExpr (Expr* left, Expr* right, Token* op) {
|
||||
if ( !op ) return 0;
|
||||
switch(op->type) {
|
||||
|
||||
|
||||
//-- additive ops
|
||||
case Token::ADDITION_OP :
|
||||
return new AdditiveExpr(left, right, AdditiveExpr::ADDITION);
|
||||
case Token::SUBTRACTION_OP:
|
||||
return new AdditiveExpr(left, right, AdditiveExpr::SUBTRACTION);
|
||||
|
||||
//-- case boolean ops
|
||||
case Token::AND_OP:
|
||||
return new BooleanExpr(left, right, BooleanExpr::AND);
|
||||
case Token::OR_OP:
|
||||
return new BooleanExpr(left, right, BooleanExpr::OR);
|
||||
|
||||
//-- equality ops
|
||||
case Token::EQUAL_OP :
|
||||
return new RelationalExpr(left, right, RelationalExpr::EQUAL);
|
||||
case Token::NOT_EQUAL_OP :
|
||||
return new RelationalExpr(left, right, RelationalExpr::NOT_EQUAL);
|
||||
|
||||
//-- relational ops
|
||||
case Token::LESS_THAN_OP:
|
||||
return new RelationalExpr(left, right, RelationalExpr::LESS_THAN);
|
||||
case Token::GREATER_THAN_OP:
|
||||
return new RelationalExpr(left, right, RelationalExpr::GREATER_THAN);
|
||||
case Token::LESS_OR_EQUAL_OP:
|
||||
return new RelationalExpr(left, right, RelationalExpr::LESS_OR_EQUAL);
|
||||
case Token::GREATER_OR_EQUAL_OP:
|
||||
return new RelationalExpr(left, right, RelationalExpr::GREATER_OR_EQUAL);
|
||||
|
||||
//-- multiplicative ops
|
||||
case Token::DIVIDE_OP :
|
||||
return new MultiplicativeExpr(left, right, MultiplicativeExpr::DIVIDE);
|
||||
case Token::MODULUS_OP :
|
||||
return new MultiplicativeExpr(left, right, MultiplicativeExpr::MODULUS);
|
||||
case Token::MULTIPLY_OP :
|
||||
return new MultiplicativeExpr(left, right, MultiplicativeExpr::MULTIPLY);
|
||||
default:
|
||||
break;
|
||||
|
||||
}
|
||||
return 0;
|
||||
//return new ErrorExpr();
|
||||
} //-- createBinaryExpr
|
||||
|
||||
|
||||
Expr* ExprParser::createExpr(ExprLexer& lexer) {
|
||||
|
||||
MBool done = MB_FALSE;
|
||||
|
||||
Expr* expr = 0;
|
||||
|
||||
Stack exprs;
|
||||
Stack ops;
|
||||
|
||||
while ( lexer.hasMoreTokens() && (!done)) {
|
||||
|
||||
Token* tok = lexer.nextToken();
|
||||
switch ( tok->type ) {
|
||||
case Token::R_BRACKET:
|
||||
case Token::R_PAREN:
|
||||
case Token::COMMA :
|
||||
lexer.pushBack();
|
||||
done = MB_TRUE;
|
||||
break;
|
||||
case Token::L_PAREN: //-- Grouping Expression
|
||||
expr = createExpr(lexer);
|
||||
//-- look for end ')'
|
||||
if ( lexer.hasMoreTokens() &&
|
||||
( lexer.nextToken()->type == Token::R_PAREN ) ) break;
|
||||
else {
|
||||
//-- error
|
||||
delete expr;
|
||||
expr = new StringExpr("missing ')' in expression");
|
||||
}
|
||||
break;
|
||||
case Token::ANCESTOR_OP:
|
||||
case Token::PARENT_OP:
|
||||
lexer.pushBack();
|
||||
if ( !expr ) expr = createPathExpr(lexer);
|
||||
else {
|
||||
PathExpr* pathExpr = createPathExpr(lexer);
|
||||
pathExpr->addPatternExpr(0, (PatternExpr*)expr,
|
||||
PathExpr::RELATIVE_OP);
|
||||
expr = pathExpr;
|
||||
}
|
||||
//done = MB_TRUE;
|
||||
break;
|
||||
case Token::UNION_OP :
|
||||
{
|
||||
UnionExpr* unionExpr = createUnionExpr(lexer);
|
||||
unionExpr->addPathExpr(0, (PathExpr*)expr );
|
||||
expr = unionExpr;
|
||||
done = MB_TRUE;
|
||||
break;
|
||||
}
|
||||
case Token::LITERAL :
|
||||
expr = new StringExpr(tok->value);
|
||||
break;
|
||||
case Token::NUMBER:
|
||||
{
|
||||
StringResult str(tok->value);
|
||||
expr = new NumberExpr(str.numberValue());
|
||||
break;
|
||||
}
|
||||
case Token::FUNCTION_NAME:
|
||||
{
|
||||
lexer.pushBack();
|
||||
expr = createFunctionCall(lexer);
|
||||
break;
|
||||
}
|
||||
case Token::VAR_REFERENCE:
|
||||
expr = new VariableRefExpr(tok->value);
|
||||
break;
|
||||
//-- additive ops
|
||||
case Token::ADDITION_OP:
|
||||
case Token::DIVIDE_OP:
|
||||
//-- boolean ops
|
||||
case Token::AND_OP :
|
||||
case Token::OR_OP :
|
||||
//-- equality ops
|
||||
case Token::EQUAL_OP:
|
||||
case Token::NOT_EQUAL_OP:
|
||||
//-- relational ops
|
||||
case Token::LESS_THAN_OP:
|
||||
case Token::GREATER_THAN_OP:
|
||||
case Token::LESS_OR_EQUAL_OP:
|
||||
case Token::GREATER_OR_EQUAL_OP:
|
||||
//-- multiplicative ops
|
||||
case Token::MODULUS_OP:
|
||||
case Token::MULTIPLY_OP:
|
||||
case Token::SUBTRACTION_OP:
|
||||
{
|
||||
if ( !exprs.empty() ) {
|
||||
short ttype = ((Token*)ops.peek())->type;
|
||||
if (precedenceLevel(tok->type) < precedenceLevel(ttype)) {
|
||||
expr = createBinaryExpr((Expr*)exprs.pop(), expr,
|
||||
(Token*)ops.pop());
|
||||
}
|
||||
}
|
||||
exprs.push(expr);
|
||||
ops.push(tok);
|
||||
expr = 0; // OG, prevent reuse of expr
|
||||
break;
|
||||
}
|
||||
default:
|
||||
lexer.pushBack();
|
||||
expr = createPatternExpr(lexer);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// make sure expr != 0, will this happen?
|
||||
if (( expr == 0 ) && (!exprs.empty()))
|
||||
expr = (Expr*) exprs.pop();
|
||||
|
||||
while (!exprs.empty() ) {
|
||||
expr = createBinaryExpr((Expr*)exprs.pop(), expr, (Token*)ops.pop());
|
||||
}
|
||||
|
||||
return expr;
|
||||
|
||||
} //-- createExpr
|
||||
|
||||
FilterExpr* ExprParser::createFilterExpr(ExprLexer& lexer) {
|
||||
|
||||
FilterExpr* filterExpr = new FilterExpr();
|
||||
Token* tok = lexer.nextToken();
|
||||
if ( !tok ) return filterExpr;
|
||||
|
||||
Expr* expr = 0;
|
||||
switch ( tok->type ) {
|
||||
case Token::FUNCTION_NAME :
|
||||
expr = createFunctionCall(lexer);
|
||||
filterExpr->setExpr(expr);
|
||||
break;
|
||||
case Token::VAR_REFERENCE :
|
||||
expr = new VariableRefExpr(tok->value);
|
||||
filterExpr->setExpr(expr);
|
||||
break;
|
||||
case Token::L_PAREN:
|
||||
//-- primary group expr:
|
||||
expr = createExpr(lexer);
|
||||
tok = lexer.nextToken();
|
||||
if ( (!tok) || (tok->type != Token::R_PAREN ) ) {
|
||||
String errMsg("error: ");
|
||||
expr->toString(errMsg);
|
||||
errMsg.append(" - missing ')'");
|
||||
delete expr; //-- free up current expr
|
||||
expr = new ErrorFunctionCall(errMsg);
|
||||
}
|
||||
filterExpr->setExpr(expr);
|
||||
break;
|
||||
case Token::PARENT_NODE :
|
||||
expr = new ParentExpr();
|
||||
filterExpr->setExpr(expr);
|
||||
break;
|
||||
case Token::SELF_NODE :
|
||||
expr = new IdentityExpr();
|
||||
filterExpr->setExpr(expr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
//-- handle predicates
|
||||
parsePredicates(filterExpr, lexer);
|
||||
|
||||
return filterExpr;
|
||||
|
||||
} //-- createFilterExpr
|
||||
|
||||
FunctionCall* ExprParser::createFunctionCall(ExprLexer& lexer) {
|
||||
|
||||
if ( !lexer.hasMoreTokens() ) {
|
||||
//-- should never see this, I hope
|
||||
return new ErrorFunctionCall("no tokens, invalid function call");
|
||||
}
|
||||
|
||||
FunctionCall* fnCall = 0;
|
||||
|
||||
Token* tok = lexer.nextToken();
|
||||
|
||||
if ( tok->type != Token::FUNCTION_NAME ) {
|
||||
return new ErrorFunctionCall("invalid function call");
|
||||
}
|
||||
|
||||
String fnName = tok->value;
|
||||
|
||||
//-- compare function names
|
||||
//-- * we should hash these names for speed
|
||||
|
||||
if ( XPathNames::BOOLEAN_FN.isEqual(tok->value) ) {
|
||||
fnCall = new BooleanFunctionCall(BooleanFunctionCall::BOOLEAN);
|
||||
}
|
||||
else if ( XPathNames::CONCAT_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::CONCAT);
|
||||
}
|
||||
else if ( XPathNames::CONTAINS_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::CONTAINS);
|
||||
}
|
||||
else if ( XPathNames::COUNT_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::COUNT);
|
||||
}
|
||||
else if ( XPathNames::FALSE_FN.isEqual(tok->value) ) {
|
||||
fnCall = new BooleanFunctionCall();
|
||||
}
|
||||
else if ( XPathNames::LAST_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::LAST);
|
||||
}
|
||||
else if ( XPathNames::LOCAL_NAME_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::LOCAL_NAME);
|
||||
}
|
||||
else if ( XPathNames::NAME_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::NAME);
|
||||
}
|
||||
else if ( XPathNames::NAMESPACE_URI_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::NAMESPACE_URI);
|
||||
}
|
||||
else if ( XPathNames::NOT_FN.isEqual(tok->value) ) {
|
||||
fnCall = new BooleanFunctionCall(BooleanFunctionCall::NOT);
|
||||
}
|
||||
else if ( XPathNames::POSITION_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NodeSetFunctionCall(NodeSetFunctionCall::POSITION);
|
||||
}
|
||||
else if ( XPathNames::STARTS_WITH_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::STARTS_WITH);
|
||||
}
|
||||
else if ( XPathNames::STRING_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::STRING);
|
||||
}
|
||||
else if ( XPathNames::STRING_LENGTH_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::STRING_LENGTH);
|
||||
}
|
||||
else if ( XPathNames::SUBSTRING_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::SUBSTRING);
|
||||
}
|
||||
else if ( XPathNames::SUBSTRING_AFTER_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::SUBSTRING_AFTER);
|
||||
}
|
||||
else if ( XPathNames::SUBSTRING_BEFORE_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::SUBSTRING_BEFORE);
|
||||
}
|
||||
else if ( XPathNames::TRANSLATE_FN.isEqual(tok->value) ) {
|
||||
fnCall = new StringFunctionCall(StringFunctionCall::TRANSLATE);
|
||||
}
|
||||
else if ( XPathNames::TRUE_FN.isEqual(tok->value) ) {
|
||||
fnCall = new BooleanFunctionCall(BooleanFunctionCall::TRUE);
|
||||
}
|
||||
// OG+
|
||||
else if ( XPathNames::NUMBER_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NumberFunctionCall(NumberFunctionCall::NUMBER);
|
||||
}
|
||||
else if ( XPathNames::ROUND_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NumberFunctionCall(NumberFunctionCall::ROUND);
|
||||
}
|
||||
else if ( XPathNames::CEILING_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NumberFunctionCall(NumberFunctionCall::CEILING);
|
||||
}
|
||||
else if ( XPathNames::FLOOR_FN.isEqual(tok->value) ) {
|
||||
fnCall = new NumberFunctionCall(NumberFunctionCall::FLOOR);
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
//-- handle parametes
|
||||
List params;
|
||||
String* errMsg = parseParameters(¶ms, lexer);
|
||||
if (errMsg) {
|
||||
String err("error with function call, \"");
|
||||
err.append(fnName);
|
||||
err.append("\" : ");
|
||||
err.append(*errMsg);
|
||||
fnCall = new ErrorFunctionCall(err);
|
||||
delete errMsg;
|
||||
}
|
||||
// copy params
|
||||
else if (params.getLength() > 0) {
|
||||
ListIterator* iter = params.iterator();
|
||||
while ( iter->hasNext() ) fnCall->addParam( (Expr*)iter->next() );
|
||||
delete iter;
|
||||
}
|
||||
return fnCall;
|
||||
} //-- createFunctionCall
|
||||
|
||||
LocationStep* ExprParser::createLocationStep(ExprLexer& lexer) {
|
||||
|
||||
LocationStep* lstep = new LocationStep();
|
||||
short axisIdentifier = LocationStep::CHILD_AXIS;
|
||||
|
||||
//-- get Axis Identifier, if present
|
||||
Token* tok = lexer.peek();
|
||||
MBool setDefaultAxis = MB_TRUE;
|
||||
if ( tok->type == Token::AXIS_IDENTIFIER ) {
|
||||
//-- eat token
|
||||
lexer.nextToken();
|
||||
setDefaultAxis = MB_FALSE;
|
||||
|
||||
//-- should switch to a hash here for speed if necessary
|
||||
if ( ANCESTOR_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::ANCESTOR_AXIS;
|
||||
else if ( ANCESTOR_OR_SELF_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::ANCESTOR_OR_SELF_AXIS;
|
||||
else if ( ATTRIBUTE_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::ATTRIBUTE_AXIS;
|
||||
else if ( CHILD_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::CHILD_AXIS;
|
||||
else if ( DESCENDANT_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::DESCENDANT_AXIS;
|
||||
else if ( DESCENDANT_OR_SELF_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::DESCENDANT_OR_SELF_AXIS;
|
||||
else if ( FOLLOWING_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::FOLLOWING_AXIS;
|
||||
else if ( FOLLOWING_SIBLING_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::FOLLOWING_SIBLING_AXIS;
|
||||
else if ( NAMESPACE_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::NAMESPACE_AXIS;
|
||||
else if ( PARENT_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::PARENT_AXIS;
|
||||
else if ( PRECEDING_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::PRECEDING_AXIS;
|
||||
else if ( PRECEDING_SIBLING_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::PRECEDING_SIBLING_AXIS;
|
||||
else if ( SELF_AXIS.isEqual(tok->value) )
|
||||
axisIdentifier = LocationStep::SELF_AXIS;
|
||||
//-- child axis is default
|
||||
else {
|
||||
//-- handle error gracefully, simply ignore invalid axis and
|
||||
//-- use default. Add error message when message observer
|
||||
//-- is implemented
|
||||
setDefaultAxis = MB_TRUE;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//-- parse NodeExpr
|
||||
NodeExpr* nodeExpr = createNodeExpr(lexer);
|
||||
lstep->setNodeExpr(nodeExpr);
|
||||
|
||||
|
||||
//-- set default axis identifiers
|
||||
if ((setDefaultAxis) && (nodeExpr)) {
|
||||
switch ( nodeExpr->getType() ) {
|
||||
case NodeExpr::ATTRIBUTE_EXPR:
|
||||
axisIdentifier = LocationStep::ATTRIBUTE_AXIS;
|
||||
break;
|
||||
default:
|
||||
axisIdentifier = LocationStep::CHILD_AXIS;
|
||||
}
|
||||
}
|
||||
lstep->setAxisIdentifier(axisIdentifier);
|
||||
|
||||
//-- handle predicates
|
||||
|
||||
parsePredicates(lstep, lexer);
|
||||
|
||||
//<debug>
|
||||
//String tmp;
|
||||
//lstep->toString(tmp);
|
||||
//cout << "returning LocationStep: "<< tmp <<endl;
|
||||
//</debug>
|
||||
|
||||
return lstep;
|
||||
} //-- createLocationPath
|
||||
|
||||
NodeExpr* ExprParser::createNodeExpr(ExprLexer& lexer) {
|
||||
|
||||
//cout << "creating NodeExpr: "<<endl;
|
||||
if (!lexer.hasMoreTokens() ) cout << "Lexer has no Tokens"<<endl;
|
||||
|
||||
if (!lexer.hasMoreTokens() ) return 0;
|
||||
|
||||
NodeExpr* nodeExpr = 0;
|
||||
|
||||
Token* tok = lexer.nextToken();
|
||||
//cout << "Token #" << tok->type <<endl;
|
||||
List params;
|
||||
|
||||
String* errMsg = 0;
|
||||
|
||||
switch ( tok->type ) {
|
||||
case Token::CNAME :
|
||||
nodeExpr = new ElementExpr(tok->value);
|
||||
break;
|
||||
case Token::WILD_CARD:
|
||||
nodeExpr = new WildCardExpr();
|
||||
break;
|
||||
case Token::COMMENT:
|
||||
nodeExpr = new BasicNodeExpr(NodeExpr::COMMENT_EXPR);
|
||||
errMsg = parseParameters(¶ms, lexer);
|
||||
//-- ignore errMsg for now
|
||||
delete errMsg;
|
||||
break;
|
||||
case Token::NODE :
|
||||
nodeExpr = new BasicNodeExpr();
|
||||
errMsg = parseParameters(¶ms, lexer);
|
||||
//-- ignore errMsg for now
|
||||
delete errMsg;
|
||||
break;
|
||||
case Token::PI :
|
||||
nodeExpr = new BasicNodeExpr(NodeExpr::PI_EXPR);
|
||||
errMsg = parseParameters(¶ms, lexer);
|
||||
//-- ignore errMsg for now
|
||||
delete errMsg;
|
||||
break;
|
||||
case Token::TEXT :
|
||||
nodeExpr = new TextExpr();
|
||||
errMsg = parseParameters(¶ms, lexer);
|
||||
//-- ignore errMsg for now
|
||||
delete errMsg;
|
||||
break;
|
||||
case Token::AT_SIGN:
|
||||
tok = lexer.nextToken();
|
||||
if ( !tok ) {
|
||||
//-- handle error
|
||||
}
|
||||
else if (tok->type == Token::CNAME) {
|
||||
nodeExpr = new AttributeExpr(tok->value);
|
||||
}
|
||||
else if ( tok->type == Token::WILD_CARD ) {
|
||||
AttributeExpr* attExpr = new AttributeExpr();
|
||||
attExpr->setWild(MB_TRUE);
|
||||
nodeExpr = attExpr;
|
||||
}
|
||||
else {
|
||||
//-- handle error
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return nodeExpr;
|
||||
} //-- createNodeExpr
|
||||
|
||||
/**
|
||||
* Creates a PathExpr using the given ExprLexer
|
||||
* @param lexer the ExprLexer for retrieving Tokens
|
||||
**/
|
||||
PathExpr* ExprParser::createPathExpr(ExprLexer& lexer) {
|
||||
|
||||
|
||||
//-- check for RootExpr
|
||||
if ( lexer.countRemainingTokens() == 1 ) {
|
||||
if ( lexer.peek()->type == Token::PARENT_OP ) {
|
||||
lexer.nextToken(); //-- eat token
|
||||
return new RootExpr();
|
||||
}
|
||||
}
|
||||
|
||||
PathExpr* pathExpr = new PathExpr();
|
||||
short ancestryOp = PathExpr::RELATIVE_OP;
|
||||
|
||||
while ( lexer.hasMoreTokens() ) {
|
||||
Token* tok = lexer.nextToken();
|
||||
if ( lexer.isOperatorToken(tok) ) {
|
||||
lexer.pushBack();
|
||||
return pathExpr;
|
||||
}
|
||||
switch ( tok->type ) {
|
||||
case Token::R_PAREN:
|
||||
case Token::R_BRACKET:
|
||||
case Token::UNION_OP:
|
||||
lexer.pushBack();
|
||||
return pathExpr;
|
||||
case Token::ANCESTOR_OP :
|
||||
ancestryOp = PathExpr::ANCESTOR_OP;
|
||||
break;
|
||||
case Token::PARENT_OP :
|
||||
ancestryOp = PathExpr::PARENT_OP;
|
||||
break;
|
||||
default:
|
||||
lexer.pushBack();
|
||||
pathExpr->addPatternExpr(createPatternExpr(lexer), ancestryOp);
|
||||
ancestryOp = PathExpr::RELATIVE_OP;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* <debug> *
|
||||
String tmp;
|
||||
pathExpr->toString(tmp);
|
||||
cout << "creating pathExpr: " << tmp << endl;
|
||||
/* </debug> */
|
||||
|
||||
return pathExpr;
|
||||
} //-- createPathExpr
|
||||
|
||||
/**
|
||||
* Creates a PatternExpr using the given ExprLexer
|
||||
* @param lexer the ExprLexer for retrieving Tokens
|
||||
**/
|
||||
PatternExpr* ExprParser::createPatternExpr(ExprLexer& lexer) {
|
||||
|
||||
PatternExpr* pExpr = 0;
|
||||
Token* tok = lexer.peek();
|
||||
if ( isLocationStepToken(tok) ) {
|
||||
pExpr = createLocationStep(lexer);
|
||||
}
|
||||
else if ( isFilterExprToken(tok) ) {
|
||||
pExpr = createFilterExpr(lexer);
|
||||
}
|
||||
else {
|
||||
cout << "invalid token: " << tok->value << endl;
|
||||
//-- eat token for now
|
||||
lexer.nextToken();
|
||||
}
|
||||
return pExpr;
|
||||
} //-- createPatternExpr
|
||||
|
||||
/**
|
||||
* Creates a PathExpr using the given ExprLexer
|
||||
* @param lexer the ExprLexer for retrieving Tokens
|
||||
**/
|
||||
UnionExpr* ExprParser::createUnionExpr(ExprLexer& lexer) {
|
||||
UnionExpr* unionExpr = new UnionExpr();
|
||||
|
||||
while ( lexer.hasMoreTokens() ) {
|
||||
Token* tok = lexer.nextToken();
|
||||
switch ( tok->type ) {
|
||||
|
||||
case Token::R_PAREN:
|
||||
case Token::R_BRACKET:
|
||||
lexer.pushBack();
|
||||
return unionExpr;
|
||||
case Token::UNION_OP :
|
||||
//-- eat token
|
||||
break;
|
||||
default:
|
||||
lexer.pushBack();
|
||||
unionExpr->addPathExpr(createPathExpr(lexer));
|
||||
break;
|
||||
}
|
||||
}
|
||||
//String tmp;
|
||||
//unionExpr->toString(tmp);
|
||||
//cout << "creating UnionExpr: " << tmp << endl;
|
||||
return unionExpr;
|
||||
} //-- createUnionExpr
|
||||
|
||||
MBool ExprParser::isFilterExprToken(Token* token) {
|
||||
if ( !token ) return MB_FALSE;
|
||||
switch (token->type) {
|
||||
case Token::LITERAL:
|
||||
case Token::NUMBER:
|
||||
case Token::FUNCTION_NAME:
|
||||
case Token::VAR_REFERENCE:
|
||||
case Token::L_PAREN: // grouping expr
|
||||
case Token::PARENT_NODE:
|
||||
case Token::SELF_NODE :
|
||||
return MB_TRUE;
|
||||
default:
|
||||
return MB_FALSE;
|
||||
}
|
||||
} //-- isFilterExprToken
|
||||
|
||||
MBool ExprParser::isLocationStepToken(Token* token) {
|
||||
if (!token) return MB_FALSE;
|
||||
return ((token->type == Token::AXIS_IDENTIFIER) || isNodeTypeToken(token));
|
||||
} //-- isLocationStepToken
|
||||
|
||||
MBool ExprParser::isNodeTypeToken(Token* token) {
|
||||
if (!token) return MB_FALSE;
|
||||
|
||||
switch ( token->type ) {
|
||||
case Token::AT_SIGN:
|
||||
case Token::CNAME:
|
||||
case Token::WILD_CARD:
|
||||
case Token::COMMENT:
|
||||
case Token::NODE :
|
||||
case Token::PI :
|
||||
case Token::TEXT :
|
||||
return MB_TRUE;
|
||||
default:
|
||||
return MB_FALSE;
|
||||
}
|
||||
} //-- isLocationStepToken
|
||||
|
||||
/**
|
||||
* Using the given lexer, parses the tokens if they represent a predicate list
|
||||
* If an error occurs a non-zero String pointer will be returned containing the
|
||||
* error message.
|
||||
* @param predicateList, the PredicateList to add predicate expressions to
|
||||
* @param lexer the ExprLexer to use for parsing tokens
|
||||
* @return 0 if successful, or a String pointer to the error message
|
||||
**/
|
||||
String* ExprParser::parsePredicates(PredicateList* predicateList, ExprLexer& lexer) {
|
||||
|
||||
String* errorMsg = 0;
|
||||
|
||||
Token* tok = lexer.peek();
|
||||
|
||||
if ( !tok ) return 0; //-- no predicates
|
||||
if ( tok->type != Token::L_BRACKET ) return 0; //-- not start of predicate list
|
||||
|
||||
lexer.nextToken();
|
||||
|
||||
while ( lexer.hasMoreTokens() ) {
|
||||
tok = lexer.peek();
|
||||
if(!tok) {
|
||||
//-- error missing ']'
|
||||
errorMsg = new String("missing close of predicate expression ']'");
|
||||
break;
|
||||
}
|
||||
if ( tok->type == Token::R_BRACKET) {
|
||||
lexer.nextToken(); //-- eat ']'
|
||||
break;
|
||||
}
|
||||
Expr* expr = createExpr(lexer);
|
||||
predicateList->add(expr);
|
||||
}
|
||||
return errorMsg;
|
||||
|
||||
} //-- parsePredicates
|
||||
|
||||
|
||||
/**
|
||||
* Using the given lexer, parses the tokens if they represent a parameter list
|
||||
* If an error occurs a non-zero String pointer will be returned containing the
|
||||
* error message.
|
||||
* @param list, the List to add parameter expressions to
|
||||
* @param lexer the ExprLexer to use for parsing tokens
|
||||
* @return 0 if successful, or a String pointer to the error message
|
||||
**/
|
||||
String* ExprParser::parseParameters(List* list, ExprLexer& lexer) {
|
||||
|
||||
String* errorMsg = 0;
|
||||
|
||||
Token* tok = lexer.peek();
|
||||
|
||||
if ( !tok ) return 0; //-- no params
|
||||
if ( tok->type != Token::L_PAREN ) return 0; //-- not start of param list
|
||||
|
||||
lexer.nextToken(); //-- eat L_PAREN
|
||||
|
||||
MBool done = MB_FALSE;
|
||||
MBool foundSep = MB_FALSE;
|
||||
|
||||
while ( lexer.hasMoreTokens() && !done) {
|
||||
tok = lexer.peek();
|
||||
switch ( tok->type ) {
|
||||
case Token::R_PAREN :
|
||||
if (foundSep) errorMsg = new String("missing expression after ','");
|
||||
lexer.nextToken(); //-- eat R_PAREN
|
||||
done = MB_TRUE;
|
||||
break;
|
||||
case Token::COMMA: //-- param separator
|
||||
//-- eat COMMA
|
||||
lexer.nextToken();
|
||||
foundSep = MB_TRUE;
|
||||
break;
|
||||
default:
|
||||
if ((list->getLength() > 0) && (!foundSep)) {
|
||||
errorMsg = new String("missing ',' or ')'");
|
||||
done = MB_TRUE;
|
||||
break;
|
||||
}
|
||||
foundSep = MB_FALSE;
|
||||
Expr* expr = createExpr(lexer);
|
||||
list->add(expr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return errorMsg;
|
||||
|
||||
} //-- parseParameters
|
||||
|
||||
short ExprParser::precedenceLevel(short tokenType) {
|
||||
switch(tokenType) {
|
||||
case Token::OR_OP:
|
||||
return 1;
|
||||
case Token::AND_OP:
|
||||
return 2;
|
||||
//-- equality
|
||||
case Token::EQUAL_OP:
|
||||
case Token::NOT_EQUAL_OP:
|
||||
return 3;
|
||||
//-- relational
|
||||
case Token::LESS_THAN_OP:
|
||||
case Token::GREATER_THAN_OP:
|
||||
case Token::LESS_OR_EQUAL_OP:
|
||||
case Token::GREATER_OR_EQUAL_OP:
|
||||
return 4;
|
||||
//-- additive operators
|
||||
case Token::ADDITION_OP:
|
||||
case Token::SUBTRACTION_OP:
|
||||
return 5;
|
||||
//-- multiplicative
|
||||
case Token::DIVIDE_OP:
|
||||
case Token::MULTIPLY_OP:
|
||||
case Token::MODULUS_OP:
|
||||
return 6;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
} //-- precedenceLevel
|
||||
|
|
@ -0,0 +1,106 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txExprParser.h,v 1.1 2005/11/02 07:33:51 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* ExprParser
|
||||
* This class is used to parse XSL Expressions
|
||||
* @author <A href="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:51 $
|
||||
* @see ExprLexer
|
||||
**/
|
||||
|
||||
#ifndef MITREXSL_EXPRPARSER_H
|
||||
#define MITREXSL_EXPRPARSER_H
|
||||
|
||||
#include "String.h"
|
||||
#include "ExprLexer.h"
|
||||
#include "Expr.h"
|
||||
#include "FunctionLib.h"
|
||||
#include "List.h"
|
||||
#include "Stack.h"
|
||||
#include <iostream.h>
|
||||
|
||||
class ExprParser {
|
||||
|
||||
public:
|
||||
|
||||
static const String R_CURLY_BRACE;
|
||||
static const String L_CURLY_BRACE;
|
||||
|
||||
/**
|
||||
* Creates a new ExprParser
|
||||
**/
|
||||
ExprParser();
|
||||
|
||||
/**
|
||||
* destroys the ExprParser
|
||||
**/
|
||||
~ExprParser();
|
||||
|
||||
Expr* createExpr (const String& pattern);
|
||||
PatternExpr* createPatternExpr (const String& pattern);
|
||||
LocationStep* createLocationStep(const String& path);
|
||||
|
||||
/**
|
||||
* Creates an Attribute Value Template using the given value
|
||||
**/
|
||||
AttributeValueTemplate* createAttributeValueTemplate(const String& attValue);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
|
||||
Expr* createBinaryExpr (Expr* left, Expr* right, Token* op);
|
||||
Expr* createExpr (ExprLexer& lexer);
|
||||
FilterExpr* createFilterExpr (ExprLexer& lexer);
|
||||
FunctionCall* createFunctionCall (ExprLexer& lexer);
|
||||
LocationStep* createLocationStep (ExprLexer& lexer);
|
||||
NodeExpr* createNodeExpr (ExprLexer& lexer);
|
||||
PathExpr* createPathExpr (ExprLexer& lexer);
|
||||
PatternExpr* createPatternExpr (ExprLexer& lexer);
|
||||
UnionExpr* createUnionExpr (ExprLexer& lexer);
|
||||
|
||||
MBool isFilterExprToken (Token* tok);
|
||||
MBool isLocationStepToken (Token* tok);
|
||||
MBool isNodeTypeToken (Token* tok);
|
||||
|
||||
static short precedenceLevel (short tokenType);
|
||||
|
||||
/**
|
||||
* Using the given lexer, parses the tokens if they represent a predicate list
|
||||
* If an error occurs a non-zero String pointer will be returned containing the
|
||||
* error message.
|
||||
* @param predicateList, the PredicateList to add predicate expressions to
|
||||
* @param lexer the ExprLexer to use for parsing tokens
|
||||
* @return 0 if successful, or a String pointer to the error message
|
||||
**/
|
||||
String* parsePredicates(PredicateList* predicateList, ExprLexer& lexer);
|
||||
String* parseParameters(List* list, ExprLexer& lexer);
|
||||
|
||||
|
||||
}; //-- ExprParser
|
||||
|
||||
#endif
|
|
@ -0,0 +1,162 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
* Larry Fitzpatrick, OpenText, lef@opentext.com
|
||||
* -- changed constant short result types to enum
|
||||
*
|
||||
* $Id: txExprResult.h,v 1.1 2005/11/02 07:33:43 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "MITREObject.h"
|
||||
#include "String.h"
|
||||
#include "baseutils.h"
|
||||
#include "primitives.h"
|
||||
|
||||
#ifndef MITREXSL_EXPRRESULT_H
|
||||
#define MITREXSL_EXPRRESULT_H
|
||||
|
||||
|
||||
/*
|
||||
* ExprResult
|
||||
* <BR />
|
||||
* Classes Represented:
|
||||
* BooleanResult, ExprResult, NumberResult, StringResult
|
||||
* <BR/>
|
||||
* Note: for NodeSet, see NodeSet.h <BR />
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:43 $
|
||||
*/
|
||||
|
||||
class ExprResult : public MITREObject {
|
||||
|
||||
public:
|
||||
|
||||
//-- ResultTypes
|
||||
enum _ResultType {
|
||||
NODESET = 1,
|
||||
STRING,
|
||||
BOOLEAN,
|
||||
TREE_FRAGMENT,
|
||||
NUMBER
|
||||
};
|
||||
|
||||
virtual ~ExprResult() {};
|
||||
|
||||
/**
|
||||
* Returns the type of ExprResult represented
|
||||
* @return the type of ExprResult represented
|
||||
**/
|
||||
virtual short getResultType() = 0;
|
||||
/**
|
||||
* Creates a String representation of this ExprResult
|
||||
* @param str the destination string to append the String representation to.
|
||||
**/
|
||||
virtual void stringValue(String& str) = 0;
|
||||
|
||||
/**
|
||||
* Converts this ExprResult to a Boolean (MBool) value
|
||||
* @return the Boolean value
|
||||
**/
|
||||
virtual MBool booleanValue() = 0;
|
||||
|
||||
/**
|
||||
* Converts this ExprResult to a Number (double) value
|
||||
* @return the Number value
|
||||
**/
|
||||
virtual double numberValue() = 0;
|
||||
|
||||
};
|
||||
|
||||
class BooleanResult : public ExprResult {
|
||||
|
||||
public:
|
||||
|
||||
BooleanResult();
|
||||
BooleanResult(MBool boolean);
|
||||
BooleanResult(const BooleanResult& boolResult);
|
||||
|
||||
MBool getValue() const;
|
||||
|
||||
void setValue(MBool boolean);
|
||||
|
||||
void setValue(const BooleanResult& boolResult);
|
||||
|
||||
virtual short getResultType();
|
||||
virtual void stringValue(String& str);
|
||||
virtual MBool booleanValue();
|
||||
virtual double numberValue();
|
||||
|
||||
private:
|
||||
MBool value;
|
||||
};
|
||||
|
||||
class NumberResult : public ExprResult {
|
||||
|
||||
public:
|
||||
|
||||
NumberResult();
|
||||
NumberResult(double dbl);
|
||||
NumberResult(const NumberResult& nbrResult);
|
||||
|
||||
double getValue() const;
|
||||
|
||||
void setValue(double dbl);
|
||||
|
||||
void setValue(const NumberResult& nbrResult);
|
||||
|
||||
MBool isNaN() const;
|
||||
|
||||
virtual short getResultType();
|
||||
virtual void stringValue(String& str);
|
||||
virtual MBool booleanValue();
|
||||
virtual double numberValue();
|
||||
|
||||
private:
|
||||
double value;
|
||||
|
||||
};
|
||||
|
||||
|
||||
class StringResult : public ExprResult {
|
||||
|
||||
public:
|
||||
|
||||
StringResult();
|
||||
StringResult(String& str);
|
||||
StringResult(const String& str);
|
||||
StringResult(const StringResult& strResult);
|
||||
|
||||
String& getValue();
|
||||
void setValue(const String& str);
|
||||
|
||||
virtual short getResultType();
|
||||
virtual void stringValue(String& str);
|
||||
virtual MBool booleanValue();
|
||||
virtual double numberValue();
|
||||
|
||||
|
||||
private:
|
||||
String value;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,147 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
* Bob Miller, kbob@oblix.com
|
||||
* -- plugged core leak.
|
||||
*
|
||||
* $Id: txFilterExpr.cpp,v 1.1 2005/11/02 07:33:50 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:50 $
|
||||
**/
|
||||
//-- Implementation of FilterExpr --/
|
||||
|
||||
|
||||
FilterExpr::FilterExpr() : PredicateList() {
|
||||
expr = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new FilterExpr using the given Expr
|
||||
* @param expr the Expr to use for evaluation
|
||||
**/
|
||||
FilterExpr::FilterExpr(Expr* expr) : PredicateList() {
|
||||
this->expr = expr;
|
||||
} //-- FilterExpr
|
||||
|
||||
/**
|
||||
* Destroys this FilterExpr, all predicates and the expr will be deleted
|
||||
**/
|
||||
FilterExpr::~FilterExpr() {
|
||||
delete expr;
|
||||
} //-- ~FilterExpr
|
||||
|
||||
/**
|
||||
* Sets the Expr of this FilterExpr for use during evaluation
|
||||
* @param expr the Expr to use for evaluation
|
||||
**/
|
||||
void FilterExpr::setExpr(Expr* expr) {
|
||||
this->expr = expr;
|
||||
} //-- setExpr
|
||||
|
||||
|
||||
//------------------------------------/
|
||||
//- Virtual methods from PatternExpr -/
|
||||
//------------------------------------/
|
||||
|
||||
/**
|
||||
* 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 ProcessorState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
* @see PatternExpr
|
||||
**/
|
||||
ExprResult* FilterExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
if (( !context ) || (! expr )) return new NodeSet;
|
||||
|
||||
ExprResult* exprResult = expr->evaluate(context, cs);
|
||||
NodeSet* nodeSet = 0;
|
||||
|
||||
switch (exprResult->getResultType()) {
|
||||
case ExprResult::NODESET:
|
||||
nodeSet = (NodeSet*)exprResult;
|
||||
break;
|
||||
/*
|
||||
case ExprResult.TREE_FRAGMENT:
|
||||
nodeSet = new NodeSet(1);
|
||||
nodeSet.add(((TreeFragmentResult)exprResult).getValue());
|
||||
break;
|
||||
*/
|
||||
default:
|
||||
break;
|
||||
/*
|
||||
throw new InvalidExprException
|
||||
("expecting NodeSet or TreeFragment as the result of the "+
|
||||
"expression: " + primaryExpr);
|
||||
*/
|
||||
}
|
||||
|
||||
//-- filter nodes (apply predicates)
|
||||
evaluatePredicates(nodeSet, cs);
|
||||
|
||||
return nodeSet;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* Returns the default priority of this Pattern based on the given Node,
|
||||
* context Node, and ContextState.
|
||||
* If this pattern does not match the given Node under the current context Node and
|
||||
* ContextState then Negative Infinity is returned.
|
||||
**/
|
||||
double FilterExpr::getDefaultPriority(Node* node, Node* context, ContextState* cs) {
|
||||
//-- this method will never be called, it's only here since
|
||||
//-- I made it manditory for PatternExprs I will remove it soon
|
||||
return Double::NEGATIVE_INFINITY;
|
||||
} //-- getDefaultPriority
|
||||
|
||||
/**
|
||||
* Determines whether this PatternExpr matches the given node within
|
||||
* the given context
|
||||
**/
|
||||
MBool FilterExpr::matches(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
if ( !expr ) return MB_FALSE;
|
||||
NodeSet* nodes = (NodeSet*)evaluate(node, cs);
|
||||
MBool result = (nodes->contains(node));
|
||||
delete nodes;
|
||||
return result;
|
||||
|
||||
} //-- matches
|
||||
|
||||
/**
|
||||
* Creates a String representation of this Expr
|
||||
* @param str the destination String to append to
|
||||
* @see Expr
|
||||
**/
|
||||
void FilterExpr::toString(String& str) {
|
||||
if ( expr ) expr->toString(str);
|
||||
else str.append("null");
|
||||
PredicateList::toString(str);
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,202 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txFunctionCall.cpp,v 1.1 2005/11/02 07:33:38 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "FunctionLib.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 $
|
||||
**/
|
||||
|
||||
const String FunctionCall::INVALID_PARAM_COUNT =
|
||||
"invalid number of parameters for function: ";
|
||||
|
||||
//- Constructors -/
|
||||
|
||||
/**
|
||||
* Creates a new FunctionCall
|
||||
**/
|
||||
FunctionCall::FunctionCall() {
|
||||
this->name = "void";
|
||||
} //-- FunctionCall
|
||||
|
||||
/**
|
||||
* Creates a new FunctionCall with the given function
|
||||
* Note: The object references in parameters will be deleted when this
|
||||
* FunctionCall gets destroyed.
|
||||
**/
|
||||
FunctionCall::FunctionCall(const String& name) {
|
||||
//-- copy name
|
||||
this->name = name;
|
||||
} //-- FunctionCall
|
||||
|
||||
/**
|
||||
* Creates a new FunctionCall with the given function name and parameter list
|
||||
* Note: The object references in parameters will be deleted when this
|
||||
* FunctionCall gets destroyed.
|
||||
**/
|
||||
FunctionCall::FunctionCall(const String& name, List* parameters) {
|
||||
//-- copy name
|
||||
this->name = name;
|
||||
|
||||
if (parameters) {
|
||||
ListIterator* pIter = parameters->iterator();
|
||||
while ( pIter->hasNext() ) {
|
||||
params.add(pIter->next());
|
||||
}
|
||||
delete pIter;
|
||||
}
|
||||
|
||||
} //-- FunctionCall
|
||||
|
||||
|
||||
/**
|
||||
* Destructor
|
||||
**/
|
||||
FunctionCall::~FunctionCall() {
|
||||
|
||||
ListIterator* iter = params.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
iter->next();
|
||||
Expr* expr = (Expr*) iter->remove();
|
||||
delete expr;
|
||||
}
|
||||
delete iter;
|
||||
} //-- ~FunctionCall
|
||||
|
||||
//------------------/
|
||||
//- Public Methods -/
|
||||
//------------------/
|
||||
|
||||
/**
|
||||
* Adds the given parameter to this FunctionCall's parameter list
|
||||
* @param expr the Expr to add to this FunctionCall's parameter list
|
||||
**/
|
||||
void FunctionCall::addParam(Expr* expr) {
|
||||
if ( expr ) params.add(expr);
|
||||
} //-- addParam
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a String.
|
||||
* The value is appended to the given destination String
|
||||
**/
|
||||
void FunctionCall::evaluateToString
|
||||
(Expr* expr, Node* context, ContextState* cs, String& dest)
|
||||
{
|
||||
if (!expr) return;
|
||||
ExprResult* exprResult = expr->evaluate(context, cs);
|
||||
exprResult->stringValue(dest);
|
||||
delete exprResult;
|
||||
} //-- evaluateToString
|
||||
|
||||
/**
|
||||
* Evaluates the given Expression and converts it's result to a number.
|
||||
**/
|
||||
double FunctionCall::evaluateToNumber
|
||||
(Expr* expr, Node* context, ContextState* cs)
|
||||
{
|
||||
double result = Double::NaN;
|
||||
if (!expr) return result;
|
||||
ExprResult* exprResult = expr->evaluate(context, cs);
|
||||
result = exprResult->numberValue();
|
||||
delete exprResult;
|
||||
return result;
|
||||
} //-- evaluateToNumber
|
||||
|
||||
/**
|
||||
* Returns the name of this FunctionCall
|
||||
* @return the name of this FunctionCall
|
||||
**/
|
||||
const String& FunctionCall::getName() {
|
||||
return (const String&) this->name;
|
||||
} //-- getName
|
||||
|
||||
/**
|
||||
* Called to check number of parameters
|
||||
**/
|
||||
MBool FunctionCall::requireParams
|
||||
(int paramCountMin, int paramCountMax, ContextState* cs)
|
||||
{
|
||||
|
||||
int argc = params.getLength();
|
||||
if (( argc < paramCountMin) || (argc > paramCountMax)) {
|
||||
String err(INVALID_PARAM_COUNT);
|
||||
toString(err);
|
||||
cs->recieveError(err);
|
||||
return MB_FALSE;
|
||||
}
|
||||
return MB_TRUE;
|
||||
} //-- requireParams
|
||||
|
||||
/**
|
||||
* Called to check number of parameters
|
||||
**/
|
||||
MBool FunctionCall::requireParams(int paramCountMin, ContextState* cs) {
|
||||
int argc = params.getLength();
|
||||
if (argc < paramCountMin) {
|
||||
String err(INVALID_PARAM_COUNT);
|
||||
toString(err);
|
||||
cs->recieveError(err);
|
||||
return MB_FALSE;
|
||||
}
|
||||
return MB_TRUE;
|
||||
} //-- requireParams
|
||||
|
||||
/**
|
||||
* Sets the function name of this FunctionCall
|
||||
* @param name the name of this Function
|
||||
**/
|
||||
void FunctionCall::setName(const String& name) {
|
||||
this->name.clear();
|
||||
this->name.append(name);
|
||||
} //-- setName
|
||||
|
||||
/**
|
||||
* Returns the String representation of this NodeExpr.
|
||||
* @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 NodeExpr.
|
||||
**/
|
||||
void FunctionCall::toString(String& dest) {
|
||||
dest.append(this->name);
|
||||
dest.append('(');
|
||||
//-- add parameters
|
||||
ListIterator* iterator = params.iterator();
|
||||
int argc = 0;
|
||||
while ( iterator->hasNext() ) {
|
||||
if ( argc > 0 ) dest.append(',');
|
||||
Expr* expr = (Expr*)iterator->next();
|
||||
expr->toString(dest);
|
||||
++argc;
|
||||
|
||||
}
|
||||
delete iterator;
|
||||
dest.append(')');
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,365 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* Olivier Gerardin, ogerardin@vo.lu
|
||||
* -- added number functions
|
||||
*
|
||||
* $Id: txFunctionLib.h,v 1.1 2005/11/02 07:33:43 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifndef MITREXSL_FUNCTIONLIB_H
|
||||
#define MITREXSL_FUNCTIONLIB_H
|
||||
|
||||
#include "String.h"
|
||||
#include "primitives.h"
|
||||
#include "NodeSet.h"
|
||||
#include "List.h"
|
||||
#include "dom.h"
|
||||
#include "ExprResult.h"
|
||||
#include "baseutils.h"
|
||||
#include "Expr.h"
|
||||
#include "Names.h"
|
||||
#include "XMLUtils.h"
|
||||
#include <math.h>
|
||||
|
||||
class XPathNames {
|
||||
|
||||
public:
|
||||
//-- Function Names
|
||||
static const String BOOLEAN_FN;
|
||||
static const String CONCAT_FN;
|
||||
static const String CONTAINS_FN;
|
||||
static const String COUNT_FN ;
|
||||
static const String FALSE_FN;
|
||||
static const String LAST_FN;
|
||||
static const String LOCAL_NAME_FN;
|
||||
static const String NAME_FN;
|
||||
static const String NAMESPACE_URI_FN;
|
||||
static const String NOT_FN;
|
||||
static const String POSITION_FN;
|
||||
static const String STARTS_WITH_FN;
|
||||
static const String STRING_FN;
|
||||
static const String STRING_LENGTH_FN;
|
||||
static const String SUBSTRING_FN;
|
||||
static const String SUBSTRING_AFTER_FN;
|
||||
static const String SUBSTRING_BEFORE_FN;
|
||||
static const String TRANSLATE_FN;
|
||||
static const String TRUE_FN;
|
||||
// OG+
|
||||
static const String NUMBER_FN;
|
||||
static const String ROUND_FN;
|
||||
static const String CEILING_FN;
|
||||
static const String FLOOR_FN;
|
||||
// OG-
|
||||
|
||||
//-- internal XSL processor functions
|
||||
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/>
|
||||
* <PRE>
|
||||
* Modifications:
|
||||
* 19990805: Keith Visco
|
||||
* - added NodeSetFunctionCall
|
||||
* - moved position() function into NodeSetFunctionCall
|
||||
* - removed PositionFunctionCall
|
||||
* 19990806: Larry Fitzpatrick
|
||||
* - changed constant short declarations for BooleanFunctionCall
|
||||
* with enumerations
|
||||
* 19990806: Keith Visco
|
||||
* - added StringFunctionCall
|
||||
* - 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 FunctionCall::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 { BOOLEAN = 1, FALSE, NOT, TRUE };
|
||||
|
||||
/**
|
||||
* Creates a default BooleanFunctionCall, which always evaluates to False
|
||||
**/
|
||||
BooleanFunctionCall();
|
||||
|
||||
/**
|
||||
* Creates a BooleanFunctionCall of the given type
|
||||
**/
|
||||
BooleanFunctionCall(short type);
|
||||
|
||||
/**
|
||||
* 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:
|
||||
short type;
|
||||
}; //-- BooleanFunctionCall
|
||||
|
||||
/**
|
||||
* Internal Function created when there is an Error during parsing
|
||||
* an Expression
|
||||
**/
|
||||
class ErrorFunctionCall : public FunctionCall {
|
||||
public:
|
||||
|
||||
ErrorFunctionCall();
|
||||
ErrorFunctionCall(const String& errorMsg);
|
||||
|
||||
/**
|
||||
* 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);
|
||||
|
||||
void setErrorMessage(String& errorMsg);
|
||||
|
||||
private:
|
||||
|
||||
String errorMessage;
|
||||
|
||||
}; //-- ErrorFunctionCall
|
||||
|
||||
/**
|
||||
* Represents the XPath NodeSet function calls
|
||||
**/
|
||||
class NodeSetFunctionCall : public FunctionCall {
|
||||
|
||||
public:
|
||||
|
||||
enum _NodeSetFunctions {
|
||||
COUNT = 1, //-- count()
|
||||
LAST, //-- last()
|
||||
LOCAL_NAME, //-- local-name()
|
||||
NAMESPACE_URI, //-- namespace-uri()
|
||||
NAME, //-- name()
|
||||
POSITION //-- position()
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a default NodeSetFunction call. Position function is the default.
|
||||
**/
|
||||
NodeSetFunctionCall();
|
||||
|
||||
/**
|
||||
* Creates a NodeSetFunctionCall of the given type
|
||||
**/
|
||||
NodeSetFunctionCall(short type);
|
||||
|
||||
/**
|
||||
* 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:
|
||||
short type;
|
||||
}; //-- NodeSetFunctionCall
|
||||
|
||||
|
||||
/**
|
||||
* Represents the XPath String Function Calls
|
||||
**/
|
||||
class StringFunctionCall : public FunctionCall {
|
||||
|
||||
public:
|
||||
|
||||
enum _StringFunctions {
|
||||
CONCAT = 1, //-- concat()
|
||||
CONTAINS, //-- contains()
|
||||
NORMALIZE, //-- normalize()
|
||||
STARTS_WITH, //-- starts-with()
|
||||
STRING, //-- string()
|
||||
STRING_LENGTH, //-- string-length()
|
||||
SUBSTRING, //-- substring()
|
||||
SUBSTRING_AFTER, //-- substring-after()
|
||||
SUBSTRING_BEFORE, //-- substring-before()
|
||||
TRANSLATE //-- translate()
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a default String function. String() function is the default.
|
||||
**/
|
||||
StringFunctionCall();
|
||||
|
||||
/**
|
||||
* Creates a String function of the given type
|
||||
**/
|
||||
StringFunctionCall(short type);
|
||||
|
||||
/**
|
||||
* 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:
|
||||
short type;
|
||||
}; //-- StringFunctionCall
|
||||
|
||||
|
||||
// OG+
|
||||
/**
|
||||
* Represents the XPath Number Function Calls
|
||||
**/
|
||||
class NumberFunctionCall : public FunctionCall {
|
||||
|
||||
public:
|
||||
|
||||
enum _NumberFunctions {
|
||||
NUMBER = 1, //-- number()
|
||||
ROUND, //-- round()
|
||||
FLOOR, //-- floor()
|
||||
CEILING //-- ceiling()
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a default Number function. number() function is the default.
|
||||
**/
|
||||
NumberFunctionCall();
|
||||
|
||||
/**
|
||||
* Creates a Number function of the given type
|
||||
**/
|
||||
NumberFunctionCall(short type);
|
||||
|
||||
/**
|
||||
* 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:
|
||||
short type;
|
||||
}; //-- NumberFunctionCall
|
||||
// OG-
|
||||
|
||||
#endif
|
|
@ -0,0 +1,317 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txLocationStep.cpp,v 1.1 2005/11/02 07:33:41 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* LocationStep
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:41 $
|
||||
**/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Creates a new LocationStep using the default Axis Identifier and no
|
||||
* NodeExpr (which matches nothing)
|
||||
**/
|
||||
LocationStep::LocationStep() : PredicateList() {
|
||||
nodeExpr = 0;
|
||||
this->axisIdentifier = CHILD_AXIS;
|
||||
} //-- LocationStep
|
||||
|
||||
/**
|
||||
* Creates a new LocationStep using the default Axis Identifier and
|
||||
* the given NodeExpr
|
||||
* @param nodeExpr the NodeExpr to use when matching Nodes
|
||||
**/
|
||||
LocationStep::LocationStep(NodeExpr* nodeExpr) : PredicateList() {
|
||||
this->nodeExpr = nodeExpr;
|
||||
this->axisIdentifier = CHILD_AXIS;
|
||||
} //-- LocationStep
|
||||
|
||||
/**
|
||||
* Creates a new LocationStep using the given NodeExpr and Axis Identifier
|
||||
* @param nodeExpr the NodeExpr to use when matching Nodes
|
||||
* @param axisIdentifier the Axis Identifier in which to search for nodes
|
||||
**/
|
||||
LocationStep::LocationStep(NodeExpr* nodeExpr, short axisIdentifier) : PredicateList() {
|
||||
this->nodeExpr = nodeExpr;
|
||||
this->axisIdentifier = axisIdentifier;
|
||||
} //-- LocationStep
|
||||
|
||||
/**
|
||||
* Destroys this LocationStep
|
||||
* All predicates will be deleted
|
||||
* The NodeExpr will be deleted
|
||||
**/
|
||||
LocationStep::~LocationStep() {
|
||||
delete nodeExpr;
|
||||
} //-- ~LocationStep
|
||||
|
||||
/**
|
||||
* Sets the Axis Identifier for this LocationStep
|
||||
* @param axisIdentifier the Axis in which to search for nodes
|
||||
**/
|
||||
void LocationStep::setAxisIdentifier(short axisIdentifier) {
|
||||
this->axisIdentifier = axisIdentifier;
|
||||
} //-- setAxisIdentifier
|
||||
|
||||
|
||||
/**
|
||||
* Sets the NodeExpr of this LocationStep for use when matching nodes
|
||||
* @param nodeExpr the NodeExpr to use when matching nodes
|
||||
**/
|
||||
void LocationStep::setNodeExpr(NodeExpr* nodeExpr) {
|
||||
this->nodeExpr = nodeExpr;
|
||||
} //-- setNodeExpr
|
||||
|
||||
|
||||
//------------------------------------/
|
||||
//- Virtual methods from PatternExpr -/
|
||||
//------------------------------------/
|
||||
|
||||
/**
|
||||
* 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 ProcessorState containing the stack information needed
|
||||
* for evaluation
|
||||
* @return the result of the evaluation
|
||||
* @see PatternExpr
|
||||
**/
|
||||
ExprResult* LocationStep::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
NodeSet* nodes = new NodeSet();
|
||||
if (( !context ) || (! nodeExpr )) return nodes;
|
||||
|
||||
Node* node = context;
|
||||
switch (axisIdentifier) {
|
||||
case ANCESTOR_AXIS :
|
||||
if (node) node = context->getParentNode();
|
||||
//-- do not break here
|
||||
case ANCESTOR_OR_SELF_AXIS :
|
||||
while (node) {
|
||||
if (nodeExpr->matches(node, context, cs)) {
|
||||
nodes->add(node);
|
||||
}
|
||||
node = node->getParentNode();
|
||||
}
|
||||
break;
|
||||
case ATTRIBUTE_AXIS :
|
||||
{
|
||||
NamedNodeMap* atts = context->getAttributes();
|
||||
if ( atts ) {
|
||||
for ( int i = 0; i < atts->getLength(); i++ ) {
|
||||
Attr* attr = (Attr*)atts->item(i);
|
||||
if ( nodeExpr->matches(attr, context, cs) ) nodes->add(attr);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case DESCENDANT_OR_SELF_AXIS :
|
||||
if ( nodeExpr->matches(context, context, cs))
|
||||
nodes->add(context);
|
||||
//-- do not break here
|
||||
case DESCENDANT_AXIS :
|
||||
fromDescendants(context, cs, nodes);
|
||||
break;
|
||||
case FOLLOWING_AXIS :
|
||||
{
|
||||
node = context->getNextSibling();
|
||||
while (node) {
|
||||
if (nodeExpr->matches(node, context, cs))
|
||||
nodes->add(node);
|
||||
|
||||
if (node->hasChildNodes())
|
||||
fromDescendants(node, cs, nodes);
|
||||
|
||||
Node* tmpNode = node->getNextSibling();
|
||||
if (!tmpNode) {
|
||||
node = node->getParentNode();
|
||||
if ((node) && (node->getNodeType() != Node::DOCUMENT_NODE))
|
||||
node = node->getNextSibling();
|
||||
}
|
||||
else node = tmpNode;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FOLLOWING_SIBLING_AXIS :
|
||||
node = context->getNextSibling();
|
||||
while (node) {
|
||||
if (nodeExpr->matches(node, context, cs))
|
||||
nodes->add(node);
|
||||
node = node->getNextSibling();
|
||||
}
|
||||
break;
|
||||
case NAMESPACE_AXIS : //-- not yet implemented
|
||||
cout << "namespace axis not yet implemented"<<endl;
|
||||
break;
|
||||
case PARENT_AXIS :
|
||||
{
|
||||
Node* parent = context->getParentNode();
|
||||
if ( nodeExpr->matches(parent, context, cs) )
|
||||
nodes->add(parent);
|
||||
break;
|
||||
}
|
||||
case PRECEDING_AXIS :
|
||||
node = context->getPreviousSibling();
|
||||
if ( !node ) node = context->getParentNode();
|
||||
while (node) {
|
||||
if (nodeExpr->matches(node, context, cs))
|
||||
nodes->add(node);
|
||||
|
||||
Node* temp = node->getPreviousSibling();
|
||||
if (!temp) node = node->getParentNode();
|
||||
else node = temp;
|
||||
}
|
||||
break;
|
||||
case PRECEDING_SIBLING_AXIS:
|
||||
node = context->getPreviousSibling();
|
||||
while (node) {
|
||||
if (nodeExpr->matches(node, context, cs))
|
||||
nodes->add(node);
|
||||
node = node->getPreviousSibling();
|
||||
}
|
||||
break;
|
||||
case SELF_AXIS :
|
||||
if ( nodeExpr->matches(context, context, cs) )
|
||||
nodes->add(context);
|
||||
break;
|
||||
default: //-- Children Axis
|
||||
{
|
||||
NodeList* nl = context->getChildNodes();
|
||||
for (int i = 0; i < nl->getLength(); i++ ) {
|
||||
if ( nodeExpr->matches(nl->item(i), context, cs) )
|
||||
nodes->add(nl->item(i));
|
||||
}
|
||||
break;
|
||||
}
|
||||
} //-- switch
|
||||
|
||||
//-- apply predicates
|
||||
evaluatePredicates(nodes, cs);
|
||||
|
||||
return nodes;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* Returns the default priority of this Pattern based on the given Node,
|
||||
* context Node, and ContextState.
|
||||
* If this pattern does not match the given Node under the current context Node and
|
||||
* ContextState then Negative Infinity is returned.
|
||||
**/
|
||||
double LocationStep::getDefaultPriority(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
if ( !nodeExpr ) {
|
||||
return Double::NEGATIVE_INFINITY;
|
||||
}
|
||||
if (!this->isEmpty()) {
|
||||
return 0.5;
|
||||
}
|
||||
return nodeExpr->getDefaultPriority(node, context, cs);
|
||||
|
||||
} //-- getDefaultPriority
|
||||
|
||||
|
||||
void LocationStep::fromDescendants(Node* context, ContextState* cs, NodeSet* nodes) {
|
||||
|
||||
if (( !context ) || (! nodeExpr )) return;
|
||||
|
||||
NodeList* nl = context->getChildNodes();
|
||||
for (int i = 0; i < nl->getLength(); i++) {
|
||||
Node* child = nl->item(i);
|
||||
if (nodeExpr->matches(child, context, cs))
|
||||
nodes->add(child);
|
||||
//-- check childs descendants
|
||||
if (child->hasChildNodes()) fromDescendants(child, cs, nodes);
|
||||
}
|
||||
|
||||
} //-- fromDescendants
|
||||
|
||||
/**
|
||||
* Determines whether this PatternExpr matches the given node within
|
||||
* the given context
|
||||
**/
|
||||
MBool LocationStep::matches(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
if ( !nodeExpr ) return MB_FALSE;
|
||||
|
||||
if ( !nodeExpr->matches(node, context, cs) ) return MB_FALSE;
|
||||
|
||||
NodeSet nodes;
|
||||
nodes.add(node);
|
||||
evaluatePredicates(&nodes, cs);
|
||||
|
||||
return (MBool)(nodes.size() > 0);
|
||||
|
||||
} //-- matches
|
||||
|
||||
/**
|
||||
* Creates a String representation of this Expr
|
||||
* @param str the destination String to append to
|
||||
* @see Expr
|
||||
**/
|
||||
void LocationStep::toString(String& str) {
|
||||
switch (axisIdentifier) {
|
||||
case ANCESTOR_AXIS :
|
||||
str.append("ancestor::");
|
||||
break;
|
||||
case ANCESTOR_OR_SELF_AXIS :
|
||||
str.append("ancestor-or-self::");
|
||||
break;
|
||||
case DESCENDANT_AXIS:
|
||||
str.append("descendant::");
|
||||
break;
|
||||
case DESCENDANT_OR_SELF_AXIS:
|
||||
str.append("descendant-or-self::");
|
||||
break;
|
||||
case FOLLOWING_AXIS :
|
||||
str.append("following::");
|
||||
break;
|
||||
case FOLLOWING_SIBLING_AXIS:
|
||||
str.append("following-sibling::");
|
||||
break;
|
||||
case NAMESPACE_AXIS:
|
||||
str.append("namespace::");
|
||||
break;
|
||||
case PARENT_AXIS :
|
||||
str.append("parent::");
|
||||
break;
|
||||
case PRECEDING_AXIS :
|
||||
str.append("preceding::");
|
||||
break;
|
||||
case PRECEDING_SIBLING_AXIS :
|
||||
str.append("preceding-sibling::");
|
||||
break;
|
||||
case SELF_AXIS :
|
||||
str.append("self::");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if ( nodeExpr ) nodeExpr->toString(str);
|
||||
else str.append("null");
|
||||
PredicateList::toString(str);
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txMultiplicativeExpr.cpp,v 1.1 2005/11/02 07:33:50 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents a MultiplicativeExpr, an binary expression that
|
||||
* performs a multiplicative operation between it's lvalue and rvalue:<BR/>
|
||||
* * : multiply
|
||||
* mod : modulus
|
||||
* div : divide
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:50 $
|
||||
**/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Creates a new MultiplicativeExpr using the default operator (MULTIPLY)
|
||||
**/
|
||||
MultiplicativeExpr::MultiplicativeExpr() {
|
||||
this->op = MULTIPLY;
|
||||
this->leftExpr = 0;
|
||||
this->rightExpr = 0;
|
||||
} //-- RelationalExpr
|
||||
|
||||
/**
|
||||
* Creates a new MultiplicativeExpr using the given operator
|
||||
**/
|
||||
MultiplicativeExpr::MultiplicativeExpr(Expr* leftExpr, Expr* rightExpr, short op) {
|
||||
this->op = op;
|
||||
this->leftExpr = leftExpr;
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- MultiplicativeExpr
|
||||
|
||||
MultiplicativeExpr::~MultiplicativeExpr() {
|
||||
delete leftExpr;
|
||||
delete rightExpr;
|
||||
} //-- ~MultiplicativeExpr
|
||||
|
||||
/**
|
||||
* Sets the left side of this MultiplicativeExpr
|
||||
**/
|
||||
void MultiplicativeExpr::setLeftExpr(Expr* leftExpr) {
|
||||
this->leftExpr = leftExpr;
|
||||
} //-- setLeftExpr
|
||||
|
||||
/**
|
||||
* Sets the right side of this MultiplicativeExpr
|
||||
**/
|
||||
void MultiplicativeExpr::setRightExpr(Expr* rightExpr) {
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- setRightExpr
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* MultiplicativeExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
|
||||
double rightDbl = Double::NaN;
|
||||
ExprResult* exprRes = 0;
|
||||
|
||||
if ( rightExpr ) {
|
||||
exprRes = rightExpr->evaluate(context, cs);
|
||||
if ( exprRes ) rightDbl = exprRes->numberValue();
|
||||
delete exprRes;
|
||||
}
|
||||
|
||||
double leftDbl = Double::NaN;
|
||||
if ( leftExpr ) {
|
||||
exprRes = leftExpr->evaluate(context, cs);
|
||||
if ( exprRes ) leftDbl = exprRes->numberValue();
|
||||
delete exprRes;
|
||||
}
|
||||
|
||||
double result = 0;
|
||||
|
||||
switch ( op ) {
|
||||
case DIVIDE:
|
||||
result = (leftDbl / rightDbl);
|
||||
break;
|
||||
case MODULUS:
|
||||
result = fmod(leftDbl, rightDbl);
|
||||
break;
|
||||
default:
|
||||
result = leftDbl * rightDbl;
|
||||
break;
|
||||
}
|
||||
return new NumberResult(result);
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void MultiplicativeExpr::toString(String& str) {
|
||||
|
||||
if ( leftExpr ) leftExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
switch ( op ) {
|
||||
case DIVIDE:
|
||||
str.append(" div ");
|
||||
break;
|
||||
case MODULUS:
|
||||
str.append(" mod ");
|
||||
break;
|
||||
default:
|
||||
str.append(" * ");
|
||||
break;
|
||||
}
|
||||
if ( rightExpr ) rightExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txNodeSetFunctionCall.cpp,v 1.1 2005/11/02 07:33:44 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* NodeSetFunctionCall
|
||||
* A representation of the XPath NodeSet funtions
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:44 $
|
||||
**/
|
||||
|
||||
#include "FunctionLib.h"
|
||||
|
||||
/**
|
||||
* Creates a default NodeSetFunctionCall. The Position function
|
||||
* is the default
|
||||
**/
|
||||
NodeSetFunctionCall::NodeSetFunctionCall() : FunctionCall(XPathNames::POSITION_FN) {
|
||||
type = POSITION;
|
||||
} //-- NodeSetFunctionCall
|
||||
|
||||
/**
|
||||
* Creates a NodeSetFunctionCall of the given type
|
||||
**/
|
||||
NodeSetFunctionCall::NodeSetFunctionCall(short type) : FunctionCall() {
|
||||
this->type = type;
|
||||
switch ( type ) {
|
||||
case COUNT :
|
||||
FunctionCall::setName(XPathNames::COUNT_FN);
|
||||
break;
|
||||
case LAST :
|
||||
FunctionCall::setName(XPathNames::LAST_FN);
|
||||
break;
|
||||
case LOCAL_NAME:
|
||||
FunctionCall::setName(XPathNames::LOCAL_NAME_FN);
|
||||
break;
|
||||
case NAME:
|
||||
FunctionCall::setName(XPathNames::NAME_FN);
|
||||
break;
|
||||
case NAMESPACE_URI:
|
||||
FunctionCall::setName(XPathNames::NAMESPACE_URI_FN);
|
||||
break;
|
||||
default:
|
||||
FunctionCall::setName(XPathNames::POSITION_FN);
|
||||
break;
|
||||
}
|
||||
} //-- NodeSetFunctionCall
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* NodeSetFunctionCall::evaluate(Node* context, ContextState* cs) {
|
||||
NodeSet* nodeSet = (NodeSet*)cs->getNodeSetStack()->peek();
|
||||
ListIterator* iter = params.iterator();
|
||||
Int32 argc = params.getLength();
|
||||
ExprResult* result = 0;
|
||||
Expr* param = 0;
|
||||
switch ( type ) {
|
||||
case COUNT :
|
||||
if ( argc == 1 ) {
|
||||
double count = 0.0;
|
||||
param = (Expr*)iter->next();
|
||||
ExprResult* exprResult = param->evaluate(context, cs);
|
||||
if ( exprResult->getResultType() != ExprResult::NODESET ) {
|
||||
String err("NodeSet expected as argument to count()");
|
||||
cs->recieveError(err);
|
||||
}
|
||||
else count = (double) ((NodeSet*)exprResult)->size();
|
||||
delete exprResult;
|
||||
result = new NumberResult(count);
|
||||
}
|
||||
else {
|
||||
String err(INVALID_PARAM_COUNT);
|
||||
this->toString(err);
|
||||
cs->recieveError(err);
|
||||
result = new NumberResult(0.0);
|
||||
}
|
||||
break;
|
||||
case LAST :
|
||||
if ( nodeSet ) result = new NumberResult((double)nodeSet->size());
|
||||
else result = new NumberResult(0.0);
|
||||
break;
|
||||
case LOCAL_NAME:
|
||||
case NAME:
|
||||
case NAMESPACE_URI :
|
||||
{
|
||||
String name;
|
||||
Node* node = 0;
|
||||
if ( argc < 2 ) {
|
||||
|
||||
//-- check for optional arg
|
||||
if ( argc == 1) {
|
||||
param = (Expr*)iter->next();
|
||||
ExprResult* exprResult = param->evaluate(context, cs);
|
||||
if ( exprResult->getResultType() != ExprResult::NODESET ) {
|
||||
String err("NodeSet expected as argument to ");
|
||||
this->toString(err);
|
||||
cs->recieveError(err);
|
||||
}
|
||||
else {
|
||||
NodeSet* nodes = (NodeSet*)exprResult;
|
||||
if (nodes->size() > 0) node = nodes->get(0);
|
||||
}
|
||||
delete exprResult;
|
||||
}
|
||||
if ( !node ) node = context;
|
||||
|
||||
switch ( type ) {
|
||||
case LOCAL_NAME :
|
||||
XMLUtils::getLocalPart(node->getNodeName(),name);
|
||||
break;
|
||||
case NAMESPACE_URI :
|
||||
XMLUtils::getNameSpace(node->getNodeName(),name);
|
||||
break;
|
||||
default:
|
||||
name = node->getNodeName();
|
||||
break;
|
||||
}
|
||||
result = new StringResult(name);
|
||||
}
|
||||
else {
|
||||
String err(INVALID_PARAM_COUNT);
|
||||
this->toString(err);
|
||||
cs->recieveError(err);
|
||||
result = new StringResult("");
|
||||
}
|
||||
break;
|
||||
}
|
||||
default : //-- position
|
||||
if ( nodeSet )
|
||||
result = new NumberResult((double)nodeSet->indexOf(context)+1);
|
||||
else
|
||||
result = new NumberResult(0.0);
|
||||
break;
|
||||
}
|
||||
delete iter;
|
||||
return result;
|
||||
} //-- evaluate
|
||||
|
||||
|
|
@ -0,0 +1,146 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Olivier Gerardin, ogerardin@vo.lu
|
||||
* -- original author.
|
||||
*/
|
||||
|
||||
/**
|
||||
* NumberFunctionCall
|
||||
* A representation of the XPath Number funtions
|
||||
* @author <A HREF="mailto:ogerardin@vo.lu">Olivier Gerardin</A>
|
||||
**/
|
||||
|
||||
#include "FunctionLib.h"
|
||||
|
||||
/**
|
||||
* Creates a default NumberFunctionCall. The number() function
|
||||
* is the default
|
||||
**/
|
||||
NumberFunctionCall::NumberFunctionCall() : FunctionCall(XPathNames::NUMBER_FN) {
|
||||
type = NUMBER;
|
||||
} //-- NumberFunctionCall
|
||||
|
||||
/**
|
||||
* Creates a NumberFunctionCall of the given type
|
||||
**/
|
||||
NumberFunctionCall::NumberFunctionCall(short type) : FunctionCall() {
|
||||
this->type = type;
|
||||
switch ( type ) {
|
||||
case ROUND :
|
||||
FunctionCall::setName(XPathNames::ROUND_FN);
|
||||
break;
|
||||
case CEILING :
|
||||
FunctionCall::setName(XPathNames::CEILING_FN);
|
||||
break;
|
||||
case FLOOR :
|
||||
FunctionCall::setName(XPathNames::FLOOR_FN);
|
||||
break;
|
||||
case NUMBER :
|
||||
default :
|
||||
FunctionCall::setName(XPathNames::NUMBER_FN);
|
||||
break;
|
||||
}
|
||||
} //-- NumberFunctionCall
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* NumberFunctionCall::evaluate(Node* context, ContextState* cs) {
|
||||
NumberResult* result = new NumberResult();
|
||||
ListIterator* iter = params.iterator();
|
||||
int argc = params.getLength();
|
||||
Expr* param = 0;
|
||||
String err;
|
||||
|
||||
switch ( type ) {
|
||||
case CEILING :
|
||||
if ( requireParams(1, 1, cs) ) {
|
||||
double dbl = evaluateToNumber((Expr*)iter->next(), context, cs);
|
||||
result->setValue(ceil(dbl));
|
||||
}
|
||||
else {
|
||||
result->setValue(0.0);
|
||||
}
|
||||
break;
|
||||
|
||||
case FLOOR :
|
||||
if ( requireParams(1, 1, cs) ) {
|
||||
double dbl = evaluateToNumber((Expr*)iter->next(), context, cs);
|
||||
result->setValue(floor(dbl));
|
||||
}
|
||||
else {
|
||||
result->setValue(0.0);
|
||||
}
|
||||
break;
|
||||
|
||||
case ROUND :
|
||||
if ( requireParams(1, 1, cs) ) {
|
||||
double dbl = evaluateToNumber((Expr*)iter->next(), context, cs);
|
||||
double res = rint(dbl);
|
||||
if ((dbl>0.0) && (res == dbl-0.5)) {
|
||||
// fix for native round function from math library (rint()) which does not
|
||||
// match the XPath spec for positive half values
|
||||
result->setValue(res+1.0);
|
||||
}
|
||||
else {
|
||||
result->setValue(res);
|
||||
}
|
||||
break;
|
||||
}
|
||||
else result->setValue(0.0);
|
||||
break;
|
||||
|
||||
case NUMBER :
|
||||
default : //-- number( object? )
|
||||
if ( requireParams(0, 1, cs) ) {
|
||||
if (iter->hasNext()) {
|
||||
param = (Expr*) iter->next();
|
||||
ExprResult* exprResult = param->evaluate(context, cs);
|
||||
result->setValue(exprResult->numberValue());
|
||||
delete exprResult;
|
||||
}
|
||||
else {
|
||||
String resultStr;
|
||||
String temp;
|
||||
XMLDOMUtils::getNodeValue(context, &temp);
|
||||
if ( cs->isStripSpaceAllowed(context) ) {
|
||||
XMLUtils::stripSpace(temp, resultStr);
|
||||
}
|
||||
else {
|
||||
resultStr.append(temp);
|
||||
}
|
||||
Double dbl(resultStr);
|
||||
result->setValue(dbl.doubleValue());
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = new NumberResult(0.0);
|
||||
}
|
||||
break;
|
||||
}
|
||||
delete iter;
|
||||
return result;
|
||||
} //-- evaluate
|
||||
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txNumberResult.cpp,v 1.1 2005/11/02 07:33:56 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* NumberResult
|
||||
* Represents the a number as the result of evaluating an Expr
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:56 $
|
||||
**/
|
||||
|
||||
#include "ExprResult.h"
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
**/
|
||||
NumberResult::NumberResult() {
|
||||
value = 0.0;
|
||||
} //-- NumberResult
|
||||
|
||||
NumberResult::NumberResult(const NumberResult& nbrResult) {
|
||||
this->value = nbrResult.getValue();
|
||||
} //-- NumberResult
|
||||
|
||||
/**
|
||||
* Creates a new NumberResult with the value of the given double parameter
|
||||
* @param dbl the double to use for initialization of this NumberResult's value
|
||||
**/
|
||||
NumberResult::NumberResult(double dbl) {
|
||||
this->value = dbl;
|
||||
} //-- NumberResult
|
||||
|
||||
/**
|
||||
* Returns the value of this NumberResult
|
||||
* @return the value of this NumberResult
|
||||
**/
|
||||
double NumberResult::getValue() const {
|
||||
return this->value;
|
||||
} //-- getValue
|
||||
|
||||
/**
|
||||
*
|
||||
**/
|
||||
MBool NumberResult::isNaN() const {
|
||||
return Double::isNaN(value);
|
||||
} //-- isNaN
|
||||
/**
|
||||
* Sets the value of this NumberResult
|
||||
* @param dbl the double to use for this NumberResult's value
|
||||
**/
|
||||
void NumberResult::setValue(double dbl) {
|
||||
this->value = dbl;
|
||||
} //-- setValue
|
||||
|
||||
/**
|
||||
* Sets the value of this NumberResult
|
||||
* @param nbrResult the NumberResult to use for setting this NumberResult's value
|
||||
**/
|
||||
void NumberResult::setValue(const NumberResult& nbrResult) {
|
||||
this->value = nbrResult.getValue();
|
||||
} //-- setValue
|
||||
|
||||
/*
|
||||
* Virtual Methods from ExprResult
|
||||
*/
|
||||
|
||||
short NumberResult::getResultType() {
|
||||
return ExprResult::NUMBER;
|
||||
} //-- getResultType
|
||||
|
||||
void NumberResult::stringValue(String& str) {
|
||||
int intVal = (int)value;
|
||||
if (intVal == value) { //-- no fraction
|
||||
Integer::toString(intVal, str);
|
||||
}
|
||||
else Double::toString(value, str);
|
||||
} //-- stringValue
|
||||
|
||||
MBool NumberResult::booleanValue() {
|
||||
// OG+
|
||||
// As per the XPath spec, the boolean value of a number is true if and only if
|
||||
// it is neither positive 0 nor negative 0 nor NaN
|
||||
return (MBool)(this->value != 0.0 && this->value != -0.0 && ! isNaN());
|
||||
// OG-
|
||||
} //-- booleanValue
|
||||
|
||||
double NumberResult::numberValue() {
|
||||
return this->value;
|
||||
} //-- numberValue
|
||||
|
|
@ -0,0 +1,306 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
* Bob Miller, kbob@oblix.com
|
||||
* -- plugged core leak.
|
||||
*
|
||||
* $Id: txPathExpr.cpp,v 1.1 2005/11/02 07:33:37 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
//------------/
|
||||
//- PathExpr -/
|
||||
//------------/
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new PathExpr
|
||||
**/
|
||||
PathExpr::PathExpr() {
|
||||
//-- do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor, will delete all Pattern Expressions
|
||||
**/
|
||||
PathExpr::~PathExpr() {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
iter->next();
|
||||
PathExprItem* pxi = (PathExprItem*)iter->remove();
|
||||
delete pxi->pExpr;
|
||||
delete pxi;
|
||||
}
|
||||
delete iter;
|
||||
} //-- ~PathExpr
|
||||
|
||||
/**
|
||||
* Adds the PatternExpr to this PathExpr
|
||||
* @param expr the Expr to add to this PathExpr
|
||||
* @param index the index at which to add the given Expr
|
||||
**/
|
||||
void PathExpr::addPatternExpr(int index, PatternExpr* expr, short ancestryOp) {
|
||||
if (expr) {
|
||||
PathExprItem* pxi = new PathExprItem;
|
||||
pxi->pExpr = expr;
|
||||
pxi->ancestryOp = ancestryOp;
|
||||
expressions.insert(index, pxi);
|
||||
}
|
||||
} //-- addPattenExpr
|
||||
|
||||
/**
|
||||
* Adds the PatternExpr to this PathExpr
|
||||
* @param expr the Expr to add to this PathExpr
|
||||
**/
|
||||
void PathExpr::addPatternExpr(PatternExpr* expr, short ancestryOp) {
|
||||
if (expr) {
|
||||
PathExprItem* pxi = new PathExprItem;
|
||||
pxi->pExpr = expr;
|
||||
pxi->ancestryOp = ancestryOp;
|
||||
expressions.add(pxi);
|
||||
}
|
||||
} //-- addPattenExpr
|
||||
|
||||
MBool PathExpr::isAbsolute() {
|
||||
if ( expressions.getLength() > 0 ) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
PathExprItem* pxi = (PathExprItem*)iter->next();
|
||||
delete iter;
|
||||
return (pxi->ancestryOp != RELATIVE_OP);
|
||||
}
|
||||
return MB_FALSE;
|
||||
} //-- isAbsolute
|
||||
|
||||
//------------------------------------/
|
||||
//- Virtual methods from PatternExpr -/
|
||||
//------------------------------------/
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* PathExpr::evaluate(Node* context, ContextState* cs) {
|
||||
//-- add selectExpr functionality here
|
||||
|
||||
if ( (!context) || (expressions.getLength() == 0))
|
||||
return new NodeSet(0);
|
||||
|
||||
NodeSet* nodes = new NodeSet();
|
||||
|
||||
if ((isAbsolute()) && (context->getNodeType() != Node::DOCUMENT_NODE))
|
||||
nodes->add(context->getOwnerDocument());
|
||||
else
|
||||
nodes->add(context);
|
||||
|
||||
|
||||
ListIterator* iter = expressions.iterator();
|
||||
|
||||
MBool ancestorMode = MB_FALSE;
|
||||
while ( iter->hasNext() ) {
|
||||
|
||||
PathExprItem* pxi = (PathExprItem*)iter->next();
|
||||
ancestorMode = (ancestorMode || (pxi->ancestryOp == ANCESTOR_OP));
|
||||
NodeSet* tmpNodes = 0;
|
||||
cs->getNodeSetStack()->push(nodes);
|
||||
for (int i = 0; i < nodes->size(); i++) {
|
||||
Node* node = nodes->get(i);
|
||||
#if 0
|
||||
NodeSet* xNodes = (NodeSet*) pxi->pExpr->evaluate(node, cs);
|
||||
#else
|
||||
ExprResult *res = pxi->pExpr->evaluate(node, cs);
|
||||
if (!res || res->getResultType() != ExprResult::NODESET)
|
||||
continue;
|
||||
NodeSet* xNodes = (NodeSet *) res;
|
||||
#endif
|
||||
if ( tmpNodes ) {
|
||||
xNodes->copyInto(*tmpNodes);
|
||||
}
|
||||
else {
|
||||
tmpNodes = xNodes;
|
||||
xNodes = 0;
|
||||
}
|
||||
delete xNodes;
|
||||
//-- handle ancestorMode
|
||||
if ( ancestorMode ) fromDescendants(pxi->pExpr, node, cs, tmpNodes);
|
||||
}
|
||||
delete (NodeSet*) cs->getNodeSetStack()->pop();
|
||||
nodes = tmpNodes;
|
||||
if ( nodes->size() == 0 ) break;
|
||||
}
|
||||
delete iter;
|
||||
|
||||
return nodes;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* Selects from the descendants of the context node
|
||||
* all nodes that match the PatternExpr
|
||||
* -- this will be moving to a Utility class
|
||||
**/
|
||||
void PathExpr::fromDescendants
|
||||
(PatternExpr* pExpr, Node* context, ContextState* cs, NodeSet* nodes)
|
||||
{
|
||||
|
||||
if (( !context ) || (! pExpr )) return;
|
||||
|
||||
NodeList* nl = context->getChildNodes();
|
||||
for (int i = 0; i < nl->getLength(); i++) {
|
||||
Node* child = nl->item(i);
|
||||
if (pExpr->matches(child, context, cs))
|
||||
nodes->add(child);
|
||||
//-- check childs descendants
|
||||
if (child->hasChildNodes())
|
||||
fromDescendants(pExpr, child, cs, nodes);
|
||||
}
|
||||
} //-- fromDescendants
|
||||
|
||||
/**
|
||||
* Returns the default priority of this Pattern based on the given Node,
|
||||
* context Node, and ContextState.
|
||||
* If this pattern does not match the given Node under the current context Node and
|
||||
* ContextState then Negative Infinity is returned.
|
||||
**/
|
||||
double PathExpr::getDefaultPriority(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
if ( matches(node, context, cs) ) {
|
||||
int size = expressions.getLength();
|
||||
if ( size == 1) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
PathExprItem* pxi = (PathExprItem*)iter->next();
|
||||
delete iter;
|
||||
return pxi->pExpr->getDefaultPriority(node, context, cs);
|
||||
}
|
||||
else if ( size > 1 ) {
|
||||
return 0.5;
|
||||
}
|
||||
}
|
||||
return Double::NEGATIVE_INFINITY;
|
||||
} //-- getDefaultPriority
|
||||
|
||||
/**
|
||||
* Determines whether this PatternExpr matches the given node within
|
||||
* the given context
|
||||
**/
|
||||
MBool PathExpr::matches(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
if ( (!node) || (expressions.getLength() == 0))
|
||||
return MB_FALSE;
|
||||
|
||||
NodeSet nodes;
|
||||
nodes.add(node);
|
||||
|
||||
|
||||
ListIterator* iter = expressions.iterator();
|
||||
iter->reverse();
|
||||
|
||||
NodeSet tmpNodes;
|
||||
MBool result = MB_FALSE;
|
||||
while ( iter->hasNext() ) {
|
||||
|
||||
PathExprItem* pxi = (PathExprItem*)iter->next();
|
||||
for (int i = 0; i < nodes.size(); i++) {
|
||||
|
||||
Node* tnode = nodes.get(i);
|
||||
|
||||
//-- select node's parent or ancestors
|
||||
switch (pxi->ancestryOp) {
|
||||
case ANCESTOR_OP:
|
||||
{
|
||||
Node* parent = tnode;
|
||||
while (parent = cs->getParentNode(parent)) {
|
||||
if (pxi->pExpr->matches(tnode, parent, cs))
|
||||
tmpNodes.add(parent);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PARENT_OP:
|
||||
{
|
||||
Node* parent = cs->getParentNode(tnode);
|
||||
if (parent) {
|
||||
if (pxi->pExpr->matches(tnode, parent, cs))
|
||||
tmpNodes.add(parent);
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if ( !iter->hasNext() ) {
|
||||
result = pxi->pExpr->matches(tnode, context, cs);
|
||||
}
|
||||
else {
|
||||
//-- error in expression
|
||||
tmpNodes.clear();
|
||||
nodes.clear();
|
||||
delete iter;
|
||||
return MB_FALSE;
|
||||
}
|
||||
break;
|
||||
}
|
||||
} //-- for
|
||||
nodes.clear();
|
||||
tmpNodes.copyInto(nodes);
|
||||
tmpNodes.clear();
|
||||
}
|
||||
|
||||
delete iter;
|
||||
|
||||
if ( this->isAbsolute()) {
|
||||
Node* doc = node->getOwnerDocument();
|
||||
return (MBool) nodes.contains(doc);
|
||||
}
|
||||
|
||||
return (MBool) (result || (nodes.size() > 0));
|
||||
|
||||
} //-- matches
|
||||
|
||||
|
||||
/**
|
||||
* Returns the String representation of this PatternExpr.
|
||||
* @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 PatternExpr.
|
||||
**/
|
||||
void PathExpr::toString(String& dest) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
//-- set operator
|
||||
PathExprItem* pxi = (PathExprItem*)iter->next();
|
||||
switch ( pxi->ancestryOp ) {
|
||||
case ANCESTOR_OP:
|
||||
dest.append("//");
|
||||
break;
|
||||
case PARENT_OP:
|
||||
dest.append('/');
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
pxi->pExpr->toString(dest);
|
||||
}
|
||||
delete iter;
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txPredicateList.cpp,v 1.1 2005/11/02 07:33:50 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
/**
|
||||
* Represents an ordered list of Predicates,
|
||||
* for use with Step and Filter Expressions
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:50 $
|
||||
**/
|
||||
//-- PredicateList Implementation --/
|
||||
|
||||
PredicateList::PredicateList() {
|
||||
} //-- PredicateList
|
||||
|
||||
/**
|
||||
* Destructor, will delete all Expressions in the list, so remove
|
||||
* any you may need
|
||||
**/
|
||||
PredicateList::~PredicateList() {
|
||||
//cout << "-PredicateList() - start"<<endl;
|
||||
ListIterator* iter = predicates.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
iter->next();
|
||||
Expr* expr = (Expr*) iter->remove();
|
||||
delete expr;
|
||||
}
|
||||
delete iter;
|
||||
//cout << "~PredicateList() - end"<<endl;
|
||||
} //-- ~PredicateList
|
||||
|
||||
/**
|
||||
* Adds the given Expr to the list
|
||||
* @param expr the Expr to add to the list
|
||||
**/
|
||||
void PredicateList::add(Expr* expr) {
|
||||
predicates.add(expr);
|
||||
} //-- add
|
||||
|
||||
void PredicateList::evaluatePredicates(NodeSet* nodes, ContextState* cs) {
|
||||
if ( !nodes ) return;
|
||||
|
||||
ListIterator* iter = predicates.iterator();
|
||||
NodeSet remNodes(nodes->size());
|
||||
|
||||
Stack* nsStack = cs->getNodeSetStack();
|
||||
nsStack->push(nodes);
|
||||
while ( iter->hasNext() ) {
|
||||
int nIdx = 0;
|
||||
|
||||
Expr* expr = (Expr*) iter->next();
|
||||
//-- filter each node currently in the NodeSet
|
||||
for (nIdx = 0; nIdx < nodes->size(); nIdx++) {
|
||||
|
||||
Node* node = nodes->get(nIdx);
|
||||
|
||||
//-- if expr evaluates to true using the node as it's context,
|
||||
//-- then we can keep it, otherwise add to remove list
|
||||
ExprResult* exprResult = expr->evaluate(node,cs);
|
||||
if ( !exprResult ) {
|
||||
cout << "ExprResult == null" << endl;
|
||||
}
|
||||
else {
|
||||
switch(exprResult->getResultType()) {
|
||||
case ExprResult::NUMBER :
|
||||
{
|
||||
//-- handle default position()
|
||||
int position = nodes->indexOf(node)+1;
|
||||
if (( position <= 0 ) ||
|
||||
( ((double)position) != exprResult->numberValue()))
|
||||
remNodes.add(node);
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
if (! exprResult->booleanValue() ) remNodes.add(node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
//-- remove unmatched nodes
|
||||
for (nIdx = 0; nIdx < remNodes.size(); nIdx++)
|
||||
nodes->remove(remNodes.get(nIdx));
|
||||
//-- clear remove list
|
||||
remNodes.clear();
|
||||
}
|
||||
nsStack->pop();
|
||||
delete iter;
|
||||
} //-- evaluatePredicates
|
||||
|
||||
/**
|
||||
* returns true if this predicate list is empty
|
||||
**/
|
||||
MBool PredicateList::isEmpty() {
|
||||
return (MBool)(predicates.getLength() == 0);
|
||||
} //-- isEmpty
|
||||
|
||||
/**
|
||||
* Removes the given Expr from the list
|
||||
* @param expr the Expr to remove from the list
|
||||
**/
|
||||
Expr* PredicateList::remove(Expr* expr) {
|
||||
return (Expr*)predicates.remove(expr);
|
||||
} //-- remove
|
||||
|
||||
void PredicateList::toString(String& dest) {
|
||||
|
||||
ListIterator* iter = predicates.iterator();
|
||||
|
||||
while ( iter->hasNext() ) {
|
||||
Expr* expr = (Expr*) iter->next();
|
||||
dest.append("[");
|
||||
expr->toString(dest);
|
||||
dest.append("]");
|
||||
}
|
||||
delete iter;
|
||||
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,208 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txRelationalExpr.cpp,v 1.1 2005/11/02 07:33:44 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
//------------------/
|
||||
//- RelationalExpr -/
|
||||
//------------------/
|
||||
|
||||
RelationalExpr::RelationalExpr() {
|
||||
this->op = EQUAL;
|
||||
this->leftExpr = 0;
|
||||
this->rightExpr = 0;
|
||||
} //-- RelationalExpr
|
||||
|
||||
RelationalExpr::RelationalExpr(Expr* leftExpr, Expr* rightExpr, short op) {
|
||||
this->op = op;
|
||||
this->leftExpr = leftExpr;
|
||||
this->rightExpr = rightExpr;
|
||||
} //-- RelationalExpr
|
||||
|
||||
RelationalExpr::~RelationalExpr() {
|
||||
delete leftExpr;
|
||||
delete rightExpr;
|
||||
} //-- ~RelationalExpr
|
||||
|
||||
/**
|
||||
* Compares the two ExprResults based on XPath 1.0 Recommendation (section 3.4)
|
||||
**/
|
||||
MBool RelationalExpr::compareResults(ExprResult* left, ExprResult* right) {
|
||||
|
||||
|
||||
short ltype = left->getResultType();
|
||||
short rtype = right->getResultType();
|
||||
|
||||
MBool result = MB_FALSE;
|
||||
|
||||
//-- handle case for just Left NodeSet or Both NodeSets
|
||||
if (ltype == ExprResult::NODESET) {
|
||||
NodeSet* nodeSet = (NodeSet*)left;
|
||||
for ( int i = 0; i < nodeSet->size(); i++) {
|
||||
String str;
|
||||
Node* node = nodeSet->get(i);
|
||||
XMLDOMUtils::getNodeValue(node, &str);
|
||||
StringResult strResult(str);
|
||||
result = compareResults(&strResult, right);
|
||||
if ( result ) break;
|
||||
}
|
||||
}
|
||||
//-- handle case for Just Right NodeSet
|
||||
else if ( rtype == ExprResult::NODESET) {
|
||||
NodeSet* nodeSet = (NodeSet*)right;
|
||||
for ( int i = 0; i < nodeSet->size(); i++) {
|
||||
String str;
|
||||
Node* node = nodeSet->get(i);
|
||||
XMLDOMUtils::getNodeValue(node, &str);
|
||||
StringResult strResult(str);
|
||||
result = compareResults(left, &strResult);
|
||||
if ( result ) break;
|
||||
}
|
||||
}
|
||||
//-- neither NodeSet
|
||||
else {
|
||||
if ( op == NOT_EQUAL) {
|
||||
|
||||
if ((ltype == ExprResult::BOOLEAN)
|
||||
|| (rtype == ExprResult::BOOLEAN)) {
|
||||
result = (left->booleanValue() != right->booleanValue());
|
||||
}
|
||||
else if ((ltype == ExprResult::NUMBER) ||
|
||||
(rtype == ExprResult::NUMBER)) {
|
||||
result = (left->numberValue() != right->numberValue());
|
||||
}
|
||||
else {
|
||||
String lStr;
|
||||
left->stringValue(lStr);
|
||||
String rStr;
|
||||
right->stringValue(rStr);
|
||||
result = !lStr.isEqual(rStr);
|
||||
}
|
||||
}
|
||||
else if ( op == EQUAL) {
|
||||
|
||||
if ((ltype == ExprResult::BOOLEAN)
|
||||
|| (rtype == ExprResult::BOOLEAN)) {
|
||||
result = (left->booleanValue() == right->booleanValue());
|
||||
}
|
||||
else if ((ltype == ExprResult::NUMBER) ||
|
||||
(rtype == ExprResult::NUMBER)) {
|
||||
result = (left->numberValue() == right->numberValue());
|
||||
}
|
||||
else {
|
||||
String lStr;
|
||||
left->stringValue(lStr);
|
||||
String rStr;
|
||||
right->stringValue(rStr);
|
||||
result = lStr.isEqual(rStr);
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
double leftDbl = left->numberValue();
|
||||
double rightDbl = right->numberValue();
|
||||
switch( op ) {
|
||||
case LESS_THAN:
|
||||
result = (MBool) (leftDbl < rightDbl);
|
||||
break;
|
||||
case LESS_OR_EQUAL:
|
||||
result = (MBool) (leftDbl <= rightDbl);
|
||||
break;
|
||||
case GREATER_THAN :
|
||||
result = (MBool) (leftDbl > rightDbl);
|
||||
break;
|
||||
case GREATER_OR_EQUAL:
|
||||
result = (MBool) (leftDbl >= rightDbl);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} //-- compareResult
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* RelationalExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
//-- get result of left expression
|
||||
ExprResult* lResult = 0;
|
||||
if ( leftExpr ) lResult = leftExpr->evaluate(context, cs);
|
||||
else return new BooleanResult();
|
||||
|
||||
//-- get result of right expr
|
||||
ExprResult* rResult = 0;
|
||||
if ( rightExpr ) rResult = rightExpr->evaluate(context, cs);
|
||||
else {
|
||||
delete lResult;
|
||||
return new BooleanResult();
|
||||
}
|
||||
return new BooleanResult(compareResults(lResult, rResult));
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void RelationalExpr::toString(String& str) {
|
||||
|
||||
if ( leftExpr ) leftExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
switch ( op ) {
|
||||
case NOT_EQUAL:
|
||||
str.append("!=");
|
||||
break;
|
||||
case LESS_THAN:
|
||||
str.append("<");
|
||||
break;
|
||||
case LESS_OR_EQUAL:
|
||||
str.append("<=");
|
||||
break;
|
||||
case GREATER_THAN :
|
||||
str.append(">");
|
||||
break;
|
||||
case GREATER_OR_EQUAL:
|
||||
str.append(">=");
|
||||
break;
|
||||
default:
|
||||
str.append("=");
|
||||
break;
|
||||
}
|
||||
|
||||
if ( rightExpr ) rightExpr->toString(str);
|
||||
else str.append("null");
|
||||
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txRootExpr.cpp,v 1.1 2005/11/02 07:33:56 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
MBool RootExpr::isAbsolute() {
|
||||
return MB_TRUE;
|
||||
} //-- isAbsolute
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* RootExpr::evaluate(Node* context, ContextState* cs) {
|
||||
NodeSet* nodeSet = new NodeSet();
|
||||
if ( !context ) return nodeSet;
|
||||
nodeSet->add(context->getOwnerDocument());
|
||||
return nodeSet;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* Returns the default priority of this Pattern based on the given Node,
|
||||
* context Node, and ContextState.
|
||||
* If this pattern does not match the given Node under the current context Node and
|
||||
* ContextState then Negative Infinity is returned.
|
||||
**/
|
||||
double RootExpr::getDefaultPriority(Node* node, Node* context, ContextState* cs) {
|
||||
if ( matches(node, context, cs) ) {
|
||||
return 0.5;
|
||||
}
|
||||
else return Double::NEGATIVE_INFINITY;
|
||||
} //-- getDefaultPriority
|
||||
|
||||
/**
|
||||
* Determines whether this NodeExpr matches the given node within
|
||||
* the given context
|
||||
**/
|
||||
MBool RootExpr::matches(Node* node, Node* context, ContextState* cs) {
|
||||
if ( node ) {
|
||||
return (MBool) (node->getNodeType() == Node::DOCUMENT_NODE);
|
||||
}
|
||||
return MB_FALSE;
|
||||
} //-- matches
|
||||
|
||||
/**
|
||||
* Returns the String representation of this PatternExpr.
|
||||
* @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 PatternExpr.
|
||||
**/
|
||||
void RootExpr::toString(String& dest) {
|
||||
dest.append('/');
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,243 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txStringFunctionCall.cpp,v 1.1 2005/11/02 07:33:36 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* StringFunctionCall
|
||||
* A representation of the XPath String funtions
|
||||
* @author <A HREF="mailto:kvisco@ziplink.net">Keith Visco</A>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:36 $
|
||||
**/
|
||||
|
||||
#include "FunctionLib.h"
|
||||
|
||||
/**
|
||||
* Creates a default StringFunctionCall. The string() function
|
||||
* is the default
|
||||
**/
|
||||
StringFunctionCall::StringFunctionCall() : FunctionCall(XPathNames::STRING_FN) {
|
||||
type = STRING;
|
||||
} //-- StringFunctionCall
|
||||
|
||||
/**
|
||||
* Creates a StringFunctionCall of the given type
|
||||
**/
|
||||
StringFunctionCall::StringFunctionCall(short type) : FunctionCall() {
|
||||
this->type = type;
|
||||
switch ( type ) {
|
||||
case CONCAT:
|
||||
FunctionCall::setName(XPathNames::CONCAT_FN);
|
||||
break;
|
||||
case CONTAINS:
|
||||
FunctionCall::setName(XPathNames::CONTAINS_FN);
|
||||
break;
|
||||
case STARTS_WITH:
|
||||
FunctionCall::setName(XPathNames::STARTS_WITH_FN);
|
||||
break;
|
||||
case STRING_LENGTH:
|
||||
FunctionCall::setName(XPathNames::STRING_LENGTH_FN);
|
||||
break;
|
||||
case SUBSTRING:
|
||||
FunctionCall::setName(XPathNames::SUBSTRING_FN);
|
||||
break;
|
||||
case SUBSTRING_AFTER:
|
||||
FunctionCall::setName(XPathNames::SUBSTRING_AFTER_FN);
|
||||
break;
|
||||
case SUBSTRING_BEFORE:
|
||||
FunctionCall::setName(XPathNames::SUBSTRING_BEFORE_FN);
|
||||
break;
|
||||
case TRANSLATE:
|
||||
FunctionCall::setName(XPathNames::TRANSLATE_FN);
|
||||
break;
|
||||
default:
|
||||
FunctionCall::setName(XPathNames::STRING_FN);
|
||||
break;
|
||||
}
|
||||
} //-- StringFunctionCall
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* StringFunctionCall::evaluate(Node* context, ContextState* cs) {
|
||||
ListIterator* iter = params.iterator();
|
||||
Int32 argc = params.getLength();
|
||||
String err;
|
||||
ExprResult* result = 0;
|
||||
switch ( type ) {
|
||||
case CONCAT :
|
||||
if ( requireParams(2, cs) ) {
|
||||
String resultStr;
|
||||
while(iter->hasNext()) {
|
||||
evaluateToString((Expr*)iter->next(),context, cs, resultStr);
|
||||
}
|
||||
result = new StringResult(resultStr);
|
||||
}
|
||||
else result = new StringResult("");
|
||||
break;
|
||||
case CONTAINS :
|
||||
if ( requireParams(2, 2, cs) ) {
|
||||
String arg1, arg2;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg1);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg2);
|
||||
result = new BooleanResult((MBool)(arg1.indexOf(arg2) >= 0));
|
||||
}
|
||||
else result = new BooleanResult(MB_FALSE);
|
||||
break;
|
||||
|
||||
case STARTS_WITH :
|
||||
if ( requireParams(2, 2, cs) ) {
|
||||
String arg1, arg2;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg1);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg2);
|
||||
result = new BooleanResult((MBool)(arg1.indexOf(arg2) == 0));
|
||||
}
|
||||
else result = new BooleanResult(MB_FALSE);
|
||||
break;
|
||||
case STRING_LENGTH:
|
||||
if ( requireParams(0, 1, cs) ) {
|
||||
String resultStr;
|
||||
if ( argc == 1) {
|
||||
evaluateToString((Expr*)iter->next(),context, cs, resultStr);
|
||||
}
|
||||
else XMLDOMUtils::getNodeValue(context, &resultStr);
|
||||
result = new NumberResult( (double) resultStr.length());
|
||||
}
|
||||
else result = new NumberResult(0.0);
|
||||
break;
|
||||
case SUBSTRING:
|
||||
if ( requireParams(2, 3, cs) ) {
|
||||
String src;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, src);
|
||||
double dbl = evaluateToNumber((Expr*)iter->next(), context, cs);
|
||||
|
||||
//-- check for NaN
|
||||
if ( Double::isNaN(dbl)) {
|
||||
result = new StringResult("");
|
||||
break;
|
||||
}
|
||||
|
||||
Int32 startIdx = (Int32)ceil(dbl);
|
||||
Int32 endIdx = src.length();
|
||||
if ( argc == 3) {
|
||||
dbl += evaluateToNumber((Expr*)iter->next(),context, cs);
|
||||
if (dbl == Double::POSITIVE_INFINITY) endIdx++;
|
||||
else if ( dbl == Double::NEGATIVE_INFINITY ) endIdx = 0;
|
||||
else endIdx = (Int32)floor(dbl);
|
||||
}
|
||||
String resultStr;
|
||||
//-- strings are indexed starting at 1 for XSL
|
||||
//-- adjust to a 0-based index
|
||||
if (startIdx > 0) startIdx--;
|
||||
else if (startIdx == 0 ) endIdx--;
|
||||
else startIdx=0;
|
||||
src.subString(startIdx,endIdx,resultStr);
|
||||
result = new StringResult(resultStr);
|
||||
}
|
||||
else result = new StringResult("");
|
||||
break;
|
||||
|
||||
case SUBSTRING_AFTER:
|
||||
if ( requireParams(2, 2, cs) ) {
|
||||
String arg1, arg2;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg1);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg2);
|
||||
Int32 idx = arg1.indexOf(arg2);
|
||||
if ((idx >= 0)&&(idx<arg1.length())) {
|
||||
Int32 len = arg2.length();
|
||||
arg2.clear();
|
||||
arg1.subString(idx+len,arg2);
|
||||
result = new StringResult(arg2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
result = new StringResult("");
|
||||
break;
|
||||
case SUBSTRING_BEFORE:
|
||||
if ( requireParams(2, 2, cs) ) {
|
||||
String arg1, arg2;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg1);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, arg2);
|
||||
Int32 idx = arg1.indexOf(arg2);
|
||||
if ((idx >= 0)&&(idx<arg1.length())) {
|
||||
arg2.clear();
|
||||
arg1.subString(0,idx,arg2);
|
||||
result = new StringResult(arg2);
|
||||
break;
|
||||
}
|
||||
}
|
||||
result = new StringResult("");
|
||||
break;
|
||||
case TRANSLATE:
|
||||
if ( requireParams(3, 3, cs) ) {
|
||||
String src, oldChars, newChars;
|
||||
evaluateToString((Expr*)iter->next(),context, cs, src);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, oldChars);
|
||||
evaluateToString((Expr*)iter->next(),context, cs, newChars);
|
||||
Int32 size = src.length();
|
||||
UNICODE_CHAR* chars = src.toUnicode(new UNICODE_CHAR[size]);
|
||||
src.clear();
|
||||
Int32 newIdx = 0;
|
||||
Int32 i;
|
||||
for (i = 0; i < size; i++) {
|
||||
Int32 idx = oldChars.indexOf(chars[i]);
|
||||
if (idx >= 0) {
|
||||
char nchar = newChars.charAt(idx);
|
||||
if (nchar != -1) src.append(nchar);
|
||||
}
|
||||
else src.append(chars[i]);
|
||||
}
|
||||
delete chars;
|
||||
return new StringResult(src);
|
||||
}
|
||||
result = new StringResult("");
|
||||
break;
|
||||
|
||||
default : //-- string( object? )
|
||||
if ( requireParams(0, 1, cs) ) {
|
||||
String resultStr;
|
||||
if (iter->hasNext()) {
|
||||
evaluateToString((Expr*)iter->next(),context, cs, resultStr);
|
||||
}
|
||||
else {
|
||||
String temp;
|
||||
XMLDOMUtils::getNodeValue(context, &temp);
|
||||
if ( cs->isStripSpaceAllowed(context) ) {
|
||||
XMLUtils::stripSpace(temp, resultStr);
|
||||
}
|
||||
else resultStr.append(temp);
|
||||
}
|
||||
result = new StringResult(resultStr);
|
||||
}
|
||||
else result = new StringResult("");
|
||||
break;
|
||||
}
|
||||
delete iter;
|
||||
return result;
|
||||
} //-- evaluate
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txStringResult.cpp,v 1.1 2005/11/02 07:33:52 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
/**
|
||||
* StringResult
|
||||
* Represents a String as a Result of evaluating an Expr
|
||||
* @author <a href="mailto:kvisco@ziplink.net">Keith Visco</a>
|
||||
* @version $Revision: 1.1 $ $Date: 2005/11/02 07:33:52 $
|
||||
**/
|
||||
#include "ExprResult.h"
|
||||
|
||||
|
||||
/**
|
||||
* Default Constructor
|
||||
**/
|
||||
StringResult::StringResult() {
|
||||
} //-- StringResult
|
||||
|
||||
/**
|
||||
* Creates a new StringResult with the value of the given String parameter
|
||||
* @param str the String to use for initialization of this StringResult's value
|
||||
**/
|
||||
StringResult::StringResult(String& str) {
|
||||
//-- copy str
|
||||
this->value = str;
|
||||
} //-- StringResult
|
||||
|
||||
/**
|
||||
* Creates a new StringResult with the value of the given String parameter
|
||||
* @param str the String to use for initialization of this StringResult's value
|
||||
**/
|
||||
StringResult::StringResult(const String& str) {
|
||||
//-- copy str
|
||||
this->value = str;
|
||||
} //-- StringResult
|
||||
|
||||
/**
|
||||
* Returns the value of this StringResult
|
||||
**/
|
||||
String& StringResult::getValue() {
|
||||
return this->value;
|
||||
} //-- getValue
|
||||
|
||||
/**
|
||||
* Sets the value of this StringResult
|
||||
* @param str the String to use for this StringResult's value
|
||||
**/
|
||||
void StringResult::setValue(const String& str){
|
||||
// copy str
|
||||
this->value = str;
|
||||
} //-- setValue
|
||||
|
||||
/*
|
||||
* Virtual Methods from ExprResult
|
||||
*/
|
||||
|
||||
short StringResult::getResultType() {
|
||||
return ExprResult::STRING;
|
||||
} //-- getResultType
|
||||
|
||||
void StringResult::stringValue(String& str) {
|
||||
str.append(this->value);
|
||||
} //-- stringValue
|
||||
|
||||
MBool StringResult::booleanValue() {
|
||||
return (MBool)(this->value.length());
|
||||
} //-- booleanValue
|
||||
|
||||
double StringResult::numberValue() {
|
||||
Double dbl(value);
|
||||
return dbl.doubleValue();
|
||||
} //-- numberValue
|
||||
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txUnionExpr.cpp,v 1.1 2005/11/02 07:33:50 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
//-------------/
|
||||
//- UnionExpr -/
|
||||
//-------------/
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new UnionExpr
|
||||
**/
|
||||
UnionExpr::UnionExpr() {
|
||||
//-- do nothing
|
||||
}
|
||||
|
||||
/**
|
||||
* Destructor, will delete all Path Expressions
|
||||
**/
|
||||
UnionExpr::~UnionExpr() {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
iter->next();
|
||||
delete (PathExpr*)iter->remove();
|
||||
}
|
||||
delete iter;
|
||||
} //-- ~UnionExpr
|
||||
|
||||
/**
|
||||
* Adds the PathExpr to this UnionExpr
|
||||
* @param pathExpr the PathExpr to add to this UnionExpr
|
||||
**/
|
||||
void UnionExpr::addPathExpr(PathExpr* pathExpr) {
|
||||
if (pathExpr) expressions.add(pathExpr);
|
||||
} //-- addPathExpr
|
||||
|
||||
/**
|
||||
* Adds the PathExpr to this UnionExpr
|
||||
* @param pathExpr the PathExpr to add to this UnionExpr
|
||||
**/
|
||||
void UnionExpr::addPathExpr(int index, PathExpr* pathExpr) {
|
||||
if (pathExpr) expressions.insert(index, pathExpr);
|
||||
} //-- addPathExpr
|
||||
|
||||
//------------------------------------/
|
||||
//- Virtual methods from PatternExpr -/
|
||||
//------------------------------------/
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* UnionExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
if ( (!context) || (expressions.getLength() == 0))
|
||||
return new NodeSet(0);
|
||||
|
||||
NodeSet* nodes = new NodeSet();
|
||||
|
||||
ListIterator* iter = expressions.iterator();
|
||||
|
||||
while ( iter->hasNext() ) {
|
||||
|
||||
PathExpr* pExpr = (PathExpr*)iter->next();
|
||||
NodeSet* tmpNodes = (NodeSet*)pExpr->evaluate(context, cs);
|
||||
for (int j = 0; j < tmpNodes->size(); j++) {
|
||||
nodes->add(tmpNodes->get(j));
|
||||
}
|
||||
delete tmpNodes;
|
||||
}
|
||||
|
||||
delete iter;
|
||||
return nodes;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* Returns the default priority of this Pattern based on the given Node,
|
||||
* context Node, and ContextState.
|
||||
* If this pattern does not match the given Node under the current context Node and
|
||||
* ContextState then Negative Infinity is returned.
|
||||
**/
|
||||
double UnionExpr::getDefaultPriority(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
//-- find highest priority
|
||||
double priority = Double::NEGATIVE_INFINITY;
|
||||
ListIterator* iter = expressions.iterator();
|
||||
while ( iter->hasNext() ) {
|
||||
PathExpr* pExpr = (PathExpr*)iter->next();
|
||||
if ( pExpr->matches(node, context, cs) ) {
|
||||
double tmpPriority = pExpr->getDefaultPriority(node, context, cs);
|
||||
priority = (tmpPriority > priority) ? tmpPriority : priority;
|
||||
}
|
||||
}
|
||||
delete iter;
|
||||
return priority;
|
||||
|
||||
} //-- getDefaultPriority
|
||||
|
||||
/**
|
||||
* Determines whether this UnionExpr matches the given node within
|
||||
* the given context
|
||||
**/
|
||||
MBool UnionExpr::matches(Node* node, Node* context, ContextState* cs) {
|
||||
|
||||
ListIterator* iter = expressions.iterator();
|
||||
|
||||
while ( iter->hasNext() ) {
|
||||
PathExpr* pExpr = (PathExpr*)iter->next();
|
||||
if ( pExpr->matches(node, context, cs) ) {
|
||||
delete iter;
|
||||
return MB_TRUE;
|
||||
}
|
||||
}
|
||||
delete iter;
|
||||
return MB_FALSE;
|
||||
} //-- matches
|
||||
|
||||
|
||||
/**
|
||||
* Returns the String representation of this PatternExpr.
|
||||
* @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 PatternExpr.
|
||||
**/
|
||||
void UnionExpr::toString(String& dest) {
|
||||
ListIterator* iter = expressions.iterator();
|
||||
|
||||
short count = 0;
|
||||
while ( iter->hasNext() ) {
|
||||
//-- set operator
|
||||
if (count > 0) dest.append(" | ");
|
||||
((PathExpr*)iter->next())->toString(dest);
|
||||
++count;
|
||||
}
|
||||
delete iter;
|
||||
} //-- toString
|
||||
|
|
@ -0,0 +1,118 @@
|
|||
/*
|
||||
* The contents of this file are subject to the Mozilla Public
|
||||
* License Version 1.1 (the "License"); you may not use this file
|
||||
* except in compliance with the License. You may obtain a copy of
|
||||
* the License at http://www.mozilla.org/MPL/
|
||||
*
|
||||
* Software distributed under the License is distributed on an "AS
|
||||
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
||||
* implied. See the License for the specific language governing
|
||||
* rights and limitations under the License.
|
||||
*
|
||||
* The Original Code is TransforMiiX XSLT processor.
|
||||
*
|
||||
* The Initial Developer of the Original Code is The MITRE Corporation.
|
||||
* Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
|
||||
*
|
||||
* Portions created by Keith Visco as a Non MITRE employee,
|
||||
* (C) 1999 Keith Visco. All Rights Reserved.
|
||||
*
|
||||
* Contributor(s):
|
||||
* Keith Visco, kvisco@ziplink.net
|
||||
* -- original author.
|
||||
*
|
||||
* $Id: txVariableRefExpr.cpp,v 1.1 2005/11/02 07:33:55 kvisco%ziplink.net Exp $
|
||||
*/
|
||||
|
||||
#include "Expr.h"
|
||||
|
||||
//-------------------/
|
||||
//- VariableRefExpr -/
|
||||
//-------------------/
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
**/
|
||||
VariableRefExpr::VariableRefExpr() {
|
||||
} //-- VariableRefExpr
|
||||
|
||||
/**
|
||||
* Creates a VariableRefExpr with the given variable name
|
||||
**/
|
||||
VariableRefExpr::VariableRefExpr(const String& name) {
|
||||
this->name = name;
|
||||
} //-- VariableRefExpr
|
||||
|
||||
/**
|
||||
* Creates a VariableRefExpr with the given variable name
|
||||
**/
|
||||
VariableRefExpr::VariableRefExpr(String& name) {
|
||||
this->name = name;
|
||||
} //-- VariableRefExpr
|
||||
|
||||
/**
|
||||
* Default destructor
|
||||
**/
|
||||
VariableRefExpr::~VariableRefExpr() {
|
||||
} //-- ~VariableRefExpr
|
||||
|
||||
/**
|
||||
* 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
|
||||
**/
|
||||
ExprResult* VariableRefExpr::evaluate(Node* context, ContextState* cs) {
|
||||
|
||||
ExprResult* exprResult = cs->getVariable(name);
|
||||
//-- make copy to prevent deletetion
|
||||
//-- I know, I should add a #copy method to ExprResult, I will
|
||||
ExprResult* copyOfResult = 0;
|
||||
|
||||
if ( exprResult ) {
|
||||
switch ( exprResult->getResultType() ) {
|
||||
//-- BooleanResult
|
||||
case ExprResult::BOOLEAN :
|
||||
copyOfResult = new BooleanResult(exprResult->booleanValue());
|
||||
break;
|
||||
//-- NodeSet
|
||||
case ExprResult::NODESET :
|
||||
{
|
||||
NodeSet* src = (NodeSet*)exprResult;
|
||||
NodeSet* dest = new NodeSet(src->size());
|
||||
for ( int i = 0; i < src->size(); i++)
|
||||
dest->add(src->get(i));
|
||||
copyOfResult = dest;
|
||||
break;
|
||||
}
|
||||
//-- NumberResult
|
||||
case ExprResult::NUMBER :
|
||||
copyOfResult = new NumberResult(exprResult->numberValue());
|
||||
break;
|
||||
//-- StringResult
|
||||
default:
|
||||
StringResult* strResult = new StringResult();
|
||||
exprResult->stringValue(strResult->getValue());
|
||||
copyOfResult = strResult;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else copyOfResult = new StringResult();
|
||||
|
||||
return copyOfResult;
|
||||
} //-- evaluate
|
||||
|
||||
/**
|
||||
* 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.
|
||||
**/
|
||||
void VariableRefExpr::toString(String& str) {
|
||||
str.append('$');
|
||||
str.append(name);
|
||||
} //-- toString
|
||||
|
Загрузка…
Ссылка в новой задаче