//---------------------------------------------------------------------------- // // Copyright (c) Intel Corporation, 2008 - 2009 All Rights Reserved. // // File: CimData.cs // // Contents: CimData is a part of the CimFramework project. // It contains the implementation of the class representing the underlying CIM data. // //---------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Xml; using System.Xml.Serialization; using Intel.Manageability.Exceptions; namespace Intel.Manageability.Cim.Untyped { /// /// Class that contains the data of CIM objects /// public class CimData : ICimData { #region DATA_MEMBERS /// /// CIM XML document /// protected XmlDocument XmlMembers { get; set; } /// /// CIM XML Namepsace URI /// protected string _xmlNamespace { get; set; } #endregion DATA_MEMBERS #region CONSTRUCTOR_FUNCTIONS /// /// Constructor /// /// name of the class /// nameSpace of the class public CimData(string className, string nameSpace) { if (className == null) throw new ArgumentNullException("className"); if (nameSpace == null) throw new ArgumentNullException("nameSpace"); XmlMembers = new XmlDocument(); _xmlNamespace = nameSpace; CreateStore(className, nameSpace); } /// /// Copy constructor /// /// public CimData(CimData other) { if (other == null) throw new ArgumentNullException("other"); XmlMembers = (XmlDocument)other.XmlMembers.Clone(); _xmlNamespace = other._xmlNamespace; } #endregion CONSTRUCTOR_FUNCTIONS /// /// Copy the CIM data /// /// CIM data to be copied public void Copy(ICimData other) { if (other == null) throw new ArgumentNullException("other"); CimData t = (CimData)other; XmlMembers = t.XmlMembers; _xmlNamespace = t._xmlNamespace; } /// /// Serialize the CIM object to XML /// /// XML representing the CIM object public string Serialize() { return XmlMembers.OuterXml; } /// /// Serialize the CIM object to XML /// /// Inner XML representing the CIM object public string SerializeInner() { return XmlMembers.FirstChild.InnerXml; } /// /// Deserialize the CIM object from XML /// /// XML representing the CIM object public void Deserialize(string xml) { try { XmlMembers.LoadXml(xml); } catch (XmlException) { throw new CimException("Invalid input: xml string"); } _xmlNamespace = XmlMembers.DocumentElement.NamespaceURI; } /// /// Gets the class name from a namespace string /// /// Namespace /// Class name public static string GetClassNameFromNamespace(string ns) { string res = null; if (ns != null) { int inx = ns.LastIndexOf('/'); if (inx == -1) { return null; } return ns.Substring(inx + 1); } return res; } /// /// Gets the namespace of the current CIM object /// /// private string GetSelfNamespace() { object[] attrs = this.GetType().GetCustomAttributes(typeof(XmlRootAttribute), true); if (attrs != null && attrs.Length > 0) { return (attrs[0] as XmlRootAttribute).Namespace; } return null; } /// /// Checks to see if an array contains a null value /// /// Array to be checked /// True if the array contains a null value, otherwise false protected bool containNullValue(string[] values) { bool hasNullValue = false; foreach (string value in values) { if (value == null) { hasNullValue = true; break; } } return hasNullValue; } /// /// Create the XML data structure /// /// Name of the class (the outer tag of the XML) /// namesapce of the class private void CreateStore(string className, string nameSpace) { XmlElement root = XmlMembers.DocumentElement; root = XmlMembers.CreateElement(className, nameSpace); XmlMembers.AppendChild(root); } /// /// Add a new CIM Field /// /// Field name /// Field values public virtual void AddField(string name, string[] values) { if (values == null || containNullValue(values)) throw new ArgumentNullException("values"); if (ContainsField(name)) throw (new CimException(string.Format("AddField: Field \"{0}\" already exists", name))); XmlElement root = XmlMembers.DocumentElement; foreach (string val in values) { //Create a new node. XmlElement elem = XmlMembers.CreateElement(name, _xmlNamespace); //elem.InnerText = val; elem.InnerXml = val; root.AppendChild(elem); } } /// /// Attempts to get a CIM Field /// /// CIM Field name /// Field values /// True if the field exists, otherwise false public bool TryGetValue(string name, out string[] values) { if (ContainsField(name)) { values = GetField(name); return true; } values = null; return false; } /// /// Removes a CIM Field /// /// CIM Field name public void RemoveField(string name) { if (!ContainsField(name)) return; XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, _xmlNamespace); List copyList = new List(); foreach (XmlNode node in elemList) { copyList.Add(node); } foreach (XmlNode node in copyList) { XmlMembers.DocumentElement.RemoveChild(node); } } /// /// Checks if a given Field exists in the current CIM object /// /// CIM Field name /// True if the given field exists in the CIM object, otherwise false public bool ContainsField(string name) { if (name == null) throw new ArgumentNullException("name"); XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, _xmlNamespace); return (elemList.Count != 0); } /// /// Get/Set CIM Field /// /// CIM Field Name /// Array of the field's values public string[] this[string name] { get { return GetField(name); } set { SetField(name, value); } } /// /// Gets a CIM Field /// /// CIM Field name /// Array of the field's values public string[] GetField(string name) { List resList = new List(); if (!ContainsField(name)) throw (new CimPropertyException("no such field name: " + name)); XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, _xmlNamespace); foreach (XmlNode xml in elemList) { resList.Add(xml.InnerXml); } return resList.ToArray(); //elemList[0].InnerXml; } /// /// Sets a CIM Field's values /// /// CIM Field name /// Field values public virtual void SetField(string name, string[] values) { if (values == null || containNullValue(values)) throw new ArgumentNullException("values"); if (!ContainsField(name)) throw (new CimException("SetField: no such field name: " + name)); RemoveField(name); AddField(name, values); } /// /// Sets or adds a CIM Field /// If the field does not exist, the function adds it, otherwise the function sets its values. /// /// CIM Field name /// Field values public virtual void SetOrAddField(string name, string[] values) { if (!ContainsField(name)) { AddField(name, values); return; } SetField(name, values); } /// /// Sets or adds a CIM Field /// If the field does not exist, the function adds it, otherwise the function sets its value. /// /// CIM Field name /// Field value public virtual void SetOrAddField(string name, string value) { SetOrAddField(name, new string[1] { value }); } } }