/
SchemaParser.cs
336 lines (281 loc) · 10.1 KB
/
SchemaParser.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
namespace XpathXSD
{
class ElementType
{
public string name = "";
public string XPath = "";
public string TreePath = "";
public bool IsArray = false;
public XmlNode innerXML;
public elementTypes Type ;
public TreeNode treeNode = null;
}
class SchemaParserToTree
{
TreeView tree;
public SchemaParser parser;
public SchemaParserToTree(TreeView tree, SchemaParser parser)
{
this.parser = parser;
this.tree = tree;
this.tree.Nodes.Clear();
}
public void GetElements()
{
tree.Nodes.Clear();
if (parser == null) return;
foreach (ElementType ct in parser.elementList)
{
TreeNode tn = new TreeNode(ct.name);
tree.Nodes.Add(tn);
tn.Tag = ct; // ElementType info = (ElementType)node.Tag;
ct.treeNode = tn;
}
}
}
public enum elementTypes {ComplexType, SimpleType};
static class XMLFormat
{
public static string FormatXml(string xml)
{
try
{
var doc = XDocument.Parse(xml);
return doc.ToString();
}
catch (Exception)
{
return xml;
}
}
}
class SchemaParser
{
private struct ReqRec
{
public string name;
public int level;
public ReqRec(string name, int level)
{
this.name = name;
this.level = level;
}
}
private bool isReq(ArrayList lista, string nazwa, int level)
{
bool res=false;
foreach (ReqRec element in lista)
{
if (element.name == nazwa && element.level < level)
{
res = true;
break;
}
}
return res;
}
string file;
XmlDocument XmlDoc = new XmlDocument();
NameTable nt = new NameTable();
XmlNamespaceManager NameSpace;
XmlNode root;
bool _drillDown;
public List<ElementType> elementList = new List<ElementType>();
public SchemaParser(string FileName)
{
NameSpace = new XmlNamespaceManager(nt);
NameSpace.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
file = FileName;
try
{
XmlDoc.Load(file);
root = XmlDoc.DocumentElement;
drillDown = true;
GetComplexTypes(@"/xs:schema/xs:complexType", elementTypes.ComplexType);
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
public bool drillDown
{
get
{ return _drillDown; }
set
{ _drillDown = value; }
}
public string parseElement(string Name)
{
ElementType element = null;
element = elementList.Find(x => x.name == Name);
if (element == null)
{
string error = String.Format("Nie znaleziono elementu {0}", Name);
throw new Exception(error);
}
return parseElement(element);
}
public string parseElement(ElementType element)
{
ArrayList rek = new ArrayList();
string res = Parse(element.innerXML, 0, rek);
return res;
}
/// <summary>
/// Główna funkcja parsująca
/// </summary>
/// <param name="node">XML zawierający kod elementu</param>
/// <param name="startPath">do wiązania z inymi typami złożonymi</param>
/// <returns></returns>
private string Parse(XmlNode node, int level, ArrayList rekurencja)
{
string result = "";
string name;
string sub;
string multi;
string prefix;
ElementType typZlozony;
if (level == 0)
{
rekurencja.Clear();
rekurencja.Add( new ReqRec( getType(node, "name"), level));
}
foreach (XmlNode element in node.ChildNodes)
{
typZlozony = null;
name = getType(element, "name");
Console.WriteLine(name);
if (name != "")
{
multi = getType(element, "maxOccurs");
if (multi == "unbounded")
{
name += "[]";
}
result += "/" + name;
typZlozony = IsComplexType(element);
// if ((typZlozony != null) && (rekurencja.IndexOf(typZlozony.name) == -1))
if ((typZlozony != null) && !isReq(rekurencja, typZlozony.name, level))
{
if (typZlozony.name != "") //czasami się zdarzają. a nie powinny...
{
rekurencja.Add(new ReqRec(typZlozony.name, level));
Console.WriteLine("add req name:{0} leve{1}", typZlozony.name, level);
}
if (drillDown)
{
sub = Parse(typZlozony.innerXML, ++level, rekurencja);
Console.WriteLine("SUBName " + name);
Console.WriteLine("Sub contet " + sub);
if (name != "" && !name.StartsWith("/"))
{
name = "/" + name;
}
prefix = addPrefix(sub, name);
result += Environment.NewLine + prefix;
}
else
{
result += "[type: " + typZlozony.name + " ]";
}
}
else
{
//if ((typZlozony != null) && (rekurencja.IndexOf(typZlozony.name) != -1))
if ((typZlozony != null) && isReq(rekurencja, typZlozony.name, level))
{
result += "[rekurencja! type: " + typZlozony.name + " ]";
}
}
result += Environment.NewLine;
}
if (element.HasChildNodes)
{
if (getType(node, "xs:annotation") == "")
{
sub = Parse(element, level, rekurencja);
if (sub != "")
{
if (name != "" && !name.StartsWith("/"))
{
name = "/"+name;
}
prefix = addPrefix(sub, name);
if (!prefix.StartsWith("/"))
{
prefix += "/";
}
result += prefix;
}
}
}
}
return result;
}
private string addPrefix(string input, string prefix)
{
// StringBuilder sb = new StringBuilder(input);
string[] sep = { "\r\n", };
string result = "";
Console.WriteLine(prefix);
if (prefix != "")
{
foreach (string line in input.Split(sep, StringSplitOptions.RemoveEmptyEntries))
{
if (line != "")
{
result = result + prefix + line + Environment.NewLine;
}
}
}
else
result = input;
return result;
}
private ElementType IsComplexType(XmlNode node)
{
ElementType element = null;
string Name = getType(node, "type");
int pos = Name.IndexOf(':');
if (pos != -1)
Name = Name.Substring(++pos);
element = elementList.Find(x => ((x.name == Name) &&(x.Type ==elementTypes.ComplexType )));
return element; //jeśli uda się znaleźć - należy użyć elementu do kolejnego wyszukania.
}
private string getType(XmlNode node, string type)
{
string res = "";
try
{
res = node.Attributes.GetNamedItem(type).Value;
}
catch(NullReferenceException e)
{
Console.WriteLine("no element @" + type);
}
return res;
}
private void GetComplexTypes(string XPath, elementTypes Type)
{
XmlNodeList nl = root.SelectNodes(XPath, NameSpace);
foreach (XmlNode element in nl)
{
ElementType ct = new ElementType();
ct.name = element.Attributes.GetNamedItem("name").Value;
ct.innerXML = element; //new XmlElement("element", element.InnerXml);
ct.Type = Type;
ct.XPath = XPath;
elementList.Add(ct);
}
}
}
}