Merge pull request #1155 from steffen-kiess/json-string
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / AssignCulture.cs
1 //
2 // AssignCulture.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //   Ankit Jain (jankit@novell.com)
7 //
8 // (C) 2006 Marek Sieradzki
9 // Copyright 2008 Novell, Inc (http://www.novell.com)
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
31 using System;
32 using System.Collections.Generic;
33 using System.Globalization;
34 using System.IO;
35 using Microsoft.Build.Framework;
36 using Microsoft.Build.Utilities;
37
38 namespace Microsoft.Build.Tasks {
39         public class AssignCulture : TaskExtension {
40         
41                 ITaskItem[]     assignedFiles;
42                 ITaskItem[]     assignedFilesWithCulture;
43                 ITaskItem[]     assignedFilesWithNoCulture;
44                 ITaskItem[]     cultureNeutralAssignedFiles;
45                 ITaskItem[]     files;
46         
47                 public AssignCulture ()
48                 {
49                 }
50                 
51                 public override bool Execute ()
52                 {
53                         if (files.Length == 0)
54                                 return true;
55
56                         List<ITaskItem> all_files = new List<ITaskItem> ();
57                         List<ITaskItem> with_culture = new List<ITaskItem> ();
58                         List<ITaskItem> no_culture = new List<ITaskItem> ();
59                         List<ITaskItem> culture_neutral = new List<ITaskItem> ();
60
61                         foreach (ITaskItem item in files) {
62                                 string only_filename, culture, extn;
63
64                                 if (TrySplitResourceName (item.ItemSpec, out only_filename, out culture, out extn)) {
65                                         //valid culture found
66                                         ITaskItem new_item = new TaskItem (item);
67                                         new_item.SetMetadata ("Culture", culture);
68                                         all_files.Add (new_item);
69
70                                         new_item = new TaskItem (item);
71                                         new_item.SetMetadata ("Culture", culture);
72                                         with_culture.Add (new_item);
73
74                                         new_item = new TaskItem (item);
75                                         new_item.SetMetadata ("Culture", culture);
76                                         new_item.ItemSpec = only_filename + "." + extn;
77                                         culture_neutral.Add (new_item);
78                                 } else {
79                                         //No valid culture
80
81                                         all_files.Add (item);
82                                         no_culture.Add (item);
83                                         culture_neutral.Add (item);
84                                 }
85                         }
86
87                         assignedFiles = all_files.ToArray ();
88                         assignedFilesWithCulture = with_culture.ToArray ();
89                         assignedFilesWithNoCulture = no_culture.ToArray ();
90                         cultureNeutralAssignedFiles = culture_neutral.ToArray ();
91
92                         return true;
93                 }
94                 
95                 [Output]
96                 public ITaskItem[] AssignedFiles {
97                         get { return assignedFiles; }
98                 }
99                 
100                 [Output]
101                 public ITaskItem[] AssignedFilesWithCulture {
102                         get { return assignedFilesWithCulture; }
103                 }
104                 
105                 [Output]
106                 public ITaskItem[] AssignedFilesWithNoCulture {
107                         get { return assignedFilesWithNoCulture; }
108                 }
109                 
110                 [Output]
111                 public ITaskItem[] CultureNeutralAssignedFiles {
112                         get { return cultureNeutralAssignedFiles; }
113                 }
114                 
115                 [Required]
116                 public ITaskItem[] Files {
117                         get { return files; }
118                         set { files = value; }
119                 }
120
121                 //Given a filename like foo.it.resx, splits it into - foo, it, resx
122                 //Returns true only if a valid culture is found
123                 //Note: hand-written as this can get called lotsa times
124                 internal static bool TrySplitResourceName (string fname, out string only_filename, out string culture, out string extn)
125                 {
126                         only_filename = culture = extn = null;
127
128                         int last_dot = -1;
129                         int culture_dot = -1;
130                         int i = fname.Length - 1;
131                         while (i >= 0) {
132                                 if (fname [i] == '.') {
133                                         last_dot = i;
134                                         break;
135                                 }
136                                 i --;
137                         }
138                         if (i < 0)
139                                 return false;
140
141                         i--;
142                         while (i >= 0) {
143                                 if (fname [i] == '.') {
144                                         culture_dot = i;
145                                         break;
146                                 }
147                                 i --;
148                         }
149                         if (culture_dot < 0)
150                                 return false;
151
152                         culture = fname.Substring (culture_dot + 1, last_dot - culture_dot - 1);
153                         if (!CultureNamesTable.ContainsKey (culture)) {
154                                 culture = null;
155                                 return false;
156                         }
157
158                         only_filename = fname.Substring (0, culture_dot);
159                         extn = fname.Substring (last_dot + 1);
160                         return true;
161                 }
162
163                 static Dictionary<string, string> cultureNamesTable;
164                 static Dictionary<string, string> CultureNamesTable {
165                         get {
166                                 if (cultureNamesTable == null) {
167                                         cultureNamesTable = new Dictionary<string, string> ();
168                                         foreach (CultureInfo ci in CultureInfo.GetCultures (CultureTypes.AllCultures))
169                                                 cultureNamesTable [ci.Name] = ci.Name;
170                                 }
171
172                                 return cultureNamesTable;
173                         }
174                 }
175
176         }
177 }
178