-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrettyPrintXmlTask.cs
280 lines (262 loc) · 11.6 KB
/
PrettyPrintXmlTask.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
#region Coypright and License
/*
* MSBuild.Axantum.Tasks - Copyright 2011, Svante Seleborg, All Rights Reserved
*
* This file is part of MSBuild.Axantum.Tasks.
*
* MSBuild.Axantum.Tasks is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MSBuild.Axantum.Tasks is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MSBuild.Axantum.Tasks. If not, see <http://www.gnu.org/licenses/>.
*
* The source is maintained at http://msbuildaxantumtasks.codeplex.com/ please visit for
* updates, contributions and contact with the author. You may also visit
* http://www.axantum.com for more information about the author.
*/
#endregion Coypright and License
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace MSBuild.Axantum.Tasks
{
public class PrettyPrintXmlTask : Task
{
public override bool Execute()
{
if (SourceFiles.Length != DestinationFiles.Length)
{
throw new ArgumentOutOfRangeException("DestinationFiles", "Not the same number of DestinationFiles as SourceFiles");
}
for (int i = 0; i < SourceFiles.Length; ++i)
{
PrettyPrintXmlFile(SourceFiles[i], DestinationFiles[i]);
}
return true;
}
private void PrettyPrintXmlFile(string sourceFile, string destinationFile)
{
FileInfo sourceInfo = new FileInfo(sourceFile);
if (!sourceInfo.Exists)
{
throw new ArgumentException("File not found.", "sourceFile");
}
XmlDocument xml = new XmlDocument();
xml.Load(sourceFile);
using (MemoryStream prettyOutput = new MemoryStream((int)(sourceInfo.Length + sourceInfo.Length / 10)))
{
PrettyPrintXml(sourceInfo, prettyOutput);
FileInfo destinationInfo = new FileInfo(destinationFile);
bool isEqual = IsFileIdenticalWithStream(destinationInfo, prettyOutput);
if (!isEqual)
{
WriteStreamToFileWithBackup(destinationInfo, prettyOutput);
}
}
}
private static void PrettyPrintXml(FileInfo xmlInfo, Stream prettyOutput)
{
using (StreamReader reader = new StreamReader(xmlInfo.FullName))
using (XmlReader xmlReader = XmlReader.Create(reader))
{
TextWriter textWriter = new StreamWriter(prettyOutput, Encoding.UTF8);
string indent = "";
string attributeIndent = "";
bool inhibitNewLineAtEndElement = false;
textWriter.Write(@"<?xml version=""1.0"" encoding=""utf-8""?>");
while (xmlReader.Read())
{
switch (xmlReader.NodeType)
{
case XmlNodeType.Attribute:
throw new InvalidOperationException("Attribute found out of sequence. Internal error.");
case XmlNodeType.CDATA:
break;
case XmlNodeType.Comment:
string[] comments = xmlReader.Value.Split(new char[] { (char)10 }, StringSplitOptions.None);
if (comments.Length == 0)
{
break;
}
string first = comments[0].Trim();
textWriter.WriteLine();
textWriter.Write("{0}<!-- {1}", indent, first);
string oldIndent = indent;
indent += " ";
int commentIndent = first.LastIndexOf(" ");
if (commentIndent >= 0)
{
indent = indent + SpaceString(commentIndent + 2);
}
for (int i = 1; i < comments.Length; ++i)
{
textWriter.WriteLine();
textWriter.Write("{0}{1}", indent, comments[i].Trim());
}
textWriter.Write(" -->");
indent = oldIndent;
break;
case XmlNodeType.Document:
break;
case XmlNodeType.DocumentFragment:
break;
case XmlNodeType.DocumentType:
break;
case XmlNodeType.Element:
attributeIndent = "";
string element = xmlReader.Name;
textWriter.WriteLine();
textWriter.Write("{0}<{1}", indent, element);
int attributesLeft = xmlReader.AttributeCount;
while (xmlReader.MoveToNextAttribute())
{
--attributesLeft;
string content = String.Format("{0} {1}=\"{2}\"", attributeIndent, xmlReader.Name, HttpUtility.HtmlEncode(xmlReader.Value));
textWriter.Write(content);
if (attributesLeft > 0)
{
textWriter.WriteLine();
}
if (String.IsNullOrEmpty(attributeIndent))
{
attributeIndent = indent + SpaceString(element.Length + 1);
}
}
xmlReader.MoveToElement();
attributeIndent = "";
if (xmlReader.IsEmptyElement)
{
textWriter.Write(" />");
}
else
{
indent += " ";
textWriter.Write(">");
}
break;
case XmlNodeType.EndElement:
indent = indent.Substring(0, indent.Length - 4);
if (!inhibitNewLineAtEndElement)
{
textWriter.WriteLine();
textWriter.Write("{0}", indent);
}
textWriter.Write("</{0}>", xmlReader.Name);
inhibitNewLineAtEndElement = false;
break;
case XmlNodeType.EndEntity:
break;
case XmlNodeType.Entity:
break;
case XmlNodeType.EntityReference:
break;
case XmlNodeType.None:
break;
case XmlNodeType.Notation:
break;
case XmlNodeType.ProcessingInstruction:
break;
case XmlNodeType.SignificantWhitespace:
break;
case XmlNodeType.Text:
textWriter.Write(xmlReader.Value);
inhibitNewLineAtEndElement = true;
break;
case XmlNodeType.Whitespace:
break;
case XmlNodeType.XmlDeclaration:
break;
default:
break;
}
}
textWriter.WriteLine();
textWriter.Flush();
}
}
private static string SpaceString(int commentIndent)
{
return Enumerable.Repeat<string>(" ", commentIndent).Aggregate((a, b) => a + b);
}
private static void PrettyPrintXml2(XmlDocument xml, Stream prettyOutput)
{
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Encoding = Encoding.UTF8;
writerSettings.Indent = true;
writerSettings.IndentChars = " ";
writerSettings.NewLineChars = Environment.NewLine;
writerSettings.NewLineHandling = NewLineHandling.Replace;
writerSettings.NewLineOnAttributes = true;
writerSettings.OmitXmlDeclaration = false;
using (XmlWriter destinationWriter = XmlWriter.Create(prettyOutput, writerSettings))
{
xml.WriteContentTo(destinationWriter);
}
}
private static bool IsFileIdenticalWithStream(FileInfo fileInfo, MemoryStream stream)
{
if (!fileInfo.Exists || fileInfo.Length != stream.Length)
{
return false;
}
byte[] newContent = stream.GetBuffer();
byte[] currentContent = new byte[fileInfo.Length];
using (FileStream destinationStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read))
{
destinationStream.Read(currentContent, 0, currentContent.Length);
}
for (int i = 0; i < currentContent.Length; ++i)
{
if (newContent[i] != currentContent[i])
{
return false;
}
}
return true;
}
private static void WriteStreamToFileWithBackup(FileInfo fileInfo, MemoryStream stream)
{
FileInfo tempInfo = new FileInfo(fileInfo.FullName + ".bak");
try
{
if (fileInfo.Exists)
{
if (tempInfo.Exists)
{
tempInfo.Delete();
}
File.Move(fileInfo.FullName, tempInfo.FullName);
}
using (FileStream destinationStream = new FileStream(fileInfo.FullName, FileMode.CreateNew, FileAccess.Write))
{
destinationStream.Write(stream.GetBuffer(), 0, (int)stream.Length);
}
}
finally
{
tempInfo.Refresh();
fileInfo.Refresh();
if (tempInfo.Exists && !fileInfo.Exists)
{
tempInfo.MoveTo(fileInfo.FullName);
}
}
}
[Required]
public string[] SourceFiles { get; set; }
[Required]
public string[] DestinationFiles { get; set; }
}
}