2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "txExpr.h"
|
|
|
|
#include "txNodeSet.h"
|
|
|
|
#include "txIXPathContext.h"
|
|
|
|
#include "txXPathTreeWalker.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compares the two ExprResults based on XPath 1.0 Recommendation (section 3.4)
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
RelationalExpr::compareResults(txIEvalContext* aContext, txAExprResult* aLeft,
|
|
|
|
txAExprResult* aRight)
|
|
|
|
{
|
|
|
|
short ltype = aLeft->getResultType();
|
|
|
|
short rtype = aRight->getResultType();
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Handle case for just Left NodeSet or Both NodeSets
|
|
|
|
if (ltype == txAExprResult::NODESET) {
|
|
|
|
if (rtype == txAExprResult::BOOLEAN) {
|
|
|
|
BooleanResult leftBool(aLeft->booleanValue());
|
|
|
|
return compareResults(aContext, &leftBool, aRight);
|
|
|
|
}
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
txNodeSet* nodeSet = static_cast<txNodeSet*>(aLeft);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<StringResult> strResult;
|
|
|
|
rv = aContext->recycler()->getStringResult(getter_AddRefs(strResult));
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < nodeSet->size(); ++i) {
|
|
|
|
strResult->mValue.Truncate();
|
|
|
|
txXPathNodeUtils::appendNodeValue(nodeSet->get(i),
|
|
|
|
strResult->mValue);
|
|
|
|
if (compareResults(aContext, strResult, aRight)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle case for Just Right NodeSet
|
|
|
|
if (rtype == txAExprResult::NODESET) {
|
|
|
|
if (ltype == txAExprResult::BOOLEAN) {
|
|
|
|
BooleanResult rightBool(aRight->booleanValue());
|
|
|
|
return compareResults(aContext, aLeft, &rightBool);
|
|
|
|
}
|
|
|
|
|
2007-07-08 00:08:04 -07:00
|
|
|
txNodeSet* nodeSet = static_cast<txNodeSet*>(aRight);
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<StringResult> strResult;
|
|
|
|
rv = aContext->recycler()->getStringResult(getter_AddRefs(strResult));
|
2011-10-17 07:59:28 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t i;
|
2007-03-22 10:30:00 -07:00
|
|
|
for (i = 0; i < nodeSet->size(); ++i) {
|
|
|
|
strResult->mValue.Truncate();
|
|
|
|
txXPathNodeUtils::appendNodeValue(nodeSet->get(i),
|
|
|
|
strResult->mValue);
|
|
|
|
if (compareResults(aContext, aLeft, strResult)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Neither is a NodeSet
|
|
|
|
if (mOp == EQUAL || mOp == NOT_EQUAL) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool result;
|
2007-03-22 10:30:00 -07:00
|
|
|
const nsString *lString, *rString;
|
|
|
|
|
|
|
|
// If either is a bool, compare as bools.
|
|
|
|
if (ltype == txAExprResult::BOOLEAN ||
|
|
|
|
rtype == txAExprResult::BOOLEAN) {
|
|
|
|
result = aLeft->booleanValue() == aRight->booleanValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If either is a number, compare as numbers.
|
|
|
|
else if (ltype == txAExprResult::NUMBER ||
|
|
|
|
rtype == txAExprResult::NUMBER) {
|
|
|
|
double lval = aLeft->numberValue();
|
|
|
|
double rval = aRight->numberValue();
|
2012-01-23 11:52:28 -08:00
|
|
|
result = (lval == rval);
|
2007-03-22 10:30:00 -07: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);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsAutoString rStr;
|
|
|
|
aRight->stringValue(rStr);
|
|
|
|
result = lString->Equals(rStr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ((rString = aRight->stringValuePointer())) {
|
|
|
|
nsAutoString lStr;
|
|
|
|
aLeft->stringValue(lStr);
|
|
|
|
result = rString->Equals(lStr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
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:
|
|
|
|
{
|
2012-01-23 11:52:28 -08:00
|
|
|
return (leftDbl < rightDbl);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
case LESS_OR_EQUAL:
|
|
|
|
{
|
2012-01-23 11:52:28 -08:00
|
|
|
return (leftDbl <= rightDbl);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
case GREATER_THAN:
|
|
|
|
{
|
2012-01-23 11:52:28 -08:00
|
|
|
return (leftDbl > rightDbl);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
case GREATER_OR_EQUAL:
|
|
|
|
{
|
2012-01-23 11:52:28 -08:00
|
|
|
return (leftDbl >= rightDbl);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("We should have caught all cases");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RelationalExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsRefPtr<txAExprResult> lResult;
|
|
|
|
nsresult rv = mLeftExpr->evaluate(aContext, getter_AddRefs(lResult));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsRefPtr<txAExprResult> rResult;
|
|
|
|
rv = mRightExpr->evaluate(aContext, getter_AddRefs(rResult));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aContext->recycler()->
|
|
|
|
getBoolResult(compareResults(aContext, lResult, rResult), aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
TX_IMPL_EXPR_STUBS_2(RelationalExpr, BOOLEAN_RESULT, mLeftExpr, mRightExpr)
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
RelationalExpr::isSensitiveTo(ContextSensitivity aContext)
|
|
|
|
{
|
|
|
|
return mLeftExpr->isSensitiveTo(aContext) ||
|
|
|
|
mRightExpr->isSensitiveTo(aContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TX_TO_STRING
|
|
|
|
void
|
|
|
|
RelationalExpr::toString(nsAString& str)
|
|
|
|
{
|
|
|
|
mLeftExpr->toString(str);
|
|
|
|
|
|
|
|
switch (mOp) {
|
|
|
|
case NOT_EQUAL:
|
|
|
|
str.AppendLiteral("!=");
|
|
|
|
break;
|
|
|
|
case LESS_THAN:
|
2014-01-04 07:02:17 -08:00
|
|
|
str.Append(char16_t('<'));
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case LESS_OR_EQUAL:
|
|
|
|
str.AppendLiteral("<=");
|
|
|
|
break;
|
|
|
|
case GREATER_THAN :
|
2014-01-04 07:02:17 -08:00
|
|
|
str.Append(char16_t('>'));
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
case GREATER_OR_EQUAL:
|
|
|
|
str.AppendLiteral(">=");
|
|
|
|
break;
|
|
|
|
default:
|
2014-01-04 07:02:17 -08:00
|
|
|
str.Append(char16_t('='));
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRightExpr->toString(str);
|
|
|
|
}
|
|
|
|
#endif
|