-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlLargeFilePrettyPrint.cs
148 lines (126 loc) · 4.85 KB
/
XmlLargeFilePrettyPrint.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
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace XmlLargeFilePrettyPrint
{
public class XmlLargeFilePrettyPrint
{
private StreamReader inputStream;
private TextWriter outputStream;
private XmlTextWriter writer;
private FormatterSetter formatterSetter;
public XmlLargeFilePrettyPrint(FormatterSetter formatterSetter)
{
this.formatterSetter = formatterSetter;
}
public void PrettyPrint(StreamReader inputStream, TextWriter outputStream)
{
this.inputStream = inputStream;
this.outputStream = outputStream;
EnsureMinimumWriter();
using (XmlReader xmlReader = new XmlTextReader(inputStream))
{
TreatInputDocument(xmlReader);
}
}
private void TreatInputDocument(XmlReader xmlReader)
{
while (xmlReader.Read())
{
writer = TreatNode(writer, xmlReader);
}
}
private XmlTextWriter TreatNode(XmlTextWriter writer, XmlReader node)
{
switch (node.NodeType)
{
case XmlNodeType.None:
throw new InvalidOperationException();
case XmlNodeType.Element:
if (node.IsEmptyElement)
{
writer.WriteStartElement(node.Name);
writer.WriteAttributes(node, false);
writer.WriteEndElement();
}
else
{
writer.WriteStartElement(node.Name);
writer.WriteAttributes(node, false);
}
break;
case XmlNodeType.Attribute:
writer.WriteAttributeString(node.Name, node.Value);
break;
case XmlNodeType.Text:
writer.WriteValue(node.Value);
break;
case XmlNodeType.CDATA:
writer.WriteCData(node.Value);
break;
case XmlNodeType.EntityReference:
writer.WriteEntityRef(node.Value);
throw new InvalidOperationException();
case XmlNodeType.Entity:
writer.WriteValue(node.Value);
throw new InvalidOperationException();
case XmlNodeType.ProcessingInstruction:
writer.WriteProcessingInstruction(node.Name, node.Value);
throw new InvalidOperationException();
case XmlNodeType.Comment:
writer.WriteComment(node.Value);
break;
case XmlNodeType.Document:
throw new InvalidOperationException();
case XmlNodeType.DocumentType:
throw new InvalidOperationException();
case XmlNodeType.DocumentFragment:
throw new InvalidOperationException();
case XmlNodeType.Notation:
throw new InvalidOperationException();
case XmlNodeType.Whitespace:
//writer.WriteWhitespace(node.Value);
break;
case XmlNodeType.SignificantWhitespace:
throw new InvalidOperationException();
case XmlNodeType.EndElement:
writer.WriteEndElement();
break;
case XmlNodeType.EndEntity:
throw new InvalidOperationException();
case XmlNodeType.XmlDeclaration:
CreateCorrectWriter(node);
break;
}
return writer;
}
private XmlTextWriter CreateCorrectWriter(XmlReader inputDeclarationNode)
{
string xmlEncoding = inputDeclarationNode.GetAttribute("encoding") ?? Encoding.UTF8.WebName;
writer = new XmlTextWriter(outputStream);
SetWriterOptions();
WriteDeclarationNode(inputDeclarationNode);
return writer;
}
private void WriteDeclarationNode(XmlReader inputDeclarationNode)
{
string standaloneRaw = inputDeclarationNode.GetAttribute("standalone");
bool standalone = (standaloneRaw ?? "no") == "yes";
writer.WriteStartDocument(standalone);
writer.WriteWhitespace(Environment.NewLine);
}
private void EnsureMinimumWriter()
{
writer = new XmlTextWriter(outputStream);
SetWriterOptions();
}
private void SetWriterOptions()
{
formatterSetter.SetOptions(writer);
}
}
}