~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// AddinPropertyCollection.cs
 
3
//  
 
4
// Author:
 
5
//       Lluis Sanchez Gual <lluis@novell.com>
 
6
// 
 
7
// Copyright (c) 2011 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
 
10
// of this software and associated documentation files (the "Software"), to deal
 
11
// in the Software without restriction, including without limitation the rights
 
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
13
// copies of the Software, and to permit persons to whom the Software is
 
14
// furnished to do so, subject to the following conditions:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
25
// THE SOFTWARE.
 
26
using System;
 
27
using System.Linq;
 
28
using System.Collections.Generic;
 
29
 
 
30
namespace Mono.Addins.Description
 
31
{
 
32
        /// <summary>
 
33
        /// A collection of add-in properties
 
34
        /// </summary>
 
35
        public interface AddinPropertyCollection: IEnumerable<AddinProperty>
 
36
        {
 
37
                /// <summary>
 
38
                /// Gets the value of a property
 
39
                /// </summary>
 
40
                /// <returns>
 
41
                /// The property value.
 
42
                /// </returns>
 
43
                /// <param name='name'>
 
44
                /// Name of the property.
 
45
                /// </param>
 
46
                /// <remarks>
 
47
                /// If the property is localized, it will return the value for the current language if exists, or the
 
48
                /// default value if it doesn't.
 
49
                /// </remarks>
 
50
                string GetPropertyValue (string name);
 
51
                
 
52
                /// <summary>
 
53
                /// Gets the value of a property
 
54
                /// </summary>
 
55
                /// <returns>
 
56
                /// The property value.
 
57
                /// </returns>
 
58
                /// <param name='name'>
 
59
                /// Name of the property.
 
60
                /// </param>
 
61
                /// <param name='locale'>
 
62
                /// Locale for which the value must be returned.
 
63
                /// </param>
 
64
                string GetPropertyValue (string name, string locale);
 
65
                
 
66
                /// <summary>
 
67
                /// Sets the value of a property
 
68
                /// </summary>
 
69
                /// <param name='name'>
 
70
                /// Name of the property
 
71
                /// </param>
 
72
                /// <param name='value'>
 
73
                /// New value.
 
74
                /// </param>
 
75
                void SetPropertyValue (string name, string value);
 
76
                
 
77
                /// <summary>
 
78
                /// Sets the value of a property for a specific locale
 
79
                /// </summary>
 
80
                /// <param name='name'>
 
81
                /// Name of the property.
 
82
                /// </param>
 
83
                /// <param name='value'>
 
84
                /// New value.
 
85
                /// </param>
 
86
                /// <param name='locale'>
 
87
                /// Locale of the property to be set.
 
88
                /// </param>
 
89
                void SetPropertyValue (string name, string value, string locale);
 
90
                
 
91
                /// <summary>
 
92
                /// Removes a property.
 
93
                /// </summary>
 
94
                /// <param name='name'>
 
95
                /// Name of the property.
 
96
                /// </param>
 
97
                /// <remarks>
 
98
                /// This method only removes properties which have no locale set.
 
99
                /// </remarks>
 
100
                void RemoveProperty (string name);
 
101
                
 
102
                /// <summary>
 
103
                /// Removes a property with a specified locale
 
104
                /// </summary>
 
105
                /// <param name='name'>
 
106
                /// Name of the property
 
107
                /// </param>
 
108
                /// <param name='locale'>
 
109
                /// Locale of the property
 
110
                /// </param>
 
111
                void RemoveProperty (string name, string locale);
 
112
        }
 
113
        
 
114
        class AddinPropertyCollectionImpl: List<AddinProperty>, AddinPropertyCollection
 
115
        {
 
116
                AddinDescription desc;
 
117
 
 
118
                public AddinPropertyCollectionImpl ()
 
119
                {
 
120
                }
 
121
                
 
122
                public AddinPropertyCollectionImpl (AddinDescription desc)
 
123
                {
 
124
                        this.desc = desc;
 
125
                }
 
126
                
 
127
                public AddinPropertyCollectionImpl (AddinPropertyCollection col)
 
128
                {
 
129
                        AddRange (col);
 
130
                }
 
131
                
 
132
                public string GetPropertyValue (string name)
 
133
                {
 
134
                        return GetPropertyValue (name, System.Threading.Thread.CurrentThread.CurrentCulture.ToString ());
 
135
                }
 
136
                
 
137
                public string GetPropertyValue (string name, string locale)
 
138
                {
 
139
                        locale = NormalizeLocale (locale);
 
140
                        string lang = GetLocaleLang (locale);
 
141
                        AddinProperty sameLangDifCountry = null;
 
142
                        AddinProperty sameLang = null;
 
143
                        AddinProperty defaultLoc = null;
 
144
                        
 
145
                        foreach (var p in this) {
 
146
                                if (p.Name == name) {
 
147
                                        if (p.Locale == locale)
 
148
                                                return ParseString (p.Value);
 
149
                                        string plang = GetLocaleLang (p.Locale);
 
150
                                        if (plang == p.Locale && plang == lang) // No country specified
 
151
                                                sameLang = p;
 
152
                                        else if (plang == lang)
 
153
                                                sameLangDifCountry = p;
 
154
                                        else if (p.Locale == null)
 
155
                                                defaultLoc = p;
 
156
                                }
 
157
                        }
 
158
                        if (sameLang != null)
 
159
                                return ParseString (sameLang.Value);
 
160
                        else if (sameLangDifCountry != null)
 
161
                                return ParseString (sameLangDifCountry.Value);
 
162
                        else if (defaultLoc != null)
 
163
                                return ParseString (defaultLoc.Value);
 
164
                        else
 
165
                                return string.Empty;
 
166
                }
 
167
 
 
168
                string ParseString (string s)
 
169
                {
 
170
                        if (desc != null)
 
171
                                return desc.ParseString (s);
 
172
                        else
 
173
                                return s;
 
174
                }
 
175
                
 
176
                string NormalizeLocale (string loc)
 
177
                {
 
178
                        if (string.IsNullOrEmpty (loc))
 
179
                                return null;
 
180
                        return loc.Replace ('_','-');
 
181
                }
 
182
                
 
183
                string GetLocaleLang (string loc)
 
184
                {
 
185
                        if (loc == null)
 
186
                                return null;
 
187
                        int i = loc.IndexOf ('-');
 
188
                        if (i != -1)
 
189
                                return loc.Substring (0, i);
 
190
                        else
 
191
                                return loc;
 
192
                }
 
193
                
 
194
                public void SetPropertyValue (string name, string value)
 
195
                {
 
196
                        SetPropertyValue (name, value, null);
 
197
                }
 
198
                
 
199
                public void SetPropertyValue (string name, string value, string locale)
 
200
                {
 
201
                        if (string.IsNullOrEmpty (name))
 
202
                                throw new ArgumentException ("name can't be null or empty");
 
203
                        
 
204
                        if (value == null)
 
205
                                throw new ArgumentNullException ("value");
 
206
                        
 
207
                        locale = NormalizeLocale (locale);
 
208
                        
 
209
                        foreach (var p in this) {
 
210
                                if (p.Name == name && p.Locale == locale) {
 
211
                                        p.Value = value;
 
212
                                        return;
 
213
                                }
 
214
                        }
 
215
                        AddinProperty prop = new AddinProperty ();
 
216
                        prop.Name = name;
 
217
                        prop.Value = value;
 
218
                        prop.Locale = locale;
 
219
                        Add (prop);
 
220
                }
 
221
                
 
222
                public void RemoveProperty (string name)
 
223
                {
 
224
                        RemoveProperty (name, null);
 
225
                }
 
226
                
 
227
                public void RemoveProperty (string name, string locale)
 
228
                {
 
229
                        locale = NormalizeLocale (locale);
 
230
                        
 
231
                        foreach (var p in this) {
 
232
                                if (p.Name == name && p.Locale == locale) {
 
233
                                        Remove (p);
 
234
                                        return;
 
235
                                }
 
236
                        }
 
237
                }
 
238
                
 
239
                internal bool HasProperty (string name)
 
240
                {
 
241
                        return this.Any (p => p.Name == name);
 
242
                }
 
243
                
 
244
                internal string ExtractCoreProperty (string name, bool removeProperty)
 
245
                {
 
246
                        foreach (var p in this) {
 
247
                                if (p.Name == name && p.Locale == null) {
 
248
                                        if (removeProperty)
 
249
                                                Remove (p);
 
250
                                        return p.Value;
 
251
                                }
 
252
                        }
 
253
                        return null;
 
254
                }
 
255
        }
 
256
}
 
257