Developer Area

root/apps/outlook/branches/1.5/DotTelSystem/Punycode/generate/GenerateRFC3454.cs @ 580

Revision 580, 12.7 kB (checked in by jonmaycock, 10 months ago)
Line 
1/// <summary> Copyright (C) 2004, 2007  Free Software Foundation, Inc.
2/// *
3/// Author: Alexander Gnauck AG-Software
4/// *
5/// This file is part of GNU Libidn.
6/// *
7/// This library is free software; you can redistribute it and/or
8/// modify it under the terms of the GNU Lesser General Public License
9/// as published by the Free Software Foundation; either version 2.1 of
10/// the License, or (at your option) any later version.
11/// *
12/// This library is distributed in the hope that it will be useful, but
13/// WITHOUT ANY WARRANTY; without even the implied warranty of
14/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15/// Lesser General Public License for more details.
16/// *
17/// You should have received a copy of the GNU Lesser General Public
18/// License along with this library; if not, write to the Free Software
19/// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20/// USA
21/// </summary>
22using System;
23using System.IO;
24using System.Text;
25
26namespace gnu.inet.encoding.misc
27{
28    public class GenerateRFC3454
29    {
30        internal const string FILE_RFC3454  = "rfc3454.txt";
31        internal const string FILE_OUTPUT   = "RFC3454.cs";
32               
33        public static void Generate()
34        {
35            if (!File.Exists(FILE_RFC3454))
36            {
37                Console.WriteLine("Unable to find rfc3454.txt.");
38                Console.WriteLine("Please download this file from:");
39                Console.WriteLine("http://www.ietf.org/rfc/rfc3454.txt");
40               
41                return;               
42            }
43
44            Console.WriteLine("Generating " + FILE_OUTPUT + " file...");
45                       
46           
47            StreamReader r = new StreamReader(FILE_RFC3454);           
48            StreamWriter w = new StreamWriter(FILE_OUTPUT);
49
50            w.WriteLine("// Do not edit !!!");
51            w.WriteLine("// this file is generated automatically");
52            w.WriteLine();
53            w.WriteLine("using System;");
54            w.WriteLine();
55            w.WriteLine("namespace Gnu.Inet.Encoding{");
56            w.WriteLine();
57            w.WriteLine("public class RFC3454");
58
59            w.WriteLine("{");
60
61            int n = 0;
62
63            string t = null;
64            System.Text.StringBuilder o1 = null;
65            System.Text.StringBuilder o2 = null;
66
67            while (true)
68            {
69                string line = r.ReadLine();
70                string l = line;
71               
72                if (null == l)
73                {
74                    break;
75                }
76                l = l.Trim();
77
78                if (l.Equals(""))
79                {
80                    // Ignore empty line
81                }
82                else if (-1 != l.IndexOf("\u000c"))
83                {
84                    // Ignore FF
85                }
86                else if (0 == l.IndexOf("RFC"))
87                {
88                    // Ignore page header
89                }
90                else if (0 == l.IndexOf("Hoffman & Blanchet"))
91                {
92                    // Ignore page footer
93                }
94                else if (-1 != l.IndexOf("----- Start Table "))
95                {
96                    // Start of a table
97                    t = l.Substring(l.IndexOf("Table") + 6, (l.LastIndexOf("-----") - 1) - (l.IndexOf("Table") + 6));
98                    o1 = new System.Text.StringBuilder();
99                    o2 = new System.Text.StringBuilder();
100                }
101                else if (-1 != l.IndexOf("----- End Table "))
102                {
103                    // End of a table
104                    if ("A.1".Equals(t))
105                    {
106                        w.WriteLine("  public static char[][] A1 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
107                    }
108                    else if ("B.1".Equals(t))
109                    {
110                        w.WriteLine("  public static char[] B1 = new char[] {\r\n" + o1.ToString() + "  };\r\n");
111                    }
112                    else if ("B.2".Equals(t))
113                    {
114                        w.WriteLine("  public static char[] B2search = new char[] {\r\n" + o1.ToString() + "  };\r\n");
115                        w.WriteLine("  public static string[] B2replace = new string[] {\r\n" + o2.ToString() + "  };\r\n");
116                    }
117                    else if ("B.3".Equals(t))
118                    {
119                        w.WriteLine("  public static char[] B3search = new char[] {\r\n" + o1.ToString() + "  };\r\n");
120                        w.WriteLine("  public static string[] B3replace = new string[] {\r\n" + o2.ToString() + "  };\r\n");
121                    }
122                    else if ("C.1.1".Equals(t))
123                    {
124                        w.WriteLine("  public static char[] C11 = new char[] {\r\n" + o1.ToString() + "  };\r\n");
125                    }
126                    else if ("C.1.2".Equals(t))
127                    {
128                        w.WriteLine("  public static char[] C12 = new char[] {\r\n" + o1.ToString() + "  };\r\n");
129                    }
130                    else if ("C.2.1".Equals(t))
131                    {
132                        w.WriteLine("  public static char[][] C21 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
133                    }
134                    else if ("C.2.2".Equals(t))
135                    {
136                        w.WriteLine("  public static char[][] C22 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
137                    }
138                    else if ("C.3".Equals(t))
139                    {
140                        w.WriteLine("  public static char[][] C3 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
141                    }
142                    else if ("C.4".Equals(t))
143                    {
144                        w.WriteLine("  public static char[][] C4 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
145                    }
146                    else if ("C.5".Equals(t))
147                    {
148                        w.WriteLine("  public static char[][] C5 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
149                    }
150                    else if ("C.6".Equals(t))
151                    {
152                        w.WriteLine("  public static char[][] C6 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
153                    }
154                    else if ("C.7".Equals(t))
155                    {
156                        w.WriteLine("  public static char[][] C7 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
157                    }
158                    else if ("C.8".Equals(t))
159                    {
160                        w.WriteLine("  public static char[][] C8 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
161                    }
162                    else if ("D.1".Equals(t))
163                    {
164                        w.WriteLine("  public static char[][] D1 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
165                    }
166                    else if ("D.2".Equals(t))
167                    {
168                        w.WriteLine("  public static char[][] D2 = new char[][] {\r\n" + o1.ToString() + "  };\r\n");
169                    }
170                    t = null;
171                }
172                else if (null != t)
173                {
174                    // Filter comments
175                    if (-1 != l.LastIndexOf(";"))
176                    {
177                        string c = l.Substring(l.LastIndexOf(";")).Trim();
178                        try
179                        {                           
180                           Convert.ToInt32(c, 16);
181                        }
182                        catch
183                        {
184                            l = l.Substring(0, (l.LastIndexOf(";")) - (0));
185                        }
186                    }
187
188                    if ("A.1".Equals(t))
189                    {
190                        if (4 == l.Length)
191                        {
192                            o1.Append("    new char[] { '\\u");
193                            o1.Append(l);
194                            o1.Append("' },");
195                            o1.Append("\t/* " + line.Trim() + " */\r\n");
196                        }
197                        else if (5 == l.Length)
198                        {
199                            //Char
200                            o1.Append("\t/* Skip characters outside the range of .NET unicode " + line.Trim() + " */\n");
201                            //Console.WriteLine("Skip: " + l);                           
202                            // Skip characters outside the range of Java unicode
203                        }
204                        else if (9 == l.Length)
205                        {
206                            o1.Append("    new char[] { '\\u");
207                            o1.Append(l.Substring(0, (4) - (0)));
208                            o1.Append("', '\\u");
209                            o1.Append(l.Substring(5, (9) - (5)));
210                            o1.Append("' },");
211                            o1.Append("\t/* " + line.Trim() + " */\r\n");
212                        }
213                        else if (11 == l.Length)
214                        {
215                            o1.Append("\t/* Skip characters outside the range of .NET unicode " + line.Trim() + " */\n");
216                            //o1.AppendLine("// " + l);
217                            // Console.WriteLine("Skip: " + l);
218                            // Skip characters outside the range of Java unicode
219                        }
220                        else
221                        {
222                            Console.WriteLine("Unknown format of A.1 line: " + l);
223                        }
224                    }
225                    else if ("B.1".Equals(t))
226                    {
227                        Tokenizer tok = new Tokenizer(l, " ;");
228                        o1.Append("    '\\u" + tok.NextToken() + "',");
229                        o1.Append("\t/* " + line.Trim() + " */\r\n");
230                    }
231                    else if ("B.2".Equals(t) || "B.3".Equals(t))
232                    {
233                        Tokenizer tok = new Tokenizer(l, "; ");
234                        string c = tok.NextToken();
235                        if (c.Length == 4)
236                        {
237                            o1.Append("    '\\u" + c + "',");
238                            o1.Append("\t/*" + line.Trim() + "*/\r\n");
239                            if (tok.HasMoreTokens())
240                            {
241                                o2.Append("    \"");
242                                while (tok.HasMoreTokens())
243                                {
244                                    o2.Append("\\u" + tok.NextToken());
245                                }
246                                o2.Append("\",");
247                                o2.Append("\t/*" + line.Trim() + "*/\r\n");
248                            }
249                            else
250                            {
251                                o2.Append("    null,");
252                                o2.Append("\t/*" + line.Trim() + "*/\r\n");
253                            }
254                        }
255                    }
256                    else if ("C.1.1".Equals(t))
257                    {
258                        o1.Append("    '\\u" + l + "',");
259                        o1.Append("\t/* " + line.Trim() + " */\r\n");
260                    }
261                    else if ("C.1.2".Equals(t))
262                    {
263                        o1.Append("    '\\u" + l + "',");
264                        o1.Append("\t/* " + line.Trim() + " */\r\n");
265                    }
266                    else if ("C.2.1".Equals(t) || "C.2.2".Equals(t) || "C.3".Equals(t) || "C.4".Equals(t) || "C.5".Equals(t) || "C.6".Equals(t) || "C.7".Equals(t) || "C.8".Equals(t) || "D.1".Equals(t) || "D.2".Equals(t))
267                    {
268                        if (4 == l.Length)
269                        {
270                            o1.Append("    new char[] { '\\u" + l + "' },");
271                            o1.Append("\t/* " + line.Trim() + " */\r\n");
272                        }
273                        else if (9 == l.Length)
274                        {
275                            o1.Append("    new char[] { '\\u");
276                            o1.Append(l.Substring(0, (4) - (0)));
277                            o1.Append("', '\\u");
278                            o1.Append(l.Substring(5, (9) - (5)));
279                            o1.Append("' },");
280                            o1.Append("\t/* " + line.Trim() + " */\r\n");
281                        }
282                    }
283                }
284
285                n++;
286            }
287
288            w.WriteLine("}");
289            w.WriteLine("}");
290            w.Close();           
291        }
292    }
293}
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us