[4c5516]: / tools / Translator / Translator / StringArray.cs  Maximize  Restore  History

Repo status: analyzing...

Download this file

293 lines (237 with data), 5.5 kB

  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
using System;
using System.Xml;
using System.Collections;
namespace TeamXBMC.TranslatorCore
{
/// <summary>
/// Represents a xml language file of XBMC
/// </summary>
///
public class StringArray : ICollection
{
private ArrayList strings=new ArrayList();
private Hashtable stringsMap=new Hashtable();
private double version=0.0;
#region Constructors
public StringArray()
{
}
#endregion
#region Language file procressing
/// <summary>
/// Loads a language file by filename
/// </summary>
public void Load(string filename)
{
XmlTextReader reader=null;
try
{
reader=new XmlTextReader(filename);
XmlDocument doc=new XmlDocument();
doc.Load(reader);
foreach (XmlNode node in doc)
{
if (node.NodeType==XmlNodeType.Comment && node.InnerText.IndexOf("$Revision")>-1)
{
string comment=node.InnerText;
int startPos=comment.IndexOf("$Revision")+10;
int endPos=comment.IndexOf("$", startPos);
if (endPos>startPos)
{
string str=comment.Substring(startPos, endPos-startPos);
version=Convert.ToDouble(str, System.Globalization.CultureInfo.InvariantCulture);
}
break;
}
}
XmlNode root=doc.DocumentElement.SelectSingleNode("/strings");
if (root==null)
throw new TranslatorException(filename+" is not a language file.");
XmlNodeList list=doc.DocumentElement.SelectNodes("/strings/string");
if (list.Count>0)
{
foreach (XmlNode node in list)
{
StringItem item=new StringItem();
item.LoadFromXml((XmlElement)node);
Add(item);
}
strings.Sort();
}
}
catch(Exception e)
{
throw new TranslatorException("Error loading "+filename, e);
}
finally
{
if (reader!=null)
reader.Close();
}
}
/// <summary>
/// Saves a language file by filename
/// </summary>
public bool Save(string filename)
{
return Save(filename, null);
}
/// <summary>
/// Saves a language file by filename, with additional comments
/// </summary>
public bool Save(string filename, string[] comments)
{
XmlTextWriter writer=null;
try
{
XmlDocument doc=new XmlDocument();
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"));
if (comments!=null)
{
foreach(string comment in comments)
{
doc.AppendChild(doc.CreateComment(comment));
}
}
XmlElement root=doc.CreateElement("strings");
doc.AppendChild(root);
foreach (StringItem item in this)
{
XmlElement element=doc.CreateElement("string");
root.AppendChild(element);
item.SaveToXml(ref element);
}
writer=new XmlTextWriter(filename, System.Text.Encoding.UTF8);
writer.Formatting=Formatting.Indented;
doc.Save(writer);
}
catch(Exception e)
{
throw new TranslatorException("Error saving "+filename, e);
}
finally
{
if (writer!=null)
writer.Close();
}
return true;
}
#endregion
#region Array methods
/// <summary>
/// Add a StringItem to the StringArray
/// </summary>
public void Add(StringItem item)
{
try
{
stringsMap.Add(item.Id, item);
}
catch (ArgumentException e)
{
throw new TranslatorException("The string with the id " + item.Id.ToString() + " appears more then once in this file.", e);
}
strings.Add(item);
}
/// <summary>
/// Remove all StringItems from the StringArray
/// </summary>
public void Clear()
{
strings.Clear();
stringsMap.Clear();
}
/// <summary>
/// Sort the StringArray by StringItem id
/// </summary>
public void Sort()
{
strings.Sort();
}
/// <summary>
/// Returns a string with a certain id
/// </summary>
public bool GetStringById(long id, ref StringItem item)
{
if (!stringsMap.Contains(id))
{
item=null;
return false;
}
item=(StringItem)stringsMap[id];
return true;
}
/// <summary>
/// Returns a StringArray with all string not in
/// arr.
/// </summary>
public StringArray GetStringsNotIn(StringArray arr)
{
StringArray ret=new StringArray();
foreach (StringItem item in strings)
{
if (item.Id!=6) // "XBMC SVN"
{
StringItem stringItem=null;
arr.GetStringById(item.Id, ref stringItem);
if (stringItem==null)
ret.Add(item);
}
}
return ret;
}
/// <summary>
/// Returns the ids of all changed strings
/// of arr.
/// </summary>
public long[] GetStringsChangedIn(StringArray arr)
{
ArrayList ret=new ArrayList();
foreach (StringItem item in strings)
{
if (item.Id!=6) // "XBMC SVN"
{
StringItem stringItem=null;
arr.GetStringById(item.Id, ref stringItem);
if (stringItem!=null && stringItem.Text!=item.Text)
ret.Add(item.Id);
}
}
return (long[])ret.ToArray(typeof(long));
}
#endregion
#region Properties
/// <summary>
/// Returns the version of the strings file
/// </summary>
public double Version
{
get { return version; }
}
#endregion
#region ICollection Members
public void CopyTo(Array array, int index)
{
strings.CopyTo(array, index);
}
public int Count
{
get{return strings.Count;}
}
public object SyncRoot
{
get{return this;}
}
public bool IsSynchronized
{
get{return false;}
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return strings.GetEnumerator();
}
#endregion
}
}