Clover.NET coverage report - Coverage

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

File Stats: LOC: 184   Methods: 7
NCLOC: 95 Classes: 1
 
Source File Conditionals Statements Methods TOTAL
Session\XmlSession.cs 83,3 % 87,1 % 100,0 % 88,0 %
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.IO;
26   using System.Reflection;
27   using System.Collections;
28   using System.Text.RegularExpressions;
29   using System.Xml;
30   using ShowX.Model;
31   using ShowX.Model.XmlModel;
32   using log4net;
33  
34   namespace ShowX.Session
35   {
36   /// <summary>
37   /// Represent a session when mapping is done by using the XML file model.
38   /// </summary>
39   public class XmlSession : ISession
40   {
41   private static readonly ILog log = LogManager.GetLogger(
42   typeof(XmlSession));
43  
44   /// <summary>
45   /// Method caller helper to invoke object handler methods.
46   /// </summary>
47   protected IObjectHandlerMethodCaller activeMethodCaller;
48  
49   /// <summary>
50   /// Mapping handler helper, to retrieve mapping for properies and classes.
51   /// </summary>
52   protected IMappingHandler activeMappingHandler;
53  
54   /// <summary>
55   /// Store information to find XML files to get class mapping. The type is used
56   /// as the hash key, and the value is the path to the XML file.
57   /// </summary>
58   protected Hashtable classXmlFilesMappingInfo;
59  
60   /// <summary>
61   /// Store information about ShowX mapped classes. The type is used as the hash
62   /// key, and the value is an instance of the XMLClassMap class.
63   /// </summary>
64   protected Hashtable classMappingInfo;
65  
66   /// <summary>
67   /// Constructor. Initialize class objects to the appropiate attribute model
68   /// instances.
69   /// </summary>
70 23 public XmlSession()
71   {
72 23 activeMethodCaller = new XmlObjectHandlerMethodCaller();
73 23 activeMappingHandler = new XmlMappingHandler();
74 23 classMappingInfo = new Hashtable();
75   }
76  
77   #region ISession Members
78  
79   /// <summary>
80   /// Expose the method caller class used to invoque object handler methods.
81   /// </summary>
82   public ShowX.Model.IObjectHandlerMethodCaller MethodCaller
83   {
84 142 get
85   {
86 142 return activeMethodCaller;
87   }
88   }
89  
90   /// <summary>
91   /// Expose the mapping handler class used to invoque object handler methods.
92   /// </summary>
93   public ShowX.Model.IMappingHandler MappingHandler
94   {
95 2757 get
96   {
97 2757 return activeMappingHandler;
98   }
99   }
100  
101   #endregion
102  
103   /// <summary>
104   /// Register the ShowX type on current session. Information about the new
105   /// type is stored in the classMappingInfo hash table.
106   /// </summary>
107   /// <param name="t">New type to register in session.</param>
108 29 public void RegisterClass(Type t)
109   {
110   //Attempt to register an already registered class
111 29 if (classMappingInfo[t] != null) return;
112  
113 28 XmlDocument mappingFile = new XmlDocument();
114  
115 28 if (classXmlFilesMappingInfo[t.FullName] != null) {
116   // An explicit mapping file defined. Load file.
117 0 mappingFile.Load(classXmlFilesMappingInfo[t.FullName] as string);
118   }
119   else {
120   //Explicitly load file from embedded resource.
121   //Look for embedded mapping file in the assembly that contains type t.
122 28 Assembly asm = t.UnderlyingSystemType.Assembly;
123  
124 28 string[] resources = asm.GetManifestResourceNames();
125 28 Regex regex = new Regex(t.Name + "\\.xml",RegexOptions.Compiled);
126 28 int index = -1;
127  
128 85 for(int i = 0; i < resources.Length; i++) {
129  
130 84 if (regex.IsMatch(resources[i])) {
131  
132 27 index = i;
133 27 break;
134   }
135   }
136  
137 28 if (index == -1) throw new SessionException(
138   string.Format("Unable to load (embedded) mapping file '{0}.xml' "
139   + " for type '{1}' in assembly '{2}'",t.Name,
140   t.FullName,asm.FullName));
141  
142 27 Stream xmlFile = asm.GetManifestResourceStream(
143   resources[index]);
144  
145 27 XmlTextReader xtr = new XmlTextReader(xmlFile);
146 27 mappingFile.Load(xtr);
147   }
148  
149 27 classMappingInfo.Add(t,new XmlClassMap(t,mappingFile));
150   }
151  
152   /// <summary>
153   /// Read-only Indexer over registered ShowX types.
154   /// </summary>
155   public XmlClassMap this [Type t]
156   {
157 5479 get
158   {
159 5479 return classMappingInfo[t] as XmlClassMap;
160   }
161   }
162  
163   /// <summary>
164   /// Provide access to the hash of associations type/path-to-XML-mapper-file.
165   /// </summary>
166   public Hashtable XmlMappingFiles
167   {
168 1 get { return this.classXmlFilesMappingInfo; }
169 23 set
170   {
171 23 if (value == null) {
172  
173 0 string errorMsg = "Cannot assign a null Hashtable to "
174   +" property XmlSession.XmlMappingFiles.";
175 0 log.Error(errorMsg);
176 0 throw new SessionException(errorMsg);
177   }
178  
179 23 this.classXmlFilesMappingInfo = value;
180   }
181   }
182   }
183   }
184