Welcome to mirror list, hosted at ThFree Co, Russian Federation.

QilPatternFactory.cs « QIL « Xsl « Xml « System « System.Data.SqlXml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 28cdbe3359d36d2e897c2dbf6c2be5a3731a6806 (plain)
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//------------------------------------------------------------------------------
// <copyright file="QilPatternFactory.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Xml.Schema;

namespace System.Xml.Xsl.Qil {

    /// <summary>
    /// Additional factory methods for constructing common QIL patterns.
    /// </summary>
    /// <remarks>
    /// Some of the methods here are exactly like the ones in QilFactory except
    /// that they perform constant-folding and other normalization.  Others are
    /// "macro patterns" that simplify the task of constructing otherwise complex patterns.
    /// </remarks>
    internal class QilPatternFactory {
        private bool       debug;
        private QilFactory f;

        public QilPatternFactory(QilFactory f, bool debug) {
            Debug.Assert(f != null);
            this.f = f;
            this.debug = debug;
        }

        public QilFactory          BaseFactory { get { return f; } }
        public bool                IsDebug     { get { return this.debug; } }

        #region Convenience methods

        public QilLiteral String(string val) {
            return f.LiteralString(val);
        }

        public QilLiteral Int32(int val) {
            return f.LiteralInt32(val);
        }

        public QilLiteral Double(double val) {
            return f.LiteralDouble(val);
        }

        public QilName QName(string local, string uri, string prefix) {
            return f.LiteralQName(local, uri, prefix);
        }

        public QilName QName(string local, string uri) {
            return f.LiteralQName(local, uri, System.String.Empty);
        }

        public QilName QName(string local) {
            return f.LiteralQName(local, System.String.Empty, System.String.Empty);
        }

        public QilNode Unknown(XmlQueryType t) {
            return f.Unknown(t);
        }
        #endregion

        #region meta
        //-----------------------------------------------
        // meta
        //-----------------------------------------------
        public QilExpression QilExpression(QilNode root, QilFactory factory) {
            return f.QilExpression(root, factory);
        }

        public QilList FunctionList() {
            return f.FunctionList();
        }

        public QilList GlobalVariableList() {
            return f.GlobalVariableList();
        }

        public QilList GlobalParameterList() {
            return f.GlobalParameterList();
        }

        public QilList ActualParameterList() {
            return f.ActualParameterList();
        }

        public QilList ActualParameterList(QilNode arg1) {
            QilList result = f.ActualParameterList();
            result.Add(arg1);
            return result;
        }

        public QilList ActualParameterList(QilNode arg1, QilNode arg2) {
            QilList result = f.ActualParameterList();
            result.Add(arg1);
            result.Add(arg2);
            return result;
        }

        public QilList ActualParameterList(params QilNode[] args) {
            return f.ActualParameterList(args);
        }

        public QilList FormalParameterList() {
            return f.FormalParameterList();
        }

        public QilList FormalParameterList(QilNode arg1) {
            QilList result = f.FormalParameterList();
            result.Add(arg1);
            return result;
        }

        public QilList FormalParameterList(QilNode arg1, QilNode arg2) {
            QilList result = f.FormalParameterList();
            result.Add(arg1);
            result.Add(arg2);
            return result;
        }

        public QilList FormalParameterList(params QilNode[] args) {
            return f.FormalParameterList(args);
        }

        public QilList SortKeyList() {
            return f.SortKeyList();
        }

        public QilList SortKeyList(QilSortKey key) {
            QilList list = f.SortKeyList();
            list.Add(key);
            return list;
        }

        public QilList BranchList(params QilNode[] args) {
            return f.BranchList(args);
        }

        public QilNode OptimizeBarrier(QilNode child) {
            return f.OptimizeBarrier(child);
        }

        #endregion // meta

        #region specials
        //-----------------------------------------------
        // specials
        //-----------------------------------------------
        public QilNode DataSource(QilNode name, QilNode baseUri) {
            return f.DataSource(name, baseUri);
        }

