* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Microsoft.Build.Tasks / Mono.XBuild.Tasks.GenerateResourceInternal / PoResourceReader.cs
1 //
2 // PoResourceReader.cs: Reader from monoresgen.
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Paolo Molaro (lupus@ximian.com)
7 //   Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 //
9 // (C) 2005 Marek Sieradzki
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 #if NET_2_0
31
32 using System;
33 using System.Collections;
34 using System.IO;
35 using System.Resources;
36 using System.Text;
37
38 namespace Mono.XBuild.Tasks.GenerateResourceInternal {
39         internal class PoResourceReader : IResourceReader {
40                 Hashtable data;
41                 Stream s;
42                 int line_num;
43
44                 public PoResourceReader (Stream stream)
45                 {
46                         data = new Hashtable ();
47                         s = stream;
48                         Load ();
49                 }
50
51                 public virtual void Close ()
52                 {
53                         s.Close ();
54                 }
55
56                 public IDictionaryEnumerator GetEnumerator()
57                 {
58                         return data.GetEnumerator ();
59                 }
60
61                 string GetValue (string line)
62                 {
63                         int begin = line.IndexOf ('"');
64                         if (begin == -1)
65                                 throw new FormatException (String.Format ("No begin quote at line {0}: {1}", line_num, line));
66
67                         int end = line.LastIndexOf ('"');
68                         if (end == -1)
69                                 throw new FormatException (String.Format ("No closing quote at line {0}: {1}", line_num, line));
70
71                         return line.Substring (begin + 1, end - begin - 1);
72                 }
73
74                 void Load ()
75                 {
76                         StreamReader reader = new StreamReader (s);
77                         string line;
78                         string msgid = null;
79                         string msgstr = null;
80                         bool ignoreNext = false;
81
82                         while ((line = reader.ReadLine ()) != null) {
83                                 line_num++;
84                                 line = line.Trim ();
85                                 if (line.Length == 0)
86                                         continue;
87                                         
88                                 if (line [0] == '#') {
89                                         if (line.Length == 1 || line [1] != ',')
90                                                 continue;
91
92                                         if (line.IndexOf ("fuzzy") != -1) {
93                                                 ignoreNext = true;
94                                                 if (msgid != null) {
95                                                         if (msgstr == null)
96                                                                 throw new FormatException ("Error. Line: " + line_num);
97                                                         data.Add (msgid, msgstr);
98                                                         msgid = null;
99                                                         msgstr = null;
100                                                 }
101                                         }
102                                         continue;
103                                 }
104                                 
105                                 if (line.StartsWith ("msgid ")) {
106                                         if (msgid == null && msgstr != null)
107                                                 throw new FormatException ("Found 2 consecutive msgid. Line: " + line_num);
108
109                                         if (msgstr != null) {
110                                                 if (!ignoreNext)
111                                                         data.Add (msgid, msgstr);
112
113                                                 ignoreNext = false;
114                                                 msgid = null;
115                                                 msgstr = null;
116                                         }
117
118                                         msgid = GetValue (line);
119                                         continue;
120                                 }
121
122                                 if (line.StartsWith ("msgstr ")) {
123                                         if (msgid == null)
124                                                 throw new FormatException ("msgstr with no msgid. Line: " + line_num);
125
126                                         msgstr = GetValue (line);
127                                         continue;
128                                 }
129
130                                 if (line [0] == '"') {
131                                         if (msgid == null || msgstr == null)
132                                                 throw new FormatException ("Invalid format. Line: " + line_num);
133
134                                         msgstr += GetValue (line);
135                                         continue;
136                                 }
137
138                                 throw new FormatException ("Unexpected data. Line: " + line_num);
139                         }
140
141                         if (msgid != null) {
142                                 if (msgstr == null)
143                                         throw new FormatException ("Expecting msgstr. Line: " + line_num);
144
145                                 if (!ignoreNext)
146                                         data.Add (msgid, msgstr);
147                         }
148                 }
149
150                 IEnumerator IEnumerable.GetEnumerator ()
151                 {
152                         return GetEnumerator();
153                 }
154
155                 void IDisposable.Dispose ()
156                 {
157                         if (data != null)
158                                 data = null;
159
160                         if (s != null) {
161                                 s.Close ();
162                                 s = null;
163                         }
164                 }
165         }
166 }
167
168 #endif