-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyXML.cs
362 lines (326 loc) · 7.31 KB
/
TinyXML.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*----------------------------------------------------------------
// Copyright (C) 2015 广州,Lucky Game
//
// 模块名:
// 创建者:D.S.Qiu
// 修改者列表:
// 创建日期:June 06 2016
// 模块描述:
//----------------------------------------------------------------*/
using UnityEngine;
using System.Collections.Generic;
namespace TinyXML
{
public class TinyXML
{
public string text;
public string name;
public List<TinyXML> children = new List<TinyXML> ();
private Dictionary<string,string> attributes;
public string GetAttribute(string attName)
{
if (attributes != null && attributes.ContainsKey (attName))
return this.attributes [attName];
return null;
}
public TinyXML GetNode (string path)
{
return null;
}
public List<TinyXML> GetNodeList(string path)
{
return null;
}
public TinyXML GetNode(string nodeName,int index)
{
int count = 0;
for (int i = 0; i < children.Count; i++)
{
TinyXML node = children [i];
if (node.name == nodeName)
{
if (index == count)
return node;
else
count++;
}
}
return null;
}
private const char LT = '<';
private const char GT = '>';
private const char SQR = ']';
private const char DASH = '-';
private const char SPACE = ' ';
private const char QUOTE = '"';
private const char SLASH = '/';
private const char QMARK = '?';
private const char EQUALS = '=';
private const char EXCLAMATION = '!';
public static TinyXML Parse(string content)
{
if (string.IsNullOrEmpty(content))
return null;
var rootNode = new TinyXML();
//trim the situation: <br/>
content = content.Replace ("/>", " />");
rootNode.text = string.Empty;
string nodeContents = "";
bool inElement = false;
bool collectNodeName = false;
bool collectAttributeName = false;
bool collectAttributeValue = false;
bool quoted = false;
string attName = "";
string attValue = "";
string nodeName = "";
string textValue = "";
bool inMetaTag = false;
bool inComment = false;
bool inDoctype = false;
bool inCDATA = false;
Stack<TinyXML> parents = new Stack<TinyXML>();
TinyXML currentNode = rootNode;
for (var i = 0; i < content.Length; i++)
{
char c= content[i];
char cn;
char cnn;
char cp;
cn = cnn = cp = '\0';
if ((i + 1) < content.Length) cn = content[i + 1];
if ((i + 2) < content.Length) cnn = content[i + 2];
if (i > 0) cp = content[i - 1];
if (inMetaTag)
{
if (c == QMARK && cn == GT)
{
inMetaTag = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == QMARK)
{
inMetaTag = true;
continue;
}
}
if (inDoctype)
{
if (cn == GT)
{
inDoctype = false;
i++;
}
continue;
}
else if (inComment)
{
if (cp == DASH && c == DASH && cn == GT)
{
inComment = false;
i++;
}
continue;
}
else
{
if (!quoted && c == LT && cn == EXCLAMATION)
{
if (content.Length > i + 9 && content.Substring(i, 9) == "<![CDATA[")
{
inCDATA = true;
i += 8;
}
else if (content.Length > i + 9 && content.Substring(i, 9) == "<!DOCTYPE")
{
inDoctype = true;
i += 8;
}
else if (content.Length > i + 2 && content.Substring(i, 4) == "<!--")
{
inComment = true;
i += 3;
}
continue;
}
}
if (inCDATA)
{
if (c == SQR && cn == SQR && cnn == GT)
{
inCDATA = false;
i += 2;
continue;
}
textValue += c;
continue;
}
if (inElement)
{
if (collectNodeName)
{
if (c == SPACE)
{
collectNodeName = false;
}
else if (c == GT)
{
collectNodeName = false;
inElement = false;
}
if (!collectNodeName && nodeName.Length > 0)
{
// close tag
if (nodeName[0] == SLASH)
{
// close tag
if (textValue.Length > 0)
{
currentNode.text += textValue;
}
textValue = "";
nodeName = "";
currentNode = parents.Pop();
}
else
{
if (textValue.Length > 0)
{
currentNode.text += textValue;
}
textValue = "";
TinyXML newNode = new TinyXML();
newNode.text = "";
newNode.name = nodeName;
if (currentNode.children == null)
{
currentNode.children = new List<TinyXML>();
}
currentNode.children.Add(newNode);
parents.Push(currentNode);
currentNode = newNode;
nodeName = "";
}
}
else
{
nodeName += c;
}
}
else
{
if (!quoted && c == SLASH && cn == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
currentNode.AddAttribute (attName, attValue);
i++;
currentNode = parents.Pop();
attName = "";
attValue = "";
}
else if (!quoted && c == GT)
{
inElement = false;
collectAttributeName = false;
collectAttributeValue = false;
currentNode.AddAttribute (attName, attValue);
attName = "";
attValue = "";
}
else {
if (collectAttributeName)
{
if (c == SPACE || c == EQUALS)
{
collectAttributeName = false;
collectAttributeValue = true;
}
else
{
attName += c;
}
}
else if (collectAttributeValue)
{
if (c == QUOTE)
{
if (quoted)
{
collectAttributeValue = false;
currentNode.AddAttribute (attName, attValue);
attValue = "";
attName = "";
quoted = false;
}
else
{
quoted = true;
}
}
else {
if (quoted)
{
attValue += c;
}
else
{
if (c == SPACE)
{
collectAttributeValue = false;
currentNode.AddAttribute (attName, attValue);
attValue = "";
attName = "";
}
}
}
}
else if (c == SPACE)
{
}
else
{
collectAttributeName = true;
attName = "" + c;
attValue = "";
quoted = false;
}
}
}
}
else
{
if (c == LT)
{
inElement = true;
collectNodeName = true;
}
else
{
textValue += c;
}
}
}
UnityEngine.Debug.Log (rootNode);
return rootNode;
}
private void AddAttribute(string attName,string attValue)
{
if (!string.IsNullOrEmpty(attName))
{
if (!string.IsNullOrEmpty(attValue))
{
attValue = string.Empty;
}
if(this.attributes == null)
this.attributes = new Dictionary<string, string>();
this.attributes.Add(attValue,attValue);
}
}
}
}