File: XmlTextReaderImplHelpers.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 (704 lines) | stat: -rw-r--r-- 25,108 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
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

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

using System;
using System.IO;
using System.Text;
using System.Security;
#if !SILVERLIGHT
using System.Xml.Schema;
#endif
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.Versioning;

#if SILVERLIGHT
using BufferBuilder=System.Xml.BufferBuilder;
#else
using BufferBuilder = System.Text.StringBuilder;
#endif

namespace System.Xml {

    internal partial class XmlTextReaderImpl  {

//
// ParsingState
//
        // Parsing state (aka. scanner data) - holds parsing buffer and entity input data information
        private struct ParsingState {
            // character buffer
            internal char[] chars;
            internal int    charPos;
            internal int    charsUsed;
            internal Encoding encoding;
            internal bool   appendMode;
            
            // input stream & byte buffer
            internal Stream  stream;
            internal Decoder decoder;
            internal byte[]  bytes;
            internal int     bytePos;
            internal int     bytesUsed;

            // input text reader
            internal TextReader textReader;

            // current line number & position
            internal int   lineNo;
            internal int   lineStartPos;

            // base uri of the current entity
            internal string baseUriStr;
            internal Uri    baseUri;

            // eof flag of the entity
            internal bool   isEof;
            internal bool   isStreamEof;

            // entity type & id
            internal IDtdEntityInfo entity;
            internal int            entityId;

            // normalization
            internal bool   eolNormalized;

#if !SILVERLIGHT // Needed only for XmlTextReader (reporting of entities)
            // EndEntity reporting
            internal bool   entityResolvedManually;
#endif

            internal void Clear() {
                chars = null;
                charPos = 0;
                charsUsed = 0;
                encoding = null;
                stream = null;
                decoder = null;
                bytes = null;
                bytePos = 0;
                bytesUsed = 0;
                textReader = null;
                lineNo = 1;
                lineStartPos = -1;
                baseUriStr = string.Empty;
                baseUri = null;
                isEof = false;
                isStreamEof = false;
                eolNormalized = true;
#if !SILVERLIGHT // Needed only for XmlTextReader (reporting of entities)
                entityResolvedManually = false;
#endif
            }

            internal void Close( bool closeInput ) {
                if ( closeInput ) {
                    if ( stream != null ) {
                        stream.Close();
                    }
                    else if ( textReader != null ) {
                        textReader.Close();
                    }
                }
            }

            internal int LineNo {
                get {
                    return lineNo;
                }
            }

            internal int LinePos {
                get {
                    return charPos - lineStartPos;
                }
            }
        }

//
// XmlContext
//
        private class XmlContext  {
            internal XmlSpace xmlSpace;
            internal string xmlLang;
            internal string defaultNamespace;
            internal XmlContext previousContext;

            internal XmlContext() {
                xmlSpace = XmlSpace.None;
                xmlLang = string.Empty;
                defaultNamespace = string.Empty;
                previousContext = null;
            }

            internal XmlContext( XmlContext previousContext ) {
                this.xmlSpace = previousContext.xmlSpace;
                this.xmlLang = previousContext.xmlLang;
                this.defaultNamespace = previousContext.defaultNamespace;
                this.previousContext = previousContext;
            }
        }

#if !SILVERLIGHT // Needed only for XmlTextReader
//
// NoNamespaceManager
//
        private class NoNamespaceManager : XmlNamespaceManager {    
            public NoNamespaceManager() : base () {}
            public override string DefaultNamespace { get { return string.Empty; } }
            public override void PushScope() {}
            public override bool PopScope() { return false; }
            public override void AddNamespace( string prefix, string uri ) {}
            public override void RemoveNamespace( string prefix, string uri ) {}
            public override IEnumerator GetEnumerator() { return null; }
            public override IDictionary<string,string> GetNamespacesInScope( XmlNamespaceScope scope ) { return null; }
            public override string LookupNamespace( string prefix ) { return string.Empty; }
            public override string LookupPrefix( string uri ) { return null; }
            public override bool HasNamespace( string prefix ) { return false; }
        }
#endif

//
// DtdParserProxy: IDtdParserAdapter proxy for XmlTextReaderImpl
//
#if SILVERLIGHT
        internal partial class DtdParserProxy : IDtdParserAdapter {
#else
        internal partial class DtdParserProxy : IDtdParserAdapterV1 {
#endif

