File: XsdCachingReader.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (658 lines) | stat: -rw-r--r-- 24,974 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
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

//------------------------------------------------------------------------------
// <copyright file="XsdCachingReader.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------

using System.IO;
using System.Text;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Diagnostics;
using System.Globalization;
using System.Collections;
using System.Security.Policy;

namespace System.Xml {

    internal partial class XsdCachingReader : XmlReader, IXmlLineInfo {

        private enum CachingReaderState {
            None = 0,
            Init = 1,
            Record = 2,
            Replay = 3,
            ReaderClosed = 4,
            Error = 5,
        }
        
        private XmlReader coreReader;
        private XmlNameTable coreReaderNameTable;

        private ValidatingReaderNodeData[] contentEvents;
        private ValidatingReaderNodeData[] attributeEvents;
                    
        private ValidatingReaderNodeData cachedNode;
        
        private CachingReaderState cacheState;
        int contentIndex;
        int attributeCount;

        private bool returnOriginalStringValues;

        private CachingEventHandler cacheHandler;

        //current state
        int currentAttrIndex;
        int currentContentIndex;
        bool readAhead;
        
        //Lineinfo
        IXmlLineInfo lineInfo;

        //ReadAttributeValue TextNode
        private ValidatingReaderNodeData textNode;

        //Constants
        private const int InitialAttributeCount = 8;
        private const int InitialContentCount = 4;

//Constructor
        internal XsdCachingReader(XmlReader reader, IXmlLineInfo lineInfo, CachingEventHandler handlerMethod) {
            this.coreReader = reader;
            this.lineInfo = lineInfo;
            this.cacheHandler = handlerMethod;
            attributeEvents = new ValidatingReaderNodeData[InitialAttributeCount];
            contentEvents = new ValidatingReaderNodeData[InitialContentCount];
            Init();
        }
        
        private void Init() {
            coreReaderNameTable = coreReader.NameTable;
            cacheState = CachingReaderState.Init;
            contentIndex = 0;
            currentAttrIndex = -1;
            currentContentIndex = -1;
            attributeCount = 0;
            cachedNode = null;
            readAhead = false;
            //Initialize the cachingReader with start state
            if (coreReader.NodeType == XmlNodeType.Element) {
                ValidatingReaderNodeData element = AddContent(coreReader.NodeType);
                element.SetItemData(coreReader.LocalName, coreReader.Prefix, coreReader.NamespaceURI, coreReader.Depth);  //Only created for element node type
                element.SetLineInfo(lineInfo);
                RecordAttributes();
            }
        }

        internal void Reset(XmlReader reader) {
            this.coreReader = reader;
            Init();
        }

        // Settings
        public override XmlReaderSettings Settings { 
            get {                
                return coreReader.Settings;
            }
        }

    // Node Properties

        // Gets the type of the current node.
        public override XmlNodeType NodeType { 
            get {
                return cachedNode.NodeType;
            }
        }

        // Gets the name of the current node, including the namespace prefix.
        public override string Name { 
            get {
                return cachedNode.GetAtomizedNameWPrefix(coreReaderNameTable); 
            }
        }

        // Gets the name of the current node without the namespace prefix.
        public override string LocalName { 
            get {
                return cachedNode.LocalName;
            }
        }

        // Gets the namespace URN (as defined in the W3C Namespace Specification) of the current namespace scope.
        public override string NamespaceURI { 
            get {
                return cachedNode.Namespace;
            }
        }

        // Gets the namespace prefix associated with the current node.
        public override string Prefix { 
            get {
                return cachedNode.Prefix;
            }
        }

        // Gets a value indicating whether the current node can have a non-empty Value.
        public override bool HasValue { 
            get {
                return XmlReader.HasValueInternal(cachedNode.NodeType);
            }
        }

        // Gets the text value of the current node.
        public override string Value { 
            get {
                return returnOriginalStringValues ? cachedNode.OriginalStringValue : cachedNode.RawValue;
            }
        }

        // Gets the depth of the current node in the XML element stack.
        public override int Depth { 
            get {
                return cachedNode.Depth;
            }
        }