        public QilNode Nop(QilNode child) {
            return f.Nop(child);
        }

        public QilNode Error(QilNode text) {
            return f.Error(text);
        }

        public QilNode Warning(QilNode text) {
            return f.Warning(text);
        }

        #endregion // specials

        #region variables
        //-----------------------------------------------
        // variables
        //-----------------------------------------------
        public QilIterator For(QilNode binding) {
            return f.For(binding);
        }

        public QilIterator Let(QilNode binding) {
            return f.Let(binding);
        }

        public QilParameter Parameter(XmlQueryType t) {
            return f.Parameter(t);
        }

        public QilParameter Parameter(QilNode defaultValue, QilName name, XmlQueryType t) {
            return f.Parameter(defaultValue, name, t);
        }

        public QilNode PositionOf(QilIterator expr) {
            return f.PositionOf(expr);
        }

        #endregion // variables

        #region literals
        //-----------------------------------------------
        // literals
        //-----------------------------------------------
        public QilNode True() {
            return f.True();
        }

        public QilNode False() {
            return f.False();
        }

        public QilNode Boolean(bool b) {
            return b ? this.True() : this.False();
        }

        #endregion // literals

        #region boolean operators
        //-----------------------------------------------
        // boolean operators
        //-----------------------------------------------

        // ToDo: Why we have nulls here at all?
        private static void CheckLogicArg(QilNode arg) {
            Debug.Assert(arg != null, "Argulent shouldn't be null");
            Debug.Assert(arg.XmlType.TypeCode == XmlTypeCode.Boolean && arg.XmlType.IsSingleton,
                "The operand must be boolean-typed"
            );
        }

        public QilNode And(QilNode left, QilNode right) {
            CheckLogicArg(left);
            CheckLogicArg(right);

            if (! debug) {
                // True, True => True (right)          other, True => other (left)
                // True, False => False (right)        other, False => False (right)
                // True, other => other (right)        other, other => And
                if (left.NodeType == QilNodeType.True || right.NodeType == QilNodeType.False) {
                    return right;
                }
                if (left.NodeType == QilNodeType.False || right.NodeType == QilNodeType.True) {
                    return left;
                }
            }
            return f.And(left, right);
        }

        public QilNode Or(QilNode left, QilNode right) {
            CheckLogicArg(left);
            CheckLogicArg(right);

            if (! debug) {
                // True, True => True (left)           other, True => True (right)
                // True, False => True (left)          other, False => other (left)
                // True, other => True (left)          other, other => Or
                if (left.NodeType == QilNodeType.True || right.NodeType == QilNodeType.False) {
                    return left;
                }
                if (left.NodeType == QilNodeType.False || right.NodeType == QilNodeType.True) {
                    return right;
                }
            }
            return f.Or(left, right);
        }

        public QilNode Not(QilNode child) {
            if (! debug) {
                switch (child.NodeType) {
                case QilNodeType.True:
                    return f.False();
                case QilNodeType.False:
                    return f.True();
                case QilNodeType.Not:
                    return ((QilUnary) child).Child;
                }
            }
            return f.Not(child);
        }

        #endregion // boolean operators

        #region choice
        //-----------------------------------------------
        // choice
        //-----------------------------------------------

        public QilNode Conditional(QilNode condition, QilNode trueBranch, QilNode falseBranch) {
            if (! debug) {
                switch (condition.NodeType) {
                case QilNodeType.True:
                    return trueBranch;
                case QilNodeType.False:
                    return falseBranch;
                case QilNodeType.Not:
                    return this.Conditional(((QilUnary)condition).Child, falseBranch, trueBranch);
                }
            }
            return f.Conditional(condition, trueBranch, falseBranch);
        }