            // Fields
            private XmlTextReaderImpl reader;

        // Constructors
            internal DtdParserProxy( XmlTextReaderImpl reader ) {
                this.reader = reader;
            }

        // IDtdParserAdapter proxies
            XmlNameTable IDtdParserAdapter.NameTable { 
                get { return reader.DtdParserProxy_NameTable; } 
            }

            IXmlNamespaceResolver IDtdParserAdapter.NamespaceResolver {
                get { return reader.DtdParserProxy_NamespaceResolver; }
            }
            
            Uri IDtdParserAdapter.BaseUri {
                // SxS: DtdParserProxy_BaseUri property on the reader may expose machine scope resources. This property
                // is just returning the value of the other property, so it may expose machine scope resource as well.
#if !SILVERLIGHT
                [ResourceConsumption(ResourceScope.Machine)]
                [ResourceExposure(ResourceScope.Machine)]
#endif
                get { return reader.DtdParserProxy_BaseUri; } 
            }
            
            bool IDtdParserAdapter.IsEof { 
                get { return reader.DtdParserProxy_IsEof; } 
            }
            
            char[] IDtdParserAdapter.ParsingBuffer { 
                get { return reader.DtdParserProxy_ParsingBuffer; } 
            }
            
            int IDtdParserAdapter.ParsingBufferLength { 
                get { return reader.DtdParserProxy_ParsingBufferLength; } 
            }
            
            int IDtdParserAdapter.CurrentPosition { 
                get { return reader.DtdParserProxy_CurrentPosition; } 
                set { reader.DtdParserProxy_CurrentPosition = value; } 
            }

            int IDtdParserAdapter.EntityStackLength { 
                get { return reader.DtdParserProxy_EntityStackLength; } 
            }
            
            bool IDtdParserAdapter.IsEntityEolNormalized { 
                get { return reader.DtdParserProxy_IsEntityEolNormalized; } 
            }
            
            void IDtdParserAdapter.OnNewLine( int pos ) { 
                reader.DtdParserProxy_OnNewLine( pos ); 
            }
            
            int IDtdParserAdapter.LineNo { 
                get { return reader.DtdParserProxy_LineNo; } 
            }
            
            int IDtdParserAdapter.LineStartPosition { 
                get { return reader.DtdParserProxy_LineStartPosition; } 
            }

			 int  IDtdParserAdapter.ReadData() { 
                return reader.DtdParserProxy_ReadData(); 
            }

             int  IDtdParserAdapter.ParseNumericCharRef( BufferBuilder internalSubsetBuilder ) { 
                return reader.DtdParserProxy_ParseNumericCharRef( internalSubsetBuilder ); 
            }
            
             int  IDtdParserAdapter.ParseNamedCharRef( bool expand, BufferBuilder internalSubsetBuilder ) { 
                return reader.DtdParserProxy_ParseNamedCharRef( expand, internalSubsetBuilder ); 
            }
            
            void IDtdParserAdapter.ParsePI( BufferBuilder sb ) { 
                reader.DtdParserProxy_ParsePI( sb ); 
            }
            
            void IDtdParserAdapter.ParseComment( BufferBuilder sb ) { 
                reader.DtdParserProxy_ParseComment( sb ); 
            }
            
             bool  IDtdParserAdapter.PushEntity( IDtdEntityInfo entity, out int entityId ) { 

                return reader.DtdParserProxy_PushEntity( entity, out entityId ); 

            }

			bool IDtdParserAdapter.PopEntity( out IDtdEntityInfo oldEntity, out int newEntityId ) { 
                return reader.DtdParserProxy_PopEntity( out oldEntity, out newEntityId ); 
            }

             bool  IDtdParserAdapter.PushExternalSubset( string systemId, string publicId ) { 
                return reader.DtdParserProxy_PushExternalSubset( systemId, publicId ); 
            }

			void IDtdParserAdapter.PushInternalDtd( string baseUri, string internalDtd ) {
                Debug.Assert(internalDtd != null);
                reader.DtdParserProxy_PushInternalDtd( baseUri, internalDtd ); 
            }