        // Gets the base URI of the current node.
        public override string BaseURI { 
            get {
                return coreReader.BaseURI;
            }
        }

        // Gets a value indicating whether the current node is an empty element (for example, <MyElement/>).
        public override bool IsEmptyElement { 
            get {
                return false;
            }
        }

        // Gets a value indicating whether the current node is an attribute that was generated from the default value defined
        // in the DTD or schema.
        public override bool IsDefault { 
            get {
                return false;
            }
        }

        // Gets the quotation mark character used to enclose the value of an attribute node.
        public override char QuoteChar { 
            get {
                return coreReader.QuoteChar;
            }
        }

        // Gets the current xml:space scope.
        public override XmlSpace XmlSpace { 
            get {
                return coreReader.XmlSpace;
            }
        }

        // Gets the current xml:lang scope.
        public override string XmlLang { 
            get {
                return coreReader.XmlLang;
            }
        }

    // Attribute Accessors

        // The number of attributes on the current node.
        public override int AttributeCount { 
            get {
                return attributeCount; 
            }
        }

        // Gets the value of the attribute with the specified Name.
        public override string GetAttribute( string name ) {
            int i;
            if (name.IndexOf( ':' ) == -1) {
                i = GetAttributeIndexWithoutPrefix(name);
            }
            else {
                i = GetAttributeIndexWithPrefix(name);
            }
            return (i >= 0) ? attributeEvents[i].RawValue : null;
        }

        // Gets the value of the attribute with the specified LocalName and NamespaceURI.
       public override string GetAttribute( string name, string namespaceURI ) {
            namespaceURI = ( namespaceURI == null ) ? string.Empty : coreReaderNameTable.Get( namespaceURI );
            name = coreReaderNameTable.Get(name);
            ValidatingReaderNodeData attribute;
            for ( int i = 0; i < attributeCount; i++ ) {
                attribute = attributeEvents[i];
                if ( Ref.Equal(attribute.LocalName, name) && Ref.Equal(attribute.Namespace, namespaceURI) ) {
                    return attribute.RawValue;
                }
            }
            return null;
        }

        // Gets the value of the attribute with the specified index.
        public override string GetAttribute( int i ) {
            if ( i < 0 || i >= attributeCount ) {
                throw new ArgumentOutOfRangeException("i");
            }
            return attributeEvents[i].RawValue;
        }

        // Gets the value of the attribute with the specified index.
        public override string this [ int i ] {
            get {
                return GetAttribute(i);
            }
        }

        // Gets the value of the attribute with the specified Name.
        public override string this [ string name ] { 
            get {
                return GetAttribute(name);
            }
        }

        // Gets the value of the attribute with the specified LocalName and NamespaceURI.
        public override string this [ string name, string namespaceURI ] { 
            get {
                return GetAttribute(name, namespaceURI);
            }
        }

        // Moves to the attribute with the specified Name.
        public override bool MoveToAttribute( string name ) {
            int i;
            if (name.IndexOf( ':' ) == -1) {
                i = GetAttributeIndexWithoutPrefix( name );
            }
            else {
                i = GetAttributeIndexWithPrefix( name );
            }

            if ( i >= 0 ) {
                currentAttrIndex = i;
                cachedNode = attributeEvents[i];
                return true;
            }
            else {
                return false;
            }
        }

        // Moves to the attribute with the specified LocalName and NamespaceURI
        public override bool MoveToAttribute( string name, string ns ) {
            ns = (ns == null) ? string.Empty : coreReaderNameTable.Get(ns);
            name = coreReaderNameTable.Get(name);
            ValidatingReaderNodeData attribute;
            for ( int i = 0; i < attributeCount; i++ ) {
                attribute = attributeEvents[i];
                if ( Ref.Equal(attribute.LocalName, name) &&
                     Ref.Equal(attribute.Namespace, ns) ) {
                         currentAttrIndex = i;
                         cachedNode = attributeEvents[i];
                    return true;
                }
            }
            return false;
        }
        
        // Moves to the attribute with the specified index.
        public override void MoveToAttribute(int i) {
            if ( i < 0 || i >= attributeCount ) {
                throw new ArgumentOutOfRangeException( "i" );
            }
            currentAttrIndex = i;
            cachedNode = attributeEvents[i];
        }