        public QilNode Choice(QilNode expr, QilList branches) {
            if (! debug) {
                switch (branches.Count) {
                case 1:
                    // If expr has no side effects, it will be eliminated by optimizer
                    return f.Loop(f.Let(expr), branches[0]);
                case 2:
                    return f.Conditional(f.Eq(expr, f.LiteralInt32(0)), branches[0], branches[1]);
                }
            }
            return f.Choice(expr, branches);
        }

        #endregion // choice

        #region collection operators
        //-----------------------------------------------
        // collection operators
        //-----------------------------------------------
        public QilNode Length(QilNode child) {
            return f.Length(child);
        }

        public QilNode Sequence() {
            return f.Sequence();
        }

        public QilNode Sequence(QilNode child) {
            if (! debug) {
                return child;
            }
            QilList res = f.Sequence();
            res.Add(child);
            return res;
        }

        public QilNode Sequence(QilNode child1, QilNode child2) {
            QilList res = f.Sequence();
            res.Add(child1);
            res.Add(child2);
            return res;
        }

        public QilNode Sequence(params QilNode[] args) {
            if (! debug) {
                switch(args.Length) {
                case 0 : return f.Sequence();
                case 1 : return args[0];
                }
            }
            QilList res = f.Sequence();
            foreach (QilNode n in args)
                res.Add(n);
            return res;
        }

        public QilNode Union(QilNode left, QilNode right) {
            return f.Union(left, right);
        }

        public QilNode Sum(QilNode collection) {
            return f.Sum(collection);
        }
        #endregion // collection operators

        #region arithmetic operators
        //-----------------------------------------------
        // arithmetic operators
        //-----------------------------------------------
        public QilNode Negate(QilNode child) {
            return f.Negate(child);
        }

        public QilNode Add(QilNode left, QilNode right) {
            return f.Add(left, right);
        }

        public QilNode Subtract(QilNode left, QilNode right) {
            return f.Subtract(left, right);
        }

        public QilNode Multiply(QilNode left, QilNode right) {
            return f.Multiply(left, right);
        }

        public QilNode Divide(QilNode left, QilNode right) {
            return f.Divide(left, right);
        }

        public QilNode Modulo(QilNode left, QilNode right) {
            return f.Modulo(left, right);
        }

        #endregion // arithmetic operators

        #region string operators
        //-----------------------------------------------
        // string operators
        //-----------------------------------------------
        public QilNode StrLength(QilNode str) {
            return f.StrLength(str);
        }

        public QilNode StrConcat(QilNode values) {
            if (! debug) {
                if (values.XmlType.IsSingleton)
                    return values;
            }
            return f.StrConcat(values);
        }

        public QilNode StrConcat(params QilNode[] args) {
            return StrConcat((IList<QilNode>)args);
        }

        public QilNode StrConcat(IList<QilNode> args) {
            if (! debug) {
                switch(args.Count) {
                case 0 :
                    return f.LiteralString(string.Empty);
                case 1 :
                    return StrConcat(args[0]);
                }
            }
            return StrConcat(f.Sequence(args));
        }

        public QilNode StrParseQName(QilNode str, QilNode ns) {
            return f.StrParseQName(str, ns);
        }
        #endregion // string operators

        #region value comparison operators
        //-----------------------------------------------
        // value comparison operators
        //-----------------------------------------------
        public QilNode Ne(QilNode left, QilNode right) {
            return f.Ne(left, right);
        }

        public QilNode Eq(QilNode left, QilNode right) {
            return f.Eq(left, right);
        }

        public QilNode Gt(QilNode left, QilNode right) {
            return f.Gt(left, right);
        }

        public QilNode Ge(QilNode left, QilNode right) {
            return f.Ge(left, right);
        }

        public QilNode Lt(QilNode left, QilNode right) {
            return f.Lt(left, right);
        }

        public QilNode Le(QilNode left, QilNode right) {
            return f.Le(left, right);
        }

        #endregion // value comparison operators

