204 lines
5.7 KiB
C#
204 lines
5.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Runtime.Serialization;
|
|
using System.Xml;
|
|
|
|
namespace VEPROMS.CSLA.Library
|
|
{
|
|
public delegate string XMLPropertiesEvent(object sender, XMLPropertiesArgs args);
|
|
[Serializable()]
|
|
public class XMLProperties
|
|
{
|
|
#region Events
|
|
public event XMLPropertiesEvent LookInAncestor;
|
|
private string OnLookInAncestor(object sender, XMLPropertiesArgs args)
|
|
{
|
|
if (LookInAncestor != null) return LookInAncestor(sender, args);
|
|
return string.Empty;
|
|
}
|
|
#endregion
|
|
private bool _AncestorLookup=false;
|
|
|
|
public bool AncestorLookup
|
|
{
|
|
get { return _AncestorLookup; }
|
|
set { _AncestorLookup = value; }
|
|
}
|
|
|
|
#region Constructors
|
|
public XMLProperties()
|
|
{
|
|
_XmlContents = new XmlDocument();
|
|
_XmlContents.LoadXml("<Config/>");
|
|
}
|
|
public XMLProperties(string xml)
|
|
{
|
|
_XmlContents = new XmlDocument();
|
|
_XmlContents.LoadXml(xml);
|
|
}
|
|
#endregion
|
|
#region BusinessMethods
|
|
[NonSerialized]
|
|
private bool _ParentLookup=false;
|
|
public bool ParentLookup
|
|
{
|
|
get { return _ParentLookup; }
|
|
set { _ParentLookup = value; }
|
|
}
|
|
[NonSerialized]
|
|
XmlDocument _XmlContents;
|
|
public XmlDocument XmlContents
|
|
{
|
|
get { return _XmlContents; }
|
|
}
|
|
private XmlNode GetGroup(string group)
|
|
{
|
|
XmlNodeList xl = _XmlContents.DocumentElement.SelectNodes(string.Format("//{0}", group));
|
|
if(xl.Count == 0 && System.Text.RegularExpressions.Regex.IsMatch(group,"^[A-Za-z0-9]+$"))
|
|
xl = _XmlContents.DocumentElement.SelectNodes(
|
|
string.Format("//*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{0}']", group.ToLower()));
|
|
switch (xl.Count)
|
|
{
|
|
case 0: // No nodes found
|
|
return null;
|
|
case 1: // Found the node
|
|
XmlNode xn = xl[0];
|
|
if (xn.GetType() == typeof(XmlElement)) return xn;
|
|
throw new XmlPropertiesException("Retrieved {0} while looking for XmlElement //{1}", xn.GetType().ToString(), group);
|
|
default: // Found more than 1 node
|
|
throw new XmlPropertiesException("Found more than one node //{0}", group);
|
|
}
|
|
}
|
|
private XmlAttribute GetItem(XmlNode xx, string item)
|
|
{
|
|
//XmlNodeList xl = xx.SelectNodes(string.Format("@{0}", item));
|
|
XmlNodeList xl = xx.SelectNodes(
|
|
string.Format("@*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{0}']", item.ToLower()));
|
|
switch (xl.Count)
|
|
{
|
|
case 0: // No nodes found
|
|
return null;
|
|
case 1: // Found the node
|
|
XmlNode xn = xl[0];
|
|
if (xn.GetType() == typeof(XmlAttribute)) return (XmlAttribute)xn;
|
|
throw new XmlPropertiesException("Retrieved {0} while looking for XmlAttribute @{1}", xn.GetType().ToString(), item);
|
|
default: // Found more than 1 node
|
|
throw new XmlPropertiesException("Found more than one node @{0}", item);
|
|
}
|
|
}
|
|
public string ParentValue(string group, string item)
|
|
{
|
|
return OnLookInAncestor(this, new XMLPropertiesArgs(group, item, true));
|
|
}
|
|
public string this[string group, string item]
|
|
{
|
|
get
|
|
{
|
|
if (_ParentLookup)
|
|
return OnLookInAncestor(this, new XMLPropertiesArgs(group, item, AncestorLookup));
|
|
XmlNode xn = GetGroup(group);
|
|
if (xn == null) return OnLookInAncestor(this, new XMLPropertiesArgs(group, item, AncestorLookup));
|
|
XmlNode xa = GetItem(xn, item);
|
|
if (xa == null) return OnLookInAncestor(this, new XMLPropertiesArgs(group, item, AncestorLookup));
|
|
return xa.InnerText;
|
|
}
|
|
set
|
|
{
|
|
if (value == null) value = string.Empty;
|
|
XmlNode xn = GetGroup(group);
|
|
if (xn == null)
|
|
{
|
|
if (value != string.Empty)// Add Group and Item
|
|
{
|
|
xn = _XmlContents.DocumentElement.AppendChild(_XmlContents.CreateElement(group));
|
|
XmlAttribute xa = xn.Attributes.Append(_XmlContents.CreateAttribute(item));
|
|
xa.Value = value;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
XmlAttribute xa = GetItem(xn, item);
|
|
if (xa == null)
|
|
{
|
|
if (value != string.Empty) // Add Item
|
|
{
|
|
xa = xn.Attributes.Append(_XmlContents.CreateAttribute(item));
|
|
xa.Value = value;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (value != string.Empty) // Add Item
|
|
{
|
|
xa.Value = value;
|
|
}
|
|
else
|
|
{
|
|
xn.Attributes.Remove(xa);
|
|
if (xn.Attributes.Count == 0)
|
|
_XmlContents.DocumentElement.RemoveChild(xn);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public override string ToString()
|
|
{
|
|
if (XmlContents == null) return null;
|
|
return XmlContents.OuterXml;
|
|
}
|
|
#endregion
|
|
#region XmlPropertiesException
|
|
public class XmlPropertiesException : Exception
|
|
{
|
|
public XmlPropertiesException() : base() { ;}
|
|
public XmlPropertiesException(string message) : base(message) { ;}
|
|
public XmlPropertiesException(string messageFormat,params object [] args) : base(string.Format(messageFormat,args)) { ;}
|
|
public XmlPropertiesException(SerializationInfo info, StreamingContext context) : base(info, context) { ;}
|
|
public XmlPropertiesException(string message,Exception innerException) : base(message, innerException) { ;}
|
|
}
|
|
#endregion
|
|
}
|
|
public partial class XMLPropertiesArgs
|
|
{
|
|
#region Business Methods
|
|
private string _Group;
|
|
public string Group
|
|
{
|
|
get { return _Group; }
|
|
set { _Group = value; }
|
|
}
|
|
private string _Item;
|
|
public string Item
|
|
{
|
|
get { return _Item; }
|
|
set { _Item = value; }
|
|
}
|
|
private bool _AncestorLookup;
|
|
|
|
public bool AncestorLookup
|
|
{
|
|
get { return _AncestorLookup; }
|
|
set { _AncestorLookup = value; }
|
|
}
|
|
|
|
#endregion
|
|
#region Factory Methods
|
|
private XMLPropertiesArgs() { ;}
|
|
public XMLPropertiesArgs(string group, string item)
|
|
{
|
|
_Group=group;
|
|
_Item=item;
|
|
_AncestorLookup = false;
|
|
}
|
|
public XMLPropertiesArgs(string group, string item, bool ancestorLookup)
|
|
{
|
|
_Group = group;
|
|
_Item = item;
|
|
_AncestorLookup = ancestorLookup;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|