-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
177 lines (160 loc) · 6.37 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace Demcon.XmlSorter
{
class Program
{
static void Main(string[] args)
{
bool showHelp = false;
try
{
// check the supplied arguments
string filename;
string[] ignoredNodes = { "" };
string path;
string[] extensions = {""};
if (args.Count() == 1)
{
filename = args[0];
SortFile(filename, ignoredNodes);
}
else if (args.Count() == 2)
{
filename = args[0];
ignoredNodes = args[1].Split(';');
SortFile(filename, ignoredNodes);
}
else if (args.Count() == 3)
{
path = args[0];
extensions = args[1].Split(';');
ignoredNodes = args[2].Split(';');
SortDir(path, extensions, ignoredNodes);
}
else
{
showHelp = true;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
showHelp = true;
}
if (showHelp)
{
Console.WriteLine("XmlSorter functionality:");
Console.WriteLine(" sort XML-nodes and XML-attributes alphabetically in XML-file");
Console.WriteLine(" remove duplicate nodes and attributes");
Console.WriteLine();
Console.WriteLine("usage1: XmlSorter \"filename\"");
Console.WriteLine("usage2: XmlSorter \"filename\" \"node;*\"");
Console.WriteLine("usage3: XmlSorter \"dir\" \"ext;*\" \"node;*\"");
Console.WriteLine(" \"filename\": only sort the given file");
Console.WriteLine(" \"node;*\": XML node-names, separate by ';', these nodes are NOT sorted");
Console.WriteLine(" \"dir\": sort all files in this directory and all sub-directories");
Console.WriteLine(" \"ext;*\": file-extensions without '.', separated by ';', only sort files with given extensions");
Console.WriteLine(" arguments can be included in double quotes \" \", but don't have to be");
Environment.Exit(1);
}
}
static private void SortDir(string path, string[] extensions, string[] ignoredNodes)
{
string[] files = Directory.GetFiles(path);
string[] subDirs = Directory.GetDirectories(path);
foreach (string file in files)
{
foreach (string extension in extensions)
{
if (file.EndsWith("." + extension))
{
SortFile(file, ignoredNodes);
}
}
}
foreach (string dir in subDirs)
{
SortDir(dir, extensions, ignoredNodes);
}
}
static private void SortFile(string pathFilename, string[] ignoredNodes)
{
// read file
string srcText = File.ReadAllText(pathFilename);
XDocument srcXDoc = XDocument.Parse(srcText);
// sort
XElement sortedNode = SortNode(srcXDoc.Root, ignoredNodes);
// write
XDocument destXDoc = new XDocument(srcXDoc.Declaration, sortedNode);
string destText;
if (srcXDoc.Declaration != null)
{
destText = srcXDoc.Declaration.ToString() + "\r\n" + destXDoc.ToString();
}
else
{
destText = destXDoc.ToString();
}
File.WriteAllText(pathFilename, destText);
}
static private XElement SortNode(XElement node, string[] ignoredNodes)
{
// do not sort this node if it is in the ignored list
string name = node.Name.LocalName;
if (ignoredNodes.Contains(node.Name.LocalName))
{
return node;
}
// do not sort this node if it has an attribute xml:space="preserve"
XAttribute attr = node.Attribute(XNamespace.Xml + "space");
if (attr != null && attr.Value == "preserve")
{
return node;
}
// sort attributes (and delete double entries)
if (node.HasAttributes)
{
List<XAttribute> sortedAttributes = node.Attributes().OrderBy(a => a.ToString()).ToList();
node.RemoveAttributes();
//sortedAttributes.ForEach(a => node.Add(a));
XAttribute attrPrev = new XAttribute("EmptyAttr", "");
foreach (XAttribute attrNew in sortedAttributes)
{
if (attrNew.ToString() != attrPrev.ToString())
{
node.Add(attrNew);
attrPrev = attrNew;
}
}
}
// sort the children (and delete double entries)
if (node.HasElements)
{
// sort all child-node content
foreach (XElement subNode in node.Elements())
{
SortNode(subNode, ignoredNodes);
}
// sort the child-nodes themselves
//List<XElement> sortedChildren = node.Elements().OrderBy(elem => elem.Attributes("Name").Any() ? elem.Attributes("Name").First().Value.ToString() : string.Empty).ToList();
List<XElement> sortedChildren = node.Elements().OrderBy(elem => elem.ToString()).ToList();
node.RemoveNodes();
//sortedChildren.ForEach(c => node.Add(c));
XElement nodePrev = new XElement("EmptyNode", "");
foreach (XElement nodeNew in sortedChildren)
{
if (nodeNew.ToString() != nodePrev.ToString())
{
node.Add(nodeNew);
nodePrev = nodeNew;
}
}
}
return node;
}
}
}