Clover.NET coverage report - Coverage

Coverage timestamp: viernes, 12 de agosto de 2005 12:53:38 PM

File Stats: LOC: 309   Methods: 22
NCLOC: 142 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Model\XmlModel\XmlPropertyMap.cs 33,3 % 55,3 % 54,5 % 53,3 %
coverage coverage
1   #region Copyright
2   /*
3   ShowX. Maps business objects into presentation layer.
4   Copyright (C) 2005 Jesus Diaz.
5  
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10  
11   This library is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19  
20   For project details see: http://showx.sourceforge.net
21   */
22   #endregion
23  
24   using System;
25   using System.Xml;
26   using System.Collections;
27   using System.Reflection;
28   using ShowX.Model.XmlModel;
29   using log4net;
30  
31   namespace ShowX.Model.XmlModel
32   {
33   /// <summary>
34   /// Represents a property of the business object.
35   /// </summary>
36   public class XmlPropertyMap : IPropertyMap
37   {
38   private static readonly ILog log = LogManager.GetLogger(
39   typeof(XmlPropertyMap));
40  
41   #region Protected fields
42  
43   /// <summary>
44   /// Name of the property associated to this mapping.
45   /// </summary>
46   protected PropertyInfo propertyInfo;
47  
48   /// <summary>
49   /// Name tho show associated of the property. Default value is "".
50   /// </summary>
51   protected string name ="";
52  
53   /// <summary>
54   /// Heading to show over this property.
55   /// </summary>
56   protected string heading = "";
57  
58   /// <summary>
59   /// Is this property read only?. If true, it won't be editable on editing over
60   /// this class. Default value is false.
61   /// </summary>
62   protected bool readOnly = false;
63  
64   /// <summary>
65   /// Is this property show? If false, the property won't be show neither on
66   /// editing or viewing. Default value is true.
67   /// </summary>
68   protected bool show = true;
69  
70   /// <summary>
71   /// Is this property editable when we are inserting a new instance?
72   /// Default value is true.
73   /// </summary>
74   protected bool editOnInsert = true;
75  
76   /// <summary>
77   /// Is this property a password?. Default value is false.
78   /// </summary>
79   protected bool isPassword = false;
80  
81   /// <summary>
82   /// Validator collection associated to this property.
83   /// </summary>
84   protected ArrayList validators;
85  
86   /// <summary>
87   /// Represent the style or kind of control that will show this item.
88   /// </summary>
89   protected ItemStyle itemStyle = ItemStyle.None;
90  
91   /// <summary>
92   /// Translator associated to this item.
93   /// </summary>
94   protected ITranslator translator;
95  
96   #endregion
97  
98   /// <summary>
99   /// Get a a certain value from an Xml attribute, or assign a default value if
100   /// attribute does not exist.
101   /// </summary>
102   /// <param name="attribute">Attribute to inspect.</param>
103   /// <param name="defaultValue">Default value to apply if attibute is not
104   /// defined.</param>
105   /// <returns>The Xml attribute value, or the default value.</returns>
106 480 public static string LoadValueFromAttribute(XmlAttribute attribute,
107   string defaultValue)
108   {
109 480 return (attribute == null) ? defaultValue : attribute.Value;
110   }
111  
112   /// <summary>
113   /// Get a a certain value from an Xml attribute, or assign a default value if
114   /// attribute does not exist. In this case, the desired value is a boolean one.
115   /// </summary>
116   /// <param name="attribute">Attribute to inspect.</param>
117   /// <param name="defaultValue">Default value to apply if attibute is not
118   /// defined.</param>
119   /// <returns>The boolean value ot the Xml attribute, or the specified
120   /// default value.
121   /// </returns>
122 590 public static bool LoadValueFromAttribute(XmlAttribute attribute,
123   bool defaultValue)
124   {
125 590 return (attribute == null) ? defaultValue : Boolean.Parse(attribute.Value);
126   }
127  
128   /// <summary>
129   /// Constructor.
130   /// </summary>
131   /// <param name="t">Type to map.</param>
132   /// <param name="propertyNode">Xml node that represents property mapping info.
133   /// </param>
134 147 public XmlPropertyMap(Type t, XmlNode propertyNode)
135   {
136   //Name attribute is mandatory
137 147 this.name = propertyNode.Attributes[
138   XmlMappingFileConst.propertyMappingNameAttr].Value;
139  
140   //If not especified a heading, assign property name value by default.
141 147 this.heading = LoadValueFromAttribute(
142   propertyNode.Attributes[XmlMappingFileConst.propertyHeadingAttr]
143   ,this.name);
144  
145 147 this.readOnly = LoadValueFromAttribute(
146   propertyNode.Attributes[XmlMappingFileConst.propertyReadOnlyAttr]
147   ,this.readOnly);
148  
149 147 this.show = LoadValueFromAttribute(
150   propertyNode.Attributes[XmlMappingFileConst.propertyShowAttr]
151   ,this.show);
152  
153 147 this.editOnInsert = LoadValueFromAttribute(
154   propertyNode.Attributes[XmlMappingFileConst.propertyEditOnInsertAttr]
155   ,this.editOnInsert);
156  
157 147 this.isPassword = LoadValueFromAttribute(
158   propertyNode.Attributes[XmlMappingFileConst.propertyIsPasswordAttr]
159   ,this.isPassword);
160  
161 147 string itemStyleStr = LoadValueFromAttribute(
162   propertyNode.Attributes[XmlMappingFileConst.propertyItemStyleAttr]
163   ,"none");
164  
165 147 string translatorStr = LoadValueFromAttribute(
166   propertyNode.Attributes[XmlMappingFileConst.propertyTranslatorAttr]
167   ,"");
168  
169 147 this.itemStyle = (ItemStyle)Enum.Parse(typeof(ItemStyle),itemStyleStr,true);
170  
171 147 if (translatorStr != "") {
172  
173 0 string[] asmAndType = translatorStr.Split(new char[]{','},2);
174  
175 0 Assembly asm = Assembly.LoadWithPartialName(asmAndType[0]);
176  
177 0 Type trans = asm.GetType(asmAndType[1],false);
178  
179 0 if (trans == null) {
180  
181   string errorMsg = string.Format(
182   "Unable to load translator class '{0}' "
183   + "for property '{1}'.",translatorStr,this.name);
184  
185   log.Fatal(errorMsg);
186   throw new ModelException(errorMsg);
187   }
188  
189 0 this.translator = trans.InvokeMember("",BindingFlags.CreateInstance,
190   null,null,null) as ITranslator;
191   }
192  
193 147 this.propertyInfo = t.GetProperty(this.name);
194  
195 147 if (this.propertyInfo == null) {
196  
197 0 string errorMsg = string.Format(
198   "Cannot load property '{0}' for type '{1}'",
199   this.name,t.UnderlyingSystemType.FullName);
200  
201 0 log.Fatal(errorMsg);
202 0 new ModelException(errorMsg);
203   }
204  
205   //Validators initialization.
206 147 validators = new ArrayList();
207  
208 147 foreach (XmlNode val in propertyNode.ChildNodes) {
209  
210 118 validators.Add(new XmlValidateMap(this.propertyInfo,val));
211   }
212   }
213  
214  
215   #region Public properties
216  
217   /// <summary>
218   /// Reflex the <see cref="propertyInfo"/> field.
219   /// </summary>
220   public PropertyInfo PropertyInfo
221   {
222 11174 get { return this.propertyInfo; }
223 0 set { this.propertyInfo = value; }
224   }
225  
226   /// <summary>
227   /// Reflex the <see cref="name"/> field.
228   /// </summary>
229   public string Name
230   {
231 302 get { return name; }
232 0 set { this.name = value; }
233   }
234  
235   /// <summary>
236   /// Reflex the <see cref="readOnly"/> field.
237   /// </summary>
238   public virtual bool ReadOnly
239   {
240 175 get { return readOnly; }
241 0 set { this.readOnly = value; }
242   }
243  
244   /// <summary>
245   /// Reflex the <see cref="editOnInsert"/> field.
246   /// </summary>
247   public virtual bool EditOnInsert
248   {
249 175 get { return editOnInsert; }
250 0 set { this.editOnInsert = value; }
251   }
252  
253   /// <summary>
254   /// Reflex the <see cref="isPassword"/> field.
255   /// </summary>
256   public virtual bool IsPassword
257   {
258 960 get { return isPassword; }
259 0 set { this.isPassword = value; }
260   }
261  
262   /// <summary>
263   /// Reflex the <see cref="show"/> field.
264   /// </summary>
265   public bool Show
266   {
267 245 get { return show; }
268 0 set { this.show = value; }
269   }
270  
271   /// <summary>
272   /// Reflex the validators associated to this property.
273   /// </summary>
274   public ArrayList Validators
275   {
276 141 get { return this.validators; }
277   }
278  
279   /// <summary>
280   /// See the <see cref="itemStyle"/> field.
281   /// </summary>
282   public ItemStyle ItemStyle
283   {
284 713 get { return this.itemStyle; }
285 0 set { this.itemStyle = value; }
286   }
287  
288   /// <summary>
289   /// See the <see cref="translator"/> field.
290   /// </summary>
291 0 public ITranslator Translator
292   {
293   get { return this.translator; }
294   set { this.translator = value; }
295   }
296  
297   /// <summary>
298   /// See the <see cref="heading"/> field.
299   /// </summary>
300   public string Heading
301   {
302 595 get { return this.heading; }
303 0 set { this.heading = value; }
304   }
305  
306   #endregion
307   }
308   }
309