This repository has been archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonValidatingReader.cs
477 lines (438 loc) · 11.2 KB
/
NonValidatingReader.cs
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
using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;
namespace NonValidating
{
public class NonValidatingReader: XmlReader
{
private string content = String.Empty;
private int index = 0,begin=0;
private XmlNodeType _NodeType = XmlNodeType.None;
public override XmlNodeType NodeType
{
get {
return _NodeType;
}
}
private string _Name = String.Empty;
public override string Name
{
get {
if (NodeType == XmlNodeType.Element || NodeType == XmlNodeType.EndElement)
{
//Console.WriteLine("Looking at name '{0}'",_Name);
return _Name;
}
else if (NodeType == XmlNodeType.Attribute)
{
string p = Prefix;
//Console.WriteLine("Grabbing {0} ({1})",GetAttributeName(attr_index),_Attributes[GetAttributeName(attr_index)]);
if (p == String.Empty)
return GetAttributeName(attr_index);
else
return p+":"+GetAttributeName(attr_index);
}
else
return String.Empty;
}
}
private string _Value = String.Empty;
public override string Value
{
get {
if (NodeType == XmlNodeType.Text)
return _Value;
else if (NodeType == XmlNodeType.Attribute)
return GetAttribute(attr_index);
else
return String.Empty;
}
}
public override string BaseURI
{
get {
return String.Empty; /* FIXME: other values ?*/
}
}
private int _Depth = 0;
public override int Depth
{
get {
return _Depth;
}
}
private ReadState rs = ReadState.Initial;
public override ReadState ReadState
{
get {
return rs;
}
}
public override bool EOF
{
get {
return rs == ReadState.EndOfFile;
}
}
public override bool HasValue
{
get {
return NodeType == XmlNodeType.Text || NodeType == XmlNodeType.Attribute;
}
}
private bool isEmpty = false;
public override bool IsEmptyElement
{
get {
return isEmpty;
}
}
public override string LocalName
{
get {
if (NodeType == XmlNodeType.Text)
return String.Empty;
string N = Name;
int idx = N.IndexOf(":");
if (idx!=-1)
return N.Substring(idx+1);
else
return N;
}
}
public override string NamespaceURI
{
get {
string N = Name;
try
{
if (N.IndexOf(":")!=-1)
return Namespaces[NameTable.Add(N.Substring(0,N.IndexOf(":")))];
/*else if (NodeType == XmlNodeType.Attribute && _Name.IndexOf(":")!=-1)
return Namespaces[NameTable.Add(_Name.Substring(0,_Name.IndexOf(":")))];*/
else if (NodeType == XmlNodeType.Attribute)
return "";
else
return Namespaces[String.Empty];
}
catch(KeyNotFoundException e)
{
if (N.IndexOf(":")!=-1 && N.IndexOf("xmlns:")==-1)
{
Console.WriteLine("name: {0} ns: {1}",N,N.Substring(0,N.IndexOf(":")));
throw e;
}
return String.Empty;
}
}
}
private Dictionary<string,string> _Attributes = null;
private int attr_index = 0;
public override int AttributeCount
{
get {
if (NodeType!=XmlNodeType.Element && NodeType!=XmlNodeType.Attribute)
return 0;
else if (_Attributes!=null)
return _Attributes.Count;
else
return 0;
}
}
public override string GetAttribute(string s)
{
throw new Exception(s);
return _Attributes[s];
}
public override string GetAttribute(string s, string ns)
{
throw new Exception();
return _Attributes[ns+":"+s]; // FIXME: not correct
}
private Dictionary<string,string> Namespaces;
public override string LookupNamespace(string s)
{
return Namespaces[s];
}
private string GetAttributeName(int i)
{
string[] vals = new string[_Attributes.Keys.Count];
_Attributes.Keys.CopyTo(vals,0);
return vals[i];
}
public override string GetAttribute(int i)
{
//Console.WriteLine("Grabbing {0} of {2} ({1})",GetAttributeName(i),_Attributes[GetAttributeName(i)],NamespaceURI);
return _Attributes[GetAttributeName(i)];
}
public override bool MoveToAttribute(string s, string ns)
{
throw new Exception();
return false; // FIXME: not correct!
}
public override bool MoveToAttribute(string s)
{
throw new Exception();
return false; // FIXME: not correct!
}
public override void MoveToAttribute(int i) // FIXME: does nothing!
{
throw new Exception();
}
public override bool MoveToElement()
{
if (NodeType == XmlNodeType.Attribute)
{
_NodeType = XmlNodeType.Element;
return true;
}
else
{
throw new Exception();
return false;
}
}
public override bool MoveToFirstAttribute()
{
if (NodeType == XmlNodeType.Attribute || (NodeType == XmlNodeType.Element && _Attributes.Count>0))
{
_NodeType = XmlNodeType.Attribute;
attr_index = 0;
return true;
}
else
{
//Console.WriteLine("nodetype: {0}, Name:{1}",NodeType,_Name);
//throw new Exception();
return false;
}
}
public override bool MoveToNextAttribute()
{
if (NodeType == XmlNodeType.Element && _Attributes.Count>0)
return MoveToFirstAttribute();
else if (NodeType == XmlNodeType.Attribute &&_Attributes.Count>attr_index+1)
{
attr_index+=1;
return true;
}
else
{
//throw new Exception();
return false;
}
}
public override bool ReadAttributeValue() // FIXME: does nothing!
{
throw new Exception();
return false;
}
public override void ResolveEntity() // FIXME: does nothing!
{
throw new Exception();
}
private NameTable _NameTable;
public override XmlNameTable NameTable
{
get {
return _NameTable;
}
}
public override string Prefix
{
get {
if (NodeType == XmlNodeType.Attribute || _Name.IndexOf(":")==-1)
return String.Empty;
else
return NameTable.Add(_Name.Substring(0,_Name.IndexOf(':')));
}
}
public override XmlSpace XmlSpace
{
get {
return XmlSpace.Preserve;
}
}
public NonValidatingReader(string content):base()
{
this.content = content;
_NameTable = new NameTable();
Namespaces = new Dictionary<string,string>();
Namespaces.Add(_NameTable.Add("xml"),"http://www.w3.org/XML/1998/namespace");
}
public override string ReadOuterXml()
{
if (_NodeType != XmlNodeType.Element)
throw new Exception("Nodetype during readouterxml should be element");
string find = _Name;
int start = begin;
Console.WriteLine("skipping until we see a {0}",find);
while(Read())
{
if (_NodeType == XmlNodeType.EndElement && _Name.CompareTo(find)==0)
break;
//Console.WriteLine("current item is {0} of type {1}",Name,NodeType);
}
Console.WriteLine("skipped until we saw a {0}",find);
//Read();
Console.WriteLine("current item is {0} of type {1}",_Name,NodeType);
return content.Substring(start,index-start);
}
public override bool Read()
{
int angle;
if (index == content.Length || content.Substring(index).Trim().Length==0)
{
_NodeType = XmlNodeType.None;
rs = ReadState.EndOfFile;
return false;
}
rs = ReadState.Interactive;
switch(NodeType)
{
case XmlNodeType.Text:
case XmlNodeType.None:
StringBuilder tempName = null;
angle = content.IndexOf("<",index);
if (angle == -1)
{
Console.WriteLine("Content: {0}",content.Substring(index));
throw new Exception("no angle none");
}
int endangle = content.IndexOf(">",angle);
if (endangle == -1)
throw new Exception("no endangle");
tempName = new StringBuilder(content.Substring(angle+1,endangle-angle-1));
//Console.WriteLine("name: {0}",tempName);
_NodeType = XmlNodeType.Element;
isEmpty = false;
if (tempName[0] == '/')
{
_NodeType = XmlNodeType.EndElement;
tempName.Remove(0,1);
_Depth -=1;
}
else
{
if (tempName[tempName.Length-1] == '/')
{
isEmpty = true;
tempName.Length-=1;
}
}
if (tempName.ToString().IndexOf("?xml")==0)
{
_NodeType = XmlNodeType.XmlDeclaration;
tempName.Length-=1;
}
int space = tempName.ToString().IndexOf(" ");
if (space!=-1)
{
string raw = tempName.ToString(space+1,tempName.Length-space-1);
_Attributes = new Dictionary<string,string>();
foreach (String s in raw.Split(new char[]{' ','\t','\n'},StringSplitOptions.RemoveEmptyEntries))
{
string[] bits = s.Trim().Split('=');
if (bits.Length!=2)
{
/*Console.WriteLine("raw: {0}",raw);
Console.WriteLine("orig: '{0}'",s);
Console.WriteLine("bits: {0}",bits.Length);
for(int i=0;i<bits.Length;i++)
Console.WriteLine("bits[{0}] '{1}'",i,bits[i]);*/
continue; // ignore!
//throw new Exception("bits length is weird");
}
try
{
if (bits[1][0]=='\"' || bits[1][0] == '\'')
bits[1] = bits[1].Substring(1);
if (bits[1][bits[1].Length-1]=='\"' || bits[1][bits[1].Length-1] == '\'')
bits[1] = bits[1].Substring(0,bits[1].Length-1);
}
catch(Exception e)
{
Console.WriteLine("raw: {0}",raw);
Console.WriteLine("orig: {0}",s);
Console.WriteLine("bits: {0}",bits.Length);
for(int i=0;i<bits.Length;i++)
Console.WriteLine("bits[{0}] {1}",i,bits[i]);
throw e;
}
//Console.WriteLine("new attr: {0}={1}",bits[0],bits[1]);
if (bits[0].IndexOf("xmlns")!=-1) // new namespace
{
if (bits[0].CompareTo("xmlns")==0)
{
Namespaces.Add(String.Empty,NameTable.Add(bits[1]));
Console.WriteLine("default ns: {0}",Namespaces[String.Empty]);
}
else
{
Namespaces.Add(NameTable.Add(bits[0].Substring(bits[0].IndexOf(":")+1)),NameTable.Add(bits[1]));
Console.WriteLine("new ns: {0}",bits[1]);
}
}
if (!_Attributes.ContainsKey(NameTable.Add(bits[0])))
_Attributes.Add(NameTable.Add(bits[0]),NameTable.Add(bits[1]));
}
tempName.Remove(space,tempName.Length-space);
}
//Console.WriteLine("name: {0}",tempName);
_Name = NameTable.Add(tempName.ToString());
_Value = String.Empty;
begin = index;
index = endangle+1;
break;
case XmlNodeType.Element:
if (_Attributes!=null && _Attributes.Count>attr_index)
{
_NodeType = XmlNodeType.Attribute;
attr_index = 0;
break;
}
else
goto case XmlNodeType.EndElement;
case XmlNodeType.EndElement:
case XmlNodeType.XmlDeclaration:
angle = content.IndexOf("<",index);
if (angle == -1)
{
Console.WriteLine("Content: {0}",content.Substring(index));
throw new Exception("no angle");
}
if (angle>index)
{
string text = content.Substring(index,angle-index);
if (text.Trim().Length>0)
{
begin = index;
index += text.Length;
_Value = text;
_NodeType = XmlNodeType.Text;
//Console.WriteLine("text node value: '{0}'",_Value);
break;
}
}
goto case XmlNodeType.None;
case XmlNodeType.Attribute:
if (_Attributes.Count>attr_index+1)
{
attr_index+=1;
break;
}
else
goto case XmlNodeType.EndElement;
default:
throw new Exception("bad type");
}
return true;
}
public override void Close()
{
content = String.Empty;
rs = ReadState.Closed;
}
}
}