-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextProvider.cs
210 lines (162 loc) · 4.63 KB
/
TextProvider.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
using System;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace TheBox.Lang
{
[ Serializable ]
/// <summary>
/// Provides localized text elements for the box
/// </summary>
public class TextProvider
{
/// <summary>
/// Gets the text associated with the specified resource
/// </summary>
public string this[string description]
{
get
{
string[] locate = description.Split( new char[] { '.' } );
if ( locate.Length != 2 )
{
throw new Exception( "Bad text request" );
}
Hashtable loc = (Hashtable) m_Sections[ locate[ 0 ] ];
if ( loc == null )
return null;
return (string) loc[ locate[1] ];
}
set
{
string[] locate = description.Split( new char[] { '.' } );
if ( locate.Length != 2 )
{
throw new Exception( "Bad descriptor when adding a new entry to text provider" );
}
Add( value, locate[0], locate[1] );
}
}
private Hashtable m_Sections;
private string m_Language;
/// <summary>
/// Gets or sets a string identifying the language represented by the text provider
/// </summary>
public string Language
{
get { return m_Language; }
set { m_Language = value; }
}
/// <summary>
/// Gets or sets the data collection for this text provider
/// </summary>
public Hashtable Data
{
get { return m_Sections; }
set { m_Sections = value; }
}
/// <summary>
/// Creates a new TextProvider object
/// </summary>
public TextProvider()
{
m_Sections = new Hashtable();
}
private void Add( string text, string category, string definition )
{
Hashtable loc = null;
if ( m_Sections.ContainsKey( category ) )
{
loc = (Hashtable) m_Sections[ category ];
}
else
{
loc = new Hashtable();
m_Sections.Add( category, loc );
}
loc[ definition ] = text;
}
/// <summary>
/// Deletes a section contained in the TextProvider
/// </summary>
/// <param name="name">The name of the section that will be deleted</param>
public void DeleteSection( string name )
{
m_Sections.Remove( name );
}
/// <summary>
/// Removes an item from the TextProvider
/// </summary>
/// <param name="section"></param>
/// <param name="item"></param>
public void RemoveItem( string section, string item )
{
Hashtable hash = (Hashtable) m_Sections[ section ];
if ( hash != null )
{
hash.Remove( item );
}
}
public void RemoveItem( string definition )
{
string[] loc = definition.Split( new char[] { '.' } );
if ( loc.Length != 2 )
return;
RemoveItem( loc[0], loc[1] );
}
public void Serialize( string filename )
{
XmlDocument dom = new XmlDocument();
XmlNode decl = dom.CreateXmlDeclaration( "1.0", null, null );
dom.AppendChild( decl );
XmlNode lang = dom.CreateElement( "Data" );
XmlAttribute langtype = dom.CreateAttribute( "language" );
langtype.Value = m_Language;
lang.Attributes.Append( langtype );
foreach ( string toplevel in m_Sections.Keys )
{
XmlNode topnode = dom.CreateElement( "section" );
XmlAttribute topname = dom.CreateAttribute( "name" );
topname.Value = toplevel;
topnode.Attributes.Append( topname );
Hashtable hash = ( (Hashtable) m_Sections[ toplevel ] );
foreach( string lowlevel in hash.Keys )
{
XmlNode entrynode = dom.CreateElement( "entry" );
XmlAttribute name = dom.CreateAttribute( "name" );
name.Value = lowlevel;
entrynode.Attributes.Append( name );
XmlAttribute val = dom.CreateAttribute( "text" );
val.Value = (string) hash[ lowlevel ];
entrynode.Attributes.Append( val );
topnode.AppendChild( entrynode );
}
lang.AppendChild( topnode );
}
dom.AppendChild( lang );
dom.Save( filename );
}
public static TextProvider Deserialize( string filename )
{
XmlDocument dom = new XmlDocument();
dom.Load( filename );
XmlNode data = dom.ChildNodes[ 1 ];
TextProvider text = new TextProvider();
text.m_Language = data.Attributes[ "language" ].Value;
foreach ( XmlNode section in data.ChildNodes )
{
string topkey = section.Attributes[ "name" ].Value;
Hashtable hash = new Hashtable();
foreach ( XmlNode entry in section.ChildNodes )
{
string lowkey = entry.Attributes[ "name" ].Value;
string t = entry.Attributes[ "text" ].Value;
hash.Add( lowkey, t );
}
text.m_Sections.Add( topkey, hash );
}
return text;
}
}
}