-
Notifications
You must be signed in to change notification settings - Fork 6
/
SymbolBank.cs
123 lines (105 loc) · 3.28 KB
/
SymbolBank.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.Diagnostics;
using System.IO;
namespace spb2xml
{
/// <summary>
/// singleton class containing all symbols and property definitions
/// </summary>
[Serializable]
public sealed class SymbolBank
{
private Hashtable guidMap = new Hashtable();
private Hashtable typesMap = new Hashtable();
private List<SymbolDef> symbolDefs = new List<SymbolDef>();
private List<string> symbolDefsNames = new List<string>();
/// <summary>
/// Initialize a blank symbol bank
/// </summary>
private SymbolBank()
{
}
public bool AddSymbolDefinitionFile(string url)
{
// extract directory
string dirName = new FileInfo(url).Directory.Parent.FullName;
XmlDocument doc = new XmlDocument();
doc.Load(url);
foreach (XmlNode n in doc.GetElementsByTagName("SymbolDef")) {
// pre-read name
XmlAttribute attr = n.Attributes["name"];
if (attr != null && !symbolDefsNames.Contains(attr.Value))
{
SymbolDef sd = new SymbolDef(dirName, n);
symbolDefsNames.Add(attr.Value);
symbolDefs.Add(sd);
}
}
return true;
}
private static SymbolBank instance = null;
private static readonly object locker = new object();
public static SymbolBank Instance
{
get
{
lock (locker)
{
if (instance == null)
{
instance = new SymbolBank();
}
return instance;
}
}
set
{
instance = value;
}
}
public void AddType(TypeDef type)
{
AddAndCheck(type, typesMap);
}
public void AddProperty(PropertyDef prop)
{
AddAndCheck(prop);
}
public void AddSet(SetDef set)
{
AddAndCheck(set);
}
private void AddAndCheck(DefinitionElement element, Hashtable col)
{
string eltName = element.Name.ToLower();
if (col.ContainsKey(eltName) || guidMap.ContainsKey(element.ID))
{
Debug.WriteLine("Addding " + element.Name + " id " + element.ID + " already exists");
return;
}
col.Add(eltName, element);
guidMap.Add(element.ID, element);
}
private void AddAndCheck(DefinitionElement element)
{
if (guidMap.ContainsKey(element.ID))
{
Debug.WriteLine("Addding " + element.Name + " id " + element.ID + " already exists");
return;
}
guidMap.Add(element.ID, element);
}
public TypeDef LookupType(string typeName)
{
return (TypeDef) typesMap[typeName.ToLower()];
}
public DefinitionElement LookupElement(Guid g)
{
return (DefinitionElement) guidMap[g];
}
}
}