            void IDtdParserAdapter.Throw( Exception e ) { 
                reader.DtdParserProxy_Throw( e ); 
            }
            
            void IDtdParserAdapter.OnSystemId( string systemId, LineInfo keywordLineInfo, LineInfo systemLiteralLineInfo ) { 
                reader.DtdParserProxy_OnSystemId( systemId, keywordLineInfo, systemLiteralLineInfo ); 
            }

            void IDtdParserAdapter.OnPublicId( string publicId, LineInfo keywordLineInfo, LineInfo publicLiteralLineInfo ) { 
                reader.DtdParserProxy_OnPublicId( publicId, keywordLineInfo, publicLiteralLineInfo ); 
            }

#if !SILVERLIGHT
            bool IDtdParserAdapterWithValidation.DtdValidation { 
                get { return reader.DtdParserProxy_DtdValidation; } 
            }

            IValidationEventHandling IDtdParserAdapterWithValidation.ValidationEventHandling {
                get { return reader.DtdParserProxy_ValidationEventHandling; }
            }

            bool IDtdParserAdapterV1.Normalization { 
                get { return reader.DtdParserProxy_Normalization; } 
            }

            bool IDtdParserAdapterV1.Namespaces { 
                get { return reader.DtdParserProxy_Namespaces; } 
            }

            bool IDtdParserAdapterV1.V1CompatibilityMode { 
                get { return reader.DtdParserProxy_V1CompatibilityMode; } 
            }
#endif

        }

//
// NodeData
//
        private class NodeData : IComparable  {
            // static instance with no data - is used when XmlTextReader is closed
            static volatile NodeData s_None;

            // NOTE: Do not use this property for reference comparison. It may not be unique.
            internal static NodeData None {
                get {
                    if ( s_None == null ) {
                        // no locking; s_None is immutable so it's not a problem that it may get initialized more than once
                        s_None = new NodeData();
                    }
                    return s_None;
                }
            }

            // type
            internal XmlNodeType type;

            // name
            internal string localName;
            internal string prefix;
            internal string ns;
            internal string nameWPrefix;

            // value:
            // value == null -> the value is kept in the 'chars' buffer starting at valueStartPos and valueLength long
            string value;          
            char[] chars;
            int    valueStartPos;   
            int    valueLength;
            
            // main line info
            internal LineInfo   lineInfo;

            // second line info
            internal LineInfo   lineInfo2;

            // quote char for attributes
            internal char   quoteChar;

            // depth
            internal int    depth;

            // empty element / default attribute
            bool   isEmptyOrDefault;

            // entity id
            internal int    entityId;

            // helper members
            internal bool   xmlContextPushed;

#if !SILVERLIGHT // Needed only for XmlTextReader (reporting of entities)
            // attribute value chunks
            internal NodeData nextAttrValueChunk;
#endif

            // type info
            internal object schemaType;
            internal object typedValue;

            internal NodeData() {
                Clear( XmlNodeType.None );
                xmlContextPushed = false;
            }

            internal int LineNo { 
                get { 
                    return lineInfo.lineNo;
                }
            }

            internal int LinePos { 
                get { 
                    return lineInfo.linePos;
                }
            }

            internal bool IsEmptyElement {
                get {
                    return type == XmlNodeType.Element && isEmptyOrDefault;
                }
                set {
                    Debug.Assert( type == XmlNodeType.Element );
                    isEmptyOrDefault = value;
                }
            }

            internal bool IsDefaultAttribute {
                get {
                    return type == XmlNodeType.Attribute && isEmptyOrDefault;
                }
                set {
                    Debug.Assert( type == XmlNodeType.Attribute );
                    isEmptyOrDefault = value;
                }
            }

            internal bool ValueBuffered {
                get { 
                    return value == null;
                }
            }

            internal string StringValue {
                get {
                    Debug.Assert( valueStartPos >= 0 || this.value != null, "Value not ready." );

                    if ( this.value == null ) {
                        this.value = new string( chars, valueStartPos, valueLength );
                    }
                    return this.value;
                }
            }

