diff options
author | Lluis Sanchez Gual <lluis@novell.com> | 2011-01-31 19:18:53 +0300 |
---|---|---|
committer | Lluis Sanchez Gual <lluis@novell.com> | 2011-01-31 19:18:53 +0300 |
commit | 52e3f5e6826e5ba2ba08fe14671ab9cb98b48175 (patch) | |
tree | e673f86d3b26f824168d6a78c21758407d60e991 /Mono.Addins | |
parent | bbc11bfdd2c27692af6df5de13eb96d31b6ee985 (diff) |
Improved handling of add-in properties.
Allow specifying properties using custom attributes.
Use the default locale when getting a property value, when none is
specified.
Diffstat (limited to 'Mono.Addins')
-rw-r--r-- | Mono.Addins/Makefile.am | 2 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins.Database/AddinScanner.cs | 11 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins.Description/AddinDescription.cs | 45 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins.Description/AddinProperty.cs | 17 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs | 53 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins.csproj | 5 | ||||
-rw-r--r-- | Mono.Addins/Mono.Addins/AddinPropertyAttribute.cs | 51 |
7 files changed, 160 insertions, 24 deletions
diff --git a/Mono.Addins/Makefile.am b/Mono.Addins/Makefile.am index d6e9639..096a2be 100644 --- a/Mono.Addins/Makefile.am +++ b/Mono.Addins/Makefile.am @@ -83,6 +83,7 @@ FILES = \ Mono.Addins/AddinLocalizerGettextAttribute.cs \ Mono.Addins/AddinManager.cs \ Mono.Addins/AddinModuleAttribute.cs \ + Mono.Addins/AddinPropertyAttribute.cs \ Mono.Addins/AddinRegistry.cs \ Mono.Addins/AddinRootAttribute.cs \ Mono.Addins/ConditionType.cs \ @@ -121,6 +122,7 @@ EXTRAS = REFERENCES = \ -r:System \ + -r:System.Core \ -r:System.Xml DLL_REFERENCES = diff --git a/Mono.Addins/Mono.Addins.Database/AddinScanner.cs b/Mono.Addins/Mono.Addins.Database/AddinScanner.cs index 5529802..b4b3365 100644 --- a/Mono.Addins/Mono.Addins.Database/AddinScanner.cs +++ b/Mono.Addins/Mono.Addins.Database/AddinScanner.cs @@ -809,9 +809,16 @@ namespace Mono.Addins.Database module.Dependencies.Add (adep); } - // Get extension points - if (isMainModule) { + + // Get properties + + object[] props = reflector.GetCustomAttributes (asm, typeof(AddinPropertyAttribute), false); + foreach (AddinPropertyAttribute prop in props) + config.Properties.SetPropertyValue (prop.Name, prop.Locale, prop.Value); + + // Get extension points + object[] extPoints = reflector.GetCustomAttributes (asm, typeof(ExtensionPointAttribute), false); foreach (ExtensionPointAttribute ext in extPoints) { ExtensionPoint ep = config.AddExtensionPoint (ext.Path); diff --git a/Mono.Addins/Mono.Addins.Description/AddinDescription.cs b/Mono.Addins/Mono.Addins.Description/AddinDescription.cs index cd798ec..ace36ef 100644 --- a/Mono.Addins/Mono.Addins.Description/AddinDescription.cs +++ b/Mono.Addins/Mono.Addins.Description/AddinDescription.cs @@ -98,6 +98,7 @@ namespace Mono.Addins.Description typeMap.RegisterType (typeof(AssemblyDependency), "AssemblyDependency"); typeMap.RegisterType (typeof(NodeTypeAttribute), "NodeTypeAttribute"); typeMap.RegisterType (typeof(AddinFileInfo), "FileInfo"); + typeMap.RegisterType (typeof(AddinProperty), "Property"); } internal AddinDatabase OwnerDatabase { @@ -159,6 +160,9 @@ namespace Mono.Addins.Description /// </value> public string Name { get { + string val = Properties.GetPropertyValue ("Name"); + if (val.Length > 0) + return val; if (name != null && name.Length > 0) return name; if (HasUserId) @@ -200,7 +204,12 @@ namespace Mono.Addins.Description /// The author. /// </value> public string Author { - get { return author != null ? author : string.Empty; } + get { + string val = Properties.GetPropertyValue ("Author"); + if (val.Length > 0) + return val; + return author ?? string.Empty; + } set { author = value; } } @@ -211,7 +220,12 @@ namespace Mono.Addins.Description /// The URL. /// </value> public string Url { - get { return url != null ? url : string.Empty; } + get { + string val = Properties.GetPropertyValue ("Url"); + if (val.Length > 0) + return val; + return url ?? string.Empty; + } set { url = value; } } @@ -222,7 +236,12 @@ namespace Mono.Addins.Description /// The copyright. /// </value> public string Copyright { - get { return copyright != null ? copyright : string.Empty; } + get { + string val = Properties.GetPropertyValue ("Copyright"); + if (val.Length > 0) + return val; + return copyright ?? string.Empty; + } set { copyright = value; } } @@ -233,7 +252,12 @@ namespace Mono.Addins.Description /// The description. /// </value> public string Description { - get { return description != null ? description : string.Empty; } + get { + string val = Properties.GetPropertyValue ("Description"); + if (val.Length > 0) + return val; + return description ?? string.Empty; + } set { description = value; } } @@ -888,7 +912,7 @@ namespace Mono.Addins.Description XmlElement prop = node as XmlElement; if (prop == null) continue; - config.Properties.SetPropertyValue (prop.LocalName, prop.InnerText, prop.GetAttribute ("locale")); + config.Properties.SetPropertyValue (prop.LocalName, prop.GetAttribute ("locale"), prop.InnerText); } } @@ -982,6 +1006,16 @@ namespace Mono.Addins.Description errors.Add ("The attribute 'type' in the Location element is required."); } + // Ensure that there are no duplicated properties + + if (properties != null) { + HashSet<string> props = new HashSet<string> (); + foreach (var prop in properties) { + if (!props.Add (prop.Name + " " + prop.Locale)) + errors.Add (string.Format ("Property {0} specified more than once", prop.Name + (prop.Locale != null ? " (" + prop.Locale + ")" : ""))); + } + } + return errors; } @@ -1030,6 +1064,7 @@ namespace Mono.Addins.Description AddinDescription tmp = desc1; desc1 = desc2; desc2 = tmp; } + ((AddinPropertyCollectionImpl)desc1.Properties).AddRange (desc2.Properties); desc1.ExtensionPoints.AddRange (desc2.ExtensionPoints); desc1.ExtensionNodeSets.AddRange (desc2.ExtensionNodeSets); desc1.ConditionTypes.AddRange (desc2.ConditionTypes); diff --git a/Mono.Addins/Mono.Addins.Description/AddinProperty.cs b/Mono.Addins/Mono.Addins.Description/AddinProperty.cs index 81ee0fa..088efc5 100644 --- a/Mono.Addins/Mono.Addins.Description/AddinProperty.cs +++ b/Mono.Addins/Mono.Addins.Description/AddinProperty.cs @@ -25,10 +25,11 @@ // THE SOFTWARE. using System; using System.Xml.Serialization; +using Mono.Addins.Serialization; namespace Mono.Addins.Description { - public class AddinProperty + public class AddinProperty: IBinaryXmlElement { [XmlAttribute ("name")] public string Name { get; set; } @@ -38,6 +39,20 @@ namespace Mono.Addins.Description [XmlText] public string Value { get; set; } + + void IBinaryXmlElement.Read (BinaryXmlReader reader) + { + Name = reader.ReadStringValue ("name"); + Locale = reader.ReadStringValue ("locale"); + Value = reader.ReadStringValue ("value"); + } + + void IBinaryXmlElement.Write (BinaryXmlWriter writer) + { + writer.WriteValue ("name", Name); + writer.WriteValue ("locale", Locale); + writer.WriteValue ("value", Value); + } } } diff --git a/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs b/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs index 63f2339..82af8b2 100644 --- a/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs +++ b/Mono.Addins/Mono.Addins.Description/AddinPropertyCollection.cs @@ -32,9 +32,7 @@ namespace Mono.Addins.Description { string GetPropertyValue (string name); string GetPropertyValue (string name, string locale); - void SetPropertyValue (string name, string value); - void SetPropertyValue (string name, string value, string locale); - void RemoveProperty (string name); + void SetPropertyValue (string name, string locale, string value); void RemoveProperty (string name, string locale); } @@ -42,35 +40,61 @@ namespace Mono.Addins.Description { public string GetPropertyValue (string name) { - return GetPropertyValue (name, null); + return GetPropertyValue (name, System.Threading.Thread.CurrentThread.CurrentCulture.ToString ()); } public string GetPropertyValue (string name, string locale) { + locale = NormalizeLocale (locale); + string lang = GetLocaleLang (locale); AddinProperty best = null; + AddinProperty defaultLoc = null; foreach (var p in this) { if (p.Name == name) { - if (best == null) - best = p; - else if (string.IsNullOrEmpty (p.Locale)) - best = p; if (p.Locale == locale) return p.Value; + else if (GetLocaleLang (p.Locale) == lang) + best = p; + else if (p.Locale == null) + defaultLoc = p; } } if (best != null) return best.Value; + else if (defaultLoc != null) + return defaultLoc.Value; else return string.Empty; } - public void SetPropertyValue (string name, string value) + string NormalizeLocale (string loc) + { + if (string.IsNullOrEmpty (loc)) + return null; + return loc.Replace ('_','-'); + } + + string GetLocaleLang (string loc) { - SetPropertyValue (name, value, null); + if (loc == null) + return null; + int i = loc.IndexOf ('-'); + if (i != -1) + return loc.Substring (0, i); + else + return loc; } - public void SetPropertyValue (string name, string value, string locale) + public void SetPropertyValue (string name, string locale, string value) { + if (string.IsNullOrEmpty (name)) + throw new ArgumentException ("name can't be null or empty"); + + if (value == null) + throw new ArgumentNullException ("value"); + + locale = NormalizeLocale (locale); + foreach (var p in this) { if (p.Name == name && p.Locale == locale) { p.Value = value; @@ -84,13 +108,10 @@ namespace Mono.Addins.Description Add (prop); } - public void RemoveProperty (string name) - { - RemoveProperty (name, null); - } - public void RemoveProperty (string name, string locale) { + locale = NormalizeLocale (locale); + foreach (var p in this) { if (p.Name == name && p.Locale == locale) { Remove (p); diff --git a/Mono.Addins/Mono.Addins.csproj b/Mono.Addins/Mono.Addins.csproj index b0310c4..727d199 100644 --- a/Mono.Addins/Mono.Addins.csproj +++ b/Mono.Addins/Mono.Addins.csproj @@ -35,6 +35,7 @@ <ItemGroup> <Reference Include="System" /> <Reference Include="System.Xml" /> + <Reference Include="System.Core" /> </ItemGroup> <ItemGroup> <Compile Include="AssemblyInfo.cs" /> @@ -136,6 +137,7 @@ <Compile Include="Mono.Addins\ContentType.cs" /> <Compile Include="Mono.Addins.Description\AddinProperty.cs" /> <Compile Include="Mono.Addins.Description\AddinPropertyCollection.cs" /> + <Compile Include="Mono.Addins\AddinPropertyAttribute.cs" /> </ItemGroup> <ItemGroup> <Content Include="Mono.Addins.dll.config"> @@ -161,4 +163,7 @@ </Properties> </MonoDevelop> </ProjectExtensions> + <ItemGroup> + <Folder Include="Mono.Addins.Description\" /> + </ItemGroup> </Project>
\ No newline at end of file diff --git a/Mono.Addins/Mono.Addins/AddinPropertyAttribute.cs b/Mono.Addins/Mono.Addins/AddinPropertyAttribute.cs new file mode 100644 index 0000000..c31e865 --- /dev/null +++ b/Mono.Addins/Mono.Addins/AddinPropertyAttribute.cs @@ -0,0 +1,51 @@ +// +// AddinPropertyAttribute.cs +// +// Author: +// Lluis Sanchez Gual <lluis@novell.com> +// +// Copyright (c) 2011 Novell, Inc (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; + +namespace Mono.Addins +{ + [AttributeUsage (AttributeTargets.Assembly, AllowMultiple=true)] + public class AddinPropertyAttribute: Attribute + { + public AddinPropertyAttribute (string name, string value): this (name, null, value) + { + } + + public AddinPropertyAttribute (string name, string locale, string value) + { + Name = name; + Locale = locale; + Value = value; + } + + public string Name { get; set; } + + public string Locale { get; set; } + + public string Value { get; set; } + } +} + |