Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / CreateVisualBasicManifestResourceName.cs
1 //
2 // CreateVisualBasicManifestResourceName.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.Tasks {
36         public class CreateVisualBasicManifestResourceName : CreateManifestResourceName {
37
38                 public CreateVisualBasicManifestResourceName ()
39                 {
40                 }
41
42                 protected override string CreateManifestName (string fileName,
43                                                               string linkFileName,
44                                                               string rootNamespace,
45                                                               string dependentUponFileName,
46                                                               Stream binaryStream)
47                 {
48                         string filename_to_use = String.IsNullOrEmpty (linkFileName) ? fileName : linkFileName;
49                         if (String.IsNullOrEmpty (dependentUponFileName) || binaryStream == null)
50                                 return GetResourceIdFromFileName
51                                         (Path.GetFileName (filename_to_use), rootNamespace);
52
53                         string ns = null;
54                         string classname = null;
55
56                         using (StreamReader rdr = new StreamReader (binaryStream)) {
57                                 while (true) {
58                                         string tok = GetNextToken (rdr);
59                                         if (tok == null)
60                                                 break;
61
62                                         if (String.Compare (tok, "namespace", true) == 0) {
63                                                 string t = GetNextToken (rdr);
64                                                 /* 'namespace' can be a attribute param also, */
65                                                 if (t == ":" && GetNextToken (rdr) == "=")
66                                                         continue;
67                                                 ns = t;
68                                         }
69
70                                         if (String.Compare (tok, "class", true) == 0) {
71                                                 string t = GetNextToken (rdr);
72                                                 /* 'class' can be a attribute param also, */
73                                                 if (t == ":" && GetNextToken (rdr) == "=")
74                                                         continue;
75                                                 classname = t;
76                                                 break;
77                                         }
78                                 }
79
80                                 if (classname == null)
81                                         return GetResourceIdFromFileName (filename_to_use, rootNamespace);
82
83                                 string culture, extn, only_filename;
84                                 if (AssignCulture.TrySplitResourceName (filename_to_use, out only_filename, out culture, out extn))
85                                         extn = "." + culture;
86                                 else
87                                         extn = String.Empty;
88
89                                 string rname;
90                                 if (ns == null)
91                                         rname = classname + extn;
92                                 else
93                                         rname = ns + '.' + classname + extn;
94
95                                 if (String.IsNullOrEmpty (rootNamespace))
96                                         return rname;
97                                 else
98                                         return rootNamespace + "." + rname;
99                         }
100                 }
101
102                 protected override bool IsSourceFile (string fileName)
103                 {
104                         return Path.GetExtension (fileName).ToLower () == ".vb";
105                 }
106
107                 /* Special parser for VB.NET files
108                  * Assumes that the file is compilable
109                  * skips comments,
110                  * skips strings "foo"
111                  */
112                 string GetNextToken (StreamReader sr)
113                 {
114                         StringBuilder sb = new StringBuilder ();
115
116                         while (true) {
117                                 int c = sr.Peek ();
118                                 if (c == -1)
119                                         return null;
120
121                                 if (c == '\r' || c == '\n') {
122                                         sr.ReadLine ();
123                                         if (sb.Length > 0)
124                                                 break;
125
126                                         continue;
127                                 }
128
129                                 if (c == '\'') {
130                                         /* comment */
131                                         sr.ReadLine ();
132                                         if (sb.Length > 0)
133                                                 return sb.ToString ();
134
135                                         continue;
136                                 }
137
138                                 if (c == '"') {
139                                         /* String */
140                                         sr.Read ();
141                                         while (true) {
142                                                 int n = sr.Peek ();
143                                                 if (n == '\r' || n == '\n' || n == -1)
144                                                         throw new Exception ("String literal not closed");
145
146                                                 if (n == '"') {
147                                                         if (sb.Length > 0) {
148                                                                 sr.Read ();
149                                                                 return sb.ToString ();
150                                                         }
151
152                                                         break;
153                                                 }
154                                                 sr.Read ();
155                                         }
156                                 } else {
157                                         if (Char.IsLetterOrDigit ((char) c) || c == '_' || c == '.') {
158                                                 sb.Append ((char) c);
159                                         } else {
160                                                 if (sb.Length > 0)
161                                                         break;
162
163                                                 if (c != ' ' && c != '\t') {
164                                                         sr.Read ();
165                                                         return ((char) c).ToString ();
166                                                 }
167                                         }
168                                 }
169
170                                 sr.Read ();
171                         }
172
173                         return sb.ToString ();
174                 }
175
176         }
177 }
178
179 #endif