            internal void TrimSpacesInValue() {
                if ( ValueBuffered ) {
                    XmlTextReaderImpl.StripSpaces( chars, valueStartPos, ref valueLength );
                }
                else {
                    value = XmlTextReaderImpl.StripSpaces(value);
                }
            }

            internal void Clear( XmlNodeType type ) {
                this.type = type;
                ClearName();
                value = string.Empty;  
                valueStartPos = -1;
                nameWPrefix = string.Empty;
                schemaType = null;
                typedValue = null;
            }

            internal void ClearName() {
                localName = string.Empty;
                prefix = string.Empty;
                ns = string.Empty;
                nameWPrefix = string.Empty;
            }

            internal void SetLineInfo( int lineNo, int linePos ) {
                lineInfo.Set( lineNo, linePos );
            }

            internal void SetLineInfo2( int lineNo, int linePos ) {
                lineInfo2.Set( lineNo, linePos );
            }

            internal void SetValueNode( XmlNodeType type, string value ) {
                Debug.Assert( value != null );

                this.type = type;
                ClearName();
                this.value = value;
                this.valueStartPos = -1;
            }

            internal void SetValueNode( XmlNodeType type, char[] chars, int startPos, int len ) {
                this.type = type;
                ClearName();
                
                this.value = null;
                this.chars = chars;
                this.valueStartPos = startPos;
                this.valueLength = len;
            }

            internal void SetNamedNode( XmlNodeType type, string localName ) {
                SetNamedNode( type, localName, string.Empty, localName );
            }

            internal void SetNamedNode( XmlNodeType type, string localName, string prefix, string nameWPrefix ) {
                Debug.Assert( localName != null );
                Debug.Assert( localName.Length > 0 );

                this.type = type;
                this.localName = localName;
                this.prefix = prefix;
                this.nameWPrefix = nameWPrefix;
                this.ns = string.Empty;
                this.value = string.Empty;
                this.valueStartPos = -1;
            }

            internal void SetValue( string value ) {
                this.valueStartPos = -1;
                this.value = value;
            }

            internal void SetValue( char[] chars, int startPos, int len ) {
                this.value = null;
                this.chars = chars;
                this.valueStartPos = startPos;
                this.valueLength = len;
            }

            internal void OnBufferInvalidated() {
                if ( this.value == null ) {
                    Debug.Assert( valueStartPos != -1 );
                    Debug.Assert( chars != null );
                    this.value = new string( chars, valueStartPos, valueLength );
                }
                valueStartPos = -1;
            }

            internal void CopyTo( int valueOffset, BufferBuilder sb ) {
                if ( value == null ) {
                    Debug.Assert( valueStartPos != -1 );
                    Debug.Assert( chars != null );
                    sb.Append( chars, valueStartPos + valueOffset, valueLength - valueOffset );
                }
                else {
                    if ( valueOffset <= 0 ) {
                        sb.Append( value );
                    }
                    else {
                        sb.Append( value, valueOffset, value.Length - valueOffset );
                    }
                }
            }

            internal int CopyTo( int valueOffset, char[] buffer, int offset, int length ) {
                if ( value == null ) {
                    Debug.Assert( valueStartPos != -1 );
                    Debug.Assert( chars != null );
                    int copyCount = valueLength - valueOffset;
                    if ( copyCount > length ) {
                        copyCount = length;
                    }
                    XmlTextReaderImpl.BlockCopyChars( chars, valueStartPos + valueOffset, buffer, offset, copyCount );
                    return copyCount;
                }
                else {
                    int copyCount = value.Length - valueOffset;
                    if ( copyCount > length ) {
                        copyCount = length;
                    }
                    value.CopyTo( valueOffset, buffer, offset, copyCount );
                    return copyCount;
                }
            }

            internal int CopyToBinary( IncrementalReadDecoder decoder, int valueOffset ) {
                if ( value == null ) {
                    Debug.Assert( valueStartPos != -1 );
                    Debug.Assert( chars != null );
                    return decoder.Decode( chars, valueStartPos + valueOffset, valueLength - valueOffset );
                }
                else {
                    return decoder.Decode( value, valueOffset, value.Length - valueOffset );
                }
            }

