Debian Codegraph @

scripts/localize/pvpgn_localize_validator.cs

raw symbols SCIP
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Xml;
6 using System.Xml.Linq;
7 using System.Xml.Serialization;
8
9 namespace pvpgn_localize_validator
10 {
11 class Program
12 {
13 static void Main(string[] args)
14 {
15 if (args.Length == 0)
16 {
17 Console.WriteLine("This utility checks XML translation file for missing translations, arguments and illegal characters\n(c) 2014 HarpyWar ([email protected])");
18 Console.WriteLine("\nUsage: {0} [filename.xml]\n", AppDomain.CurrentDomain.FriendlyName);
19
20 Environment.Exit(0);
21 }
22
23 process_file(args[0]);
24 }
25
26 static void process_file(string filename)
27 {
28 try
29 {
30 XDocument doc = XDocument.Load(filename, LoadOptions.SetLineInfo);
31
32 // * no author name
33 var authors = doc.Descendants("author");
34 foreach (var author in authors)
35 {
36 if (author.Attribute("email").Value == "[email protected]")
37 logerror(author, "Please, remove or update example author.");
38 }
39
40 var items = doc.Descendants("item");
41 XElement original, translate;
42 XAttribute id;
43 foreach (var item in items)
44 {
45 original = item.Element("original");
46 translate = item.Element("translate");
47
48 // * check missing references
49 if ((id = translate.Attribute("refid")) != null)
50 {
51 if (items.Where(x => x.Attribute("id").Value == id.Value).Count() == 0)
52 logerror(translate, string.Format("string id=\"{0}\" is not found", id.Value));
53 }
54 else
55 check_string(original, translate);
56 }
57 }
58 catch (Exception e)
59 {
60 Console.WriteLine("[ERROR] " + e.Message);
61 }
62 }
63
64
65 static void check_string(XElement original, XElement translate)
66 {
67 string a = original.Value;
68 string b = translate.Value;
69
70 if (a.Trim().Length == 0)
71 {
72 logerror(translate, "empty original string");
73 return;
74 }
75 if (b.Trim().Length == 0)
76 {
77 logerror(translate, "empty translation");
78 return;
79 }
80
81 // * illegal characters
82 if (!validate_symbols(a) || !validate_symbols(b))
83 logerror(translate, string.Format("string contains one of invalid characters: {0}", string.Join(", ", invalid_symbols)));
84
85 // * missing arguments
86 if (a.Count(f => f == '{') != b.Count(f => f == '{') && a.Count(f => f == '}') != b.Count(f => f == '}'))
87 logerror(translate, "missing argument in translation?");
88
89 // * reduntant or missing spaces at start
90 if (get_pos(a, ' ') != get_pos(b, ' '))
91 logerror(translate, "spaces count at the beginning of the translation is not equal with original");
92
93 // * reduntant or missing spaces at the end
94 if (get_pos(a, ' ', true) != get_pos(b, ' ', true))
95 logerror(translate, "spaces count at the end of the translation is not equal with original");
96 }
97
98 static int get_pos(string str, char c, bool reverse = false)
99 {
100 int pos = 0;
101 if (!reverse)
102 for (int i = 0; i < str.Length; i++)
103 {
104 if (str[i] == c)
105 pos++;
106 else
107 break;
108 }
109 else
110 for (int i = str.Length-1; i > 0; i--)
111 {
112 if (str[i] == c)
113 pos++;
114 else
115 break;
116 }
117 return pos;
118 }
119
120 static string[] invalid_symbols = new string[]
121 {
122 ">",
123 "<"
124 };
125 static bool validate_symbols(string str)
126 {
127 foreach(var s in invalid_symbols)
128 if (str.Contains(s))
129 return false;
130 return true;
131 }
132
133 static void logerror(XElement el, string text)
134 {
135 IXmlLineInfo info = el;
136 int lineNumber = info.LineNumber;
137
138 Console.WriteLine("[WARNING] line {0}: {1}", lineNumber, text);
139 }
140 }
141
142
143 }