File: KeyMatchBuilder.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (111 lines) | stat: -rw-r--r-- 4,544 bytes parent folder | download | duplicates (6)
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
//------------------------------------------------------------------------------
// <copyright file="KeyMatchBuilder.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using MS.Internal.Xml;
using System.Xml.Xsl.XPath;
using System.Xml.Xsl.Qil;

namespace System.Xml.Xsl.Xslt {

    internal class KeyMatchBuilder : XPathBuilder, XPathPatternParser.IPatternBuilder {
        private int depth = 0;
        PathConvertor convertor;

        public KeyMatchBuilder(IXPathEnvironment env) : base(env) {
            convertor = new PathConvertor(env.Factory);
        }

        public override void StartBuild() {
            Debug.Assert(0 <= depth && depth <= 1, "this shouldn't happen");
            if (depth == 0) {
                base.StartBuild();
            }
            depth ++;
        }

        public override QilNode EndBuild(QilNode result) {
            depth --;
            Debug.Assert(0 <= depth && depth <= 1, "this shouldn't happen");
            if (result == null) { // special door to clean builder state in exception handlers
                return base.EndBuild(result);
            }
            if (depth == 0) {
                Debug.Assert(base.numFixupLast     == 0);
                Debug.Assert(base.numFixupPosition == 0);
                result = convertor.ConvertReletive2Absolute(result, base.fixupCurrent);
                result = base.EndBuild(result);
            }
            return result;
        }

        // -------------------------------------- GetPredicateBuilder() ---------------------------------------

        public virtual IXPathBuilder<QilNode> GetPredicateBuilder(QilNode ctx) {
            return this;
        }

        // This code depends on particula shapes that XPathBuilder generates.
        // It works only on pathes.
        // ToDo: We can do better here.
        internal class PathConvertor : QilReplaceVisitor {
            new XPathQilFactory f;
            QilNode fixup;
            public PathConvertor(XPathQilFactory f) : base (f.BaseFactory) {
                this.f = f;
            }

            public QilNode ConvertReletive2Absolute(QilNode node, QilNode fixup) {
                QilDepthChecker.Check(node);
                Debug.Assert(node  != null);
                Debug.Assert(fixup != null);
                this.fixup = fixup;
                return this.Visit(node);
            }

            // transparantly passing through Union and DocOrder
            protected override QilNode Visit(QilNode n) {
                if (
                    n.NodeType == QilNodeType.Union ||
                    n.NodeType == QilNodeType.DocOrderDistinct ||
                    n.NodeType == QilNodeType.Filter ||
                    n.NodeType == QilNodeType.Loop
                ) {
                    return base.Visit(n);
                }
                return n;
            }
            // Filers that travers Content being converted to global travers:
            // Filter($j= ... Filter($i = Content(fixup), ...))  -> Filter($j= ... Filter($i = Loop($j = DesendentOrSelf(Root(fixup)), Content($j), ...)))
            protected override QilNode VisitLoop(QilLoop n) {
                if (n.Variable.Binding.NodeType == QilNodeType.Root || n.Variable.Binding.NodeType == QilNodeType.Deref) {
                    // This is absolute path already. We shouldn't touch it
                    return n;
                }
                if (n.Variable.Binding.NodeType == QilNodeType.Content) {
                    // This is "begin" of reletive path. Let's rewrite it as absolute:
                    QilUnary content = (QilUnary)n.Variable.Binding;
                    Debug.Assert(content.Child == this.fixup, "Unexpected content node");
                    QilIterator it = f.For(f.DescendantOrSelf(f.Root(this.fixup)));
                    content.Child = it;
                    n.Variable.Binding = f.Loop(it, content);
                    return n;
                }
                n.Variable.Binding = Visit(n.Variable.Binding);
                return n;
            }

            protected override QilNode VisitFilter(QilLoop n) {
                return VisitLoop(n);
            }
        }
    }
}