-
-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathUtil.cs
141 lines (126 loc) · 4.14 KB
/
Util.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
using System;
using System.IO;
using System.Text;
using System.Xml.Schema;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
namespace Schneegans.Unattend;
internal static class Util
{
internal static MemoryStream LoadFromResource(string name)
{
Type type = typeof(Util);
using var stream = type.Assembly.GetManifestResourceStream(type, "resource." + name) ?? throw new ArgumentException($"Resource '{name}' not found.");
var mstr = new MemoryStream();
stream.CopyTo(mstr);
mstr.Seek(0, SeekOrigin.Begin);
return mstr;
}
internal static string StringFromResource(string name)
{
using var mstr = LoadFromResource(name);
return new StreamReader(mstr, Encoding.UTF8).ReadToEnd();
}
internal static XmlReader XmlReaderFromResource(string name)
{
return XmlReader.Create(LoadFromResource(name));
}
internal static XmlDocument XmlDocumentFromResource(string name)
{
var doc = new XmlDocument();
doc.Load(XmlReaderFromResource(name));
return doc;
}
internal static XmlSchema XmlSchemaFromResource(string name)
{
return XmlSchema.Read(XmlReaderFromResource(name), null) ?? throw new NullReferenceException();
}
internal static XmlSchemaSet ToSchemaSet(XmlSchema schema)
{
var schemas = new XmlSchemaSet();
schemas.Add(schema);
return schemas;
}
internal static void ValidateAgainstSchema(XmlDocument doc, string schemaName)
{
var schema = XmlSchemaFromResource(schemaName);
{
string? expected = schema.TargetNamespace;
string? actual = doc.DocumentElement?.NamespaceURI;
if (expected != actual)
{
throw new XmlSchemaValidationException($"Namespace URI of root element must be '{expected}', but was '{actual}'.");
}
}
var settings = new XmlReaderSettings()
{
ValidationType = ValidationType.Schema,
Schemas = ToSchemaSet(schema),
};
using var reader = XmlReader.Create(new XmlNodeReader(doc), settings);
while (reader.Read())
{
}
}
internal static List<string> SplitLines(string s)
{
return SplitLines(new StringReader(s));
}
internal static List<string> SplitLines(TextReader reader)
{
IEnumerable<string> Enumerate()
{
string? line;
while ((line = reader.ReadLine()) != null)
{
yield return line;
}
}
return Enumerate().ToList();
}
public static XmlElement GetOrCreateElement(Pass pass, string component, XmlDocument doc, XmlNamespaceManager ns)
{
var setting = doc.SelectSingleNodeOrThrow($"/u:unattend/u:settings[@pass='{pass}']", ns);
XmlElement? elem = (XmlElement?)setting.SelectSingleNode($"u:component[@name='{component}']", ns);
if (elem == null)
{
elem = doc.CreateElement("component", ns.LookupNamespace("u"));
elem.SetAttribute("name", component);
elem.SetAttribute("processorArchitecture", "x86");
elem.SetAttribute("publicKeyToken", "31bf3856ad364e35");
elem.SetAttribute("language", "neutral");
elem.SetAttribute("versionScope", "nonSxS");
setting.AppendChild(elem);
}
return elem;
}
public static XmlElement GetOrCreateElement(Pass pass, string component, string element, XmlDocument doc, XmlNamespaceManager ns)
{
XmlElement comp = GetOrCreateElement(pass, component, doc, ns);
XmlElement? elem = (XmlElement?)comp.SelectSingleNode($"u:{element}", ns);
if (elem == null)
{
elem = doc.CreateElement(element, ns.LookupNamespace("u"));
comp.AppendChild(elem);
}
return elem;
}
public static XmlElement NewSimpleElement(string name, XmlElement parent, string innerText, XmlDocument doc, XmlNamespaceManager ns)
{
XmlElement element = doc.CreateElement(name, ns.LookupNamespace("u"));
element.InnerText = innerText;
parent.AppendChild(element);
return element;
}
public static XmlElement NewElement(string name, XmlElement parent, XmlDocument doc, XmlNamespaceManager ns)
{
XmlElement element = doc.CreateElement(name, ns.LookupNamespace("u"));
parent.AppendChild(element);
return element;
}
public static string Indent(string value)
{
return $"\r\n{value.Trim()}\r\n\t\t";
}
}