            internal void AdjustLineInfo( int valueOffset, bool isNormalized, ref LineInfo lineInfo ) {
                if ( valueOffset == 0 ) {
                    return;
                }
                if ( valueStartPos != -1 ) {
                    XmlTextReaderImpl.AdjustLineInfo( chars, valueStartPos, valueStartPos + valueOffset, isNormalized, ref lineInfo );
                }
                else {
                    XmlTextReaderImpl.AdjustLineInfo( value, 0, valueOffset, isNormalized, ref lineInfo );
                }
            }

            // This should be inlined by JIT compiler
            internal string GetNameWPrefix( XmlNameTable nt ) { 
                if ( nameWPrefix != null ) {
                    return nameWPrefix;
                }
                else {
                    return CreateNameWPrefix( nt );
                }
            }

            internal string CreateNameWPrefix( XmlNameTable nt ) { 
                Debug.Assert( nameWPrefix == null );
                if ( prefix.Length == 0 ) {
                    nameWPrefix = localName;
                }
                else {
                    nameWPrefix = nt.Add( string.Concat( prefix, ":", localName ) );
                }
                return nameWPrefix;
            }

            int IComparable.CompareTo( object obj ) {
                NodeData other = obj as NodeData;
                if ( other != null ) {
                    if ( Ref.Equal( localName, other.localName ) ) {
                        if ( Ref.Equal( ns, other.ns ) ) {
                            return 0;
                        }
                        else {
                            return string.CompareOrdinal( ns, other.ns );
                        }
                    }
                    else {
                        return string.CompareOrdinal( localName, other.localName );
                    }
                }
                else {
                    Debug.Assert( false, "We should never get to this point." );
                    // 'other' is null, 'this' is not null. Always return 1, like "".CompareTo(null).
                    return 1;
                }
            }
        }

        // 
        // DtdDefaultAttributeInfoToNodeDataComparer
        // 
        // Compares IDtdDefaultAttributeInfo to NodeData
        private class DtdDefaultAttributeInfoToNodeDataComparer : IComparer<object> {
            private static IComparer<object> s_instance = new DtdDefaultAttributeInfoToNodeDataComparer();

            internal static IComparer<object> Instance {
                get { return s_instance; }
            }

            public int Compare(object x, object y) {
                Debug.Assert(x == null || x is NodeData || x is IDtdDefaultAttributeInfo);
                Debug.Assert(y == null || y is NodeData || y is IDtdDefaultAttributeInfo);

                string localName, localName2;
                string prefix, prefix2;

                if (x == null) {
                    return y == null ? 0 : -1;
                }
                else if (y == null) {
                    return 1;
                }

                NodeData nodeData = x as NodeData;
                if (nodeData != null) {
                    localName = nodeData.localName;
                    prefix = nodeData.prefix;
                }
                else {
                    IDtdDefaultAttributeInfo attrDef = x as IDtdDefaultAttributeInfo;
                    if (attrDef != null) {
                        localName = attrDef.LocalName;
                        prefix = attrDef.Prefix;
                    }
                    else {
                        throw new XmlException(Res.Xml_DefaultException, string.Empty);
                    }
                }

                nodeData = y as NodeData;
                if (nodeData != null) {
                    localName2 = nodeData.localName;
                    prefix2 = nodeData.prefix;
                }
                else {
                    IDtdDefaultAttributeInfo attrDef = y as IDtdDefaultAttributeInfo;
                    if (attrDef != null) {
                        localName2 = attrDef.LocalName;
                        prefix2 = attrDef.Prefix;
                    }
                    else {
                        throw new XmlException(Res.Xml_DefaultException, string.Empty);
                    }
                }

                // string.Compare does reference euqality first for us, so we don't have to do it here
                int result = string.Compare(localName, localName2, StringComparison.Ordinal);
                if (result != 0) {
                    return result;
                }

                return string.Compare(prefix, prefix2, StringComparison.Ordinal);
            }
        }

#if !SILVERLIGHT
//
// OnDefaultAttributeUse delegate
//
        internal delegate void OnDefaultAttributeUseDelegate(IDtdDefaultAttributeInfo defaultAttribute, XmlTextReaderImpl coreReader);
#endif

    }
}