        #region node comparison operators
        //-----------------------------------------------
        // node comparison operators
        //-----------------------------------------------
        public QilNode Is(QilNode left, QilNode right) {
            return f.Is(left, right);
        }

        public QilNode After(QilNode left, QilNode right) {
            return f.After(left, right);
        }

        public QilNode Before(QilNode left, QilNode right) {
            return f.Before(left, right);
        }

        #endregion // node comparison operators

        #region loops
        //-----------------------------------------------
        // loops
        //-----------------------------------------------
        public QilNode Loop(QilIterator variable, QilNode body) {
            if (! debug) {
                //((Loop (For $Binding) ($Binding) ) => ($binding))
                if (body == variable.Binding) {
                    return body;
                }
            }
            return f.Loop(variable, body);
        }

        public QilNode Filter(QilIterator variable, QilNode expr) {
            if (! debug) {
                //((Filter (For $Binding) (True ) ) => ($binding))
                if (expr.NodeType == QilNodeType.True) {
                    return variable.Binding;
                }
                // The following optimization is not safe if the iterator has side effects
                //((Filter (For $Binding) (False) ) => (Sequence))
            }
            return f.Filter(variable, expr);
        }

        #endregion // loops

        #region sorting
        //-----------------------------------------------
        // sorting
        //-----------------------------------------------
        public QilNode Sort(QilIterator iter, QilNode keys) {
            return f.Sort(iter, keys);
        }

        public QilSortKey SortKey(QilNode key, QilNode collation) {
            return f.SortKey(key, collation);
        }

        public QilNode DocOrderDistinct(QilNode collection) {
            if (collection.NodeType == QilNodeType.DocOrderDistinct) {
                return collection;
            }
            return f.DocOrderDistinct(collection);
        }

        #endregion // sorting

        #region function definition and invocation
        //-----------------------------------------------
        // function definition and invocation
        //-----------------------------------------------
        public QilFunction Function(QilList args, QilNode sideEffects, XmlQueryType resultType) {
            Debug.Assert(args.NodeType == QilNodeType.FormalParameterList);
            return f.Function(args, sideEffects, resultType);
        }
        public QilFunction Function(QilList args, QilNode defn, QilNode sideEffects) {
            Debug.Assert(args.NodeType == QilNodeType.FormalParameterList);
            return f.Function(args, defn, sideEffects, defn.XmlType);
        }

        public QilNode Invoke(QilFunction func, QilList args) {
            Debug.Assert(args.NodeType == QilNodeType.ActualParameterList);
            Debug.Assert(func.Arguments.Count == args.Count);
            return f.Invoke(func, args);
        }
        #endregion // function definition and invocation

        #region XML navigation
        //-----------------------------------------------
        // XML navigation
        //-----------------------------------------------
        public QilNode Content(QilNode context) {
            return f.Content(context);
        }

        public QilNode Parent(QilNode context) {
            return f.Parent(context);
        }

        public QilNode Root(QilNode context) {
            return f.Root(context);
        }

        public QilNode XmlContext() {
            return f.XmlContext();
        }

        public QilNode Descendant(QilNode expr) {
            return f.Descendant(expr);
        }

        public QilNode DescendantOrSelf(QilNode context) {
            return f.DescendantOrSelf(context);
        }

        public QilNode Ancestor(QilNode expr) {
            return f.Ancestor(expr);
        }

        public QilNode AncestorOrSelf(QilNode expr) {
            return f.AncestorOrSelf(expr);
        }

        public QilNode Preceding(QilNode expr) {
            return f.Preceding(expr);
        }

        public QilNode FollowingSibling(QilNode expr) {
            return f.FollowingSibling(expr);
        }

        public QilNode PrecedingSibling(QilNode expr) {
            return f.PrecedingSibling(expr);
        }

        public QilNode NodeRange(QilNode left, QilNode right) {
            return f.NodeRange(left, right);
        }

        public QilBinary Deref(QilNode context, QilNode id) {
            return f.Deref(context, id);
        }
        #endregion // XML navigation