        // Moves to the first attribute.
        public override bool MoveToFirstAttribute() {
            if (attributeCount == 0) {
                return false;
            }
            currentAttrIndex = 0;
            cachedNode = attributeEvents[0];
            return true;
        }

        // Moves to the next attribute.
        public override bool MoveToNextAttribute() {
            if (currentAttrIndex + 1 < attributeCount) {
                cachedNode = attributeEvents[++currentAttrIndex];
                return true;
            }
            return false;
        }

        // Moves to the element that contains the current attribute node.
        public override bool MoveToElement() {
            if (cacheState != CachingReaderState.Replay || cachedNode.NodeType != XmlNodeType.Attribute) {
                return false;
            }
            currentContentIndex = 0;
            currentAttrIndex = -1;
            Read();
            return true;
        }

        // Reads the next node from the stream/TextReader.
        public override  bool  Read() {
            switch (cacheState) {
                case CachingReaderState.Init:
                    cacheState = CachingReaderState.Record;
                    goto case CachingReaderState.Record;

                case CachingReaderState.Record: 
                    ValidatingReaderNodeData recordedNode = null;
                    if (coreReader.Read()) {
                        switch(coreReader.NodeType) {
                            case XmlNodeType.Element:
                                //Dont record element within the content of a union type since the main reader will break on this and the underlying coreReader will be positioned on this node
                                cacheState = CachingReaderState.ReaderClosed;
                                return false;

                            case XmlNodeType.EndElement:
                                recordedNode = AddContent(coreReader.NodeType);
                                recordedNode.SetItemData(coreReader.LocalName, coreReader.Prefix, coreReader.NamespaceURI, coreReader.Depth);  //Only created for element node type
                                recordedNode.SetLineInfo(lineInfo);
                                break;

                            case XmlNodeType.Comment:
                            case XmlNodeType.ProcessingInstruction:
                            case XmlNodeType.Text:
                            case XmlNodeType.CDATA:
                            case XmlNodeType.Whitespace:
                            case XmlNodeType.SignificantWhitespace:
                                recordedNode = AddContent(coreReader.NodeType);
                                recordedNode.SetItemData(coreReader.Value);
                                recordedNode.SetLineInfo(lineInfo);
                                recordedNode.Depth = coreReader.Depth;
                                break;

                            default:
                                break;       
                        }
                        cachedNode = recordedNode;
                        return true;    
                    }
                    else {
                        cacheState = CachingReaderState.ReaderClosed;
                        return false;
                    }    

                case CachingReaderState.Replay:
                    if (currentContentIndex >= contentIndex) { //When positioned on the last cached node, switch back as the underlying coreReader is still positioned on this node
                        cacheState = CachingReaderState.ReaderClosed;
                        cacheHandler(this);
                        if (coreReader.NodeType != XmlNodeType.Element || readAhead) { //Only when coreReader not positioned on Element node, read ahead, otherwise it is on the next element node already, since this was not cached
                            return coreReader.Read();
                        }
                        return true;                        
                    }
                    cachedNode = contentEvents[currentContentIndex];
                    if (currentContentIndex > 0) {
                        ClearAttributesInfo();
                    }
                    currentContentIndex++;
                    return true;

                default:
                    return false;
            }
        }

        internal ValidatingReaderNodeData RecordTextNode(string textValue, string originalStringValue, int depth, int lineNo, int linePos) {
            ValidatingReaderNodeData textNode = AddContent(XmlNodeType.Text);
            textNode.SetItemData(textValue, originalStringValue);
            textNode.SetLineInfo(lineNo, linePos);
            textNode.Depth = depth;
            return textNode;
        }

        internal void SwitchTextNodeAndEndElement( string textValue, string originalStringValue ) {
            Debug.Assert(coreReader.NodeType == XmlNodeType.EndElement || (coreReader.NodeType == XmlNodeType.Element && coreReader.IsEmptyElement));

            ValidatingReaderNodeData textNode = RecordTextNode(textValue, originalStringValue, coreReader.Depth + 1, 0, 0);
            int endElementIndex = contentIndex - 2;
            ValidatingReaderNodeData endElementNode = contentEvents[endElementIndex];
            Debug.Assert(endElementNode.NodeType == XmlNodeType.EndElement);
            contentEvents[endElementIndex] = textNode;
            contentEvents[contentIndex - 1] = endElementNode;   
        }

