1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
//------------------------------------------------------------------------------
// <copyright file="LogicalExpr.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
namespace MS.Internal.Xml.XPath {
using System;
using System.Xml;
using System.Xml.XPath;
using System.Diagnostics;
using System.Globalization;
using System.Xml.Xsl;
internal sealed class LogicalExpr : ValueQuery {
Operator.Op op;
Query opnd1;
Query opnd2;
public LogicalExpr(Operator.Op op, Query opnd1, Query opnd2) {
Debug.Assert(
Operator.Op.LT == op || Operator.Op.GT == op ||
Operator.Op.LE == op || Operator.Op.GE == op ||
Operator.Op.EQ == op || Operator.Op.NE == op
);
this.op = op;
this.opnd1 = opnd1;
this.opnd2 = opnd2;
}
private LogicalExpr(LogicalExpr other) : base (other) {
this.op = other.op;
this.opnd1 = Clone(other.opnd1);
this.opnd2 = Clone(other.opnd2);
}
public override void SetXsltContext(XsltContext context){
opnd1.SetXsltContext(context);
opnd2.SetXsltContext(context);
}
public override object Evaluate(XPathNodeIterator nodeIterator) {
Operator.Op op = this.op;
object val1 = this.opnd1.Evaluate(nodeIterator);
object val2 = this.opnd2.Evaluate(nodeIterator);
int type1 = (int)GetXPathType(val1);
int type2 = (int)GetXPathType(val2);
if (type1 < type2) {
op = Operator.InvertOperator(op);
object valTemp = val1;
val1 = val2;
val2 = valTemp;
int typeTmp = type1;
type1 = type2;
type2 = typeTmp;
}
if (op == Operator.Op.EQ || op == Operator.Op.NE) {
return CompXsltE[type1][type2](op, val1, val2);
} else {
return CompXsltO[type1][type2](op, val1, val2);
}
}
delegate bool cmpXslt(Operator.Op op, object val1, object val2);
// Number, String, Boolean, NodeSet, Navigator
private static readonly cmpXslt[][] CompXsltE = {
new cmpXslt[] { new cmpXslt(cmpNumberNumber), null , null , null , null },
new cmpXslt[] { new cmpXslt(cmpStringNumber), new cmpXslt(cmpStringStringE), null , null , null },
new cmpXslt[] { new cmpXslt(cmpBoolNumberE ), new cmpXslt(cmpBoolStringE ), new cmpXslt(cmpBoolBoolE ), null , null },
new cmpXslt[] { new cmpXslt(cmpQueryNumber ), new cmpXslt(cmpQueryStringE ), new cmpXslt(cmpQueryBoolE ), new cmpXslt(cmpQueryQueryE ), null },
new cmpXslt[] { new cmpXslt(cmpRtfNumber ), new cmpXslt(cmpRtfStringE ), new cmpXslt(cmpRtfBoolE ), new cmpXslt(cmpRtfQueryE ), new cmpXslt(cmpRtfRtfE) },
};
private static readonly cmpXslt[][] CompXsltO = {
new cmpXslt[] { new cmpXslt(cmpNumberNumber), null , null , null , null },
new cmpXslt[] { new cmpXslt(cmpStringNumber), new cmpXslt(cmpStringStringO), null , null , null },
new cmpXslt[] { new cmpXslt(cmpBoolNumberO ), new cmpXslt(cmpBoolStringO ), new cmpXslt(cmpBoolBoolO ), null , null },
new cmpXslt[] { new cmpXslt(cmpQueryNumber ), new cmpXslt(cmpQueryStringO ), new cmpXslt(cmpQueryBoolO ), new cmpXslt(cmpQueryQueryO ), null },
new cmpXslt[] { new cmpXslt(cmpRtfNumber ), new cmpXslt(cmpRtfStringO ), new cmpXslt(cmpRtfBoolO ), new cmpXslt(cmpRtfQueryO ), new cmpXslt(cmpRtfRtfO) },
};
/*cmpXslt:*/
static bool cmpQueryQueryE(Operator.Op op, object val1, object val2) {
Debug.Assert(op == Operator.Op.EQ || op == Operator.Op.NE);
bool isEQ = (op == Operator.Op.EQ);
NodeSet n1 = new NodeSet(val1);
NodeSet n2 = new NodeSet(val2);
while (true) {
if (! n1.MoveNext()) {
return false;
}
if (! n2.MoveNext()) {
return false;
}
string str1 = n1.Value;
do {
if ((str1 == n2.Value) == isEQ) {
return true;
}
}while (n2.MoveNext());
n2.Reset();
}
}
/*cmpXslt:*/
static bool cmpQueryQueryO(Operator.Op op, object val1, object val2) {
Debug.Assert(
op == Operator.Op.LT || op == Operator.Op.GT ||
op == Operator.Op.LE || op == Operator.Op.GE
);
NodeSet n1 = new NodeSet(val1);
NodeSet n2 = new NodeSet(val2);
while (true) {
if (!n1.MoveNext()) {
return false;
}
if (!n2.MoveNext()) {
return false;
}
double num1 = NumberFunctions.Number(n1.Value);
do {
if (cmpNumberNumber(op, num1, NumberFunctions.Number(n2.Value))) {
return true;
}
} while (n2.MoveNext());
n2.Reset();
}
}
static bool cmpQueryNumber(Operator.Op op, object val1, object val2) {
NodeSet n1 = new NodeSet(val1);
double n2 = (double) val2;
while (n1.MoveNext()) {
if (cmpNumberNumber(op, NumberFunctions.Number(n1.Value), n2)) {
return true;
}
}
return false;
}
static bool cmpQueryStringE(Operator.Op op, object val1, object val2) {
NodeSet n1 = new NodeSet(val1);
string n2 = (string) val2;
while (n1.MoveNext()) {
if (cmpStringStringE(op, n1.Value, n2)) {
return true;
}
}
return false;
}
static bool cmpQueryStringO(Operator.Op op, object val1, object val2) {
NodeSet n1 = new NodeSet(val1);
double n2 = NumberFunctions.Number((string) val2);
while (n1.MoveNext()) {
if (cmpNumberNumberO(op, NumberFunctions.Number(n1.Value), n2)) {
return true;
}
}
return false;
}
static bool cmpRtfQueryE(Operator.Op op, object val1, object val2) {
string n1 = Rtf(val1);
NodeSet n2 = new NodeSet(val2);
while (n2.MoveNext()) {
if (cmpStringStringE(op, n1, n2.Value)) {
return true;
}
}
return false;
}
static bool cmpRtfQueryO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number(Rtf(val1));
NodeSet n2 = new NodeSet(val2);
while (n2.MoveNext()) {
if (cmpNumberNumberO(op, n1, NumberFunctions.Number(n2.Value))) {
return true;
}
}
return false;
}
static bool cmpQueryBoolE(Operator.Op op, object val1, object val2) {
NodeSet n1 = new NodeSet(val1);
bool b1 = n1.MoveNext();
bool b2 = (bool)val2;
return cmpBoolBoolE(op, b1, b2);
}
static bool cmpQueryBoolO(Operator.Op op, object val1, object val2) {
NodeSet n1 = new NodeSet(val1);
double d1 = n1.MoveNext() ? 1.0 : 0;
double d2 = NumberFunctions.Number((bool)val2);
return cmpNumberNumberO(op, d1, d2);
}
static bool cmpBoolBoolE(Operator.Op op, bool n1, bool n2) {
Debug.Assert( op == Operator.Op.EQ || op == Operator.Op.NE,
"Unexpected Operator.op code in cmpBoolBoolE()"
);
return (op == Operator.Op.EQ) == (n1 == n2);
}
static bool cmpBoolBoolE(Operator.Op op, object val1, object val2) {
bool n1 = (bool)val1;
bool n2 = (bool)val2;
return cmpBoolBoolE(op, n1, n2);
}
static bool cmpBoolBoolO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number((bool)val1);
double n2 = NumberFunctions.Number((bool)val2);
return cmpNumberNumberO(op, n1, n2);
}
static bool cmpBoolNumberE(Operator.Op op, object val1, object val2) {
bool n1 = (bool)val1;
bool n2 = BooleanFunctions.toBoolean((double)val2);
return cmpBoolBoolE(op, n1, n2);
}
static bool cmpBoolNumberO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number((bool)val1);
double n2 = (double)val2;
return cmpNumberNumberO(op, n1, n2);
}
static bool cmpBoolStringE(Operator.Op op, object val1, object val2) {
bool n1 = (bool)val1;
bool n2 = BooleanFunctions.toBoolean((string) val2);
return cmpBoolBoolE(op, n1, n2);
}
static bool cmpRtfBoolE(Operator.Op op, object val1, object val2) {
bool n1 = BooleanFunctions.toBoolean(Rtf(val1));
bool n2 = (bool)val2;
return cmpBoolBoolE(op, n1, n2);
}
static bool cmpBoolStringO(Operator.Op op, object val1, object val2) {
return cmpNumberNumberO(op,
NumberFunctions.Number((bool)val1),
NumberFunctions.Number((string) val2)
);
}
static bool cmpRtfBoolO(Operator.Op op, object val1, object val2) {
return cmpNumberNumberO(op,
NumberFunctions.Number(Rtf(val1)),
NumberFunctions.Number((bool)val2)
);
}
static bool cmpNumberNumber(Operator.Op op, double n1, double n2) {
switch (op) {
case Operator.Op.LT : return( n1 < n2 ) ;
case Operator.Op.GT : return( n1 > n2 ) ;
case Operator.Op.LE : return( n1 <= n2 ) ;
case Operator.Op.GE : return( n1 >= n2 ) ;
case Operator.Op.EQ : return( n1 == n2 ) ;
case Operator.Op.NE : return( n1 != n2 ) ;
}
Debug.Fail("Unexpected Operator.op code in cmpNumberNumber()");
return false;
}
static bool cmpNumberNumberO(Operator.Op op, double n1, double n2) {
switch (op) {
case Operator.Op.LT : return( n1 < n2 ) ;
case Operator.Op.GT : return( n1 > n2 ) ;
case Operator.Op.LE : return( n1 <= n2 ) ;
case Operator.Op.GE : return( n1 >= n2 ) ;
}
Debug.Fail("Unexpected Operator.op code in cmpNumberNumberO()");
return false;
}
static bool cmpNumberNumber(Operator.Op op, object val1, object val2) {
double n1 = (double)val1;
double n2 = (double)val2;
return cmpNumberNumber(op, n1, n2);
}
static bool cmpStringNumber(Operator.Op op, object val1, object val2) {
double n2 = (double)val2;
double n1 = NumberFunctions.Number((string) val1);
return cmpNumberNumber(op, n1, n2);
}
static bool cmpRtfNumber(Operator.Op op, object val1, object val2) {
double n2 = (double)val2;
double n1 = NumberFunctions.Number(Rtf(val1));
return cmpNumberNumber(op, n1, n2);
}
static bool cmpStringStringE(Operator.Op op, string n1, string n2) {
Debug.Assert( op == Operator.Op.EQ || op == Operator.Op.NE,
"Unexpected Operator.op code in cmpStringStringE()"
);
return (op == Operator.Op.EQ) == (n1 == n2);
}
static bool cmpStringStringE(Operator.Op op, object val1, object val2) {
string n1 = (string) val1;
string n2 = (string) val2;
return cmpStringStringE(op, n1, n2);
}
static bool cmpRtfStringE(Operator.Op op, object val1, object val2) {
string n1 = Rtf(val1);
string n2 = (string) val2;
return cmpStringStringE(op, n1, n2);
}
static bool cmpRtfRtfE(Operator.Op op, object val1, object val2) {
string n1 = Rtf(val1);
string n2 = Rtf(val2);
return cmpStringStringE(op, n1, n2);
}
static bool cmpStringStringO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number((string) val1);
double n2 = NumberFunctions.Number((string) val2);
return cmpNumberNumberO(op, n1, n2);
}
static bool cmpRtfStringO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number(Rtf(val1));
double n2 = NumberFunctions.Number((string)val2);
return cmpNumberNumberO(op, n1, n2);
}
static bool cmpRtfRtfO(Operator.Op op, object val1, object val2) {
double n1 = NumberFunctions.Number(Rtf(val1));
double n2 = NumberFunctions.Number(Rtf(val2));
return cmpNumberNumberO(op, n1, n2);
}
public override XPathNodeIterator Clone() { return new LogicalExpr(this); }
private struct NodeSet {
private Query opnd;
private XPathNavigator current;
public NodeSet(object opnd) {
this.opnd = (Query) opnd;
current = null;
}
public bool MoveNext() {
current = opnd.Advance();
return current != null;
}
public void Reset() {
opnd.Reset();
}
public string Value { get { return this.current.Value; } }
}
private static string Rtf( object o) { return ((XPathNavigator)o).Value; }
public override XPathResultType StaticType { get { return XPathResultType.Boolean; } }
public override void PrintQuery(XmlWriter w) {
w.WriteStartElement(this.GetType().Name);
w.WriteAttributeString("op", op.ToString());
opnd1.PrintQuery(w);
opnd2.PrintQuery(w);
w.WriteEndElement();
}
}
}
|