-
Notifications
You must be signed in to change notification settings - Fork 1
/
Schemer.cs
285 lines (243 loc) · 13.2 KB
/
Schemer.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
using MDSDK;
using MDSDKBase;
using MDSDKDerived;
using System.Collections;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
namespace MDSDK
{
/// <summary>
/// A class that (via its derived types) adapts to either a topic or a non-topic child element.
/// </summary>
///
internal abstract class ChildElementAdapter
{
}
internal class ChildElementAdapterTopic : ChildElementAdapter
{
public SchemerComplexTypeElementEditor SchemerComplexTypeElementEditorThatIsAChildOfThis { get; private set; }
public ChildElementAdapterTopic(SchemerComplexTypeElementEditor schemerComplexTypeElementEditorThatIsAChildOfThis)
{
this.SchemerComplexTypeElementEditorThatIsAChildOfThis = schemerComplexTypeElementEditorThatIsAChildOfThis;
}
}
internal class ChildElementAdapterNonTopic : ChildElementAdapter
{
public XmlSchemaElement XmlSchemaElementThatIsAChildOfThis { get; private set; }
public SchemerElementExistingTopicEditor? SchemerElementExistingTopicEditor { get; private set; }
public ChildElementAdapterNonTopic(XmlSchemaElement xmlSchemaElementThatIsAChildOfThis, SchemerElementExistingTopicEditor? schemerElementExistingChildTopicEditor)
{
this.XmlSchemaElementThatIsAChildOfThis = xmlSchemaElementThatIsAChildOfThis;
this.SchemerElementExistingTopicEditor = schemerElementExistingChildTopicEditor;
}
}
/// <summary>
/// A class that generates and maintains XML schema content.
/// See https://learn.microsoft.com/dotnet/standard/data/xml/reading-and-writing-xml-schemas.
/// </summary>
internal class Schemer
{
private SchemerElementsLandingPageEditor? _schemerElementsLandingPageEditor = null;
private SchemerComplexTypeElementEditor? _schemerComplexTypeElementEditorRoot = null;
private List<string> _xsdFileNames;
public Schemer(string topicsRootPath, string topicsFolderName, string schemaDisplayName, string schemaNameForFilenames, List<string> xsdFileNames)
{
SchemerEditorBase.DirectoryInfoForExistingTopics = new DirectoryInfo(topicsRootPath + topicsFolderName);
SchemerEditorBase.DirectoryInfoForNewTopics = new DirectoryInfo(topicsRootPath + topicsFolderName + @"_gen");
SchemerEditorBase.SchemaDisplayName = schemaDisplayName;
SchemerEditorBase.SchemaNameForFilenames = schemaNameForFilenames;
this._xsdFileNames = xsdFileNames;
SchemerCustomConfiguration.ReadConfigurationFile(this);
}
public void DebugInit()
{
if (SchemerEditorBase.DirectoryInfoForNewTopics!.Exists)
{
SchemerEditorBase.DirectoryInfoForNewTopics!.Delete(true);
}
}
/// <summary>
/// // See what's in the xsd, build tree of Editors, and create files.
/// </summary>
/// <exception cref="MDSDKException"></exception>
public void Survey()
{
ProgramBase.ConsoleWrite("*** SURVEY PHASE ***", ConsoleWriteStyle.Default, 2);
ProgramBase.ConsoleWrite("Reading xsd, and building tree of Editors...");
if (!SchemerEditorBase.DirectoryInfoForExistingTopics!.Exists)
{
ProgramBase.ConsoleWrite(SchemerEditorBase.DirectoryInfoForExistingTopics.FullName + " doesn't exist.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
if (SchemerEditorBase.DirectoryInfoForNewTopics!.Exists)
{
ProgramBase.ConsoleWrite(SchemerEditorBase.DirectoryInfoForNewTopics.FullName + " already exists.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
else
{
SchemerEditorBase.DirectoryInfoForNewTopics.Create();
}
try
{
var xmlSchemaSet = new XmlSchemaSet();
xmlSchemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback);
foreach(var xsdFileName in this._xsdFileNames)
xmlSchemaSet.Add(null, xsdFileName);
xmlSchemaSet.Compile();
foreach (XmlSchema xmlSchema in xmlSchemaSet.Schemas())
{
if (xmlSchema!.Elements.Count != 1)
{
ProgramBase.ConsoleWrite("The assumption is that there's only one element at the root. Need to rewrite the tool.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
foreach (XmlSchemaElement xmlSchemaElement in xmlSchema!.Elements.Values)
{
this.SurveyElementRecursive(null, null, xmlSchemaElement);
}
}
ProgramBase.ConsoleWrite(string.Empty);
ProgramBase.ConsoleWrite("Printing out tree of Editors...");
this._schemerComplexTypeElementEditorRoot!.ConsoleWriteElementTree();
}
catch (Exception e)
{
ProgramBase.ConsoleWrite(e.Message);
}
}
private void SurveyElementRecursive(SchemerComplexTypeElementEditor? schemerComplexTypeElementEditorParent, XmlSchemaElement? xmlSchemaElementParent, XmlSchemaElement xmlSchemaElement, int numberOfCharsToIndent = 0)
{
SchemerComplexTypeElementEditor? schemerComplexTypeElementEditor = null;
ProgramBase.ConsoleWriteIndent(numberOfCharsToIndent);
ProgramBase.ConsoleWrite(xmlSchemaElement.Name + " element", ConsoleWriteStyle.Highlight, 0);
ProgramBase.ConsoleWrite(" (", ConsoleWriteStyle.Default, 0);
XmlSchemaComplexType? xmlSchemaComplexType = xmlSchemaElement.ElementSchemaType as XmlSchemaComplexType;
if (xmlSchemaComplexType is not null)
{
if (xmlSchemaComplexType.ContentType != XmlSchemaContentType.ElementOnly)
{
ProgramBase.ConsoleWrite("The assumption is that complex types contain only elements (not attributes). Need to rewrite the tool.", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
XmlSchemaSequence? xmlSchemaSequence = xmlSchemaComplexType.ContentTypeParticle as XmlSchemaSequence;
if (xmlSchemaSequence is not null)
{
foreach (var item in xmlSchemaSequence.Items)
{
XmlSchemaElement? childXmlSchemaElement = item as XmlSchemaElement;
if (childXmlSchemaElement is not null)
{
if (schemerComplexTypeElementEditor is null)
{
FileInfo fileInfoForNewTopic = SchemerComplexTypeElementEditor.GetFileInfoForNewTopic(xmlSchemaElementParent, xmlSchemaElement);
ProgramBase.ConsoleWrite("will create " + fileInfoForNewTopic.FullName + ")");
// Create a new element topic, and add it to its parent topic's child element adapters collection.
schemerComplexTypeElementEditor = new SchemerComplexTypeElementEditor(fileInfoForNewTopic, xmlSchemaElement);
if (schemerComplexTypeElementEditorParent is not null)
{
schemerComplexTypeElementEditorParent.AddChildElementAdapter(schemerComplexTypeElementEditor);
}
// Predict what the path and filename for this element's topic's would be if it already existed, and get a FileInfo for it.
FileInfo fileInfoPossiblyExisting = SchemerComplexTypeElementEditor.GetFileInfoForExistingTopic(xmlSchemaElementParent, xmlSchemaElement);
// If there *is* an existing topic for this element, then create an Editor for it.
if (fileInfoPossiblyExisting.Exists)
{
schemerComplexTypeElementEditor.SetSchemerElementExistingTopicEditor(fileInfoPossiblyExisting);
}
if (this._schemerComplexTypeElementEditorRoot is null)
{
this._schemerComplexTypeElementEditorRoot = schemerComplexTypeElementEditor;
}
}
if (this._schemerElementsLandingPageEditor is null)
{
FileInfo fileInfoForNewTopic = SchemerElementsLandingPageEditor.GetFileInfoForNewTopic();
this._schemerElementsLandingPageEditor = new SchemerElementsLandingPageEditor(fileInfoForNewTopic, schemerComplexTypeElementEditor!);
}
this.SurveyElementRecursive(schemerComplexTypeElementEditor, xmlSchemaElement, childXmlSchemaElement, numberOfCharsToIndent + ProgramBase.NumberOfCharsToIndentIncrement);
}
XmlSchemaAny? childXmlSchemaAny = item as XmlSchemaAny;
if (childXmlSchemaAny is not null)
{
if (schemerComplexTypeElementEditor is not null)
{
schemerComplexTypeElementEditor.AddChildAny(childXmlSchemaAny);
}
}
}
}
}
if (schemerComplexTypeElementEditor is null)
{
// Predict what the path and filename for the child element's topic's would be if it already existed, and get a FileInfo for it.
FileInfo fileInfoPossiblyExistingChildTopic = SchemerComplexTypeElementEditor.GetFileInfoForExistingTopic(xmlSchemaElementParent, xmlSchemaElement);
// If there *is* an existing topic for the child element, then create an Editor for it.
SchemerElementExistingTopicEditor? schemerElementExistingChildTopicEditor = null;
if (fileInfoPossiblyExistingChildTopic.Exists)
{
schemerElementExistingChildTopicEditor = new SchemerElementExistingTopicEditor(fileInfoPossiblyExistingChildTopic);
ProgramBase.ConsoleWrite("will delete " + fileInfoPossiblyExistingChildTopic.FullName + ")");
}
else
{
ProgramBase.ConsoleWrite("nothing to delete)");
}
if (schemerComplexTypeElementEditorParent is not null)
{
schemerComplexTypeElementEditorParent.AddChildElementAdapter(xmlSchemaElement, schemerElementExistingChildTopicEditor);
}
}
}
private static void ValidationCallback(object? sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
ProgramBase.ConsoleWrite("WARNING: ", ConsoleWriteStyle.Warning);
else if (args.Severity == XmlSeverityType.Error)
ProgramBase.ConsoleWrite("ERROR: ", ConsoleWriteStyle.Error);
ProgramBase.ConsoleWrite(args.Message);
}
/// <summary>
/// Generate the content in the files.
/// </summary>
public void Generate()
{
ProgramBase.ConsoleWrite("*** GENERATE PHASE ***", ConsoleWriteStyle.Default, 2);
this._schemerElementsLandingPageEditor!.Generate();
this._schemerComplexTypeElementEditorRoot!.Generate();
}
/// <summary>
/// Delete existing files, and report any that still exist with the schemaName prefix (for manual deletion).
/// </summary>
public void Commit()
{
ProgramBase.ConsoleWrite("*** COMMIT PHASE ***", ConsoleWriteStyle.Default, 2);
this._schemerComplexTypeElementEditorRoot!.Commit();
}
public static bool NeedsInlineSimpleType(XmlSchemaElement xmlSchemaElement, XmlSchemaSimpleType? xmlSchemaSimpleType)
{
if (xmlSchemaElement.SchemaTypeName.Name == string.Empty && xmlSchemaSimpleType is not null)
{
switch (xmlSchemaSimpleType.TypeCode)
{
case XmlTypeCode.HexBinary:
case XmlTypeCode.Integer:
case XmlTypeCode.String:
return true;
case XmlTypeCode.Boolean:
return false;
default:
ProgramBase.ConsoleWrite($"Need to handle xmlSchemaSimpleType.TypeCode == {xmlSchemaSimpleType.TypeCode} IN ALL SWITCHES", ConsoleWriteStyle.Error);
throw new MDSDKException();
}
}
return false;
}
}
}