        internal void RecordEndElementNode() {
            ValidatingReaderNodeData recordedNode = AddContent(XmlNodeType.EndElement);
            Debug.Assert(coreReader.NodeType == XmlNodeType.EndElement || (coreReader.NodeType == XmlNodeType.Element && coreReader.IsEmptyElement));
            recordedNode.SetItemData(coreReader.LocalName, coreReader.Prefix, coreReader.NamespaceURI, coreReader.Depth);  
            recordedNode.SetLineInfo(coreReader as IXmlLineInfo);
            if (coreReader.IsEmptyElement) { //Simulated endElement node for <e/>, the coreReader is on cached Element node itself.
                readAhead = true;
            }
        }

        internal string ReadOriginalContentAsString() {
            returnOriginalStringValues = true;
            string strValue = InternalReadContentAsString();
            returnOriginalStringValues = false;
            return strValue;
        }

        // Gets a value indicating whether XmlReader is positioned at the end of the stream.
        public override bool EOF { 
            get {
                return cacheState == CachingReaderState.ReaderClosed && coreReader.EOF;
            }
        }

        // Closes the stream, changes the ReadState to Closed, and sets all the properties back to zero.
        public override void Close() {
            coreReader.Close();
            cacheState = CachingReaderState.ReaderClosed;
        }

        // Returns the read state of the stream.
        public override ReadState ReadState { 
            get {
                return coreReader.ReadState;
            }
        }

        // Skips to the end tag of the current element.
        public override void Skip() {
            //Skip on caching reader should move to the end of the subtree, past all cached events
            switch (cachedNode.NodeType) {
                case XmlNodeType.Element:
                    if (coreReader.NodeType != XmlNodeType.EndElement && !readAhead) { //will be true for IsDefault cases where we peek only one node ahead
                        int startDepth = coreReader.Depth - 1;
                        while (coreReader.Read() && coreReader.Depth > startDepth) 
                        ;
                    }
                    coreReader.Read();
                    cacheState = CachingReaderState.ReaderClosed;
                    cacheHandler(this);
                    break;
    
                case XmlNodeType.Attribute:
                    MoveToElement();
                    goto case XmlNodeType.Element;

                default:
                    Debug.Assert(cacheState == CachingReaderState.Replay);
                    Read();
                    break;
            }
        }

        // Gets the XmlNameTable associated with this implementation.
        public override XmlNameTable NameTable { 
            get {
                return coreReaderNameTable;
            }
        }

        // Resolves a namespace prefix in the current element's scope.
        public override string LookupNamespace( string prefix) {
            return coreReader.LookupNamespace(prefix);
        }

        // Resolves the entity reference for nodes of NodeType EntityReference.
        public override void ResolveEntity() {
            throw new InvalidOperationException();
        }

        // Parses the attribute value into one or more Text and/or EntityReference node types.
        public override bool ReadAttributeValue() {
            Debug.Assert(cacheState == CachingReaderState.Replay);
            if (cachedNode.NodeType != XmlNodeType.Attribute) {
                return false;
            }
            cachedNode = CreateDummyTextNode(cachedNode.RawValue, cachedNode.Depth + 1);
            return true;
        }

        //
        // IXmlLineInfo members
        //

        bool IXmlLineInfo.HasLineInfo() {
            return true;
        }

        int IXmlLineInfo.LineNumber {
            get {
                return cachedNode.LineNumber;
            }
        }

        int IXmlLineInfo.LinePosition { 
            get {
                return cachedNode.LinePosition;
            }
        }

//Private methods
        internal void SetToReplayMode() {
            cacheState = CachingReaderState.Replay;
            currentContentIndex = 0;
            currentAttrIndex = -1;
            Read(); //Position on first node recorded to begin replaying
        }

        internal XmlReader GetCoreReader() {
            return coreReader;
        }

        internal IXmlLineInfo GetLineInfo() {
            return lineInfo;
        }

