Added initial documentation tree using doxygen. More tweaks on the license text ensur...
[lwes-dotnet/github-mirror.git] / Org.Lwes / DB / FilePathEventTemplateDB.cs
blobbaaf82192887c483d6864cb75410578c22cca77d
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (cerebralkungfu[at*g mail[dot*com)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.DB
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
26 using Org.Lwes.ESF;
27 using Org.Lwes.Properties;
29 /// <summary>
30 /// Event template database implementation that uses a file path
31 /// and ".esf" files for event definitions.
32 /// </summary>
33 public class FilePathEventTemplateDB : IEventTemplateDB, ITraceable
35 #region Fields
37 const string EsfFileSearchPattern = "*.esf";
39 bool _initialized;
40 Dictionary<string, EventTemplate> _templates = new Dictionary<string, EventTemplate>();
42 #endregion Fields
44 #region Properties
46 /// <summary>
47 /// An enumerable containing the names of defined events.
48 /// </summary>
49 public IEnumerable<string> EventNames
51 get
53 if (!_initialized) throw new InvalidOperationException(Resources.Error_NotYetInitialized);
55 return new List<string>(_templates.Keys);
59 /// <summary>
60 /// Indicates whether the template db has been initialized.
61 /// </summary>
62 public bool IsInitialized
64 get { return _initialized; }
67 #endregion Properties
69 #region Methods
71 /// <summary>
72 /// Gets the named event template.
73 /// </summary>
74 /// <param name="evName">the event name</param>
75 /// <returns>the event template for the named event</returns>
76 /// <exception cref="ArgumentOutOfRangeException">thrown if the event is not defined in the DB</exception>
77 public EventTemplate GetEventTemplate(string evName)
79 if (!_initialized) throw new InvalidOperationException(Resources.Error_NotYetInitialized);
80 if (evName == null) throw new ArgumentNullException("evName");
81 if (evName.Length == 0) throw new ArgumentException(Resources.Error_EmptyStringNotAllowed, "evName");
83 return _templates[evName];
86 /// <summary>
87 /// Initilizes the template db by reading ESF files at <paramref name="filePath"/>.
88 /// </summary>
89 /// <param name="filePath">a path containing ESF (*.esf) files</param>
90 /// <param name="includeSubdirectories">indicates whether subdirectories should be included
91 /// in the search for esf files</param>
92 /// <exception cref="InvalidOperationException">thrown if the template db has already been initalized</exception>
93 /// <exception cref="ArgumentNullException">thrown if <paramref name="filePath"/> is null</exception>
94 public void InitializeFromFilePath(string filePath, bool includeSubdirectories)
96 if (_initialized) throw new InvalidOperationException("Already initialized");
97 if (filePath == null) throw new ArgumentNullException("filePath");
98 if (!Directory.Exists(filePath))
99 throw new IOException(String.Concat("Directory does not exist: ", filePath));
101 EsfParser parser = new EsfParser();
102 SearchOption option = (includeSubdirectories) ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
103 foreach (var fn in Directory.GetFiles(filePath, EsfFileSearchPattern, option))
105 using (FileStream fs = File.Open(fn, FileMode.Open))
107 var templates = parser.ParseEventTemplates(fs);
108 foreach (var evt in templates)
110 if (_templates.ContainsKey(evt.Name))
112 // There is already a template by the same name: warn about it
113 this.TraceWarning(Resources.Warning_DuplicateEventTemplateFromESF, evt.Name, fn);
114 _templates[evt.Name] = evt;
116 else
117 _templates.Add(evt.Name, evt);
121 _initialized = true;
124 /// <summary>
125 /// Initializes the template db by reading ESF templates from the <paramref name="esfStream"/> (NOT IMPLEMENTED).
126 /// </summary>
127 /// <param name="esfStream">a stream containing ESF template definitions</param>
128 /// <exception cref="NotImplementedException">thrown by this implementation because the method is
129 /// not implemented.</exception>
130 public void InitializeFromStream(Stream esfStream)
132 throw new NotImplementedException();
135 /// <summary>
136 /// Checks to see if a named event template exists.
137 /// </summary>
138 /// <param name="eventName">the event name</param>
139 /// <returns><em>true</em> if the template with the <paramref name="eventName"/> exists;
140 /// otherwise <em>false</em>.</returns>
141 public bool TemplateExists(string eventName)
143 return (_initialized && _templates.ContainsKey(eventName));
146 /// <summary>
147 /// Tries to create the named event.
148 /// </summary>
149 /// <param name="evName">event name</param>
150 /// <param name="ev">reference to a variable that contains the event upon success</param>
151 /// <param name="performValidation">whether the event should perform validation</param>
152 /// <param name="enc">the encoding used for the event</param>
153 /// <returns><em>true</em> if the named event is created; otherwise <em>false</em></returns>
154 public bool TryCreateEvent(string evName, out Event ev, bool performValidation, SupportedEncoding enc)
156 EventTemplate template;
157 if (TryGetEventTemplate(evName, out template))
159 ev = new Event(template, performValidation, enc);
160 return true;
162 ev = default(Event);
163 return false;
166 /// <summary>
167 /// Tries to get the named event template.
168 /// </summary>
169 /// <param name="evName">the event's name</param>
170 /// <param name="template">reference to a variable that will contain the template upon success</param>
171 /// <returns><em>true</em> if the named event is retreived; otherwise <em>false</em></returns>
172 public bool TryGetEventTemplate(string evName, out EventTemplate template)
174 if (!_initialized) throw new InvalidOperationException(Resources.Error_NotYetInitialized);
175 if (evName == null) throw new ArgumentNullException("evName");
176 if (evName.Length == 0) throw new ArgumentException(Resources.Error_EmptyStringNotAllowed, "evName");
178 return _templates.TryGetValue(evName, out template);
181 #endregion Methods