2007-12-03 16:28:26 +00:00

283 lines
6.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace vlnObjectLibrary
{
[Serializable()]
public class vlnObject : IXmlSerializable , IDisposable
{
#region Log4Net
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
#endregion
public void Dispose()
{
/* See if I need to do anything */
}
#region XML Serialization
public void WriteXml(XmlWriter w)
{
WriteXml(w, true);
}
public void WriteXml(XmlWriter w,bool bSaveParent)
{
w.WriteAttributeString("title", Title);
w.WriteAttributeString("path", Path);
w.WriteAttributeString("type", Type);
if (m_PropArray.Count > 0)
{
w.WriteStartElement("properties");
foreach (string sKey in m_PropArray.Keys)
{
w.WriteStartElement("property");
w.WriteAttributeString("name", sKey);
w.WriteAttributeString("value", PropArray[sKey]);
w.WriteEndElement();
}
w.WriteEndElement();
}
if (bSaveParent && Parent != null)
{
w.WriteStartElement("parent");
Parent.WriteXml(w);
w.WriteEndElement();
}
}
private bool SkipToElement(XmlReader xr)
{
while (xr.Read())
if (xr.NodeType == XmlNodeType.Element || xr.NodeType == XmlNodeType.EndElement)
return xr.NodeType == XmlNodeType.Element;
return false;
}
public void ReadXml(XmlReader xr)
{
SkipToElement(xr);
if (xr.MoveToAttribute("title")) m_Title = xr.Value;
if (xr.MoveToAttribute("type")) m_Type = xr.Value;
if (xr.MoveToAttribute("path")) m_Path = xr.Value;
while (SkipToElement(xr))
{
if (xr.NodeType == XmlNodeType.Element && xr.Name == "properties")
while (SkipToElement(xr))
if (xr.NodeType == XmlNodeType.Element && xr.Name == "property")
AddProp(xr.ReadSubtree());
if (xr.NodeType == XmlNodeType.Element && xr.Name == "parent")
m_Parent = new vlnObject(xr.ReadSubtree());
}
xr.Close();
}
public XmlSchema GetSchema()
{
return (null);
}
#endregion
#region Constructors
public vlnObject(vlnObject parent, string type, string title, string path)
{
m_Parent = parent;
m_Type = type;
m_Title = title;
m_Path = path;
m_PropArray = new Dictionary<string,string>();
}
public vlnObject()
{
m_Parent = null;
m_Title = null;
m_Path = null;
m_Type = null;
m_PropArray = new Dictionary<string, string>();
}
public vlnObject(XmlReader xr)
{
m_Parent = null;
m_PropArray = new Dictionary<string, string>();
this.ReadXml(xr);
}
public vlnObject(string sXML)
{
m_Parent = null;
m_PropArray = new Dictionary<string, string>();
this.ReadXml(new XmlTextReader(new StringReader(sXML)));
}
#endregion
#region Public Properties
private vlnObject m_Parent;
public vlnObject Parent
{
get { return m_Parent; }
set { m_Parent = value; }
}
private string m_Title;
public string Title
{
get { return m_Title; }
set { m_Title = value; }
}
private string m_Path;
public string Path
{
get { return m_Path; }
set { m_Path = value; }
}
private string m_Type;
public string Type
{
get { return m_Type; }
set { m_Type = value; }
}
private Dictionary<string,string> m_PropArray;
public Dictionary<string, string> PropArray
{
get { return m_PropArray; }
set { m_PropArray = value; }
}
#endregion
#region Public Methods
public vlnObject Add(string type, string title, string path)
{
vlnObject b = new vlnObject(this, type, title, path);
return b;
}
public void AddProp(string name, string value)
{
PropArray[name.ToUpper()] = value;
}
public void AddProp(XmlReader xr)
{
SkipToElement(xr);
string sName = null;
string sValue = null;
if (xr.MoveToAttribute("name")) sName = xr.Value;
if (xr.MoveToAttribute("value")) sValue = xr.Value;
if (sName != null && sValue != null) AddProp(sName, sValue);
xr.Close();
}
public override string ToString()
{
StringWriter swxml = new StringWriter();
XmlSerializer mySer = new XmlSerializer(this.GetType());
mySer.Serialize(swxml, this);
return swxml.ToString();
}
#endregion
private List<vlnObject> m_Children;
public List<vlnObject> Children
{
get
{
return m_Children;
}
set
{
m_Children = value;
}
}
public void LoadChildren(string sXml)
{
LoadChildren(new XmlTextReader(new StringReader(sXml)));
}
public void LoadChildren(XmlReader xr)
{
m_Children = new List<vlnObject>();
SkipToElement(xr);
while(SkipToElement(xr)){
vlnObject o = new vlnObject(xr.ReadSubtree());
o.Parent = this;
m_Children.Add(o);
}
}
public string AllChildren()
{
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xw.WriteStartElement("children");
foreach (vlnObject vb in Children)
{
xw.WriteStartElement("vlnObject");
vb.WriteXml(xw,false);
xw.WriteEndElement();
}
xw.WriteEndElement();
return sw.ToString();
}
public string ProcessString(string s)
{
//string retval=s;
//string retval = Regex.Replace(s, "{[.]*}", new MatchEvaluator(ReplaceKey));
string retval = Regex.Replace(s, "{{.*?}}", new MatchEvaluator(ReplaceRow));
retval = Regex.Replace(retval, "{.*?}", new MatchEvaluator(ReplaceDrv));
return retval;
}
private string ReplaceRow(Match m)
{
string sResult = FindField(m.Value.Substring(2, m.Value.Length - 4));
if (sResult == null && Parent != null)
{
sResult = Parent.ReplaceRow(m);
}
if (sResult == null)
{
log.ErrorFormat("vlnObject - Couldn't find {0}", m.Value);
}
return sResult;
}
private string ReplaceDrv(Match m)
{
string sRetVal = FindField(m.Value.Substring(1, m.Value.Length - 2));
if (sRetVal == null) sRetVal = "";
return sRetVal;
}
private string FindField(string s)
{
string retval = "";
string[] parts = s.ToUpper().Split(":".ToCharArray());
switch (s.ToUpper())
{
case "PATH":
retval = m_Path;
break;
//case "TITLE":
//retval = m_Title;
//break;
case "TYPE":
retval = m_Type;
break;
default:
if (PropArray.ContainsKey(parts[0]))
{
retval = PropArray[parts[0]].ToString();
}
else
{
return null;
}
break;
}
if (parts.Length > 1)
{
int width = Convert.ToInt32(parts[1]);
if (parts[1][0] == ' ' || parts[1][0] == '0')
{
retval = retval.PadLeft(width, parts[1][0]);
}
else
{
retval = retval.PadRight(width).Substring(0, width).Trim();
}
}
return retval;
}
}
}