        private void ClearAttributesInfo() {
            attributeCount = 0;
            currentAttrIndex = -1;
        }

        private ValidatingReaderNodeData AddAttribute(int attIndex) {
            Debug.Assert(attIndex <= attributeEvents.Length);
            ValidatingReaderNodeData attInfo = attributeEvents[attIndex];
            if (attInfo != null) {
                attInfo.Clear(XmlNodeType.Attribute);
                return attInfo;
            }
            if (attIndex >= attributeEvents.Length -1 ) { //reached capacity of array, Need to increase capacity to twice the initial
                ValidatingReaderNodeData[] newAttributeEvents = new ValidatingReaderNodeData[attributeEvents.Length * 2];
                Array.Copy(attributeEvents, 0, newAttributeEvents, 0, attributeEvents.Length);
                attributeEvents = newAttributeEvents;
            }
            attInfo = attributeEvents[attIndex];
            if (attInfo == null) {
                attInfo = new ValidatingReaderNodeData(XmlNodeType.Attribute);
                attributeEvents[attIndex] = attInfo;
            }
            return attInfo;
        }

        private ValidatingReaderNodeData AddContent(XmlNodeType nodeType) {
            Debug.Assert(contentIndex <= contentEvents.Length);
            ValidatingReaderNodeData contentInfo = contentEvents[contentIndex];
            if (contentInfo != null) {
                contentInfo.Clear(nodeType);
                contentIndex++;
                return contentInfo;
            }
            if (contentIndex >= contentEvents.Length -1 ) { //reached capacity of array, Need to increase capacity to twice the initial
                ValidatingReaderNodeData[] newContentEvents = new ValidatingReaderNodeData[contentEvents.Length * 2];
                Array.Copy(contentEvents, 0, newContentEvents, 0, contentEvents.Length);
                contentEvents = newContentEvents;
            }
            contentInfo = contentEvents[contentIndex];
            if (contentInfo == null) {
                contentInfo = new ValidatingReaderNodeData(nodeType);
                contentEvents[contentIndex] = contentInfo;
            }
            contentIndex++;
            return contentInfo;
        }

        private void RecordAttributes() {
            Debug.Assert(coreReader.NodeType == XmlNodeType.Element);
            ValidatingReaderNodeData attInfo;
            attributeCount = coreReader.AttributeCount;
            if (coreReader.MoveToFirstAttribute()) {
                int attIndex = 0;
                do {
                    attInfo = AddAttribute(attIndex);
                    attInfo.SetItemData(coreReader.LocalName, coreReader.Prefix, coreReader.NamespaceURI, coreReader.Depth);
                    attInfo.SetLineInfo(lineInfo);
                    attInfo.RawValue = coreReader.Value;
                    attIndex++;
                } while (coreReader.MoveToNextAttribute());
                coreReader.MoveToElement();
            }
        }
    
        private int GetAttributeIndexWithoutPrefix(string name) {
            name = coreReaderNameTable.Get(name);
            if ( name == null ) {
                return -1;
            }
            ValidatingReaderNodeData attribute;
            for ( int i = 0; i < attributeCount; i++ ) {
                attribute = attributeEvents[i];
                if ( Ref.Equal(attribute.LocalName, name) && attribute.Prefix.Length == 0 ) {
                    return i;
                }
            }
            return -1;
        }

        private int GetAttributeIndexWithPrefix(string name) {
            name = coreReaderNameTable.Get(name);
            if ( name == null ) {
                return -1;
            }
            ValidatingReaderNodeData attribute;
            for ( int i = 0; i < attributeCount; i++ ) {
                attribute = attributeEvents[i];
                if ( Ref.Equal(attribute.GetAtomizedNameWPrefix(coreReaderNameTable), name) ) {
                    return i;
                }
            }
            return -1;
        }

        private ValidatingReaderNodeData CreateDummyTextNode(string attributeValue, int depth) {
            if (textNode == null) {
                textNode = new ValidatingReaderNodeData(XmlNodeType.Text);
            }
            textNode.Depth = depth;
            textNode.RawValue = attributeValue;
            return textNode;
        }

    }
}