2005-11-02 10:42:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2005-11-02 20:34:14 +03:00
|
|
|
#include "txExpr.h"
|
2005-11-02 10:41:38 +03:00
|
|
|
#include "txNodeSet.h"
|
2005-11-02 10:40:48 +03:00
|
|
|
#include "txIXPathContext.h"
|
2006-04-01 06:12:15 +04:00
|
|
|
#include "txXPathTreeWalker.h"
|
2005-11-02 10:34:13 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the two ExprResults based on XPath 1.0 Recommendation (section 3.4)
|
2005-11-02 10:40:21 +03:00
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2005-11-02 10:40:48 +03:00
|
|
|
RelationalExpr::compareResults(txIEvalContext* aContext, txAExprResult* aLeft,
|
|
|
|
txAExprResult* aRight)
|
2005-11-02 10:40:21 +03:00
|
|
|
{
|
|
|
|
short ltype = aLeft->getResultType();
|
|
|
|
short rtype = aRight->getResultType();
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult rv = NS_OK;
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2005-11-02 10:40:21 +03:00
|
|
|
// Handle case for just Left NodeSet or Both NodeSets
|
2005-11-02 10:40:48 +03:00
|
|
|
if (ltype == txAExprResult::NODESET) {
|
|
|
|
if (rtype == txAExprResult::BOOLEAN) {
|
2005-11-02 10:40:21 +03:00
|
|
|
BooleanResult leftBool(aLeft->booleanValue());
|
2005-11-02 10:40:48 +03:00
|
|
|
return compareResults(aContext, &leftBool, aRight);
|
2005-11-02 10:37:36 +03:00
|
|
|
}
|
2005-11-02 10:37:35 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
txNodeSet* nodeSet = static_cast<txNodeSet*>(aLeft);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StringResult> strResult;
|
2005-11-02 10:40:48 +03:00
|
|
|
rv = aContext->recycler()->getStringResult(getter_AddRefs(strResult));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2005-11-02 10:40:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2005-11-02 10:40:21 +03:00
|
|
|
for (i = 0; i < nodeSet->size(); ++i) {
|
2005-11-02 10:40:48 +03:00
|
|
|
strResult->mValue.Truncate();
|
2005-11-02 10:41:43 +03:00
|
|
|
txXPathNodeUtils::appendNodeValue(nodeSet->get(i),
|
|
|
|
strResult->mValue);
|
2005-11-02 10:40:48 +03:00
|
|
|
if (compareResults(aContext, strResult, aRight)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
|
|
|
|
// Handle case for Just Right NodeSet
|
2005-11-02 10:40:48 +03:00
|
|
|
if (rtype == txAExprResult::NODESET) {
|
|
|
|
if (ltype == txAExprResult::BOOLEAN) {
|
2005-11-02 10:40:21 +03:00
|
|
|
BooleanResult rightBool(aRight->booleanValue());
|
2005-11-02 10:40:48 +03:00
|
|
|
return compareResults(aContext, aLeft, &rightBool);
|
2005-11-02 10:37:36 +03:00
|
|
|
}
|
2005-11-02 10:37:35 +03:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
txNodeSet* nodeSet = static_cast<txNodeSet*>(aRight);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<StringResult> strResult;
|
2005-11-02 10:40:48 +03:00
|
|
|
rv = aContext->recycler()->getStringResult(getter_AddRefs(strResult));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2005-11-02 10:40:48 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2005-11-02 10:40:21 +03:00
|
|
|
for (i = 0; i < nodeSet->size(); ++i) {
|
2005-11-02 10:40:48 +03:00
|
|
|
strResult->mValue.Truncate();
|
2005-11-02 10:41:43 +03:00
|
|
|
txXPathNodeUtils::appendNodeValue(nodeSet->get(i),
|
|
|
|
strResult->mValue);
|
2005-11-02 10:40:48 +03:00
|
|
|
if (compareResults(aContext, aLeft, strResult)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:40:21 +03:00
|
|
|
// Neither is a NodeSet
|
|
|
|
if (mOp == EQUAL || mOp == NOT_EQUAL) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool result;
|
2006-04-01 06:12:15 +04:00
|
|
|
const nsString *lString, *rString;
|
2005-11-02 10:40:21 +03:00
|
|
|
|
|
|
|
// If either is a bool, compare as bools.
|
2005-11-02 10:40:48 +03:00
|
|
|
if (ltype == txAExprResult::BOOLEAN ||
|
|
|
|
rtype == txAExprResult::BOOLEAN) {
|
2005-11-02 10:40:21 +03:00
|
|
|
result = aLeft->booleanValue() == aRight->booleanValue();
|
2005-11-02 10:34:45 +03:00
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2005-11-02 10:40:21 +03:00
|
|
|
// If either is a number, compare as numbers.
|
2005-11-02 10:40:48 +03:00
|
|
|
else if (ltype == txAExprResult::NUMBER ||
|
|
|
|
rtype == txAExprResult::NUMBER) {
|
2005-11-02 10:40:21 +03:00
|
|
|
double lval = aLeft->numberValue();
|
|
|
|
double rval = aRight->numberValue();
|
2012-01-23 23:52:28 +04:00
|
|
|
result = (lval == rval);
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise compare as strings. Try to use the stringobject in
|
|
|
|
// StringResult if possible since that is a common case.
|
|
|
|
else if ((lString = aLeft->stringValuePointer())) {
|
|
|
|
if ((rString = aRight->stringValuePointer())) {
|
|
|
|
result = lString->Equals(*rString);
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
else {
|
2005-11-02 10:39:22 +03:00
|
|
|
nsAutoString rStr;
|
2005-11-02 10:40:21 +03:00
|
|
|
aRight->stringValue(rStr);
|
2006-04-01 06:12:15 +04:00
|
|
|
result = lString->Equals(rStr);
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
|
|
|
else if ((rString = aRight->stringValuePointer())) {
|
|
|
|
nsAutoString lStr;
|
|
|
|
aLeft->stringValue(lStr);
|
|
|
|
result = rString->Equals(lStr);
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
else {
|
2005-11-02 10:40:21 +03:00
|
|
|
nsAutoString lStr, rStr;
|
|
|
|
aLeft->stringValue(lStr);
|
|
|
|
aRight->stringValue(rStr);
|
|
|
|
result = lStr.Equals(rStr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mOp == EQUAL ? result : !result;
|
|
|
|
}
|
|
|
|
|
|
|
|
double leftDbl = aLeft->numberValue();
|
|
|
|
double rightDbl = aRight->numberValue();
|
|
|
|
switch (mOp) {
|
|
|
|
case LESS_THAN:
|
2005-11-02 10:41:43 +03:00
|
|
|
{
|
2012-01-23 23:52:28 +04:00
|
|
|
return (leftDbl < rightDbl);
|
2005-11-02 10:41:43 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
case LESS_OR_EQUAL:
|
2005-11-02 10:41:43 +03:00
|
|
|
{
|
2012-01-23 23:52:28 +04:00
|
|
|
return (leftDbl <= rightDbl);
|
2005-11-02 10:41:43 +03:00
|
|
|
}
|
|
|
|
case GREATER_THAN:
|
|
|
|
{
|
2012-01-23 23:52:28 +04:00
|
|
|
return (leftDbl > rightDbl);
|
2005-11-02 10:41:43 +03:00
|
|
|
}
|
2005-11-02 10:40:21 +03:00
|
|
|
case GREATER_OR_EQUAL:
|
2005-11-02 10:41:43 +03:00
|
|
|
{
|
2012-01-23 23:52:28 +04:00
|
|
|
return (leftDbl >= rightDbl);
|
2005-11-02 10:41:43 +03:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("We should have caught all cases");
|
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
|
|
|
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult
|
|
|
|
RelationalExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
|
2005-11-02 10:37:50 +03:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txAExprResult> lResult;
|
2005-11-02 10:40:48 +03:00
|
|
|
nsresult rv = mLeftExpr->evaluate(aContext, getter_AddRefs(lResult));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<txAExprResult> rResult;
|
2005-11-02 10:40:48 +03:00
|
|
|
rv = mRightExpr->evaluate(aContext, getter_AddRefs(rResult));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aContext->recycler()->
|
|
|
|
getBoolResult(compareResults(aContext, lResult, rResult), aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
2005-11-02 10:40:21 +03:00
|
|
|
}
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2006-01-26 04:50:30 +03:00
|
|
|
TX_IMPL_EXPR_STUBS_2(RelationalExpr, BOOLEAN_RESULT, mLeftExpr, mRightExpr)
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2006-01-26 04:50:30 +03:00
|
|
|
RelationalExpr::isSensitiveTo(ContextSensitivity aContext)
|
|
|
|
{
|
|
|
|
return mLeftExpr->isSensitiveTo(aContext) ||
|
|
|
|
mRightExpr->isSensitiveTo(aContext);
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:42:10 +03:00
|
|
|
#ifdef TX_TO_STRING
|
2005-11-02 10:40:21 +03:00
|
|
|
void
|
|
|
|
RelationalExpr::toString(nsAString& str)
|
|
|
|
{
|
|
|
|
mLeftExpr->toString(str);
|
2005-11-02 10:34:13 +03:00
|
|
|
|
2005-11-02 10:40:21 +03:00
|
|
|
switch (mOp) {
|
2005-11-02 10:34:13 +03:00
|
|
|
case NOT_EQUAL:
|
2005-11-02 10:42:33 +03:00
|
|
|
str.AppendLiteral("!=");
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
case LESS_THAN:
|
2014-01-04 19:02:17 +04:00
|
|
|
str.Append(char16_t('<'));
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
case LESS_OR_EQUAL:
|
2005-11-02 10:42:33 +03:00
|
|
|
str.AppendLiteral("<=");
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
case GREATER_THAN :
|
2014-01-04 19:02:17 +04:00
|
|
|
str.Append(char16_t('>'));
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
case GREATER_OR_EQUAL:
|
2005-11-02 10:42:33 +03:00
|
|
|
str.AppendLiteral(">=");
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
default:
|
2014-01-04 19:02:17 +04:00
|
|
|
str.Append(char16_t('='));
|
2005-11-02 10:34:13 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-11-02 10:40:21 +03:00
|
|
|
mRightExpr->toString(str);
|
|
|
|
}
|
2005-11-02 10:42:10 +03:00
|
|
|
#endif
|