1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
6
| This program is free software; you can redistribute it and/or
7
| modify it under the terms of version 2 of the GNU General Public License as
8
| published by the Free Software Foundation.
10
| This program is distributed in the hope that it will be useful,
11
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
| GNU General Public License for more details.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
22
|***************************************************************************/
27
using System.Collections;
29
namespace Simias.Storage.Provider
32
/// Represents a Collection Store Provider record.
34
public class Record : IEnumerable
36
private XmlElement recordEl;
38
/// The Name of the Record.
42
/// The ID of the Record.
46
/// The type of the Record.
51
/// Creates a Record from an XML Element. Can be used to enumerate
52
/// the Properties contained in this record.
54
/// <param name="recordXml">The XML Element that describes the Record.</param>
55
public Record(XmlElement recordXml)
59
// Get the Name, ID, and type.
60
Name = recordEl.GetAttribute(XmlTags.NameAttr);
61
Id = recordEl.GetAttribute(XmlTags.IdAttr);
62
Type = recordEl.GetAttribute(XmlTags.TypeAttr);
64
// Make sure this is a valid record.
65
if (Name == null || Id == null || Type == null)
67
throw new FormatException();
72
/// Creates a Record from the strings.
74
/// <param name="name">The name of the Record</param>
75
/// <param name="id">The ID of the Record.</param>
76
/// <param name="type">The type of the Record.</param>
77
public Record(string name, string id, string type)
86
/// Called to get an XML node for this Record. This node does not include
87
/// the property nodes.
89
/// <param name="doc">The document that the XML node belongs to.</param>
90
/// <returns>The XmlElement representing this Record.</returns>
91
public XmlElement ToXml(XmlDocument doc)
93
recordEl = doc.CreateElement(XmlTags.ObjectTag);
94
recordEl.SetAttribute(XmlTags.NameAttr, Name);
95
recordEl.SetAttribute(XmlTags.IdAttr, Id);
96
recordEl.SetAttribute(XmlTags.TypeAttr, Type);
100
#region IEnumerable Members
103
/// Gets the Property enumerator of this Record.
105
/// <returns></returns>
106
public IEnumerator GetEnumerator()
108
return (new PropertyEnumerator(recordEl.SelectNodes(XmlTags.PropertyTag)));
115
/// Class to enumerate through properties of a record.
117
public class PropertyEnumerator : IEnumerator
119
IEnumerator properties;
122
/// Exposes an XmlNodeList as Properties.
124
/// <param name="pList"></param>
125
public PropertyEnumerator(XmlNodeList pList)
127
properties = pList.GetEnumerator();
130
#region IEnumerator Members
133
/// Sets the enumerator to its initial position, which is before the first Property in the collection.
141
/// Gets the current Property in the collection.
143
public object Current
147
return new Property((XmlElement)properties.Current);
152
/// Advances the enumerator to the next Property in the collection.
154
/// <returns></returns>
155
public bool MoveNext()
157
return properties.MoveNext();
163
#region Property class
166
/// Class to convert a property to and from XML.
168
public class Property
170
private XmlElement property;
189
/// Creates a Property from an XML element.
191
/// <param name="propertyEl">The XML element for this Property.</param>
192
public Property(XmlElement propertyEl)
194
property = propertyEl;
195
Name = property.GetAttribute(XmlTags.NameAttr);
196
Type = property.GetAttribute(XmlTags.TypeAttr);
197
Value = property.InnerXml;
198
Flags = property.GetAttribute(XmlTags.FlagsAttr);
199
if (string.Empty == Flags)
206
/// Called to create an XML node that represents a property.
208
/// <param name="doc">The document the XML node belongs to.</param>
209
/// <param name="Name">The name of the property.</param>
210
/// <param name="Type">The type of the property.</param>
211
/// <param name="Flags">The flags of the property.</param>
212
/// <param name="Value">The value of the property.</param>
213
/// <returns>The created XMLElement.</returns>
214
public static XmlElement CreateXmlNode(XmlDocument doc, string Name, string Type, string Flags, string Value)
216
XmlElement node = doc.CreateElement(XmlTags.PropertyTag);
217
node.SetAttribute(XmlTags.NameAttr, Name);
218
node.SetAttribute(XmlTags.TypeAttr, Type);
219
node.SetAttribute(XmlTags.FlagsAttr, Flags);
220
node.InnerXml = Value;