schangxiang@126.com
2025-09-18 49a51c068d62084bc4c3e77c4be94a20de556c4a
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
"use strict";
exports.__esModule = true;
/**
 * Scope for XML namespaces
 * @param {NamespaceScope} [parent] Parent scope
 * @returns {NamespaceScope}
 * @constructor
 */
var NamespaceScope = /** @class */ (function () {
    function NamespaceScope(parent) {
        this.parent = parent;
        this.namespaces = {};
    }
    /**
     * Look up the namespace URI by prefix
     * @param {String} prefix Namespace prefix
     * @param {Boolean} [localOnly] Search current scope only
     * @returns {String} Namespace URI
     */
    NamespaceScope.prototype.getNamespaceURI = function (prefix, localOnly) {
        switch (prefix) {
            case 'xml':
                return 'http://www.w3.org/XML/1998/namespace';
            case 'xmlns':
                return 'http://www.w3.org/2000/xmlns/';
            default:
                var nsUri = this.namespaces[prefix];
                /*jshint -W116 */
                if (nsUri != null) {
                    return nsUri.uri;
                }
                else if (!localOnly && this.parent) {
                    return this.parent.getNamespaceURI(prefix);
                }
                else {
                    return null;
                }
        }
    };
    NamespaceScope.prototype.getNamespaceMapping = function (prefix) {
        switch (prefix) {
            case 'xml':
                return {
                    uri: 'http://www.w3.org/XML/1998/namespace',
                    prefix: 'xml',
                    declared: true
                };
            case 'xmlns':
                return {
                    uri: 'http://www.w3.org/2000/xmlns/',
                    prefix: 'xmlns',
                    declared: true
                };
            default:
                var mapping = this.namespaces[prefix];
                /*jshint -W116 */
                if (mapping != null) {
                    return mapping;
                }
                else if (this.parent) {
                    return this.parent.getNamespaceMapping(prefix);
                }
                else {
                    return null;
                }
        }
    };
    /**
     * Look up the namespace prefix by URI
     * @param {String} nsUri Namespace URI
     * @param {Boolean} [localOnly] Search current scope only
     * @returns {String} Namespace prefix
     */
    NamespaceScope.prototype.getPrefix = function (nsUri, localOnly) {
        switch (nsUri) {
            case 'http://www.w3.org/XML/1998/namespace':
                return 'xml';
            case 'http://www.w3.org/2000/xmlns/':
                return 'xmlns';
            default:
                for (var p in this.namespaces) {
                    if (this.namespaces[p].uri === nsUri) {
                        return p;
                    }
                }
                if (!localOnly && this.parent) {
                    return this.parent.getPrefix(nsUri);
                }
                else {
                    return null;
                }
        }
    };
    return NamespaceScope;
}());
/**
 * Namespace context that manages hierarchical scopes
 * @returns {NamespaceContext}
 * @constructor
 */
var NamespaceContext = /** @class */ (function () {
    function NamespaceContext() {
        this.scopes = [];
        this.pushContext();
        this.prefixCount = 0;
    }
    /**
     * Add a prefix/URI namespace mapping
     * @param {String} prefix Namespace prefix
     * @param {String} nsUri Namespace URI
     * @param {Boolean} [localOnly] Search current scope only
     * @returns {boolean} true if the mapping is added or false if the mapping
     * already exists
     */
    NamespaceContext.prototype.addNamespace = function (prefix, nsUri, localOnly) {
        if (this.getNamespaceURI(prefix, localOnly) === nsUri) {
            return false;
        }
        if (this.currentScope) {
            this.currentScope.namespaces[prefix] = {
                uri: nsUri,
                prefix: prefix,
                declared: false
            };
            return true;
        }
        return false;
    };
    /**
     * Push a scope into the context
     * @returns {NamespaceScope} The current scope
     */
    NamespaceContext.prototype.pushContext = function () {
        var scope = new NamespaceScope(this.currentScope);
        this.scopes.push(scope);
        this.currentScope = scope;
        return scope;
    };
    /**
     * Pop a scope out of the context
     * @returns {NamespaceScope} The removed scope
     */
    NamespaceContext.prototype.popContext = function () {
        var scope = this.scopes.pop();
        if (scope) {
            this.currentScope = scope.parent;
        }
        else {
            this.currentScope = null;
        }
        return scope;
    };
    /**
     * Look up the namespace URI by prefix
     * @param {String} prefix Namespace prefix
     * @param {Boolean} [localOnly] Search current scope only
     * @returns {String} Namespace URI
     */
    NamespaceContext.prototype.getNamespaceURI = function (prefix, localOnly) {
        return this.currentScope && this.currentScope.getNamespaceURI(prefix, localOnly);
    };
    /**
     * Look up the namespace prefix by URI
     * @param {String} nsURI Namespace URI
     * @param {Boolean} [localOnly] Search current scope only
     * @returns {String} Namespace prefix
     */
    NamespaceContext.prototype.getPrefix = function (nsUri, localOnly) {
        return this.currentScope && this.currentScope.getPrefix(nsUri, localOnly);
    };
    /**
     * Register a namespace
     * @param {String} nsUri Namespace URI
     * @returns {String} The matching or generated namespace prefix
     */
    NamespaceContext.prototype.registerNamespace = function (nsUri) {
        var prefix = this.getPrefix(nsUri);
        if (prefix) {
            // If the namespace has already mapped to a prefix
            return prefix;
        }
        else {
            // Try to generate a unique namespace
            while (true) {
                prefix = 'ns' + (++this.prefixCount);
                if (!this.getNamespaceURI(prefix)) {
                    // The prefix is not used
                    break;
                }
            }
        }
        this.addNamespace(prefix, nsUri, true);
        return prefix;
    };
    /**
     * Declare a namespace prefix/uri mapping
     * @param {String} prefix Namespace prefix
     * @param {String} nsUri Namespace URI
     * @returns {Boolean} true if the declaration is created
     */
    NamespaceContext.prototype.declareNamespace = function (prefix, nsUri) {
        if (this.currentScope) {
            var mapping = this.currentScope.getNamespaceMapping(prefix);
            if (mapping && mapping.uri === nsUri && mapping.declared) {
                return false;
            }
            this.currentScope.namespaces[prefix] = {
                uri: nsUri,
                prefix: prefix,
                declared: true
            };
            return true;
        }
        return false;
    };
    return NamespaceContext;
}());
exports.NamespaceContext = NamespaceContext;
//# sourceMappingURL=nscontext.js.map