372 lines
11 KiB
C#
372 lines
11 KiB
C#
//----------------------------------------------------------------------------
|
|
//
|
|
// 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
|
|
{
|
|
/// <summary>
|
|
/// Class that contains the data of CIM objects
|
|
/// </summary>
|
|
public class CimData : ICimData
|
|
{
|
|
#region DATA_MEMBERS
|
|
|
|
/// <summary>
|
|
/// CIM XML document
|
|
/// </summary>
|
|
protected XmlDocument XmlMembers { get; set; }
|
|
|
|
/// <summary>
|
|
/// CIM XML Namepsace URI
|
|
/// </summary>
|
|
protected string _xmlNamespace { get; set; }
|
|
|
|
|
|
#endregion DATA_MEMBERS
|
|
|
|
#region CONSTRUCTOR_FUNCTIONS
|
|
/// <summary>
|
|
/// Constructor
|
|
/// </summary>
|
|
/// <param name="className">name of the class</param>
|
|
/// <param name="nameSpace">nameSpace of the class</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Copy constructor
|
|
/// </summary>
|
|
/// <param name="other"></param>
|
|
public CimData(CimData other)
|
|
{
|
|
if (other == null)
|
|
throw new ArgumentNullException("other");
|
|
XmlMembers = (XmlDocument)other.XmlMembers.Clone();
|
|
_xmlNamespace = other._xmlNamespace;
|
|
}
|
|
|
|
#endregion CONSTRUCTOR_FUNCTIONS
|
|
|
|
|
|
/// <summary>
|
|
/// Copy the CIM data
|
|
/// </summary>
|
|
/// <param name="other">CIM data to be copied</param>
|
|
public void Copy(ICimData other)
|
|
{
|
|
if (other == null)
|
|
throw new ArgumentNullException("other");
|
|
|
|
CimData t = (CimData)other;
|
|
XmlMembers = t.XmlMembers;
|
|
_xmlNamespace = t._xmlNamespace;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize the CIM object to XML
|
|
/// </summary>
|
|
/// <returns>XML representing the CIM object</returns>
|
|
public string Serialize()
|
|
{
|
|
return XmlMembers.OuterXml;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Serialize the CIM object to XML
|
|
/// </summary>
|
|
/// <returns>Inner XML representing the CIM object</returns>
|
|
public string SerializeInner()
|
|
{
|
|
return XmlMembers.FirstChild.InnerXml;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Deserialize the CIM object from XML
|
|
/// </summary>
|
|
/// <param name="xml">XML representing the CIM object</param>
|
|
public void Deserialize(string xml)
|
|
{
|
|
try
|
|
{
|
|
XmlMembers.LoadXml(xml);
|
|
}
|
|
catch (XmlException)
|
|
{
|
|
throw new CimException("Invalid input: xml string");
|
|
}
|
|
_xmlNamespace = XmlMembers.DocumentElement.NamespaceURI;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the class name from a namespace string
|
|
/// </summary>
|
|
/// <param name="ns">Namespace</param>
|
|
/// <returns>Class name</returns>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the namespace of the current CIM object
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Checks to see if an array contains a null value
|
|
/// </summary>
|
|
/// <param name="values">Array to be checked</param>
|
|
/// <returns>True if the array contains a null value, otherwise false</returns>
|
|
protected bool containNullValue(string[] values)
|
|
{
|
|
bool hasNullValue = false;
|
|
foreach (string value in values)
|
|
{
|
|
if (value == null)
|
|
{
|
|
hasNullValue = true;
|
|
break;
|
|
}
|
|
}
|
|
return hasNullValue;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Create the XML data structure
|
|
/// </summary>
|
|
/// <param name="className">Name of the class (the outer tag of the XML)</param>
|
|
/// <param name="nameSpace">namesapce of the class</param>
|
|
private void CreateStore(string className, string nameSpace)
|
|
{
|
|
XmlElement root = XmlMembers.DocumentElement;
|
|
root = XmlMembers.CreateElement(className, nameSpace);
|
|
XmlMembers.AppendChild(root);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Add a new CIM Field
|
|
/// </summary>
|
|
/// <param name="name">Field name</param>
|
|
/// <param name="values">Field values</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Attempts to get a CIM Field
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <param name="values">Field values</param>
|
|
/// <returns>True if the field exists, otherwise false</returns>
|
|
public bool TryGetValue(string name, out string[] values)
|
|
{
|
|
if (ContainsField(name))
|
|
{
|
|
values = GetField(name);
|
|
return true;
|
|
}
|
|
|
|
values = null;
|
|
return false;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Removes a CIM Field
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
public void RemoveField(string name)
|
|
{
|
|
if (!ContainsField(name))
|
|
return;
|
|
XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, _xmlNamespace);
|
|
|
|
List<XmlNode> copyList = new List<XmlNode>();
|
|
foreach (XmlNode node in elemList)
|
|
{
|
|
copyList.Add(node);
|
|
}
|
|
|
|
foreach (XmlNode node in copyList)
|
|
{
|
|
XmlMembers.DocumentElement.RemoveChild(node);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if a given Field exists in the current CIM object
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <returns>True if the given field exists in the CIM object, otherwise false</returns>
|
|
public bool ContainsField(string name)
|
|
{
|
|
if (name == null)
|
|
throw new ArgumentNullException("name");
|
|
XmlNodeList elemList = XmlMembers.GetElementsByTagName(name, _xmlNamespace);
|
|
return (elemList.Count != 0);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get/Set CIM Field
|
|
/// </summary>
|
|
/// <param name="name">CIM Field Name</param>
|
|
/// <returns>Array of the field's values</returns>
|
|
public string[] this[string name]
|
|
{
|
|
get
|
|
{
|
|
return GetField(name);
|
|
}
|
|
|
|
set
|
|
{
|
|
SetField(name, value);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets a CIM Field
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <returns>Array of the field's values</returns>
|
|
public string[] GetField(string name)
|
|
{
|
|
|
|
List<string> resList = new List<string>();
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Sets a CIM Field's values
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <param name="values">Field values</param>
|
|
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);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets or adds a CIM Field
|
|
/// If the field does not exist, the function adds it, otherwise the function sets its values.
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <param name="values">Field values</param>
|
|
public virtual void SetOrAddField(string name, string[] values)
|
|
{
|
|
if (!ContainsField(name))
|
|
{
|
|
AddField(name, values);
|
|
return;
|
|
}
|
|
SetField(name, values);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets or adds a CIM Field
|
|
/// If the field does not exist, the function adds it, otherwise the function sets its value.
|
|
/// </summary>
|
|
/// <param name="name">CIM Field name</param>
|
|
/// <param name="value">Field value</param>
|
|
public virtual void SetOrAddField(string name, string value)
|
|
{
|
|
SetOrAddField(name, new string[1] { value });
|
|
}
|
|
}
|
|
|
|
} |