merge r67228-r67235, r67237, r67251 and r67256-67259 to trunk (they are
[mono.git] / mcs / class / Microsoft.Build.Tasks / Mono.XBuild.Tasks.GenerateResourceInternal / PoResourceWriter.cs
1 //
2 // PoResourceWriter.cs: Writer 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.IO;
34 using System.Resources;
35 using System.Text;
36
37 namespace Mono.XBuild.Tasks.GenerateResourceInternal {
38         internal class PoResourceWriter : IResourceWriter
39         {
40                 TextWriter s;
41                 bool headerWritten;
42                 
43                 public PoResourceWriter (Stream stream)
44                 {
45                         s = new StreamWriter (stream);
46                 }
47                 
48                 public void AddResource (string name, byte [] value)
49                 {
50                         throw new InvalidOperationException ("Binary data not valid in a po resource file");
51                 }
52                 
53                 public void AddResource (string name, object value)
54                 {
55                         if (value is string) {
56                                 AddResource (name, (string) value);
57                                 return;
58                         }
59                         throw new InvalidOperationException ("Objects not valid in a po resource file");
60                 }
61
62                 StringBuilder ebuilder = new StringBuilder ();
63                 
64                 public string Escape (string ns)
65                 {
66                         ebuilder.Length = 0;
67
68                         foreach (char c in ns){
69                                 switch (c){
70                                 case '"':
71                                 case '\\':
72                                         ebuilder.Append ('\\');
73                                         ebuilder.Append (c);
74                                         break;
75                                 case '\a':
76                                         ebuilder.Append ("\\a");
77                                         break;
78                                 case '\n':
79                                         ebuilder.Append ("\\n");
80                                         break;
81                                 case '\r':
82                                         ebuilder.Append ("\\r");
83                                         break;
84                                 default:
85                                         ebuilder.Append (c);
86                                         break;
87                                 }
88                         }
89                         return ebuilder.ToString ();
90                 }
91                 
92                 public void AddResource (string name, string value)
93                 {
94                         if (!headerWritten) {
95                                 headerWritten = true;
96                                 WriteHeader ();
97                         }
98                         
99                         s.WriteLine ("msgid \"{0}\"", Escape (name));
100                         s.WriteLine ("msgstr \"{0}\"", Escape (value));
101                         s.WriteLine (String.Empty);
102                 }
103                 
104                 void WriteHeader ()
105                 {
106                         s.WriteLine ("msgid \"\"");
107                         s.WriteLine ("msgstr \"\"");
108                         s.WriteLine ("\"MIME-Version: 1.0\\n\"");
109                         s.WriteLine ("\"Content-Type: text/plain; charset=UTF-8\\n\"");
110                         s.WriteLine ("\"Content-Transfer-Encoding: 8bit\\n\"");
111                         s.WriteLine ("\"X-Generator: Mono resgen 0.1\\n\"");
112                         s.WriteLine ("#\"Project-Id-Version: FILLME\\n\"");
113                         s.WriteLine ("#\"POT-Creation-Date: yyyy-MM-dd HH:MM+zzzz\\n\"");
114                         s.WriteLine ("#\"PO-Revision-Date: yyyy-MM-dd HH:MM+zzzz\\n\"");
115                         s.WriteLine ("#\"Last-Translator: FILLME\\n\"");
116                         s.WriteLine ("#\"Language-Team: FILLME\\n\"");
117                         s.WriteLine ("#\"Report-Msgid-Bugs-To: \\n\"");
118                         s.WriteLine ();
119                 }
120
121                 public void Close ()
122                 {
123                         s.Close ();
124                 }
125                 
126                 public void Dispose ()
127                 {
128                 }
129                 
130                 public void Generate ()
131                 {
132                 }
133         }
134 }
135
136 #endif