-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTreeComparer.php
555 lines (487 loc) · 15 KB
/
TreeComparer.php
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
/**
* XPath 2.0 for PHP
* _ _ _ _ _
* | | _ _ __ _ _ _(_) __| (_) |_ _ _
* | | | | | |/ _` | | | | |/ _` | | __| | | |
* | |__| |_| | (_| | |_| | | (_| | | |_| |_| |
* |_____\__, |\__, |\__,_|_|\__,_|_|\__|\__, |
* |___/ |_| |___/
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2017 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace lyquidity\XPath2;
use lyquidity\xml\xpath\XPathItem;
use lyquidity\XPath2\Value\AnyUriValue;
use lyquidity\XPath2\Value\UntypedAtomic;
use lyquidity\XPath2\Proxy\ValueProxy;
use lyquidity\xml\xpath\XPathNodeIterator;
use lyquidity\xml\xpath\XPathNodeType;
use lyquidity\xml\xpath\XPathNavigator;
use lyquidity\XPath2\Iterator\ExprIterator;
use lyquidity\XPath2\Properties\Resources;
use lyquidity\xml\MS\XmlTypeCode;
use lyquidity\XPath2\Iterator\ChildNodeIterator;
use lyquidity\xml\MS\XmlReservedNs;
/**
* TreeComparer (public)
*/
class TreeComparer
{
/**
* context
* @var XPath2Context $context
*/
protected $context;
/**
* The name of a collation to use when comparing items
* @var string
*/
protected $collation = false;
/**
* Constructor
* @param XPath2Context $context
* @param string $collation
*/
public function __construct( $context, $collation = null )
{
$this->context = $context;
if ( is_null( $collation ) ) return;
$this->$collation = $collation;
if ( $collation == XmlReservedNs::collationCodepoint ) return;
$result = setlocale( LC_COLLATE, $collation );
if ( ! $result )
{
throw XPath2Exception::withErrorCodeAndParam( "FOCH0002", Resources::FOCH0002, $collation );
}
}
/**
* excludeComments
* @var bool $excludeComments
*/
public $excludeComments = false;
/**
* excludeWhitespace
* @var bool $excludeWhitespace
*/
public $excludeWhitespace = false;
/**
* When true the comparison of element node values will taken into account the types so 001.234 will equal 1.234
* @var bool $useValueCompare
*/
public $useValueCompare = false;
/**
* Set to true if untyped values can be tested as numbers if they are numeric
* @var bool $untypedCanBeNumeric
*/
public $untypedCanBeNumeric = false;
// BMS 2018-03-21 Add because a query like:
// xfi:nodes-correspond( //t:P1[@id='t1'], //t:P1[@id='t2'] )
// passes a pair of nodes including the attribute 'id' to the nodes-correspond
// function but by definition the id values are different
//
/**
* The attributeToIgnore function allows the caller to define the selection attribute that should not affect equality.
* @var mixed
*/
public $attributeToIgnore = null;
/**
* TextEqual
* @param string $a
* @param string $b
* @return bool
*/
protected function TextEqual( $a, $b )
{
if ( $this->excludeWhitespace )
return strcasecmp( CoreFuncs::Normalizespace( $a ), CoreFuncs::Normalizespace( $b ) ) == 0;
else
return strcasecmp( $a, $b ) == 0;
}
/**
* Test if two items are equal
* @param XPathItem $item1
* @param XPathItem $item2
* @return bool
*/
protected function ItemEqual( $item1, $item2 )
{
$xTypeCode = $item1->getSchemaType()->TypeCode;
$yTypeCode = $item2->getSchemaType()->TypeCode;
$types = array( XmlTypeCode::Decimal, XmlTypeCode::Float, XmlTypeCode::Double, XmlTypeCode::Integer );
$x = $item1->GetTypedValue();
// BMS 2018-01-19 Could be numeric
// Added to support typed member comparisons in FactVariables
if ( $this->untypedCanBeNumeric && $x instanceof UntypedAtomic )
{
if ( is_numeric( $x->getValue() ) )
{
$x = floatval( $x->getValue() );
}
}
if ( $x instanceof UntypedAtomic || $x instanceof AnyUriValue )
{
// Special case
// If $x is AnyUriValue or the attribute name is 'scheme' then
// normalize by making sure the scheme starts with a scheme
// such as http:// https:// ftp:// etc
$special = /* $x instanceof AnyUriValue || */ ( $item1 instanceof XPathNavigator && $item1->getLocalName() == 'scheme' );
$x = $x->ToString();
$xTypeCode = XmlTypeCode::String;
if ( $special && strpos( $x, "://" ) === false )
{
$x = "http://" . $x;
}
}
$y = $item2->GetTypedValue();
// BMS 2018-01-19 Could be numeric
// Added to support typed member comparisons in FactVariables
if ( $this->untypedCanBeNumeric && $y instanceof UntypedAtomic )
{
if ( is_numeric( $y->getValue() ) )
{
$y = floatval( $y->getValue() );
}
}
if ( $y instanceof UntypedAtomic || $y instanceof AnyUriValue )
{
// Special case
// If $y is AnyUriValue or the attribute name is 'scheme' then
// normalize by making sure the scheme starts with a scheme
// such as http:// https:// ftp:// etc
$special = /* $y instanceof AnyUriValue || */( $item2 instanceof XPathNavigator && $item2->getLocalName() == 'scheme' );
$y = $y->ToString();
$yTypeCode = XmlTypeCode::String;
if ( $special && strpos( $y, "://" ) === false )
{
$y = "http://" . $y;
}
}
if ( $this->useValueCompare )
{
if ( is_string( $x ) )
{
$x = trim( $x );
}
}
if ( $this->useValueCompare )
{
if ( is_string( $y ) )
{
$y = trim( $y );
}
}
if ( is_double( $x ) && is_nan( $x ) && is_double( $y ) && is_nan( $y ) )
{
return true;
}
if ( is_double( $x ) && is_infinite( $x ) && is_double( $y ) && is_infinite( $y ) )
{
return true;
}
if ( $xTypeCode != $yTypeCode )
{
// Handle the exceptions
$exceptions = (
( $xTypeCode == XmlTypeCode::Integer && in_array( $yTypeCode, $types ) ) ||
( $yTypeCode == XmlTypeCode::Integer && in_array( $xTypeCode, $types ) ) ||
( $xTypeCode == XmlTypeCode::Decimal && in_array( $yTypeCode, $types ) ) ||
( $yTypeCode == XmlTypeCode::Decimal && in_array( $xTypeCode, $types ) )
);
if ( ! $exceptions )
{
return false;
}
}
if ( ! is_null( $this->collation ) && is_string( $x ) && is_string( $y ) )
{
if ( $this->collation == XmlReservedNs::collationCodepoint )
{
$x = \normalizer_normalize( $x, \Normalizer::FORM_C );
$y = \normalizer_normalize( $y, \Normalizer::FORM_C );
return strcmp( $x, $y );
}
else
{
return strcoll( $x, $y ) == 0;
}
}
if ( ! is_object( $x ) && ! is_object( $y ) )
{
if ( $x == $y )
{
return true;
}
}
else if ( is_object( $x ) && is_object( $y ) && method_exists( $x, "Equals" ) && method_exists( $y, "Equals" ) )
{
if ( $x->Equals( $y ) )
{
return true;
}
}
$res = null;
$result = ValueProxy::EqValues( $x, $y, $res) && $res;
return $result;
}
/**
* NodeEqual
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
protected function NodeEqual( $nav1, $nav2 )
{
if ( $nav1->getNodeType() != $nav2->getNodeType() )
{
return false;
}
switch ( $nav1->getNodeType() )
{
case XPathNodeType::Element:
return $this->ElementEqual( $nav1, $nav2 );
case XPathNodeType::Attribute:
return $this->AttributeEqual( $nav1, $nav2 );
case XPathNodeType::Text:
case XPathNodeType::SignificantWhitespace:
case XPathNodeType::Whitespace:
case XPathNodeType::Comment:
if ( $this->useValueCompare )
{
if ( $nav1->getValue() == $nav2->getValue() )
{
// return true;
}
$value = $nav1->getValue();
$result = $this->ItemEqual( $nav1, $nav2);
return $result;
}
else
{
return $this->TextEqual( $nav1->getValue(), $nav2->getValue() );
}
case XPathNodeType::ProcessingInstruction:
return $this->ProcessingInstructionEqual( $nav1, $nav2 );
default:
return $this->DeepEqualByNavigator( $nav1, $nav2 );
}
}
/**
* ElementEqual
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
protected function ElementEqual( $nav1, $nav2 )
{
if ( $nav1->getLocalName() != $nav2->getLocalName() || $nav1->getNamespaceURI() != $nav2->getNamespaceURI() )
{
return false;
}
if ( ! $this->ElementAttributesEqual( $nav1->CloneInstance(), $nav2->CloneInstance() ) )
{
return false;
}
return $this->DeepEqualByNavigator( $nav1, $nav2 );
}
/**
* ElementAttributesEqual
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
protected function ElementAttributesEqual( $nav1, $nav2 )
{
if ( $nav1->getHasAttributes() != $nav2->getHasAttributes() )
{
return false;
}
if ( ! $nav1->getHasAttributes() ) return true;
// Check they have the same number of attributes
$flag1 = $nav1->MoveToFirstAttribute();
$flag2 = $nav2->MoveToFirstAttribute();
while ( $flag1 && $flag2 )
{
$flag1 = $nav1->MoveToNextAttribute();
$flag2 = $nav2->MoveToNextAttribute();
}
if ( $flag1 != $flag2 ) return false;
$nav1->MoveToParent();
$nav2->MoveToParent();
for ( $flag3 = $nav1->MoveToFirstAttribute(); $flag3; $flag3 = $nav1->MoveToNextAttribute() )
{
$flag4 = $nav2->MoveToFirstAttribute();
while ( $flag4 )
{
if ( $this->AttributeEqual( $nav1, $nav2 ) )
{
break;
}
$flag4 = $nav2->MoveToNextAttribute();
}
$nav2->MoveToParent();
if ( ! $flag4)
{
$nav1->MoveToParent();
return false;
}
}
// $nav1->MoveToParent();
return true;
}
/**
* ProcessingInstructionEqual
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
protected function ProcessingInstructionEqual( $nav1, $nav2 )
{
return $nav1->getLocalName() == $nav2->getLocalName() && $nav1->getValue() == $nav2->getValue();
}
/**
* AttributeEqual
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
protected function AttributeEqual($nav1, $nav2)
{
if ( $nav1->getLocalName() != $nav2->getLocalName() || $nav1->getNamespaceURI() != $nav2->getNamespaceURI() ) return false;
// BMS 2018-03-21 Add because a query like:
// xfi:nodes-correspond( //t:P1[@id='t1'], //t:P1[@id='t2'] )
// passes a pair of nodes including the attribute 'id' to the nodes-correspond
// function but by definition the id values are different
// The attributeToIgnore function allows the caller to define the selection attribute that should not affect equality.
if ( ! is_null( $this->attributeToIgnore ) )
{
if ( $nav1->getLocalName() == $this->attributeToIgnore ) return true;
}
return $this->ItemEqual( $nav1, $nav2 );
// return $nav1->GetTypedValue() == $nav2->GetTypedValue();
}
/**
* DeepEqualByNavigator Iterate over nodes in order-sensitive manner
* @param XPathNavigator $nav1
* @param XPathNavigator $nav2
* @return bool
*/
public function DeepEqualByNavigator( $nav1, $nav2 )
{
/**
* @var XPathNodeIterator $iter1
* @var XPathNodeIterator $iter2
*/
// $iter1 = $nav1->SelectChildrenByType( XPathNodeType::All );
// $iter2 = $nav2->SelectChildrenByType( XPathNodeType::All );
$nodeTest = null; // XmlQualifiedNameTest::create();
$iter1 = ChildNodeIterator::fromNodeTest( $this->context, $nodeTest, XPath2NodeIterator::Create( $nav1 ) );
$iter1->excludeComments = true; // $this->excludeComments;
$iter1->excludeWhitespace = $this->excludeWhitespace;
$iter2 = ChildNodeIterator::fromNodeTest( $this->context, $nodeTest, XPath2NodeIterator::Create( $nav2 ) );
$iter2->excludeComments = true; // $this->excludeComments;
$iter2->excludeWhitespace = $this->excludeWhitespace;
$leftCount = $iter1->getCount();
$rightCount = $iter2->getCount();
if ( $leftCount != $rightCount )
{
// Maybe one or both iterators have skipped comments and all the nodes are text nodes.
// If this is the case join them to see if they compare.
$leftTextCount = $iter1->getCountType( XPathNodeType::Text );
$rightTextCount = $iter2->getCountType( XPathNodeType::Text );
if ( $leftTextCount != $leftCount ) return false;
if ( $rightTextCount != $rightCount ) return false;
// By this time all the nodes are text so see if when concatenated they are equal
$text1 = ExtFuncs::StringJoin( $iter1, "" );
$text2 = ExtFuncs::StringJoin( $iter2, "" );
return $text1 == $text2;
}
$leftElementCount = $iter1->getCountType( XPathNodeType::Element );
if ( $leftElementCount )
{
$textMatched = $nav1->getValue() == $nav2->getValue();
return $this->DeepEqualByIterator( $iter1, $iter2, $textMatched );
}
if ( ! $this->ItemEqual( $nav1, $nav2 ) )
{
return false;
}
return true;
}
/**
* DeepEqualByIterator Alternative way to iterate over nodes in order-sensitive manner
* @param XPath2NodeIterator $iter1
* @param XPath2NodeIterator $iter2
* @param bool $elementsOnly
* @return bool
*/
public function DeepEqualByIterator( $iter1, $iter2, $elementsOnly = false )
{
$iter1 = $iter1->CloneInstance();
$iter2 = $iter2->CloneInstance();
$flag1 = false;
$flag2 = false;
do
{
$flag1 = $iter1->MoveNext();
$flag2 = $iter2->MoveNext();
if ( $flag1 != $flag2 ) return false;
if ($flag1 && $flag2)
{
$iter1Current = $iter1->getCurrent();
$iter2Current = $iter2->getCurrent();
// If one of the iterators is a ForIterator and it returns an ExprIterator handle it
if ( $iter1Current instanceof ExprIterator || $iter2Current instanceof ExprIterator )
{
if ( ! $iter1Current instanceof ExprIterator ) $iter1Current = $iter1;
if ( ! $iter2Current instanceof ExprIterator ) $iter2Current = $iter2;
return $this->DeepEqualByIterator( $iter1Current->CloneInstance(), $iter2Current->CloneInstance() );
}
if ( $iter1Current->getIsNode() != $iter2Current->getIsNode() )
{
return false;
}
else
{
if ( $iter1Current->getIsNode() && $iter2Current->getIsNode() )
{
if ( $elementsOnly && $iter1Current->getNodeType() != XPathNodeType::Element )
{
continue;
}
if ( ! $this->NodeEqual( $iter1Current, $iter2Current ) )
{
return false;
}
}
else
{
if ( ! $this->ItemEqual( $iter1Current, $iter2Current ) )
{
return false;
}
}
}
}
}
while ( $flag1 && $flag2 );
return true;
}
}
?>