File: GameXmlToGetString.cs

package info (click to toggle)
gbrainy 1:2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 14,372 kB
  • sloc: cs: 18,019; sh: 3,662; xml: 1,421; makefile: 558
file content (126 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 2010 Jordi Mas i Hernàndez <jmas@softcatala.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Text;
using System.IO;
using System.Collections.Generic;

using gbrainy.Core.Main.Xml;

public class GameXmlToGetString
{
	static readonly string output = "GameXmlGetString.cs";
	static readonly string games = "../data/games.xml";
	static readonly string template = "GameXmlGetStringTemplate.cs";

	static void Main (string[] args)
	{
		Dictionary <string, string> tokens = new Dictionary <string, string> ();
		StringBuilder strings = new StringBuilder ();
		GamesXmlFactory factory;
		TextWriter tw;
		string games_file, template_file, output_file, str;
		int cnt = 0;

		output_file = args.Length > 1 ?  Path.Combine (args[1], output) : output;

		tw = new StreamWriter (output_file);
		factory = new GamesXmlFactory ();

		games_file = args.Length > 0 ?  Path.Combine (args[0], games) : games;
		factory.Read (games_file);

		// Build GetStrings
		foreach (GameXmlDefinition definition in factory.Definitions)
		{
			if (definition.Question != null) {
				str = GetStringFromDefinition (definition.Question);
				if (String.IsNullOrEmpty (str) == false) {
					strings.AppendLine (str);
					cnt++;
				}
			}

			if (definition.Rationale != null) {
				str = GetStringFromDefinition (definition.Rationale);
				if (String.IsNullOrEmpty (str) == false) {
					strings.AppendLine (str);
					cnt++;
				}
			}

			foreach (GameXmlDefinitionVariant variant in definition.Variants)
			{
				if (variant.Question != null)
				{
					str = GetStringFromDefinition (variant.Question);
					if (String.IsNullOrEmpty (str) == false) {
						strings.AppendLine (str);
						cnt++;
					}
				}

				if (variant.Rationale != null)
				{
					str = GetStringFromDefinition (variant.Rationale);
					if (String.IsNullOrEmpty (str) == false) {
						strings.AppendLine (str);
						cnt++;
					}
				}
			}
		}

		tokens.Add ("@STRINGS@", strings.ToString ());

		// Replace tokens
		template_file = args.Length > 0 ?  Path.Combine (args[0], template) : template;

		string line;
		Stream read = File.OpenRead (template_file);
		StreamReader sr = new StreamReader (read);
		while (true)
		{
			line = sr.ReadLine ();
			if (line == null)
				break;

			foreach (string token in tokens.Keys)
			{
				line = line.Replace (token, tokens[token]);
			}
			tw.WriteLine (line);
		}
		read.Close ();
		tw.Close ();

		Console.WriteLine ("gbrainy's GameXmlToGetString, {0} strings extracted", cnt);
	}

	static string GetStringFromDefinition (LocalizableString localizable)
	{
		if (localizable.IsPlural () == false)
			return null;

		return String.Format ("\t\tCatalog.GetPluralString (\"{0}\",\n\t\t\t\"{1}\",\n\t\t\t{2});\n",
			localizable.String,
			localizable.PluralString,
			"variable");
	}
}