using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using System.Management.Automation;
using System.Management.Automation.Provider;
namespace Intel.Management.PSModule
{
///
/// Base definition of an AmtDriveItem
///
class DriveItem
{
public class Comparer : IComparer
{
public int Compare(DriveItem item1, DriveItem item2)
{
return string.Compare(item1.Name, item2.Name, true);
}
}
protected string _name;
protected object _value;
protected DriveItem _parent;
// defines an Empty Container
protected static readonly Comparer _defaultComparer = new Comparer();
protected static readonly string[] _defaultProperties = new string[0];
public DriveItem(String name,string value)
{
_name = DecodeName(name);
_value = value;
_parent = null;
}
public DriveItem(String name,DriveItem parent)
{
_name = DecodeName(name);
_value = string.Empty;
_parent = parent;
}
public DriveItem(String name,object value,DriveItem parent)
{
_name = DecodeName(name);
_value = value;
_parent = parent;
}
public virtual void Clear()
{
}
public void SetParent(DriveItem newParent)
{
_parent = newParent;
}
public virtual object GetReturnObject()
{
return new DirectoryItem(Name, Value, Properties);
}
public DriveItem GetRoot()
{
DriveItem parent = _parent;
while (parent._parent != null)
parent = parent._parent;
return parent;
}
public virtual void RenameItem(string newName)
{
_name =newName;
}
public virtual void SetItem(object values, DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object SetItemDynamicParameters()
{
return null;
}
public virtual void ClearItem(DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object GetProperty(Collection propList, DriveProvider provider)
{
LoadProperties(provider);
return GetProperties(propList);
}
public virtual void SetProperty(PSObject propertyValue, DriveProvider provider)
{
SetProperties(propertyValue);
}
public virtual object ClearProperty(Collection propList, DriveProvider providere)
{
throw new NotSupportedException();
}
public virtual void Invoke(DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object InvokeDefaultActionDynamicParameters()
{
return null;
}
public virtual DriveItem NewItem(string name, string type, object newItem, DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object NewItemDynamicParameters(string itemTypeName, object newItemValue)
{
return null;
}
public virtual void RemoveItem(DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object GetContentWriterDynamicParameters()
{
return null;
}
public virtual IContentWriter GetContentWriter( DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual object GetContentReaderDynamicParameters()
{
return null;
}
public virtual IContentReader GetContentReader(DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual void ClearContent(DriveProvider provider)
{
throw new NotSupportedException();
}
public virtual string[] Properties
{
get
{
return _defaultProperties;
}
}
public virtual string Name
{
get
{
return EncodeName(_name);
}
}
public virtual object Value
{
get { return _value; }
}
public static string DecodeName(string name)
{
if (name.Equals("%00"))
return string.Empty;
return name.Replace("%5C", "\\").Replace("%2F", "/").Replace("%2A","*").Replace("%25", "%");
}
public static string EncodeName(string name)
{
if (name == null)
return string.Empty;
if (name.Equals(string.Empty))
return "%00";
return name.Replace("%", "%25").Replace("/", "%2F").Replace("\\", "%5C").Replace("*","%2A");
}
protected virtual void LoadProperties(DriveProvider provider)
{
}
public virtual bool RemoveChildItem(DriveItem item)
{
return false;
}
public virtual void AddChildItem(DriveItem item)
{
}
protected void SetProperties(PSObject propertyValue)
{
foreach (PSPropertyInfo propInfo in propertyValue.Properties)
{
PropertyInfo info = FindProperty(propInfo.Name, GetType().GetProperties());
info.SetValue(this, propInfo.Value, null);
}
}
protected PropertyInfo FindProperty(string name,PropertyInfo[] props)
{
PropertyInfo result = null;
foreach (PropertyInfo info in props)
{
if (string.Compare(info.Name, name, true) == 0)
{
result = info;
break;
}
}
if (result == null)
throw new PSArgumentException(
string.Format(Intel.Management.PSModule.Properties.Resources.PROPERTY_NOT_FOUND,
name,Name));
return result;
}
protected object GetProperties(Collection propList)
{
object result = null;
if (propList.Count == 0)//write all properties
{
result = new PSObject();
foreach (string propName in Properties)
{
PropertyInfo info= GetType().GetProperty(propName);
((PSObject)result).Properties.Add(
new PSNoteProperty(propName,info.GetValue(this, null)));
}
}
else if (propList.Count == 1) //write the value of the one property
{
PropertyInfo info = FindProperty(propList[0], GetType().GetProperties());
return info.GetValue(this, null);
}
else // write the set of propeties
{
result = new PSObject();
foreach (string propName in propList)
{
PropertyInfo info = FindProperty(propList[0], GetType().GetProperties());
((PSObject)result).Properties.Add(
new PSNoteProperty(propName, info.GetValue(this, null)));
}
}
return result;
}
/* protected object GetProperties(IDictionary props, Collection propList)
{
object result = null;
if (propList.Count == 0)
{
result = new PSObject();
//write all the properties
foreach (KeyValuePair pair in props)
{
((PSObject)result).Properties.Add(
new PSNoteProperty(pair.Key, pair.Value));
}
}
else if (propList.Count == 1)
{
result = props[propList[0]];
}
else
{
result = new PSObject();
foreach (string propName in propList)
{
((PSObject)result).Properties.Add(
new PSNoteProperty(propName, props[propName]));
}
}
return result;
}*/
}
}