        #region XML construction
        //-----------------------------------------------
        // XML construction
        //-----------------------------------------------
        public QilNode ElementCtor(QilNode name, QilNode content) {
            return f.ElementCtor(name, content);
        }

        public QilNode AttributeCtor(QilNode name, QilNode val) {
            return f.AttributeCtor(name, val);
        }

        public QilNode CommentCtor(QilNode content) {
            return f.CommentCtor(content);
        }

        public QilNode PICtor(QilNode name, QilNode content) {
            return f.PICtor(name, content);
        }

        public QilNode TextCtor(QilNode content) {
            return f.TextCtor(content);
        }

        public QilNode RawTextCtor(QilNode content) {
            return f.RawTextCtor(content);
        }

        public QilNode DocumentCtor(QilNode child) {
            return f.DocumentCtor(child);
        }

        public QilNode NamespaceDecl(QilNode prefix, QilNode uri) {
            return f.NamespaceDecl(prefix, uri);
        }

        public QilNode RtfCtor(QilNode content, QilNode baseUri) {
            return f.RtfCtor(content, baseUri);
        }

        #endregion // XML construction

        #region Node properties
        //-----------------------------------------------
        // Node properties
        //-----------------------------------------------
        public QilNode NameOf(QilNode expr) {
            return f.NameOf(expr);
        }

        public QilNode LocalNameOf(QilNode expr) {
            return f.LocalNameOf(expr);
        }

        public QilNode NamespaceUriOf(QilNode expr) {
            return f.NamespaceUriOf(expr);
        }

        public QilNode PrefixOf(QilNode expr) {
            return f.PrefixOf(expr);
        }

        #endregion // Node properties

        #region Type operators
        //-----------------------------------------------
        // Type operators
        //-----------------------------------------------
        public QilNode TypeAssert(QilNode expr, XmlQueryType t) {
            return f.TypeAssert(expr, t);
        }

        public QilNode IsType(QilNode expr, XmlQueryType t) {
            Debug.Assert(t != null, "Type can't be null");
            return f.IsType(expr, t);
        }

        public QilNode IsEmpty(QilNode set) {
            return f.IsEmpty(set);
        }

        #endregion // Type operators

        #region XPath operators
        //-----------------------------------------------
        // XPath operators
        //-----------------------------------------------
        public QilNode XPathNodeValue(QilNode expr) {
            return f.XPathNodeValue(expr);
        }

        public QilNode XPathFollowing(QilNode expr) {
            return f.XPathFollowing(expr);
        }

        public QilNode XPathNamespace(QilNode expr) {
            return f.XPathNamespace(expr);
        }

        public QilNode XPathPreceding(QilNode expr) {
            return f.XPathPreceding(expr);
        }

        #endregion // XPath operators

        #region XSLT
        //-----------------------------------------------
        // XSLT
        //-----------------------------------------------
        public QilNode XsltGenerateId(QilNode expr) {
            return f.XsltGenerateId(expr);
        }

        public QilNode XsltInvokeEarlyBound(QilNode name, MethodInfo d, XmlQueryType t, IList<QilNode> args) {
            QilList list = f.ActualParameterList();
            list.Add(args);
            return f.XsltInvokeEarlyBound(name, f.LiteralObject(d), list, t);
        }

        public QilNode XsltInvokeLateBound(QilNode name, IList<QilNode> args) {
            QilList list = f.ActualParameterList();
            list.Add(args);
            return f.XsltInvokeLateBound(name, list);
        }

        public QilNode XsltCopy(QilNode expr, QilNode content) {
            return f.XsltCopy(expr, content);
        }

        public QilNode XsltCopyOf(QilNode expr) {
            return f.XsltCopyOf(expr);
        }

        public QilNode XsltConvert(QilNode expr, XmlQueryType t) {
            return f.XsltConvert(expr, t);
        }

        #endregion // XSLT
    }
}